blob: 68158eb233b30cb2d768d469ea267dc487fce2b3 [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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020045#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020064#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020068#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
Willy Tarreau522d6c02009-12-06 18:49:18 +010070const char HTTP_100[] =
71 "HTTP/1.1 100 Continue\r\n\r\n";
72
73const struct chunk http_100_chunk = {
74 .str = (char *)&HTTP_100,
75 .len = sizeof(HTTP_100)-1
76};
77
Willy Tarreaua9679ac2010-01-03 17:32:57 +010078/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
Willy Tarreau0f772532006-12-23 20:51:41 +010084const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010088 "Location: "; /* not terminated since it will be concatenated with the URL */
89
90/* same as 302 except that the browser MUST retry with the GET method */
91const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040097
98/* same as 302 except that the browser MUST retry with the same method */
99const char *HTTP_307 =
100 "HTTP/1.1 307 Temporary Redirect\r\n"
101 "Cache-Control: no-cache\r\n"
102 "Content-length: 0\r\n"
103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
105/* same as 301 except that the browser MUST retry with the same method */
106const char *HTTP_308 =
107 "HTTP/1.1 308 Permanent Redirect\r\n"
108 "Content-length: 0\r\n"
109 "Location: "; /* not terminated since it will be concatenated with the URL */
110
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
112const char *HTTP_401_fmt =
113 "HTTP/1.0 401 Unauthorized\r\n"
114 "Cache-Control: no-cache\r\n"
115 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200116 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
118 "\r\n"
119 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
120
Willy Tarreau844a7e72010-01-31 21:46:18 +0100121const char *HTTP_407_fmt =
122 "HTTP/1.0 407 Unauthorized\r\n"
123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
127 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800128 "<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 +0100129
Willy Tarreau0f772532006-12-23 20:51:41 +0100130
131const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200132 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] = 400,
134 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400135 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400137 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_500] = 500,
139 [HTTP_ERR_502] = 502,
140 [HTTP_ERR_503] = 503,
141 [HTTP_ERR_504] = 504,
142};
143
Willy Tarreau80587432006-12-24 17:47:20 +0100144static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200145 [HTTP_ERR_200] =
146 "HTTP/1.0 200 OK\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
152
Willy Tarreau0f772532006-12-23 20:51:41 +0100153 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100154 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
160
161 [HTTP_ERR_403] =
162 "HTTP/1.0 403 Forbidden\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
168
CJ Ess108b1dd2015-04-07 12:03:37 -0400169 [HTTP_ERR_405] =
170 "HTTP/1.0 405 Method Not Allowed\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<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",
176
Willy Tarreau0f772532006-12-23 20:51:41 +0100177 [HTTP_ERR_408] =
178 "HTTP/1.0 408 Request Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
184
CJ Ess108b1dd2015-04-07 12:03:37 -0400185 [HTTP_ERR_429] =
186 "HTTP/1.0 429 Too Many Requests\r\n"
187 "Cache-Control: no-cache\r\n"
188 "Connection: close\r\n"
189 "Content-Type: text/html\r\n"
190 "\r\n"
191 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
192
Willy Tarreau0f772532006-12-23 20:51:41 +0100193 [HTTP_ERR_500] =
194 "HTTP/1.0 500 Server Error\r\n"
195 "Cache-Control: no-cache\r\n"
196 "Connection: close\r\n"
197 "Content-Type: text/html\r\n"
198 "\r\n"
199 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
200
201 [HTTP_ERR_502] =
202 "HTTP/1.0 502 Bad Gateway\r\n"
203 "Cache-Control: no-cache\r\n"
204 "Connection: close\r\n"
205 "Content-Type: text/html\r\n"
206 "\r\n"
207 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
208
209 [HTTP_ERR_503] =
210 "HTTP/1.0 503 Service Unavailable\r\n"
211 "Cache-Control: no-cache\r\n"
212 "Connection: close\r\n"
213 "Content-Type: text/html\r\n"
214 "\r\n"
215 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
216
217 [HTTP_ERR_504] =
218 "HTTP/1.0 504 Gateway Time-out\r\n"
219 "Cache-Control: no-cache\r\n"
220 "Connection: close\r\n"
221 "Content-Type: text/html\r\n"
222 "\r\n"
223 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
224
225};
226
Cyril Bonté19979e12012-04-04 12:57:21 +0200227/* status codes available for the stats admin page (strictly 4 chars length) */
228const char *stat_status_codes[STAT_STATUS_SIZE] = {
229 [STAT_STATUS_DENY] = "DENY",
230 [STAT_STATUS_DONE] = "DONE",
231 [STAT_STATUS_ERRP] = "ERRP",
232 [STAT_STATUS_EXCD] = "EXCD",
233 [STAT_STATUS_NONE] = "NONE",
234 [STAT_STATUS_PART] = "PART",
235 [STAT_STATUS_UNKN] = "UNKN",
236};
237
238
William Lallemand73025dd2014-04-24 14:38:37 +0200239/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200240struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200241 .list = LIST_HEAD_INIT(http_req_keywords.list)
242};
243
244/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_res_keywords.list)
247};
248
Willy Tarreau80587432006-12-24 17:47:20 +0100249/* We must put the messages here since GCC cannot initialize consts depending
250 * on strlen().
251 */
252struct chunk http_err_chunks[HTTP_ERR_SIZE];
253
Willy Tarreaua890d072013-04-02 12:01:06 +0200254/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
255static struct hdr_ctx static_hdr_ctx;
256
Willy Tarreau42250582007-04-01 01:30:43 +0200257#define FD_SETS_ARE_BITFIELDS
258#ifdef FD_SETS_ARE_BITFIELDS
259/*
260 * This map is used with all the FD_* macros to check whether a particular bit
261 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
262 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
263 * byte should be encoded. Be careful to always pass bytes from 0 to 255
264 * exclusively to the macros.
265 */
266fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
267fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100268fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200269
270#else
271#error "Check if your OS uses bitfields for fd_sets"
272#endif
273
Willy Tarreau87b09662015-04-03 00:22:06 +0200274static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200275
Willy Tarreau80587432006-12-24 17:47:20 +0100276void init_proto_http()
277{
Willy Tarreau42250582007-04-01 01:30:43 +0200278 int i;
279 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100280 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200281
Willy Tarreau80587432006-12-24 17:47:20 +0100282 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
283 if (!http_err_msgs[msg]) {
284 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
285 abort();
286 }
287
288 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
289 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
290 }
Willy Tarreau42250582007-04-01 01:30:43 +0200291
292 /* initialize the log header encoding map : '{|}"#' should be encoded with
293 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
294 * URL encoding only requires '"', '#' to be encoded as well as non-
295 * printable characters above.
296 */
297 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
298 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100299 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200300 for (i = 0; i < 32; i++) {
301 FD_SET(i, hdr_encode_map);
302 FD_SET(i, url_encode_map);
303 }
304 for (i = 127; i < 256; i++) {
305 FD_SET(i, hdr_encode_map);
306 FD_SET(i, url_encode_map);
307 }
308
309 tmp = "\"#{|}";
310 while (*tmp) {
311 FD_SET(*tmp, hdr_encode_map);
312 tmp++;
313 }
314
315 tmp = "\"#";
316 while (*tmp) {
317 FD_SET(*tmp, url_encode_map);
318 tmp++;
319 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200320
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100321 /* initialize the http header encoding map. The draft httpbis define the
322 * header content as:
323 *
324 * HTTP-message = start-line
325 * *( header-field CRLF )
326 * CRLF
327 * [ message-body ]
328 * header-field = field-name ":" OWS field-value OWS
329 * field-value = *( field-content / obs-fold )
330 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
331 * obs-fold = CRLF 1*( SP / HTAB )
332 * field-vchar = VCHAR / obs-text
333 * VCHAR = %x21-7E
334 * obs-text = %x80-FF
335 *
336 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
337 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
338 * "obs-fold" is volontary forgotten because haproxy remove this.
339 */
340 memset(http_encode_map, 0, sizeof(http_encode_map));
341 for (i = 0x00; i <= 0x08; i++)
342 FD_SET(i, http_encode_map);
343 for (i = 0x0a; i <= 0x1f; i++)
344 FD_SET(i, http_encode_map);
345 FD_SET(0x7f, http_encode_map);
346
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200347 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200348 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200349 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100350 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100351}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352
Willy Tarreau53b6c742006-12-17 13:37:46 +0100353/*
354 * We have 26 list of methods (1 per first letter), each of which can have
355 * up to 3 entries (2 valid, 1 null).
356 */
357struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100358 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100359 int len;
360 const char text[8];
361};
362
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100363const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100364 ['C' - 'A'] = {
365 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
366 },
367 ['D' - 'A'] = {
368 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
369 },
370 ['G' - 'A'] = {
371 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
372 },
373 ['H' - 'A'] = {
374 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
375 },
376 ['P' - 'A'] = {
377 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
378 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
379 },
380 ['T' - 'A'] = {
381 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
382 },
383 /* rest is empty like this :
384 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
385 */
386};
387
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100388const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
389 [HTTP_METH_NONE] = { "", 0 },
390 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
391 [HTTP_METH_GET] = { "GET", 3 },
392 [HTTP_METH_HEAD] = { "HEAD", 4 },
393 [HTTP_METH_POST] = { "POST", 4 },
394 [HTTP_METH_PUT] = { "PUT", 3 },
395 [HTTP_METH_DELETE] = { "DELETE", 6 },
396 [HTTP_METH_TRACE] = { "TRACE", 5 },
397 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
398};
399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100400/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200401 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100402 * RFC2616 for those chars.
403 */
404
405const char http_is_spht[256] = {
406 [' '] = 1, ['\t'] = 1,
407};
408
409const char http_is_crlf[256] = {
410 ['\r'] = 1, ['\n'] = 1,
411};
412
413const char http_is_lws[256] = {
414 [' '] = 1, ['\t'] = 1,
415 ['\r'] = 1, ['\n'] = 1,
416};
417
418const char http_is_sep[256] = {
419 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
420 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
421 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
422 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
423 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
424};
425
426const char http_is_ctl[256] = {
427 [0 ... 31] = 1,
428 [127] = 1,
429};
430
431/*
432 * A token is any ASCII char that is neither a separator nor a CTL char.
433 * Do not overwrite values in assignment since gcc-2.95 will not handle
434 * them correctly. Instead, define every non-CTL char's status.
435 */
436const char http_is_token[256] = {
437 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
438 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
439 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
440 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
441 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
442 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
443 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
444 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
445 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
446 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
447 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
448 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
449 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
450 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
451 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
452 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
453 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
454 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
455 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
456 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
457 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
458 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
459 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
460 ['|'] = 1, ['}'] = 0, ['~'] = 1,
461};
462
463
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100464/*
465 * An http ver_token is any ASCII which can be found in an HTTP version,
466 * which includes 'H', 'T', 'P', '/', '.' and any digit.
467 */
468const char http_is_ver_token[256] = {
469 ['.'] = 1, ['/'] = 1,
470 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
471 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100472 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100473};
474
475
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100476/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100477 * Adds a header and its CRLF at the tail of the message's buffer, just before
478 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100479 * The header is also automatically added to the index <hdr_idx>, and the end
480 * of headers is automatically adjusted. The number of bytes added is returned
481 * on success, otherwise <0 is returned indicating an error.
482 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100483int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100484{
485 int bytes, len;
486
487 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200488 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 if (!bytes)
490 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100491 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100492 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
493}
494
495/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100496 * Adds a header and its CRLF at the tail of the message's buffer, just before
497 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100498 * the buffer is only opened and the space reserved, but nothing is copied.
499 * The header is also automatically added to the index <hdr_idx>, and the end
500 * of headers is automatically adjusted. The number of bytes added is returned
501 * on success, otherwise <0 is returned indicating an error.
502 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100503int http_header_add_tail2(struct http_msg *msg,
504 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100505{
506 int bytes;
507
Willy Tarreau9b28e032012-10-12 23:49:43 +0200508 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100509 if (!bytes)
510 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100511 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100512 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
513}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200514
515/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100516 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
517 * If so, returns the position of the first non-space character relative to
518 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
519 * to return a pointer to the place after the first space. Returns 0 if the
520 * header name does not match. Checks are case-insensitive.
521 */
522int http_header_match2(const char *hdr, const char *end,
523 const char *name, int len)
524{
525 const char *val;
526
527 if (hdr + len >= end)
528 return 0;
529 if (hdr[len] != ':')
530 return 0;
531 if (strncasecmp(hdr, name, len) != 0)
532 return 0;
533 val = hdr + len + 1;
534 while (val < end && HTTP_IS_SPHT(*val))
535 val++;
536 if ((val >= end) && (len + 2 <= end - hdr))
537 return len + 2; /* we may replace starting from second space */
538 return val - hdr;
539}
540
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200541/* Find the first or next occurrence of header <name> in message buffer <sol>
542 * using headers index <idx>, and return it in the <ctx> structure. This
543 * structure holds everything necessary to use the header and find next
544 * occurrence. If its <idx> member is 0, the header is searched from the
545 * beginning. Otherwise, the next occurrence is returned. The function returns
546 * 1 when it finds a value, and 0 when there is no more. It is very similar to
547 * http_find_header2() except that it is designed to work with full-line headers
548 * whose comma is not a delimiter but is part of the syntax. As a special case,
549 * if ctx->val is NULL when searching for a new values of a header, the current
550 * header is rescanned. This allows rescanning after a header deletion.
551 */
552int http_find_full_header2(const char *name, int len,
553 char *sol, struct hdr_idx *idx,
554 struct hdr_ctx *ctx)
555{
556 char *eol, *sov;
557 int cur_idx, old_idx;
558
559 cur_idx = ctx->idx;
560 if (cur_idx) {
561 /* We have previously returned a header, let's search another one */
562 sol = ctx->line;
563 eol = sol + idx->v[cur_idx].len;
564 goto next_hdr;
565 }
566
567 /* first request for this header */
568 sol += hdr_idx_first_pos(idx);
569 old_idx = 0;
570 cur_idx = hdr_idx_first_idx(idx);
571 while (cur_idx) {
572 eol = sol + idx->v[cur_idx].len;
573
574 if (len == 0) {
575 /* No argument was passed, we want any header.
576 * To achieve this, we simply build a fake request. */
577 while (sol + len < eol && sol[len] != ':')
578 len++;
579 name = sol;
580 }
581
582 if ((len < eol - sol) &&
583 (sol[len] == ':') &&
584 (strncasecmp(sol, name, len) == 0)) {
585 ctx->del = len;
586 sov = sol + len + 1;
587 while (sov < eol && http_is_lws[(unsigned char)*sov])
588 sov++;
589
590 ctx->line = sol;
591 ctx->prev = old_idx;
592 ctx->idx = cur_idx;
593 ctx->val = sov - sol;
594 ctx->tws = 0;
595 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
596 eol--;
597 ctx->tws++;
598 }
599 ctx->vlen = eol - sov;
600 return 1;
601 }
602 next_hdr:
603 sol = eol + idx->v[cur_idx].cr + 1;
604 old_idx = cur_idx;
605 cur_idx = idx->v[cur_idx].next;
606 }
607 return 0;
608}
609
Willy Tarreauc90dc232015-02-20 13:51:36 +0100610/* Find the first or next header field in message buffer <sol> using headers
611 * index <idx>, and return it in the <ctx> structure. This structure holds
612 * everything necessary to use the header and find next occurrence. If its
613 * <idx> member is 0, the first header is retrieved. Otherwise, the next
614 * occurrence is returned. The function returns 1 when it finds a value, and
615 * 0 when there is no more. It is equivalent to http_find_full_header2() with
616 * no header name.
617 */
618int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
619{
620 char *eol, *sov;
621 int cur_idx, old_idx;
622 int len;
623
624 cur_idx = ctx->idx;
625 if (cur_idx) {
626 /* We have previously returned a header, let's search another one */
627 sol = ctx->line;
628 eol = sol + idx->v[cur_idx].len;
629 goto next_hdr;
630 }
631
632 /* first request for this header */
633 sol += hdr_idx_first_pos(idx);
634 old_idx = 0;
635 cur_idx = hdr_idx_first_idx(idx);
636 while (cur_idx) {
637 eol = sol + idx->v[cur_idx].len;
638
639 len = 0;
640 while (1) {
641 if (len >= eol - sol)
642 goto next_hdr;
643 if (sol[len] == ':')
644 break;
645 len++;
646 }
647
648 ctx->del = len;
649 sov = sol + len + 1;
650 while (sov < eol && http_is_lws[(unsigned char)*sov])
651 sov++;
652
653 ctx->line = sol;
654 ctx->prev = old_idx;
655 ctx->idx = cur_idx;
656 ctx->val = sov - sol;
657 ctx->tws = 0;
658
659 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
660 eol--;
661 ctx->tws++;
662 }
663 ctx->vlen = eol - sov;
664 return 1;
665
666 next_hdr:
667 sol = eol + idx->v[cur_idx].cr + 1;
668 old_idx = cur_idx;
669 cur_idx = idx->v[cur_idx].next;
670 }
671 return 0;
672}
673
Willy Tarreau68085d82010-01-18 14:54:04 +0100674/* Find the end of the header value contained between <s> and <e>. See RFC2616,
675 * par 2.2 for more information. Note that it requires a valid header to return
676 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200677 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100678char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200679{
680 int quoted, qdpair;
681
682 quoted = qdpair = 0;
683 for (; s < e; s++) {
684 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200685 else if (quoted) {
686 if (*s == '\\') qdpair = 1;
687 else if (*s == '"') quoted = 0;
688 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200689 else if (*s == '"') quoted = 1;
690 else if (*s == ',') return s;
691 }
692 return s;
693}
694
695/* Find the first or next occurrence of header <name> in message buffer <sol>
696 * using headers index <idx>, and return it in the <ctx> structure. This
697 * structure holds everything necessary to use the header and find next
698 * occurrence. If its <idx> member is 0, the header is searched from the
699 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100700 * 1 when it finds a value, and 0 when there is no more. It is designed to work
701 * with headers defined as comma-separated lists. As a special case, if ctx->val
702 * is NULL when searching for a new values of a header, the current header is
703 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200704 */
705int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100706 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200707 struct hdr_ctx *ctx)
708{
Willy Tarreau68085d82010-01-18 14:54:04 +0100709 char *eol, *sov;
710 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200711
Willy Tarreau68085d82010-01-18 14:54:04 +0100712 cur_idx = ctx->idx;
713 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200714 /* We have previously returned a value, let's search
715 * another one on the same line.
716 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200717 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200718 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100719 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200720 eol = sol + idx->v[cur_idx].len;
721
722 if (sov >= eol)
723 /* no more values in this header */
724 goto next_hdr;
725
Willy Tarreau68085d82010-01-18 14:54:04 +0100726 /* values remaining for this header, skip the comma but save it
727 * for later use (eg: for header deletion).
728 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200729 sov++;
730 while (sov < eol && http_is_lws[(unsigned char)*sov])
731 sov++;
732
733 goto return_hdr;
734 }
735
736 /* first request for this header */
737 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100738 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200740 while (cur_idx) {
741 eol = sol + idx->v[cur_idx].len;
742
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200743 if (len == 0) {
744 /* No argument was passed, we want any header.
745 * To achieve this, we simply build a fake request. */
746 while (sol + len < eol && sol[len] != ':')
747 len++;
748 name = sol;
749 }
750
Willy Tarreau33a7e692007-06-10 19:45:56 +0200751 if ((len < eol - sol) &&
752 (sol[len] == ':') &&
753 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100754 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200755 sov = sol + len + 1;
756 while (sov < eol && http_is_lws[(unsigned char)*sov])
757 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100758
Willy Tarreau33a7e692007-06-10 19:45:56 +0200759 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100760 ctx->prev = old_idx;
761 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200762 ctx->idx = cur_idx;
763 ctx->val = sov - sol;
764
765 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200766 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200767 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200768 eol--;
769 ctx->tws++;
770 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200771 ctx->vlen = eol - sov;
772 return 1;
773 }
774 next_hdr:
775 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100776 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200777 cur_idx = idx->v[cur_idx].next;
778 }
779 return 0;
780}
781
782int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100783 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200784 struct hdr_ctx *ctx)
785{
786 return http_find_header2(name, strlen(name), sol, idx, ctx);
787}
788
Willy Tarreau68085d82010-01-18 14:54:04 +0100789/* Remove one value of a header. This only works on a <ctx> returned by one of
790 * the http_find_header functions. The value is removed, as well as surrounding
791 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100792 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 * message <msg>. The new index is returned. If it is zero, it means there is
794 * no more header, so any processing may stop. The ctx is always left in a form
795 * that can be handled by http_find_header2() to find next occurrence.
796 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100797int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100798{
799 int cur_idx = ctx->idx;
800 char *sol = ctx->line;
801 struct hdr_idx_elem *hdr;
802 int delta, skip_comma;
803
804 if (!cur_idx)
805 return 0;
806
807 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200808 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100809 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200810 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100811 http_msg_move_end(msg, delta);
812 idx->used--;
813 hdr->len = 0; /* unused entry */
814 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100815 if (idx->tail == ctx->idx)
816 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100817 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100818 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100819 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200820 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100821 return ctx->idx;
822 }
823
824 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200825 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
826 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100827 */
828
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200829 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200830 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200831 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100832 NULL, 0);
833 hdr->len += delta;
834 http_msg_move_end(msg, delta);
835 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200836 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100837 return ctx->idx;
838}
839
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100840/* This function handles a server error at the stream interface level. The
841 * stream interface is assumed to be already in a closed state. An optional
842 * message is copied into the input buffer, and an HTTP status code stored.
843 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100844 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200846static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100847 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100849 channel_auto_read(si_oc(si));
850 channel_abort(si_oc(si));
851 channel_auto_close(si_oc(si));
852 channel_erase(si_oc(si));
853 channel_auto_close(si_ic(si));
854 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100855 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200856 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100857 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200859 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200860 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200861 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200862 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863}
864
Willy Tarreau87b09662015-04-03 00:22:06 +0200865/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100866 * and message.
867 */
868
Willy Tarreau87b09662015-04-03 00:22:06 +0200869struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100870{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200871 if (s->be->errmsg[msgnum].str)
872 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200873 else if (strm_fe(s)->errmsg[msgnum].str)
874 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100875 else
876 return &http_err_chunks[msgnum];
877}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878
Willy Tarreau53b6c742006-12-17 13:37:46 +0100879/*
880 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
881 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
882 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100883enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100884{
885 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100886 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100887
888 m = ((unsigned)*str - 'A');
889
890 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100891 for (h = http_methods[m]; h->len > 0; h++) {
892 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100893 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100894 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100895 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100896 };
897 return HTTP_METH_OTHER;
898 }
899 return HTTP_METH_NONE;
900
901}
902
Willy Tarreau21d2af32008-02-14 20:25:24 +0100903/* Parse the URI from the given transaction (which is assumed to be in request
904 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
905 * It is returned otherwise.
906 */
907static char *
908http_get_path(struct http_txn *txn)
909{
910 char *ptr, *end;
911
Willy Tarreau9b28e032012-10-12 23:49:43 +0200912 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100913 end = ptr + txn->req.sl.rq.u_l;
914
915 if (ptr >= end)
916 return NULL;
917
918 /* RFC2616, par. 5.1.2 :
919 * Request-URI = "*" | absuri | abspath | authority
920 */
921
922 if (*ptr == '*')
923 return NULL;
924
925 if (isalpha((unsigned char)*ptr)) {
926 /* this is a scheme as described by RFC3986, par. 3.1 */
927 ptr++;
928 while (ptr < end &&
929 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
930 ptr++;
931 /* skip '://' */
932 if (ptr == end || *ptr++ != ':')
933 return NULL;
934 if (ptr == end || *ptr++ != '/')
935 return NULL;
936 if (ptr == end || *ptr++ != '/')
937 return NULL;
938 }
939 /* skip [user[:passwd]@]host[:[port]] */
940
941 while (ptr < end && *ptr != '/')
942 ptr++;
943
944 if (ptr == end)
945 return NULL;
946
947 /* OK, we got the '/' ! */
948 return ptr;
949}
950
William Lallemand65ad6e12014-01-31 15:08:02 +0100951/* Parse the URI from the given string and look for the "/" beginning the PATH.
952 * If not found, return NULL. It is returned otherwise.
953 */
954static char *
955http_get_path_from_string(char *str)
956{
957 char *ptr = str;
958
959 /* RFC2616, par. 5.1.2 :
960 * Request-URI = "*" | absuri | abspath | authority
961 */
962
963 if (*ptr == '*')
964 return NULL;
965
966 if (isalpha((unsigned char)*ptr)) {
967 /* this is a scheme as described by RFC3986, par. 3.1 */
968 ptr++;
969 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
970 ptr++;
971 /* skip '://' */
972 if (*ptr == '\0' || *ptr++ != ':')
973 return NULL;
974 if (*ptr == '\0' || *ptr++ != '/')
975 return NULL;
976 if (*ptr == '\0' || *ptr++ != '/')
977 return NULL;
978 }
979 /* skip [user[:passwd]@]host[:[port]] */
980
981 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
982 ptr++;
983
984 if (*ptr == '\0' || *ptr == ' ')
985 return NULL;
986
987 /* OK, we got the '/' ! */
988 return ptr;
989}
990
Willy Tarreau71241ab2012-12-27 11:30:54 +0100991/* Returns a 302 for a redirectable request that reaches a server working in
992 * in redirect mode. This may only be called just after the stream interface
993 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
994 * follow normal proxy processing. NOTE: this function is designed to support
995 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100996 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200997void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100998{
999 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001000 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001001 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001002 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003
1004 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001005 trash.len = strlen(HTTP_302);
1006 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001007
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001008 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001009
Willy Tarreauefb453c2008-10-26 20:49:47 +01001010 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001011 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001012 return;
1013
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001014 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001015 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001016 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1017 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001018 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001019
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001020 /* 3: add the request URI. Since it was already forwarded, we need
1021 * to temporarily rewind the buffer.
1022 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001023 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001024 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001025
Willy Tarreauefb453c2008-10-26 20:49:47 +01001026 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001027 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 +02001028
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001029 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001030
Willy Tarreauefb453c2008-10-26 20:49:47 +01001031 if (!path)
1032 return;
1033
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001034 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001035 return;
1036
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001037 memcpy(trash.str + trash.len, path, len);
1038 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001039
1040 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001041 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1042 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001043 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001044 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1045 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001046 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001047
1048 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001049 si_shutr(si);
1050 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001051 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001052 si->state = SI_ST_CLO;
1053
1054 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001055 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001056
1057 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001058 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001059 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001060}
1061
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001062/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001063 * that the server side is closed. Note that err_type is actually a
1064 * bitmask, where almost only aborts may be cumulated with other
1065 * values. We consider that aborted operations are more important
1066 * than timeouts or errors due to the fact that nobody else in the
1067 * logs might explain incomplete retries. All others should avoid
1068 * being cumulated. It should normally not be possible to have multiple
1069 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001070 * Note that connection errors appearing on the second request of a keep-alive
1071 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001072 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001073void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001074{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001075 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001076
1077 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001078 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001079 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001080 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001081 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001082 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001083 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001084 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001085 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001086 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001087 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001088 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001089 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001090 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001091 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001092 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001093 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001094 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001095 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1096 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001097 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001098 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001099 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001100 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001101 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001102 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001103 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001104 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001105}
1106
Willy Tarreau42250582007-04-01 01:30:43 +02001107extern const char sess_term_cond[8];
1108extern const char sess_fin_state[8];
1109extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001110struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001111struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001112struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001113struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114
Willy Tarreau117f59e2007-03-04 18:17:17 +01001115/*
1116 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001117 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001118 */
1119void capture_headers(char *som, struct hdr_idx *idx,
1120 char **cap, struct cap_hdr *cap_hdr)
1121{
1122 char *eol, *sol, *col, *sov;
1123 int cur_idx;
1124 struct cap_hdr *h;
1125 int len;
1126
1127 sol = som + hdr_idx_first_pos(idx);
1128 cur_idx = hdr_idx_first_idx(idx);
1129
1130 while (cur_idx) {
1131 eol = sol + idx->v[cur_idx].len;
1132
1133 col = sol;
1134 while (col < eol && *col != ':')
1135 col++;
1136
1137 sov = col + 1;
1138 while (sov < eol && http_is_lws[(unsigned char)*sov])
1139 sov++;
1140
1141 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001142 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001143 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1144 if (cap[h->index] == NULL)
1145 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001146 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001147
1148 if (cap[h->index] == NULL) {
1149 Alert("HTTP capture : out of memory.\n");
1150 continue;
1151 }
1152
1153 len = eol - sov;
1154 if (len > h->len)
1155 len = h->len;
1156
1157 memcpy(cap[h->index], sov, len);
1158 cap[h->index][len]=0;
1159 }
1160 }
1161 sol = eol + idx->v[cur_idx].cr + 1;
1162 cur_idx = idx->v[cur_idx].next;
1163 }
1164}
1165
1166
Willy Tarreau42250582007-04-01 01:30:43 +02001167/* either we find an LF at <ptr> or we jump to <bad>.
1168 */
1169#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1170
1171/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1172 * otherwise to <http_msg_ood> with <state> set to <st>.
1173 */
1174#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1175 ptr++; \
1176 if (likely(ptr < end)) \
1177 goto good; \
1178 else { \
1179 state = (st); \
1180 goto http_msg_ood; \
1181 } \
1182 } while (0)
1183
1184
Willy Tarreaubaaee002006-06-26 02:48:02 +02001185/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001186 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001187 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1188 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1189 * will give undefined results.
1190 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1191 * and that msg->sol points to the beginning of the response.
1192 * If a complete line is found (which implies that at least one CR or LF is
1193 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1194 * returned indicating an incomplete line (which does not mean that parts have
1195 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1196 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1197 * upon next call.
1198 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001199 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1201 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001202 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001203 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001204const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001205 enum ht_state state, const char *ptr, const char *end,
1206 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001207{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001208 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001209
Willy Tarreau8973c702007-01-21 23:58:29 +01001210 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001211 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001212 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001213 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001214 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1215
1216 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001217 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001218 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1219 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001220 state = HTTP_MSG_ERROR;
1221 break;
1222
Willy Tarreau8973c702007-01-21 23:58:29 +01001223 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001224 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001225 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001226 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001227 goto http_msg_rpcode;
1228 }
1229 if (likely(HTTP_IS_SPHT(*ptr)))
1230 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1231 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001232 state = HTTP_MSG_ERROR;
1233 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001234
Willy Tarreau8973c702007-01-21 23:58:29 +01001235 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001236 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001237 if (likely(!HTTP_IS_LWS(*ptr)))
1238 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1239
1240 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001241 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001242 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1243 }
1244
1245 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001246 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001247 http_msg_rsp_reason:
1248 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001250 msg->sl.st.r_l = 0;
1251 goto http_msg_rpline_eol;
1252
Willy Tarreau8973c702007-01-21 23:58:29 +01001253 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001254 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001255 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001256 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001257 goto http_msg_rpreason;
1258 }
1259 if (likely(HTTP_IS_SPHT(*ptr)))
1260 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1261 /* so it's a CR/LF, so there is no reason phrase */
1262 goto http_msg_rsp_reason;
1263
Willy Tarreau8973c702007-01-21 23:58:29 +01001264 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001265 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001266 if (likely(!HTTP_IS_CRLF(*ptr)))
1267 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001268 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001269 http_msg_rpline_eol:
1270 /* We have seen the end of line. Note that we do not
1271 * necessarily have the \n yet, but at least we know that we
1272 * have EITHER \r OR \n, otherwise the response would not be
1273 * complete. We can then record the response length and return
1274 * to the caller which will be able to register it.
1275 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001276 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001277 return ptr;
1278
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001280#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001281 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1282 exit(1);
1283#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001284 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001285 }
1286
1287 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001288 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001289 if (ret_state)
1290 *ret_state = state;
1291 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001292 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001294}
1295
Willy Tarreau8973c702007-01-21 23:58:29 +01001296/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 * This function parses a request line between <ptr> and <end>, starting with
1298 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1299 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1300 * will give undefined results.
1301 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1302 * and that msg->sol points to the beginning of the request.
1303 * If a complete line is found (which implies that at least one CR or LF is
1304 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1305 * returned indicating an incomplete line (which does not mean that parts have
1306 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1307 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1308 * upon next call.
1309 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001310 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1312 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001313 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001314 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001315const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001316 enum ht_state state, const char *ptr, const char *end,
1317 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001318{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001319 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001323 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324 if (likely(HTTP_IS_TOKEN(*ptr)))
1325 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001327 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001328 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001329 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1330 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001331
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 if (likely(HTTP_IS_CRLF(*ptr))) {
1333 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001334 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001336 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001338 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001340 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 msg->sl.rq.v_l = 0;
1342 goto http_msg_rqline_eol;
1343 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001344 state = HTTP_MSG_ERROR;
1345 break;
1346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001348 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001350 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 goto http_msg_rquri;
1352 }
1353 if (likely(HTTP_IS_SPHT(*ptr)))
1354 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1355 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1356 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001359 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001360 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001362
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001364 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1366 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001368 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001369 /* non-ASCII chars are forbidden unless option
1370 * accept-invalid-http-request is enabled in the frontend.
1371 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001372 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001373 if (msg->err_pos < -1)
1374 goto invalid_char;
1375 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001376 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001377 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1378 }
1379
1380 if (likely(HTTP_IS_CRLF(*ptr))) {
1381 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1382 goto http_msg_req09_uri_e;
1383 }
1384
1385 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001386 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001387 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001388 state = HTTP_MSG_ERROR;
1389 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001392 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001394 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 goto http_msg_rqver;
1396 }
1397 if (likely(HTTP_IS_SPHT(*ptr)))
1398 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1399 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1400 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001403 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001404 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001406
1407 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001408 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001409 http_msg_rqline_eol:
1410 /* We have seen the end of line. Note that we do not
1411 * necessarily have the \n yet, but at least we know that we
1412 * have EITHER \r OR \n, otherwise the request would not be
1413 * complete. We can then record the request length and return
1414 * to the caller which will be able to register it.
1415 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001416 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001417 return ptr;
1418 }
1419
1420 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001421 state = HTTP_MSG_ERROR;
1422 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001425#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1427 exit(1);
1428#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001429 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001433 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 if (ret_state)
1435 *ret_state = state;
1436 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001437 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001440
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001441/*
1442 * Returns the data from Authorization header. Function may be called more
1443 * than once so data is stored in txn->auth_data. When no header is found
1444 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001445 * searching again for something we are unable to find anyway. However, if
1446 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001447 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001448 */
1449
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001450/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1451 * set according to global.tune.bufsize.
1452 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001453char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001454
1455int
Willy Tarreau87b09662015-04-03 00:22:06 +02001456get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001457{
1458
Willy Tarreaueee5b512015-04-03 23:46:31 +02001459 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001460 struct chunk auth_method;
1461 struct hdr_ctx ctx;
1462 char *h, *p;
1463 int len;
1464
1465#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001466 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001467#endif
1468
1469 if (txn->auth.method == HTTP_AUTH_WRONG)
1470 return 0;
1471
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001472 txn->auth.method = HTTP_AUTH_WRONG;
1473
1474 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001475
1476 if (txn->flags & TX_USE_PX_CONN) {
1477 h = "Proxy-Authorization";
1478 len = strlen(h);
1479 } else {
1480 h = "Authorization";
1481 len = strlen(h);
1482 }
1483
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001484 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001485 return 0;
1486
1487 h = ctx.line + ctx.val;
1488
1489 p = memchr(h, ' ', ctx.vlen);
1490 if (!p || p == h)
1491 return 0;
1492
1493 chunk_initlen(&auth_method, h, 0, p-h);
1494 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1495
1496 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1497
1498 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001499 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001500
1501 if (len < 0)
1502 return 0;
1503
1504
1505 get_http_auth_buff[len] = '\0';
1506
1507 p = strchr(get_http_auth_buff, ':');
1508
1509 if (!p)
1510 return 0;
1511
1512 txn->auth.user = get_http_auth_buff;
1513 *p = '\0';
1514 txn->auth.pass = p+1;
1515
1516 txn->auth.method = HTTP_AUTH_BASIC;
1517 return 1;
1518 }
1519
1520 return 0;
1521}
1522
Willy Tarreau58f10d72006-12-04 02:26:12 +01001523
Willy Tarreau8973c702007-01-21 23:58:29 +01001524/*
1525 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001526 * depending on the initial msg->msg_state. The caller is responsible for
1527 * ensuring that the message does not wrap. The function can be preempted
1528 * everywhere when data are missing and recalled at the exact same location
1529 * with no information loss. The message may even be realigned between two
1530 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001531 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001532 * fields. Note that msg->sol will be initialized after completing the first
1533 * state, so that none of the msg pointers has to be initialized prior to the
1534 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001535 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001536void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537{
Willy Tarreau3770f232013-12-07 00:01:53 +01001538 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001540 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001541
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001542 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001543 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 ptr = buf->p + msg->next;
1545 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001546
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 if (unlikely(ptr >= end))
1548 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001551 /*
1552 * First, states that are specific to the response only.
1553 * We check them first so that request and headers are
1554 * closer to each other (accessed more often).
1555 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001556 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001557 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001558 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001559 /* we have a start of message, but we have to check
1560 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001561 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001562 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001563 if (unlikely(ptr != buf->p)) {
1564 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001565 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001566 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001567 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001568 }
Willy Tarreau26927362012-05-18 23:22:52 +02001569 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001570 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001571 hdr_idx_init(idx);
1572 state = HTTP_MSG_RPVER;
1573 goto http_msg_rpver;
1574 }
1575
1576 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1577 goto http_msg_invalid;
1578
1579 if (unlikely(*ptr == '\n'))
1580 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1581 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1582 /* stop here */
1583
Willy Tarreau8973c702007-01-21 23:58:29 +01001584 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001585 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001586 EXPECT_LF_HERE(ptr, http_msg_invalid);
1587 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1588 /* stop here */
1589
Willy Tarreau8973c702007-01-21 23:58:29 +01001590 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001591 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001592 case HTTP_MSG_RPVER_SP:
1593 case HTTP_MSG_RPCODE:
1594 case HTTP_MSG_RPCODE_SP:
1595 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001596 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001597 state, ptr, end,
1598 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001599 if (unlikely(!ptr))
1600 return;
1601
1602 /* we have a full response and we know that we have either a CR
1603 * or an LF at <ptr>.
1604 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001605 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1606
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001607 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001608 if (likely(*ptr == '\r'))
1609 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1610 goto http_msg_rpline_end;
1611
Willy Tarreau8973c702007-01-21 23:58:29 +01001612 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001613 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001614 /* msg->sol must point to the first of CR or LF. */
1615 EXPECT_LF_HERE(ptr, http_msg_invalid);
1616 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1617 /* stop here */
1618
1619 /*
1620 * Second, states that are specific to the request only
1621 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001623 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001625 /* we have a start of message, but we have to check
1626 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001627 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001628 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001629 if (likely(ptr != buf->p)) {
1630 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001631 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001632 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001633 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 }
Willy Tarreau26927362012-05-18 23:22:52 +02001635 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001636 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001637 state = HTTP_MSG_RQMETH;
1638 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1642 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001644 if (unlikely(*ptr == '\n'))
1645 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1646 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001647 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001650 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 EXPECT_LF_HERE(ptr, http_msg_invalid);
1652 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001653 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001654
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001656 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001657 case HTTP_MSG_RQMETH_SP:
1658 case HTTP_MSG_RQURI:
1659 case HTTP_MSG_RQURI_SP:
1660 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001661 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001662 state, ptr, end,
1663 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 if (unlikely(!ptr))
1665 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001667 /* we have a full request and we know that we have either a CR
1668 * or an LF at <ptr>.
1669 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001671
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001672 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 if (likely(*ptr == '\r'))
1674 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001676
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001678 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* check for HTTP/0.9 request : no version information available.
1680 * msg->sol must point to the first of CR or LF.
1681 */
1682 if (unlikely(msg->sl.rq.v_l == 0))
1683 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 EXPECT_LF_HERE(ptr, http_msg_invalid);
1686 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001687 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 /*
1690 * Common states below
1691 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001693 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001694 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001695 if (likely(!HTTP_IS_CRLF(*ptr))) {
1696 goto http_msg_hdr_name;
1697 }
1698
1699 if (likely(*ptr == '\r'))
1700 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1701 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001704 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 /* assumes msg->sol points to the first char */
1706 if (likely(HTTP_IS_TOKEN(*ptr)))
1707 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001708
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001709 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001711
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001712 if (likely(msg->err_pos < -1) || *ptr == '\n')
1713 goto http_msg_invalid;
1714
1715 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001716 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001717
1718 /* and we still accept this non-token character */
1719 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001723 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(HTTP_IS_SPHT(*ptr)))
1725 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001726
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001728 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001729
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 if (likely(!HTTP_IS_CRLF(*ptr))) {
1731 goto http_msg_hdr_val;
1732 }
1733
1734 if (likely(*ptr == '\r'))
1735 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1736 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001737
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001739 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 EXPECT_LF_HERE(ptr, http_msg_invalid);
1741 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001742
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001744 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 if (likely(HTTP_IS_SPHT(*ptr))) {
1746 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001747 for (; buf->p + msg->sov < ptr; msg->sov++)
1748 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 goto http_msg_hdr_l1_sp;
1750 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001751 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001752 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 goto http_msg_complete_header;
1754
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001756 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001757 /* assumes msg->sol points to the first char, and msg->sov
1758 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 */
1760 if (likely(!HTTP_IS_CRLF(*ptr)))
1761 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001762
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001763 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 /* Note: we could also copy eol into ->eoh so that we have the
1765 * real header end in case it ends with lots of LWS, but is this
1766 * really needed ?
1767 */
1768 if (likely(*ptr == '\r'))
1769 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1770 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001771
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001772 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001773 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 EXPECT_LF_HERE(ptr, http_msg_invalid);
1775 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001778 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1780 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001781 for (; buf->p + msg->eol < ptr; msg->eol++)
1782 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001783 goto http_msg_hdr_val;
1784 }
1785 http_msg_complete_header:
1786 /*
1787 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001788 * Assumes msg->sol points to the first char, msg->sov points
1789 * to the first character of the value and msg->eol to the
1790 * first CR or LF so we know how the line ends. We insert last
1791 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001792 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001793 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 idx, idx->tail) < 0))
1795 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001796
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001797 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001798 if (likely(!HTTP_IS_CRLF(*ptr))) {
1799 goto http_msg_hdr_name;
1800 }
1801
1802 if (likely(*ptr == '\r'))
1803 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1804 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001807 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001808 /* Assumes msg->sol points to the first of either CR or LF.
1809 * Sets ->sov and ->next to the total header length, ->eoh to
1810 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1811 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 EXPECT_LF_HERE(ptr, http_msg_invalid);
1813 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001814 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001815 msg->eoh = msg->sol;
1816 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001817 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001818 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001820
1821 case HTTP_MSG_ERROR:
1822 /* this may only happen if we call http_msg_analyser() twice with an error */
1823 break;
1824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001826#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1828 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001829#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001830 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 }
1832 http_msg_ood:
1833 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001834 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001835 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 http_msg_invalid:
1839 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001840 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001841 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 return;
1843}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001844
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001845/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1846 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1847 * nothing is done and 1 is returned.
1848 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001849static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001850{
1851 int delta;
1852 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001853 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001854
1855 if (msg->sl.rq.v_l != 0)
1856 return 1;
1857
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001858 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1859 if (txn->meth != HTTP_METH_GET)
1860 return 0;
1861
Willy Tarreau9b28e032012-10-12 23:49:43 +02001862 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001863 delta = 0;
1864
1865 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001866 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1867 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001868 }
1869 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001870 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001871 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001872 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001873 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001874 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001875 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001876 NULL, NULL);
1877 if (unlikely(!cur_end))
1878 return 0;
1879
1880 /* we have a full HTTP/1.0 request now and we know that
1881 * we have either a CR or an LF at <ptr>.
1882 */
1883 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1884 return 1;
1885}
1886
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001888 * and "keep-alive" values. If we already know that some headers may safely
1889 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001890 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1891 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001892 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001893 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1894 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1895 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001896 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001897 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001898void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001899{
Willy Tarreau5b154472009-12-21 20:11:07 +01001900 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001901 const char *hdr_val = "Connection";
1902 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001903
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001904 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001905 return;
1906
Willy Tarreau88d349d2010-01-25 12:15:43 +01001907 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1908 hdr_val = "Proxy-Connection";
1909 hdr_len = 16;
1910 }
1911
Willy Tarreau5b154472009-12-21 20:11:07 +01001912 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001913 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001914 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001915 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1916 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001917 if (to_del & 2)
1918 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001919 else
1920 txn->flags |= TX_CON_KAL_SET;
1921 }
1922 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1923 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001924 if (to_del & 1)
1925 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001926 else
1927 txn->flags |= TX_CON_CLO_SET;
1928 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001929 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1930 txn->flags |= TX_HDR_CONN_UPG;
1931 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001932 }
1933
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001934 txn->flags |= TX_HDR_CONN_PRS;
1935 return;
1936}
Willy Tarreau5b154472009-12-21 20:11:07 +01001937
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001938/* Apply desired changes on the Connection: header. Values may be removed and/or
1939 * added depending on the <wanted> flags, which are exclusively composed of
1940 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1941 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1942 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001943void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001944{
1945 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001946 const char *hdr_val = "Connection";
1947 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001948
1949 ctx.idx = 0;
1950
Willy Tarreau88d349d2010-01-25 12:15:43 +01001951
1952 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1953 hdr_val = "Proxy-Connection";
1954 hdr_len = 16;
1955 }
1956
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001957 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001958 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001959 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1960 if (wanted & TX_CON_KAL_SET)
1961 txn->flags |= TX_CON_KAL_SET;
1962 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001963 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001964 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001965 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1966 if (wanted & TX_CON_CLO_SET)
1967 txn->flags |= TX_CON_CLO_SET;
1968 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001969 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001970 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001971 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001972
1973 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1974 return;
1975
1976 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1977 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001978 hdr_val = "Connection: close";
1979 hdr_len = 17;
1980 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1981 hdr_val = "Proxy-Connection: close";
1982 hdr_len = 23;
1983 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001984 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001985 }
1986
1987 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1988 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001989 hdr_val = "Connection: keep-alive";
1990 hdr_len = 22;
1991 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1992 hdr_val = "Proxy-Connection: keep-alive";
1993 hdr_len = 28;
1994 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001995 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001996 }
1997 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001998}
1999
Willy Tarreauc24715e2014-04-17 15:21:20 +02002000/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2001 * the first byte of data after the chunk size, so that we know we can forward
2002 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2003 * the chunk size. That way it is always possible to differentiate between the
2004 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002007 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002008static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002009{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002010 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002011 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002012 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002013 const char *end = buf->data + buf->size;
2014 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002015 unsigned int chunk = 0;
2016
2017 /* The chunk size is in the following form, though we are only
2018 * interested in the size and CRLF :
2019 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2020 */
2021 while (1) {
2022 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002023 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002024 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002026 if (c < 0) /* not a hex digit anymore */
2027 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002028 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002029 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002030 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002031 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002032 chunk = (chunk << 4) + c;
2033 }
2034
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002036 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002037 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038
2039 while (http_is_spht[(unsigned char)*ptr]) {
2040 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002042 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002043 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002044 }
2045
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046 /* Up to there, we know that at least one byte is present at *ptr. Check
2047 * for the end of chunk size.
2048 */
2049 while (1) {
2050 if (likely(HTTP_IS_CRLF(*ptr))) {
2051 /* we now have a CR or an LF at ptr */
2052 if (likely(*ptr == '\r')) {
2053 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002055 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056 return 0;
2057 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002058
Willy Tarreaud98cf932009-12-27 22:54:55 +01002059 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002060 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002063 /* done */
2064 break;
2065 }
2066 else if (*ptr == ';') {
2067 /* chunk extension, ends at next CRLF */
2068 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002069 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002070 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002071 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072
2073 while (!HTTP_IS_CRLF(*ptr)) {
2074 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002076 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002077 return 0;
2078 }
2079 /* we have a CRLF now, loop above */
2080 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002081 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002083 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002084 }
2085
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002087 * which may or may not be present. We save that into ->next,
2088 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002089 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002090 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002091 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002092 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002093 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002094 msg->chunk_len = chunk;
2095 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002096 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002097 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002098 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002099 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002100 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002101}
2102
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002103/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002104 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 * the trailers is found, it is automatically scheduled to be forwarded,
2106 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2107 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002108 * except maybe msg->next if it could parse some lines, and returns zero.
2109 * If a parse error is encountered, the function returns < 0 and does not
2110 * change anything except maybe msg->next. Note that the message must
2111 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002112 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002113 * forwarding, and that msg->next exactly matches the length of trailers
2114 * already parsed and not forwarded. It is also important to note that this
2115 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002117static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002119 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002120
Willy Tarreaua458b672012-03-05 11:17:50 +01002121 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002123 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002124 const char *ptr = b_ptr(buf, msg->next);
2125 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002126 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127
2128 /* scan current line and stop at LF or CRLF */
2129 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002130 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002131 return 0;
2132
2133 if (*ptr == '\n') {
2134 if (!p1)
2135 p1 = ptr;
2136 p2 = ptr;
2137 break;
2138 }
2139
2140 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002141 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002142 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002144 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002145 p1 = ptr;
2146 }
2147
2148 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002149 if (ptr >= buf->data + buf->size)
2150 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002151 }
2152
2153 /* after LF; point to beginning of next line */
2154 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002155 if (p2 >= buf->data + buf->size)
2156 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002157
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002158 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002159 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002160 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002161
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002162 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002163 /* LF/CRLF at beginning of line => end of trailers at p2.
2164 * Everything was scheduled for forwarding, there's nothing
2165 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002166 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002167 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002168 msg->msg_state = HTTP_MSG_DONE;
2169 return 1;
2170 }
2171 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002172 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002173 }
2174}
2175
Willy Tarreauc24715e2014-04-17 15:21:20 +02002176/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2177 * or a possible LF alone at the end of a chunk. It automatically adjusts
2178 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002179 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002180 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2181 * not enough data are available, the function does not change anything and
2182 * returns zero. If a parse error is encountered, the function returns < 0 and
2183 * does not change anything. Note: this function is designed to parse wrapped
2184 * CRLF at the end of the buffer.
2185 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002186static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002187{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002188 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002189 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002190 int bytes;
2191
2192 /* NB: we'll check data availabilty at the end. It's not a
2193 * problem because whatever we match first will be checked
2194 * against the correct length.
2195 */
2196 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002197 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002198 if (*ptr == '\r') {
2199 bytes++;
2200 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002201 if (ptr >= buf->data + buf->size)
2202 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002203 }
2204
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002205 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002206 return 0;
2207
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002208 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002209 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002210 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002211 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002212
2213 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002214 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002215 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002216 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002217 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002218 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2219 return 1;
2220}
Willy Tarreau5b154472009-12-21 20:11:07 +01002221
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002222/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2223 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2224 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2225 * Unparsable qvalues return 1000 as "q=1.000".
2226 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002227int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002228{
2229 int q = 1000;
2230
Willy Tarreau506c69a2014-07-08 00:59:48 +02002231 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002232 goto out;
2233 q = (*qvalue++ - '0') * 1000;
2234
2235 if (*qvalue++ != '.')
2236 goto out;
2237
Willy Tarreau506c69a2014-07-08 00:59:48 +02002238 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002239 goto out;
2240 q += (*qvalue++ - '0') * 100;
2241
Willy Tarreau506c69a2014-07-08 00:59:48 +02002242 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002243 goto out;
2244 q += (*qvalue++ - '0') * 10;
2245
Willy Tarreau506c69a2014-07-08 00:59:48 +02002246 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002247 goto out;
2248 q += (*qvalue++ - '0') * 1;
2249 out:
2250 if (q > 1000)
2251 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002252 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002253 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002254 return q;
2255}
William Lallemand82fe75c2012-10-23 10:25:10 +02002256
2257/*
2258 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002259 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002260int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002261{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002262 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002263 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 struct hdr_ctx ctx;
2265 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002266 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002267
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002268 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2269 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002270 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2271 */
2272 ctx.idx = 0;
2273 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2274 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002275 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2276 (ctx.vlen < 31 ||
2277 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2278 ctx.line[ctx.val + 30] < '6' ||
2279 (ctx.line[ctx.val + 30] == '6' &&
2280 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2281 s->comp_algo = NULL;
2282 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002283 }
2284
William Lallemand82fe75c2012-10-23 10:25:10 +02002285 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002286 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002287 int best_q = 0;
2288
William Lallemand82fe75c2012-10-23 10:25:10 +02002289 ctx.idx = 0;
2290 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002291 const char *qval;
2292 int q;
2293 int toklen;
2294
2295 /* try to isolate the token from the optional q-value */
2296 toklen = 0;
2297 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2298 toklen++;
2299
2300 qval = ctx.line + ctx.val + toklen;
2301 while (1) {
2302 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2303 qval++;
2304
2305 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2306 qval = NULL;
2307 break;
2308 }
2309 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002310
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002311 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2312 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002313
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002314 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2315 qval = NULL;
2316 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002317 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002318 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2319 break;
2320
2321 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2322 qval++;
2323 }
2324
2325 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002326 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002327
2328 if (q <= best_q)
2329 continue;
2330
2331 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2332 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002333 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002334 s->comp_algo = comp_algo;
2335 best_q = q;
2336 break;
2337 }
2338 }
2339 }
2340 }
2341
2342 /* remove all occurrences of the header when "compression offload" is set */
2343 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002344 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002345 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2346 ctx.idx = 0;
2347 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2348 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002349 }
2350 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002351 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002352 }
2353
2354 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002355 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002356 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002357 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002358 s->comp_algo = comp_algo;
2359 return 1;
2360 }
2361 }
2362 }
2363
2364 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002365 return 0;
2366}
2367
2368/*
2369 * Selects a comression algorithm depending of the server response.
2370 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002371int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002372{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002373 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002374 struct http_msg *msg = &txn->rsp;
2375 struct hdr_ctx ctx;
2376 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002377
2378 /* no common compression algorithm was found in request header */
2379 if (s->comp_algo == NULL)
2380 goto fail;
2381
2382 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002383 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002384 goto fail;
2385
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002386 /* compress 200,201,202,203 responses only */
2387 if ((txn->status != 200) &&
2388 (txn->status != 201) &&
2389 (txn->status != 202) &&
2390 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002391 goto fail;
2392
William Lallemand82fe75c2012-10-23 10:25:10 +02002393 /* Content-Length is null */
2394 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2395 goto fail;
2396
2397 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002398 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002399 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2400 goto fail;
2401
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002402 /* no compression when Cache-Control: no-transform is present in the message */
2403 ctx.idx = 0;
2404 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2405 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2406 goto fail;
2407 }
2408
William Lallemand82fe75c2012-10-23 10:25:10 +02002409 comp_type = NULL;
2410
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002411 /* we don't want to compress multipart content-types, nor content-types that are
2412 * not listed in the "compression type" directive if any. If no content-type was
2413 * found but configuration requires one, we don't compress either. Backend has
2414 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002415 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002416 ctx.idx = 0;
2417 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2418 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2419 goto fail;
2420
2421 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002422 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002423 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002424 if (ctx.vlen >= comp_type->name_len &&
2425 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002426 /* this Content-Type should be compressed */
2427 break;
2428 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002429 /* this Content-Type should not be compressed */
2430 if (comp_type == NULL)
2431 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002432 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002433 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002434 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002435 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002436 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002437 }
2438
William Lallemandd85f9172012-11-09 17:05:39 +01002439 /* limit compression rate */
2440 if (global.comp_rate_lim > 0)
2441 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2442 goto fail;
2443
William Lallemand072a2bf2012-11-20 17:01:01 +01002444 /* limit cpu usage */
2445 if (idle_pct < compress_min_idle)
2446 goto fail;
2447
William Lallemand4c49fae2012-11-07 15:00:23 +01002448 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002449 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002450 goto fail;
2451
Willy Tarreaue7dff022015-04-03 01:14:29 +02002452 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002453
William Lallemand82fe75c2012-10-23 10:25:10 +02002454 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002455 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002456 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2457 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2458
2459 /* add Transfer-Encoding header */
2460 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2461 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2462
2463 /*
2464 * Add Content-Encoding header when it's not identity encoding.
2465 * RFC 2616 : Identity encoding: This content-coding is used only in the
2466 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2467 * header.
2468 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002469 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002470 trash.len = 18;
2471 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002472 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2473 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002474 trash.str[trash.len] = '\0';
2475 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002476 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002477 return 1;
2478
2479fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002480 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002481 return 0;
2482}
2483
Willy Tarreau87b09662015-04-03 00:22:06 +02002484void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002485{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002486 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002487 int tmp = TX_CON_WANT_KAL;
2488
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002489 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2490 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002491 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2492 tmp = TX_CON_WANT_TUN;
2493
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002494 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002495 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2496 tmp = TX_CON_WANT_TUN;
2497 }
2498
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002499 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002500 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2501 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002502 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002503 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2504 tmp = TX_CON_WANT_CLO;
2505 else
2506 tmp = TX_CON_WANT_SCL;
2507 }
2508
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002509 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002510 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2511 tmp = TX_CON_WANT_CLO;
2512
2513 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2514 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2515
2516 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2517 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2518 /* parse the Connection header and possibly clean it */
2519 int to_del = 0;
2520 if ((msg->flags & HTTP_MSGF_VER_11) ||
2521 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002522 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002523 to_del |= 2; /* remove "keep-alive" */
2524 if (!(msg->flags & HTTP_MSGF_VER_11))
2525 to_del |= 1; /* remove "close" */
2526 http_parse_connection_header(txn, msg, to_del);
2527 }
2528
2529 /* check if client or config asks for explicit close in KAL/SCL */
2530 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2531 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2532 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2533 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2534 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002535 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002536 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2537}
William Lallemand82fe75c2012-10-23 10:25:10 +02002538
Willy Tarreaud787e662009-07-07 10:14:51 +02002539/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2540 * processing can continue on next analysers, or zero if it either needs more
2541 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002542 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002543 * when it has nothing left to do, and may remove any analyser when it wants to
2544 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002545 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002546int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002547{
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 /*
2549 * We will parse the partial (or complete) lines.
2550 * We will check the request syntax, and also join multi-line
2551 * headers. An index of all the lines will be elaborated while
2552 * parsing.
2553 *
2554 * For the parsing, we use a 28 states FSM.
2555 *
2556 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002557 * req->buf->p = beginning of request
2558 * req->buf->p + msg->eoh = end of processed headers / start of current one
2559 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002560 * msg->eol = end of current header or line (LF or CRLF)
2561 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002562 *
2563 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002564 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002565 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2566 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002568
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002570 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002571 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002573 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002574
Willy Tarreau87b09662015-04-03 00:22:06 +02002575 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 +01002576 now_ms, __FUNCTION__,
2577 s,
2578 req,
2579 req->rex, req->wex,
2580 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002581 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002582 req->analysers);
2583
Willy Tarreau52a0c602009-08-16 22:45:38 +02002584 /* we're speaking HTTP here, so let's speak HTTP to the client */
2585 s->srv_error = http_return_srv_error;
2586
Willy Tarreau83e3af02009-12-28 17:39:57 +01002587 /* There's a protected area at the end of the buffer for rewriting
2588 * purposes. We don't want to start to parse the request if the
2589 * protected area is affected, because we may have to move processed
2590 * data later, which is much more complicated.
2591 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002592 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002593 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002594 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002595 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002596 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002597 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002598 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002599 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002600 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002601 return 0;
2602 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002603 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2604 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2605 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002606 }
2607
Willy Tarreau065e8332010-01-08 00:30:20 +01002608 /* Note that we have the same problem with the response ; we
2609 * may want to send a redirect, error or anything which requires
2610 * some spare space. So we'll ensure that we have at least
2611 * maxrewrite bytes available in the response buffer before
2612 * processing that one. This will only affect pipelined
2613 * keep-alive requests.
2614 */
2615 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002616 unlikely(!channel_is_rewritable(&s->res) ||
2617 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2618 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2619 if (s->res.buf->o) {
2620 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002621 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002622 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002623 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002624 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2625 s->res.flags |= CF_WAKE_WRITE;
2626 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002627 return 0;
2628 }
2629 }
2630
Willy Tarreau9b28e032012-10-12 23:49:43 +02002631 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002632 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002633 }
2634
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 /* 1: we might have to print this header in debug mode */
2636 if (unlikely((global.mode & MODE_DEBUG) &&
2637 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002638 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002640
Willy Tarreau9b28e032012-10-12 23:49:43 +02002641 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002642 /* this is a bit complex : in case of error on the request line,
2643 * we know that rq.l is still zero, so we display only the part
2644 * up to the end of the line (truncated by debug_hdr).
2645 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002646 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002648
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 sol += hdr_idx_first_pos(&txn->hdr_idx);
2650 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002651
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 while (cur_idx) {
2653 eol = sol + txn->hdr_idx.v[cur_idx].len;
2654 debug_hdr("clihdr", s, sol, eol);
2655 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2656 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002657 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 }
2659
Willy Tarreau58f10d72006-12-04 02:26:12 +01002660
Willy Tarreau59234e92008-11-30 23:51:27 +01002661 /*
2662 * Now we quickly check if we have found a full valid request.
2663 * If not so, we check the FD and buffer states before leaving.
2664 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002665 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002666 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002667 * on a keep-alive stream, if we encounter and error, close, t/o,
2668 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002669 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002670 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002671 * Last, we may increase some tracked counters' http request errors on
2672 * the cases that are deliberately the client's fault. For instance,
2673 * a timeout or connection reset is not counted as an error. However
2674 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002676
Willy Tarreau655dce92009-11-08 13:10:58 +01002677 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002678 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002680 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002681 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002682 stream_inc_http_req_ctr(s);
2683 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002684 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002686 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 /* 1: Since we are in header mode, if there's no space
2689 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002690 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002691 * must terminate it now.
2692 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002693 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 /* FIXME: check if URI is set and return Status
2695 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002696 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002697 stream_inc_http_req_ctr(s);
2698 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002699 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002700 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002701 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002702 goto return_bad_req;
2703 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002706 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002707 if (!(s->flags & SF_ERR_MASK))
2708 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002709
Willy Tarreaufcffa692010-01-10 14:21:19 +01002710 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002711 goto failed_keep_alive;
2712
Willy Tarreau0f228a02015-05-01 15:37:53 +02002713 if (sess->fe->options & PR_O_IGNORE_PRB)
2714 goto failed_keep_alive;
2715
Willy Tarreau59234e92008-11-30 23:51:27 +01002716 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002717 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002718 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002719 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002720 }
2721
Willy Tarreaudc979f22012-12-04 10:39:01 +01002722 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002723 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002724 msg->msg_state = HTTP_MSG_ERROR;
2725 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002726
Willy Tarreau87b09662015-04-03 00:22:06 +02002727 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002728 proxy_inc_fe_req_ctr(sess->fe);
2729 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002730 if (sess->listener->counters)
2731 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002732
Willy Tarreaue7dff022015-04-03 01:14:29 +02002733 if (!(s->flags & SF_FINST_MASK))
2734 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002735 return 0;
2736 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002737
Willy Tarreau59234e92008-11-30 23:51:27 +01002738 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002739 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002740 if (!(s->flags & SF_ERR_MASK))
2741 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002742
Willy Tarreaufcffa692010-01-10 14:21:19 +01002743 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002744 goto failed_keep_alive;
2745
Willy Tarreau0f228a02015-05-01 15:37:53 +02002746 if (sess->fe->options & PR_O_IGNORE_PRB)
2747 goto failed_keep_alive;
2748
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002750 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002751 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002752 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002753 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002754 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002755 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 msg->msg_state = HTTP_MSG_ERROR;
2757 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002758
Willy Tarreau87b09662015-04-03 00:22:06 +02002759 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002760 proxy_inc_fe_req_ctr(sess->fe);
2761 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002762 if (sess->listener->counters)
2763 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002764
Willy Tarreaue7dff022015-04-03 01:14:29 +02002765 if (!(s->flags & SF_FINST_MASK))
2766 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 return 0;
2768 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002771 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002772 if (!(s->flags & SF_ERR_MASK))
2773 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002774
Willy Tarreaufcffa692010-01-10 14:21:19 +01002775 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002776 goto failed_keep_alive;
2777
Willy Tarreau0f228a02015-05-01 15:37:53 +02002778 if (sess->fe->options & PR_O_IGNORE_PRB)
2779 goto failed_keep_alive;
2780
Willy Tarreau4076a152009-04-02 15:18:36 +02002781 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002782 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002783 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002784 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002785 msg->msg_state = HTTP_MSG_ERROR;
2786 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002787
Willy Tarreau87b09662015-04-03 00:22:06 +02002788 stream_inc_http_err_ctr(s);
2789 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002790 proxy_inc_fe_req_ctr(sess->fe);
2791 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002792 if (sess->listener->counters)
2793 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002794
Willy Tarreaue7dff022015-04-03 01:14:29 +02002795 if (!(s->flags & SF_FINST_MASK))
2796 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002797 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002798 }
2799
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002800 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002801 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002802 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002803#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002804 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2805 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002806 /* We need more data, we have to re-enable quick-ack in case we
2807 * previously disabled it, otherwise we might cause the client
2808 * to delay next data.
2809 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002810 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002811 }
2812#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002813
Willy Tarreaufcffa692010-01-10 14:21:19 +01002814 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2815 /* If the client starts to talk, let's fall back to
2816 * request timeout processing.
2817 */
2818 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002819 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002820 }
2821
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002823 if (!tick_isset(req->analyse_exp)) {
2824 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2825 (txn->flags & TX_WAIT_NEXT_RQ) &&
2826 tick_isset(s->be->timeout.httpka))
2827 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2828 else
2829 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2830 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002831
Willy Tarreau59234e92008-11-30 23:51:27 +01002832 /* we're not ready yet */
2833 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002834
2835 failed_keep_alive:
2836 /* Here we process low-level errors for keep-alive requests. In
2837 * short, if the request is not the first one and it experiences
2838 * a timeout, read error or shutdown, we just silently close so
2839 * that the client can try again.
2840 */
2841 txn->status = 0;
2842 msg->msg_state = HTTP_MSG_RQBEFORE;
2843 req->analysers = 0;
2844 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002845 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002846 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002847 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002848 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002849 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002850
Willy Tarreaud787e662009-07-07 10:14:51 +02002851 /* OK now we have a complete HTTP request with indexed headers. Let's
2852 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002853 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002854 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002855 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002856 * byte after the last LF. msg->sov points to the first byte of data.
2857 * msg->eol cannot be trusted because it may have been left uninitialized
2858 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002859 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002860
Willy Tarreau87b09662015-04-03 00:22:06 +02002861 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002862 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002863
Willy Tarreaub16a5742010-01-10 14:46:16 +01002864 if (txn->flags & TX_WAIT_NEXT_RQ) {
2865 /* kill the pending keep-alive timeout */
2866 txn->flags &= ~TX_WAIT_NEXT_RQ;
2867 req->analyse_exp = TICK_ETERNITY;
2868 }
2869
2870
Willy Tarreaud787e662009-07-07 10:14:51 +02002871 /* Maybe we found in invalid header name while we were configured not
2872 * to block on that, so we have to capture it now.
2873 */
2874 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002875 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002876
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 /*
2878 * 1: identify the method
2879 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002880 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002881
2882 /* we can make use of server redirect on GET and HEAD */
2883 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002884 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002885
Willy Tarreau59234e92008-11-30 23:51:27 +01002886 /*
2887 * 2: check if the URI matches the monitor_uri.
2888 * We have to do this for every request which gets in, because
2889 * the monitor-uri is defined by the frontend.
2890 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002891 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2892 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002893 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002894 sess->fe->monitor_uri,
2895 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002896 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002897 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002898 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002899 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002900
Willy Tarreaue7dff022015-04-03 01:14:29 +02002901 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002902 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002903
Willy Tarreau59234e92008-11-30 23:51:27 +01002904 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002905 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002906 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002907
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 ret = acl_pass(ret);
2909 if (cond->pol == ACL_COND_UNLESS)
2910 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002911
Willy Tarreau59234e92008-11-30 23:51:27 +01002912 if (ret) {
2913 /* we fail this request, let's return 503 service unavail */
2914 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002915 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002916 if (!(s->flags & SF_ERR_MASK))
2917 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002919 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002920 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002921
Willy Tarreau59234e92008-11-30 23:51:27 +01002922 /* nothing to fail, let's reply normaly */
2923 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002924 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002925 if (!(s->flags & SF_ERR_MASK))
2926 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002927 goto return_prx_cond;
2928 }
2929
2930 /*
2931 * 3: Maybe we have to copy the original REQURI for the logs ?
2932 * Note: we cannot log anymore if the request has been
2933 * classified as invalid.
2934 */
2935 if (unlikely(s->logs.logwait & LW_REQ)) {
2936 /* we have a complete HTTP request that we must log */
2937 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2938 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002939
Willy Tarreau59234e92008-11-30 23:51:27 +01002940 if (urilen >= REQURI_LEN)
2941 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002942 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002943 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002944
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002945 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002946 s->do_log(s);
2947 } else {
2948 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002949 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002950 }
Willy Tarreau06619262006-12-17 08:37:22 +01002951
Willy Tarreau91852eb2015-05-01 13:26:00 +02002952 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2953 * exactly one digit "." one digit. This check may be disabled using
2954 * option accept-invalid-http-request.
2955 */
2956 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2957 if (msg->sl.rq.v_l != 8) {
2958 msg->err_pos = msg->sl.rq.v;
2959 goto return_bad_req;
2960 }
2961
2962 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2963 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2964 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2965 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2966 msg->err_pos = msg->sl.rq.v + 4;
2967 goto return_bad_req;
2968 }
2969 }
Willy Tarreau13317662015-05-01 13:47:08 +02002970 else {
2971 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2972 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2973 goto return_bad_req;
2974 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002975
Willy Tarreau5b154472009-12-21 20:11:07 +01002976 /* ... and check if the request is HTTP/1.1 or above */
2977 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002978 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2979 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2980 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002981 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002982
2983 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002984 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 +01002985
Willy Tarreau88d349d2010-01-25 12:15:43 +01002986 /* if the frontend has "option http-use-proxy-header", we'll check if
2987 * we have what looks like a proxied connection instead of a connection,
2988 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2989 * Note that this is *not* RFC-compliant, however browsers and proxies
2990 * happen to do that despite being non-standard :-(
2991 * We consider that a request not beginning with either '/' or '*' is
2992 * a proxied connection, which covers both "scheme://location" and
2993 * CONNECT ip:port.
2994 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002995 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002996 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002997 txn->flags |= TX_USE_PX_CONN;
2998
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002999 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003000 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003001
Willy Tarreau59234e92008-11-30 23:51:27 +01003002 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003003 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003004 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003005 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003006
Willy Tarreau557f1992015-05-01 10:05:17 +02003007 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3008 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003009 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003010 * The length of a message body is determined by one of the following
3011 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003012 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003013 * 1. Any response to a HEAD request and any response with a 1xx
3014 * (Informational), 204 (No Content), or 304 (Not Modified) status
3015 * code is always terminated by the first empty line after the
3016 * header fields, regardless of the header fields present in the
3017 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003018 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003019 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3020 * the connection will become a tunnel immediately after the empty
3021 * line that concludes the header fields. A client MUST ignore any
3022 * Content-Length or Transfer-Encoding header fields received in
3023 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003024 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003025 * 3. If a Transfer-Encoding header field is present and the chunked
3026 * transfer coding (Section 4.1) is the final encoding, the message
3027 * body length is determined by reading and decoding the chunked
3028 * data until the transfer coding indicates the data is complete.
3029 *
3030 * If a Transfer-Encoding header field is present in a response and
3031 * the chunked transfer coding is not the final encoding, the
3032 * message body length is determined by reading the connection until
3033 * it is closed by the server. If a Transfer-Encoding header field
3034 * is present in a request and the chunked transfer coding is not
3035 * the final encoding, the message body length cannot be determined
3036 * reliably; the server MUST respond with the 400 (Bad Request)
3037 * status code and then close the connection.
3038 *
3039 * If a message is received with both a Transfer-Encoding and a
3040 * Content-Length header field, the Transfer-Encoding overrides the
3041 * Content-Length. Such a message might indicate an attempt to
3042 * perform request smuggling (Section 9.5) or response splitting
3043 * (Section 9.4) and ought to be handled as an error. A sender MUST
3044 * remove the received Content-Length field prior to forwarding such
3045 * a message downstream.
3046 *
3047 * 4. If a message is received without Transfer-Encoding and with
3048 * either multiple Content-Length header fields having differing
3049 * field-values or a single Content-Length header field having an
3050 * invalid value, then the message framing is invalid and the
3051 * recipient MUST treat it as an unrecoverable error. If this is a
3052 * request message, the server MUST respond with a 400 (Bad Request)
3053 * status code and then close the connection. If this is a response
3054 * message received by a proxy, the proxy MUST close the connection
3055 * to the server, discard the received response, and send a 502 (Bad
3056 * Gateway) response to the client. If this is a response message
3057 * received by a user agent, the user agent MUST close the
3058 * connection to the server and discard the received response.
3059 *
3060 * 5. If a valid Content-Length header field is present without
3061 * Transfer-Encoding, its decimal value defines the expected message
3062 * body length in octets. If the sender closes the connection or
3063 * the recipient times out before the indicated number of octets are
3064 * received, the recipient MUST consider the message to be
3065 * incomplete and close the connection.
3066 *
3067 * 6. If this is a request message and none of the above are true, then
3068 * the message body length is zero (no message body is present).
3069 *
3070 * 7. Otherwise, this is a response message without a declared message
3071 * body length, so the message body length is determined by the
3072 * number of octets received prior to the server closing the
3073 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003074 */
3075
Willy Tarreau32b47f42009-10-18 20:55:02 +02003076 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003077 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003078 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003079 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003080 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3081 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003082 /* chunked not last, return badreq */
3083 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003084 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003085 }
3086
Willy Tarreau1c913912015-04-30 10:57:51 +02003087 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003088 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003089 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3090 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3091 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3092 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003093 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003094 signed long long cl;
3095
Willy Tarreauad14f752011-09-02 20:33:27 +02003096 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003097 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003098 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003099 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003100
Willy Tarreauad14f752011-09-02 20:33:27 +02003101 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003102 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003103 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003104 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003105
Willy Tarreauad14f752011-09-02 20:33:27 +02003106 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003107 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003108 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003109 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003110
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003111 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003112 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003113 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003114 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003115
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003116 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003117 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003118 }
3119
Willy Tarreau34dfc602015-05-01 10:09:49 +02003120 /* even bodyless requests have a known length */
3121 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003122
Willy Tarreau179085c2014-04-28 16:48:56 +02003123 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3124 * only change if both the request and the config reference something else.
3125 * Option httpclose by itself sets tunnel mode where headers are mangled.
3126 * However, if another mode is set, it will affect it (eg: server-close/
3127 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3128 * if FE and BE have the same settings (common). The method consists in
3129 * checking if options changed between the two calls (implying that either
3130 * one is non-null, or one of them is non-null and we are there for the first
3131 * time.
3132 */
3133 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003134 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003135 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003136
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003137 /* we may have to wait for the request's body */
3138 if ((s->be->options & PR_O_WREQ_BODY) &&
3139 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3140 req->analysers |= AN_REQ_HTTP_BODY;
3141
Willy Tarreaud787e662009-07-07 10:14:51 +02003142 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003143 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003144 req->analyse_exp = TICK_ETERNITY;
3145 return 1;
3146
3147 return_bad_req:
3148 /* We centralize bad requests processing here */
3149 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3150 /* we detected a parsing error. We want to archive this request
3151 * in the dedicated proxy area for later troubleshooting.
3152 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003153 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003154 }
3155
3156 txn->req.msg_state = HTTP_MSG_ERROR;
3157 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003158 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003159
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003160 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003161 if (sess->listener->counters)
3162 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003163
3164 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003165 if (!(s->flags & SF_ERR_MASK))
3166 s->flags |= SF_ERR_PRXCOND;
3167 if (!(s->flags & SF_FINST_MASK))
3168 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003169
3170 req->analysers = 0;
3171 req->analyse_exp = TICK_ETERNITY;
3172 return 0;
3173}
3174
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003175
Willy Tarreau347a35d2013-11-22 17:51:09 +01003176/* This function prepares an applet to handle the stats. It can deal with the
3177 * "100-continue" expectation, check that admin rules are met for POST requests,
3178 * and program a response message if something was unexpected. It cannot fail
3179 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003180 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003181 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003182 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003183 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003184int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003185{
3186 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003187 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003188 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003189 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003190 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003191 struct uri_auth *uri_auth = s->be->uri_auth;
3192 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003193 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003194
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003195 appctx = si_appctx(si);
3196 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3197 appctx->st1 = appctx->st2 = 0;
3198 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3199 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003200 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003201 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003202
3203 uri = msg->chn->buf->p + msg->sl.rq.u;
3204 lookup = uri + uri_auth->uri_len;
3205
3206 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3207 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003208 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003209 break;
3210 }
3211 }
3212
3213 if (uri_auth->refresh) {
3214 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3215 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003216 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003217 break;
3218 }
3219 }
3220 }
3221
3222 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3223 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003224 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003225 break;
3226 }
3227 }
3228
3229 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3230 if (memcmp(h, ";st=", 4) == 0) {
3231 int i;
3232 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003233 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003234 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3235 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003236 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003237 break;
3238 }
3239 }
3240 break;
3241 }
3242 }
3243
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003244 appctx->ctx.stats.scope_str = 0;
3245 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003246 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3247 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3248 int itx = 0;
3249 const char *h2;
3250 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3251 const char *err;
3252
3253 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3254 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003255 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003256 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3257 itx++;
3258 h++;
3259 }
3260
3261 if (itx > STAT_SCOPE_TXT_MAXLEN)
3262 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003263 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003264
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003265 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003266 memcpy(scope_txt, h2, itx);
3267 scope_txt[itx] = '\0';
3268 err = invalid_char(scope_txt);
3269 if (err) {
3270 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003271 appctx->ctx.stats.scope_str = 0;
3272 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003273 }
3274 break;
3275 }
3276 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003277
3278 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003279 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003280 int ret = 1;
3281
3282 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003283 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 +01003284 ret = acl_pass(ret);
3285 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3286 ret = !ret;
3287 }
3288
3289 if (ret) {
3290 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003291 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003292 break;
3293 }
3294 }
3295
3296 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003297 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003298 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003299 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003300 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3301 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003302 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003303 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003304 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003305 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3306 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003307 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003308 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003309 else {
3310 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003311 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003312 }
3313
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003314 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003315 return 1;
3316}
3317
Lukas Tribus67db8df2013-06-23 17:37:13 +02003318/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3319 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3320 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003321void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003322{
3323#ifdef IP_TOS
3324 if (from.ss_family == AF_INET)
3325 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3326#endif
3327#ifdef IPV6_TCLASS
3328 if (from.ss_family == AF_INET6) {
3329 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3330 /* v4-mapped addresses need IP_TOS */
3331 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3332 else
3333 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3334 }
3335#endif
3336}
3337
Willy Tarreau87b09662015-04-03 00:22:06 +02003338int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003339 const char* name, unsigned int name_len,
3340 const char *str, struct my_regex *re,
3341 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003342{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003343 struct hdr_ctx ctx;
3344 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003345 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003346 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3347 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003348 struct chunk *output = get_trash_chunk();
3349
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003350 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003351
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003352 /* Choose the header browsing function. */
3353 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003354 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003355 http_find_hdr_func = http_find_header2;
3356 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003357 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003358 http_find_hdr_func = http_find_full_header2;
3359 break;
3360 default: /* impossible */
3361 return -1;
3362 }
3363
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003364 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3365 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003366 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003367 char *val = ctx.line + ctx.val;
3368 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003369
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003370 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3371 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003372
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003373 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003374 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003375 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003376
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003377 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003378
3379 hdr->len += delta;
3380 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003381
3382 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003383 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003384 }
3385
3386 return 0;
3387}
3388
Willy Tarreau87b09662015-04-03 00:22:06 +02003389static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003390 const char* name, unsigned int name_len,
3391 struct list *fmt, struct my_regex *re,
3392 int action)
3393{
3394 struct chunk *replace = get_trash_chunk();
3395
3396 replace->len = build_logline(s, replace->str, replace->size, fmt);
3397 if (replace->len >= replace->size - 1)
3398 return -1;
3399
3400 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3401}
3402
Willy Tarreau87b09662015-04-03 00:22:06 +02003403/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003404 * transaction <txn>. Returns the verdict of the first rule that prevents
3405 * further processing of the request (auth, deny, ...), and defaults to
3406 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3407 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3408 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003409 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003410enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003411http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003412{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003413 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003414 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003415 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003416 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003417 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003418 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003419
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003420 /* If "the current_rule_list" match the executed rule list, we are in
3421 * resume condition. If a resume is needed it is always in the action
3422 * and never in the ACL or converters. In this case, we initialise the
3423 * current rule, and go to the action execution point.
3424 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003425 if (s->current_rule) {
3426 rule = s->current_rule;
3427 s->current_rule = NULL;
3428 if (s->current_rule_list == rules)
3429 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003430 }
3431 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003432
Willy Tarreauff011f22011-01-06 17:51:27 +01003433 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003434
Willy Tarreau96257ec2012-12-27 10:46:37 +01003435 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003436 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003437 int ret;
3438
Willy Tarreau192252e2015-04-04 01:47:55 +02003439 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003440 ret = acl_pass(ret);
3441
Willy Tarreauff011f22011-01-06 17:51:27 +01003442 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003443 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003444
3445 if (!ret) /* condition not matched */
3446 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003447 }
3448
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003449resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003450 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003451 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003452 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003453
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003454 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003455 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003456 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003457
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003458 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003459 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003460 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003461 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003462
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003463 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003464 /* Auth might be performed on regular http-req rules as well as on stats */
3465 auth_realm = rule->arg.auth.realm;
3466 if (!auth_realm) {
3467 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3468 auth_realm = STATS_DEFAULT_REALM;
3469 else
3470 auth_realm = px->id;
3471 }
3472 /* send 401/407 depending on whether we use a proxy or not. We still
3473 * count one error, because normal browsing won't significantly
3474 * increase the counter but brute force attempts will.
3475 */
3476 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3477 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3478 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003479 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003480 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003481
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003482 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003483 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3484 return HTTP_RULE_RES_BADREQ;
3485 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003486
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003487 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003488 s->task->nice = rule->arg.nice;
3489 break;
3490
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003491 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003492 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003493 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003494 break;
3495
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003496 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003497#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003498 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003499 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003500#endif
3501 break;
3502
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003503 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003504 s->logs.level = rule->arg.loglevel;
3505 break;
3506
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003507 case ACT_HTTP_REPLACE_HDR:
3508 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003509 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3510 rule->arg.hdr_add.name_len,
3511 &rule->arg.hdr_add.fmt,
3512 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003513 return HTTP_RULE_RES_BADREQ;
3514 break;
3515
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003516 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003517 ctx.idx = 0;
3518 /* remove all occurrences of the header */
3519 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3520 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3521 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003522 }
Willy Tarreau85603282015-01-21 20:39:27 +01003523 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003524
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003525 case ACT_HTTP_SET_HDR:
3526 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003527 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3528 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3529 trash.len = rule->arg.hdr_add.name_len;
3530 trash.str[trash.len++] = ':';
3531 trash.str[trash.len++] = ' ';
3532 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 +01003533
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003534 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003535 /* remove all occurrences of the header */
3536 ctx.idx = 0;
3537 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3538 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3539 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3540 }
3541 }
3542
Willy Tarreau96257ec2012-12-27 10:46:37 +01003543 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3544 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003545
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003546 case ACT_HTTP_DEL_ACL:
3547 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003548 struct pat_ref *ref;
3549 char *key;
3550 int len;
3551
3552 /* collect reference */
3553 ref = pat_ref_lookup(rule->arg.map.ref);
3554 if (!ref)
3555 continue;
3556
3557 /* collect key */
3558 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3559 key = trash.str;
3560 key[len] = '\0';
3561
3562 /* perform update */
3563 /* returned code: 1=ok, 0=ko */
3564 pat_ref_delete(ref, key);
3565
3566 break;
3567 }
3568
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003569 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003570 struct pat_ref *ref;
3571 char *key;
3572 struct chunk *trash_key;
3573 int len;
3574
3575 trash_key = get_trash_chunk();
3576
3577 /* collect reference */
3578 ref = pat_ref_lookup(rule->arg.map.ref);
3579 if (!ref)
3580 continue;
3581
3582 /* collect key */
3583 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3584 key = trash_key->str;
3585 key[len] = '\0';
3586
3587 /* perform update */
3588 /* add entry only if it does not already exist */
3589 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003590 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003591
3592 break;
3593 }
3594
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003595 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003596 struct pat_ref *ref;
3597 char *key, *value;
3598 struct chunk *trash_key, *trash_value;
3599 int len;
3600
3601 trash_key = get_trash_chunk();
3602 trash_value = get_trash_chunk();
3603
3604 /* collect reference */
3605 ref = pat_ref_lookup(rule->arg.map.ref);
3606 if (!ref)
3607 continue;
3608
3609 /* collect key */
3610 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3611 key = trash_key->str;
3612 key[len] = '\0';
3613
3614 /* collect value */
3615 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3616 value = trash_value->str;
3617 value[len] = '\0';
3618
3619 /* perform update */
3620 if (pat_ref_find_elt(ref, key) != NULL)
3621 /* update entry if it exists */
3622 pat_ref_set(ref, key, value, NULL);
3623 else
3624 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003625 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003626
3627 break;
3628 }
William Lallemand73025dd2014-04-24 14:38:37 +02003629
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +02003630 case ACT_ACTION_CONT:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003631 switch (rule->action_ptr(rule, px, s->sess, s)) {
3632 case ACT_RET_ERR:
3633 case ACT_RET_CONT:
3634 break;
3635 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003636 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003637 return HTTP_RULE_RES_YIELD;
3638 }
William Lallemand73025dd2014-04-24 14:38:37 +02003639 break;
3640
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +02003641 case ACT_ACTION_STOP:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003642 switch (rule->action_ptr(rule, px, s->sess, s)) {
3643 case ACT_RET_YIELD:
3644 case ACT_RET_ERR:
3645 case ACT_RET_CONT:
3646 return HTTP_RULE_RES_DONE;
3647 }
3648 break;
Willy Tarreau09448f72014-06-25 18:12:15 +02003649
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003650 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003651 /* Note: only the first valid tracking parameter of each
3652 * applies.
3653 */
3654
3655 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3656 struct stktable *t;
3657 struct stksess *ts;
3658 struct stktable_key *key;
3659 void *ptr;
3660
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003661 t = rule->arg.trk_ctr.table.t;
3662 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 +02003663
3664 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003665 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003666
3667 /* let's count a new HTTP request as it's the first time we do it */
3668 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3669 if (ptr)
3670 stktable_data_cast(ptr, http_req_cnt)++;
3671
3672 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3673 if (ptr)
3674 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3675 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3676
3677 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003678 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003679 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3680 }
3681 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003682 break;
3683
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003684 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003685 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3686 struct sample *smp;
3687
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003688 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR);
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003689
3690 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003691 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003692 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003693 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003694 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003695 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003696 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003697 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr));
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003698 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3699 }
3700 }
3701 }
3702 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003703
3704 /* other flags exists, but normaly, they never be matched. */
3705 default:
3706 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003707 }
3708 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003709
3710 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003711 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003712}
3713
Willy Tarreau71241ab2012-12-27 11:30:54 +01003714
Willy Tarreau51d861a2015-05-22 17:30:48 +02003715/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3716 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3717 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3718 * is returned, the process can continue the evaluation of next rule list. If
3719 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3720 * is returned, it means the operation could not be processed and a server error
3721 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3722 * deny rule. If *YIELD is returned, the caller must call again the function
3723 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003724 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003725static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003726http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003727{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003728 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003729 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003730 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003731 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003732 struct hdr_ctx ctx;
3733
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003734 /* If "the current_rule_list" match the executed rule list, we are in
3735 * resume condition. If a resume is needed it is always in the action
3736 * and never in the ACL or converters. In this case, we initialise the
3737 * current rule, and go to the action execution point.
3738 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003739 if (s->current_rule) {
3740 rule = s->current_rule;
3741 s->current_rule = NULL;
3742 if (s->current_rule_list == rules)
3743 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003744 }
3745 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003746
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003747 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003748
3749 /* check optional condition */
3750 if (rule->cond) {
3751 int ret;
3752
Willy Tarreau192252e2015-04-04 01:47:55 +02003753 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003754 ret = acl_pass(ret);
3755
3756 if (rule->cond->pol == ACL_COND_UNLESS)
3757 ret = !ret;
3758
3759 if (!ret) /* condition not matched */
3760 continue;
3761 }
3762
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003763resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003764 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003765 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003766 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003767
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003768 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003769 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003770 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003771
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003772 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003773 s->task->nice = rule->arg.nice;
3774 break;
3775
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003776 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003777 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003778 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003779 break;
3780
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003781 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003782#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003783 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003784 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003785#endif
3786 break;
3787
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003788 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003789 s->logs.level = rule->arg.loglevel;
3790 break;
3791
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003792 case ACT_HTTP_REPLACE_HDR:
3793 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003794 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3795 rule->arg.hdr_add.name_len,
3796 &rule->arg.hdr_add.fmt,
3797 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003798 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003799 break;
3800
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003801 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802 ctx.idx = 0;
3803 /* remove all occurrences of the header */
3804 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3805 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3806 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3807 }
Willy Tarreau85603282015-01-21 20:39:27 +01003808 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003809
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003810 case ACT_HTTP_SET_HDR:
3811 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003812 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3813 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3814 trash.len = rule->arg.hdr_add.name_len;
3815 trash.str[trash.len++] = ':';
3816 trash.str[trash.len++] = ' ';
3817 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 +01003818
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003819 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003820 /* remove all occurrences of the header */
3821 ctx.idx = 0;
3822 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3823 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3824 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3825 }
3826 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003827 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3828 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003829
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003830 case ACT_HTTP_DEL_ACL:
3831 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003832 struct pat_ref *ref;
3833 char *key;
3834 int len;
3835
3836 /* collect reference */
3837 ref = pat_ref_lookup(rule->arg.map.ref);
3838 if (!ref)
3839 continue;
3840
3841 /* collect key */
3842 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3843 key = trash.str;
3844 key[len] = '\0';
3845
3846 /* perform update */
3847 /* returned code: 1=ok, 0=ko */
3848 pat_ref_delete(ref, key);
3849
3850 break;
3851 }
3852
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003853 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003854 struct pat_ref *ref;
3855 char *key;
3856 struct chunk *trash_key;
3857 int len;
3858
3859 trash_key = get_trash_chunk();
3860
3861 /* collect reference */
3862 ref = pat_ref_lookup(rule->arg.map.ref);
3863 if (!ref)
3864 continue;
3865
3866 /* collect key */
3867 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3868 key = trash_key->str;
3869 key[len] = '\0';
3870
3871 /* perform update */
3872 /* check if the entry already exists */
3873 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003874 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003875
3876 break;
3877 }
3878
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003879 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003880 struct pat_ref *ref;
3881 char *key, *value;
3882 struct chunk *trash_key, *trash_value;
3883 int len;
3884
3885 trash_key = get_trash_chunk();
3886 trash_value = get_trash_chunk();
3887
3888 /* collect reference */
3889 ref = pat_ref_lookup(rule->arg.map.ref);
3890 if (!ref)
3891 continue;
3892
3893 /* collect key */
3894 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3895 key = trash_key->str;
3896 key[len] = '\0';
3897
3898 /* collect value */
3899 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3900 value = trash_value->str;
3901 value[len] = '\0';
3902
3903 /* perform update */
3904 if (pat_ref_find_elt(ref, key) != NULL)
3905 /* update entry if it exists */
3906 pat_ref_set(ref, key, value, NULL);
3907 else
3908 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003909 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003910
3911 break;
3912 }
William Lallemand73025dd2014-04-24 14:38:37 +02003913
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003914 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003915 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3916 return HTTP_RULE_RES_BADREQ;
3917 return HTTP_RULE_RES_DONE;
3918
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +02003919 case ACT_ACTION_CONT:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003920 switch (rule->action_ptr(rule, px, s->sess, s)) {
3921 case ACT_RET_ERR:
3922 case ACT_RET_CONT:
3923 break;
3924 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003925 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003926 return HTTP_RULE_RES_YIELD;
3927 }
William Lallemand73025dd2014-04-24 14:38:37 +02003928 break;
3929
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +02003930 case ACT_ACTION_STOP:
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003931 rule->action_ptr(rule, px, s->sess, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003932 return HTTP_RULE_RES_STOP;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003933
3934 /* other flags exists, but normaly, they never be matched. */
3935 default:
3936 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003937 }
3938 }
3939
3940 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003941 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003942}
3943
3944
Willy Tarreau71241ab2012-12-27 11:30:54 +01003945/* Perform an HTTP redirect based on the information in <rule>. The function
3946 * returns non-zero on success, or zero in case of a, irrecoverable error such
3947 * as too large a request to build a valid response.
3948 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003949static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003950{
Willy Tarreaub329a312015-05-22 16:27:37 +02003951 struct http_msg *req = &txn->req;
3952 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003953 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003954 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003955
3956 /* build redirect message */
3957 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003958 case 308:
3959 msg_fmt = HTTP_308;
3960 break;
3961 case 307:
3962 msg_fmt = HTTP_307;
3963 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003964 case 303:
3965 msg_fmt = HTTP_303;
3966 break;
3967 case 301:
3968 msg_fmt = HTTP_301;
3969 break;
3970 case 302:
3971 default:
3972 msg_fmt = HTTP_302;
3973 break;
3974 }
3975
3976 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3977 return 0;
3978
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003979 location = trash.str + trash.len;
3980
Willy Tarreau71241ab2012-12-27 11:30:54 +01003981 switch(rule->type) {
3982 case REDIRECT_TYPE_SCHEME: {
3983 const char *path;
3984 const char *host;
3985 struct hdr_ctx ctx;
3986 int pathlen;
3987 int hostlen;
3988
3989 host = "";
3990 hostlen = 0;
3991 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003992 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003993 host = ctx.line + ctx.val;
3994 hostlen = ctx.vlen;
3995 }
3996
3997 path = http_get_path(txn);
3998 /* build message using path */
3999 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004000 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004001 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4002 int qs = 0;
4003 while (qs < pathlen) {
4004 if (path[qs] == '?') {
4005 pathlen = qs;
4006 break;
4007 }
4008 qs++;
4009 }
4010 }
4011 } else {
4012 path = "/";
4013 pathlen = 1;
4014 }
4015
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004016 if (rule->rdr_str) { /* this is an old "redirect" rule */
4017 /* check if we can add scheme + "://" + host + path */
4018 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4019 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004020
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004021 /* add scheme */
4022 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4023 trash.len += rule->rdr_len;
4024 }
4025 else {
4026 /* add scheme with executing log format */
4027 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004028
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004029 /* check if we can add scheme + "://" + host + path */
4030 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4031 return 0;
4032 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004033 /* add "://" */
4034 memcpy(trash.str + trash.len, "://", 3);
4035 trash.len += 3;
4036
4037 /* add host */
4038 memcpy(trash.str + trash.len, host, hostlen);
4039 trash.len += hostlen;
4040
4041 /* add path */
4042 memcpy(trash.str + trash.len, path, pathlen);
4043 trash.len += pathlen;
4044
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004045 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004046 if (trash.len && trash.str[trash.len - 1] != '/' &&
4047 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4048 if (trash.len > trash.size - 5)
4049 return 0;
4050 trash.str[trash.len] = '/';
4051 trash.len++;
4052 }
4053
4054 break;
4055 }
4056 case REDIRECT_TYPE_PREFIX: {
4057 const char *path;
4058 int pathlen;
4059
4060 path = http_get_path(txn);
4061 /* build message using path */
4062 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004063 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004064 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4065 int qs = 0;
4066 while (qs < pathlen) {
4067 if (path[qs] == '?') {
4068 pathlen = qs;
4069 break;
4070 }
4071 qs++;
4072 }
4073 }
4074 } else {
4075 path = "/";
4076 pathlen = 1;
4077 }
4078
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004079 if (rule->rdr_str) { /* this is an old "redirect" rule */
4080 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4081 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004082
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004083 /* add prefix. Note that if prefix == "/", we don't want to
4084 * add anything, otherwise it makes it hard for the user to
4085 * configure a self-redirection.
4086 */
4087 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4088 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4089 trash.len += rule->rdr_len;
4090 }
4091 }
4092 else {
4093 /* add prefix with executing log format */
4094 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4095
4096 /* Check length */
4097 if (trash.len + pathlen > trash.size - 4)
4098 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004099 }
4100
4101 /* add path */
4102 memcpy(trash.str + trash.len, path, pathlen);
4103 trash.len += pathlen;
4104
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004105 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004106 if (trash.len && trash.str[trash.len - 1] != '/' &&
4107 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4108 if (trash.len > trash.size - 5)
4109 return 0;
4110 trash.str[trash.len] = '/';
4111 trash.len++;
4112 }
4113
4114 break;
4115 }
4116 case REDIRECT_TYPE_LOCATION:
4117 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004118 if (rule->rdr_str) { /* this is an old "redirect" rule */
4119 if (trash.len + rule->rdr_len > trash.size - 4)
4120 return 0;
4121
4122 /* add location */
4123 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4124 trash.len += rule->rdr_len;
4125 }
4126 else {
4127 /* add location with executing log format */
4128 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004129
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004130 /* Check left length */
4131 if (trash.len > trash.size - 4)
4132 return 0;
4133 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004134 break;
4135 }
4136
4137 if (rule->cookie_len) {
4138 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4139 trash.len += 14;
4140 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4141 trash.len += rule->cookie_len;
4142 memcpy(trash.str + trash.len, "\r\n", 2);
4143 trash.len += 2;
4144 }
4145
4146 /* add end of headers and the keep-alive/close status.
4147 * We may choose to set keep-alive if the Location begins
4148 * with a slash, because the client will come back to the
4149 * same server.
4150 */
4151 txn->status = rule->code;
4152 /* let's log the request time */
4153 s->logs.tv_request = now;
4154
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004155 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004156 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004157 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004158 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4159 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4160 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004161 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004162 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4163 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4164 trash.len += 30;
4165 } else {
4166 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4167 trash.len += 24;
4168 }
4169 }
4170 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4171 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004172 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004173 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004174 bi_fast_delete(req->chn->buf, req->sov);
4175 req->next -= req->sov;
4176 req->sov = 0;
4177 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004178 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004179 req->msg_state = HTTP_MSG_CLOSED;
4180 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004181 /* Trim any possible response */
4182 res->chn->buf->i = 0;
4183 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004184 } else {
4185 /* keep-alive not possible */
4186 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4187 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4188 trash.len += 29;
4189 } else {
4190 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4191 trash.len += 23;
4192 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004193 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004194 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004195 }
4196
Willy Tarreaue7dff022015-04-03 01:14:29 +02004197 if (!(s->flags & SF_ERR_MASK))
4198 s->flags |= SF_ERR_LOCAL;
4199 if (!(s->flags & SF_FINST_MASK))
4200 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004201
4202 return 1;
4203}
4204
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004205/* This stream analyser runs all HTTP request processing which is common to
4206 * frontends and backends, which means blocking ACLs, filters, connection-close,
4207 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004208 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004209 * either needs more data or wants to immediately abort the request (eg: deny,
4210 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004211 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004212int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004213{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004214 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004215 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004216 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004217 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004218 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004219 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004220
Willy Tarreau655dce92009-11-08 13:10:58 +01004221 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004222 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004223 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004224 }
4225
Willy Tarreau87b09662015-04-03 00:22:06 +02004226 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 +02004227 now_ms, __FUNCTION__,
4228 s,
4229 req,
4230 req->rex, req->wex,
4231 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004232 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004233 req->analysers);
4234
Willy Tarreau65410832014-04-28 21:25:43 +02004235 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004236 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004237
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004238 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004239 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004240 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004241
Willy Tarreau0b748332014-04-29 00:13:29 +02004242 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004243 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4244 goto return_prx_yield;
4245
Willy Tarreau0b748332014-04-29 00:13:29 +02004246 case HTTP_RULE_RES_CONT:
4247 case HTTP_RULE_RES_STOP: /* nothing to do */
4248 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004249
Willy Tarreau0b748332014-04-29 00:13:29 +02004250 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4251 if (txn->flags & TX_CLTARPIT)
4252 goto tarpit;
4253 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004254
Willy Tarreau0b748332014-04-29 00:13:29 +02004255 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4256 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004257
Willy Tarreau0b748332014-04-29 00:13:29 +02004258 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004259 goto done;
4260
Willy Tarreau0b748332014-04-29 00:13:29 +02004261 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4262 goto return_bad_req;
4263 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004264 }
4265
Willy Tarreau52542592014-04-28 18:33:26 +02004266 /* OK at this stage, we know that the request was accepted according to
4267 * the http-request rules, we can check for the stats. Note that the
4268 * URI is detected *before* the req* rules in order not to be affected
4269 * by a possible reqrep, while they are processed *after* so that a
4270 * reqdeny can still block them. This clearly needs to change in 1.6!
4271 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004272 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004273 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004274 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004275 txn->status = 500;
4276 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004277 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004278
Willy Tarreaue7dff022015-04-03 01:14:29 +02004279 if (!(s->flags & SF_ERR_MASK))
4280 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004281 goto return_prx_cond;
4282 }
4283
4284 /* parse the whole stats request and extract the relevant information */
4285 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004286 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004287 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004288
Willy Tarreau0b748332014-04-29 00:13:29 +02004289 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4290 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004291
Willy Tarreau0b748332014-04-29 00:13:29 +02004292 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4293 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004294 }
4295
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004296 /* evaluate the req* rules except reqadd */
4297 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004298 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004299 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004300
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004301 if (txn->flags & TX_CLDENY)
4302 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004303
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004304 if (txn->flags & TX_CLTARPIT)
4305 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004306 }
Willy Tarreau06619262006-12-17 08:37:22 +01004307
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004308 /* add request headers from the rule sets in the same order */
4309 list_for_each_entry(wl, &px->req_add, list) {
4310 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004311 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004312 ret = acl_pass(ret);
4313 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4314 ret = !ret;
4315 if (!ret)
4316 continue;
4317 }
4318
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004319 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004320 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004321 }
4322
Willy Tarreau52542592014-04-28 18:33:26 +02004323
4324 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004325 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004326 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004327 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4328 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004329
Willy Tarreaue7dff022015-04-03 01:14:29 +02004330 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4331 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4332 if (!(s->flags & SF_FINST_MASK))
4333 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004334
Willy Tarreau70730dd2014-04-24 18:06:27 +02004335 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004336 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004337 select_compression_request_header(s, req->buf);
4338
4339 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004340 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004341 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004342 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004343
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004344 /* check whether we have some ACLs set to redirect this request */
4345 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004346 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004347 int ret;
4348
Willy Tarreau192252e2015-04-04 01:47:55 +02004349 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004350 ret = acl_pass(ret);
4351 if (rule->cond->pol == ACL_COND_UNLESS)
4352 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004353 if (!ret)
4354 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004355 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004356 if (!http_apply_redirect_rule(rule, s, txn))
4357 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004358 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004359 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004360
Willy Tarreau2be39392010-01-03 17:24:51 +01004361 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4362 * If this happens, then the data will not come immediately, so we must
4363 * send all what we have without waiting. Note that due to the small gain
4364 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004365 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004366 * itself once used.
4367 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004368 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004369
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004370 done: /* done with this analyser, continue with next ones that the calling
4371 * points will have set, if any.
4372 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004373 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004374 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4375 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004376 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004377
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004378 tarpit:
4379 /* When a connection is tarpitted, we use the tarpit timeout,
4380 * which may be the same as the connect timeout if unspecified.
4381 * If unset, then set it to zero because we really want it to
4382 * eventually expire. We build the tarpit as an analyser.
4383 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004384 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004385
4386 /* wipe the request out so that we can drop the connection early
4387 * if the client closes first.
4388 */
4389 channel_dont_connect(req);
4390 req->analysers = 0; /* remove switching rules etc... */
4391 req->analysers |= AN_REQ_HTTP_TARPIT;
4392 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4393 if (!req->analyse_exp)
4394 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004395 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004396 sess->fe->fe_counters.denied_req++;
4397 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004398 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004399 if (sess->listener->counters)
4400 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004401 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004402
4403 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004404 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004405 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004406 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004407 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004408 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004409 sess->fe->fe_counters.denied_req++;
4410 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004411 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004412 if (sess->listener->counters)
4413 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004414 goto return_prx_cond;
4415
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004416 return_bad_req:
4417 /* We centralize bad requests processing here */
4418 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4419 /* we detected a parsing error. We want to archive this request
4420 * in the dedicated proxy area for later troubleshooting.
4421 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004422 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004423 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004424
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004425 txn->req.msg_state = HTTP_MSG_ERROR;
4426 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004427 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004428
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004429 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004430 if (sess->listener->counters)
4431 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004432
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004433 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004434 if (!(s->flags & SF_ERR_MASK))
4435 s->flags |= SF_ERR_PRXCOND;
4436 if (!(s->flags & SF_FINST_MASK))
4437 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004438
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004439 req->analysers = 0;
4440 req->analyse_exp = TICK_ETERNITY;
4441 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004442
4443 return_prx_yield:
4444 channel_dont_connect(req);
4445 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004446}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004447
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004448/* This function performs all the processing enabled for the current request.
4449 * It returns 1 if the processing can continue on next analysers, or zero if it
4450 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004451 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004452 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004453int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004454{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004455 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004456 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004457 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004458 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004459
Willy Tarreau655dce92009-11-08 13:10:58 +01004460 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004461 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004462 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004463 return 0;
4464 }
4465
Willy Tarreau87b09662015-04-03 00:22:06 +02004466 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 +02004467 now_ms, __FUNCTION__,
4468 s,
4469 req,
4470 req->rex, req->wex,
4471 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004472 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004473 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004474
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004475 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004476 select_compression_request_header(s, req->buf);
4477
Willy Tarreau59234e92008-11-30 23:51:27 +01004478 /*
4479 * Right now, we know that we have processed the entire headers
4480 * and that unwanted requests have been filtered out. We can do
4481 * whatever we want with the remaining request. Also, now we
4482 * may have separate values for ->fe, ->be.
4483 */
Willy Tarreau06619262006-12-17 08:37:22 +01004484
Willy Tarreau59234e92008-11-30 23:51:27 +01004485 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004486 * If HTTP PROXY is set we simply get remote server address parsing
4487 * incoming request. Note that this requires that a connection is
4488 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004489 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004490 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004491 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004492 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004493
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004494 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004495 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004496 txn->req.msg_state = HTTP_MSG_ERROR;
4497 txn->status = 500;
4498 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004499 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004500
Willy Tarreaue7dff022015-04-03 01:14:29 +02004501 if (!(s->flags & SF_ERR_MASK))
4502 s->flags |= SF_ERR_RESOURCE;
4503 if (!(s->flags & SF_FINST_MASK))
4504 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004505
4506 return 0;
4507 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004508
4509 path = http_get_path(txn);
4510 url2sa(req->buf->p + msg->sl.rq.u,
4511 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004512 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004513 /* if the path was found, we have to remove everything between
4514 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4515 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4516 * u_l characters by a single "/".
4517 */
4518 if (path) {
4519 char *cur_ptr = req->buf->p;
4520 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4521 int delta;
4522
4523 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4524 http_msg_move_end(&txn->req, delta);
4525 cur_end += delta;
4526 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4527 goto return_bad_req;
4528 }
4529 else {
4530 char *cur_ptr = req->buf->p;
4531 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4532 int delta;
4533
4534 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4535 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4536 http_msg_move_end(&txn->req, delta);
4537 cur_end += delta;
4538 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4539 goto return_bad_req;
4540 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004541 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004542
Willy Tarreau59234e92008-11-30 23:51:27 +01004543 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004544 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004545 * Note that doing so might move headers in the request, but
4546 * the fields will stay coherent and the URI will not move.
4547 * This should only be performed in the backend.
4548 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004549 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004550 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4551 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004552
William Lallemanda73203e2012-03-12 12:48:57 +01004553 /* add unique-id if "header-unique-id" is specified */
4554
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004555 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004556 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4557 goto return_bad_req;
4558 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004559 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004560 }
William Lallemanda73203e2012-03-12 12:48:57 +01004561
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004562 if (sess->fe->header_unique_id && s->unique_id) {
4563 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004564 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004565 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004566 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004567 goto return_bad_req;
4568 }
4569
Cyril Bontéb21570a2009-11-29 20:04:48 +01004570 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004571 * 9: add X-Forwarded-For if either the frontend or the backend
4572 * asks for it.
4573 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004574 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004575 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004576 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4577 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4578 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004579 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004580 /* The header is set to be added only if none is present
4581 * and we found it, so don't do anything.
4582 */
4583 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004584 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004585 /* Add an X-Forwarded-For header unless the source IP is
4586 * in the 'except' network range.
4587 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004588 if ((!sess->fe->except_mask.s_addr ||
4589 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4590 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004591 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004592 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004593 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004594 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004595 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004596 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004597
4598 /* Note: we rely on the backend to get the header name to be used for
4599 * x-forwarded-for, because the header is really meant for the backends.
4600 * However, if the backend did not specify any option, we have to rely
4601 * on the frontend's header name.
4602 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004603 if (s->be->fwdfor_hdr_len) {
4604 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004605 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004606 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004607 len = sess->fe->fwdfor_hdr_len;
4608 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004609 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004610 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 +01004611
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004612 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004613 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004614 }
4615 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004616 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004617 /* FIXME: for the sake of completeness, we should also support
4618 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004619 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004620 int len;
4621 char pn[INET6_ADDRSTRLEN];
4622 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004623 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004624 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004625
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 /* Note: we rely on the backend to get the header name to be used for
4627 * x-forwarded-for, because the header is really meant for the backends.
4628 * However, if the backend did not specify any option, we have to rely
4629 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004630 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004631 if (s->be->fwdfor_hdr_len) {
4632 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004633 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004634 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004635 len = sess->fe->fwdfor_hdr_len;
4636 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004637 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004638 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004639
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004640 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004641 goto return_bad_req;
4642 }
4643 }
4644
4645 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004646 * 10: add X-Original-To if either the frontend or the backend
4647 * asks for it.
4648 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004649 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004650
4651 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004652 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004653 /* Add an X-Original-To header unless the destination IP is
4654 * in the 'except' network range.
4655 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004656 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004657
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004658 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004659 ((!sess->fe->except_mask_to.s_addr ||
4660 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4661 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004662 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004663 (((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 +02004664 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004665 int len;
4666 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004667 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004668
4669 /* Note: we rely on the backend to get the header name to be used for
4670 * x-original-to, because the header is really meant for the backends.
4671 * However, if the backend did not specify any option, we have to rely
4672 * on the frontend's header name.
4673 */
4674 if (s->be->orgto_hdr_len) {
4675 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004676 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004677 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004678 len = sess->fe->orgto_hdr_len;
4679 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004680 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004681 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 +02004682
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004683 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004684 goto return_bad_req;
4685 }
4686 }
4687 }
4688
Willy Tarreau50fc7772012-11-11 22:19:57 +01004689 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4690 * If an "Upgrade" token is found, the header is left untouched in order not to have
4691 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4692 * "Upgrade" is present in the Connection header.
4693 */
4694 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4695 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004696 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004697 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004698 unsigned int want_flags = 0;
4699
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004700 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004701 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004702 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004703 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004704 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004705 want_flags |= TX_CON_CLO_SET;
4706 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004707 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004708 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004709 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004710 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004711 want_flags |= TX_CON_KAL_SET;
4712 }
4713
4714 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004715 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004716 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004717
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004718
Willy Tarreau522d6c02009-12-06 18:49:18 +01004719 /* If we have no server assigned yet and we're balancing on url_param
4720 * with a POST request, we may be interested in checking the body for
4721 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004722 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004723 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004724 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004725 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004726 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004727 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004728 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004729
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004730 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004731 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004732#ifdef TCP_QUICKACK
4733 /* We expect some data from the client. Unless we know for sure
4734 * we already have a full request, we have to re-enable quick-ack
4735 * in case we previously disabled it, otherwise we might cause
4736 * the client to delay further data.
4737 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004738 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004739 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004740 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004741 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004742 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004743#endif
4744 }
Willy Tarreau03945942009-12-22 16:50:27 +01004745
Willy Tarreau59234e92008-11-30 23:51:27 +01004746 /*************************************************************
4747 * OK, that's finished for the headers. We have done what we *
4748 * could. Let's switch to the DATA state. *
4749 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004750 req->analyse_exp = TICK_ETERNITY;
4751 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004752
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004753 /* if the server closes the connection, we want to immediately react
4754 * and close the socket to save packets and syscalls.
4755 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004756 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004757 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004758
Willy Tarreau59234e92008-11-30 23:51:27 +01004759 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004760 /* OK let's go on with the BODY now */
4761 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004762
Willy Tarreau59234e92008-11-30 23:51:27 +01004763 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004764 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004765 /* we detected a parsing error. We want to archive this request
4766 * in the dedicated proxy area for later troubleshooting.
4767 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004768 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004769 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004770
Willy Tarreau59234e92008-11-30 23:51:27 +01004771 txn->req.msg_state = HTTP_MSG_ERROR;
4772 txn->status = 400;
4773 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004774 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004775
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004776 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004777 if (sess->listener->counters)
4778 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004779
Willy Tarreaue7dff022015-04-03 01:14:29 +02004780 if (!(s->flags & SF_ERR_MASK))
4781 s->flags |= SF_ERR_PRXCOND;
4782 if (!(s->flags & SF_FINST_MASK))
4783 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004784 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004785}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004786
Willy Tarreau60b85b02008-11-30 23:28:40 +01004787/* This function is an analyser which processes the HTTP tarpit. It always
4788 * returns zero, at the beginning because it prevents any other processing
4789 * from occurring, and at the end because it terminates the request.
4790 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004791int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004792{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004793 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004794
4795 /* This connection is being tarpitted. The CLIENT side has
4796 * already set the connect expiration date to the right
4797 * timeout. We just have to check that the client is still
4798 * there and that the timeout has not expired.
4799 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004800 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004801 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004802 !tick_is_expired(req->analyse_exp, now_ms))
4803 return 0;
4804
4805 /* We will set the queue timer to the time spent, just for
4806 * logging purposes. We fake a 500 server error, so that the
4807 * attacker will not suspect his connection has been tarpitted.
4808 * It will not cause trouble to the logs because we can exclude
4809 * the tarpitted connections by filtering on the 'PT' status flags.
4810 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004811 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4812
4813 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004814 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004815 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004816
4817 req->analysers = 0;
4818 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004819
Willy Tarreaue7dff022015-04-03 01:14:29 +02004820 if (!(s->flags & SF_ERR_MASK))
4821 s->flags |= SF_ERR_PRXCOND;
4822 if (!(s->flags & SF_FINST_MASK))
4823 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004824 return 0;
4825}
4826
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004827/* This function is an analyser which waits for the HTTP request body. It waits
4828 * for either the buffer to be full, or the full advertised contents to have
4829 * reached the buffer. It must only be called after the standard HTTP request
4830 * processing has occurred, because it expects the request to be parsed and will
4831 * look for the Expect header. It may send a 100-Continue interim response. It
4832 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4833 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4834 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004835 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004836int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004837{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004838 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004839 struct http_txn *txn = s->txn;
4840 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004841
4842 /* We have to parse the HTTP request body to find any required data.
4843 * "balance url_param check_post" should have been the only way to get
4844 * into this. We were brought here after HTTP header analysis, so all
4845 * related structures are ready.
4846 */
4847
Willy Tarreau890988f2014-04-10 11:59:33 +02004848 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4849 /* This is the first call */
4850 if (msg->msg_state < HTTP_MSG_BODY)
4851 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004852
Willy Tarreau890988f2014-04-10 11:59:33 +02004853 if (msg->msg_state < HTTP_MSG_100_SENT) {
4854 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4855 * send an HTTP/1.1 100 Continue intermediate response.
4856 */
4857 if (msg->flags & HTTP_MSGF_VER_11) {
4858 struct hdr_ctx ctx;
4859 ctx.idx = 0;
4860 /* Expect is allowed in 1.1, look for it */
4861 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4862 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004863 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004864 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004865 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004866 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004867 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004868
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004869 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004870 * req->buf->p still points to the beginning of the message. We
4871 * must save the body in msg->next because it survives buffer
4872 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004873 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004874 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004875
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004876 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004877 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4878 else
4879 msg->msg_state = HTTP_MSG_DATA;
4880 }
4881
Willy Tarreau890988f2014-04-10 11:59:33 +02004882 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4883 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004884 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004885 goto missing_data;
4886
4887 /* OK we have everything we need now */
4888 goto http_end;
4889 }
4890
4891 /* OK here we're parsing a chunked-encoded message */
4892
Willy Tarreau522d6c02009-12-06 18:49:18 +01004893 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004894 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004895 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004896 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004897 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004898 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004899
Willy Tarreau115acb92009-12-26 13:56:06 +01004900 if (!ret)
4901 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004902 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004903 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004904 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004905 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004906 }
4907
Willy Tarreaud98cf932009-12-27 22:54:55 +01004908 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004909 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4910 * for at least a whole chunk or the whole content length bytes after
4911 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004912 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004913 if (msg->msg_state == HTTP_MSG_TRAILERS)
4914 goto http_end;
4915
Willy Tarreaue115b492015-05-01 23:05:14 +02004916 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004917 goto http_end;
4918
4919 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004920 /* we get here if we need to wait for more data. If the buffer is full,
4921 * we have the maximum we can expect.
4922 */
4923 if (buffer_full(req->buf, global.tune.maxrewrite))
4924 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004925
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004926 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004927 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004928 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004929
Willy Tarreaue7dff022015-04-03 01:14:29 +02004930 if (!(s->flags & SF_ERR_MASK))
4931 s->flags |= SF_ERR_CLITO;
4932 if (!(s->flags & SF_FINST_MASK))
4933 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004934 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004935 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004936
4937 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004938 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004939 /* Not enough data. We'll re-use the http-request
4940 * timeout here. Ideally, we should set the timeout
4941 * relative to the accept() date. We just set the
4942 * request timeout once at the beginning of the
4943 * request.
4944 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004945 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004946 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004947 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004948 return 0;
4949 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004950
4951 http_end:
4952 /* The situation will not evolve, so let's give up on the analysis. */
4953 s->logs.tv_request = now; /* update the request timer to reflect full request */
4954 req->analysers &= ~an_bit;
4955 req->analyse_exp = TICK_ETERNITY;
4956 return 1;
4957
4958 return_bad_req: /* let's centralize all bad requests */
4959 txn->req.msg_state = HTTP_MSG_ERROR;
4960 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004961 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004962
Willy Tarreaue7dff022015-04-03 01:14:29 +02004963 if (!(s->flags & SF_ERR_MASK))
4964 s->flags |= SF_ERR_PRXCOND;
4965 if (!(s->flags & SF_FINST_MASK))
4966 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004967
Willy Tarreau522d6c02009-12-06 18:49:18 +01004968 return_err_msg:
4969 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004970 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004971 if (sess->listener->counters)
4972 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004973 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004974}
4975
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004976/* send a server's name with an outgoing request over an established connection.
4977 * Note: this function is designed to be called once the request has been scheduled
4978 * for being forwarded. This is the reason why it rewinds the buffer before
4979 * proceeding.
4980 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004981int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004982
4983 struct hdr_ctx ctx;
4984
Mark Lamourinec2247f02012-01-04 13:02:01 -05004985 char *hdr_name = be->server_id_hdr_name;
4986 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004987 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004988 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004989 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004990
William Lallemandd9e90662012-01-30 17:27:17 +01004991 ctx.idx = 0;
4992
Willy Tarreau211cdec2014-04-17 20:18:08 +02004993 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004994 if (old_o) {
4995 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004996 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004997 txn->req.next += old_o;
4998 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004999 }
5000
Willy Tarreau9b28e032012-10-12 23:49:43 +02005001 old_i = chn->buf->i;
5002 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 -05005003 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005004 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005005 }
5006
5007 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005008 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005009 memcpy(hdr_val, hdr_name, hdr_name_len);
5010 hdr_val += hdr_name_len;
5011 *hdr_val++ = ':';
5012 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005013 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5014 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005015
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005016 if (old_o) {
5017 /* If this was a forwarded request, we must readjust the amount of
5018 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005019 * variations. Note that the current state is >= HTTP_MSG_BODY,
5020 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005021 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005022 old_o += chn->buf->i - old_i;
5023 b_adv(chn->buf, old_o);
5024 txn->req.next -= old_o;
5025 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005026 }
5027
Mark Lamourinec2247f02012-01-04 13:02:01 -05005028 return 0;
5029}
5030
Willy Tarreau610ecce2010-01-04 21:15:02 +01005031/* Terminate current transaction and prepare a new one. This is very tricky
5032 * right now but it works.
5033 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005034void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005035{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005036 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005037 struct proxy *fe = strm_fe(s);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005038 struct connection *srv_conn;
5039 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005040 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005041
Willy Tarreau610ecce2010-01-04 21:15:02 +01005042 /* FIXME: We need a more portable way of releasing a backend's and a
5043 * server's connections. We need a safer way to reinitialize buffer
5044 * flags. We also need a more accurate method for computing per-request
5045 * data.
5046 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005047 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005048
Willy Tarreau4213a112013-12-15 10:25:42 +01005049 /* unless we're doing keep-alive, we want to quickly close the connection
5050 * to the server.
5051 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005052 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005053 !si_conn_ready(&s->si[1])) {
5054 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5055 si_shutr(&s->si[1]);
5056 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005057 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005058
Willy Tarreaue7dff022015-04-03 01:14:29 +02005059 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005060 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005061 if (unlikely(s->srv_conn))
5062 sess_change_server(s, NULL);
5063 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005064
5065 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005066 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005067
Willy Tarreaueee5b512015-04-03 23:46:31 +02005068 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005069 int n;
5070
Willy Tarreaueee5b512015-04-03 23:46:31 +02005071 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005072 if (n < 1 || n > 5)
5073 n = 0;
5074
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005075 if (fe->mode == PR_MODE_HTTP) {
5076 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005077 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005078 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005079 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005080 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005081 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005082 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005083 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005084 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005085 s->be->be_counters.p.http.comp_rsp++;
5086 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005087 }
5088
5089 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005090 s->logs.bytes_in -= s->req.buf->i;
5091 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005092
5093 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005094 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005095 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005096 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097 s->do_log(s);
5098 }
5099
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005100 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005101 stream_stop_content_counters(s);
5102 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005103
Willy Tarreau610ecce2010-01-04 21:15:02 +01005104 s->logs.accept_date = date; /* user-visible date for logging */
5105 s->logs.tv_accept = now; /* corrected date for internal use */
5106 tv_zero(&s->logs.tv_request);
5107 s->logs.t_queue = -1;
5108 s->logs.t_connect = -1;
5109 s->logs.t_data = -1;
5110 s->logs.t_close = 0;
5111 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5112 s->logs.srv_queue_size = 0; /* we will get this number soon */
5113
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005114 s->logs.bytes_in = s->req.total = s->req.buf->i;
5115 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116
5117 if (s->pend_pos)
5118 pendconn_free(s->pend_pos);
5119
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005120 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005121 if (s->flags & SF_CURR_SESS) {
5122 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005123 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005124 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005125 if (may_dequeue_tasks(objt_server(s->target), s->be))
5126 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005127 }
5128
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005129 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005130
Willy Tarreau4213a112013-12-15 10:25:42 +01005131 /* only release our endpoint if we don't intend to reuse the
5132 * connection.
5133 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005134 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005135 !si_conn_ready(&s->si[1])) {
5136 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005137 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005138 }
5139
Willy Tarreau350f4872014-11-28 14:42:25 +01005140 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5141 s->si[1].err_type = SI_ET_NONE;
5142 s->si[1].conn_retries = 0; /* used for logging too */
5143 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005144 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 +01005145 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);
5146 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 +02005147 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5148 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5149 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005150
Willy Tarreaueee5b512015-04-03 23:46:31 +02005151 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005152 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005153 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005154
5155 if (prev_status == 401 || prev_status == 407) {
5156 /* In HTTP keep-alive mode, if we receive a 401, we still have
5157 * a chance of being able to send the visitor again to the same
5158 * server over the same connection. This is required by some
5159 * broken protocols such as NTLM, and anyway whenever there is
5160 * an opportunity for sending the challenge to the proper place,
5161 * it's better to do it (at least it helps with debugging).
5162 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005163 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau387ebf82015-08-04 19:24:13 +02005164 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005165 }
5166
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005167 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005168 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005169
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005170 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005171 s->req.flags |= CF_NEVER_WAIT;
5172 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005173 }
5174
Willy Tarreau610ecce2010-01-04 21:15:02 +01005175 /* if the request buffer is not empty, it means we're
5176 * about to process another request, so send pending
5177 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005178 * Just don't do this if the buffer is close to be full,
5179 * because the request will wait for it to flush a little
5180 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005181 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005182 if (s->req.buf->i) {
5183 if (s->res.buf->o &&
5184 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5185 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5186 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005187 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005188
5189 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005190 channel_auto_read(&s->req);
5191 channel_auto_close(&s->req);
5192 channel_auto_read(&s->res);
5193 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194
Willy Tarreau27375622013-12-17 00:00:28 +01005195 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005196 if (srv_conn) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005197 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005198 if (!srv)
5199 si_idle_conn(&s->si[1], NULL);
5200 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5201 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5202 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005203 else if (prev_flags & TX_NOT_FIRST)
5204 /* note: we check the request, not the connection, but
5205 * this is valid for strategies SAFE and AGGR, and in
5206 * case of ALWS, we don't care anyway.
5207 */
5208 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005209 else
5210 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005211 }
Willy Tarreau27375622013-12-17 00:00:28 +01005212
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005213 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005214 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005215}
5216
5217
5218/* This function updates the request state machine according to the response
5219 * state machine and buffer flags. It returns 1 if it changes anything (flag
5220 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5221 * it is only used to find when a request/response couple is complete. Both
5222 * this function and its equivalent should loop until both return zero. It
5223 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5224 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005225int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005226{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005227 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005228 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005229 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005230 unsigned int old_state = txn->req.msg_state;
5231
Willy Tarreau610ecce2010-01-04 21:15:02 +01005232 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5233 return 0;
5234
5235 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005236 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005237 * We can shut the read side unless we want to abort_on_close,
5238 * or we have a POST request. The issue with POST requests is
5239 * that some browsers still send a CRLF after the request, and
5240 * this CRLF must be read so that it does not remain in the kernel
5241 * buffers, otherwise a close could cause an RST on some systems
5242 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005243 * Note that if we're using keep-alive on the client side, we'd
5244 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005245 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005246 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005247 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005248 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5249 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5250 !(s->be->options & PR_O_ABRT_CLOSE) &&
5251 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005252 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005253
Willy Tarreau40f151a2012-12-20 12:10:09 +01005254 /* if the server closes the connection, we want to immediately react
5255 * and close the socket to save packets and syscalls.
5256 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005257 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005258
Willy Tarreau610ecce2010-01-04 21:15:02 +01005259 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5260 goto wait_other_side;
5261
5262 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5263 /* The server has not finished to respond, so we
5264 * don't want to move in order not to upset it.
5265 */
5266 goto wait_other_side;
5267 }
5268
5269 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5270 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005271 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005273 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005274 goto wait_other_side;
5275 }
5276
5277 /* When we get here, it means that both the request and the
5278 * response have finished receiving. Depending on the connection
5279 * mode, we'll have to wait for the last bytes to leave in either
5280 * direction, and sometimes for a close to be effective.
5281 */
5282
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005283 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5284 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005285 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5286 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005287 }
5288 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5289 /* Option forceclose is set, or either side wants to close,
5290 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005291 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005292 * once both states are CLOSED.
5293 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005294 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5295 channel_shutr_now(chn);
5296 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005297 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005298 }
5299 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005300 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5301 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005302 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005303 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5304 channel_auto_read(chn);
5305 txn->req.msg_state = HTTP_MSG_TUNNEL;
5306 chn->flags |= CF_NEVER_WAIT;
5307 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005308 }
5309
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005310 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005311 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005312 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005313
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005314 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 txn->req.msg_state = HTTP_MSG_CLOSING;
5316 goto http_msg_closing;
5317 }
5318 else {
5319 txn->req.msg_state = HTTP_MSG_CLOSED;
5320 goto http_msg_closed;
5321 }
5322 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005323 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005324 }
5325
5326 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5327 http_msg_closing:
5328 /* nothing else to forward, just waiting for the output buffer
5329 * to be empty and for the shutw_now to take effect.
5330 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005331 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332 txn->req.msg_state = HTTP_MSG_CLOSED;
5333 goto http_msg_closed;
5334 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005335 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 txn->req.msg_state = HTTP_MSG_ERROR;
5337 goto wait_other_side;
5338 }
5339 }
5340
5341 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5342 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005343 /* see above in MSG_DONE why we only do this in these states */
5344 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5345 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5346 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005347 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005348 goto wait_other_side;
5349 }
5350
5351 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005352 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005353}
5354
5355
5356/* This function updates the response state machine according to the request
5357 * state machine and buffer flags. It returns 1 if it changes anything (flag
5358 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5359 * it is only used to find when a request/response couple is complete. Both
5360 * this function and its equivalent should loop until both return zero. It
5361 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5362 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005363int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005364{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005365 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005366 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005367 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005368 unsigned int old_state = txn->rsp.msg_state;
5369
Willy Tarreau610ecce2010-01-04 21:15:02 +01005370 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5371 return 0;
5372
5373 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5374 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005375 * still monitor the server connection for a possible close
5376 * while the request is being uploaded, so we don't disable
5377 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005378 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005379 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380
5381 if (txn->req.msg_state == HTTP_MSG_ERROR)
5382 goto wait_other_side;
5383
5384 if (txn->req.msg_state < HTTP_MSG_DONE) {
5385 /* The client seems to still be sending data, probably
5386 * because we got an error response during an upload.
5387 * We have the choice of either breaking the connection
5388 * or letting it pass through. Let's do the later.
5389 */
5390 goto wait_other_side;
5391 }
5392
5393 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5394 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005395 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005396 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005397 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005398 goto wait_other_side;
5399 }
5400
5401 /* When we get here, it means that both the request and the
5402 * response have finished receiving. Depending on the connection
5403 * mode, we'll have to wait for the last bytes to leave in either
5404 * direction, and sometimes for a close to be effective.
5405 */
5406
5407 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5408 /* Server-close mode : shut read and wait for the request
5409 * side to close its output buffer. The caller will detect
5410 * when we're in DONE and the other is in CLOSED and will
5411 * catch that for the final cleanup.
5412 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005413 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5414 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005415 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005416 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5417 /* Option forceclose is set, or either side wants to close,
5418 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005419 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005420 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005421 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005422 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5423 channel_shutr_now(chn);
5424 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005425 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426 }
5427 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005428 /* The last possible modes are keep-alive and tunnel. Tunnel will
5429 * need to forward remaining data. Keep-alive will need to monitor
5430 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005431 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005432 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005433 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005434 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5435 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005436 }
5437
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005438 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005439 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005440 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5442 goto http_msg_closing;
5443 }
5444 else {
5445 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5446 goto http_msg_closed;
5447 }
5448 }
5449 goto wait_other_side;
5450 }
5451
5452 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5453 http_msg_closing:
5454 /* nothing else to forward, just waiting for the output buffer
5455 * to be empty and for the shutw_now to take effect.
5456 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005457 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005458 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5459 goto http_msg_closed;
5460 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005461 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005462 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005463 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005464 if (objt_server(s->target))
5465 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005466 goto wait_other_side;
5467 }
5468 }
5469
5470 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5471 http_msg_closed:
5472 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005473 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005474 channel_auto_close(chn);
5475 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005476 goto wait_other_side;
5477 }
5478
5479 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005480 /* We force the response to leave immediately if we're waiting for the
5481 * other side, since there is no pending shutdown to push it out.
5482 */
5483 if (!channel_is_empty(chn))
5484 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005485 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005486}
5487
5488
5489/* Resync the request and response state machines. Return 1 if either state
5490 * changes.
5491 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005492int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005493{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005494 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005495 int old_req_state = txn->req.msg_state;
5496 int old_res_state = txn->rsp.msg_state;
5497
Willy Tarreau610ecce2010-01-04 21:15:02 +01005498 http_sync_req_state(s);
5499 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005500 if (!http_sync_res_state(s))
5501 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005502 if (!http_sync_req_state(s))
5503 break;
5504 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005505
Willy Tarreau610ecce2010-01-04 21:15:02 +01005506 /* OK, both state machines agree on a compatible state.
5507 * There are a few cases we're interested in :
5508 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5509 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5510 * directions, so let's simply disable both analysers.
5511 * - HTTP_MSG_CLOSED on the response only means we must abort the
5512 * request.
5513 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5514 * with server-close mode means we've completed one request and we
5515 * must re-initialize the server connection.
5516 */
5517
5518 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5519 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5520 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5521 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005522 s->req.analysers = 0;
5523 channel_auto_close(&s->req);
5524 channel_auto_read(&s->req);
5525 s->res.analysers = 0;
5526 channel_auto_close(&s->res);
5527 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005528 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005529 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005530 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005531 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005532 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005533 s->res.analysers = 0;
5534 channel_auto_close(&s->res);
5535 channel_auto_read(&s->res);
5536 s->req.analysers = 0;
5537 channel_abort(&s->req);
5538 channel_auto_close(&s->req);
5539 channel_auto_read(&s->req);
5540 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005541 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005542 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5543 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005544 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005545 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5546 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5547 /* server-close/keep-alive: terminate this transaction,
5548 * possibly killing the server connection and reinitialize
5549 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005550 */
5551 http_end_txn_clean_session(s);
5552 }
5553
Willy Tarreau610ecce2010-01-04 21:15:02 +01005554 return txn->req.msg_state != old_req_state ||
5555 txn->rsp.msg_state != old_res_state;
5556}
5557
Willy Tarreaud98cf932009-12-27 22:54:55 +01005558/* This function is an analyser which forwards request body (including chunk
5559 * sizes if any). It is called as soon as we must forward, even if we forward
5560 * zero byte. The only situation where it must not be called is when we're in
5561 * tunnel mode and we want to forward till the close. It's used both to forward
5562 * remaining data and to resync after end of body. It expects the msg_state to
5563 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005564 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005565 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005566 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005567 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005568int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005569{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005570 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005571 struct http_txn *txn = s->txn;
5572 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005573
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005574 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5575 return 0;
5576
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005577 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005578 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005579 /* Output closed while we were sending data. We must abort and
5580 * wake the other side up.
5581 */
5582 msg->msg_state = HTTP_MSG_ERROR;
5583 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005584 return 1;
5585 }
5586
Willy Tarreaud98cf932009-12-27 22:54:55 +01005587 /* Note that we don't have to send 100-continue back because we don't
5588 * need the data to complete our job, and it's up to the server to
5589 * decide whether to return 100, 417 or anything else in return of
5590 * an "Expect: 100-continue" header.
5591 */
5592
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005593 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005594 /* we have msg->sov which points to the first byte of message
5595 * body, and req->buf.p still points to the beginning of the
5596 * message. We forward the headers now, as we don't need them
5597 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005598 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005599 b_adv(req->buf, msg->sov);
5600 msg->next -= msg->sov;
5601 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005602
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005603 /* The previous analysers guarantee that the state is somewhere
5604 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5605 * msg->next are always correct.
5606 */
5607 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5608 if (msg->flags & HTTP_MSGF_TE_CHNK)
5609 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5610 else
5611 msg->msg_state = HTTP_MSG_DATA;
5612 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005613 }
5614
Willy Tarreau7ba23542014-04-17 21:50:00 +02005615 /* Some post-connect processing might want us to refrain from starting to
5616 * forward data. Currently, the only reason for this is "balance url_param"
5617 * whichs need to parse/process the request after we've enabled forwarding.
5618 */
5619 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005620 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005621 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005622 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005623 goto missing_data;
5624 }
5625 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5626 }
5627
Willy Tarreau80a92c02014-03-12 10:41:13 +01005628 /* in most states, we should abort in case of early close */
5629 channel_auto_close(req);
5630
Willy Tarreauefdf0942014-04-24 20:08:57 +02005631 if (req->to_forward) {
5632 /* We can't process the buffer's contents yet */
5633 req->flags |= CF_WAKE_WRITE;
5634 goto missing_data;
5635 }
5636
Willy Tarreaud98cf932009-12-27 22:54:55 +01005637 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005638 if (msg->msg_state == HTTP_MSG_DATA) {
5639 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005640 /* we may have some pending data starting at req->buf->p */
5641 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005642 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005643 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005644 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005645 msg->next += msg->chunk_len;
5646 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005647
5648 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005649 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005650 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005651 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005652 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005653 }
5654 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005655 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005656 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005657 * TRAILERS state.
5658 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005659 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005660
Willy Tarreau54d23df2012-10-25 19:04:45 +02005661 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005662 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005663 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005664 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005665 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005666 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005667 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005668 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005669 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005670 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005671 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005672 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005673 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005674
5675 if (ret == 0)
5676 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005677 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005678 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005679 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005680 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005681 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005682 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005683 /* we're in MSG_CHUNK_SIZE now */
5684 }
5685 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005686 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687
5688 if (ret == 0)
5689 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005690 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005691 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005692 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005693 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005694 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005695 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005696 /* we're in HTTP_MSG_DONE now */
5697 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005698 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005699 int old_state = msg->msg_state;
5700
Willy Tarreau610ecce2010-01-04 21:15:02 +01005701 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005702
5703 /* we may have some pending data starting at req->buf->p
5704 * such as last chunk of data or trailers.
5705 */
5706 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005707 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005708 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005709 msg->next = 0;
5710
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005711 /* we don't want to forward closes on DONE except in
5712 * tunnel mode.
5713 */
5714 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005715 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005716 if (http_resync_states(s)) {
5717 /* some state changes occurred, maybe the analyser
5718 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005719 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005720 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005721 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005722 /* request errors are most likely due to
5723 * the server aborting the transfer.
5724 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005725 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005726 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005727 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005728 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005729 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005730 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005731 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005732 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005733
5734 /* If "option abortonclose" is set on the backend, we
5735 * want to monitor the client's connection and forward
5736 * any shutdown notification to the server, which will
5737 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005738 * request. We only do that in tunnel mode, and not in
5739 * other modes since it can be abused to exhaust source
5740 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005741 */
5742 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005743 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005744 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5745 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5746 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005747 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005748 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005749 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005750 /* POST requests may require to read extra CRLF
5751 * sent by broken browsers and which could cause
5752 * an RST to be sent upon close on some systems
5753 * (eg: Linux).
5754 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005755 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005756 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005757
Willy Tarreau610ecce2010-01-04 21:15:02 +01005758 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005759 }
5760 }
5761
Willy Tarreaud98cf932009-12-27 22:54:55 +01005762 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005763 /* we may have some pending data starting at req->buf->p */
5764 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005765 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005766 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5767
Willy Tarreaubed410e2014-04-22 08:19:34 +02005768 msg->next = 0;
5769 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5770
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005771 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005772 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005773 if (!(s->flags & SF_ERR_MASK))
5774 s->flags |= SF_ERR_CLICL;
5775 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005776 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005777 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005778 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005779 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005780 }
5781
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005782 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005783 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005784 if (objt_server(s->target))
5785 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005786
5787 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005788 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005789
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005790 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005791 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005792 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005793
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005794 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005795 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005796 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005797 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005798 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005799
Willy Tarreau5c620922011-05-11 19:56:11 +02005800 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005801 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005802 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005803 * modes are already handled by the stream sock layer. We must not do
5804 * this in content-length mode because it could present the MSG_MORE
5805 * flag with the last block of forwarded data, which would cause an
5806 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005807 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005808 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005809 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005810
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005811 return 0;
5812
Willy Tarreaud98cf932009-12-27 22:54:55 +01005813 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005814 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005815 if (sess->listener->counters)
5816 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005817
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005818 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005819 /* we may have some pending data starting at req->buf->p */
5820 b_adv(req->buf, msg->next);
5821 msg->next = 0;
5822
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005823 txn->req.msg_state = HTTP_MSG_ERROR;
5824 if (txn->status) {
5825 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005826 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005827 } else {
5828 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005829 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005830 }
5831 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005832 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005833
Willy Tarreaue7dff022015-04-03 01:14:29 +02005834 if (!(s->flags & SF_ERR_MASK))
5835 s->flags |= SF_ERR_PRXCOND;
5836 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005837 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005838 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005839 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005840 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005841 }
5842 return 0;
5843
5844 aborted_xfer:
5845 txn->req.msg_state = HTTP_MSG_ERROR;
5846 if (txn->status) {
5847 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005848 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005849 } else {
5850 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005851 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005852 }
5853 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005854 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005855
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005856 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005857 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005858 if (objt_server(s->target))
5859 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005860
Willy Tarreaue7dff022015-04-03 01:14:29 +02005861 if (!(s->flags & SF_ERR_MASK))
5862 s->flags |= SF_ERR_SRVCL;
5863 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005864 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005865 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005866 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005867 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005868 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005869 return 0;
5870}
5871
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005872/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5873 * processing can continue on next analysers, or zero if it either needs more
5874 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005875 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005876 * when it has nothing left to do, and may remove any analyser when it wants to
5877 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005878 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005879int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005880{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005881 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005882 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005883 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005884 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005885 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005886 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005887 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005888
Willy Tarreau87b09662015-04-03 00:22:06 +02005889 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 +02005890 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005891 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005892 rep,
5893 rep->rex, rep->wex,
5894 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005895 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005896 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005897
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005898 /*
5899 * Now parse the partial (or complete) lines.
5900 * We will check the response syntax, and also join multi-line
5901 * headers. An index of all the lines will be elaborated while
5902 * parsing.
5903 *
5904 * For the parsing, we use a 28 states FSM.
5905 *
5906 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005907 * rep->buf->p = beginning of response
5908 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5909 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005910 * msg->eol = end of current header or line (LF or CRLF)
5911 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005912 */
5913
Willy Tarreau628c40c2014-04-24 19:11:26 +02005914 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005915 /* There's a protected area at the end of the buffer for rewriting
5916 * purposes. We don't want to start to parse the request if the
5917 * protected area is affected, because we may have to move processed
5918 * data later, which is much more complicated.
5919 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005920 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005921 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005922 /* some data has still not left the buffer, wake us once that's done */
5923 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5924 goto abort_response;
5925 channel_dont_close(rep);
5926 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005927 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005928 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005929 }
5930
Willy Tarreau379357a2013-06-08 12:55:46 +02005931 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5932 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5933 buffer_slow_realign(rep->buf);
5934
Willy Tarreau9b28e032012-10-12 23:49:43 +02005935 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005936 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005937 }
5938
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005939 /* 1: we might have to print this header in debug mode */
5940 if (unlikely((global.mode & MODE_DEBUG) &&
5941 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005942 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005943 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005944
Willy Tarreau9b28e032012-10-12 23:49:43 +02005945 sol = rep->buf->p;
5946 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005947 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005948
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949 sol += hdr_idx_first_pos(&txn->hdr_idx);
5950 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005951
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005952 while (cur_idx) {
5953 eol = sol + txn->hdr_idx.v[cur_idx].len;
5954 debug_hdr("srvhdr", s, sol, eol);
5955 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5956 cur_idx = txn->hdr_idx.v[cur_idx].next;
5957 }
5958 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005959
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005960 /*
5961 * Now we quickly check if we have found a full valid response.
5962 * If not so, we check the FD and buffer states before leaving.
5963 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005964 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005965 * responses are checked first.
5966 *
5967 * Depending on whether the client is still there or not, we
5968 * may send an error response back or not. Note that normally
5969 * we should only check for HTTP status there, and check I/O
5970 * errors somewhere else.
5971 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005972
Willy Tarreau655dce92009-11-08 13:10:58 +01005973 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005974 /* Invalid response */
5975 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5976 /* we detected a parsing error. We want to archive this response
5977 * in the dedicated proxy area for later troubleshooting.
5978 */
5979 hdr_response_bad:
5980 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005981 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005983 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005984 if (objt_server(s->target)) {
5985 objt_server(s->target)->counters.failed_resp++;
5986 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005987 }
Willy Tarreau64648412010-03-05 10:41:54 +01005988 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005989 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005990 rep->analysers = 0;
5991 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005992 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005993 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005994 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995
Willy Tarreaue7dff022015-04-03 01:14:29 +02005996 if (!(s->flags & SF_ERR_MASK))
5997 s->flags |= SF_ERR_PRXCOND;
5998 if (!(s->flags & SF_FINST_MASK))
5999 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006000
6001 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006002 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006003
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006004 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006005 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006006 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006007 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006008 goto hdr_response_bad;
6009 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006010
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006011 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006012 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006013 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006014 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006015 else if (txn->flags & TX_NOT_FIRST)
6016 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006017
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006018 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006019 if (objt_server(s->target)) {
6020 objt_server(s->target)->counters.failed_resp++;
6021 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006022 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006023
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006024 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025 rep->analysers = 0;
6026 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006027 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006028 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006029 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006030
Willy Tarreaue7dff022015-04-03 01:14:29 +02006031 if (!(s->flags & SF_ERR_MASK))
6032 s->flags |= SF_ERR_SRVCL;
6033 if (!(s->flags & SF_FINST_MASK))
6034 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006035 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006036 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006037
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006038 /* read timeout : return a 504 to the client. */
6039 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006040 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006041 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006042 else if (txn->flags & TX_NOT_FIRST)
6043 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006044
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006045 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006046 if (objt_server(s->target)) {
6047 objt_server(s->target)->counters.failed_resp++;
6048 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006049 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006050
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006051 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006052 rep->analysers = 0;
6053 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006054 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006055 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006056 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006057
Willy Tarreaue7dff022015-04-03 01:14:29 +02006058 if (!(s->flags & SF_ERR_MASK))
6059 s->flags |= SF_ERR_SRVTO;
6060 if (!(s->flags & SF_FINST_MASK))
6061 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006062 return 0;
6063 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006064
Willy Tarreauf003d372012-11-26 13:35:37 +01006065 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006066 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006067 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006068 s->be->be_counters.cli_aborts++;
6069 if (objt_server(s->target))
6070 objt_server(s->target)->counters.cli_aborts++;
6071
6072 rep->analysers = 0;
6073 channel_auto_close(rep);
6074
6075 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006076 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006077 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006078
Willy Tarreaue7dff022015-04-03 01:14:29 +02006079 if (!(s->flags & SF_ERR_MASK))
6080 s->flags |= SF_ERR_CLICL;
6081 if (!(s->flags & SF_FINST_MASK))
6082 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006083
Willy Tarreau87b09662015-04-03 00:22:06 +02006084 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006085 return 0;
6086 }
6087
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006088 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006089 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006090 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006091 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006092 else if (txn->flags & TX_NOT_FIRST)
6093 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006094
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006095 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006096 if (objt_server(s->target)) {
6097 objt_server(s->target)->counters.failed_resp++;
6098 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006099 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006100
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006101 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006102 rep->analysers = 0;
6103 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006104 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006105 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006106 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006107
Willy Tarreaue7dff022015-04-03 01:14:29 +02006108 if (!(s->flags & SF_ERR_MASK))
6109 s->flags |= SF_ERR_SRVCL;
6110 if (!(s->flags & SF_FINST_MASK))
6111 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006112 return 0;
6113 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006114
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006115 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006116 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006117 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006118 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006119 else if (txn->flags & TX_NOT_FIRST)
6120 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006121
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006122 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006123 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006124 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006125
Willy Tarreaue7dff022015-04-03 01:14:29 +02006126 if (!(s->flags & SF_ERR_MASK))
6127 s->flags |= SF_ERR_CLICL;
6128 if (!(s->flags & SF_FINST_MASK))
6129 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006130
Willy Tarreau87b09662015-04-03 00:22:06 +02006131 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006132 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006133 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006134
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006135 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006136 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006137 return 0;
6138 }
6139
6140 /* More interesting part now : we know that we have a complete
6141 * response which at least looks like HTTP. We have an indicator
6142 * of each header's length, so we can parse them quickly.
6143 */
6144
6145 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006146 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006147
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006148 /*
6149 * 1: get the status code
6150 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006151 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006152 if (n < 1 || n > 5)
6153 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006154 /* when the client triggers a 4xx from the server, it's most often due
6155 * to a missing object or permission. These events should be tracked
6156 * because if they happen often, it may indicate a brute force or a
6157 * vulnerability scan.
6158 */
6159 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006160 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006161
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006162 if (objt_server(s->target))
6163 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006164
Willy Tarreau91852eb2015-05-01 13:26:00 +02006165 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6166 * exactly one digit "." one digit. This check may be disabled using
6167 * option accept-invalid-http-response.
6168 */
6169 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6170 if (msg->sl.st.v_l != 8) {
6171 msg->err_pos = 0;
6172 goto hdr_response_bad;
6173 }
6174
6175 if (rep->buf->p[4] != '/' ||
6176 !isdigit((unsigned char)rep->buf->p[5]) ||
6177 rep->buf->p[6] != '.' ||
6178 !isdigit((unsigned char)rep->buf->p[7])) {
6179 msg->err_pos = 4;
6180 goto hdr_response_bad;
6181 }
6182 }
6183
Willy Tarreau5b154472009-12-21 20:11:07 +01006184 /* check if the response is HTTP/1.1 or above */
6185 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006186 ((rep->buf->p[5] > '1') ||
6187 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006188 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006189
6190 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006191 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 +01006192
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006193 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006194 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006195
Willy Tarreau9b28e032012-10-12 23:49:43 +02006196 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006197
Willy Tarreau39650402010-03-15 19:44:39 +01006198 /* Adjust server's health based on status code. Note: status codes 501
6199 * and 505 are triggered on demand by client request, so we must not
6200 * count them as server failures.
6201 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006202 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006203 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006204 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006205 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006206 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006207 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006208
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006209 /*
6210 * 2: check for cacheability.
6211 */
6212
6213 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006214 case 100:
6215 /*
6216 * We may be facing a 100-continue response, in which case this
6217 * is not the right response, and we're waiting for the next one.
6218 * Let's allow this response to go to the client and wait for the
6219 * next one.
6220 */
6221 hdr_idx_init(&txn->hdr_idx);
6222 msg->next -= channel_forward(rep, msg->next);
6223 msg->msg_state = HTTP_MSG_RPBEFORE;
6224 txn->status = 0;
6225 s->logs.t_data = -1; /* was not a response yet */
6226 goto next_one;
6227
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006228 case 200:
6229 case 203:
6230 case 206:
6231 case 300:
6232 case 301:
6233 case 410:
6234 /* RFC2616 @13.4:
6235 * "A response received with a status code of
6236 * 200, 203, 206, 300, 301 or 410 MAY be stored
6237 * by a cache (...) unless a cache-control
6238 * directive prohibits caching."
6239 *
6240 * RFC2616 @9.5: POST method :
6241 * "Responses to this method are not cacheable,
6242 * unless the response includes appropriate
6243 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006244 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006245 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006246 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006247 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6248 break;
6249 default:
6250 break;
6251 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006252
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006253 /*
6254 * 3: we may need to capture headers
6255 */
6256 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006257 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006258 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006259 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006260
Willy Tarreau557f1992015-05-01 10:05:17 +02006261 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6262 * by RFC7230#3.3.3 :
6263 *
6264 * The length of a message body is determined by one of the following
6265 * (in order of precedence):
6266 *
6267 * 1. Any response to a HEAD request and any response with a 1xx
6268 * (Informational), 204 (No Content), or 304 (Not Modified) status
6269 * code is always terminated by the first empty line after the
6270 * header fields, regardless of the header fields present in the
6271 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006272 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006273 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6274 * the connection will become a tunnel immediately after the empty
6275 * line that concludes the header fields. A client MUST ignore any
6276 * Content-Length or Transfer-Encoding header fields received in
6277 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006278 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006279 * 3. If a Transfer-Encoding header field is present and the chunked
6280 * transfer coding (Section 4.1) is the final encoding, the message
6281 * body length is determined by reading and decoding the chunked
6282 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006283 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006284 * If a Transfer-Encoding header field is present in a response and
6285 * the chunked transfer coding is not the final encoding, the
6286 * message body length is determined by reading the connection until
6287 * it is closed by the server. If a Transfer-Encoding header field
6288 * is present in a request and the chunked transfer coding is not
6289 * the final encoding, the message body length cannot be determined
6290 * reliably; the server MUST respond with the 400 (Bad Request)
6291 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006292 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006293 * If a message is received with both a Transfer-Encoding and a
6294 * Content-Length header field, the Transfer-Encoding overrides the
6295 * Content-Length. Such a message might indicate an attempt to
6296 * perform request smuggling (Section 9.5) or response splitting
6297 * (Section 9.4) and ought to be handled as an error. A sender MUST
6298 * remove the received Content-Length field prior to forwarding such
6299 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006300 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006301 * 4. If a message is received without Transfer-Encoding and with
6302 * either multiple Content-Length header fields having differing
6303 * field-values or a single Content-Length header field having an
6304 * invalid value, then the message framing is invalid and the
6305 * recipient MUST treat it as an unrecoverable error. If this is a
6306 * request message, the server MUST respond with a 400 (Bad Request)
6307 * status code and then close the connection. If this is a response
6308 * message received by a proxy, the proxy MUST close the connection
6309 * to the server, discard the received response, and send a 502 (Bad
6310 * Gateway) response to the client. If this is a response message
6311 * received by a user agent, the user agent MUST close the
6312 * connection to the server and discard the received response.
6313 *
6314 * 5. If a valid Content-Length header field is present without
6315 * Transfer-Encoding, its decimal value defines the expected message
6316 * body length in octets. If the sender closes the connection or
6317 * the recipient times out before the indicated number of octets are
6318 * received, the recipient MUST consider the message to be
6319 * incomplete and close the connection.
6320 *
6321 * 6. If this is a request message and none of the above are true, then
6322 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006323 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006324 * 7. Otherwise, this is a response message without a declared message
6325 * body length, so the message body length is determined by the
6326 * number of octets received prior to the server closing the
6327 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006328 */
6329
6330 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006331 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006332 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006333 * FIXME: should we parse anyway and return an error on chunked encoding ?
6334 */
6335 if (txn->meth == HTTP_METH_HEAD ||
6336 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006337 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006338 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006339 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006340 goto skip_content_length;
6341 }
6342
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006343 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006344 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006345 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006346 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006347 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6348 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006349 /* bad transfer-encoding (chunked followed by something else) */
6350 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006351 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006352 break;
6353 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006354 }
6355
Willy Tarreau1c913912015-04-30 10:57:51 +02006356 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006357 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006358 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006359 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6360 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6361 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006362 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006363 signed long long cl;
6364
Willy Tarreauad14f752011-09-02 20:33:27 +02006365 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006366 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006367 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006368 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006369
Willy Tarreauad14f752011-09-02 20:33:27 +02006370 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006371 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006372 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006373 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006374
Willy Tarreauad14f752011-09-02 20:33:27 +02006375 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006376 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006377 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006378 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006379
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006380 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006381 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006382 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006383 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006384
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006385 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006386 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006387 }
6388
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006389 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006390 select_compression_response_header(s, rep->buf);
6391
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006392skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006393 /* Now we have to check if we need to modify the Connection header.
6394 * This is more difficult on the response than it is on the request,
6395 * because we can have two different HTTP versions and we don't know
6396 * how the client will interprete a response. For instance, let's say
6397 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6398 * HTTP/1.1 response without any header. Maybe it will bound itself to
6399 * HTTP/1.0 because it only knows about it, and will consider the lack
6400 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6401 * the lack of header as a keep-alive. Thus we will use two flags
6402 * indicating how a request MAY be understood by the client. In case
6403 * of multiple possibilities, we'll fix the header to be explicit. If
6404 * ambiguous cases such as both close and keepalive are seen, then we
6405 * will fall back to explicit close. Note that we won't take risks with
6406 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006407 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006408 */
6409
Willy Tarreaudc008c52010-02-01 16:20:08 +01006410 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6411 txn->status == 101)) {
6412 /* Either we've established an explicit tunnel, or we're
6413 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006414 * to understand the next protocols. We have to switch to tunnel
6415 * mode, so that we transfer the request and responses then let
6416 * this protocol pass unmodified. When we later implement specific
6417 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006418 * header which contains information about that protocol for
6419 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006420 */
6421 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6422 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006423 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6424 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006425 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006426 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006427 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006428
Willy Tarreau70dffda2014-01-30 03:07:23 +01006429 /* this situation happens when combining pretend-keepalive with httpclose. */
6430 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006431 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006432 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006433 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6434
Willy Tarreau60466522010-01-18 19:08:45 +01006435 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006436 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006437 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6438 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006439
Willy Tarreau60466522010-01-18 19:08:45 +01006440 /* now adjust header transformations depending on current state */
6441 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6442 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6443 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006444 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006445 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006446 }
Willy Tarreau60466522010-01-18 19:08:45 +01006447 else { /* SCL / KAL */
6448 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006449 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006450 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006451 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006452
Willy Tarreau60466522010-01-18 19:08:45 +01006453 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006454 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006455
Willy Tarreau60466522010-01-18 19:08:45 +01006456 /* Some keep-alive responses are converted to Server-close if
6457 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006458 */
Willy Tarreau60466522010-01-18 19:08:45 +01006459 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6460 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006461 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006462 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006463 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006464 }
6465
Willy Tarreau7959a552013-09-23 16:44:27 +02006466 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006467 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006468
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006469 /* end of job, return OK */
6470 rep->analysers &= ~an_bit;
6471 rep->analyse_exp = TICK_ETERNITY;
6472 channel_auto_close(rep);
6473 return 1;
6474
6475 abort_keep_alive:
6476 /* A keep-alive request to the server failed on a network error.
6477 * The client is required to retry. We need to close without returning
6478 * any other information so that the client retries.
6479 */
6480 txn->status = 0;
6481 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006482 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006483 channel_auto_close(rep);
6484 s->logs.logwait = 0;
6485 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006486 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006487 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006488 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006489 return 0;
6490}
6491
6492/* This function performs all the processing enabled for the current response.
6493 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006494 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006495 * other functions. It works like process_request (see indications above).
6496 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006497int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006498{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006499 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006500 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006501 struct http_msg *msg = &txn->rsp;
6502 struct proxy *cur_proxy;
6503 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006504 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006505
Willy Tarreau87b09662015-04-03 00:22:06 +02006506 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 +02006507 now_ms, __FUNCTION__,
6508 s,
6509 rep,
6510 rep->rex, rep->wex,
6511 rep->flags,
6512 rep->buf->i,
6513 rep->analysers);
6514
6515 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6516 return 0;
6517
Willy Tarreau70730dd2014-04-24 18:06:27 +02006518 /* The stats applet needs to adjust the Connection header but we don't
6519 * apply any filter there.
6520 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006521 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6522 rep->analysers &= ~an_bit;
6523 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006524 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006525 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006526
Willy Tarreau58975672014-04-24 21:13:57 +02006527 /*
6528 * We will have to evaluate the filters.
6529 * As opposed to version 1.2, now they will be evaluated in the
6530 * filters order and not in the header order. This means that
6531 * each filter has to be validated among all headers.
6532 *
6533 * Filters are tried with ->be first, then with ->fe if it is
6534 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006535 *
6536 * Maybe we are in resume condiion. In this case I choose the
6537 * "struct proxy" which contains the rule list matching the resume
6538 * pointer. If none of theses "struct proxy" match, I initialise
6539 * the process with the first one.
6540 *
6541 * In fact, I check only correspondance betwwen the current list
6542 * pointer and the ->fe rule list. If it doesn't match, I initialize
6543 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006544 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006545 if (s->current_rule_list == &sess->fe->http_res_rules)
6546 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006547 else
6548 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006549 while (1) {
6550 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006551
Willy Tarreau58975672014-04-24 21:13:57 +02006552 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006553 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006554 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006555
Willy Tarreau51d861a2015-05-22 17:30:48 +02006556 if (ret == HTTP_RULE_RES_BADREQ)
6557 goto return_srv_prx_502;
6558
6559 if (ret == HTTP_RULE_RES_DONE) {
6560 rep->analysers &= ~an_bit;
6561 rep->analyse_exp = TICK_ETERNITY;
6562 return 1;
6563 }
6564 }
6565
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006566 /* we need to be called again. */
6567 if (ret == HTTP_RULE_RES_YIELD) {
6568 channel_dont_close(rep);
6569 return 0;
6570 }
6571
Willy Tarreau58975672014-04-24 21:13:57 +02006572 /* try headers filters */
6573 if (rule_set->rsp_exp != NULL) {
6574 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6575 return_bad_resp:
6576 if (objt_server(s->target)) {
6577 objt_server(s->target)->counters.failed_resp++;
6578 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006579 }
Willy Tarreau58975672014-04-24 21:13:57 +02006580 s->be->be_counters.failed_resp++;
6581 return_srv_prx_502:
6582 rep->analysers = 0;
6583 txn->status = 502;
6584 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006585 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006586 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006587 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006588 if (!(s->flags & SF_ERR_MASK))
6589 s->flags |= SF_ERR_PRXCOND;
6590 if (!(s->flags & SF_FINST_MASK))
6591 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006592 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006593 }
Willy Tarreau58975672014-04-24 21:13:57 +02006594 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006595
Willy Tarreau58975672014-04-24 21:13:57 +02006596 /* has the response been denied ? */
6597 if (txn->flags & TX_SVDENY) {
6598 if (objt_server(s->target))
6599 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006600
Willy Tarreau58975672014-04-24 21:13:57 +02006601 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006602 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006603 if (sess->listener->counters)
6604 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006605
Willy Tarreau58975672014-04-24 21:13:57 +02006606 goto return_srv_prx_502;
6607 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006608
Willy Tarreau58975672014-04-24 21:13:57 +02006609 /* add response headers from the rule sets in the same order */
6610 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006611 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006612 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006613 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006614 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006615 ret = acl_pass(ret);
6616 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6617 ret = !ret;
6618 if (!ret)
6619 continue;
6620 }
6621 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6622 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006623 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006624
Willy Tarreau58975672014-04-24 21:13:57 +02006625 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006626 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006627 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006628 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006629 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006630
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006631 /* After this point, this anayzer can't return yield, so we can
6632 * remove the bit corresponding to this analyzer from the list.
6633 *
6634 * Note that the intermediate returns and goto found previously
6635 * reset the analyzers.
6636 */
6637 rep->analysers &= ~an_bit;
6638 rep->analyse_exp = TICK_ETERNITY;
6639
Willy Tarreau58975672014-04-24 21:13:57 +02006640 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006641 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006642 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006643
Willy Tarreau58975672014-04-24 21:13:57 +02006644 /*
6645 * Now check for a server cookie.
6646 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006647 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006648 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006649
Willy Tarreau58975672014-04-24 21:13:57 +02006650 /*
6651 * Check for cache-control or pragma headers if required.
6652 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006653 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 +02006654 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006655
Willy Tarreau58975672014-04-24 21:13:57 +02006656 /*
6657 * Add server cookie in the response if needed
6658 */
6659 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6660 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006661 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006662 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6663 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6664 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6665 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6666 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006667 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006668 /* the server is known, it's not the one the client requested, or the
6669 * cookie's last seen date needs to be refreshed. We have to
6670 * insert a set-cookie here, except if we want to insert only on POST
6671 * requests and this one isn't. Note that servers which don't have cookies
6672 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006673 */
Willy Tarreau58975672014-04-24 21:13:57 +02006674 if (!objt_server(s->target)->cookie) {
6675 chunk_printf(&trash,
6676 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6677 s->be->cookie_name);
6678 }
6679 else {
6680 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006681
Willy Tarreau58975672014-04-24 21:13:57 +02006682 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6683 /* emit last_date, which is mandatory */
6684 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6685 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6686 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006687
Willy Tarreau58975672014-04-24 21:13:57 +02006688 if (s->be->cookie_maxlife) {
6689 /* emit first_date, which is either the original one or
6690 * the current date.
6691 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006692 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006693 s30tob64(txn->cookie_first_date ?
6694 txn->cookie_first_date >> 2 :
6695 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006696 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006697 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006698 }
Willy Tarreau58975672014-04-24 21:13:57 +02006699 chunk_appendf(&trash, "; path=/");
6700 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006701
Willy Tarreau58975672014-04-24 21:13:57 +02006702 if (s->be->cookie_domain)
6703 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006704
Willy Tarreau58975672014-04-24 21:13:57 +02006705 if (s->be->ck_opts & PR_CK_HTTPONLY)
6706 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006707
Willy Tarreau58975672014-04-24 21:13:57 +02006708 if (s->be->ck_opts & PR_CK_SECURE)
6709 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006710
Willy Tarreau58975672014-04-24 21:13:57 +02006711 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6712 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006713
Willy Tarreau58975672014-04-24 21:13:57 +02006714 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006715 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006716 /* the server did not change, only the date was updated */
6717 txn->flags |= TX_SCK_UPDATED;
6718 else
6719 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006720
Willy Tarreau58975672014-04-24 21:13:57 +02006721 /* Here, we will tell an eventual cache on the client side that we don't
6722 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6723 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6724 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006725 */
Willy Tarreau58975672014-04-24 21:13:57 +02006726 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006727
Willy Tarreau58975672014-04-24 21:13:57 +02006728 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006729
Willy Tarreau58975672014-04-24 21:13:57 +02006730 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6731 "Cache-control: private", 22) < 0))
6732 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006733 }
Willy Tarreau58975672014-04-24 21:13:57 +02006734 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006735
Willy Tarreau58975672014-04-24 21:13:57 +02006736 /*
6737 * Check if result will be cacheable with a cookie.
6738 * We'll block the response if security checks have caught
6739 * nasty things such as a cacheable cookie.
6740 */
6741 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6742 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6743 (s->be->options & PR_O_CHK_CACHE)) {
6744 /* we're in presence of a cacheable response containing
6745 * a set-cookie header. We'll block it as requested by
6746 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006747 */
Willy Tarreau58975672014-04-24 21:13:57 +02006748 if (objt_server(s->target))
6749 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006750
Willy Tarreau58975672014-04-24 21:13:57 +02006751 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006752 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006753 if (sess->listener->counters)
6754 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006755
Willy Tarreau58975672014-04-24 21:13:57 +02006756 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6757 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6758 send_log(s->be, LOG_ALERT,
6759 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6760 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6761 goto return_srv_prx_502;
6762 }
Willy Tarreau03945942009-12-22 16:50:27 +01006763
Willy Tarreau70730dd2014-04-24 18:06:27 +02006764 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006765 /*
6766 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6767 * If an "Upgrade" token is found, the header is left untouched in order
6768 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006769 * if anything but "Upgrade" is present in the Connection header. We don't
6770 * want to touch any 101 response either since it's switching to another
6771 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006772 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006773 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006774 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006775 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006776 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6777 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006778
Willy Tarreau58975672014-04-24 21:13:57 +02006779 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6780 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6781 /* we want a keep-alive response here. Keep-alive header
6782 * required if either side is not 1.1.
6783 */
6784 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6785 want_flags |= TX_CON_KAL_SET;
6786 }
6787 else {
6788 /* we want a close response here. Close header required if
6789 * the server is 1.1, regardless of the client.
6790 */
6791 if (msg->flags & HTTP_MSGF_VER_11)
6792 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006793 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794
Willy Tarreau58975672014-04-24 21:13:57 +02006795 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6796 http_change_connection_header(txn, msg, want_flags);
6797 }
6798
6799 skip_header_mangling:
6800 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6801 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6802 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803
Willy Tarreau58975672014-04-24 21:13:57 +02006804 /* if the user wants to log as soon as possible, without counting
6805 * bytes from the server, then this is the right moment. We have
6806 * to temporarily assign bytes_out to log what we currently have.
6807 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006808 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006809 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6810 s->logs.bytes_out = txn->rsp.eoh;
6811 s->do_log(s);
6812 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006813 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006814 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006815}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006816
Willy Tarreaud98cf932009-12-27 22:54:55 +01006817/* This function is an analyser which forwards response body (including chunk
6818 * sizes if any). It is called as soon as we must forward, even if we forward
6819 * zero byte. The only situation where it must not be called is when we're in
6820 * tunnel mode and we want to forward till the close. It's used both to forward
6821 * remaining data and to resync after end of body. It expects the msg_state to
6822 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006823 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006824 *
6825 * It is capable of compressing response data both in content-length mode and
6826 * in chunked mode. The state machines follows different flows depending on
6827 * whether content-length and chunked modes are used, since there are no
6828 * trailers in content-length :
6829 *
6830 * chk-mode cl-mode
6831 * ,----- BODY -----.
6832 * / \
6833 * V size > 0 V chk-mode
6834 * .--> SIZE -------------> DATA -------------> CRLF
6835 * | | size == 0 | last byte |
6836 * | v final crlf v inspected |
6837 * | TRAILERS -----------> DONE |
6838 * | |
6839 * `----------------------------------------------'
6840 *
6841 * Compression only happens in the DATA state, and must be flushed in final
6842 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6843 * is performed at once on final states for all bytes parsed, or when leaving
6844 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006845 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006846int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006847{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006848 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006849 struct http_txn *txn = s->txn;
6850 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006851 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006852 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006853 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006854
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006855 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6856 return 0;
6857
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006858 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006859 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006860 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006861 /* Output closed while we were sending data. We must abort and
6862 * wake the other side up.
6863 */
6864 msg->msg_state = HTTP_MSG_ERROR;
6865 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006866 return 1;
6867 }
6868
Willy Tarreau4fe41902010-06-07 22:27:41 +02006869 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006870 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006871
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006872 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006873 /* we have msg->sov which points to the first byte of message
6874 * body, and res->buf.p still points to the beginning of the
6875 * message. We forward the headers now, as we don't need them
6876 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006877 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006878 b_adv(res->buf, msg->sov);
6879 msg->next -= msg->sov;
6880 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006881
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006882 /* The previous analysers guarantee that the state is somewhere
6883 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6884 * msg->next are always correct.
6885 */
6886 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6887 if (msg->flags & HTTP_MSGF_TE_CHNK)
6888 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6889 else
6890 msg->msg_state = HTTP_MSG_DATA;
6891 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006892 }
6893
Willy Tarreauefdf0942014-04-24 20:08:57 +02006894 if (res->to_forward) {
6895 /* We can't process the buffer's contents yet */
6896 res->flags |= CF_WAKE_WRITE;
6897 goto missing_data;
6898 }
6899
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006900 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6901 /* We need a compression buffer in the DATA state to put the
6902 * output of compressed data, and in CRLF state to let the
6903 * TRAILERS state finish the job of removing the trailing CRLF.
6904 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006905 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006906 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006907 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006908 goto aborted_xfer; /* no memory */
6909 }
6910
6911 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006912 if (ret < 0) {
6913 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006914 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006915 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006916 compressing = 1;
6917 }
6918
Willy Tarreaud98cf932009-12-27 22:54:55 +01006919 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006920 switch (msg->msg_state - HTTP_MSG_DATA) {
6921 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006922 /* we may have some pending data starting at res->buf->p */
6923 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006924 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006925 if (ret < 0)
6926 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006927
Willy Tarreaud5a67832014-04-21 10:54:27 +02006928 if (msg->chunk_len) {
6929 /* input empty or output full */
6930 if (res->buf->i > msg->next)
6931 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006932 goto missing_data;
6933 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006934 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006935 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006936 if (msg->chunk_len > res->buf->i - msg->next) {
6937 /* output full */
6938 res->flags |= CF_WAKE_WRITE;
6939 goto missing_data;
6940 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006941 msg->next += msg->chunk_len;
6942 msg->chunk_len = 0;
6943 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006944
6945 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006946 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006947 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006948 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006949 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006950 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006951 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006952 /* fall through for HTTP_MSG_CHUNK_CRLF */
6953
6954 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6955 /* we want the CRLF after the data */
6956
6957 ret = http_skip_chunk_crlf(msg);
6958 if (ret == 0)
6959 goto missing_data;
6960 else if (ret < 0) {
6961 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006962 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006963 goto return_bad_res;
6964 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006965 /* we're in MSG_CHUNK_SIZE now, fall through */
6966
6967 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006968 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006969 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006970 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006971 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006972
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006973 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006974 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006975 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006976 else if (ret < 0) {
6977 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006978 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006979 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006980 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006981 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006982 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006983
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006984 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006985 if (unlikely(compressing)) {
6986 /* we need to flush output contents before syncing FSMs */
6987 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6988 compressing = 0;
6989 }
6990
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006991 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006992 if (ret == 0)
6993 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006994 else if (ret < 0) {
6995 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006996 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006997 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006998 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006999 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007000
7001 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007002 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007003 if (unlikely(compressing)) {
7004 /* we need to flush output contents before syncing FSMs */
7005 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7006 compressing = 0;
7007 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007008
Willy Tarreauc623c172014-04-18 09:53:50 +02007009 /* we may have some pending data starting at res->buf->p
7010 * such as a last chunk of data or trailers.
7011 */
7012 b_adv(res->buf, msg->next);
7013 msg->next = 0;
7014
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007015 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007016 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007017 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7018 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007019 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007020
Willy Tarreau610ecce2010-01-04 21:15:02 +01007021 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007022 /* some state changes occurred, maybe the analyser
7023 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007024 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007025 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007026 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007027 /* response errors are most likely due to
7028 * the client aborting the transfer.
7029 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007030 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007031 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007032 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007033 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007034 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007035 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007036 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007037 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007038 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007039 }
7040 }
7041
Willy Tarreaud98cf932009-12-27 22:54:55 +01007042 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007043 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007044 if (unlikely(compressing)) {
7045 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007046 compressing = 0;
7047 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007048
Willy Tarreauc623c172014-04-18 09:53:50 +02007049 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7050 b_adv(res->buf, msg->next);
7051 msg->next = 0;
7052 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7053 }
7054
Willy Tarreauf003d372012-11-26 13:35:37 +01007055 if (res->flags & CF_SHUTW)
7056 goto aborted_xfer;
7057
7058 /* stop waiting for data if the input is closed before the end. If the
7059 * client side was already closed, it means that the client has aborted,
7060 * so we don't want to count this as a server abort. Otherwise it's a
7061 * server abort.
7062 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007063 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007064 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007065 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007066 if (!(s->flags & SF_ERR_MASK))
7067 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007068 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007069 if (objt_server(s->target))
7070 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007071 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007072 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007073
Willy Tarreau40dba092010-03-04 18:14:51 +01007074 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007075 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007076 goto return_bad_res;
7077
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007078 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007079 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007080 * Similarly, with keep-alive on the client side, we don't want to forward a
7081 * close.
7082 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007083 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007084 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7085 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007086 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007087
Willy Tarreau5c620922011-05-11 19:56:11 +02007088 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007089 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007090 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007091 * modes are already handled by the stream sock layer. We must not do
7092 * this in content-length mode because it could present the MSG_MORE
7093 * flag with the last block of forwarded data, which would cause an
7094 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007095 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007096 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007097 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007098
Willy Tarreau87b09662015-04-03 00:22:06 +02007099 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007100 return 0;
7101
Willy Tarreau40dba092010-03-04 18:14:51 +01007102 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007103 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007104 if (objt_server(s->target))
7105 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007106
7107 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007108 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007109 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007110 compressing = 0;
7111 }
7112
Willy Tarreauc623c172014-04-18 09:53:50 +02007113 /* we may have some pending data starting at res->buf->p */
7114 if (s->comp_algo == NULL) {
7115 b_adv(res->buf, msg->next);
7116 msg->next = 0;
7117 }
7118
Willy Tarreaud98cf932009-12-27 22:54:55 +01007119 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007120 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007121 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007122 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007123 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007124 if (objt_server(s->target))
7125 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007126
Willy Tarreaue7dff022015-04-03 01:14:29 +02007127 if (!(s->flags & SF_ERR_MASK))
7128 s->flags |= SF_ERR_PRXCOND;
7129 if (!(s->flags & SF_FINST_MASK))
7130 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007131 return 0;
7132
7133 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007134 if (unlikely(compressing)) {
7135 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7136 compressing = 0;
7137 }
7138
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007139 txn->rsp.msg_state = HTTP_MSG_ERROR;
7140 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007141 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007142 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007143 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007144
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007145 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007146 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007147 if (objt_server(s->target))
7148 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007149
Willy Tarreaue7dff022015-04-03 01:14:29 +02007150 if (!(s->flags & SF_ERR_MASK))
7151 s->flags |= SF_ERR_CLICL;
7152 if (!(s->flags & SF_FINST_MASK))
7153 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007154 return 0;
7155}
7156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007157/* Iterate the same filter through all request headers.
7158 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159 * Since it can manage the switch to another backend, it updates the per-proxy
7160 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007161 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007162int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007164 char *cur_ptr, *cur_end, *cur_next;
7165 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007166 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007167 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007168 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007170 last_hdr = 0;
7171
Willy Tarreau9b28e032012-10-12 23:49:43 +02007172 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 old_idx = 0;
7174
7175 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007176 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007177 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007178 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007179 (exp->action == ACT_ALLOW ||
7180 exp->action == ACT_DENY ||
7181 exp->action == ACT_TARPIT))
7182 return 0;
7183
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007184 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007185 if (!cur_idx)
7186 break;
7187
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007188 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007189 cur_ptr = cur_next;
7190 cur_end = cur_ptr + cur_hdr->len;
7191 cur_next = cur_end + cur_hdr->cr + 1;
7192
7193 /* Now we have one header between cur_ptr and cur_end,
7194 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007195 */
7196
Willy Tarreau15a53a42015-01-21 13:39:42 +01007197 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007198 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007199 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007200 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007201 last_hdr = 1;
7202 break;
7203
7204 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007205 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007206 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007207 break;
7208
7209 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007210 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007211 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007212 break;
7213
7214 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007215 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7216 if (trash.len < 0)
7217 return -1;
7218
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007219 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007220 /* FIXME: if the user adds a newline in the replacement, the
7221 * index will not be recalculated for now, and the new line
7222 * will not be counted as a new header.
7223 */
7224
7225 cur_end += delta;
7226 cur_next += delta;
7227 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007228 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007229 break;
7230
7231 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007232 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007233 cur_next += delta;
7234
Willy Tarreaufa355d42009-11-29 18:12:29 +01007235 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007236 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7237 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007238 cur_hdr->len = 0;
7239 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007240 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007241 break;
7242
7243 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007244 }
7245
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246 /* keep the link from this header to next one in case of later
7247 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007248 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007249 old_idx = cur_idx;
7250 }
7251 return 0;
7252}
7253
7254
7255/* Apply the filter to the request line.
7256 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7257 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007258 * Since it can manage the switch to another backend, it updates the per-proxy
7259 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007260 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007261int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007263 char *cur_ptr, *cur_end;
7264 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007265 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007266 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007267
Willy Tarreau3d300592007-03-18 18:34:41 +01007268 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007269 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007270 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 (exp->action == ACT_ALLOW ||
7272 exp->action == ACT_DENY ||
7273 exp->action == ACT_TARPIT))
7274 return 0;
7275 else if (exp->action == ACT_REMOVE)
7276 return 0;
7277
7278 done = 0;
7279
Willy Tarreau9b28e032012-10-12 23:49:43 +02007280 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007281 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007282
7283 /* Now we have the request line between cur_ptr and cur_end */
7284
Willy Tarreau15a53a42015-01-21 13:39:42 +01007285 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007286 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007287 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007288 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 done = 1;
7290 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007292 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007293 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007294 done = 1;
7295 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007298 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007299 done = 1;
7300 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007303 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7304 if (trash.len < 0)
7305 return -1;
7306
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007307 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007308 /* FIXME: if the user adds a newline in the replacement, the
7309 * index will not be recalculated for now, and the new line
7310 * will not be counted as a new header.
7311 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007312
Willy Tarreaufa355d42009-11-29 18:12:29 +01007313 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007315 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007316 HTTP_MSG_RQMETH,
7317 cur_ptr, cur_end + 1,
7318 NULL, NULL);
7319 if (unlikely(!cur_end))
7320 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007321
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007322 /* we have a full request and we know that we have either a CR
7323 * or an LF at <ptr>.
7324 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007325 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7326 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007327 /* there is no point trying this regex on headers */
7328 return 1;
7329 }
7330 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007331 return done;
7332}
Willy Tarreau97de6242006-12-27 17:18:38 +01007333
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334
Willy Tarreau58f10d72006-12-04 02:26:12 +01007335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007337 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007338 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339 * unparsable request. Since it can manage the switch to another backend, it
7340 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007341 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007342int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007343{
Willy Tarreau192252e2015-04-04 01:47:55 +02007344 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007345 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007346 struct hdr_exp *exp;
7347
7348 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007349 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007350
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007351 /*
7352 * The interleaving of transformations and verdicts
7353 * makes it difficult to decide to continue or stop
7354 * the evaluation.
7355 */
7356
Willy Tarreau6c123b12010-01-28 20:22:06 +01007357 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7358 break;
7359
Willy Tarreau3d300592007-03-18 18:34:41 +01007360 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007362 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007363 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007364
7365 /* if this filter had a condition, evaluate it now and skip to
7366 * next filter if the condition does not match.
7367 */
7368 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007369 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007370 ret = acl_pass(ret);
7371 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7372 ret = !ret;
7373
7374 if (!ret)
7375 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007376 }
7377
7378 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007379 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007380 if (unlikely(ret < 0))
7381 return -1;
7382
7383 if (likely(ret == 0)) {
7384 /* The filter did not match the request, it can be
7385 * iterated through all headers.
7386 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007387 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7388 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007389 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007390 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007392}
7393
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007394
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007395/* Find the end of a cookie value contained between <s> and <e>. It works the
7396 * same way as with headers above except that the semi-colon also ends a token.
7397 * See RFC2965 for more information. Note that it requires a valid header to
7398 * return a valid result.
7399 */
7400char *find_cookie_value_end(char *s, const char *e)
7401{
7402 int quoted, qdpair;
7403
7404 quoted = qdpair = 0;
7405 for (; s < e; s++) {
7406 if (qdpair) qdpair = 0;
7407 else if (quoted) {
7408 if (*s == '\\') qdpair = 1;
7409 else if (*s == '"') quoted = 0;
7410 }
7411 else if (*s == '"') quoted = 1;
7412 else if (*s == ',' || *s == ';') return s;
7413 }
7414 return s;
7415}
7416
7417/* Delete a value in a header between delimiters <from> and <next> in buffer
7418 * <buf>. The number of characters displaced is returned, and the pointer to
7419 * the first delimiter is updated if required. The function tries as much as
7420 * possible to respect the following principles :
7421 * - replace <from> delimiter by the <next> one unless <from> points to a
7422 * colon, in which case <next> is simply removed
7423 * - set exactly one space character after the new first delimiter, unless
7424 * there are not enough characters in the block being moved to do so.
7425 * - remove unneeded spaces before the previous delimiter and after the new
7426 * one.
7427 *
7428 * It is the caller's responsibility to ensure that :
7429 * - <from> points to a valid delimiter or the colon ;
7430 * - <next> points to a valid delimiter or the final CR/LF ;
7431 * - there are non-space chars before <from> ;
7432 * - there is a CR/LF at or after <next>.
7433 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007434int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007435{
7436 char *prev = *from;
7437
7438 if (*prev == ':') {
7439 /* We're removing the first value, preserve the colon and add a
7440 * space if possible.
7441 */
7442 if (!http_is_crlf[(unsigned char)*next])
7443 next++;
7444 prev++;
7445 if (prev < next)
7446 *prev++ = ' ';
7447
7448 while (http_is_spht[(unsigned char)*next])
7449 next++;
7450 } else {
7451 /* Remove useless spaces before the old delimiter. */
7452 while (http_is_spht[(unsigned char)*(prev-1)])
7453 prev--;
7454 *from = prev;
7455
7456 /* copy the delimiter and if possible a space if we're
7457 * not at the end of the line.
7458 */
7459 if (!http_is_crlf[(unsigned char)*next]) {
7460 *prev++ = *next++;
7461 if (prev + 1 < next)
7462 *prev++ = ' ';
7463 while (http_is_spht[(unsigned char)*next])
7464 next++;
7465 }
7466 }
7467 return buffer_replace2(buf, prev, next, NULL, 0);
7468}
7469
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007470/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007471 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472 * desirable to call it only when needed. This code is quite complex because
7473 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7474 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007475 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007476void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007477{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007478 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007479 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007480 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007481 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007482 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7483 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007486 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007487 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007488
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007489 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007491 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007492
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007493 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007494 hdr_beg = hdr_next;
7495 hdr_end = hdr_beg + cur_hdr->len;
7496 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007498 /* We have one full header between hdr_beg and hdr_end, and the
7499 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007500 * "Cookie:" headers.
7501 */
7502
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007503 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007504 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007505 old_idx = cur_idx;
7506 continue;
7507 }
7508
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007509 del_from = NULL; /* nothing to be deleted */
7510 preserve_hdr = 0; /* assume we may kill the whole header */
7511
Willy Tarreau58f10d72006-12-04 02:26:12 +01007512 /* Now look for cookies. Conforming to RFC2109, we have to support
7513 * attributes whose name begin with a '$', and associate them with
7514 * the right cookie, if we want to delete this cookie.
7515 * So there are 3 cases for each cookie read :
7516 * 1) it's a special attribute, beginning with a '$' : ignore it.
7517 * 2) it's a server id cookie that we *MAY* want to delete : save
7518 * some pointers on it (last semi-colon, beginning of cookie...)
7519 * 3) it's an application cookie : we *MAY* have to delete a previous
7520 * "special" cookie.
7521 * At the end of loop, if a "special" cookie remains, we may have to
7522 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007523 * *MUST* delete it.
7524 *
7525 * Note: RFC2965 is unclear about the processing of spaces around
7526 * the equal sign in the ATTR=VALUE form. A careful inspection of
7527 * the RFC explicitly allows spaces before it, and not within the
7528 * tokens (attrs or values). An inspection of RFC2109 allows that
7529 * too but section 10.1.3 lets one think that spaces may be allowed
7530 * after the equal sign too, resulting in some (rare) buggy
7531 * implementations trying to do that. So let's do what servers do.
7532 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7533 * allowed quoted strings in values, with any possible character
7534 * after a backslash, including control chars and delimitors, which
7535 * causes parsing to become ambiguous. Browsers also allow spaces
7536 * within values even without quotes.
7537 *
7538 * We have to keep multiple pointers in order to support cookie
7539 * removal at the beginning, middle or end of header without
7540 * corrupting the header. All of these headers are valid :
7541 *
7542 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7543 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7544 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7545 * | | | | | | | | |
7546 * | | | | | | | | hdr_end <--+
7547 * | | | | | | | +--> next
7548 * | | | | | | +----> val_end
7549 * | | | | | +-----------> val_beg
7550 * | | | | +--------------> equal
7551 * | | | +----------------> att_end
7552 * | | +---------------------> att_beg
7553 * | +--------------------------> prev
7554 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007555 */
7556
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007557 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7558 /* Iterate through all cookies on this line */
7559
7560 /* find att_beg */
7561 att_beg = prev + 1;
7562 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7563 att_beg++;
7564
7565 /* find att_end : this is the first character after the last non
7566 * space before the equal. It may be equal to hdr_end.
7567 */
7568 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007569
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 while (equal < hdr_end) {
7571 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007572 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007573 if (http_is_spht[(unsigned char)*equal++])
7574 continue;
7575 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576 }
7577
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007578 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7579 * is between <att_beg> and <equal>, both may be identical.
7580 */
7581
7582 /* look for end of cookie if there is an equal sign */
7583 if (equal < hdr_end && *equal == '=') {
7584 /* look for the beginning of the value */
7585 val_beg = equal + 1;
7586 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7587 val_beg++;
7588
7589 /* find the end of the value, respecting quotes */
7590 next = find_cookie_value_end(val_beg, hdr_end);
7591
7592 /* make val_end point to the first white space or delimitor after the value */
7593 val_end = next;
7594 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7595 val_end--;
7596 } else {
7597 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007598 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007599
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007600 /* We have nothing to do with attributes beginning with '$'. However,
7601 * they will automatically be removed if a header before them is removed,
7602 * since they're supposed to be linked together.
7603 */
7604 if (*att_beg == '$')
7605 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007606
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 /* Ignore cookies with no equal sign */
7608 if (equal == next) {
7609 /* This is not our cookie, so we must preserve it. But if we already
7610 * scheduled another cookie for removal, we cannot remove the
7611 * complete header, but we can remove the previous block itself.
7612 */
7613 preserve_hdr = 1;
7614 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007615 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007616 val_end += delta;
7617 next += delta;
7618 hdr_end += delta;
7619 hdr_next += delta;
7620 cur_hdr->len += delta;
7621 http_msg_move_end(&txn->req, delta);
7622 prev = del_from;
7623 del_from = NULL;
7624 }
7625 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007626 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007627
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007628 /* if there are spaces around the equal sign, we need to
7629 * strip them otherwise we'll get trouble for cookie captures,
7630 * or even for rewrites. Since this happens extremely rarely,
7631 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007632 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007633 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7634 int stripped_before = 0;
7635 int stripped_after = 0;
7636
7637 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007638 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 equal += stripped_before;
7640 val_beg += stripped_before;
7641 }
7642
7643 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007644 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007645 val_beg += stripped_after;
7646 stripped_before += stripped_after;
7647 }
7648
7649 val_end += stripped_before;
7650 next += stripped_before;
7651 hdr_end += stripped_before;
7652 hdr_next += stripped_before;
7653 cur_hdr->len += stripped_before;
7654 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007655 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007656 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007657
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007658 /* First, let's see if we want to capture this cookie. We check
7659 * that we don't already have a client side cookie, because we
7660 * can only capture one. Also as an optimisation, we ignore
7661 * cookies shorter than the declared name.
7662 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007663 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7664 (val_end - att_beg >= sess->fe->capture_namelen) &&
7665 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007666 int log_len = val_end - att_beg;
7667
7668 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7669 Alert("HTTP logging : out of memory.\n");
7670 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007671 if (log_len > sess->fe->capture_len)
7672 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007673 memcpy(txn->cli_cookie, att_beg, log_len);
7674 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007675 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007677
Willy Tarreaubca99692010-10-06 19:25:55 +02007678 /* Persistence cookies in passive, rewrite or insert mode have the
7679 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007681 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007682 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007683 * For cookies in prefix mode, the form is :
7684 *
7685 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007687 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7688 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7689 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7693 * have the server ID between val_beg and delim, and the original cookie between
7694 * delim+1 and val_end. Otherwise, delim==val_end :
7695 *
7696 * Cookie: NAME=SRV; # in all but prefix modes
7697 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7698 * | || || | |+-> next
7699 * | || || | +--> val_end
7700 * | || || +---------> delim
7701 * | || |+------------> val_beg
7702 * | || +-------------> att_end = equal
7703 * | |+-----------------> att_beg
7704 * | +------------------> prev
7705 * +-------------------------> hdr_beg
7706 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007707
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007708 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007709 for (delim = val_beg; delim < val_end; delim++)
7710 if (*delim == COOKIE_DELIM)
7711 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007712 } else {
7713 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007714 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007715 /* Now check if the cookie contains a date field, which would
7716 * appear after a vertical bar ('|') just after the server name
7717 * and before the delimiter.
7718 */
7719 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7720 if (vbar1) {
7721 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007722 * right is the last seen date. It is a base64 encoded
7723 * 30-bit value representing the UNIX date since the
7724 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007725 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007726 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007727 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007728 if (val_end - vbar1 >= 5) {
7729 val = b64tos30(vbar1);
7730 if (val > 0)
7731 txn->cookie_last_date = val << 2;
7732 }
7733 /* look for a second vertical bar */
7734 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7735 if (vbar1 && (val_end - vbar1 > 5)) {
7736 val = b64tos30(vbar1 + 1);
7737 if (val > 0)
7738 txn->cookie_first_date = val << 2;
7739 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007740 }
7741 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007742
Willy Tarreauf64d1412010-10-07 20:06:11 +02007743 /* if the cookie has an expiration date and the proxy wants to check
7744 * it, then we do that now. We first check if the cookie is too old,
7745 * then only if it has expired. We detect strict overflow because the
7746 * time resolution here is not great (4 seconds). Cookies with dates
7747 * in the future are ignored if their offset is beyond one day. This
7748 * allows an admin to fix timezone issues without expiring everyone
7749 * and at the same time avoids keeping unwanted side effects for too
7750 * long.
7751 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007752 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7753 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007754 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007755 txn->flags &= ~TX_CK_MASK;
7756 txn->flags |= TX_CK_OLD;
7757 delim = val_beg; // let's pretend we have not found the cookie
7758 txn->cookie_first_date = 0;
7759 txn->cookie_last_date = 0;
7760 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007761 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7762 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007763 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007764 txn->flags &= ~TX_CK_MASK;
7765 txn->flags |= TX_CK_EXPIRED;
7766 delim = val_beg; // let's pretend we have not found the cookie
7767 txn->cookie_first_date = 0;
7768 txn->cookie_last_date = 0;
7769 }
7770
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007771 /* Here, we'll look for the first running server which supports the cookie.
7772 * This allows to share a same cookie between several servers, for example
7773 * to dedicate backup servers to specific servers only.
7774 * However, to prevent clients from sticking to cookie-less backup server
7775 * when they have incidentely learned an empty cookie, we simply ignore
7776 * empty cookies and mark them as invalid.
7777 * The same behaviour is applied when persistence must be ignored.
7778 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007779 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007780 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007781
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 while (srv) {
7783 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7784 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007785 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007786 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007787 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 /* we found the server and we can use it */
7789 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007790 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007791 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007792 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007793 break;
7794 } else {
7795 /* we found a server, but it's down,
7796 * mark it as such and go on in case
7797 * another one is available.
7798 */
7799 txn->flags &= ~TX_CK_MASK;
7800 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007801 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007802 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007803 srv = srv->next;
7804 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007805
Willy Tarreauf64d1412010-10-07 20:06:11 +02007806 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007807 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007808 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007809 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007810 txn->flags |= TX_CK_UNUSED;
7811 else
7812 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007813 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007814
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007815 /* depending on the cookie mode, we may have to either :
7816 * - delete the complete cookie if we're in insert+indirect mode, so that
7817 * the server never sees it ;
7818 * - remove the server id from the cookie value, and tag the cookie as an
7819 * application cookie so that it does not get accidentely removed later,
7820 * if we're in cookie prefix mode
7821 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007822 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007823 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007824
Willy Tarreau9b28e032012-10-12 23:49:43 +02007825 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007826 val_end += delta;
7827 next += delta;
7828 hdr_end += delta;
7829 hdr_next += delta;
7830 cur_hdr->len += delta;
7831 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007832
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007833 del_from = NULL;
7834 preserve_hdr = 1; /* we want to keep this cookie */
7835 }
7836 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007837 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007838 del_from = prev;
7839 }
7840 } else {
7841 /* This is not our cookie, so we must preserve it. But if we already
7842 * scheduled another cookie for removal, we cannot remove the
7843 * complete header, but we can remove the previous block itself.
7844 */
7845 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007846
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007847 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007848 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007849 if (att_beg >= del_from)
7850 att_beg += delta;
7851 if (att_end >= del_from)
7852 att_end += delta;
7853 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007854 val_end += delta;
7855 next += delta;
7856 hdr_end += delta;
7857 hdr_next += delta;
7858 cur_hdr->len += delta;
7859 http_msg_move_end(&txn->req, delta);
7860 prev = del_from;
7861 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007862 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007863 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007865 /* continue with next cookie on this header line */
7866 att_beg = next;
7867 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007868
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007869 /* There are no more cookies on this line.
7870 * We may still have one (or several) marked for deletion at the
7871 * end of the line. We must do this now in two ways :
7872 * - if some cookies must be preserved, we only delete from the
7873 * mark to the end of line ;
7874 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007875 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007876 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007877 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007878 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007879 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007880 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007881 cur_hdr->len += delta;
7882 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007883 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007884
7885 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007886 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7887 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007888 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007889 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007890 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007891 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007892 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007893 }
7894
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007895 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007896 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007897 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007898}
7899
7900
Willy Tarreaua15645d2007-03-18 16:22:39 +01007901/* Iterate the same filter through all response headers contained in <rtr>.
7902 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7903 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007904int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007905{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007906 char *cur_ptr, *cur_end, *cur_next;
7907 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007908 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007909 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007910 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007911
7912 last_hdr = 0;
7913
Willy Tarreau9b28e032012-10-12 23:49:43 +02007914 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007915 old_idx = 0;
7916
7917 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007918 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007919 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007920 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007921 (exp->action == ACT_ALLOW ||
7922 exp->action == ACT_DENY))
7923 return 0;
7924
7925 cur_idx = txn->hdr_idx.v[old_idx].next;
7926 if (!cur_idx)
7927 break;
7928
7929 cur_hdr = &txn->hdr_idx.v[cur_idx];
7930 cur_ptr = cur_next;
7931 cur_end = cur_ptr + cur_hdr->len;
7932 cur_next = cur_end + cur_hdr->cr + 1;
7933
7934 /* Now we have one header between cur_ptr and cur_end,
7935 * and the next header starts at cur_next.
7936 */
7937
Willy Tarreau15a53a42015-01-21 13:39:42 +01007938 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007939 switch (exp->action) {
7940 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007941 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007942 last_hdr = 1;
7943 break;
7944
7945 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007946 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947 last_hdr = 1;
7948 break;
7949
7950 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007951 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7952 if (trash.len < 0)
7953 return -1;
7954
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007955 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007956 /* FIXME: if the user adds a newline in the replacement, the
7957 * index will not be recalculated for now, and the new line
7958 * will not be counted as a new header.
7959 */
7960
7961 cur_end += delta;
7962 cur_next += delta;
7963 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007964 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965 break;
7966
7967 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007968 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007969 cur_next += delta;
7970
Willy Tarreaufa355d42009-11-29 18:12:29 +01007971 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007972 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7973 txn->hdr_idx.used--;
7974 cur_hdr->len = 0;
7975 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007976 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007977 break;
7978
7979 }
7980 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981
7982 /* keep the link from this header to next one in case of later
7983 * removal of next header.
7984 */
7985 old_idx = cur_idx;
7986 }
7987 return 0;
7988}
7989
7990
7991/* Apply the filter to the status line in the response buffer <rtr>.
7992 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7993 * or -1 if a replacement resulted in an invalid status line.
7994 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007995int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007997 char *cur_ptr, *cur_end;
7998 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007999 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008000 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001
8002
Willy Tarreau3d300592007-03-18 18:34:41 +01008003 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008005 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008006 (exp->action == ACT_ALLOW ||
8007 exp->action == ACT_DENY))
8008 return 0;
8009 else if (exp->action == ACT_REMOVE)
8010 return 0;
8011
8012 done = 0;
8013
Willy Tarreau9b28e032012-10-12 23:49:43 +02008014 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008015 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016
8017 /* Now we have the status line between cur_ptr and cur_end */
8018
Willy Tarreau15a53a42015-01-21 13:39:42 +01008019 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020 switch (exp->action) {
8021 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008022 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 done = 1;
8024 break;
8025
8026 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008027 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028 done = 1;
8029 break;
8030
8031 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008032 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8033 if (trash.len < 0)
8034 return -1;
8035
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008036 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008037 /* FIXME: if the user adds a newline in the replacement, the
8038 * index will not be recalculated for now, and the new line
8039 * will not be counted as a new header.
8040 */
8041
Willy Tarreaufa355d42009-11-29 18:12:29 +01008042 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008044 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008045 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 cur_ptr, cur_end + 1,
8047 NULL, NULL);
8048 if (unlikely(!cur_end))
8049 return -1;
8050
8051 /* we have a full respnse and we know that we have either a CR
8052 * or an LF at <ptr>.
8053 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008054 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008055 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008056 /* there is no point trying this regex on headers */
8057 return 1;
8058 }
8059 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060 return done;
8061}
8062
8063
8064
8065/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008066 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8068 * unparsable response.
8069 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008070int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008071{
Willy Tarreau192252e2015-04-04 01:47:55 +02008072 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008073 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008074 struct hdr_exp *exp;
8075
8076 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008077 int ret;
8078
8079 /*
8080 * The interleaving of transformations and verdicts
8081 * makes it difficult to decide to continue or stop
8082 * the evaluation.
8083 */
8084
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008085 if (txn->flags & TX_SVDENY)
8086 break;
8087
Willy Tarreau3d300592007-03-18 18:34:41 +01008088 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8090 exp->action == ACT_PASS)) {
8091 exp = exp->next;
8092 continue;
8093 }
8094
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008095 /* if this filter had a condition, evaluate it now and skip to
8096 * next filter if the condition does not match.
8097 */
8098 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008099 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008100 ret = acl_pass(ret);
8101 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8102 ret = !ret;
8103 if (!ret)
8104 continue;
8105 }
8106
Willy Tarreaua15645d2007-03-18 16:22:39 +01008107 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008108 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008109 if (unlikely(ret < 0))
8110 return -1;
8111
8112 if (likely(ret == 0)) {
8113 /* The filter did not match the response, it can be
8114 * iterated through all headers.
8115 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008116 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8117 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119 }
8120 return 0;
8121}
8122
8123
Willy Tarreaua15645d2007-03-18 16:22:39 +01008124/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008125 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008126 * desirable to call it only when needed. This function is also used when we
8127 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008129void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008131 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008132 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008133 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008134 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008136 char *hdr_beg, *hdr_end, *hdr_next;
8137 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008138
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139 /* Iterate through the headers.
8140 * we start with the start line.
8141 */
8142 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008143 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144
8145 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8146 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008147 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008148
8149 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008150 hdr_beg = hdr_next;
8151 hdr_end = hdr_beg + cur_hdr->len;
8152 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153
Willy Tarreau24581ba2010-08-31 22:39:35 +02008154 /* We have one full header between hdr_beg and hdr_end, and the
8155 * next header starts at hdr_next. We're only interested in
8156 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157 */
8158
Willy Tarreau24581ba2010-08-31 22:39:35 +02008159 is_cookie2 = 0;
8160 prev = hdr_beg + 10;
8161 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008162 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008163 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8164 if (!val) {
8165 old_idx = cur_idx;
8166 continue;
8167 }
8168 is_cookie2 = 1;
8169 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008170 }
8171
Willy Tarreau24581ba2010-08-31 22:39:35 +02008172 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8173 * <prev> points to the colon.
8174 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008175 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008176
Willy Tarreau24581ba2010-08-31 22:39:35 +02008177 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8178 * check-cache is enabled) and we are not interested in checking
8179 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008180 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008181 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008182 return;
8183
Willy Tarreau24581ba2010-08-31 22:39:35 +02008184 /* OK so now we know we have to process this response cookie.
8185 * The format of the Set-Cookie header is slightly different
8186 * from the format of the Cookie header in that it does not
8187 * support the comma as a cookie delimiter (thus the header
8188 * cannot be folded) because the Expires attribute described in
8189 * the original Netscape's spec may contain an unquoted date
8190 * with a comma inside. We have to live with this because
8191 * many browsers don't support Max-Age and some browsers don't
8192 * support quoted strings. However the Set-Cookie2 header is
8193 * clean.
8194 *
8195 * We have to keep multiple pointers in order to support cookie
8196 * removal at the beginning, middle or end of header without
8197 * corrupting the header (in case of set-cookie2). A special
8198 * pointer, <scav> points to the beginning of the set-cookie-av
8199 * fields after the first semi-colon. The <next> pointer points
8200 * either to the end of line (set-cookie) or next unquoted comma
8201 * (set-cookie2). All of these headers are valid :
8202 *
8203 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8204 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8205 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8206 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8207 * | | | | | | | | | |
8208 * | | | | | | | | +-> next hdr_end <--+
8209 * | | | | | | | +------------> scav
8210 * | | | | | | +--------------> val_end
8211 * | | | | | +--------------------> val_beg
8212 * | | | | +----------------------> equal
8213 * | | | +------------------------> att_end
8214 * | | +----------------------------> att_beg
8215 * | +------------------------------> prev
8216 * +-----------------------------------------> hdr_beg
8217 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218
Willy Tarreau24581ba2010-08-31 22:39:35 +02008219 for (; prev < hdr_end; prev = next) {
8220 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221
Willy Tarreau24581ba2010-08-31 22:39:35 +02008222 /* find att_beg */
8223 att_beg = prev + 1;
8224 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8225 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226
Willy Tarreau24581ba2010-08-31 22:39:35 +02008227 /* find att_end : this is the first character after the last non
8228 * space before the equal. It may be equal to hdr_end.
8229 */
8230 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231
Willy Tarreau24581ba2010-08-31 22:39:35 +02008232 while (equal < hdr_end) {
8233 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8234 break;
8235 if (http_is_spht[(unsigned char)*equal++])
8236 continue;
8237 att_end = equal;
8238 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239
Willy Tarreau24581ba2010-08-31 22:39:35 +02008240 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8241 * is between <att_beg> and <equal>, both may be identical.
8242 */
8243
8244 /* look for end of cookie if there is an equal sign */
8245 if (equal < hdr_end && *equal == '=') {
8246 /* look for the beginning of the value */
8247 val_beg = equal + 1;
8248 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8249 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008250
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 /* find the end of the value, respecting quotes */
8252 next = find_cookie_value_end(val_beg, hdr_end);
8253
8254 /* make val_end point to the first white space or delimitor after the value */
8255 val_end = next;
8256 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8257 val_end--;
8258 } else {
8259 /* <equal> points to next comma, semi-colon or EOL */
8260 val_beg = val_end = next = equal;
8261 }
8262
8263 if (next < hdr_end) {
8264 /* Set-Cookie2 supports multiple cookies, and <next> points to
8265 * a colon or semi-colon before the end. So skip all attr-value
8266 * pairs and look for the next comma. For Set-Cookie, since
8267 * commas are permitted in values, skip to the end.
8268 */
8269 if (is_cookie2)
8270 next = find_hdr_value_end(next, hdr_end);
8271 else
8272 next = hdr_end;
8273 }
8274
8275 /* Now everything is as on the diagram above */
8276
8277 /* Ignore cookies with no equal sign */
8278 if (equal == val_end)
8279 continue;
8280
8281 /* If there are spaces around the equal sign, we need to
8282 * strip them otherwise we'll get trouble for cookie captures,
8283 * or even for rewrites. Since this happens extremely rarely,
8284 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008285 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008286 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8287 int stripped_before = 0;
8288 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008289
Willy Tarreau24581ba2010-08-31 22:39:35 +02008290 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008291 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008292 equal += stripped_before;
8293 val_beg += stripped_before;
8294 }
8295
8296 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008297 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008298 val_beg += stripped_after;
8299 stripped_before += stripped_after;
8300 }
8301
8302 val_end += stripped_before;
8303 next += stripped_before;
8304 hdr_end += stripped_before;
8305 hdr_next += stripped_before;
8306 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008307 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008308 }
8309
8310 /* First, let's see if we want to capture this cookie. We check
8311 * that we don't already have a server side cookie, because we
8312 * can only capture one. Also as an optimisation, we ignore
8313 * cookies shorter than the declared name.
8314 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008315 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008316 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008317 (val_end - att_beg >= sess->fe->capture_namelen) &&
8318 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008319 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008320 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008321 Alert("HTTP logging : out of memory.\n");
8322 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008323 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008324 if (log_len > sess->fe->capture_len)
8325 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008326 memcpy(txn->srv_cookie, att_beg, log_len);
8327 txn->srv_cookie[log_len] = 0;
8328 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008329 }
8330
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008331 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008332 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008333 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008334 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8335 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008336 /* assume passive cookie by default */
8337 txn->flags &= ~TX_SCK_MASK;
8338 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008339
8340 /* If the cookie is in insert mode on a known server, we'll delete
8341 * this occurrence because we'll insert another one later.
8342 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008343 * a direct access.
8344 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008345 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008346 /* The "preserve" flag was set, we don't want to touch the
8347 * server's cookie.
8348 */
8349 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008350 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008351 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008352 /* this cookie must be deleted */
8353 if (*prev == ':' && next == hdr_end) {
8354 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008355 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008356 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8357 txn->hdr_idx.used--;
8358 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008359 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008360 hdr_next += delta;
8361 http_msg_move_end(&txn->rsp, delta);
8362 /* note: while both invalid now, <next> and <hdr_end>
8363 * are still equal, so the for() will stop as expected.
8364 */
8365 } else {
8366 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008367 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008368 next = prev;
8369 hdr_end += delta;
8370 hdr_next += delta;
8371 cur_hdr->len += delta;
8372 http_msg_move_end(&txn->rsp, delta);
8373 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008374 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008375 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008376 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008377 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008378 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008379 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008380 * with this server since we know it.
8381 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008382 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008383 next += delta;
8384 hdr_end += delta;
8385 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008386 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008387 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008388
Willy Tarreauf1348312010-10-07 15:54:11 +02008389 txn->flags &= ~TX_SCK_MASK;
8390 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008391 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008392 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008393 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008394 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008395 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008396 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008397 next += delta;
8398 hdr_end += delta;
8399 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008400 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008401 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008402
Willy Tarreau827aee92011-03-10 16:55:02 +01008403 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008404 txn->flags &= ~TX_SCK_MASK;
8405 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406 }
8407 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008408 /* that's done for this cookie, check the next one on the same
8409 * line when next != hdr_end (only if is_cookie2).
8410 */
8411 }
8412 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008413 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008414 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008415}
8416
8417
Willy Tarreaua15645d2007-03-18 16:22:39 +01008418/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008419 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008420 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008421void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008422{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008423 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008424 char *p1, *p2;
8425
8426 char *cur_ptr, *cur_end, *cur_next;
8427 int cur_idx;
8428
Willy Tarreau5df51872007-11-25 16:20:08 +01008429 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430 return;
8431
8432 /* Iterate through the headers.
8433 * we start with the start line.
8434 */
8435 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008436 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437
8438 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8439 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008440 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008441
8442 cur_hdr = &txn->hdr_idx.v[cur_idx];
8443 cur_ptr = cur_next;
8444 cur_end = cur_ptr + cur_hdr->len;
8445 cur_next = cur_end + cur_hdr->cr + 1;
8446
8447 /* We have one full header between cur_ptr and cur_end, and the
8448 * next header starts at cur_next. We're only interested in
8449 * "Cookie:" headers.
8450 */
8451
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008452 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8453 if (val) {
8454 if ((cur_end - (cur_ptr + val) >= 8) &&
8455 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8456 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8457 return;
8458 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008459 }
8460
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008461 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8462 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008463 continue;
8464
8465 /* OK, right now we know we have a cache-control header at cur_ptr */
8466
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008467 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468
8469 if (p1 >= cur_end) /* no more info */
8470 continue;
8471
8472 /* p1 is at the beginning of the value */
8473 p2 = p1;
8474
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008475 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008476 p2++;
8477
8478 /* we have a complete value between p1 and p2 */
8479 if (p2 < cur_end && *p2 == '=') {
8480 /* we have something of the form no-cache="set-cookie" */
8481 if ((cur_end - p1 >= 21) &&
8482 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8483 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008484 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008485 continue;
8486 }
8487
8488 /* OK, so we know that either p2 points to the end of string or to a comma */
8489 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008490 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8492 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8493 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008494 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495 return;
8496 }
8497
8498 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008499 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008500 continue;
8501 }
8502 }
8503}
8504
Willy Tarreau58f10d72006-12-04 02:26:12 +01008505
Willy Tarreaub2513902006-12-17 14:52:38 +01008506/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008507 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008508 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008509 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008510 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008511 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008512 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008513 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008514 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008515int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008516{
8517 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008518 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008519 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008520
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008521 if (!uri_auth)
8522 return 0;
8523
Cyril Bonté70be45d2010-10-12 00:14:35 +02008524 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008525 return 0;
8526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008527 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008528 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008529 return 0;
8530
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008531 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008532 return 0;
8533
Willy Tarreaub2513902006-12-17 14:52:38 +01008534 return 1;
8535}
8536
Willy Tarreau4076a152009-04-02 15:18:36 +02008537/*
8538 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008539 * By default it tries to report the error position as msg->err_pos. However if
8540 * this one is not set, it will then report msg->next, which is the last known
8541 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008542 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008543 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008544void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008545 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008546 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008547{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008548 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008549 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008550 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008551
Willy Tarreau9b28e032012-10-12 23:49:43 +02008552 es->len = MIN(chn->buf->i, sizeof(es->buf));
8553 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008554 len1 = MIN(len1, es->len);
8555 len2 = es->len - len1; /* remaining data if buffer wraps */
8556
Willy Tarreau9b28e032012-10-12 23:49:43 +02008557 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008558 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008559 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008560
Willy Tarreau4076a152009-04-02 15:18:36 +02008561 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008562 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008563 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008564 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008565
Willy Tarreau4076a152009-04-02 15:18:36 +02008566 es->when = date; // user-visible date
8567 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008568 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008569 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008570 if (objt_conn(sess->origin))
8571 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008572 else
8573 memset(&es->src, 0, sizeof(es->src));
8574
Willy Tarreau078272e2010-12-12 12:46:33 +01008575 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008576 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008577 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008578 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008579 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008580 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008581 es->b_out = chn->buf->o;
8582 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008583 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008584 es->m_clen = msg->chunk_len;
8585 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008586}
Willy Tarreaub2513902006-12-17 14:52:38 +01008587
Willy Tarreau294c4732011-12-16 21:35:50 +01008588/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8589 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8590 * performed over the whole headers. Otherwise it must contain a valid header
8591 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8592 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8593 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8594 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008595 * -1. The value fetch stops at commas, so this function is suited for use with
8596 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008597 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008598 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008599unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008600 struct hdr_idx *idx, int occ,
8601 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008602{
Willy Tarreau294c4732011-12-16 21:35:50 +01008603 struct hdr_ctx local_ctx;
8604 char *ptr_hist[MAX_HDR_HISTORY];
8605 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008606 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008607 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008608
Willy Tarreau294c4732011-12-16 21:35:50 +01008609 if (!ctx) {
8610 local_ctx.idx = 0;
8611 ctx = &local_ctx;
8612 }
8613
Willy Tarreaubce70882009-09-07 11:51:47 +02008614 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008615 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008616 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008617 occ--;
8618 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008619 *vptr = ctx->line + ctx->val;
8620 *vlen = ctx->vlen;
8621 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008622 }
8623 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008624 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008625 }
8626
8627 /* negative occurrence, we scan all the list then walk back */
8628 if (-occ > MAX_HDR_HISTORY)
8629 return 0;
8630
Willy Tarreau294c4732011-12-16 21:35:50 +01008631 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008632 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008633 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8634 len_hist[hist_ptr] = ctx->vlen;
8635 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008636 hist_ptr = 0;
8637 found++;
8638 }
8639 if (-occ > found)
8640 return 0;
8641 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008642 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8643 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8644 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008645 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008646 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008647 if (hist_ptr >= MAX_HDR_HISTORY)
8648 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008649 *vptr = ptr_hist[hist_ptr];
8650 *vlen = len_hist[hist_ptr];
8651 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008652}
8653
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008654/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8655 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8656 * performed over the whole headers. Otherwise it must contain a valid header
8657 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8658 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8659 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8660 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8661 * -1. This function differs from http_get_hdr() in that it only returns full
8662 * line header values and does not stop at commas.
8663 * The return value is 0 if nothing was found, or non-zero otherwise.
8664 */
8665unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8666 struct hdr_idx *idx, int occ,
8667 struct hdr_ctx *ctx, char **vptr, int *vlen)
8668{
8669 struct hdr_ctx local_ctx;
8670 char *ptr_hist[MAX_HDR_HISTORY];
8671 int len_hist[MAX_HDR_HISTORY];
8672 unsigned int hist_ptr;
8673 int found;
8674
8675 if (!ctx) {
8676 local_ctx.idx = 0;
8677 ctx = &local_ctx;
8678 }
8679
8680 if (occ >= 0) {
8681 /* search from the beginning */
8682 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8683 occ--;
8684 if (occ <= 0) {
8685 *vptr = ctx->line + ctx->val;
8686 *vlen = ctx->vlen;
8687 return 1;
8688 }
8689 }
8690 return 0;
8691 }
8692
8693 /* negative occurrence, we scan all the list then walk back */
8694 if (-occ > MAX_HDR_HISTORY)
8695 return 0;
8696
8697 found = hist_ptr = 0;
8698 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8699 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8700 len_hist[hist_ptr] = ctx->vlen;
8701 if (++hist_ptr >= MAX_HDR_HISTORY)
8702 hist_ptr = 0;
8703 found++;
8704 }
8705 if (-occ > found)
8706 return 0;
8707 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8708 * find occurrence -occ, so we have to check [hist_ptr+occ].
8709 */
8710 hist_ptr += occ;
8711 if (hist_ptr >= MAX_HDR_HISTORY)
8712 hist_ptr -= MAX_HDR_HISTORY;
8713 *vptr = ptr_hist[hist_ptr];
8714 *vlen = len_hist[hist_ptr];
8715 return 1;
8716}
8717
Willy Tarreaubaaee002006-06-26 02:48:02 +02008718/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008719 * Print a debug line with a header. Always stop at the first CR or LF char,
8720 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8721 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008722 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008723void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008724{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008725 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008726 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008727
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008728 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008729 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008730 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008731 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 +02008732
8733 for (max = 0; start + max < end; max++)
8734 if (start[max] == '\r' || start[max] == '\n')
8735 break;
8736
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008737 UBOUND(max, trash.size - trash.len - 3);
8738 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8739 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008740 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008741}
8742
Willy Tarreaueee5b512015-04-03 23:46:31 +02008743
8744/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8745 * The hdr_idx is allocated as well. In case of allocation failure, everything
8746 * allocated is freed and NULL is returned. Otherwise the new transaction is
8747 * assigned to the stream and returned.
8748 */
8749struct http_txn *http_alloc_txn(struct stream *s)
8750{
8751 struct http_txn *txn = s->txn;
8752
8753 if (txn)
8754 return txn;
8755
8756 txn = pool_alloc2(pool2_http_txn);
8757 if (!txn)
8758 return txn;
8759
8760 txn->hdr_idx.size = global.tune.max_http_hdr;
8761 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8762 if (!txn->hdr_idx.v) {
8763 pool_free2(pool2_http_txn, txn);
8764 return NULL;
8765 }
8766
8767 s->txn = txn;
8768 return txn;
8769}
8770
Willy Tarreau0937bc42009-12-22 15:03:09 +01008771/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008772 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008773 * the required fields are properly allocated and that we only need to (re)init
8774 * them. This should be used before processing any new request.
8775 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008776void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008777{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008778 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008779 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008780
8781 txn->flags = 0;
8782 txn->status = -1;
8783
Willy Tarreauf64d1412010-10-07 20:06:11 +02008784 txn->cookie_first_date = 0;
8785 txn->cookie_last_date = 0;
8786
Willy Tarreaueee5b512015-04-03 23:46:31 +02008787 txn->srv_cookie = NULL;
8788 txn->cli_cookie = NULL;
8789 txn->uri = NULL;
8790
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008791 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008792 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008793 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008794 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008795 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008796 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008797 txn->req.chunk_len = 0LL;
8798 txn->req.body_len = 0LL;
8799 txn->rsp.chunk_len = 0LL;
8800 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008801 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8802 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008803 txn->req.chn = &s->req;
8804 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008805
8806 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008807
8808 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8809 if (fe->options2 & PR_O2_REQBUG_OK)
8810 txn->req.err_pos = -1; /* let buggy requests pass */
8811
Willy Tarreau0937bc42009-12-22 15:03:09 +01008812 if (txn->hdr_idx.v)
8813 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008814
8815 vars_init(&s->vars_txn, SCOPE_TXN);
8816 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008817}
8818
8819/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008820void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008821{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008822 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008823 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008824
Willy Tarreau75195602014-03-11 15:48:55 +01008825 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008826 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008827 s->comp_algo->end(&s->comp_ctx);
8828 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008829 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008830
Willy Tarreau0937bc42009-12-22 15:03:09 +01008831 /* these ones will have been dynamically allocated */
8832 pool_free2(pool2_requri, txn->uri);
8833 pool_free2(pool2_capture, txn->cli_cookie);
8834 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008835 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008836
William Lallemanda73203e2012-03-12 12:48:57 +01008837 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008838 txn->uri = NULL;
8839 txn->srv_cookie = NULL;
8840 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008841
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008842 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008843 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008844 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008845 pool_free2(h->pool, s->req_cap[h->index]);
8846 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008847 }
8848
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008849 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008850 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008851 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008852 pool_free2(h->pool, s->res_cap[h->index]);
8853 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008854 }
8855
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008856 vars_prune(&s->vars_txn, s);
8857 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008858}
8859
8860/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008861void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008862{
8863 http_end_txn(s);
8864 http_init_txn(s);
8865
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008866 /* reinitialise the current rule list pointer to NULL. We are sure that
8867 * any rulelist match the NULL pointer.
8868 */
8869 s->current_rule_list = NULL;
8870
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008871 s->be = strm_fe(s);
8872 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008873 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008874 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008875 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008876 /* re-init store persistence */
8877 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008878 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008879
Willy Tarreau0937bc42009-12-22 15:03:09 +01008880 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008881
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008882 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008883
Willy Tarreau739cfba2010-01-25 23:11:14 +01008884 /* We must trim any excess data from the response buffer, because we
8885 * may have blocked an invalid response from a server that we don't
8886 * want to accidentely forward once we disable the analysers, nor do
8887 * we want those data to come along with next response. A typical
8888 * example of such data would be from a buggy server responding to
8889 * a HEAD with some data, or sending more than the advertised
8890 * content-length.
8891 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008892 if (unlikely(s->res.buf->i))
8893 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008894
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008895 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008896 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008898 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008899 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008900
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008901 s->req.rex = TICK_ETERNITY;
8902 s->req.wex = TICK_ETERNITY;
8903 s->req.analyse_exp = TICK_ETERNITY;
8904 s->res.rex = TICK_ETERNITY;
8905 s->res.wex = TICK_ETERNITY;
8906 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008907}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008908
Sasha Pachev218f0642014-06-16 12:05:59 -06008909void free_http_res_rules(struct list *r)
8910{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008911 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008912
8913 list_for_each_entry_safe(pr, tr, r, list) {
8914 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008915 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008916 free(pr);
8917 }
8918}
8919
8920void free_http_req_rules(struct list *r)
8921{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008922 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008923
8924 list_for_each_entry_safe(pr, tr, r, list) {
8925 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008926 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008927 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008928
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008929 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008930 free(pr);
8931 }
8932}
8933
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008934/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008935struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008936{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008937 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008938 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008939 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008940 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008941
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008942 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008943 if (!rule) {
8944 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008945 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008946 }
8947
CJ Ess108b1dd2015-04-07 12:03:37 -04008948 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008949 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008950 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008951 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008952 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008953 int code;
8954 int hc;
8955
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008956 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01008957 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008958 if (strcmp(args[cur_arg], "deny_status") == 0) {
8959 cur_arg++;
8960 if (!args[cur_arg]) {
8961 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8962 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8963 goto out_err;
8964 }
8965
8966 code = atol(args[cur_arg]);
8967 cur_arg++;
8968 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8969 if (http_err_codes[hc] == code) {
8970 rule->deny_status = hc;
8971 break;
8972 }
8973 }
8974
8975 if (hc >= HTTP_ERR_SIZE) {
8976 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
8977 file, linenum, code);
8978 }
8979 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01008980 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008981 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008982 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008983 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008984 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008985 cur_arg = 1;
8986
8987 while(*args[cur_arg]) {
8988 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008989 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008990 cur_arg+=2;
8991 continue;
8992 } else
8993 break;
8994 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008995 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008996 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008997 cur_arg = 1;
8998
8999 if (!*args[cur_arg] ||
9000 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9001 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9002 file, linenum, args[0]);
9003 goto out_err;
9004 }
9005 rule->arg.nice = atoi(args[cur_arg]);
9006 if (rule->arg.nice < -1024)
9007 rule->arg.nice = -1024;
9008 else if (rule->arg.nice > 1024)
9009 rule->arg.nice = 1024;
9010 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009011 } else if (!strcmp(args[0], "set-tos")) {
9012#ifdef IP_TOS
9013 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009014 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009015 cur_arg = 1;
9016
9017 if (!*args[cur_arg] ||
9018 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9019 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9020 file, linenum, args[0]);
9021 goto out_err;
9022 }
9023
9024 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9025 if (err && *err != '\0') {
9026 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9027 file, linenum, err, args[0]);
9028 goto out_err;
9029 }
9030 cur_arg++;
9031#else
9032 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9033 goto out_err;
9034#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009035 } else if (!strcmp(args[0], "set-mark")) {
9036#ifdef SO_MARK
9037 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009038 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009039 cur_arg = 1;
9040
9041 if (!*args[cur_arg] ||
9042 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9043 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9044 file, linenum, args[0]);
9045 goto out_err;
9046 }
9047
9048 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9049 if (err && *err != '\0') {
9050 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9051 file, linenum, err, args[0]);
9052 goto out_err;
9053 }
9054 cur_arg++;
9055 global.last_checks |= LSTCHK_NETADM;
9056#else
9057 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9058 goto out_err;
9059#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009060 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009061 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009062 cur_arg = 1;
9063
9064 if (!*args[cur_arg] ||
9065 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9066 bad_log_level:
9067 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9068 file, linenum, args[0]);
9069 goto out_err;
9070 }
9071 if (strcmp(args[cur_arg], "silent") == 0)
9072 rule->arg.loglevel = -1;
9073 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9074 goto bad_log_level;
9075 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009076 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009077 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009078 cur_arg = 1;
9079
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009080 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9081 (*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 +01009082 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9083 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009084 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009085 }
9086
9087 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9088 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9089 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009090
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009091 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009092 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009093 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9094 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009095 free(proxy->conf.lfs_file);
9096 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9097 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009098 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009099 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009100 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009101 cur_arg = 1;
9102
9103 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009104 (*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 -06009105 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9106 file, linenum, args[0]);
9107 goto out_err;
9108 }
9109
9110 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9111 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9112 LIST_INIT(&rule->arg.hdr_add.fmt);
9113
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009114 error = NULL;
9115 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9116 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9117 args[cur_arg + 1], error);
9118 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009119 goto out_err;
9120 }
9121
9122 proxy->conf.args.ctx = ARGC_HRQ;
9123 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9124 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9125 file, linenum);
9126
9127 free(proxy->conf.lfs_file);
9128 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9129 proxy->conf.lfs_line = proxy->conf.args.line;
9130 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009131 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009132 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009133 cur_arg = 1;
9134
9135 if (!*args[cur_arg] ||
9136 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9137 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9138 file, linenum, args[0]);
9139 goto out_err;
9140 }
9141
9142 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9143 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9144
9145 proxy->conf.args.ctx = ARGC_HRQ;
9146 free(proxy->conf.lfs_file);
9147 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9148 proxy->conf.lfs_line = proxy->conf.args.line;
9149 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009150 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9151 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009152 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009153 struct sample_expr *expr;
9154 unsigned int where;
9155 char *err = NULL;
9156
9157 cur_arg = 1;
9158 proxy->conf.args.ctx = ARGC_TRK;
9159
9160 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9161 if (!expr) {
9162 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9163 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9164 free(err);
9165 goto out_err;
9166 }
9167
9168 where = 0;
9169 if (proxy->cap & PR_CAP_FE)
9170 where |= SMP_VAL_FE_HRQ_HDR;
9171 if (proxy->cap & PR_CAP_BE)
9172 where |= SMP_VAL_BE_HRQ_HDR;
9173
9174 if (!(expr->fetch->val & where)) {
9175 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9176 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9177 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9178 args[cur_arg-1], sample_src_names(expr->fetch->use));
9179 free(expr);
9180 goto out_err;
9181 }
9182
9183 if (strcmp(args[cur_arg], "table") == 0) {
9184 cur_arg++;
9185 if (!args[cur_arg]) {
9186 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9187 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9188 free(expr);
9189 goto out_err;
9190 }
9191 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009192 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009193 cur_arg++;
9194 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009195 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009196 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009197 } else if (strcmp(args[0], "redirect") == 0) {
9198 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009199 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009200
Willy Tarreaube4653b2015-05-28 15:26:58 +02009201 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009202 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9203 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9204 goto out_err;
9205 }
9206
9207 /* this redirect rule might already contain a parsed condition which
9208 * we'll pass to the http-request rule.
9209 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009210 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009211 rule->arg.redir = redir;
9212 rule->cond = redir->cond;
9213 redir->cond = NULL;
9214 cur_arg = 2;
9215 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009216 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9217 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009218 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009219 /*
9220 * '+ 8' for 'add-acl('
9221 * '- 9' for 'add-acl(' + trailing ')'
9222 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009223 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009224
9225 cur_arg = 1;
9226
9227 if (!*args[cur_arg] ||
9228 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9229 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9230 file, linenum, args[0]);
9231 goto out_err;
9232 }
9233
9234 LIST_INIT(&rule->arg.map.key);
9235 proxy->conf.args.ctx = ARGC_HRQ;
9236 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9237 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9238 file, linenum);
9239 free(proxy->conf.lfs_file);
9240 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9241 proxy->conf.lfs_line = proxy->conf.args.line;
9242 cur_arg += 1;
9243 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9244 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009245 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009246 /*
9247 * '+ 8' for 'del-acl('
9248 * '- 9' for 'del-acl(' + trailing ')'
9249 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009250 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009251
9252 cur_arg = 1;
9253
9254 if (!*args[cur_arg] ||
9255 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9256 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9257 file, linenum, args[0]);
9258 goto out_err;
9259 }
9260
9261 LIST_INIT(&rule->arg.map.key);
9262 proxy->conf.args.ctx = ARGC_HRQ;
9263 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9264 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9265 file, linenum);
9266 free(proxy->conf.lfs_file);
9267 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9268 proxy->conf.lfs_line = proxy->conf.args.line;
9269 cur_arg += 1;
9270 } else if (strncmp(args[0], "del-map", 7) == 0) {
9271 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009272 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009273 /*
9274 * '+ 8' for 'del-map('
9275 * '- 9' for 'del-map(' + trailing ')'
9276 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009277 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009278
9279 cur_arg = 1;
9280
9281 if (!*args[cur_arg] ||
9282 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9283 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9284 file, linenum, args[0]);
9285 goto out_err;
9286 }
9287
9288 LIST_INIT(&rule->arg.map.key);
9289 proxy->conf.args.ctx = ARGC_HRQ;
9290 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9291 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9292 file, linenum);
9293 free(proxy->conf.lfs_file);
9294 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9295 proxy->conf.lfs_line = proxy->conf.args.line;
9296 cur_arg += 1;
9297 } else if (strncmp(args[0], "set-map", 7) == 0) {
9298 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009299 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009300 /*
9301 * '+ 8' for 'set-map('
9302 * '- 9' for 'set-map(' + trailing ')'
9303 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009304 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009305
9306 cur_arg = 1;
9307
9308 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9309 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9310 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9311 file, linenum, args[0]);
9312 goto out_err;
9313 }
9314
9315 LIST_INIT(&rule->arg.map.key);
9316 LIST_INIT(&rule->arg.map.value);
9317 proxy->conf.args.ctx = ARGC_HRQ;
9318
9319 /* key pattern */
9320 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9321 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9322 file, linenum);
9323
9324 /* value pattern */
9325 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9326 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9327 file, linenum);
9328 free(proxy->conf.lfs_file);
9329 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9330 proxy->conf.lfs_line = proxy->conf.args.line;
9331
9332 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009333 } else if (strncmp(args[0], "set-src", 7) == 0) {
9334 struct sample_expr *expr;
9335 unsigned int where;
9336 char *err = NULL;
9337
9338 cur_arg = 1;
9339 proxy->conf.args.ctx = ARGC_HRQ;
9340
9341 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9342 if (!expr) {
9343 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9344 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9345 free(err);
9346 goto out_err;
9347 }
9348
9349 where = 0;
9350 if (proxy->cap & PR_CAP_FE)
9351 where |= SMP_VAL_FE_HRQ_HDR;
9352 if (proxy->cap & PR_CAP_BE)
9353 where |= SMP_VAL_BE_HRQ_HDR;
9354
9355 if (!(expr->fetch->val & where)) {
9356 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9357 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9358 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9359 args[cur_arg-1], sample_src_names(expr->fetch->use));
9360 free(expr);
9361 goto out_err;
9362 }
9363
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009364 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009365 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009366 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9367 char *errmsg = NULL;
9368 cur_arg = 1;
9369 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009370 rule->from = ACT_F_HTTP_REQ;
William Lallemand73025dd2014-04-24 14:38:37 +02009371 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9372 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9373 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9374 free(errmsg);
9375 goto out_err;
9376 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009377 } else {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009378 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'set-var', 'set-src', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009379 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009380 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009381 }
9382
9383 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9384 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009385 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009386
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009387 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9388 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9389 file, linenum, args[0], errmsg);
9390 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009391 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009392 }
9393 rule->cond = cond;
9394 }
9395 else if (*args[cur_arg]) {
9396 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9397 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9398 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009399 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009400 }
9401
9402 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009403 out_err:
9404 free(rule);
9405 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009406}
9407
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009408/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009409struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009410{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009411 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009412 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009413 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009414 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009415
9416 rule = calloc(1, sizeof(*rule));
9417 if (!rule) {
9418 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9419 goto out_err;
9420 }
9421
9422 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009423 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009424 cur_arg = 1;
9425 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009426 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009427 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009428 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009429 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009430 cur_arg = 1;
9431
9432 if (!*args[cur_arg] ||
9433 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9434 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9435 file, linenum, args[0]);
9436 goto out_err;
9437 }
9438 rule->arg.nice = atoi(args[cur_arg]);
9439 if (rule->arg.nice < -1024)
9440 rule->arg.nice = -1024;
9441 else if (rule->arg.nice > 1024)
9442 rule->arg.nice = 1024;
9443 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009444 } else if (!strcmp(args[0], "set-tos")) {
9445#ifdef IP_TOS
9446 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009447 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009448 cur_arg = 1;
9449
9450 if (!*args[cur_arg] ||
9451 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9452 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9453 file, linenum, args[0]);
9454 goto out_err;
9455 }
9456
9457 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9458 if (err && *err != '\0') {
9459 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9460 file, linenum, err, args[0]);
9461 goto out_err;
9462 }
9463 cur_arg++;
9464#else
9465 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9466 goto out_err;
9467#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009468 } else if (!strcmp(args[0], "set-mark")) {
9469#ifdef SO_MARK
9470 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009471 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009472 cur_arg = 1;
9473
9474 if (!*args[cur_arg] ||
9475 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9476 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9477 file, linenum, args[0]);
9478 goto out_err;
9479 }
9480
9481 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9482 if (err && *err != '\0') {
9483 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9484 file, linenum, err, args[0]);
9485 goto out_err;
9486 }
9487 cur_arg++;
9488 global.last_checks |= LSTCHK_NETADM;
9489#else
9490 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9491 goto out_err;
9492#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009493 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009494 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009495 cur_arg = 1;
9496
9497 if (!*args[cur_arg] ||
9498 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9499 bad_log_level:
9500 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9501 file, linenum, args[0]);
9502 goto out_err;
9503 }
9504 if (strcmp(args[cur_arg], "silent") == 0)
9505 rule->arg.loglevel = -1;
9506 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9507 goto bad_log_level;
9508 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009509 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009510 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009511 cur_arg = 1;
9512
9513 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9514 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9515 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9516 file, linenum, args[0]);
9517 goto out_err;
9518 }
9519
9520 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9521 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9522 LIST_INIT(&rule->arg.hdr_add.fmt);
9523
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009524 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009525 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009526 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9527 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009528 free(proxy->conf.lfs_file);
9529 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9530 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009531 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009532 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009533 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009534 cur_arg = 1;
9535
9536 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009537 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9538 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009539 file, linenum, args[0]);
9540 goto out_err;
9541 }
9542
9543 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9544 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9545 LIST_INIT(&rule->arg.hdr_add.fmt);
9546
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009547 error = NULL;
9548 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9549 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9550 args[cur_arg + 1], error);
9551 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009552 goto out_err;
9553 }
9554
9555 proxy->conf.args.ctx = ARGC_HRQ;
9556 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9557 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9558 file, linenum);
9559
9560 free(proxy->conf.lfs_file);
9561 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9562 proxy->conf.lfs_line = proxy->conf.args.line;
9563 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009564 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009565 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009566 cur_arg = 1;
9567
9568 if (!*args[cur_arg] ||
9569 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9570 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9571 file, linenum, args[0]);
9572 goto out_err;
9573 }
9574
9575 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9576 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9577
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009578 proxy->conf.args.ctx = ARGC_HRS;
9579 free(proxy->conf.lfs_file);
9580 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9581 proxy->conf.lfs_line = proxy->conf.args.line;
9582 cur_arg += 1;
9583 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9584 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009585 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009586 /*
9587 * '+ 8' for 'add-acl('
9588 * '- 9' for 'add-acl(' + trailing ')'
9589 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009590 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009591
9592 cur_arg = 1;
9593
9594 if (!*args[cur_arg] ||
9595 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9596 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9597 file, linenum, args[0]);
9598 goto out_err;
9599 }
9600
9601 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009602 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009603 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9604 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9605 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009606 free(proxy->conf.lfs_file);
9607 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9608 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009609
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009610 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009611 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9612 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009613 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009614 /*
9615 * '+ 8' for 'del-acl('
9616 * '- 9' for 'del-acl(' + trailing ')'
9617 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009618 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009619
9620 cur_arg = 1;
9621
9622 if (!*args[cur_arg] ||
9623 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9624 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9625 file, linenum, args[0]);
9626 goto out_err;
9627 }
9628
9629 LIST_INIT(&rule->arg.map.key);
9630 proxy->conf.args.ctx = ARGC_HRS;
9631 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9632 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9633 file, linenum);
9634 free(proxy->conf.lfs_file);
9635 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9636 proxy->conf.lfs_line = proxy->conf.args.line;
9637 cur_arg += 1;
9638 } else if (strncmp(args[0], "del-map", 7) == 0) {
9639 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009640 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009641 /*
9642 * '+ 8' for 'del-map('
9643 * '- 9' for 'del-map(' + trailing ')'
9644 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009645 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009646
9647 cur_arg = 1;
9648
9649 if (!*args[cur_arg] ||
9650 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9651 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9652 file, linenum, args[0]);
9653 goto out_err;
9654 }
9655
9656 LIST_INIT(&rule->arg.map.key);
9657 proxy->conf.args.ctx = ARGC_HRS;
9658 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9659 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9660 file, linenum);
9661 free(proxy->conf.lfs_file);
9662 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9663 proxy->conf.lfs_line = proxy->conf.args.line;
9664 cur_arg += 1;
9665 } else if (strncmp(args[0], "set-map", 7) == 0) {
9666 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009667 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009668 /*
9669 * '+ 8' for 'set-map('
9670 * '- 9' for 'set-map(' + trailing ')'
9671 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009672 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009673
9674 cur_arg = 1;
9675
9676 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9677 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9678 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9679 file, linenum, args[0]);
9680 goto out_err;
9681 }
9682
9683 LIST_INIT(&rule->arg.map.key);
9684 LIST_INIT(&rule->arg.map.value);
9685
9686 proxy->conf.args.ctx = ARGC_HRS;
9687
9688 /* key pattern */
9689 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9690 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9691 file, linenum);
9692
9693 /* value pattern */
9694 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9695 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9696 file, linenum);
9697
9698 free(proxy->conf.lfs_file);
9699 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9700 proxy->conf.lfs_line = proxy->conf.args.line;
9701
9702 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009703 } else if (strcmp(args[0], "redirect") == 0) {
9704 struct redirect_rule *redir;
9705 char *errmsg = NULL;
9706
9707 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9708 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9709 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9710 goto out_err;
9711 }
9712
9713 /* this redirect rule might already contain a parsed condition which
9714 * we'll pass to the http-request rule.
9715 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009716 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009717 rule->arg.redir = redir;
9718 rule->cond = redir->cond;
9719 redir->cond = NULL;
9720 cur_arg = 2;
9721 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009722 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9723 char *errmsg = NULL;
9724 cur_arg = 1;
9725 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009726 rule->from = ACT_F_HTTP_RES;
William Lallemand73025dd2014-04-24 14:38:37 +02009727 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9728 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9729 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9730 free(errmsg);
9731 goto out_err;
9732 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009733 } else {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009734 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', 'set-var' but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009735 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9736 goto out_err;
9737 }
9738
9739 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9740 struct acl_cond *cond;
9741 char *errmsg = NULL;
9742
9743 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9744 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9745 file, linenum, args[0], errmsg);
9746 free(errmsg);
9747 goto out_err;
9748 }
9749 rule->cond = cond;
9750 }
9751 else if (*args[cur_arg]) {
9752 Alert("parsing [%s:%d]: 'http-response %s' expects"
9753 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9754 file, linenum, args[0], args[cur_arg]);
9755 goto out_err;
9756 }
9757
9758 return rule;
9759 out_err:
9760 free(rule);
9761 return NULL;
9762}
9763
Willy Tarreau4baae242012-12-27 12:00:31 +01009764/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009765 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009766 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9767 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009768 */
9769struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009770 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009771{
9772 struct redirect_rule *rule;
9773 int cur_arg;
9774 int type = REDIRECT_TYPE_NONE;
9775 int code = 302;
9776 const char *destination = NULL;
9777 const char *cookie = NULL;
9778 int cookie_set = 0;
9779 unsigned int flags = REDIRECT_FLAG_NONE;
9780 struct acl_cond *cond = NULL;
9781
9782 cur_arg = 0;
9783 while (*(args[cur_arg])) {
9784 if (strcmp(args[cur_arg], "location") == 0) {
9785 if (!*args[cur_arg + 1])
9786 goto missing_arg;
9787
9788 type = REDIRECT_TYPE_LOCATION;
9789 cur_arg++;
9790 destination = args[cur_arg];
9791 }
9792 else if (strcmp(args[cur_arg], "prefix") == 0) {
9793 if (!*args[cur_arg + 1])
9794 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009795 type = REDIRECT_TYPE_PREFIX;
9796 cur_arg++;
9797 destination = args[cur_arg];
9798 }
9799 else if (strcmp(args[cur_arg], "scheme") == 0) {
9800 if (!*args[cur_arg + 1])
9801 goto missing_arg;
9802
9803 type = REDIRECT_TYPE_SCHEME;
9804 cur_arg++;
9805 destination = args[cur_arg];
9806 }
9807 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9808 if (!*args[cur_arg + 1])
9809 goto missing_arg;
9810
9811 cur_arg++;
9812 cookie = args[cur_arg];
9813 cookie_set = 1;
9814 }
9815 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9816 if (!*args[cur_arg + 1])
9817 goto missing_arg;
9818
9819 cur_arg++;
9820 cookie = args[cur_arg];
9821 cookie_set = 0;
9822 }
9823 else if (strcmp(args[cur_arg], "code") == 0) {
9824 if (!*args[cur_arg + 1])
9825 goto missing_arg;
9826
9827 cur_arg++;
9828 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009829 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009830 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009831 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009832 args[cur_arg - 1], args[cur_arg]);
9833 return NULL;
9834 }
9835 }
9836 else if (!strcmp(args[cur_arg],"drop-query")) {
9837 flags |= REDIRECT_FLAG_DROP_QS;
9838 }
9839 else if (!strcmp(args[cur_arg],"append-slash")) {
9840 flags |= REDIRECT_FLAG_APPEND_SLASH;
9841 }
9842 else if (strcmp(args[cur_arg], "if") == 0 ||
9843 strcmp(args[cur_arg], "unless") == 0) {
9844 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9845 if (!cond) {
9846 memprintf(errmsg, "error in condition: %s", *errmsg);
9847 return NULL;
9848 }
9849 break;
9850 }
9851 else {
9852 memprintf(errmsg,
9853 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9854 args[cur_arg]);
9855 return NULL;
9856 }
9857 cur_arg++;
9858 }
9859
9860 if (type == REDIRECT_TYPE_NONE) {
9861 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9862 return NULL;
9863 }
9864
Willy Tarreaube4653b2015-05-28 15:26:58 +02009865 if (dir && type != REDIRECT_TYPE_LOCATION) {
9866 memprintf(errmsg, "response only supports redirect type 'location'");
9867 return NULL;
9868 }
9869
Willy Tarreau4baae242012-12-27 12:00:31 +01009870 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9871 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009872 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009873
9874 if (!use_fmt) {
9875 /* old-style static redirect rule */
9876 rule->rdr_str = strdup(destination);
9877 rule->rdr_len = strlen(destination);
9878 }
9879 else {
9880 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009881
9882 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9883 * if prefix == "/", we don't want to add anything, otherwise it
9884 * makes it hard for the user to configure a self-redirection.
9885 */
Godbachd9722032014-12-18 15:44:58 +08009886 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009887 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009888 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009889 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9890 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009891 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009892 free(curproxy->conf.lfs_file);
9893 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9894 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009895 }
9896 }
9897
Willy Tarreau4baae242012-12-27 12:00:31 +01009898 if (cookie) {
9899 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9900 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9901 */
9902 rule->cookie_len = strlen(cookie);
9903 if (cookie_set) {
9904 rule->cookie_str = malloc(rule->cookie_len + 10);
9905 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9906 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9907 rule->cookie_len += 9;
9908 } else {
9909 rule->cookie_str = malloc(rule->cookie_len + 21);
9910 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9911 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9912 rule->cookie_len += 20;
9913 }
9914 }
9915 rule->type = type;
9916 rule->code = code;
9917 rule->flags = flags;
9918 LIST_INIT(&rule->list);
9919 return rule;
9920
9921 missing_arg:
9922 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9923 return NULL;
9924}
9925
Willy Tarreau8797c062007-05-07 00:55:35 +02009926/************************************************************************/
9927/* The code below is dedicated to ACL parsing and matching */
9928/************************************************************************/
9929
9930
Willy Tarreau14174bc2012-04-16 14:34:04 +02009931/* This function ensures that the prerequisites for an L7 fetch are ready,
9932 * which means that a request or response is ready. If some data is missing,
9933 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009934 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9935 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009936 *
9937 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009938 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9939 * decide whether or not an HTTP message is present ;
9940 * 0 if the requested data cannot be fetched or if it is certain that
9941 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009942 * 1 if an HTTP message is ready
9943 */
9944static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02009945smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009946 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009947{
Willy Tarreau192252e2015-04-04 01:47:55 +02009948 struct http_txn *txn;
9949 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009950
Willy Tarreaueee5b512015-04-03 23:46:31 +02009951 /* Note: this function may only be used from places where
9952 * http_init_txn() has already been done, and implies that <s>,
9953 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
9954 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009955 */
9956
Willy Tarreau192252e2015-04-04 01:47:55 +02009957 if (!s)
9958 return 0;
9959 txn = s->txn;
9960
9961 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009962 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +02009963 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009964
9965 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009966 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009967
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009968 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009969 /* If the buffer does not leave enough free space at the end,
9970 * we must first realign it.
9971 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009972 if (s->req.buf->p > s->req.buf->data &&
9973 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9974 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009975
Willy Tarreau14174bc2012-04-16 14:34:04 +02009976 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009977 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009978 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009979
9980 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009981 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009982 http_msg_analyzer(msg, &txn->hdr_idx);
9983
9984 /* Still no valid request ? */
9985 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009986 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009987 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009988 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009989 }
9990 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009991 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009992 return 0;
9993 }
9994
9995 /* OK we just got a valid HTTP request. We have some minor
9996 * preparation to perform so that further checks can rely
9997 * on HTTP tests.
9998 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009999
10000 /* If the request was parsed but was too large, we must absolutely
10001 * return an error so that it is not processed. At the moment this
10002 * cannot happen, but if the parsers are to change in the future,
10003 * we want this check to be maintained.
10004 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010005 if (unlikely(s->req.buf->i + s->req.buf->p >
10006 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010007 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010008 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010009 return 1;
10010 }
10011
Willy Tarreau9b28e032012-10-12 23:49:43 +020010012 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010013 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010014 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010015
Willy Tarreau506d0502013-07-06 13:29:24 +020010016 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10017 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010018 }
10019
Willy Tarreau506d0502013-07-06 13:29:24 +020010020 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010021 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010022 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010023
10024 /* otherwise everything's ready for the request */
10025 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010026 else {
10027 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010028 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10029 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010030 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010031 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010032 }
10033
10034 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010035 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010036 return 1;
10037}
Willy Tarreau8797c062007-05-07 00:55:35 +020010038
Willy Tarreau6c616e02014-06-25 16:56:41 +020010039/* Note: these functinos *do* modify the sample. Even in case of success, at
10040 * least the type and uint value are modified.
10041 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010042#define CHECK_HTTP_MESSAGE_FIRST() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010043 do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +020010044
Willy Tarreau24e32d82012-04-23 23:55:44 +020010045#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010046 do { int r = smp_prefetch_http(smp->px, smp->strm, smp->opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +020010047
Willy Tarreau8797c062007-05-07 00:55:35 +020010048
10049/* 1. Check on METHOD
10050 * We use the pre-parsed method if it is known, and store its number as an
10051 * integer. If it is unknown, we use the pointer and the length.
10052 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010053static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010054{
10055 int len, meth;
10056
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010057 len = strlen(text);
10058 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010059
10060 pattern->val.i = meth;
10061 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010062 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010063 pattern->len = len;
10064 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010065 else {
10066 pattern->ptr.str = NULL;
10067 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010068 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010069 return 1;
10070}
10071
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010072/* This function fetches the method of current HTTP request and stores
10073 * it in the global pattern struct as a chunk. There are two possibilities :
10074 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10075 * in <len> and <ptr> is NULL ;
10076 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10077 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010078 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010079 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010080static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010081smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010082{
10083 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010084 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010085
Willy Tarreau24e32d82012-04-23 23:55:44 +020010086 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010087
Willy Tarreau8797c062007-05-07 00:55:35 +020010088 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010089 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010090 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010091 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010092 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10093 /* ensure the indexes are not affected */
10094 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010095 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010096 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10097 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010098 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010099 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010100 return 1;
10101}
10102
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010103/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010104static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010105{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010106 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010107 struct pattern_list *lst;
10108 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010109
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010110 list_for_each_entry(lst, &expr->patterns, list) {
10111 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010112
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010113 /* well-known method */
10114 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010115 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010116 return pattern;
10117 else
10118 continue;
10119 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010120
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010121 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010122 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010123 continue;
10124
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010125 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010126 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10127 (!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 +010010128 return pattern;
10129 }
10130 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010131}
10132
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010133static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010134smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010135{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010136 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010137 char *ptr;
10138 int len;
10139
Willy Tarreauc0239e02012-04-16 14:42:55 +020010140 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010141
Willy Tarreau8797c062007-05-07 00:55:35 +020010142 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010143 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010144
10145 while ((len-- > 0) && (*ptr++ != '/'));
10146 if (len <= 0)
10147 return 0;
10148
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010149 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010150 smp->data.u.str.str = ptr;
10151 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010152
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010153 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010154 return 1;
10155}
10156
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010157static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010158smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010159{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010160 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010161 char *ptr;
10162 int len;
10163
Willy Tarreauc0239e02012-04-16 14:42:55 +020010164 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010165
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010166 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010167 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10168 return 0;
10169
Willy Tarreau8797c062007-05-07 00:55:35 +020010170 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010171 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010172
10173 while ((len-- > 0) && (*ptr++ != '/'));
10174 if (len <= 0)
10175 return 0;
10176
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010177 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010178 smp->data.u.str.str = ptr;
10179 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010180
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010181 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010182 return 1;
10183}
10184
10185/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010186static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010187smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010188{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010189 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010190 char *ptr;
10191 int len;
10192
Willy Tarreauc0239e02012-04-16 14:42:55 +020010193 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010194
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010195 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010196 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10197 return 0;
10198
Willy Tarreau8797c062007-05-07 00:55:35 +020010199 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010200 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010201
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010202 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010203 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010204 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010205 return 1;
10206}
10207
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010208/* returns the longest available part 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(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010213{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010214 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010215 struct http_msg *msg;
10216 unsigned long len;
10217 unsigned long block1;
10218 char *body;
10219 struct chunk *temp;
10220
10221 CHECK_HTTP_MESSAGE_FIRST();
10222
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010223 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010224 msg = &txn->req;
10225 else
10226 msg = &txn->rsp;
10227
10228 len = http_body_bytes(msg);
10229 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10230
10231 block1 = len;
10232 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10233 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10234
10235 if (block1 == len) {
10236 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010237 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010238 smp->data.u.str.str = body;
10239 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010240 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10241 }
10242 else {
10243 /* buffer is wrapped, we need to defragment it */
10244 temp = get_trash_chunk();
10245 memcpy(temp->str, body, block1);
10246 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010247 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010248 smp->data.u.str.str = temp->str;
10249 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010250 smp->flags = SMP_F_VOL_TEST;
10251 }
10252 return 1;
10253}
10254
10255
10256/* returns the available length of the body. This requires that the body
10257 * has been waited for using http-buffer-request.
10258 */
10259static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010260smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010261{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010262 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010263 struct http_msg *msg;
10264
10265 CHECK_HTTP_MESSAGE_FIRST();
10266
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010267 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010268 msg = &txn->req;
10269 else
10270 msg = &txn->rsp;
10271
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010272 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010273 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010274
10275 smp->flags = SMP_F_VOL_TEST;
10276 return 1;
10277}
10278
10279
10280/* returns the advertised length of the body, or the advertised size of the
10281 * chunks available in the buffer. This requires that the body has been waited
10282 * for using http-buffer-request.
10283 */
10284static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010285smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010286{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010287 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010288 struct http_msg *msg;
10289
10290 CHECK_HTTP_MESSAGE_FIRST();
10291
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010292 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010293 msg = &txn->req;
10294 else
10295 msg = &txn->rsp;
10296
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010297 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010298 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010299
10300 smp->flags = SMP_F_VOL_TEST;
10301 return 1;
10302}
10303
10304
Willy Tarreau8797c062007-05-07 00:55:35 +020010305/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010306static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010307smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010308{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010309 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010310
Willy Tarreauc0239e02012-04-16 14:42:55 +020010311 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010312
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010313 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010314 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010315 smp->data.u.str.len = txn->req.sl.rq.u_l;
10316 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010317 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010318 return 1;
10319}
10320
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010321static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010322smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010323{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010324 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010325 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010326
Willy Tarreauc0239e02012-04-16 14:42:55 +020010327 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010328
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010329 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010330 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 +020010331 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010332 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010333
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010334 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010335 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010336 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010337 return 1;
10338}
10339
10340static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010341smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010342{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010343 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010344 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010345
Willy Tarreauc0239e02012-04-16 14:42:55 +020010346 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010347
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010348 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010349 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 +020010350 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10351 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010352
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010353 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010354 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010355 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010356 return 1;
10357}
10358
Willy Tarreau185b5c42012-04-26 15:11:51 +020010359/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10360 * Accepts an optional argument of type string containing the header field name,
10361 * and an optional argument of type signed or unsigned integer to request an
10362 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010363 * headers are considered from the first one. It does not stop on commas and
10364 * returns full lines instead (useful for User-Agent or Date for example).
10365 */
10366static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010367smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010368{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010369 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010370 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010371 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010372 int occ = 0;
10373 const char *name_str = NULL;
10374 int name_len = 0;
10375
10376 if (!ctx) {
10377 /* first call */
10378 ctx = &static_hdr_ctx;
10379 ctx->idx = 0;
10380 smp->ctx.a[0] = ctx;
10381 }
10382
10383 if (args) {
10384 if (args[0].type != ARGT_STR)
10385 return 0;
10386 name_str = args[0].data.str.str;
10387 name_len = args[0].data.str.len;
10388
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010389 if (args[1].type == ARGT_SINT)
10390 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010391 }
10392
10393 CHECK_HTTP_MESSAGE_FIRST();
10394
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010395 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010396 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 +020010397
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010398 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10399 /* search for header from the beginning */
10400 ctx->idx = 0;
10401
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010402 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010403 /* no explicit occurrence and single fetch => last header by default */
10404 occ = -1;
10405
10406 if (!occ)
10407 /* prepare to report multiple occurrences for ACL fetches */
10408 smp->flags |= SMP_F_NOT_LAST;
10409
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010410 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010411 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010412 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 +020010413 return 1;
10414
10415 smp->flags &= ~SMP_F_NOT_LAST;
10416 return 0;
10417}
10418
10419/* 6. Check on HTTP header count. The number of occurrences is returned.
10420 * Accepts exactly 1 argument of type string. It does not stop on commas and
10421 * returns full lines instead (useful for User-Agent or Date for example).
10422 */
10423static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010424smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010425{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010426 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010427 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010428 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010429 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010430 const char *name = NULL;
10431 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010432
Willy Tarreau601a4d12015-04-01 19:16:09 +020010433 if (args && args->type == ARGT_STR) {
10434 name = args->data.str.str;
10435 len = args->data.str.len;
10436 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010437
10438 CHECK_HTTP_MESSAGE_FIRST();
10439
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010440 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010441 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 +020010442
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010443 ctx.idx = 0;
10444 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010445 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010446 cnt++;
10447
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010448 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010449 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010450 smp->flags = SMP_F_VOL_HDR;
10451 return 1;
10452}
10453
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010454static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010455smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010456{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010457 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010458 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010459 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010460 struct chunk *temp;
10461 char del = ',';
10462
10463 if (args && args->type == ARGT_STR)
10464 del = *args[0].data.str.str;
10465
10466 CHECK_HTTP_MESSAGE_FIRST();
10467
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010468 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010469 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 +020010470
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010471 temp = get_trash_chunk();
10472
10473 ctx.idx = 0;
10474 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10475 if (temp->len)
10476 temp->str[temp->len++] = del;
10477 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10478 temp->len += ctx.del;
10479 }
10480
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010481 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010482 smp->data.u.str.str = temp->str;
10483 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010484 smp->flags = SMP_F_VOL_HDR;
10485 return 1;
10486}
10487
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010488/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10489 * Accepts an optional argument of type string containing the header field name,
10490 * and an optional argument of type signed or unsigned integer to request an
10491 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010492 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010493 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010494static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010495smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010496{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010497 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010498 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010499 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010500 int occ = 0;
10501 const char *name_str = NULL;
10502 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010503
Willy Tarreaua890d072013-04-02 12:01:06 +020010504 if (!ctx) {
10505 /* first call */
10506 ctx = &static_hdr_ctx;
10507 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010508 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010509 }
10510
Willy Tarreau185b5c42012-04-26 15:11:51 +020010511 if (args) {
10512 if (args[0].type != ARGT_STR)
10513 return 0;
10514 name_str = args[0].data.str.str;
10515 name_len = args[0].data.str.len;
10516
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010517 if (args[1].type == ARGT_SINT)
10518 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010519 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010520
Willy Tarreaue333ec92012-04-16 16:26:40 +020010521 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010522
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010523 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010524 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 +020010525
Willy Tarreau185b5c42012-04-26 15:11:51 +020010526 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010527 /* search for header from the beginning */
10528 ctx->idx = 0;
10529
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010530 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010531 /* no explicit occurrence and single fetch => last header by default */
10532 occ = -1;
10533
10534 if (!occ)
10535 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010536 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010537
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010538 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010539 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010540 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 +020010541 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010542
Willy Tarreau37406352012-04-23 16:16:37 +020010543 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010544 return 0;
10545}
10546
Willy Tarreauc11416f2007-06-17 16:58:38 +020010547/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010548 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010549 */
10550static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010551smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010552{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010553 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010554 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010555 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010556 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010557 const char *name = NULL;
10558 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010559
Willy Tarreau601a4d12015-04-01 19:16:09 +020010560 if (args && args->type == ARGT_STR) {
10561 name = args->data.str.str;
10562 len = args->data.str.len;
10563 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010564
Willy Tarreaue333ec92012-04-16 16:26:40 +020010565 CHECK_HTTP_MESSAGE_FIRST();
10566
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010567 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010568 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 +020010569
Willy Tarreau33a7e692007-06-10 19:45:56 +020010570 ctx.idx = 0;
10571 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010572 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010573 cnt++;
10574
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010575 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010576 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010577 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010578 return 1;
10579}
10580
Willy Tarreau185b5c42012-04-26 15:11:51 +020010581/* Fetch an HTTP header's integer value. The integer value is returned. It
10582 * takes a mandatory argument of type string and an optional one of type int
10583 * to designate a specific occurrence. It returns an unsigned integer, which
10584 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010585 */
10586static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010587smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010588{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010589 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010590
Willy Tarreauf853c462012-04-23 18:53:56 +020010591 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010592 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010593 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010594 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010595
Willy Tarreaud53e2422012-04-16 17:21:11 +020010596 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010597}
10598
Cyril Bonté69fa9922012-10-25 00:01:06 +020010599/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10600 * and an optional one of type int to designate a specific occurrence.
10601 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010602 */
10603static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010604smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010605{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010606 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010607
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010608 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010609 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010610 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010611 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010612 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010613 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010614 if (smp->data.u.str.len < temp->size - 1) {
10615 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10616 temp->str[smp->data.u.str.len] = '\0';
10617 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010618 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010619 break;
10620 }
10621 }
10622 }
10623
Willy Tarreaud53e2422012-04-16 17:21:11 +020010624 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010625 if (!(smp->flags & SMP_F_NOT_LAST))
10626 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010627 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010628 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010629}
10630
Willy Tarreau737b0c12007-06-10 21:28:46 +020010631/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10632 * the first '/' after the possible hostname, and ends before the possible '?'.
10633 */
10634static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010635smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010636{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010637 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010638 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010639
Willy Tarreauc0239e02012-04-16 14:42:55 +020010640 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010641
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010642 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010643 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010644 ptr = http_get_path(txn);
10645 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010646 return 0;
10647
10648 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010649 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010650 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010651
10652 while (ptr < end && *ptr != '?')
10653 ptr++;
10654
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010655 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010656 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010657 return 1;
10658}
10659
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010660/* This produces a concatenation of the first occurrence of the Host header
10661 * followed by the path component if it begins with a slash ('/'). This means
10662 * that '*' will not be added, resulting in exactly the first Host entry.
10663 * If no Host header is found, then the path is returned as-is. The returned
10664 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010665 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010666 */
10667static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010668smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010669{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010670 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010671 char *ptr, *end, *beg;
10672 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010673 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010674
10675 CHECK_HTTP_MESSAGE_FIRST();
10676
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010677 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010678 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010679 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010680 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010681
10682 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010683 temp = get_trash_chunk();
10684 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010685 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010686 smp->data.u.str.str = temp->str;
10687 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010688
10689 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010690 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010691 beg = http_get_path(txn);
10692 if (!beg)
10693 beg = end;
10694
10695 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10696
10697 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010698 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10699 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010700 }
10701
10702 smp->flags = SMP_F_VOL_1ST;
10703 return 1;
10704}
10705
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010706/* This produces a 32-bit hash of the concatenation of the first occurrence of
10707 * the Host header followed by the path component if it begins with a slash ('/').
10708 * This means that '*' will not be added, resulting in exactly the first Host
10709 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010710 * is hashed using the path hash followed by a full avalanche hash and provides a
10711 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010712 * high-traffic sites without having to store whole paths.
10713 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010714int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010715smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010716{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010717 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010718 struct hdr_ctx ctx;
10719 unsigned int hash = 0;
10720 char *ptr, *beg, *end;
10721 int len;
10722
10723 CHECK_HTTP_MESSAGE_FIRST();
10724
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010725 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010726 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010727 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010728 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10729 ptr = ctx.line + ctx.val;
10730 len = ctx.vlen;
10731 while (len--)
10732 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10733 }
10734
10735 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010736 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010737 beg = http_get_path(txn);
10738 if (!beg)
10739 beg = end;
10740
10741 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10742
10743 if (beg < ptr && *beg == '/') {
10744 while (beg < ptr)
10745 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10746 }
10747 hash = full_hash(hash);
10748
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010749 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010750 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010751 smp->flags = SMP_F_VOL_1ST;
10752 return 1;
10753}
10754
Willy Tarreau4a550602012-12-09 14:53:32 +010010755/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010756 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10757 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10758 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010759 * that in environments where IPv6 is insignificant, truncating the output to
10760 * 8 bytes would still work.
10761 */
10762static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010763smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010764{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010765 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010766 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010767
10768 if (!cli_conn)
10769 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010770
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010771 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010772 return 0;
10773
Willy Tarreau47ca5452012-12-23 20:22:19 +010010774 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010775 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010776 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010777
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010778 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010779 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010780 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010781 temp->len += 4;
10782 break;
10783 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010784 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010785 temp->len += 16;
10786 break;
10787 default:
10788 return 0;
10789 }
10790
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010791 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010792 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010793 return 1;
10794}
10795
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010796/* Extracts the query string, which comes after the question mark '?'. If no
10797 * question mark is found, nothing is returned. Otherwise it returns a sample
10798 * of type string carrying the whole query string.
10799 */
10800static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010801smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010802{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010803 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010804 char *ptr, *end;
10805
10806 CHECK_HTTP_MESSAGE_FIRST();
10807
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010808 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010809 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10810 end = ptr + txn->req.sl.rq.u_l;
10811
10812 /* look up the '?' */
10813 do {
10814 if (ptr == end)
10815 return 0;
10816 } while (*ptr++ != '?');
10817
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010818 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010819 smp->data.u.str.str = ptr;
10820 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010821 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10822 return 1;
10823}
10824
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010825static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010826smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010827{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010828 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10829 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10830 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010831
Willy Tarreau24e32d82012-04-23 23:55:44 +020010832 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010833
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010834 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010835 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010836 return 1;
10837}
10838
Willy Tarreau7f18e522010-10-22 20:04:13 +020010839/* return a valid test if the current request is the first one on the connection */
10840static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010841smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010842{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010843 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010844 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010845 return 1;
10846}
10847
Willy Tarreau34db1082012-04-19 17:16:54 +020010848/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010849static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010850smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010851{
10852
Willy Tarreau24e32d82012-04-23 23:55:44 +020010853 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010854 return 0;
10855
Willy Tarreauc0239e02012-04-16 14:42:55 +020010856 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010857
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010858 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010859 return 0;
10860
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010861 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010862 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010863 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010864 return 1;
10865}
Willy Tarreau8797c062007-05-07 00:55:35 +020010866
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010867/* Accepts exactly 1 argument of type userlist */
10868static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010869smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010870{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010871 if (!args || args->type != ARGT_USR)
10872 return 0;
10873
10874 CHECK_HTTP_MESSAGE_FIRST();
10875
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010876 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010877 return 0;
10878
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010879 /* if the user does not belong to the userlist or has a wrong password,
10880 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010881 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010882 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010883 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10884 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010885 return 0;
10886
10887 /* pat_match_auth() will need the user list */
10888 smp->ctx.a[0] = args->data.usr;
10889
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010890 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010891 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010892 smp->data.u.str.str = smp->strm->txn->auth.user;
10893 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010894
10895 return 1;
10896}
10897
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010898/* Try to find the next occurrence of a cookie name in a cookie header value.
10899 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10900 * the cookie value is returned into *value and *value_l, and the function
10901 * returns a pointer to the next pointer to search from if the value was found.
10902 * Otherwise if the cookie was not found, NULL is returned and neither value
10903 * nor value_l are touched. The input <hdr> string should first point to the
10904 * header's value, and the <hdr_end> pointer must point to the first character
10905 * not part of the value. <list> must be non-zero if value may represent a list
10906 * of values (cookie headers). This makes it faster to abort parsing when no
10907 * list is expected.
10908 */
10909static char *
10910extract_cookie_value(char *hdr, const char *hdr_end,
10911 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010912 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010913{
10914 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10915 char *next;
10916
10917 /* we search at least a cookie name followed by an equal, and more
10918 * generally something like this :
10919 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10920 */
10921 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10922 /* Iterate through all cookies on this line */
10923
10924 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10925 att_beg++;
10926
10927 /* find att_end : this is the first character after the last non
10928 * space before the equal. It may be equal to hdr_end.
10929 */
10930 equal = att_end = att_beg;
10931
10932 while (equal < hdr_end) {
10933 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10934 break;
10935 if (http_is_spht[(unsigned char)*equal++])
10936 continue;
10937 att_end = equal;
10938 }
10939
10940 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10941 * is between <att_beg> and <equal>, both may be identical.
10942 */
10943
10944 /* look for end of cookie if there is an equal sign */
10945 if (equal < hdr_end && *equal == '=') {
10946 /* look for the beginning of the value */
10947 val_beg = equal + 1;
10948 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10949 val_beg++;
10950
10951 /* find the end of the value, respecting quotes */
10952 next = find_cookie_value_end(val_beg, hdr_end);
10953
10954 /* make val_end point to the first white space or delimitor after the value */
10955 val_end = next;
10956 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10957 val_end--;
10958 } else {
10959 val_beg = val_end = next = equal;
10960 }
10961
10962 /* We have nothing to do with attributes beginning with '$'. However,
10963 * they will automatically be removed if a header before them is removed,
10964 * since they're supposed to be linked together.
10965 */
10966 if (*att_beg == '$')
10967 continue;
10968
10969 /* Ignore cookies with no equal sign */
10970 if (equal == next)
10971 continue;
10972
10973 /* Now we have the cookie name between att_beg and att_end, and
10974 * its value between val_beg and val_end.
10975 */
10976
10977 if (att_end - att_beg == cookie_name_l &&
10978 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10979 /* let's return this value and indicate where to go on from */
10980 *value = val_beg;
10981 *value_l = val_end - val_beg;
10982 return next + 1;
10983 }
10984
10985 /* Set-Cookie headers only have the name in the first attr=value part */
10986 if (!list)
10987 break;
10988 }
10989
10990 return NULL;
10991}
10992
William Lallemanda43ba4e2014-01-28 18:14:25 +010010993/* Fetch a captured HTTP request header. The index is the position of
10994 * the "capture" option in the configuration file
10995 */
10996static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010997smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010998{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010999 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011000 int idx;
11001
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011002 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011003 return 0;
11004
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011005 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011006
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011007 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 +010011008 return 0;
11009
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011010 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011011 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011012 smp->data.u.str.str = smp->strm->req_cap[idx];
11013 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011014
11015 return 1;
11016}
11017
11018/* Fetch a captured HTTP response header. The index is the position of
11019 * the "capture" option in the configuration file
11020 */
11021static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011022smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011023{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011024 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011025 int idx;
11026
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011027 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011028 return 0;
11029
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011030 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011031
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011032 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 +010011033 return 0;
11034
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011035 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011036 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011037 smp->data.u.str.str = smp->strm->res_cap[idx];
11038 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011039
11040 return 1;
11041}
11042
William Lallemand65ad6e12014-01-31 15:08:02 +010011043/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11044static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011045smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011046{
11047 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011048 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011049 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011050
Willy Tarreau15e91e12015-04-04 00:52:09 +020011051 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011052 return 0;
11053
William Lallemand96a77852014-02-05 00:30:02 +010011054 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011055
William Lallemand96a77852014-02-05 00:30:02 +010011056 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11057 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011058
William Lallemand96a77852014-02-05 00:30:02 +010011059 temp = get_trash_chunk();
11060 temp->str = txn->uri;
11061 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011062 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011063 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011064 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011065
11066 return 1;
11067
11068}
11069
11070/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11071static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011072smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011073{
11074 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011075 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011076 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011077
Willy Tarreau15e91e12015-04-04 00:52:09 +020011078 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011079 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011080
William Lallemand65ad6e12014-01-31 15:08:02 +010011081 ptr = txn->uri;
11082
11083 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11084 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011085
William Lallemand65ad6e12014-01-31 15:08:02 +010011086 if (!*ptr)
11087 return 0;
11088
11089 ptr++; /* skip the space */
11090
11091 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011092 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011093 if (!ptr)
11094 return 0;
11095 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11096 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011097
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011098 smp->data.u.str = *temp;
11099 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011100 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011101 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011102
11103 return 1;
11104}
11105
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011106/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11107 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11108 */
11109static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011110smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011111{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011112 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011113
Willy Tarreau15e91e12015-04-04 00:52:09 +020011114 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011115 return 0;
11116
11117 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011118 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011119 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011120 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011121
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011122 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011123 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011124 smp->flags = SMP_F_CONST;
11125 return 1;
11126
11127}
11128
11129/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11130 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11131 */
11132static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011133smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011134{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011135 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011136
Willy Tarreau15e91e12015-04-04 00:52:09 +020011137 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011138 return 0;
11139
11140 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011141 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011142 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011143 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011144
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011145 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011146 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011147 smp->flags = SMP_F_CONST;
11148 return 1;
11149
11150}
11151
William Lallemand65ad6e12014-01-31 15:08:02 +010011152
Willy Tarreaue333ec92012-04-16 16:26:40 +020011153/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011154 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011155 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011156 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011157 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011158 * Accepts exactly 1 argument of type string. If the input options indicate
11159 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011160 * The returned sample is of type CSTR. Can be used to parse cookies in other
11161 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011162 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011163int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011164{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011165 struct http_txn *txn;
11166 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011167 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011168 const struct http_msg *msg;
11169 const char *hdr_name;
11170 int hdr_name_len;
11171 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011172 int occ = 0;
11173 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011174
Willy Tarreau24e32d82012-04-23 23:55:44 +020011175 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011176 return 0;
11177
Willy Tarreaua890d072013-04-02 12:01:06 +020011178 if (!ctx) {
11179 /* first call */
11180 ctx = &static_hdr_ctx;
11181 ctx->idx = 0;
11182 smp->ctx.a[2] = ctx;
11183 }
11184
Willy Tarreaue333ec92012-04-16 16:26:40 +020011185 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011186
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011187 txn = smp->strm->txn;
11188 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011189
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011190 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011191 msg = &txn->req;
11192 hdr_name = "Cookie";
11193 hdr_name_len = 6;
11194 } else {
11195 msg = &txn->rsp;
11196 hdr_name = "Set-Cookie";
11197 hdr_name_len = 10;
11198 }
11199
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011200 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011201 /* no explicit occurrence and single fetch => last cookie by default */
11202 occ = -1;
11203
11204 /* OK so basically here, either we want only one value and it's the
11205 * last one, or we want to iterate over all of them and we fetch the
11206 * next one.
11207 */
11208
Willy Tarreau9b28e032012-10-12 23:49:43 +020011209 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011210 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011211 /* search for the header from the beginning, we must first initialize
11212 * the search parameters.
11213 */
Willy Tarreau37406352012-04-23 16:16:37 +020011214 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011215 ctx->idx = 0;
11216 }
11217
Willy Tarreau28376d62012-04-26 21:26:10 +020011218 smp->flags |= SMP_F_VOL_HDR;
11219
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011220 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011221 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11222 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011223 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11224 goto out;
11225
Willy Tarreau24e32d82012-04-23 23:55:44 +020011226 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011227 continue;
11228
Willy Tarreau37406352012-04-23 16:16:37 +020011229 smp->ctx.a[0] = ctx->line + ctx->val;
11230 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011231 }
11232
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011233 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011234 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011235 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011236 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011237 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011238 &smp->data.u.str.str,
11239 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011240 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011241 found = 1;
11242 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011243 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011244 smp->flags |= SMP_F_NOT_LAST;
11245 return 1;
11246 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011247 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011248 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011249 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011250 /* all cookie headers and values were scanned. If we're looking for the
11251 * last occurrence, we may return it now.
11252 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011253 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011254 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011255 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011256}
11257
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011258/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011259 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011260 * multiple cookies may be parsed on the same line. The returned sample is of
11261 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011262 */
11263static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011264smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011265{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011266 struct http_txn *txn;
11267 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011268 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011269 const struct http_msg *msg;
11270 const char *hdr_name;
11271 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011272 int cnt;
11273 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011274 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011275
Willy Tarreau24e32d82012-04-23 23:55:44 +020011276 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011277 return 0;
11278
Willy Tarreaue333ec92012-04-16 16:26:40 +020011279 CHECK_HTTP_MESSAGE_FIRST();
11280
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011281 txn = smp->strm->txn;
11282 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011283
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011284 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011285 msg = &txn->req;
11286 hdr_name = "Cookie";
11287 hdr_name_len = 6;
11288 } else {
11289 msg = &txn->rsp;
11290 hdr_name = "Set-Cookie";
11291 hdr_name_len = 10;
11292 }
11293
Willy Tarreau9b28e032012-10-12 23:49:43 +020011294 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011295 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011296 ctx.idx = 0;
11297 cnt = 0;
11298
11299 while (1) {
11300 /* Note: val_beg == NULL every time we need to fetch a new header */
11301 if (!val_beg) {
11302 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11303 break;
11304
Willy Tarreau24e32d82012-04-23 23:55:44 +020011305 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011306 continue;
11307
11308 val_beg = ctx.line + ctx.val;
11309 val_end = val_beg + ctx.vlen;
11310 }
11311
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011312 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011313 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011314 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011315 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011316 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011317 &smp->data.u.str.str,
11318 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011319 cnt++;
11320 }
11321 }
11322
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011323 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011324 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011325 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011326 return 1;
11327}
11328
Willy Tarreau51539362012-05-08 12:46:28 +020011329/* Fetch an cookie's integer value. The integer value is returned. It
11330 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11331 */
11332static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011333smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011334{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011335 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011336
11337 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011338 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011339 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011340 }
11341
11342 return ret;
11343}
11344
Willy Tarreau8797c062007-05-07 00:55:35 +020011345/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011346/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011347/************************************************************************/
11348
David Cournapeau16023ee2010-12-23 20:55:41 +090011349/*
11350 * Given a path string and its length, find the position of beginning of the
11351 * query string. Returns NULL if no query string is found in the path.
11352 *
11353 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11354 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011355 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011356 */
bedis4c75cca2012-10-05 08:38:24 +020011357static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011358{
11359 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011360
bedis4c75cca2012-10-05 08:38:24 +020011361 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011362 return p ? p + 1 : NULL;
11363}
11364
bedis4c75cca2012-10-05 08:38:24 +020011365static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011366{
bedis4c75cca2012-10-05 08:38:24 +020011367 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011368}
11369
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011370/* after increasing a pointer value, it can exceed the first buffer
11371 * size. This function transform the value of <ptr> according with
11372 * the expected position. <chunks> is an array of the one or two
11373 * avalaible chunks. The first value is the start of the first chunk,
11374 * the second value if the end+1 of the first chunks. The third value
11375 * is NULL or the start of the second chunk and the fourth value is
11376 * the end+1 of the second chunk. The function returns 1 if does a
11377 * wrap, else returns 0.
11378 */
11379static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11380{
11381 if (*ptr < chunks[1])
11382 return 0;
11383 if (!chunks[2])
11384 return 0;
11385 *ptr = chunks[2] + ( *ptr - chunks[1] );
11386 return 1;
11387}
11388
David Cournapeau16023ee2010-12-23 20:55:41 +090011389/*
11390 * Given a url parameter, find the starting position of the first occurence,
11391 * or NULL if the parameter is not found.
11392 *
11393 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11394 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011395 *
11396 * Warning:this function returns a pointer that can be point to the first chunk
11397 * or the second chunk. The caller must be check the position before using the
11398 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011399 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011400static const char *
11401find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011402 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011403 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011404{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011405 const char *pos, *last, *equal;
11406 const char **bufs = chunks;
11407 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011408
David Cournapeau16023ee2010-12-23 20:55:41 +090011409
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011410 pos = bufs[0];
11411 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011412 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011413 /* Check the equal. */
11414 equal = pos + url_param_name_l;
11415 if (fix_pointer_if_wrap(chunks, &equal)) {
11416 if (equal >= chunks[3])
11417 return NULL;
11418 } else {
11419 if (equal >= chunks[1])
11420 return NULL;
11421 }
11422 if (*equal == '=') {
11423 if (pos + url_param_name_l > last) {
11424 /* process wrap case, we detect a wrap. In this case, the
11425 * comparison is performed in two parts.
11426 */
11427
11428 /* This is the end, we dont have any other chunk. */
11429 if (bufs != chunks || !bufs[2])
11430 return NULL;
11431
11432 /* Compute the length of each part of the comparison. */
11433 l1 = last - pos;
11434 l2 = url_param_name_l - l1;
11435
11436 /* The second buffer is too short to contain the compared string. */
11437 if (bufs[2] + l2 > bufs[3])
11438 return NULL;
11439
11440 if (memcmp(pos, url_param_name, l1) == 0 &&
11441 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11442 return pos;
11443
11444 /* Perform wrapping and jump the string who fail the comparison. */
11445 bufs += 2;
11446 pos = bufs[0] + l2;
11447 last = bufs[1];
11448
11449 } else {
11450 /* process a simple comparison. */
11451 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11452 return pos; }
11453 pos += url_param_name_l + 1;
11454 if (fix_pointer_if_wrap(chunks, &pos))
11455 last = bufs[2];
11456 }
11457 }
11458
11459 while (1) {
11460 /* Look for the next delimiter. */
11461 while (pos <= last && !is_param_delimiter(*pos, delim))
11462 pos++;
11463 if (pos < last)
11464 break;
11465 /* process buffer wrapping. */
11466 if (bufs != chunks || !bufs[2])
11467 return NULL;
11468 bufs += 2;
11469 pos = bufs[0];
11470 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011471 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011472 pos++;
11473 }
11474 return NULL;
11475}
11476
11477/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011478 * Given a url parameter name and a query string, returns its value and size
11479 * into *value and *value_l respectively, and returns non-zero. An empty
11480 * url_param_name matches the first available parameter. If the parameter is
11481 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011482 */
11483static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011484find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011485 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011486 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011487{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011488 const char *arg_start, *qs_end;
11489 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011490
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011491 arg_start = chunks[0];
11492 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011493 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011494 /* Looks for an argument name. */
11495 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011496 url_param_name, url_param_name_l,
11497 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011498 /* Check for wrapping. */
11499 if (arg_start > qs_end)
11500 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011501 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011502 if (!arg_start)
11503 return 0;
11504
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011505 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011506 while (1) {
11507 /* looks for the first argument. */
11508 value_start = memchr(arg_start, '=', qs_end - arg_start);
11509 if (!value_start) {
11510
11511 /* Check for wrapping. */
11512 if (arg_start >= chunks[0] &&
11513 arg_start <= chunks[1] &&
11514 chunks[2]) {
11515 arg_start = chunks[2];
11516 qs_end = chunks[3];
11517 continue;
11518 }
11519 return 0;
11520 }
11521 break;
11522 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011523 value_start++;
11524 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011525 else {
11526 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011527 value_start = arg_start + url_param_name_l + 1;
11528
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011529 /* Check for pointer wrapping. */
11530 if (fix_pointer_if_wrap(chunks, &value_start)) {
11531 /* Update the end pointer. */
11532 qs_end = chunks[3];
11533
11534 /* Check for overflow. */
11535 if (value_start > qs_end)
11536 return 0;
11537 }
11538 }
11539
David Cournapeau16023ee2010-12-23 20:55:41 +090011540 value_end = value_start;
11541
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011542 while (1) {
11543 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11544 value_end++;
11545 if (value_end < qs_end)
11546 break;
11547 /* process buffer wrapping. */
11548 if (value_end >= chunks[0] &&
11549 value_end <= chunks[1] &&
11550 chunks[2]) {
11551 value_end = chunks[2];
11552 qs_end = chunks[3];
11553 continue;
11554 }
11555 break;
11556 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011557
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011558 *vstart = value_start;
11559 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011560 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011561}
11562
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011563/* This scans a URL-encoded query string. It takes an optionally wrapping
11564 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11565 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11566 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011567 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011568static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011569smp_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 +090011570{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011571 const char *vstart, *vend;
11572 struct chunk *temp;
11573 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011574
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011575 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011576 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011577 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011578 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011579 return 0;
11580
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011581 /* Create sample. If the value is contiguous, return the pointer as CONST,
11582 * if the value is wrapped, copy-it in a buffer.
11583 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011584 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011585 if (chunks[2] &&
11586 vstart >= chunks[0] && vstart <= chunks[1] &&
11587 vend >= chunks[2] && vend <= chunks[3]) {
11588 /* Wrapped case. */
11589 temp = get_trash_chunk();
11590 memcpy(temp->str, vstart, chunks[1] - vstart);
11591 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011592 smp->data.u.str.str = temp->str;
11593 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011594 } else {
11595 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011596 smp->data.u.str.str = (char *)vstart;
11597 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011598 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11599 }
11600
11601 /* Update context, check wrapping. */
11602 chunks[0] = vend;
11603 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11604 chunks[1] = chunks[3];
11605 chunks[2] = NULL;
11606 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011607
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011608 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011609 smp->flags |= SMP_F_NOT_LAST;
11610
David Cournapeau16023ee2010-12-23 20:55:41 +090011611 return 1;
11612}
11613
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011614/* This function iterates over each parameter of the query string. It uses
11615 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011616 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11617 * An optional parameter name is passed in args[0], otherwise any parameter is
11618 * considered. It supports an optional delimiter argument for the beginning of
11619 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011620 */
11621static int
11622smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11623{
11624 struct http_msg *msg;
11625 char delim = '?';
11626 const char *name;
11627 int name_len;
11628
Dragan Dosen26f77e52015-05-25 10:02:11 +020011629 if (!args ||
11630 (args[0].type && args[0].type != ARGT_STR) ||
11631 (args[1].type && args[1].type != ARGT_STR))
11632 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011633
Dragan Dosen26f77e52015-05-25 10:02:11 +020011634 name = "";
11635 name_len = 0;
11636 if (args->type == ARGT_STR) {
11637 name = args->data.str.str;
11638 name_len = args->data.str.len;
11639 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011640
Dragan Dosen26f77e52015-05-25 10:02:11 +020011641 if (args[1].type)
11642 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011643
Dragan Dosen26f77e52015-05-25 10:02:11 +020011644 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011645 CHECK_HTTP_MESSAGE_FIRST();
11646
11647 msg = &smp->strm->txn->req;
11648
11649 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11650 msg->sl.rq.u_l, delim);
11651 if (!smp->ctx.a[0])
11652 return 0;
11653
11654 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011655
11656 /* Assume that the context is filled with NULL pointer
11657 * before the first call.
11658 * smp->ctx.a[2] = NULL;
11659 * smp->ctx.a[3] = NULL;
11660 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011661 }
11662
11663 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11664}
11665
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011666/* This function iterates over each parameter of the body. This requires
11667 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011668 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11669 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11670 * optional second part if the body wraps at the end of the buffer. An optional
11671 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011672 */
11673static int
11674smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11675{
11676 struct http_txn *txn = smp->strm->txn;
11677 struct http_msg *msg;
11678 unsigned long len;
11679 unsigned long block1;
11680 char *body;
11681 const char *name;
11682 int name_len;
11683
11684 if (!args || (args[0].type && args[0].type != ARGT_STR))
11685 return 0;
11686
11687 name = "";
11688 name_len = 0;
11689 if (args[0].type == ARGT_STR) {
11690 name = args[0].data.str.str;
11691 name_len = args[0].data.str.len;
11692 }
11693
11694 if (!smp->ctx.a[0]) { // first call, find the query string
11695 CHECK_HTTP_MESSAGE_FIRST();
11696
11697 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11698 msg = &txn->req;
11699 else
11700 msg = &txn->rsp;
11701
11702 len = http_body_bytes(msg);
11703 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11704
11705 block1 = len;
11706 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11707 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11708
11709 if (block1 == len) {
11710 /* buffer is not wrapped (or empty) */
11711 smp->ctx.a[0] = body;
11712 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011713
11714 /* Assume that the context is filled with NULL pointer
11715 * before the first call.
11716 * smp->ctx.a[2] = NULL;
11717 * smp->ctx.a[3] = NULL;
11718 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011719 }
11720 else {
11721 /* buffer is wrapped, we need to defragment it */
11722 smp->ctx.a[0] = body;
11723 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011724 smp->ctx.a[2] = msg->chn->buf->data;
11725 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011726 }
11727 }
11728 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11729}
11730
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011731/* Return the signed integer value for the specified url parameter (see url_param
11732 * above).
11733 */
11734static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011735smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011736{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011737 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011738
11739 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011740 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011741 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011742 }
11743
11744 return ret;
11745}
11746
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011747/* This produces a 32-bit hash of the concatenation of the first occurrence of
11748 * the Host header followed by the path component if it begins with a slash ('/').
11749 * This means that '*' will not be added, resulting in exactly the first Host
11750 * entry. If no Host header is found, then the path is used. The resulting value
11751 * is hashed using the url hash followed by a full avalanche hash and provides a
11752 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11753 * high-traffic sites without having to store whole paths.
11754 * this differs from the base32 functions in that it includes the url parameters
11755 * as well as the path
11756 */
11757static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011758smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011759{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011760 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011761 struct hdr_ctx ctx;
11762 unsigned int hash = 0;
11763 char *ptr, *beg, *end;
11764 int len;
11765
11766 CHECK_HTTP_MESSAGE_FIRST();
11767
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011768 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011769 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011770 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011771 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11772 ptr = ctx.line + ctx.val;
11773 len = ctx.vlen;
11774 while (len--)
11775 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11776 }
11777
11778 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011779 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 +000011780 beg = http_get_path(txn);
11781 if (!beg)
11782 beg = end;
11783
11784 for (ptr = beg; ptr < end ; ptr++);
11785
11786 if (beg < ptr && *beg == '/') {
11787 while (beg < ptr)
11788 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11789 }
11790 hash = full_hash(hash);
11791
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011792 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011793 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011794 smp->flags = SMP_F_VOL_1ST;
11795 return 1;
11796}
11797
11798/* This concatenates the source address with the 32-bit hash of the Host and
11799 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11800 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11801 * on the source address length. The URL hash is stored before the address so
11802 * that in environments where IPv6 is insignificant, truncating the output to
11803 * 8 bytes would still work.
11804 */
11805static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011806smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011807{
11808 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011809 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011810 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011811
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011812 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011813 return 0;
11814
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011815 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011816 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011817
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011818 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011819 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11820 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011821
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011822 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011823 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011824 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011825 temp->len += 4;
11826 break;
11827 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011828 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011829 temp->len += 16;
11830 break;
11831 default:
11832 return 0;
11833 }
11834
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011835 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011836 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011837 return 1;
11838}
11839
Willy Tarreau185b5c42012-04-26 15:11:51 +020011840/* This function is used to validate the arguments passed to any "hdr" fetch
11841 * keyword. These keywords support an optional positive or negative occurrence
11842 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11843 * is assumed that the types are already the correct ones. Returns 0 on error,
11844 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11845 * error message in case of error, that the caller is responsible for freeing.
11846 * The initial location must either be freeable or NULL.
11847 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011848int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011849{
11850 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011851 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011852 return 0;
11853 }
11854 return 1;
11855}
11856
Willy Tarreau276fae92013-07-25 14:36:01 +020011857/* takes an UINT value on input supposed to represent the time since EPOCH,
11858 * adds an optional offset found in args[0] and emits a string representing
11859 * the date in RFC-1123/5322 format.
11860 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011861static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011862{
11863 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11864 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11865 struct chunk *temp;
11866 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011867 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011868 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011869
11870 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011871 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011872 curr_date += args[0].data.sint;
11873
11874 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011875 if (!tm)
11876 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011877
11878 temp = get_trash_chunk();
11879 temp->len = snprintf(temp->str, temp->size - temp->len,
11880 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11881 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11882 tm->tm_hour, tm->tm_min, tm->tm_sec);
11883
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011884 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011885 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011886 return 1;
11887}
11888
Thierry FOURNIERad903512014-04-11 17:51:01 +020011889/* Match language range with language tag. RFC2616 14.4:
11890 *
11891 * A language-range matches a language-tag if it exactly equals
11892 * the tag, or if it exactly equals a prefix of the tag such
11893 * that the first tag character following the prefix is "-".
11894 *
11895 * Return 1 if the strings match, else return 0.
11896 */
11897static inline int language_range_match(const char *range, int range_len,
11898 const char *tag, int tag_len)
11899{
11900 const char *end = range + range_len;
11901 const char *tend = tag + tag_len;
11902 while (range < end) {
11903 if (*range == '-' && tag == tend)
11904 return 1;
11905 if (*range != *tag || tag == tend)
11906 return 0;
11907 range++;
11908 tag++;
11909 }
11910 /* Return true only if the last char of the tag is matched. */
11911 return tag == tend;
11912}
11913
11914/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011915static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011916{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011917 const char *al = smp->data.u.str.str;
11918 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011919 const char *token;
11920 int toklen;
11921 int qvalue;
11922 const char *str;
11923 const char *w;
11924 int best_q = 0;
11925
11926 /* Set the constant to the sample, because the output of the
11927 * function will be peek in the constant configuration string.
11928 */
11929 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011930 smp->data.u.str.size = 0;
11931 smp->data.u.str.str = "";
11932 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011933
11934 /* Parse the accept language */
11935 while (1) {
11936
11937 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011938 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011939 al++;
11940 if (al >= end)
11941 break;
11942
11943 /* Start of the fisrt word. */
11944 token = al;
11945
11946 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011947 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011948 al++;
11949 if (al == token)
11950 goto expect_comma;
11951
11952 /* Length of the token. */
11953 toklen = al - token;
11954 qvalue = 1000;
11955
11956 /* Check if the token exists in the list. If the token not exists,
11957 * jump to the next token.
11958 */
11959 str = args[0].data.str.str;
11960 w = str;
11961 while (1) {
11962 if (*str == ';' || *str == '\0') {
11963 if (language_range_match(token, toklen, w, str-w))
11964 goto look_for_q;
11965 if (*str == '\0')
11966 goto expect_comma;
11967 w = str + 1;
11968 }
11969 str++;
11970 }
11971 goto expect_comma;
11972
11973look_for_q:
11974
11975 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011976 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011977 al++;
11978 if (al >= end)
11979 goto process_value;
11980
11981 /* If ',' is found, process the result */
11982 if (*al == ',')
11983 goto process_value;
11984
11985 /* If the character is different from ';', look
11986 * for the end of the header part in best effort.
11987 */
11988 if (*al != ';')
11989 goto expect_comma;
11990
11991 /* Assumes that the char is ';', now expect "q=". */
11992 al++;
11993
11994 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011995 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011996 al++;
11997 if (al >= end)
11998 goto process_value;
11999
12000 /* Expect 'q'. If no 'q', continue in best effort */
12001 if (*al != 'q')
12002 goto process_value;
12003 al++;
12004
12005 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012006 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012007 al++;
12008 if (al >= end)
12009 goto process_value;
12010
12011 /* Expect '='. If no '=', continue in best effort */
12012 if (*al != '=')
12013 goto process_value;
12014 al++;
12015
12016 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012017 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012018 al++;
12019 if (al >= end)
12020 goto process_value;
12021
12022 /* Parse the q value. */
12023 qvalue = parse_qvalue(al, &al);
12024
12025process_value:
12026
12027 /* If the new q value is the best q value, then store the associated
12028 * language in the response. If qvalue is the biggest value (1000),
12029 * break the process.
12030 */
12031 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012032 smp->data.u.str.str = (char *)w;
12033 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012034 if (qvalue >= 1000)
12035 break;
12036 best_q = qvalue;
12037 }
12038
12039expect_comma:
12040
12041 /* Expect comma or end. If the end is detected, quit the loop. */
12042 while (al < end && *al != ',')
12043 al++;
12044 if (al >= end)
12045 break;
12046
12047 /* Comma is found, jump it and restart the analyzer. */
12048 al++;
12049 }
12050
12051 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012052 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12053 smp->data.u.str.str = args[1].data.str.str;
12054 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012055 }
12056
12057 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012058 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012059}
12060
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012061/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012062static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012063{
12064 /* If the constant flag is set or if not size is avalaible at
12065 * the end of the buffer, copy the string in other buffer
12066 * before decoding.
12067 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012068 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012069 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012070 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12071 smp->data.u.str.str = str->str;
12072 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012073 smp->flags &= ~SMP_F_CONST;
12074 }
12075
12076 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012077 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12078 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012079 return 1;
12080}
12081
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012082static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12083{
12084 struct proxy *fe = strm_fe(smp->strm);
12085 int idx, i;
12086 struct cap_hdr *hdr;
12087 int len;
12088
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012089 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012090 return 0;
12091
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012092 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012093
12094 /* Check the availibity of the capture id. */
12095 if (idx > fe->nb_req_cap - 1)
12096 return 0;
12097
12098 /* Look for the original configuration. */
12099 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12100 hdr != NULL && i != idx ;
12101 i--, hdr = hdr->next);
12102 if (!hdr)
12103 return 0;
12104
12105 /* check for the memory allocation */
12106 if (smp->strm->req_cap[hdr->index] == NULL)
12107 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12108 if (smp->strm->req_cap[hdr->index] == NULL)
12109 return 0;
12110
12111 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012112 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012113 if (len > hdr->len)
12114 len = hdr->len;
12115
12116 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012117 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012118 smp->strm->req_cap[idx][len] = '\0';
12119
12120 return 1;
12121}
12122
12123static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12124{
12125 struct proxy *fe = strm_fe(smp->strm);
12126 int idx, i;
12127 struct cap_hdr *hdr;
12128 int len;
12129
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012130 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012131 return 0;
12132
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012133 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012134
12135 /* Check the availibity of the capture id. */
12136 if (idx > fe->nb_rsp_cap - 1)
12137 return 0;
12138
12139 /* Look for the original configuration. */
12140 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12141 hdr != NULL && i != idx ;
12142 i--, hdr = hdr->next);
12143 if (!hdr)
12144 return 0;
12145
12146 /* check for the memory allocation */
12147 if (smp->strm->res_cap[hdr->index] == NULL)
12148 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12149 if (smp->strm->res_cap[hdr->index] == NULL)
12150 return 0;
12151
12152 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012153 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012154 if (len > hdr->len)
12155 len = hdr->len;
12156
12157 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012158 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012159 smp->strm->res_cap[idx][len] = '\0';
12160
12161 return 1;
12162}
12163
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012164/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012165 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012166 * the relevant part of the request line accordingly. Then it updates various
12167 * pointers to the next elements which were moved, and the total buffer length.
12168 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012169 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12170 * error, though this can be revisited when this code is finally exploited.
12171 *
12172 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12173 * query string and 3 to replace uri.
12174 *
12175 * In query string case, the mark question '?' must be set at the start of the
12176 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012177 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012178int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012179 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012180{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012181 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012182 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012183 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012184 int delta;
12185
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012186 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012187 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012188 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012189 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12190
12191 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012192 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012193 txn->req.sl.rq.m_l += delta;
12194 txn->req.sl.rq.u += delta;
12195 txn->req.sl.rq.v += delta;
12196 break;
12197
12198 case 1: // path
12199 cur_ptr = http_get_path(txn);
12200 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012201 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012202
12203 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012204 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 +010012205 cur_end++;
12206
12207 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012208 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012209 txn->req.sl.rq.u_l += delta;
12210 txn->req.sl.rq.v += delta;
12211 break;
12212
12213 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012214 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012215 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012216 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12217 while (cur_ptr < cur_end && *cur_ptr != '?')
12218 cur_ptr++;
12219
12220 /* skip the question mark or indicate that we must insert it
12221 * (but only if the format string is not empty then).
12222 */
12223 if (cur_ptr < cur_end)
12224 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012225 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012226 offset = 0;
12227
12228 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012229 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012230 txn->req.sl.rq.u_l += delta;
12231 txn->req.sl.rq.v += delta;
12232 break;
12233
12234 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012235 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012236 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12237
12238 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012239 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012240 txn->req.sl.rq.u_l += delta;
12241 txn->req.sl.rq.v += delta;
12242 break;
12243
12244 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012245 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012246 }
12247
12248 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012249 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012250 txn->req.sl.rq.l += delta;
12251 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012252 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012253 return 0;
12254}
12255
12256/* This function executes one of the set-{method,path,query,uri} actions. It
12257 * builds a string in the trash from the specified format string. It finds
12258 * the action to be performed in p[2], previously filled by function
12259 * parse_set_req_line(). The replacement action is excuted by the function
12260 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
12261 * the action is canceled, but the rule processing continue.
12262 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012263enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
12264 struct session *sess, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012265{
12266 chunk_reset(&trash);
12267
12268 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012269 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012270 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012271 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012272
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012273 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012274 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012275}
12276
12277/* 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).
12286 * It returns 0 on success, < 0 on error.
12287 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012288int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012289{
12290 int cur_arg = *orig_arg;
12291
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +020012292 rule->action = ACT_ACTION_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012293
12294 switch (args[0][4]) {
12295 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012296 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012297 rule->action_ptr = http_action_set_req_line;
12298 break;
12299 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012300 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012301 rule->action_ptr = http_action_set_req_line;
12302 break;
12303 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012304 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012305 rule->action_ptr = http_action_set_req_line;
12306 break;
12307 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012308 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012309 rule->action_ptr = http_action_set_req_line;
12310 break;
12311 default:
12312 memprintf(err, "internal error: unhandled action '%s'", args[0]);
12313 return -1;
12314 }
12315
12316 if (!*args[cur_arg] ||
12317 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12318 memprintf(err, "expects exactly 1 argument <format>");
12319 return -1;
12320 }
12321
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012322 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012323 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012324 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012325 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12326 proxy->conf.args.file, proxy->conf.args.line);
12327
12328 (*orig_arg)++;
12329 return 0;
12330}
12331
Willy Tarreaua9083d02015-05-08 15:27:59 +020012332/* This function executes the "capture" action. It executes a fetch expression,
12333 * turns the result into a string and puts it in a capture slot. It always
12334 * returns 1. If an error occurs the action is cancelled, but the rule
12335 * processing continues.
12336 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012337enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
12338 struct session *sess, struct stream *s)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012339{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012340 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012341 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012342 char **cap = s->req_cap;
12343 int len;
12344
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012345 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 +020012346 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012347 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012348
12349 if (cap[h->index] == NULL)
12350 cap[h->index] = pool_alloc2(h->pool);
12351
12352 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012353 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012354
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012355 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012356 if (len > h->len)
12357 len = h->len;
12358
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012359 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012360 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012361 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012362}
12363
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012364/* This function executes the "capture" action and store the result in a
12365 * capture slot if exists. It executes a fetch expression, turns the result
12366 * into a string and puts it in a capture slot. It always returns 1. If an
12367 * error occurs the action is cancelled, but the rule processing continues.
12368 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012369enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
12370 struct session *sess, struct stream *s)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012371{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012372 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012373 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012374 char **cap = s->req_cap;
12375 struct proxy *fe = strm_fe(s);
12376 int len;
12377 int i;
12378
12379 /* Look for the original configuration. */
12380 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012381 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012382 i--, h = h->next);
12383 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012384 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012385
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012386 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 +020012387 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012388 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012389
12390 if (cap[h->index] == NULL)
12391 cap[h->index] = pool_alloc2(h->pool);
12392
12393 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012394 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012395
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012396 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012397 if (len > h->len)
12398 len = h->len;
12399
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012400 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012401 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012402 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012403}
12404
Willy Tarreaua9083d02015-05-08 15:27:59 +020012405/* parse an "http-request capture" action. It takes a single argument which is
12406 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012407 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Willy Tarreaua9083d02015-05-08 15:27:59 +020012408 * It returns 0 on success, < 0 on error.
12409 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012410int parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012411{
12412 struct sample_expr *expr;
12413 struct cap_hdr *hdr;
12414 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012415 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012416
12417 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12418 if (strcmp(args[cur_arg], "if") == 0 ||
12419 strcmp(args[cur_arg], "unless") == 0)
12420 break;
12421
12422 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012423 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Willy Tarreaua9083d02015-05-08 15:27:59 +020012424 return -1;
12425 }
12426
Willy Tarreaua9083d02015-05-08 15:27:59 +020012427 cur_arg = *orig_arg;
12428 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12429 if (!expr)
12430 return -1;
12431
12432 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12433 memprintf(err,
12434 "fetch method '%s' extracts information from '%s', none of which is available here",
12435 args[cur_arg-1], sample_src_names(expr->fetch->use));
12436 free(expr);
12437 return -1;
12438 }
12439
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012440 if (!args[cur_arg] || !*args[cur_arg]) {
12441 memprintf(err, "expects 'len or 'id'");
12442 free(expr);
12443 return -1;
12444 }
12445
Willy Tarreaua9083d02015-05-08 15:27:59 +020012446 if (strcmp(args[cur_arg], "len") == 0) {
12447 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012448
12449 if (!(px->cap & PR_CAP_FE)) {
12450 memprintf(err, "proxy '%s' has no frontend capability", px->id);
12451 return -1;
12452 }
12453
12454 proxy->conf.args.ctx = ARGC_CAP;
12455
Willy Tarreaua9083d02015-05-08 15:27:59 +020012456 if (!args[cur_arg]) {
12457 memprintf(err, "missing length value");
12458 free(expr);
12459 return -1;
12460 }
12461 /* we copy the table name for now, it will be resolved later */
12462 len = atoi(args[cur_arg]);
12463 if (len <= 0) {
12464 memprintf(err, "length must be > 0");
12465 free(expr);
12466 return -1;
12467 }
12468 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012469
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012470 if (!len) {
12471 memprintf(err, "a positive 'len' argument is mandatory");
12472 free(expr);
12473 return -1;
12474 }
12475
12476 hdr = calloc(sizeof(struct cap_hdr), 1);
12477 hdr->next = px->req_cap;
12478 hdr->name = NULL; /* not a header capture */
12479 hdr->namelen = 0;
12480 hdr->len = len;
12481 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12482 hdr->index = px->nb_req_cap++;
12483
12484 px->req_cap = hdr;
12485 px->to_log |= LW_REQHDR;
12486
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +020012487 rule->action = ACT_ACTION_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012488 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012489 rule->arg.cap.expr = expr;
12490 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012491 }
12492
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012493 else if (strcmp(args[cur_arg], "id") == 0) {
12494 int id;
12495 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012496
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012497 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012498
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012499 if (!args[cur_arg]) {
12500 memprintf(err, "missing id value");
12501 free(expr);
12502 return -1;
12503 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012504
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012505 id = strtol(args[cur_arg], &error, 10);
12506 if (*error != '\0') {
12507 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12508 free(expr);
12509 return -1;
12510 }
12511 cur_arg++;
12512
12513 proxy->conf.args.ctx = ARGC_CAP;
12514
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +020012515 rule->action = ACT_ACTION_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012516 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012517 rule->arg.capid.expr = expr;
12518 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012519 }
12520
12521 else {
12522 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12523 free(expr);
12524 return -1;
12525 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012526
12527 *orig_arg = cur_arg;
12528 return 0;
12529}
12530
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012531/* This function executes the "capture" action and store the result in a
12532 * capture slot if exists. It executes a fetch expression, turns the result
12533 * into a string and puts it in a capture slot. It always returns 1. If an
12534 * error occurs the action is cancelled, but the rule processing continues.
12535 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012536enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
12537 struct session *sess, struct stream *s)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012538{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012539 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012540 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012541 char **cap = s->res_cap;
12542 struct proxy *fe = strm_fe(s);
12543 int len;
12544 int i;
12545
12546 /* Look for the original configuration. */
12547 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012548 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012549 i--, h = h->next);
12550 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012551 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012552
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012553 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 +020012554 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012555 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012556
12557 if (cap[h->index] == NULL)
12558 cap[h->index] = pool_alloc2(h->pool);
12559
12560 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012561 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012562
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012563 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012564 if (len > h->len)
12565 len = h->len;
12566
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012567 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012568 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012569 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012570}
12571
12572/* parse an "http-response capture" action. It takes a single argument which is
12573 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12574 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
12575 * It returns 0 on success, < 0 on error.
12576 */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +020012577int parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012578{
12579 struct sample_expr *expr;
12580 int cur_arg;
12581 int id;
12582 char *error;
12583
12584 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12585 if (strcmp(args[cur_arg], "if") == 0 ||
12586 strcmp(args[cur_arg], "unless") == 0)
12587 break;
12588
12589 if (cur_arg < *orig_arg + 3) {
12590 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
12591 return -1;
12592 }
12593
12594 cur_arg = *orig_arg;
12595 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12596 if (!expr)
12597 return -1;
12598
12599 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12600 memprintf(err,
12601 "fetch method '%s' extracts information from '%s', none of which is available here",
12602 args[cur_arg-1], sample_src_names(expr->fetch->use));
12603 free(expr);
12604 return -1;
12605 }
12606
12607 if (!args[cur_arg] || !*args[cur_arg]) {
12608 memprintf(err, "expects 'len or 'id'");
12609 free(expr);
12610 return -1;
12611 }
12612
12613 if (strcmp(args[cur_arg], "id") != 0) {
12614 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12615 free(expr);
12616 return -1;
12617 }
12618
12619 cur_arg++;
12620
12621 if (!args[cur_arg]) {
12622 memprintf(err, "missing id value");
12623 free(expr);
12624 return -1;
12625 }
12626
12627 id = strtol(args[cur_arg], &error, 10);
12628 if (*error != '\0') {
12629 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12630 free(expr);
12631 return -1;
12632 }
12633 cur_arg++;
12634
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012635 proxy->conf.args.ctx = ARGC_CAP;
12636
Thierry FOURNIER91f6ba02015-08-06 08:30:11 +020012637 rule->action = ACT_ACTION_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012638 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012639 rule->arg.capid.expr = expr;
12640 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012641
12642 *orig_arg = cur_arg;
12643 return 0;
12644}
12645
William Lallemand73025dd2014-04-24 14:38:37 +020012646/*
12647 * Return the struct http_req_action_kw associated to a keyword.
12648 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012649struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012650{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012651 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012652}
12653
12654/*
12655 * Return the struct http_res_action_kw associated to a keyword.
12656 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012657struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012658{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012659 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012660}
12661
Willy Tarreau4a568972010-05-12 08:08:50 +020012662/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012663/* All supported ACL keywords must be declared here. */
12664/************************************************************************/
12665
12666/* Note: must not be declared <const> as its list will be overwritten.
12667 * Please take care of keeping this list alphabetically sorted.
12668 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012669static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012670 { "base", "base", PAT_MATCH_STR },
12671 { "base_beg", "base", PAT_MATCH_BEG },
12672 { "base_dir", "base", PAT_MATCH_DIR },
12673 { "base_dom", "base", PAT_MATCH_DOM },
12674 { "base_end", "base", PAT_MATCH_END },
12675 { "base_len", "base", PAT_MATCH_LEN },
12676 { "base_reg", "base", PAT_MATCH_REG },
12677 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012678
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012679 { "cook", "req.cook", PAT_MATCH_STR },
12680 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12681 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12682 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12683 { "cook_end", "req.cook", PAT_MATCH_END },
12684 { "cook_len", "req.cook", PAT_MATCH_LEN },
12685 { "cook_reg", "req.cook", PAT_MATCH_REG },
12686 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012687
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012688 { "hdr", "req.hdr", PAT_MATCH_STR },
12689 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12690 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12691 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12692 { "hdr_end", "req.hdr", PAT_MATCH_END },
12693 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12694 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12695 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012696
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012697 /* these two declarations uses strings with list storage (in place
12698 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12699 * and delete functions are relative to the list management. The parse
12700 * and match method are related to the corresponding fetch methods. This
12701 * is very particular ACL declaration mode.
12702 */
12703 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12704 { "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 +020012705
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012706 { "path", "path", PAT_MATCH_STR },
12707 { "path_beg", "path", PAT_MATCH_BEG },
12708 { "path_dir", "path", PAT_MATCH_DIR },
12709 { "path_dom", "path", PAT_MATCH_DOM },
12710 { "path_end", "path", PAT_MATCH_END },
12711 { "path_len", "path", PAT_MATCH_LEN },
12712 { "path_reg", "path", PAT_MATCH_REG },
12713 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012714
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012715 { "req_ver", "req.ver", PAT_MATCH_STR },
12716 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012717
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012718 { "scook", "res.cook", PAT_MATCH_STR },
12719 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12720 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12721 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12722 { "scook_end", "res.cook", PAT_MATCH_END },
12723 { "scook_len", "res.cook", PAT_MATCH_LEN },
12724 { "scook_reg", "res.cook", PAT_MATCH_REG },
12725 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012726
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012727 { "shdr", "res.hdr", PAT_MATCH_STR },
12728 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12729 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12730 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12731 { "shdr_end", "res.hdr", PAT_MATCH_END },
12732 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12733 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12734 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012735
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012736 { "url", "url", PAT_MATCH_STR },
12737 { "url_beg", "url", PAT_MATCH_BEG },
12738 { "url_dir", "url", PAT_MATCH_DIR },
12739 { "url_dom", "url", PAT_MATCH_DOM },
12740 { "url_end", "url", PAT_MATCH_END },
12741 { "url_len", "url", PAT_MATCH_LEN },
12742 { "url_reg", "url", PAT_MATCH_REG },
12743 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012744
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012745 { "urlp", "urlp", PAT_MATCH_STR },
12746 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12747 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12748 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12749 { "urlp_end", "urlp", PAT_MATCH_END },
12750 { "urlp_len", "urlp", PAT_MATCH_LEN },
12751 { "urlp_reg", "urlp", PAT_MATCH_REG },
12752 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012753
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012754 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012755}};
12756
12757/************************************************************************/
12758/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012759/************************************************************************/
12760/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012761static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012762 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012763 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012764 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12765
Willy Tarreau87b09662015-04-03 00:22:06 +020012766 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012767 { "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 +020012768
12769 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012770 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12771 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12772 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012773
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012774 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12775 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012776
Willy Tarreau409bcde2013-01-08 00:31:00 +010012777 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12778 * are only here to match the ACL's name, are request-only and are used
12779 * for ACL compatibility only.
12780 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012781 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12782 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012783 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12784 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012785
12786 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12787 * only here to match the ACL's name, are request-only and are used for
12788 * ACL compatibility only.
12789 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012790 { "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 +020012791 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012792 { "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 +020012793 { "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 +010012794
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012795 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012796 { "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 +010012797 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012798 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012799 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012800 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012801
12802 /* HTTP protocol on the request path */
12803 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012804 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012805
12806 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012807 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12808 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012809
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012810 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012811 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12812 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012813 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012814
Willy Tarreau18ed2562013-01-14 15:56:36 +010012815 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012816 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12817 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012818
Willy Tarreau18ed2562013-01-14 15:56:36 +010012819 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012820 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012821 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12822 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012823
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012824 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012825 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012826 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012827 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012828 { "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 +010012829 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012830 { "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 +010012831
12832 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012833 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012834 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12835 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012836
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012837 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012838 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012839 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012840 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012841 { "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 +010012842 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012843 { "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 +010012844
Willy Tarreau409bcde2013-01-08 00:31:00 +010012845 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012846 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012847 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12848 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012849 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012850
12851 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012852 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012853 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012854 { "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 +020012855 { "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 +010012856
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012857 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012858 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012859 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012860 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012861 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012862 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012863 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12864 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012865 { "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 +010012866 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012867}};
12868
Willy Tarreau8797c062007-05-07 00:55:35 +020012869
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012870/************************************************************************/
12871/* All supported converter keywords must be declared here. */
12872/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012873/* Note: must not be declared <const> as its list will be overwritten */
12874static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012875 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012876 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012877 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12878 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012879 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012880 { NULL, NULL, 0, 0, 0 },
12881}};
12882
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012883
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012884/************************************************************************/
12885/* All supported http-request action keywords must be declared here. */
12886/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012887struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012888 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012889 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012890 { "set-method", parse_set_req_line },
12891 { "set-path", parse_set_req_line },
12892 { "set-query", parse_set_req_line },
12893 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012894 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012895 }
12896};
12897
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012898struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012899 .kw = {
12900 { "capture", parse_http_res_capture },
12901 { NULL, NULL }
12902 }
12903};
12904
Willy Tarreau8797c062007-05-07 00:55:35 +020012905__attribute__((constructor))
12906static void __http_protocol_init(void)
12907{
12908 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012909 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012910 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012911 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012912 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012913}
12914
12915
Willy Tarreau58f10d72006-12-04 02:26:12 +010012916/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012917 * Local variables:
12918 * c-indent-level: 8
12919 * c-basic-offset: 8
12920 * End:
12921 */