blob: c35e6a1ef581849018ebc1a6b1dbfadb57f30299 [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
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
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" */
240struct http_req_action_kw_list http_req_keywords = {
241 .list = LIST_HEAD_INIT(http_req_keywords.list)
242};
243
244/* List head of all known action keywords for "http-response" */
245struct http_res_action_kw_list http_res_keywords = {
246 .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) {
3354 case HTTP_REQ_ACT_REPLACE_VAL:
3355 case HTTP_RES_ACT_REPLACE_VAL:
3356 http_find_hdr_func = http_find_header2;
3357 break;
3358 case HTTP_REQ_ACT_REPLACE_HDR:
3359 case HTTP_RES_ACT_REPLACE_HDR:
3360 http_find_hdr_func = http_find_full_header2;
3361 break;
3362 default: /* impossible */
3363 return -1;
3364 }
3365
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003366 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3367 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003368 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003369 char *val = ctx.line + ctx.val;
3370 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003371
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003372 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3373 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003374
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003375 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003376 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003377 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003378
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003379 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003380
3381 hdr->len += delta;
3382 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003383
3384 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003385 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003386 }
3387
3388 return 0;
3389}
3390
Willy Tarreau87b09662015-04-03 00:22:06 +02003391static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003392 const char* name, unsigned int name_len,
3393 struct list *fmt, struct my_regex *re,
3394 int action)
3395{
3396 struct chunk *replace = get_trash_chunk();
3397
3398 replace->len = build_logline(s, replace->str, replace->size, fmt);
3399 if (replace->len >= replace->size - 1)
3400 return -1;
3401
3402 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3403}
3404
Willy Tarreau87b09662015-04-03 00:22:06 +02003405/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003406 * transaction <txn>. Returns the verdict of the first rule that prevents
3407 * further processing of the request (auth, deny, ...), and defaults to
3408 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3409 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3410 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003411 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003412enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003413http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003414{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003415 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003416 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003417 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003418 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003419 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003420 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003421
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003422 /* If "the current_rule_list" match the executed rule list, we are in
3423 * resume condition. If a resume is needed it is always in the action
3424 * and never in the ACL or converters. In this case, we initialise the
3425 * current rule, and go to the action execution point.
3426 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003427 if (s->current_rule) {
3428 rule = s->current_rule;
3429 s->current_rule = NULL;
3430 if (s->current_rule_list == rules)
3431 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003432 }
3433 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003434
Willy Tarreauff011f22011-01-06 17:51:27 +01003435 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003436 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003437 continue;
3438
Willy Tarreau96257ec2012-12-27 10:46:37 +01003439 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003440 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003441 int ret;
3442
Willy Tarreau192252e2015-04-04 01:47:55 +02003443 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003444 ret = acl_pass(ret);
3445
Willy Tarreauff011f22011-01-06 17:51:27 +01003446 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003447 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003448
3449 if (!ret) /* condition not matched */
3450 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003451 }
3452
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003453resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003454 switch (rule->action) {
3455 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003456 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003457
3458 case HTTP_REQ_ACT_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003459 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003460 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003461
Willy Tarreauccbcc372012-12-27 12:37:57 +01003462 case HTTP_REQ_ACT_TARPIT:
3463 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003464 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003465 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003466
Willy Tarreau96257ec2012-12-27 10:46:37 +01003467 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003468 /* Auth might be performed on regular http-req rules as well as on stats */
3469 auth_realm = rule->arg.auth.realm;
3470 if (!auth_realm) {
3471 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3472 auth_realm = STATS_DEFAULT_REALM;
3473 else
3474 auth_realm = px->id;
3475 }
3476 /* send 401/407 depending on whether we use a proxy or not. We still
3477 * count one error, because normal browsing won't significantly
3478 * increase the counter but brute force attempts will.
3479 */
3480 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3481 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3482 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003483 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003484 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003485
Willy Tarreau81499eb2012-12-27 12:19:02 +01003486 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003487 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3488 return HTTP_RULE_RES_BADREQ;
3489 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003490
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003491 case HTTP_REQ_ACT_SET_NICE:
3492 s->task->nice = rule->arg.nice;
3493 break;
3494
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003495 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003496 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003497 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003498 break;
3499
Willy Tarreau51347ed2013-06-11 19:34:13 +02003500 case HTTP_REQ_ACT_SET_MARK:
3501#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003502 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003503 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003504#endif
3505 break;
3506
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003507 case HTTP_REQ_ACT_SET_LOGL:
3508 s->logs.level = rule->arg.loglevel;
3509 break;
3510
Sasha Pachev218f0642014-06-16 12:05:59 -06003511 case HTTP_REQ_ACT_REPLACE_HDR:
3512 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003513 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3514 rule->arg.hdr_add.name_len,
3515 &rule->arg.hdr_add.fmt,
3516 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003517 return HTTP_RULE_RES_BADREQ;
3518 break;
3519
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003520 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003521 ctx.idx = 0;
3522 /* remove all occurrences of the header */
3523 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3524 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3525 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003526 }
Willy Tarreau85603282015-01-21 20:39:27 +01003527 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003528
Willy Tarreau85603282015-01-21 20:39:27 +01003529 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003530 case HTTP_REQ_ACT_ADD_HDR:
3531 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3532 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3533 trash.len = rule->arg.hdr_add.name_len;
3534 trash.str[trash.len++] = ':';
3535 trash.str[trash.len++] = ' ';
3536 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 +01003537
3538 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3539 /* remove all occurrences of the header */
3540 ctx.idx = 0;
3541 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3542 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3543 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3544 }
3545 }
3546
Willy Tarreau96257ec2012-12-27 10:46:37 +01003547 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3548 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003549
3550 case HTTP_REQ_ACT_DEL_ACL:
3551 case HTTP_REQ_ACT_DEL_MAP: {
3552 struct pat_ref *ref;
3553 char *key;
3554 int len;
3555
3556 /* collect reference */
3557 ref = pat_ref_lookup(rule->arg.map.ref);
3558 if (!ref)
3559 continue;
3560
3561 /* collect key */
3562 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3563 key = trash.str;
3564 key[len] = '\0';
3565
3566 /* perform update */
3567 /* returned code: 1=ok, 0=ko */
3568 pat_ref_delete(ref, key);
3569
3570 break;
3571 }
3572
3573 case HTTP_REQ_ACT_ADD_ACL: {
3574 struct pat_ref *ref;
3575 char *key;
3576 struct chunk *trash_key;
3577 int len;
3578
3579 trash_key = get_trash_chunk();
3580
3581 /* collect reference */
3582 ref = pat_ref_lookup(rule->arg.map.ref);
3583 if (!ref)
3584 continue;
3585
3586 /* collect key */
3587 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3588 key = trash_key->str;
3589 key[len] = '\0';
3590
3591 /* perform update */
3592 /* add entry only if it does not already exist */
3593 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003594 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003595
3596 break;
3597 }
3598
3599 case HTTP_REQ_ACT_SET_MAP: {
3600 struct pat_ref *ref;
3601 char *key, *value;
3602 struct chunk *trash_key, *trash_value;
3603 int len;
3604
3605 trash_key = get_trash_chunk();
3606 trash_value = get_trash_chunk();
3607
3608 /* collect reference */
3609 ref = pat_ref_lookup(rule->arg.map.ref);
3610 if (!ref)
3611 continue;
3612
3613 /* collect key */
3614 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3615 key = trash_key->str;
3616 key[len] = '\0';
3617
3618 /* collect value */
3619 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3620 value = trash_value->str;
3621 value[len] = '\0';
3622
3623 /* perform update */
3624 if (pat_ref_find_elt(ref, key) != NULL)
3625 /* update entry if it exists */
3626 pat_ref_set(ref, key, value, NULL);
3627 else
3628 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003629 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003630
3631 break;
3632 }
William Lallemand73025dd2014-04-24 14:38:37 +02003633
3634 case HTTP_REQ_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003635 if (!rule->action_ptr(rule, px, s)) {
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
3641 case HTTP_REQ_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003642 rule->action_ptr(rule, px, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003643 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003644
3645 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3646 /* Note: only the first valid tracking parameter of each
3647 * applies.
3648 */
3649
3650 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3651 struct stktable *t;
3652 struct stksess *ts;
3653 struct stktable_key *key;
3654 void *ptr;
3655
3656 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02003657 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02003658
3659 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003660 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003661
3662 /* let's count a new HTTP request as it's the first time we do it */
3663 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3664 if (ptr)
3665 stktable_data_cast(ptr, http_req_cnt)++;
3666
3667 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3668 if (ptr)
3669 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3670 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3671
3672 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003673 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003674 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3675 }
3676 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003677 break;
3678
3679 case HTTP_REQ_ACT_SET_SRC:
3680 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3681 struct sample *smp;
3682
3683 smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.act.p[0], SMP_T_ADDR);
3684
3685 if (smp) {
3686 if (smp->type == SMP_T_IPV4) {
3687 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
3688 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.ipv4.s_addr;
3689 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
3690 } else if (smp->type == SMP_T_IPV6) {
3691 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
3692 memcpy(&((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, &smp->data.ipv6, sizeof(struct in6_addr));
3693 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3694 }
3695 }
3696 }
3697 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003698 }
3699 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003700
3701 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003702 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003703}
3704
Willy Tarreau71241ab2012-12-27 11:30:54 +01003705
Willy Tarreau51d861a2015-05-22 17:30:48 +02003706/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3707 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3708 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3709 * is returned, the process can continue the evaluation of next rule list. If
3710 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3711 * is returned, it means the operation could not be processed and a server error
3712 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3713 * deny rule. If *YIELD is returned, the caller must call again the function
3714 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003715 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003716static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003717http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003718{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003719 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003720 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003721 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003722 struct http_res_rule *rule;
3723 struct hdr_ctx ctx;
3724
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003725 /* If "the current_rule_list" match the executed rule list, we are in
3726 * resume condition. If a resume is needed it is always in the action
3727 * and never in the ACL or converters. In this case, we initialise the
3728 * current rule, and go to the action execution point.
3729 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003730 if (s->current_rule) {
3731 rule = s->current_rule;
3732 s->current_rule = NULL;
3733 if (s->current_rule_list == rules)
3734 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003735 }
3736 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003737
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003738 list_for_each_entry(rule, rules, list) {
3739 if (rule->action >= HTTP_RES_ACT_MAX)
3740 continue;
3741
3742 /* check optional condition */
3743 if (rule->cond) {
3744 int ret;
3745
Willy Tarreau192252e2015-04-04 01:47:55 +02003746 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003747 ret = acl_pass(ret);
3748
3749 if (rule->cond->pol == ACL_COND_UNLESS)
3750 ret = !ret;
3751
3752 if (!ret) /* condition not matched */
3753 continue;
3754 }
3755
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003756resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003757 switch (rule->action) {
3758 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003759 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003760
3761 case HTTP_RES_ACT_DENY:
3762 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003763 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003764
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003765 case HTTP_RES_ACT_SET_NICE:
3766 s->task->nice = rule->arg.nice;
3767 break;
3768
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003769 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003770 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003771 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003772 break;
3773
Willy Tarreau51347ed2013-06-11 19:34:13 +02003774 case HTTP_RES_ACT_SET_MARK:
3775#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003776 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003777 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003778#endif
3779 break;
3780
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003781 case HTTP_RES_ACT_SET_LOGL:
3782 s->logs.level = rule->arg.loglevel;
3783 break;
3784
Sasha Pachev218f0642014-06-16 12:05:59 -06003785 case HTTP_RES_ACT_REPLACE_HDR:
3786 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003787 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3788 rule->arg.hdr_add.name_len,
3789 &rule->arg.hdr_add.fmt,
3790 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003791 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003792 break;
3793
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003794 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003795 ctx.idx = 0;
3796 /* remove all occurrences of the header */
3797 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3798 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3799 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3800 }
Willy Tarreau85603282015-01-21 20:39:27 +01003801 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802
Willy Tarreau85603282015-01-21 20:39:27 +01003803 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003804 case HTTP_RES_ACT_ADD_HDR:
3805 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3806 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3807 trash.len = rule->arg.hdr_add.name_len;
3808 trash.str[trash.len++] = ':';
3809 trash.str[trash.len++] = ' ';
3810 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 +01003811
3812 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3813 /* remove all occurrences of the header */
3814 ctx.idx = 0;
3815 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3816 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3817 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3818 }
3819 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003820 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3821 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003822
3823 case HTTP_RES_ACT_DEL_ACL:
3824 case HTTP_RES_ACT_DEL_MAP: {
3825 struct pat_ref *ref;
3826 char *key;
3827 int len;
3828
3829 /* collect reference */
3830 ref = pat_ref_lookup(rule->arg.map.ref);
3831 if (!ref)
3832 continue;
3833
3834 /* collect key */
3835 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3836 key = trash.str;
3837 key[len] = '\0';
3838
3839 /* perform update */
3840 /* returned code: 1=ok, 0=ko */
3841 pat_ref_delete(ref, key);
3842
3843 break;
3844 }
3845
3846 case HTTP_RES_ACT_ADD_ACL: {
3847 struct pat_ref *ref;
3848 char *key;
3849 struct chunk *trash_key;
3850 int len;
3851
3852 trash_key = get_trash_chunk();
3853
3854 /* collect reference */
3855 ref = pat_ref_lookup(rule->arg.map.ref);
3856 if (!ref)
3857 continue;
3858
3859 /* collect key */
3860 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3861 key = trash_key->str;
3862 key[len] = '\0';
3863
3864 /* perform update */
3865 /* check if the entry already exists */
3866 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003867 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003868
3869 break;
3870 }
3871
3872 case HTTP_RES_ACT_SET_MAP: {
3873 struct pat_ref *ref;
3874 char *key, *value;
3875 struct chunk *trash_key, *trash_value;
3876 int len;
3877
3878 trash_key = get_trash_chunk();
3879 trash_value = get_trash_chunk();
3880
3881 /* collect reference */
3882 ref = pat_ref_lookup(rule->arg.map.ref);
3883 if (!ref)
3884 continue;
3885
3886 /* collect key */
3887 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3888 key = trash_key->str;
3889 key[len] = '\0';
3890
3891 /* collect value */
3892 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3893 value = trash_value->str;
3894 value[len] = '\0';
3895
3896 /* perform update */
3897 if (pat_ref_find_elt(ref, key) != NULL)
3898 /* update entry if it exists */
3899 pat_ref_set(ref, key, value, NULL);
3900 else
3901 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003902 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003903
3904 break;
3905 }
William Lallemand73025dd2014-04-24 14:38:37 +02003906
Willy Tarreau51d861a2015-05-22 17:30:48 +02003907 case HTTP_RES_ACT_REDIR:
3908 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3909 return HTTP_RULE_RES_BADREQ;
3910 return HTTP_RULE_RES_DONE;
3911
William Lallemand73025dd2014-04-24 14:38:37 +02003912 case HTTP_RES_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003913 if (!rule->action_ptr(rule, px, s)) {
Willy Tarreau152b81e2015-04-20 13:26:17 +02003914 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003915 return HTTP_RULE_RES_YIELD;
3916 }
William Lallemand73025dd2014-04-24 14:38:37 +02003917 break;
3918
3919 case HTTP_RES_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003920 rule->action_ptr(rule, px, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003921 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003922 }
3923 }
3924
3925 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003926 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003927}
3928
3929
Willy Tarreau71241ab2012-12-27 11:30:54 +01003930/* Perform an HTTP redirect based on the information in <rule>. The function
3931 * returns non-zero on success, or zero in case of a, irrecoverable error such
3932 * as too large a request to build a valid response.
3933 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003934static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003935{
Willy Tarreaub329a312015-05-22 16:27:37 +02003936 struct http_msg *req = &txn->req;
3937 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003938 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003939 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003940
3941 /* build redirect message */
3942 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003943 case 308:
3944 msg_fmt = HTTP_308;
3945 break;
3946 case 307:
3947 msg_fmt = HTTP_307;
3948 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003949 case 303:
3950 msg_fmt = HTTP_303;
3951 break;
3952 case 301:
3953 msg_fmt = HTTP_301;
3954 break;
3955 case 302:
3956 default:
3957 msg_fmt = HTTP_302;
3958 break;
3959 }
3960
3961 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3962 return 0;
3963
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003964 location = trash.str + trash.len;
3965
Willy Tarreau71241ab2012-12-27 11:30:54 +01003966 switch(rule->type) {
3967 case REDIRECT_TYPE_SCHEME: {
3968 const char *path;
3969 const char *host;
3970 struct hdr_ctx ctx;
3971 int pathlen;
3972 int hostlen;
3973
3974 host = "";
3975 hostlen = 0;
3976 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003977 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003978 host = ctx.line + ctx.val;
3979 hostlen = ctx.vlen;
3980 }
3981
3982 path = http_get_path(txn);
3983 /* build message using path */
3984 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003985 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003986 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3987 int qs = 0;
3988 while (qs < pathlen) {
3989 if (path[qs] == '?') {
3990 pathlen = qs;
3991 break;
3992 }
3993 qs++;
3994 }
3995 }
3996 } else {
3997 path = "/";
3998 pathlen = 1;
3999 }
4000
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004001 if (rule->rdr_str) { /* this is an old "redirect" rule */
4002 /* check if we can add scheme + "://" + host + path */
4003 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4004 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004005
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004006 /* add scheme */
4007 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4008 trash.len += rule->rdr_len;
4009 }
4010 else {
4011 /* add scheme with executing log format */
4012 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004013
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004014 /* check if we can add scheme + "://" + host + path */
4015 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4016 return 0;
4017 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004018 /* add "://" */
4019 memcpy(trash.str + trash.len, "://", 3);
4020 trash.len += 3;
4021
4022 /* add host */
4023 memcpy(trash.str + trash.len, host, hostlen);
4024 trash.len += hostlen;
4025
4026 /* add path */
4027 memcpy(trash.str + trash.len, path, pathlen);
4028 trash.len += pathlen;
4029
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004030 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004031 if (trash.len && trash.str[trash.len - 1] != '/' &&
4032 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4033 if (trash.len > trash.size - 5)
4034 return 0;
4035 trash.str[trash.len] = '/';
4036 trash.len++;
4037 }
4038
4039 break;
4040 }
4041 case REDIRECT_TYPE_PREFIX: {
4042 const char *path;
4043 int pathlen;
4044
4045 path = http_get_path(txn);
4046 /* build message using path */
4047 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004048 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004049 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4050 int qs = 0;
4051 while (qs < pathlen) {
4052 if (path[qs] == '?') {
4053 pathlen = qs;
4054 break;
4055 }
4056 qs++;
4057 }
4058 }
4059 } else {
4060 path = "/";
4061 pathlen = 1;
4062 }
4063
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004064 if (rule->rdr_str) { /* this is an old "redirect" rule */
4065 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4066 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004067
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004068 /* add prefix. Note that if prefix == "/", we don't want to
4069 * add anything, otherwise it makes it hard for the user to
4070 * configure a self-redirection.
4071 */
4072 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4073 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4074 trash.len += rule->rdr_len;
4075 }
4076 }
4077 else {
4078 /* add prefix with executing log format */
4079 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4080
4081 /* Check length */
4082 if (trash.len + pathlen > trash.size - 4)
4083 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004084 }
4085
4086 /* add path */
4087 memcpy(trash.str + trash.len, path, pathlen);
4088 trash.len += pathlen;
4089
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004090 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004091 if (trash.len && trash.str[trash.len - 1] != '/' &&
4092 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4093 if (trash.len > trash.size - 5)
4094 return 0;
4095 trash.str[trash.len] = '/';
4096 trash.len++;
4097 }
4098
4099 break;
4100 }
4101 case REDIRECT_TYPE_LOCATION:
4102 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004103 if (rule->rdr_str) { /* this is an old "redirect" rule */
4104 if (trash.len + rule->rdr_len > trash.size - 4)
4105 return 0;
4106
4107 /* add location */
4108 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4109 trash.len += rule->rdr_len;
4110 }
4111 else {
4112 /* add location with executing log format */
4113 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004114
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004115 /* Check left length */
4116 if (trash.len > trash.size - 4)
4117 return 0;
4118 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004119 break;
4120 }
4121
4122 if (rule->cookie_len) {
4123 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4124 trash.len += 14;
4125 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4126 trash.len += rule->cookie_len;
4127 memcpy(trash.str + trash.len, "\r\n", 2);
4128 trash.len += 2;
4129 }
4130
4131 /* add end of headers and the keep-alive/close status.
4132 * We may choose to set keep-alive if the Location begins
4133 * with a slash, because the client will come back to the
4134 * same server.
4135 */
4136 txn->status = rule->code;
4137 /* let's log the request time */
4138 s->logs.tv_request = now;
4139
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004140 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004141 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004142 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004143 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4144 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4145 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004146 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004147 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4148 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4149 trash.len += 30;
4150 } else {
4151 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4152 trash.len += 24;
4153 }
4154 }
4155 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4156 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004157 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004158 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004159 bi_fast_delete(req->chn->buf, req->sov);
4160 req->next -= req->sov;
4161 req->sov = 0;
4162 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004163 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004164 req->msg_state = HTTP_MSG_CLOSED;
4165 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004166 /* Trim any possible response */
4167 res->chn->buf->i = 0;
4168 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004169 } else {
4170 /* keep-alive not possible */
4171 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4172 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4173 trash.len += 29;
4174 } else {
4175 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4176 trash.len += 23;
4177 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004178 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004179 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004180 }
4181
Willy Tarreaue7dff022015-04-03 01:14:29 +02004182 if (!(s->flags & SF_ERR_MASK))
4183 s->flags |= SF_ERR_LOCAL;
4184 if (!(s->flags & SF_FINST_MASK))
4185 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004186
4187 return 1;
4188}
4189
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004190/* This stream analyser runs all HTTP request processing which is common to
4191 * frontends and backends, which means blocking ACLs, filters, connection-close,
4192 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004193 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004194 * either needs more data or wants to immediately abort the request (eg: deny,
4195 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004196 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004197int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004198{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004199 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004200 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004201 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004202 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004203 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004204 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004205
Willy Tarreau655dce92009-11-08 13:10:58 +01004206 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004207 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004208 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004209 }
4210
Willy Tarreau87b09662015-04-03 00:22:06 +02004211 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 +02004212 now_ms, __FUNCTION__,
4213 s,
4214 req,
4215 req->rex, req->wex,
4216 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004217 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004218 req->analysers);
4219
Willy Tarreau65410832014-04-28 21:25:43 +02004220 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004221 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004222
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004223 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004224 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004225 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004226
Willy Tarreau0b748332014-04-29 00:13:29 +02004227 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004228 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4229 goto return_prx_yield;
4230
Willy Tarreau0b748332014-04-29 00:13:29 +02004231 case HTTP_RULE_RES_CONT:
4232 case HTTP_RULE_RES_STOP: /* nothing to do */
4233 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004234
Willy Tarreau0b748332014-04-29 00:13:29 +02004235 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4236 if (txn->flags & TX_CLTARPIT)
4237 goto tarpit;
4238 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004239
Willy Tarreau0b748332014-04-29 00:13:29 +02004240 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4241 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004242
Willy Tarreau0b748332014-04-29 00:13:29 +02004243 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004244 goto done;
4245
Willy Tarreau0b748332014-04-29 00:13:29 +02004246 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4247 goto return_bad_req;
4248 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004249 }
4250
Willy Tarreau52542592014-04-28 18:33:26 +02004251 /* OK at this stage, we know that the request was accepted according to
4252 * the http-request rules, we can check for the stats. Note that the
4253 * URI is detected *before* the req* rules in order not to be affected
4254 * by a possible reqrep, while they are processed *after* so that a
4255 * reqdeny can still block them. This clearly needs to change in 1.6!
4256 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004257 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004258 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004259 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004260 txn->status = 500;
4261 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004262 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004263
Willy Tarreaue7dff022015-04-03 01:14:29 +02004264 if (!(s->flags & SF_ERR_MASK))
4265 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004266 goto return_prx_cond;
4267 }
4268
4269 /* parse the whole stats request and extract the relevant information */
4270 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004271 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004272 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004273
Willy Tarreau0b748332014-04-29 00:13:29 +02004274 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4275 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004276
Willy Tarreau0b748332014-04-29 00:13:29 +02004277 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4278 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004279 }
4280
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004281 /* evaluate the req* rules except reqadd */
4282 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004283 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004284 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004285
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004286 if (txn->flags & TX_CLDENY)
4287 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004288
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004289 if (txn->flags & TX_CLTARPIT)
4290 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004291 }
Willy Tarreau06619262006-12-17 08:37:22 +01004292
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004293 /* add request headers from the rule sets in the same order */
4294 list_for_each_entry(wl, &px->req_add, list) {
4295 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004296 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004297 ret = acl_pass(ret);
4298 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4299 ret = !ret;
4300 if (!ret)
4301 continue;
4302 }
4303
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004304 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004305 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004306 }
4307
Willy Tarreau52542592014-04-28 18:33:26 +02004308
4309 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004310 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004311 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004312 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4313 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004314
Willy Tarreaue7dff022015-04-03 01:14:29 +02004315 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4316 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4317 if (!(s->flags & SF_FINST_MASK))
4318 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004319
Willy Tarreau70730dd2014-04-24 18:06:27 +02004320 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004321 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004322 select_compression_request_header(s, req->buf);
4323
4324 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004325 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004326 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004327 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004328
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004329 /* check whether we have some ACLs set to redirect this request */
4330 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004331 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004332 int ret;
4333
Willy Tarreau192252e2015-04-04 01:47:55 +02004334 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004335 ret = acl_pass(ret);
4336 if (rule->cond->pol == ACL_COND_UNLESS)
4337 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004338 if (!ret)
4339 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004340 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004341 if (!http_apply_redirect_rule(rule, s, txn))
4342 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004343 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004344 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004345
Willy Tarreau2be39392010-01-03 17:24:51 +01004346 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4347 * If this happens, then the data will not come immediately, so we must
4348 * send all what we have without waiting. Note that due to the small gain
4349 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004350 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004351 * itself once used.
4352 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004353 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004354
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004355 done: /* done with this analyser, continue with next ones that the calling
4356 * points will have set, if any.
4357 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004358 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004359 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4360 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004361 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004362
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004363 tarpit:
4364 /* When a connection is tarpitted, we use the tarpit timeout,
4365 * which may be the same as the connect timeout if unspecified.
4366 * If unset, then set it to zero because we really want it to
4367 * eventually expire. We build the tarpit as an analyser.
4368 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004369 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004370
4371 /* wipe the request out so that we can drop the connection early
4372 * if the client closes first.
4373 */
4374 channel_dont_connect(req);
4375 req->analysers = 0; /* remove switching rules etc... */
4376 req->analysers |= AN_REQ_HTTP_TARPIT;
4377 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4378 if (!req->analyse_exp)
4379 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004380 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004381 sess->fe->fe_counters.denied_req++;
4382 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004383 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004384 if (sess->listener->counters)
4385 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004386 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004387
4388 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004389 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004390 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004391 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004392 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004393 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004394 sess->fe->fe_counters.denied_req++;
4395 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004396 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004397 if (sess->listener->counters)
4398 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004399 goto return_prx_cond;
4400
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004401 return_bad_req:
4402 /* We centralize bad requests processing here */
4403 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4404 /* we detected a parsing error. We want to archive this request
4405 * in the dedicated proxy area for later troubleshooting.
4406 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004407 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004408 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004409
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004410 txn->req.msg_state = HTTP_MSG_ERROR;
4411 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004412 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004413
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004414 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004415 if (sess->listener->counters)
4416 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004417
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004418 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004419 if (!(s->flags & SF_ERR_MASK))
4420 s->flags |= SF_ERR_PRXCOND;
4421 if (!(s->flags & SF_FINST_MASK))
4422 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004423
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004424 req->analysers = 0;
4425 req->analyse_exp = TICK_ETERNITY;
4426 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004427
4428 return_prx_yield:
4429 channel_dont_connect(req);
4430 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004431}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004432
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004433/* This function performs all the processing enabled for the current request.
4434 * It returns 1 if the processing can continue on next analysers, or zero if it
4435 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004436 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004437 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004438int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004439{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004440 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004441 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004442 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004443 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004444
Willy Tarreau655dce92009-11-08 13:10:58 +01004445 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004446 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004447 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004448 return 0;
4449 }
4450
Willy Tarreau87b09662015-04-03 00:22:06 +02004451 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 +02004452 now_ms, __FUNCTION__,
4453 s,
4454 req,
4455 req->rex, req->wex,
4456 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004457 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004458 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004459
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004460 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004461 select_compression_request_header(s, req->buf);
4462
Willy Tarreau59234e92008-11-30 23:51:27 +01004463 /*
4464 * Right now, we know that we have processed the entire headers
4465 * and that unwanted requests have been filtered out. We can do
4466 * whatever we want with the remaining request. Also, now we
4467 * may have separate values for ->fe, ->be.
4468 */
Willy Tarreau06619262006-12-17 08:37:22 +01004469
Willy Tarreau59234e92008-11-30 23:51:27 +01004470 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004471 * If HTTP PROXY is set we simply get remote server address parsing
4472 * incoming request. Note that this requires that a connection is
4473 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004474 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004475 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004476 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004477 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004478
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004479 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004480 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004481 txn->req.msg_state = HTTP_MSG_ERROR;
4482 txn->status = 500;
4483 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004484 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004485
Willy Tarreaue7dff022015-04-03 01:14:29 +02004486 if (!(s->flags & SF_ERR_MASK))
4487 s->flags |= SF_ERR_RESOURCE;
4488 if (!(s->flags & SF_FINST_MASK))
4489 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004490
4491 return 0;
4492 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004493
4494 path = http_get_path(txn);
4495 url2sa(req->buf->p + msg->sl.rq.u,
4496 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004497 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004498 /* if the path was found, we have to remove everything between
4499 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4500 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4501 * u_l characters by a single "/".
4502 */
4503 if (path) {
4504 char *cur_ptr = req->buf->p;
4505 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4506 int delta;
4507
4508 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4509 http_msg_move_end(&txn->req, delta);
4510 cur_end += delta;
4511 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4512 goto return_bad_req;
4513 }
4514 else {
4515 char *cur_ptr = req->buf->p;
4516 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4517 int delta;
4518
4519 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4520 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4521 http_msg_move_end(&txn->req, delta);
4522 cur_end += delta;
4523 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4524 goto return_bad_req;
4525 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004526 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004527
Willy Tarreau59234e92008-11-30 23:51:27 +01004528 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004529 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004530 * Note that doing so might move headers in the request, but
4531 * the fields will stay coherent and the URI will not move.
4532 * This should only be performed in the backend.
4533 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004534 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004535 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4536 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004537
Willy Tarreau59234e92008-11-30 23:51:27 +01004538 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004539 * 8: the appsession cookie was looked up very early in 1.2,
4540 * so let's do the same now.
4541 */
4542
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004543 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004544 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004545 get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01004546 }
4547
William Lallemanda73203e2012-03-12 12:48:57 +01004548 /* add unique-id if "header-unique-id" is specified */
4549
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004550 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004551 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4552 goto return_bad_req;
4553 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004554 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004555 }
William Lallemanda73203e2012-03-12 12:48:57 +01004556
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004557 if (sess->fe->header_unique_id && s->unique_id) {
4558 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004559 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004560 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004561 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004562 goto return_bad_req;
4563 }
4564
Cyril Bontéb21570a2009-11-29 20:04:48 +01004565 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004566 * 9: add X-Forwarded-For if either the frontend or the backend
4567 * asks for it.
4568 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004569 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004570 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004571 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4572 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4573 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004574 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004575 /* The header is set to be added only if none is present
4576 * and we found it, so don't do anything.
4577 */
4578 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004579 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004580 /* Add an X-Forwarded-For header unless the source IP is
4581 * in the 'except' network range.
4582 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004583 if ((!sess->fe->except_mask.s_addr ||
4584 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4585 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004586 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004587 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004588 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004589 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004590 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004591 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004592
4593 /* Note: we rely on the backend to get the header name to be used for
4594 * x-forwarded-for, because the header is really meant for the backends.
4595 * However, if the backend did not specify any option, we have to rely
4596 * on the frontend's header name.
4597 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004598 if (s->be->fwdfor_hdr_len) {
4599 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004600 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004601 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004602 len = sess->fe->fwdfor_hdr_len;
4603 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004604 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004605 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 +01004606
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004607 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004608 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004609 }
4610 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004611 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004612 /* FIXME: for the sake of completeness, we should also support
4613 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004614 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004615 int len;
4616 char pn[INET6_ADDRSTRLEN];
4617 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004618 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004619 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004620
Willy Tarreau59234e92008-11-30 23:51:27 +01004621 /* Note: we rely on the backend to get the header name to be used for
4622 * x-forwarded-for, because the header is really meant for the backends.
4623 * However, if the backend did not specify any option, we have to rely
4624 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004625 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004626 if (s->be->fwdfor_hdr_len) {
4627 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004628 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004629 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004630 len = sess->fe->fwdfor_hdr_len;
4631 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004632 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004633 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004634
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004635 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004636 goto return_bad_req;
4637 }
4638 }
4639
4640 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004641 * 10: add X-Original-To if either the frontend or the backend
4642 * asks for it.
4643 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004644 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004645
4646 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004647 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004648 /* Add an X-Original-To header unless the destination IP is
4649 * in the 'except' network range.
4650 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004651 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004652
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004653 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004654 ((!sess->fe->except_mask_to.s_addr ||
4655 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4656 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004657 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004658 (((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 +02004659 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004660 int len;
4661 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004662 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004663
4664 /* Note: we rely on the backend to get the header name to be used for
4665 * x-original-to, because the header is really meant for the backends.
4666 * However, if the backend did not specify any option, we have to rely
4667 * on the frontend's header name.
4668 */
4669 if (s->be->orgto_hdr_len) {
4670 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004671 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004672 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004673 len = sess->fe->orgto_hdr_len;
4674 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004675 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004676 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 +02004677
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004678 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004679 goto return_bad_req;
4680 }
4681 }
4682 }
4683
Willy Tarreau50fc7772012-11-11 22:19:57 +01004684 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4685 * If an "Upgrade" token is found, the header is left untouched in order not to have
4686 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4687 * "Upgrade" is present in the Connection header.
4688 */
4689 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4690 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004691 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004692 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004693 unsigned int want_flags = 0;
4694
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004695 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004696 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004697 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004698 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004699 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004700 want_flags |= TX_CON_CLO_SET;
4701 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004702 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004703 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004704 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004705 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004706 want_flags |= TX_CON_KAL_SET;
4707 }
4708
4709 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004710 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004711 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004712
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004713
Willy Tarreau522d6c02009-12-06 18:49:18 +01004714 /* If we have no server assigned yet and we're balancing on url_param
4715 * with a POST request, we may be interested in checking the body for
4716 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004717 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004718 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004719 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004720 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004721 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004722 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004723 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004724
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004725 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004726 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004727#ifdef TCP_QUICKACK
4728 /* We expect some data from the client. Unless we know for sure
4729 * we already have a full request, we have to re-enable quick-ack
4730 * in case we previously disabled it, otherwise we might cause
4731 * the client to delay further data.
4732 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004733 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004734 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004735 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004736 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004737 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004738#endif
4739 }
Willy Tarreau03945942009-12-22 16:50:27 +01004740
Willy Tarreau59234e92008-11-30 23:51:27 +01004741 /*************************************************************
4742 * OK, that's finished for the headers. We have done what we *
4743 * could. Let's switch to the DATA state. *
4744 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004745 req->analyse_exp = TICK_ETERNITY;
4746 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004747
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004748 /* if the server closes the connection, we want to immediately react
4749 * and close the socket to save packets and syscalls.
4750 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004751 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004752 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004753
Willy Tarreau59234e92008-11-30 23:51:27 +01004754 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004755 /* OK let's go on with the BODY now */
4756 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004757
Willy Tarreau59234e92008-11-30 23:51:27 +01004758 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004759 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004760 /* we detected a parsing error. We want to archive this request
4761 * in the dedicated proxy area for later troubleshooting.
4762 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004763 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004764 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004765
Willy Tarreau59234e92008-11-30 23:51:27 +01004766 txn->req.msg_state = HTTP_MSG_ERROR;
4767 txn->status = 400;
4768 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004769 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004770
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004771 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004772 if (sess->listener->counters)
4773 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004774
Willy Tarreaue7dff022015-04-03 01:14:29 +02004775 if (!(s->flags & SF_ERR_MASK))
4776 s->flags |= SF_ERR_PRXCOND;
4777 if (!(s->flags & SF_FINST_MASK))
4778 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004779 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004780}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004781
Willy Tarreau60b85b02008-11-30 23:28:40 +01004782/* This function is an analyser which processes the HTTP tarpit. It always
4783 * returns zero, at the beginning because it prevents any other processing
4784 * from occurring, and at the end because it terminates the request.
4785 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004786int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004787{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004788 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004789
4790 /* This connection is being tarpitted. The CLIENT side has
4791 * already set the connect expiration date to the right
4792 * timeout. We just have to check that the client is still
4793 * there and that the timeout has not expired.
4794 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004795 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004796 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004797 !tick_is_expired(req->analyse_exp, now_ms))
4798 return 0;
4799
4800 /* We will set the queue timer to the time spent, just for
4801 * logging purposes. We fake a 500 server error, so that the
4802 * attacker will not suspect his connection has been tarpitted.
4803 * It will not cause trouble to the logs because we can exclude
4804 * the tarpitted connections by filtering on the 'PT' status flags.
4805 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004806 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4807
4808 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004809 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004810 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004811
4812 req->analysers = 0;
4813 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004814
Willy Tarreaue7dff022015-04-03 01:14:29 +02004815 if (!(s->flags & SF_ERR_MASK))
4816 s->flags |= SF_ERR_PRXCOND;
4817 if (!(s->flags & SF_FINST_MASK))
4818 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004819 return 0;
4820}
4821
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004822/* This function is an analyser which waits for the HTTP request body. It waits
4823 * for either the buffer to be full, or the full advertised contents to have
4824 * reached the buffer. It must only be called after the standard HTTP request
4825 * processing has occurred, because it expects the request to be parsed and will
4826 * look for the Expect header. It may send a 100-Continue interim response. It
4827 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4828 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4829 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004830 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004831int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004832{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004833 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004834 struct http_txn *txn = s->txn;
4835 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004836
4837 /* We have to parse the HTTP request body to find any required data.
4838 * "balance url_param check_post" should have been the only way to get
4839 * into this. We were brought here after HTTP header analysis, so all
4840 * related structures are ready.
4841 */
4842
Willy Tarreau890988f2014-04-10 11:59:33 +02004843 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4844 /* This is the first call */
4845 if (msg->msg_state < HTTP_MSG_BODY)
4846 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004847
Willy Tarreau890988f2014-04-10 11:59:33 +02004848 if (msg->msg_state < HTTP_MSG_100_SENT) {
4849 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4850 * send an HTTP/1.1 100 Continue intermediate response.
4851 */
4852 if (msg->flags & HTTP_MSGF_VER_11) {
4853 struct hdr_ctx ctx;
4854 ctx.idx = 0;
4855 /* Expect is allowed in 1.1, look for it */
4856 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4857 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004858 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004859 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004860 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004861 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004862 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004863
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004864 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004865 * req->buf->p still points to the beginning of the message. We
4866 * must save the body in msg->next because it survives buffer
4867 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004868 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004869 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004870
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004871 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004872 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4873 else
4874 msg->msg_state = HTTP_MSG_DATA;
4875 }
4876
Willy Tarreau890988f2014-04-10 11:59:33 +02004877 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4878 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004879 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004880 goto missing_data;
4881
4882 /* OK we have everything we need now */
4883 goto http_end;
4884 }
4885
4886 /* OK here we're parsing a chunked-encoded message */
4887
Willy Tarreau522d6c02009-12-06 18:49:18 +01004888 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004889 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004890 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004891 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004892 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004893 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004894
Willy Tarreau115acb92009-12-26 13:56:06 +01004895 if (!ret)
4896 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004897 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004898 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004899 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004900 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004901 }
4902
Willy Tarreaud98cf932009-12-27 22:54:55 +01004903 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004904 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4905 * for at least a whole chunk or the whole content length bytes after
4906 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004907 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004908 if (msg->msg_state == HTTP_MSG_TRAILERS)
4909 goto http_end;
4910
Willy Tarreaue115b492015-05-01 23:05:14 +02004911 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004912 goto http_end;
4913
4914 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004915 /* we get here if we need to wait for more data. If the buffer is full,
4916 * we have the maximum we can expect.
4917 */
4918 if (buffer_full(req->buf, global.tune.maxrewrite))
4919 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004920
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004921 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004922 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004923 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004924
Willy Tarreaue7dff022015-04-03 01:14:29 +02004925 if (!(s->flags & SF_ERR_MASK))
4926 s->flags |= SF_ERR_CLITO;
4927 if (!(s->flags & SF_FINST_MASK))
4928 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004929 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004930 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004931
4932 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004933 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004934 /* Not enough data. We'll re-use the http-request
4935 * timeout here. Ideally, we should set the timeout
4936 * relative to the accept() date. We just set the
4937 * request timeout once at the beginning of the
4938 * request.
4939 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004940 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004941 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004942 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004943 return 0;
4944 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004945
4946 http_end:
4947 /* The situation will not evolve, so let's give up on the analysis. */
4948 s->logs.tv_request = now; /* update the request timer to reflect full request */
4949 req->analysers &= ~an_bit;
4950 req->analyse_exp = TICK_ETERNITY;
4951 return 1;
4952
4953 return_bad_req: /* let's centralize all bad requests */
4954 txn->req.msg_state = HTTP_MSG_ERROR;
4955 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004956 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004957
Willy Tarreaue7dff022015-04-03 01:14:29 +02004958 if (!(s->flags & SF_ERR_MASK))
4959 s->flags |= SF_ERR_PRXCOND;
4960 if (!(s->flags & SF_FINST_MASK))
4961 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004962
Willy Tarreau522d6c02009-12-06 18:49:18 +01004963 return_err_msg:
4964 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004965 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004966 if (sess->listener->counters)
4967 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004968 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004969}
4970
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004971/* send a server's name with an outgoing request over an established connection.
4972 * Note: this function is designed to be called once the request has been scheduled
4973 * for being forwarded. This is the reason why it rewinds the buffer before
4974 * proceeding.
4975 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004976int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004977
4978 struct hdr_ctx ctx;
4979
Mark Lamourinec2247f02012-01-04 13:02:01 -05004980 char *hdr_name = be->server_id_hdr_name;
4981 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004982 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004983 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004984 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004985
William Lallemandd9e90662012-01-30 17:27:17 +01004986 ctx.idx = 0;
4987
Willy Tarreau211cdec2014-04-17 20:18:08 +02004988 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004989 if (old_o) {
4990 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004991 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004992 txn->req.next += old_o;
4993 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004994 }
4995
Willy Tarreau9b28e032012-10-12 23:49:43 +02004996 old_i = chn->buf->i;
4997 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 -05004998 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004999 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005000 }
5001
5002 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005003 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005004 memcpy(hdr_val, hdr_name, hdr_name_len);
5005 hdr_val += hdr_name_len;
5006 *hdr_val++ = ':';
5007 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005008 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5009 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005010
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005011 if (old_o) {
5012 /* If this was a forwarded request, we must readjust the amount of
5013 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005014 * variations. Note that the current state is >= HTTP_MSG_BODY,
5015 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005016 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005017 old_o += chn->buf->i - old_i;
5018 b_adv(chn->buf, old_o);
5019 txn->req.next -= old_o;
5020 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005021 }
5022
Mark Lamourinec2247f02012-01-04 13:02:01 -05005023 return 0;
5024}
5025
Willy Tarreau610ecce2010-01-04 21:15:02 +01005026/* Terminate current transaction and prepare a new one. This is very tricky
5027 * right now but it works.
5028 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005029void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005030{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005031 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005032 struct proxy *fe = strm_fe(s);
Willy Tarreau068621e2013-12-23 15:11:25 +01005033
Willy Tarreau610ecce2010-01-04 21:15:02 +01005034 /* FIXME: We need a more portable way of releasing a backend's and a
5035 * server's connections. We need a safer way to reinitialize buffer
5036 * flags. We also need a more accurate method for computing per-request
5037 * data.
5038 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005039
Willy Tarreau4213a112013-12-15 10:25:42 +01005040 /* unless we're doing keep-alive, we want to quickly close the connection
5041 * to the server.
5042 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005043 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005044 !si_conn_ready(&s->si[1])) {
5045 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5046 si_shutr(&s->si[1]);
5047 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005048 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005049
Willy Tarreaue7dff022015-04-03 01:14:29 +02005050 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005051 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005052 if (unlikely(s->srv_conn))
5053 sess_change_server(s, NULL);
5054 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005055
5056 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005057 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005058
Willy Tarreaueee5b512015-04-03 23:46:31 +02005059 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005060 int n;
5061
Willy Tarreaueee5b512015-04-03 23:46:31 +02005062 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005063 if (n < 1 || n > 5)
5064 n = 0;
5065
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005066 if (fe->mode == PR_MODE_HTTP) {
5067 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005068 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005069 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005070 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005071 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005072 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005073 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005074 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005075 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005076 s->be->be_counters.p.http.comp_rsp++;
5077 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078 }
5079
5080 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005081 s->logs.bytes_in -= s->req.buf->i;
5082 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005083
5084 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005085 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005086 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005087 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005088 s->do_log(s);
5089 }
5090
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005091 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005092 stream_stop_content_counters(s);
5093 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005094
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 s->logs.accept_date = date; /* user-visible date for logging */
5096 s->logs.tv_accept = now; /* corrected date for internal use */
5097 tv_zero(&s->logs.tv_request);
5098 s->logs.t_queue = -1;
5099 s->logs.t_connect = -1;
5100 s->logs.t_data = -1;
5101 s->logs.t_close = 0;
5102 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5103 s->logs.srv_queue_size = 0; /* we will get this number soon */
5104
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005105 s->logs.bytes_in = s->req.total = s->req.buf->i;
5106 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005107
5108 if (s->pend_pos)
5109 pendconn_free(s->pend_pos);
5110
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005111 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005112 if (s->flags & SF_CURR_SESS) {
5113 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005114 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005115 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005116 if (may_dequeue_tasks(objt_server(s->target), s->be))
5117 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005118 }
5119
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005120 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121
Willy Tarreau4213a112013-12-15 10:25:42 +01005122 /* only release our endpoint if we don't intend to reuse the
5123 * connection.
5124 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005125 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005126 !si_conn_ready(&s->si[1])) {
5127 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005128 }
5129
Willy Tarreau350f4872014-11-28 14:42:25 +01005130 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5131 s->si[1].err_type = SI_ET_NONE;
5132 s->si[1].conn_retries = 0; /* used for logging too */
5133 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005134 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 +01005135 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);
5136 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 +02005137 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5138 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5139 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005140
Willy Tarreaueee5b512015-04-03 23:46:31 +02005141 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005142 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005143 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005144
5145 if (prev_status == 401 || prev_status == 407) {
5146 /* In HTTP keep-alive mode, if we receive a 401, we still have
5147 * a chance of being able to send the visitor again to the same
5148 * server over the same connection. This is required by some
5149 * broken protocols such as NTLM, and anyway whenever there is
5150 * an opportunity for sending the challenge to the proper place,
5151 * it's better to do it (at least it helps with debugging).
5152 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005153 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01005154 }
5155
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005156 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005157 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005159 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005160 s->req.flags |= CF_NEVER_WAIT;
5161 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005162 }
5163
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164 /* if the request buffer is not empty, it means we're
5165 * about to process another request, so send pending
5166 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005167 * Just don't do this if the buffer is close to be full,
5168 * because the request will wait for it to flush a little
5169 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005170 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005171 if (s->req.buf->i) {
5172 if (s->res.buf->o &&
5173 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5174 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5175 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005176 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005177
5178 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005179 channel_auto_read(&s->req);
5180 channel_auto_close(&s->req);
5181 channel_auto_read(&s->res);
5182 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005183
Willy Tarreau27375622013-12-17 00:00:28 +01005184 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005185 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005186
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005187 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005188 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189}
5190
5191
5192/* This function updates the request state machine according to the response
5193 * state machine and buffer flags. It returns 1 if it changes anything (flag
5194 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5195 * it is only used to find when a request/response couple is complete. Both
5196 * this function and its equivalent should loop until both return zero. It
5197 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5198 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005199int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005201 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005202 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005203 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005204 unsigned int old_state = txn->req.msg_state;
5205
Willy Tarreau610ecce2010-01-04 21:15:02 +01005206 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5207 return 0;
5208
5209 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005210 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005211 * We can shut the read side unless we want to abort_on_close,
5212 * or we have a POST request. The issue with POST requests is
5213 * that some browsers still send a CRLF after the request, and
5214 * this CRLF must be read so that it does not remain in the kernel
5215 * buffers, otherwise a close could cause an RST on some systems
5216 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005217 * Note that if we're using keep-alive on the client side, we'd
5218 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005219 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005220 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005221 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005222 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5223 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5224 !(s->be->options & PR_O_ABRT_CLOSE) &&
5225 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005226 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005227
Willy Tarreau40f151a2012-12-20 12:10:09 +01005228 /* if the server closes the connection, we want to immediately react
5229 * and close the socket to save packets and syscalls.
5230 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005231 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005232
Willy Tarreau610ecce2010-01-04 21:15:02 +01005233 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5234 goto wait_other_side;
5235
5236 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5237 /* The server has not finished to respond, so we
5238 * don't want to move in order not to upset it.
5239 */
5240 goto wait_other_side;
5241 }
5242
5243 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5244 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005245 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005246 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005247 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 goto wait_other_side;
5249 }
5250
5251 /* When we get here, it means that both the request and the
5252 * response have finished receiving. Depending on the connection
5253 * mode, we'll have to wait for the last bytes to leave in either
5254 * direction, and sometimes for a close to be effective.
5255 */
5256
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005257 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5258 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005259 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5260 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005261 }
5262 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5263 /* Option forceclose is set, or either side wants to close,
5264 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005265 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005266 * once both states are CLOSED.
5267 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005268 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5269 channel_shutr_now(chn);
5270 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005271 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005272 }
5273 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005274 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5275 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005276 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005277 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5278 channel_auto_read(chn);
5279 txn->req.msg_state = HTTP_MSG_TUNNEL;
5280 chn->flags |= CF_NEVER_WAIT;
5281 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005282 }
5283
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005284 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005285 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005286 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005287
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005288 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289 txn->req.msg_state = HTTP_MSG_CLOSING;
5290 goto http_msg_closing;
5291 }
5292 else {
5293 txn->req.msg_state = HTTP_MSG_CLOSED;
5294 goto http_msg_closed;
5295 }
5296 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005297 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005298 }
5299
5300 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5301 http_msg_closing:
5302 /* nothing else to forward, just waiting for the output buffer
5303 * to be empty and for the shutw_now to take effect.
5304 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005305 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005306 txn->req.msg_state = HTTP_MSG_CLOSED;
5307 goto http_msg_closed;
5308 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005309 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005310 txn->req.msg_state = HTTP_MSG_ERROR;
5311 goto wait_other_side;
5312 }
5313 }
5314
5315 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5316 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005317 /* see above in MSG_DONE why we only do this in these states */
5318 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5319 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5320 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005321 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005322 goto wait_other_side;
5323 }
5324
5325 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005326 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005327}
5328
5329
5330/* This function updates the response state machine according to the request
5331 * state machine and buffer flags. It returns 1 if it changes anything (flag
5332 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5333 * it is only used to find when a request/response couple is complete. Both
5334 * this function and its equivalent should loop until both return zero. It
5335 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5336 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005337int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005338{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005339 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005340 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005341 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342 unsigned int old_state = txn->rsp.msg_state;
5343
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5345 return 0;
5346
5347 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5348 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005349 * still monitor the server connection for a possible close
5350 * while the request is being uploaded, so we don't disable
5351 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005352 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005353 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005354
5355 if (txn->req.msg_state == HTTP_MSG_ERROR)
5356 goto wait_other_side;
5357
5358 if (txn->req.msg_state < HTTP_MSG_DONE) {
5359 /* The client seems to still be sending data, probably
5360 * because we got an error response during an upload.
5361 * We have the choice of either breaking the connection
5362 * or letting it pass through. Let's do the later.
5363 */
5364 goto wait_other_side;
5365 }
5366
5367 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5368 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005369 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005370 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005371 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005372 goto wait_other_side;
5373 }
5374
5375 /* When we get here, it means that both the request and the
5376 * response have finished receiving. Depending on the connection
5377 * mode, we'll have to wait for the last bytes to leave in either
5378 * direction, and sometimes for a close to be effective.
5379 */
5380
5381 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5382 /* Server-close mode : shut read and wait for the request
5383 * side to close its output buffer. The caller will detect
5384 * when we're in DONE and the other is in CLOSED and will
5385 * catch that for the final cleanup.
5386 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005387 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5388 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005389 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005390 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5391 /* Option forceclose is set, or either side wants to close,
5392 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005393 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005394 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005395 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005396 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5397 channel_shutr_now(chn);
5398 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005399 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005400 }
5401 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005402 /* The last possible modes are keep-alive and tunnel. Tunnel will
5403 * need to forward remaining data. Keep-alive will need to monitor
5404 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005406 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005407 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005408 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5409 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005410 }
5411
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005412 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005413 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005414 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005415 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5416 goto http_msg_closing;
5417 }
5418 else {
5419 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5420 goto http_msg_closed;
5421 }
5422 }
5423 goto wait_other_side;
5424 }
5425
5426 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5427 http_msg_closing:
5428 /* nothing else to forward, just waiting for the output buffer
5429 * to be empty and for the shutw_now to take effect.
5430 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005431 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005432 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5433 goto http_msg_closed;
5434 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005435 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005436 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005437 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005438 if (objt_server(s->target))
5439 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005440 goto wait_other_side;
5441 }
5442 }
5443
5444 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5445 http_msg_closed:
5446 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005447 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005448 channel_auto_close(chn);
5449 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005450 goto wait_other_side;
5451 }
5452
5453 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005454 /* We force the response to leave immediately if we're waiting for the
5455 * other side, since there is no pending shutdown to push it out.
5456 */
5457 if (!channel_is_empty(chn))
5458 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005459 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005460}
5461
5462
5463/* Resync the request and response state machines. Return 1 if either state
5464 * changes.
5465 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005466int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005467{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005468 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 int old_req_state = txn->req.msg_state;
5470 int old_res_state = txn->rsp.msg_state;
5471
Willy Tarreau610ecce2010-01-04 21:15:02 +01005472 http_sync_req_state(s);
5473 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005474 if (!http_sync_res_state(s))
5475 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005476 if (!http_sync_req_state(s))
5477 break;
5478 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005479
Willy Tarreau610ecce2010-01-04 21:15:02 +01005480 /* OK, both state machines agree on a compatible state.
5481 * There are a few cases we're interested in :
5482 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5483 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5484 * directions, so let's simply disable both analysers.
5485 * - HTTP_MSG_CLOSED on the response only means we must abort the
5486 * request.
5487 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5488 * with server-close mode means we've completed one request and we
5489 * must re-initialize the server connection.
5490 */
5491
5492 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5493 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5494 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5495 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005496 s->req.analysers = 0;
5497 channel_auto_close(&s->req);
5498 channel_auto_read(&s->req);
5499 s->res.analysers = 0;
5500 channel_auto_close(&s->res);
5501 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005502 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005503 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005504 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005505 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005506 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005507 s->res.analysers = 0;
5508 channel_auto_close(&s->res);
5509 channel_auto_read(&s->res);
5510 s->req.analysers = 0;
5511 channel_abort(&s->req);
5512 channel_auto_close(&s->req);
5513 channel_auto_read(&s->req);
5514 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005515 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005516 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5517 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005518 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005519 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5520 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5521 /* server-close/keep-alive: terminate this transaction,
5522 * possibly killing the server connection and reinitialize
5523 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005524 */
5525 http_end_txn_clean_session(s);
5526 }
5527
Willy Tarreau610ecce2010-01-04 21:15:02 +01005528 return txn->req.msg_state != old_req_state ||
5529 txn->rsp.msg_state != old_res_state;
5530}
5531
Willy Tarreaud98cf932009-12-27 22:54:55 +01005532/* This function is an analyser which forwards request body (including chunk
5533 * sizes if any). It is called as soon as we must forward, even if we forward
5534 * zero byte. The only situation where it must not be called is when we're in
5535 * tunnel mode and we want to forward till the close. It's used both to forward
5536 * remaining data and to resync after end of body. It expects the msg_state to
5537 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005538 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005539 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005540 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005541 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005542int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005544 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005545 struct http_txn *txn = s->txn;
5546 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005547
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005548 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5549 return 0;
5550
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005551 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005552 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005553 /* Output closed while we were sending data. We must abort and
5554 * wake the other side up.
5555 */
5556 msg->msg_state = HTTP_MSG_ERROR;
5557 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005558 return 1;
5559 }
5560
Willy Tarreaud98cf932009-12-27 22:54:55 +01005561 /* Note that we don't have to send 100-continue back because we don't
5562 * need the data to complete our job, and it's up to the server to
5563 * decide whether to return 100, 417 or anything else in return of
5564 * an "Expect: 100-continue" header.
5565 */
5566
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005567 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005568 /* we have msg->sov which points to the first byte of message
5569 * body, and req->buf.p still points to the beginning of the
5570 * message. We forward the headers now, as we don't need them
5571 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005572 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005573 b_adv(req->buf, msg->sov);
5574 msg->next -= msg->sov;
5575 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005576
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005577 /* The previous analysers guarantee that the state is somewhere
5578 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5579 * msg->next are always correct.
5580 */
5581 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5582 if (msg->flags & HTTP_MSGF_TE_CHNK)
5583 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5584 else
5585 msg->msg_state = HTTP_MSG_DATA;
5586 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005587 }
5588
Willy Tarreau7ba23542014-04-17 21:50:00 +02005589 /* Some post-connect processing might want us to refrain from starting to
5590 * forward data. Currently, the only reason for this is "balance url_param"
5591 * whichs need to parse/process the request after we've enabled forwarding.
5592 */
5593 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005594 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005595 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005596 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005597 goto missing_data;
5598 }
5599 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5600 }
5601
Willy Tarreau80a92c02014-03-12 10:41:13 +01005602 /* in most states, we should abort in case of early close */
5603 channel_auto_close(req);
5604
Willy Tarreauefdf0942014-04-24 20:08:57 +02005605 if (req->to_forward) {
5606 /* We can't process the buffer's contents yet */
5607 req->flags |= CF_WAKE_WRITE;
5608 goto missing_data;
5609 }
5610
Willy Tarreaud98cf932009-12-27 22:54:55 +01005611 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005612 if (msg->msg_state == HTTP_MSG_DATA) {
5613 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005614 /* we may have some pending data starting at req->buf->p */
5615 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005616 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005617 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005618 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005619 msg->next += msg->chunk_len;
5620 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005621
5622 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005623 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005624 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005625 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005626 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005627 }
5628 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005629 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005630 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005631 * TRAILERS state.
5632 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005633 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005634
Willy Tarreau54d23df2012-10-25 19:04:45 +02005635 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005636 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005637 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005638 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005639 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005640 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005641 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005642 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005643 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005644 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005645 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005646 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005647 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005648
5649 if (ret == 0)
5650 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005651 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005652 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005653 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005654 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005655 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005656 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005657 /* we're in MSG_CHUNK_SIZE now */
5658 }
5659 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005660 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005661
5662 if (ret == 0)
5663 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005664 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005665 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005666 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005667 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005668 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005669 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005670 /* we're in HTTP_MSG_DONE now */
5671 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005672 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005673 int old_state = msg->msg_state;
5674
Willy Tarreau610ecce2010-01-04 21:15:02 +01005675 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005676
5677 /* we may have some pending data starting at req->buf->p
5678 * such as last chunk of data or trailers.
5679 */
5680 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005681 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005682 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005683 msg->next = 0;
5684
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005685 /* we don't want to forward closes on DONE except in
5686 * tunnel mode.
5687 */
5688 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005689 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005690 if (http_resync_states(s)) {
5691 /* some state changes occurred, maybe the analyser
5692 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005693 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005694 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005695 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005696 /* request errors are most likely due to
5697 * the server aborting the transfer.
5698 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005699 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005700 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005701 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005702 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005703 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005704 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005705 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005706 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005707
5708 /* If "option abortonclose" is set on the backend, we
5709 * want to monitor the client's connection and forward
5710 * any shutdown notification to the server, which will
5711 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005712 * request. We only do that in tunnel mode, and not in
5713 * other modes since it can be abused to exhaust source
5714 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005715 */
5716 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005717 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005718 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5719 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5720 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005721 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005722 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005723 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005724 /* POST requests may require to read extra CRLF
5725 * sent by broken browsers and which could cause
5726 * an RST to be sent upon close on some systems
5727 * (eg: Linux).
5728 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005729 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005730 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005731
Willy Tarreau610ecce2010-01-04 21:15:02 +01005732 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005733 }
5734 }
5735
Willy Tarreaud98cf932009-12-27 22:54:55 +01005736 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005737 /* we may have some pending data starting at req->buf->p */
5738 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005739 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005740 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5741
Willy Tarreaubed410e2014-04-22 08:19:34 +02005742 msg->next = 0;
5743 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5744
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005745 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005746 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005747 if (!(s->flags & SF_ERR_MASK))
5748 s->flags |= SF_ERR_CLICL;
5749 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005750 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005751 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005752 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005753 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005754 }
5755
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005756 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005757 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005758 if (objt_server(s->target))
5759 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005760
5761 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005762 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005763
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005764 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005765 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005766 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005767
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005768 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005769 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005770 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005771 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005772 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005773
Willy Tarreau5c620922011-05-11 19:56:11 +02005774 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005775 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005776 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005777 * modes are already handled by the stream sock layer. We must not do
5778 * this in content-length mode because it could present the MSG_MORE
5779 * flag with the last block of forwarded data, which would cause an
5780 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005781 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005782 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005783 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005784
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005785 return 0;
5786
Willy Tarreaud98cf932009-12-27 22:54:55 +01005787 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005788 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005789 if (sess->listener->counters)
5790 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005791
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005792 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005793 /* we may have some pending data starting at req->buf->p */
5794 b_adv(req->buf, msg->next);
5795 msg->next = 0;
5796
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005797 txn->req.msg_state = HTTP_MSG_ERROR;
5798 if (txn->status) {
5799 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005800 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005801 } else {
5802 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005803 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005804 }
5805 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005806 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005807
Willy Tarreaue7dff022015-04-03 01:14:29 +02005808 if (!(s->flags & SF_ERR_MASK))
5809 s->flags |= SF_ERR_PRXCOND;
5810 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005811 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005812 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005813 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005814 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005815 }
5816 return 0;
5817
5818 aborted_xfer:
5819 txn->req.msg_state = HTTP_MSG_ERROR;
5820 if (txn->status) {
5821 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005822 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005823 } else {
5824 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005825 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005826 }
5827 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005828 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005829
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005830 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005831 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005832 if (objt_server(s->target))
5833 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005834
Willy Tarreaue7dff022015-04-03 01:14:29 +02005835 if (!(s->flags & SF_ERR_MASK))
5836 s->flags |= SF_ERR_SRVCL;
5837 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005838 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005839 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005840 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005841 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005842 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005843 return 0;
5844}
5845
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005846/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5847 * processing can continue on next analysers, or zero if it either needs more
5848 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005849 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005850 * when it has nothing left to do, and may remove any analyser when it wants to
5851 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005852 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005853int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005854{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005855 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005856 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005857 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005858 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005859 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005860 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005861 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005862
Willy Tarreau87b09662015-04-03 00:22:06 +02005863 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 +02005864 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005866 rep,
5867 rep->rex, rep->wex,
5868 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005869 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005870 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005871
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005872 /*
5873 * Now parse the partial (or complete) lines.
5874 * We will check the response syntax, and also join multi-line
5875 * headers. An index of all the lines will be elaborated while
5876 * parsing.
5877 *
5878 * For the parsing, we use a 28 states FSM.
5879 *
5880 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005881 * rep->buf->p = beginning of response
5882 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5883 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005884 * msg->eol = end of current header or line (LF or CRLF)
5885 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005886 */
5887
Willy Tarreau628c40c2014-04-24 19:11:26 +02005888 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005889 /* There's a protected area at the end of the buffer for rewriting
5890 * purposes. We don't want to start to parse the request if the
5891 * protected area is affected, because we may have to move processed
5892 * data later, which is much more complicated.
5893 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005894 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005895 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005896 /* some data has still not left the buffer, wake us once that's done */
5897 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5898 goto abort_response;
5899 channel_dont_close(rep);
5900 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005901 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005902 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005903 }
5904
Willy Tarreau379357a2013-06-08 12:55:46 +02005905 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5906 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5907 buffer_slow_realign(rep->buf);
5908
Willy Tarreau9b28e032012-10-12 23:49:43 +02005909 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005910 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005911 }
5912
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005913 /* 1: we might have to print this header in debug mode */
5914 if (unlikely((global.mode & MODE_DEBUG) &&
5915 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005916 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005917 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005918
Willy Tarreau9b28e032012-10-12 23:49:43 +02005919 sol = rep->buf->p;
5920 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005921 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005922
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005923 sol += hdr_idx_first_pos(&txn->hdr_idx);
5924 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005925
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005926 while (cur_idx) {
5927 eol = sol + txn->hdr_idx.v[cur_idx].len;
5928 debug_hdr("srvhdr", s, sol, eol);
5929 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5930 cur_idx = txn->hdr_idx.v[cur_idx].next;
5931 }
5932 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005933
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005934 /*
5935 * Now we quickly check if we have found a full valid response.
5936 * If not so, we check the FD and buffer states before leaving.
5937 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005938 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005939 * responses are checked first.
5940 *
5941 * Depending on whether the client is still there or not, we
5942 * may send an error response back or not. Note that normally
5943 * we should only check for HTTP status there, and check I/O
5944 * errors somewhere else.
5945 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005946
Willy Tarreau655dce92009-11-08 13:10:58 +01005947 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005948 /* Invalid response */
5949 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5950 /* we detected a parsing error. We want to archive this response
5951 * in the dedicated proxy area for later troubleshooting.
5952 */
5953 hdr_response_bad:
5954 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005955 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005956
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005957 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005958 if (objt_server(s->target)) {
5959 objt_server(s->target)->counters.failed_resp++;
5960 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005961 }
Willy Tarreau64648412010-03-05 10:41:54 +01005962 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005963 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005964 rep->analysers = 0;
5965 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005966 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005967 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005968 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005969
Willy Tarreaue7dff022015-04-03 01:14:29 +02005970 if (!(s->flags & SF_ERR_MASK))
5971 s->flags |= SF_ERR_PRXCOND;
5972 if (!(s->flags & SF_FINST_MASK))
5973 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005974
5975 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005976 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005977
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005978 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005979 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005980 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005981 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982 goto hdr_response_bad;
5983 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005984
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005986 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005987 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005988 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005989 else if (txn->flags & TX_NOT_FIRST)
5990 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005991
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005992 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005993 if (objt_server(s->target)) {
5994 objt_server(s->target)->counters.failed_resp++;
5995 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005996 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005997
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005998 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005999 rep->analysers = 0;
6000 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006001 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006002 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006003 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006004
Willy Tarreaue7dff022015-04-03 01:14:29 +02006005 if (!(s->flags & SF_ERR_MASK))
6006 s->flags |= SF_ERR_SRVCL;
6007 if (!(s->flags & SF_FINST_MASK))
6008 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006009 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006010 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006011
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006012 /* read timeout : return a 504 to the client. */
6013 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006014 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006015 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006016 else if (txn->flags & TX_NOT_FIRST)
6017 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006018
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006019 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006020 if (objt_server(s->target)) {
6021 objt_server(s->target)->counters.failed_resp++;
6022 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006023 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006024
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006025 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006026 rep->analysers = 0;
6027 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006028 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006029 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006030 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006031
Willy Tarreaue7dff022015-04-03 01:14:29 +02006032 if (!(s->flags & SF_ERR_MASK))
6033 s->flags |= SF_ERR_SRVTO;
6034 if (!(s->flags & SF_FINST_MASK))
6035 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006036 return 0;
6037 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006038
Willy Tarreauf003d372012-11-26 13:35:37 +01006039 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006040 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006041 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006042 s->be->be_counters.cli_aborts++;
6043 if (objt_server(s->target))
6044 objt_server(s->target)->counters.cli_aborts++;
6045
6046 rep->analysers = 0;
6047 channel_auto_close(rep);
6048
6049 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006050 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006051 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006052
Willy Tarreaue7dff022015-04-03 01:14:29 +02006053 if (!(s->flags & SF_ERR_MASK))
6054 s->flags |= SF_ERR_CLICL;
6055 if (!(s->flags & SF_FINST_MASK))
6056 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006057
Willy Tarreau87b09662015-04-03 00:22:06 +02006058 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006059 return 0;
6060 }
6061
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006062 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006063 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006064 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006065 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006066 else if (txn->flags & TX_NOT_FIRST)
6067 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006068
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006069 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006070 if (objt_server(s->target)) {
6071 objt_server(s->target)->counters.failed_resp++;
6072 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006073 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006074
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006075 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006076 rep->analysers = 0;
6077 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006078 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006079 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006080 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006081
Willy Tarreaue7dff022015-04-03 01:14:29 +02006082 if (!(s->flags & SF_ERR_MASK))
6083 s->flags |= SF_ERR_SRVCL;
6084 if (!(s->flags & SF_FINST_MASK))
6085 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006086 return 0;
6087 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006088
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006089 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006090 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006091 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006092 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006093 else if (txn->flags & TX_NOT_FIRST)
6094 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006095
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006096 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006097 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006098 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006099
Willy Tarreaue7dff022015-04-03 01:14:29 +02006100 if (!(s->flags & SF_ERR_MASK))
6101 s->flags |= SF_ERR_CLICL;
6102 if (!(s->flags & SF_FINST_MASK))
6103 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006104
Willy Tarreau87b09662015-04-03 00:22:06 +02006105 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006106 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006107 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006108
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006109 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006110 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006111 return 0;
6112 }
6113
6114 /* More interesting part now : we know that we have a complete
6115 * response which at least looks like HTTP. We have an indicator
6116 * of each header's length, so we can parse them quickly.
6117 */
6118
6119 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006120 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006121
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006122 /*
6123 * 1: get the status code
6124 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006125 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006126 if (n < 1 || n > 5)
6127 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006128 /* when the client triggers a 4xx from the server, it's most often due
6129 * to a missing object or permission. These events should be tracked
6130 * because if they happen often, it may indicate a brute force or a
6131 * vulnerability scan.
6132 */
6133 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006134 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006135
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006136 if (objt_server(s->target))
6137 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006138
Willy Tarreau91852eb2015-05-01 13:26:00 +02006139 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6140 * exactly one digit "." one digit. This check may be disabled using
6141 * option accept-invalid-http-response.
6142 */
6143 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6144 if (msg->sl.st.v_l != 8) {
6145 msg->err_pos = 0;
6146 goto hdr_response_bad;
6147 }
6148
6149 if (rep->buf->p[4] != '/' ||
6150 !isdigit((unsigned char)rep->buf->p[5]) ||
6151 rep->buf->p[6] != '.' ||
6152 !isdigit((unsigned char)rep->buf->p[7])) {
6153 msg->err_pos = 4;
6154 goto hdr_response_bad;
6155 }
6156 }
6157
Willy Tarreau5b154472009-12-21 20:11:07 +01006158 /* check if the response is HTTP/1.1 or above */
6159 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006160 ((rep->buf->p[5] > '1') ||
6161 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006162 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006163
6164 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006165 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 +01006166
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006167 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006168 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006169
Willy Tarreau9b28e032012-10-12 23:49:43 +02006170 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006171
Willy Tarreau39650402010-03-15 19:44:39 +01006172 /* Adjust server's health based on status code. Note: status codes 501
6173 * and 505 are triggered on demand by client request, so we must not
6174 * count them as server failures.
6175 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006176 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006177 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006178 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006179 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006180 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006181 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006182
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006183 /*
6184 * 2: check for cacheability.
6185 */
6186
6187 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006188 case 100:
6189 /*
6190 * We may be facing a 100-continue response, in which case this
6191 * is not the right response, and we're waiting for the next one.
6192 * Let's allow this response to go to the client and wait for the
6193 * next one.
6194 */
6195 hdr_idx_init(&txn->hdr_idx);
6196 msg->next -= channel_forward(rep, msg->next);
6197 msg->msg_state = HTTP_MSG_RPBEFORE;
6198 txn->status = 0;
6199 s->logs.t_data = -1; /* was not a response yet */
6200 goto next_one;
6201
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006202 case 200:
6203 case 203:
6204 case 206:
6205 case 300:
6206 case 301:
6207 case 410:
6208 /* RFC2616 @13.4:
6209 * "A response received with a status code of
6210 * 200, 203, 206, 300, 301 or 410 MAY be stored
6211 * by a cache (...) unless a cache-control
6212 * directive prohibits caching."
6213 *
6214 * RFC2616 @9.5: POST method :
6215 * "Responses to this method are not cacheable,
6216 * unless the response includes appropriate
6217 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006218 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006219 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006220 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006221 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6222 break;
6223 default:
6224 break;
6225 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006226
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006227 /*
6228 * 3: we may need to capture headers
6229 */
6230 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006231 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006232 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006233 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006234
Willy Tarreau557f1992015-05-01 10:05:17 +02006235 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6236 * by RFC7230#3.3.3 :
6237 *
6238 * The length of a message body is determined by one of the following
6239 * (in order of precedence):
6240 *
6241 * 1. Any response to a HEAD request and any response with a 1xx
6242 * (Informational), 204 (No Content), or 304 (Not Modified) status
6243 * code is always terminated by the first empty line after the
6244 * header fields, regardless of the header fields present in the
6245 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006246 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006247 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6248 * the connection will become a tunnel immediately after the empty
6249 * line that concludes the header fields. A client MUST ignore any
6250 * Content-Length or Transfer-Encoding header fields received in
6251 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006252 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006253 * 3. If a Transfer-Encoding header field is present and the chunked
6254 * transfer coding (Section 4.1) is the final encoding, the message
6255 * body length is determined by reading and decoding the chunked
6256 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006257 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006258 * If a Transfer-Encoding header field is present in a response and
6259 * the chunked transfer coding is not the final encoding, the
6260 * message body length is determined by reading the connection until
6261 * it is closed by the server. If a Transfer-Encoding header field
6262 * is present in a request and the chunked transfer coding is not
6263 * the final encoding, the message body length cannot be determined
6264 * reliably; the server MUST respond with the 400 (Bad Request)
6265 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006266 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006267 * If a message is received with both a Transfer-Encoding and a
6268 * Content-Length header field, the Transfer-Encoding overrides the
6269 * Content-Length. Such a message might indicate an attempt to
6270 * perform request smuggling (Section 9.5) or response splitting
6271 * (Section 9.4) and ought to be handled as an error. A sender MUST
6272 * remove the received Content-Length field prior to forwarding such
6273 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006274 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006275 * 4. If a message is received without Transfer-Encoding and with
6276 * either multiple Content-Length header fields having differing
6277 * field-values or a single Content-Length header field having an
6278 * invalid value, then the message framing is invalid and the
6279 * recipient MUST treat it as an unrecoverable error. If this is a
6280 * request message, the server MUST respond with a 400 (Bad Request)
6281 * status code and then close the connection. If this is a response
6282 * message received by a proxy, the proxy MUST close the connection
6283 * to the server, discard the received response, and send a 502 (Bad
6284 * Gateway) response to the client. If this is a response message
6285 * received by a user agent, the user agent MUST close the
6286 * connection to the server and discard the received response.
6287 *
6288 * 5. If a valid Content-Length header field is present without
6289 * Transfer-Encoding, its decimal value defines the expected message
6290 * body length in octets. If the sender closes the connection or
6291 * the recipient times out before the indicated number of octets are
6292 * received, the recipient MUST consider the message to be
6293 * incomplete and close the connection.
6294 *
6295 * 6. If this is a request message and none of the above are true, then
6296 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006297 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006298 * 7. Otherwise, this is a response message without a declared message
6299 * body length, so the message body length is determined by the
6300 * number of octets received prior to the server closing the
6301 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006302 */
6303
6304 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006305 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006306 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006307 * FIXME: should we parse anyway and return an error on chunked encoding ?
6308 */
6309 if (txn->meth == HTTP_METH_HEAD ||
6310 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006311 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006312 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006313 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006314 goto skip_content_length;
6315 }
6316
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006317 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006318 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006319 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006320 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006321 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6322 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006323 /* bad transfer-encoding (chunked followed by something else) */
6324 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006325 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006326 break;
6327 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006328 }
6329
Willy Tarreau1c913912015-04-30 10:57:51 +02006330 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006331 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006332 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006333 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6334 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6335 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006336 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006337 signed long long cl;
6338
Willy Tarreauad14f752011-09-02 20:33:27 +02006339 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006340 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006341 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006342 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006343
Willy Tarreauad14f752011-09-02 20:33:27 +02006344 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006345 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006346 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006347 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006348
Willy Tarreauad14f752011-09-02 20:33:27 +02006349 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006350 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006351 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006352 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006353
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006354 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006355 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006356 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006357 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006358
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006359 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006360 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006361 }
6362
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006363 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006364 select_compression_response_header(s, rep->buf);
6365
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006366skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006367 /* Now we have to check if we need to modify the Connection header.
6368 * This is more difficult on the response than it is on the request,
6369 * because we can have two different HTTP versions and we don't know
6370 * how the client will interprete a response. For instance, let's say
6371 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6372 * HTTP/1.1 response without any header. Maybe it will bound itself to
6373 * HTTP/1.0 because it only knows about it, and will consider the lack
6374 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6375 * the lack of header as a keep-alive. Thus we will use two flags
6376 * indicating how a request MAY be understood by the client. In case
6377 * of multiple possibilities, we'll fix the header to be explicit. If
6378 * ambiguous cases such as both close and keepalive are seen, then we
6379 * will fall back to explicit close. Note that we won't take risks with
6380 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006381 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006382 */
6383
Willy Tarreaudc008c52010-02-01 16:20:08 +01006384 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6385 txn->status == 101)) {
6386 /* Either we've established an explicit tunnel, or we're
6387 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006388 * to understand the next protocols. We have to switch to tunnel
6389 * mode, so that we transfer the request and responses then let
6390 * this protocol pass unmodified. When we later implement specific
6391 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006392 * header which contains information about that protocol for
6393 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006394 */
6395 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6396 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006397 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6398 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006399 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006400 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006401 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006402
Willy Tarreau70dffda2014-01-30 03:07:23 +01006403 /* this situation happens when combining pretend-keepalive with httpclose. */
6404 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006405 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006406 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006407 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6408
Willy Tarreau60466522010-01-18 19:08:45 +01006409 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006410 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006411 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6412 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006413
Willy Tarreau60466522010-01-18 19:08:45 +01006414 /* now adjust header transformations depending on current state */
6415 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6416 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6417 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006418 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006419 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006420 }
Willy Tarreau60466522010-01-18 19:08:45 +01006421 else { /* SCL / KAL */
6422 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006423 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006424 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006425 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006426
Willy Tarreau60466522010-01-18 19:08:45 +01006427 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006428 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006429
Willy Tarreau60466522010-01-18 19:08:45 +01006430 /* Some keep-alive responses are converted to Server-close if
6431 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006432 */
Willy Tarreau60466522010-01-18 19:08:45 +01006433 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6434 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006435 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006436 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006437 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006438 }
6439
Willy Tarreau7959a552013-09-23 16:44:27 +02006440 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006441 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006442
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006443 /* end of job, return OK */
6444 rep->analysers &= ~an_bit;
6445 rep->analyse_exp = TICK_ETERNITY;
6446 channel_auto_close(rep);
6447 return 1;
6448
6449 abort_keep_alive:
6450 /* A keep-alive request to the server failed on a network error.
6451 * The client is required to retry. We need to close without returning
6452 * any other information so that the client retries.
6453 */
6454 txn->status = 0;
6455 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006456 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006457 channel_auto_close(rep);
6458 s->logs.logwait = 0;
6459 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006460 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006461 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006462 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006463 return 0;
6464}
6465
6466/* This function performs all the processing enabled for the current response.
6467 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006468 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006469 * other functions. It works like process_request (see indications above).
6470 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006471int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006472{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006473 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006474 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006475 struct http_msg *msg = &txn->rsp;
6476 struct proxy *cur_proxy;
6477 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006478 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006479
Willy Tarreau87b09662015-04-03 00:22:06 +02006480 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 +02006481 now_ms, __FUNCTION__,
6482 s,
6483 rep,
6484 rep->rex, rep->wex,
6485 rep->flags,
6486 rep->buf->i,
6487 rep->analysers);
6488
6489 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6490 return 0;
6491
Willy Tarreau70730dd2014-04-24 18:06:27 +02006492 /* The stats applet needs to adjust the Connection header but we don't
6493 * apply any filter there.
6494 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006495 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6496 rep->analysers &= ~an_bit;
6497 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006498 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006499 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006500
Willy Tarreau58975672014-04-24 21:13:57 +02006501 /*
6502 * We will have to evaluate the filters.
6503 * As opposed to version 1.2, now they will be evaluated in the
6504 * filters order and not in the header order. This means that
6505 * each filter has to be validated among all headers.
6506 *
6507 * Filters are tried with ->be first, then with ->fe if it is
6508 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006509 *
6510 * Maybe we are in resume condiion. In this case I choose the
6511 * "struct proxy" which contains the rule list matching the resume
6512 * pointer. If none of theses "struct proxy" match, I initialise
6513 * the process with the first one.
6514 *
6515 * In fact, I check only correspondance betwwen the current list
6516 * pointer and the ->fe rule list. If it doesn't match, I initialize
6517 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006518 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006519 if (s->current_rule_list == &sess->fe->http_res_rules)
6520 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006521 else
6522 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006523 while (1) {
6524 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006525
Willy Tarreau58975672014-04-24 21:13:57 +02006526 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006527 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006528 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006529
Willy Tarreau51d861a2015-05-22 17:30:48 +02006530 if (ret == HTTP_RULE_RES_BADREQ)
6531 goto return_srv_prx_502;
6532
6533 if (ret == HTTP_RULE_RES_DONE) {
6534 rep->analysers &= ~an_bit;
6535 rep->analyse_exp = TICK_ETERNITY;
6536 return 1;
6537 }
6538 }
6539
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006540 /* we need to be called again. */
6541 if (ret == HTTP_RULE_RES_YIELD) {
6542 channel_dont_close(rep);
6543 return 0;
6544 }
6545
Willy Tarreau58975672014-04-24 21:13:57 +02006546 /* try headers filters */
6547 if (rule_set->rsp_exp != NULL) {
6548 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6549 return_bad_resp:
6550 if (objt_server(s->target)) {
6551 objt_server(s->target)->counters.failed_resp++;
6552 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006553 }
Willy Tarreau58975672014-04-24 21:13:57 +02006554 s->be->be_counters.failed_resp++;
6555 return_srv_prx_502:
6556 rep->analysers = 0;
6557 txn->status = 502;
6558 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006559 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006560 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006561 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006562 if (!(s->flags & SF_ERR_MASK))
6563 s->flags |= SF_ERR_PRXCOND;
6564 if (!(s->flags & SF_FINST_MASK))
6565 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006566 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006567 }
Willy Tarreau58975672014-04-24 21:13:57 +02006568 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006569
Willy Tarreau58975672014-04-24 21:13:57 +02006570 /* has the response been denied ? */
6571 if (txn->flags & TX_SVDENY) {
6572 if (objt_server(s->target))
6573 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006574
Willy Tarreau58975672014-04-24 21:13:57 +02006575 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006576 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006577 if (sess->listener->counters)
6578 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006579
Willy Tarreau58975672014-04-24 21:13:57 +02006580 goto return_srv_prx_502;
6581 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006582
Willy Tarreau58975672014-04-24 21:13:57 +02006583 /* add response headers from the rule sets in the same order */
6584 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006585 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006586 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006587 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006588 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006589 ret = acl_pass(ret);
6590 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6591 ret = !ret;
6592 if (!ret)
6593 continue;
6594 }
6595 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6596 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006597 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006598
Willy Tarreau58975672014-04-24 21:13:57 +02006599 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006600 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006601 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006602 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006603 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006604
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006605 /* After this point, this anayzer can't return yield, so we can
6606 * remove the bit corresponding to this analyzer from the list.
6607 *
6608 * Note that the intermediate returns and goto found previously
6609 * reset the analyzers.
6610 */
6611 rep->analysers &= ~an_bit;
6612 rep->analyse_exp = TICK_ETERNITY;
6613
Willy Tarreau58975672014-04-24 21:13:57 +02006614 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006615 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006616 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006617
Willy Tarreau58975672014-04-24 21:13:57 +02006618 /*
6619 * Now check for a server cookie.
6620 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006621 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006622 (s->be->options & PR_O_CHK_CACHE))
6623 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006624
Willy Tarreau58975672014-04-24 21:13:57 +02006625 /*
6626 * Check for cache-control or pragma headers if required.
6627 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006628 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 +02006629 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006630
Willy Tarreau58975672014-04-24 21:13:57 +02006631 /*
6632 * Add server cookie in the response if needed
6633 */
6634 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6635 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006636 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006637 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6638 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6639 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6640 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6641 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006642 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006643 /* the server is known, it's not the one the client requested, or the
6644 * cookie's last seen date needs to be refreshed. We have to
6645 * insert a set-cookie here, except if we want to insert only on POST
6646 * requests and this one isn't. Note that servers which don't have cookies
6647 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648 */
Willy Tarreau58975672014-04-24 21:13:57 +02006649 if (!objt_server(s->target)->cookie) {
6650 chunk_printf(&trash,
6651 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6652 s->be->cookie_name);
6653 }
6654 else {
6655 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006656
Willy Tarreau58975672014-04-24 21:13:57 +02006657 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6658 /* emit last_date, which is mandatory */
6659 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6660 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6661 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006662
Willy Tarreau58975672014-04-24 21:13:57 +02006663 if (s->be->cookie_maxlife) {
6664 /* emit first_date, which is either the original one or
6665 * the current date.
6666 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006667 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006668 s30tob64(txn->cookie_first_date ?
6669 txn->cookie_first_date >> 2 :
6670 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006671 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006672 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006673 }
Willy Tarreau58975672014-04-24 21:13:57 +02006674 chunk_appendf(&trash, "; path=/");
6675 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006676
Willy Tarreau58975672014-04-24 21:13:57 +02006677 if (s->be->cookie_domain)
6678 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006679
Willy Tarreau58975672014-04-24 21:13:57 +02006680 if (s->be->ck_opts & PR_CK_HTTPONLY)
6681 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006682
Willy Tarreau58975672014-04-24 21:13:57 +02006683 if (s->be->ck_opts & PR_CK_SECURE)
6684 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006685
Willy Tarreau58975672014-04-24 21:13:57 +02006686 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6687 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006688
Willy Tarreau58975672014-04-24 21:13:57 +02006689 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006690 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006691 /* the server did not change, only the date was updated */
6692 txn->flags |= TX_SCK_UPDATED;
6693 else
6694 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006695
Willy Tarreau58975672014-04-24 21:13:57 +02006696 /* Here, we will tell an eventual cache on the client side that we don't
6697 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6698 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6699 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006700 */
Willy Tarreau58975672014-04-24 21:13:57 +02006701 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006702
Willy Tarreau58975672014-04-24 21:13:57 +02006703 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006704
Willy Tarreau58975672014-04-24 21:13:57 +02006705 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6706 "Cache-control: private", 22) < 0))
6707 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006708 }
Willy Tarreau58975672014-04-24 21:13:57 +02006709 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006710
Willy Tarreau58975672014-04-24 21:13:57 +02006711 /*
6712 * Check if result will be cacheable with a cookie.
6713 * We'll block the response if security checks have caught
6714 * nasty things such as a cacheable cookie.
6715 */
6716 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6717 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6718 (s->be->options & PR_O_CHK_CACHE)) {
6719 /* we're in presence of a cacheable response containing
6720 * a set-cookie header. We'll block it as requested by
6721 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722 */
Willy Tarreau58975672014-04-24 21:13:57 +02006723 if (objt_server(s->target))
6724 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006725
Willy Tarreau58975672014-04-24 21:13:57 +02006726 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006727 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006728 if (sess->listener->counters)
6729 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006730
Willy Tarreau58975672014-04-24 21:13:57 +02006731 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6732 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6733 send_log(s->be, LOG_ALERT,
6734 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6735 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6736 goto return_srv_prx_502;
6737 }
Willy Tarreau03945942009-12-22 16:50:27 +01006738
Willy Tarreau70730dd2014-04-24 18:06:27 +02006739 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006740 /*
6741 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6742 * If an "Upgrade" token is found, the header is left untouched in order
6743 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006744 * if anything but "Upgrade" is present in the Connection header. We don't
6745 * want to touch any 101 response either since it's switching to another
6746 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006747 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006748 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006749 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006750 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006751 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6752 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006753
Willy Tarreau58975672014-04-24 21:13:57 +02006754 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6755 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6756 /* we want a keep-alive response here. Keep-alive header
6757 * required if either side is not 1.1.
6758 */
6759 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6760 want_flags |= TX_CON_KAL_SET;
6761 }
6762 else {
6763 /* we want a close response here. Close header required if
6764 * the server is 1.1, regardless of the client.
6765 */
6766 if (msg->flags & HTTP_MSGF_VER_11)
6767 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006768 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769
Willy Tarreau58975672014-04-24 21:13:57 +02006770 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6771 http_change_connection_header(txn, msg, want_flags);
6772 }
6773
6774 skip_header_mangling:
6775 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6776 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6777 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006778
Willy Tarreau58975672014-04-24 21:13:57 +02006779 /* if the user wants to log as soon as possible, without counting
6780 * bytes from the server, then this is the right moment. We have
6781 * to temporarily assign bytes_out to log what we currently have.
6782 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006783 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006784 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6785 s->logs.bytes_out = txn->rsp.eoh;
6786 s->do_log(s);
6787 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006788 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006789 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006790}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791
Willy Tarreaud98cf932009-12-27 22:54:55 +01006792/* This function is an analyser which forwards response body (including chunk
6793 * sizes if any). It is called as soon as we must forward, even if we forward
6794 * zero byte. The only situation where it must not be called is when we're in
6795 * tunnel mode and we want to forward till the close. It's used both to forward
6796 * remaining data and to resync after end of body. It expects the msg_state to
6797 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006798 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006799 *
6800 * It is capable of compressing response data both in content-length mode and
6801 * in chunked mode. The state machines follows different flows depending on
6802 * whether content-length and chunked modes are used, since there are no
6803 * trailers in content-length :
6804 *
6805 * chk-mode cl-mode
6806 * ,----- BODY -----.
6807 * / \
6808 * V size > 0 V chk-mode
6809 * .--> SIZE -------------> DATA -------------> CRLF
6810 * | | size == 0 | last byte |
6811 * | v final crlf v inspected |
6812 * | TRAILERS -----------> DONE |
6813 * | |
6814 * `----------------------------------------------'
6815 *
6816 * Compression only happens in the DATA state, and must be flushed in final
6817 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6818 * is performed at once on final states for all bytes parsed, or when leaving
6819 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006820 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006821int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006822{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006823 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006824 struct http_txn *txn = s->txn;
6825 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006826 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006827 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006828 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006829
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006830 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6831 return 0;
6832
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006833 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006834 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006835 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006836 /* Output closed while we were sending data. We must abort and
6837 * wake the other side up.
6838 */
6839 msg->msg_state = HTTP_MSG_ERROR;
6840 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006841 return 1;
6842 }
6843
Willy Tarreau4fe41902010-06-07 22:27:41 +02006844 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006845 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006846
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006847 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006848 /* we have msg->sov which points to the first byte of message
6849 * body, and res->buf.p still points to the beginning of the
6850 * message. We forward the headers now, as we don't need them
6851 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006852 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006853 b_adv(res->buf, msg->sov);
6854 msg->next -= msg->sov;
6855 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006856
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006857 /* The previous analysers guarantee that the state is somewhere
6858 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6859 * msg->next are always correct.
6860 */
6861 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6862 if (msg->flags & HTTP_MSGF_TE_CHNK)
6863 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6864 else
6865 msg->msg_state = HTTP_MSG_DATA;
6866 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006867 }
6868
Willy Tarreauefdf0942014-04-24 20:08:57 +02006869 if (res->to_forward) {
6870 /* We can't process the buffer's contents yet */
6871 res->flags |= CF_WAKE_WRITE;
6872 goto missing_data;
6873 }
6874
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006875 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6876 /* We need a compression buffer in the DATA state to put the
6877 * output of compressed data, and in CRLF state to let the
6878 * TRAILERS state finish the job of removing the trailing CRLF.
6879 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006880 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006881 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006882 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006883 goto aborted_xfer; /* no memory */
6884 }
6885
6886 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006887 if (ret < 0) {
6888 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006889 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006890 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006891 compressing = 1;
6892 }
6893
Willy Tarreaud98cf932009-12-27 22:54:55 +01006894 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006895 switch (msg->msg_state - HTTP_MSG_DATA) {
6896 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006897 /* we may have some pending data starting at res->buf->p */
6898 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006899 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006900 if (ret < 0)
6901 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006902
Willy Tarreaud5a67832014-04-21 10:54:27 +02006903 if (msg->chunk_len) {
6904 /* input empty or output full */
6905 if (res->buf->i > msg->next)
6906 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006907 goto missing_data;
6908 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006909 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006910 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006911 if (msg->chunk_len > res->buf->i - msg->next) {
6912 /* output full */
6913 res->flags |= CF_WAKE_WRITE;
6914 goto missing_data;
6915 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006916 msg->next += msg->chunk_len;
6917 msg->chunk_len = 0;
6918 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006919
6920 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006921 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006922 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006923 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006924 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006925 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006926 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006927 /* fall through for HTTP_MSG_CHUNK_CRLF */
6928
6929 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6930 /* we want the CRLF after the data */
6931
6932 ret = http_skip_chunk_crlf(msg);
6933 if (ret == 0)
6934 goto missing_data;
6935 else if (ret < 0) {
6936 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006937 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006938 goto return_bad_res;
6939 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006940 /* we're in MSG_CHUNK_SIZE now, fall through */
6941
6942 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006943 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006944 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006945 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006946 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006947
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006948 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006949 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006950 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006951 else if (ret < 0) {
6952 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006953 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006954 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006955 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006956 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006957 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006958
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006959 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006960 if (unlikely(compressing)) {
6961 /* we need to flush output contents before syncing FSMs */
6962 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6963 compressing = 0;
6964 }
6965
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006966 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006967 if (ret == 0)
6968 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006969 else if (ret < 0) {
6970 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006971 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006972 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006973 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006974 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006975
6976 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006977 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006978 if (unlikely(compressing)) {
6979 /* we need to flush output contents before syncing FSMs */
6980 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6981 compressing = 0;
6982 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006983
Willy Tarreauc623c172014-04-18 09:53:50 +02006984 /* we may have some pending data starting at res->buf->p
6985 * such as a last chunk of data or trailers.
6986 */
6987 b_adv(res->buf, msg->next);
6988 msg->next = 0;
6989
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006990 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006991 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006992 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6993 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006994 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006995
Willy Tarreau610ecce2010-01-04 21:15:02 +01006996 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006997 /* some state changes occurred, maybe the analyser
6998 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006999 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007000 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007001 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007002 /* response errors are most likely due to
7003 * the client aborting the transfer.
7004 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007005 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007006 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007007 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007008 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007009 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007010 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007011 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007012 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007013 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007014 }
7015 }
7016
Willy Tarreaud98cf932009-12-27 22:54:55 +01007017 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007018 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007019 if (unlikely(compressing)) {
7020 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007021 compressing = 0;
7022 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007023
Willy Tarreauc623c172014-04-18 09:53:50 +02007024 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7025 b_adv(res->buf, msg->next);
7026 msg->next = 0;
7027 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7028 }
7029
Willy Tarreauf003d372012-11-26 13:35:37 +01007030 if (res->flags & CF_SHUTW)
7031 goto aborted_xfer;
7032
7033 /* stop waiting for data if the input is closed before the end. If the
7034 * client side was already closed, it means that the client has aborted,
7035 * so we don't want to count this as a server abort. Otherwise it's a
7036 * server abort.
7037 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007038 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007039 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007040 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007041 if (!(s->flags & SF_ERR_MASK))
7042 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007043 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007044 if (objt_server(s->target))
7045 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007046 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007047 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007048
Willy Tarreau40dba092010-03-04 18:14:51 +01007049 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007050 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007051 goto return_bad_res;
7052
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007053 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007054 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007055 * Similarly, with keep-alive on the client side, we don't want to forward a
7056 * close.
7057 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007058 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007059 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7060 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007061 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007062
Willy Tarreau5c620922011-05-11 19:56:11 +02007063 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007064 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007065 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007066 * modes are already handled by the stream sock layer. We must not do
7067 * this in content-length mode because it could present the MSG_MORE
7068 * flag with the last block of forwarded data, which would cause an
7069 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007070 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007071 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007072 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007073
Willy Tarreau87b09662015-04-03 00:22:06 +02007074 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007075 return 0;
7076
Willy Tarreau40dba092010-03-04 18:14:51 +01007077 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007078 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007079 if (objt_server(s->target))
7080 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007081
7082 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007083 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007084 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007085 compressing = 0;
7086 }
7087
Willy Tarreauc623c172014-04-18 09:53:50 +02007088 /* we may have some pending data starting at res->buf->p */
7089 if (s->comp_algo == NULL) {
7090 b_adv(res->buf, msg->next);
7091 msg->next = 0;
7092 }
7093
Willy Tarreaud98cf932009-12-27 22:54:55 +01007094 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007095 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007096 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007097 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007098 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007099 if (objt_server(s->target))
7100 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007101
Willy Tarreaue7dff022015-04-03 01:14:29 +02007102 if (!(s->flags & SF_ERR_MASK))
7103 s->flags |= SF_ERR_PRXCOND;
7104 if (!(s->flags & SF_FINST_MASK))
7105 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007106 return 0;
7107
7108 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007109 if (unlikely(compressing)) {
7110 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7111 compressing = 0;
7112 }
7113
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007114 txn->rsp.msg_state = HTTP_MSG_ERROR;
7115 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007116 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007117 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007118 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007119
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007120 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007121 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007122 if (objt_server(s->target))
7123 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007124
Willy Tarreaue7dff022015-04-03 01:14:29 +02007125 if (!(s->flags & SF_ERR_MASK))
7126 s->flags |= SF_ERR_CLICL;
7127 if (!(s->flags & SF_FINST_MASK))
7128 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007129 return 0;
7130}
7131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132/* Iterate the same filter through all request headers.
7133 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 * Since it can manage the switch to another backend, it updates the per-proxy
7135 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007136 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007137int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007138{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007139 char *cur_ptr, *cur_end, *cur_next;
7140 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007141 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007142 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007143 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007144
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007145 last_hdr = 0;
7146
Willy Tarreau9b28e032012-10-12 23:49:43 +02007147 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007148 old_idx = 0;
7149
7150 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007151 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007152 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007153 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 (exp->action == ACT_ALLOW ||
7155 exp->action == ACT_DENY ||
7156 exp->action == ACT_TARPIT))
7157 return 0;
7158
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007159 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007160 if (!cur_idx)
7161 break;
7162
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007163 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007164 cur_ptr = cur_next;
7165 cur_end = cur_ptr + cur_hdr->len;
7166 cur_next = cur_end + cur_hdr->cr + 1;
7167
7168 /* Now we have one header between cur_ptr and cur_end,
7169 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007170 */
7171
Willy Tarreau15a53a42015-01-21 13:39:42 +01007172 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007174 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007175 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176 last_hdr = 1;
7177 break;
7178
7179 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007180 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007181 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007182 break;
7183
7184 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007185 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007186 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007187 break;
7188
7189 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007190 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7191 if (trash.len < 0)
7192 return -1;
7193
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007194 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007195 /* FIXME: if the user adds a newline in the replacement, the
7196 * index will not be recalculated for now, and the new line
7197 * will not be counted as a new header.
7198 */
7199
7200 cur_end += delta;
7201 cur_next += delta;
7202 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007203 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007204 break;
7205
7206 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007207 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007208 cur_next += delta;
7209
Willy Tarreaufa355d42009-11-29 18:12:29 +01007210 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007211 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7212 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007213 cur_hdr->len = 0;
7214 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007215 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007216 break;
7217
7218 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007219 }
7220
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007221 /* keep the link from this header to next one in case of later
7222 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007223 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007224 old_idx = cur_idx;
7225 }
7226 return 0;
7227}
7228
7229
7230/* Apply the filter to the request line.
7231 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7232 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 * Since it can manage the switch to another backend, it updates the per-proxy
7234 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007235 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007236int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007237{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007238 char *cur_ptr, *cur_end;
7239 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007240 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007241 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007242
Willy Tarreau3d300592007-03-18 18:34:41 +01007243 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007244 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007245 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246 (exp->action == ACT_ALLOW ||
7247 exp->action == ACT_DENY ||
7248 exp->action == ACT_TARPIT))
7249 return 0;
7250 else if (exp->action == ACT_REMOVE)
7251 return 0;
7252
7253 done = 0;
7254
Willy Tarreau9b28e032012-10-12 23:49:43 +02007255 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007256 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007257
7258 /* Now we have the request line between cur_ptr and cur_end */
7259
Willy Tarreau15a53a42015-01-21 13:39:42 +01007260 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007261 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007263 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007264 done = 1;
7265 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007267 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007268 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007269 done = 1;
7270 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007272 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007273 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 done = 1;
7275 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007278 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7279 if (trash.len < 0)
7280 return -1;
7281
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007282 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 /* FIXME: if the user adds a newline in the replacement, the
7284 * index will not be recalculated for now, and the new line
7285 * will not be counted as a new header.
7286 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007287
Willy Tarreaufa355d42009-11-29 18:12:29 +01007288 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007290 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007291 HTTP_MSG_RQMETH,
7292 cur_ptr, cur_end + 1,
7293 NULL, NULL);
7294 if (unlikely(!cur_end))
7295 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 /* we have a full request and we know that we have either a CR
7298 * or an LF at <ptr>.
7299 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007300 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7301 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 /* there is no point trying this regex on headers */
7303 return 1;
7304 }
7305 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 return done;
7307}
Willy Tarreau97de6242006-12-27 17:18:38 +01007308
Willy Tarreau58f10d72006-12-04 02:26:12 +01007309
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007312 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007313 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314 * unparsable request. Since it can manage the switch to another backend, it
7315 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007316 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007317int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318{
Willy Tarreau192252e2015-04-04 01:47:55 +02007319 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007320 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007321 struct hdr_exp *exp;
7322
7323 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007324 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007326 /*
7327 * The interleaving of transformations and verdicts
7328 * makes it difficult to decide to continue or stop
7329 * the evaluation.
7330 */
7331
Willy Tarreau6c123b12010-01-28 20:22:06 +01007332 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7333 break;
7334
Willy Tarreau3d300592007-03-18 18:34:41 +01007335 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007337 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007338 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007339
7340 /* if this filter had a condition, evaluate it now and skip to
7341 * next filter if the condition does not match.
7342 */
7343 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007344 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007345 ret = acl_pass(ret);
7346 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7347 ret = !ret;
7348
7349 if (!ret)
7350 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007351 }
7352
7353 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007354 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007355 if (unlikely(ret < 0))
7356 return -1;
7357
7358 if (likely(ret == 0)) {
7359 /* The filter did not match the request, it can be
7360 * iterated through all headers.
7361 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007362 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7363 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007364 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007365 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007367}
7368
7369
Willy Tarreaua15645d2007-03-18 16:22:39 +01007370
Willy Tarreau58f10d72006-12-04 02:26:12 +01007371/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007372 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007373 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007374 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007375void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02007376 struct http_txn *txn = s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007377 appsess *asession = NULL;
7378 char *sessid_temp = NULL;
7379
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007380 if (len > s->be->appsession_len) {
7381 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007382 }
7383
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007384 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007385 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007386 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007387 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007388 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007389 }
7390
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007391 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007392 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007393 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007394 return;
7395 }
7396
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007397 memcpy(txn->sessid, buf, len);
7398 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007399 }
7400
7401 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7402 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007403 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007404 return;
7405 }
7406
Cyril Bontéb21570a2009-11-29 20:04:48 +01007407 memcpy(sessid_temp, buf, len);
7408 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007409
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007410 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007411 /* free previously allocated memory */
7412 pool_free2(apools.sessid, sessid_temp);
7413
7414 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007415 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7416 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007417 asession->request_count++;
7418
7419 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007420 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007421
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007422 while (srv) {
7423 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007424 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007425 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007426 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007427 /* we found the server and it's usable */
7428 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007429 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007430 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007431 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007432
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007433 break;
7434 } else {
7435 txn->flags &= ~TX_CK_MASK;
7436 txn->flags |= TX_CK_DOWN;
7437 }
7438 }
7439 srv = srv->next;
7440 }
7441 }
7442 }
7443}
7444
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007445/* Find the end of a cookie value contained between <s> and <e>. It works the
7446 * same way as with headers above except that the semi-colon also ends a token.
7447 * See RFC2965 for more information. Note that it requires a valid header to
7448 * return a valid result.
7449 */
7450char *find_cookie_value_end(char *s, const char *e)
7451{
7452 int quoted, qdpair;
7453
7454 quoted = qdpair = 0;
7455 for (; s < e; s++) {
7456 if (qdpair) qdpair = 0;
7457 else if (quoted) {
7458 if (*s == '\\') qdpair = 1;
7459 else if (*s == '"') quoted = 0;
7460 }
7461 else if (*s == '"') quoted = 1;
7462 else if (*s == ',' || *s == ';') return s;
7463 }
7464 return s;
7465}
7466
7467/* Delete a value in a header between delimiters <from> and <next> in buffer
7468 * <buf>. The number of characters displaced is returned, and the pointer to
7469 * the first delimiter is updated if required. The function tries as much as
7470 * possible to respect the following principles :
7471 * - replace <from> delimiter by the <next> one unless <from> points to a
7472 * colon, in which case <next> is simply removed
7473 * - set exactly one space character after the new first delimiter, unless
7474 * there are not enough characters in the block being moved to do so.
7475 * - remove unneeded spaces before the previous delimiter and after the new
7476 * one.
7477 *
7478 * It is the caller's responsibility to ensure that :
7479 * - <from> points to a valid delimiter or the colon ;
7480 * - <next> points to a valid delimiter or the final CR/LF ;
7481 * - there are non-space chars before <from> ;
7482 * - there is a CR/LF at or after <next>.
7483 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007484int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485{
7486 char *prev = *from;
7487
7488 if (*prev == ':') {
7489 /* We're removing the first value, preserve the colon and add a
7490 * space if possible.
7491 */
7492 if (!http_is_crlf[(unsigned char)*next])
7493 next++;
7494 prev++;
7495 if (prev < next)
7496 *prev++ = ' ';
7497
7498 while (http_is_spht[(unsigned char)*next])
7499 next++;
7500 } else {
7501 /* Remove useless spaces before the old delimiter. */
7502 while (http_is_spht[(unsigned char)*(prev-1)])
7503 prev--;
7504 *from = prev;
7505
7506 /* copy the delimiter and if possible a space if we're
7507 * not at the end of the line.
7508 */
7509 if (!http_is_crlf[(unsigned char)*next]) {
7510 *prev++ = *next++;
7511 if (prev + 1 < next)
7512 *prev++ = ' ';
7513 while (http_is_spht[(unsigned char)*next])
7514 next++;
7515 }
7516 }
7517 return buffer_replace2(buf, prev, next, NULL, 0);
7518}
7519
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007520/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007521 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 * desirable to call it only when needed. This code is quite complex because
7523 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7524 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007525 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007526void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007527{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007528 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007529 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007530 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007531 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007532 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7533 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007534
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007535 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007536 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007537 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007538
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007539 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007540 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007541 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007542
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007543 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007544 hdr_beg = hdr_next;
7545 hdr_end = hdr_beg + cur_hdr->len;
7546 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007548 /* We have one full header between hdr_beg and hdr_end, and the
7549 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007550 * "Cookie:" headers.
7551 */
7552
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007553 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007554 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007555 old_idx = cur_idx;
7556 continue;
7557 }
7558
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007559 del_from = NULL; /* nothing to be deleted */
7560 preserve_hdr = 0; /* assume we may kill the whole header */
7561
Willy Tarreau58f10d72006-12-04 02:26:12 +01007562 /* Now look for cookies. Conforming to RFC2109, we have to support
7563 * attributes whose name begin with a '$', and associate them with
7564 * the right cookie, if we want to delete this cookie.
7565 * So there are 3 cases for each cookie read :
7566 * 1) it's a special attribute, beginning with a '$' : ignore it.
7567 * 2) it's a server id cookie that we *MAY* want to delete : save
7568 * some pointers on it (last semi-colon, beginning of cookie...)
7569 * 3) it's an application cookie : we *MAY* have to delete a previous
7570 * "special" cookie.
7571 * At the end of loop, if a "special" cookie remains, we may have to
7572 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007573 * *MUST* delete it.
7574 *
7575 * Note: RFC2965 is unclear about the processing of spaces around
7576 * the equal sign in the ATTR=VALUE form. A careful inspection of
7577 * the RFC explicitly allows spaces before it, and not within the
7578 * tokens (attrs or values). An inspection of RFC2109 allows that
7579 * too but section 10.1.3 lets one think that spaces may be allowed
7580 * after the equal sign too, resulting in some (rare) buggy
7581 * implementations trying to do that. So let's do what servers do.
7582 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7583 * allowed quoted strings in values, with any possible character
7584 * after a backslash, including control chars and delimitors, which
7585 * causes parsing to become ambiguous. Browsers also allow spaces
7586 * within values even without quotes.
7587 *
7588 * We have to keep multiple pointers in order to support cookie
7589 * removal at the beginning, middle or end of header without
7590 * corrupting the header. All of these headers are valid :
7591 *
7592 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7593 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7594 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7595 * | | | | | | | | |
7596 * | | | | | | | | hdr_end <--+
7597 * | | | | | | | +--> next
7598 * | | | | | | +----> val_end
7599 * | | | | | +-----------> val_beg
7600 * | | | | +--------------> equal
7601 * | | | +----------------> att_end
7602 * | | +---------------------> att_beg
7603 * | +--------------------------> prev
7604 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007605 */
7606
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7608 /* Iterate through all cookies on this line */
7609
7610 /* find att_beg */
7611 att_beg = prev + 1;
7612 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7613 att_beg++;
7614
7615 /* find att_end : this is the first character after the last non
7616 * space before the equal. It may be equal to hdr_end.
7617 */
7618 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007619
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 while (equal < hdr_end) {
7621 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007622 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007623 if (http_is_spht[(unsigned char)*equal++])
7624 continue;
7625 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007626 }
7627
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007628 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7629 * is between <att_beg> and <equal>, both may be identical.
7630 */
7631
7632 /* look for end of cookie if there is an equal sign */
7633 if (equal < hdr_end && *equal == '=') {
7634 /* look for the beginning of the value */
7635 val_beg = equal + 1;
7636 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7637 val_beg++;
7638
7639 /* find the end of the value, respecting quotes */
7640 next = find_cookie_value_end(val_beg, hdr_end);
7641
7642 /* make val_end point to the first white space or delimitor after the value */
7643 val_end = next;
7644 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7645 val_end--;
7646 } else {
7647 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007648 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007650 /* We have nothing to do with attributes beginning with '$'. However,
7651 * they will automatically be removed if a header before them is removed,
7652 * since they're supposed to be linked together.
7653 */
7654 if (*att_beg == '$')
7655 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007656
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007657 /* Ignore cookies with no equal sign */
7658 if (equal == next) {
7659 /* This is not our cookie, so we must preserve it. But if we already
7660 * scheduled another cookie for removal, we cannot remove the
7661 * complete header, but we can remove the previous block itself.
7662 */
7663 preserve_hdr = 1;
7664 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007665 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007666 val_end += delta;
7667 next += delta;
7668 hdr_end += delta;
7669 hdr_next += delta;
7670 cur_hdr->len += delta;
7671 http_msg_move_end(&txn->req, delta);
7672 prev = del_from;
7673 del_from = NULL;
7674 }
7675 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007676 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007677
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007678 /* if there are spaces around the equal sign, we need to
7679 * strip them otherwise we'll get trouble for cookie captures,
7680 * or even for rewrites. Since this happens extremely rarely,
7681 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007682 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007683 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7684 int stripped_before = 0;
7685 int stripped_after = 0;
7686
7687 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007688 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 equal += stripped_before;
7690 val_beg += stripped_before;
7691 }
7692
7693 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007694 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007695 val_beg += stripped_after;
7696 stripped_before += stripped_after;
7697 }
7698
7699 val_end += stripped_before;
7700 next += stripped_before;
7701 hdr_end += stripped_before;
7702 hdr_next += stripped_before;
7703 cur_hdr->len += stripped_before;
7704 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007707
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007708 /* First, let's see if we want to capture this cookie. We check
7709 * that we don't already have a client side cookie, because we
7710 * can only capture one. Also as an optimisation, we ignore
7711 * cookies shorter than the declared name.
7712 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007713 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7714 (val_end - att_beg >= sess->fe->capture_namelen) &&
7715 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007716 int log_len = val_end - att_beg;
7717
7718 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7719 Alert("HTTP logging : out of memory.\n");
7720 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007721 if (log_len > sess->fe->capture_len)
7722 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007723 memcpy(txn->cli_cookie, att_beg, log_len);
7724 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007725 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007726 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007727
Willy Tarreaubca99692010-10-06 19:25:55 +02007728 /* Persistence cookies in passive, rewrite or insert mode have the
7729 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007731 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007733 * For cookies in prefix mode, the form is :
7734 *
7735 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007736 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007737 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7738 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7739 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007740 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007741
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007742 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7743 * have the server ID between val_beg and delim, and the original cookie between
7744 * delim+1 and val_end. Otherwise, delim==val_end :
7745 *
7746 * Cookie: NAME=SRV; # in all but prefix modes
7747 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7748 * | || || | |+-> next
7749 * | || || | +--> val_end
7750 * | || || +---------> delim
7751 * | || |+------------> val_beg
7752 * | || +-------------> att_end = equal
7753 * | |+-----------------> att_beg
7754 * | +------------------> prev
7755 * +-------------------------> hdr_beg
7756 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007757
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007758 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 for (delim = val_beg; delim < val_end; delim++)
7760 if (*delim == COOKIE_DELIM)
7761 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007762 } else {
7763 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007764 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007765 /* Now check if the cookie contains a date field, which would
7766 * appear after a vertical bar ('|') just after the server name
7767 * and before the delimiter.
7768 */
7769 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7770 if (vbar1) {
7771 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007772 * right is the last seen date. It is a base64 encoded
7773 * 30-bit value representing the UNIX date since the
7774 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007775 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007776 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007777 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007778 if (val_end - vbar1 >= 5) {
7779 val = b64tos30(vbar1);
7780 if (val > 0)
7781 txn->cookie_last_date = val << 2;
7782 }
7783 /* look for a second vertical bar */
7784 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7785 if (vbar1 && (val_end - vbar1 > 5)) {
7786 val = b64tos30(vbar1 + 1);
7787 if (val > 0)
7788 txn->cookie_first_date = val << 2;
7789 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007790 }
7791 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007792
Willy Tarreauf64d1412010-10-07 20:06:11 +02007793 /* if the cookie has an expiration date and the proxy wants to check
7794 * it, then we do that now. We first check if the cookie is too old,
7795 * then only if it has expired. We detect strict overflow because the
7796 * time resolution here is not great (4 seconds). Cookies with dates
7797 * in the future are ignored if their offset is beyond one day. This
7798 * allows an admin to fix timezone issues without expiring everyone
7799 * and at the same time avoids keeping unwanted side effects for too
7800 * long.
7801 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007802 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7803 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007804 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007805 txn->flags &= ~TX_CK_MASK;
7806 txn->flags |= TX_CK_OLD;
7807 delim = val_beg; // let's pretend we have not found the cookie
7808 txn->cookie_first_date = 0;
7809 txn->cookie_last_date = 0;
7810 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007811 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7812 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007813 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007814 txn->flags &= ~TX_CK_MASK;
7815 txn->flags |= TX_CK_EXPIRED;
7816 delim = val_beg; // let's pretend we have not found the cookie
7817 txn->cookie_first_date = 0;
7818 txn->cookie_last_date = 0;
7819 }
7820
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007821 /* Here, we'll look for the first running server which supports the cookie.
7822 * This allows to share a same cookie between several servers, for example
7823 * to dedicate backup servers to specific servers only.
7824 * However, to prevent clients from sticking to cookie-less backup server
7825 * when they have incidentely learned an empty cookie, we simply ignore
7826 * empty cookies and mark them as invalid.
7827 * The same behaviour is applied when persistence must be ignored.
7828 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007829 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007830 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007831
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007832 while (srv) {
7833 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7834 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007835 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007836 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007837 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007838 /* we found the server and we can use it */
7839 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007840 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007841 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007842 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007843 break;
7844 } else {
7845 /* we found a server, but it's down,
7846 * mark it as such and go on in case
7847 * another one is available.
7848 */
7849 txn->flags &= ~TX_CK_MASK;
7850 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007851 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007852 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007853 srv = srv->next;
7854 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007855
Willy Tarreauf64d1412010-10-07 20:06:11 +02007856 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007857 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007858 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007859 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007860 txn->flags |= TX_CK_UNUSED;
7861 else
7862 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007863 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007865 /* depending on the cookie mode, we may have to either :
7866 * - delete the complete cookie if we're in insert+indirect mode, so that
7867 * the server never sees it ;
7868 * - remove the server id from the cookie value, and tag the cookie as an
7869 * application cookie so that it does not get accidentely removed later,
7870 * if we're in cookie prefix mode
7871 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007872 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007873 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007874
Willy Tarreau9b28e032012-10-12 23:49:43 +02007875 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007876 val_end += delta;
7877 next += delta;
7878 hdr_end += delta;
7879 hdr_next += delta;
7880 cur_hdr->len += delta;
7881 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007882
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007883 del_from = NULL;
7884 preserve_hdr = 1; /* we want to keep this cookie */
7885 }
7886 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007887 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007888 del_from = prev;
7889 }
7890 } else {
7891 /* This is not our cookie, so we must preserve it. But if we already
7892 * scheduled another cookie for removal, we cannot remove the
7893 * complete header, but we can remove the previous block itself.
7894 */
7895 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007896
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007897 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007898 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007899 if (att_beg >= del_from)
7900 att_beg += delta;
7901 if (att_end >= del_from)
7902 att_end += delta;
7903 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007904 val_end += delta;
7905 next += delta;
7906 hdr_end += delta;
7907 hdr_next += delta;
7908 cur_hdr->len += delta;
7909 http_msg_move_end(&txn->req, delta);
7910 prev = del_from;
7911 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007912 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007913 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007914
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007915 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007916 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007917 int cmp_len, value_len;
7918 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007919
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007920 if (s->be->options2 & PR_O2_AS_PFX) {
7921 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7922 value_begin = att_beg + s->be->appsession_name_len;
7923 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 } else {
7925 cmp_len = att_end - att_beg;
7926 value_begin = val_beg;
7927 value_len = val_end - val_beg;
7928 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007929
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007930 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007931 if (cmp_len == s->be->appsession_name_len &&
7932 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7933 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007934 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007935 }
7936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007937 /* continue with next cookie on this header line */
7938 att_beg = next;
7939 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007940
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007941 /* There are no more cookies on this line.
7942 * We may still have one (or several) marked for deletion at the
7943 * end of the line. We must do this now in two ways :
7944 * - if some cookies must be preserved, we only delete from the
7945 * mark to the end of line ;
7946 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007947 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007948 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007949 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007950 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007951 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007952 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007953 cur_hdr->len += delta;
7954 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007955 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007956
7957 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007958 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7959 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007960 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007961 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007962 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007963 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007964 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007965 }
7966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007967 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007968 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007969 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007970}
7971
7972
Willy Tarreaua15645d2007-03-18 16:22:39 +01007973/* Iterate the same filter through all response headers contained in <rtr>.
7974 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7975 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007976int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007977{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978 char *cur_ptr, *cur_end, *cur_next;
7979 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007980 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007982 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983
7984 last_hdr = 0;
7985
Willy Tarreau9b28e032012-10-12 23:49:43 +02007986 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007987 old_idx = 0;
7988
7989 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007990 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007992 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993 (exp->action == ACT_ALLOW ||
7994 exp->action == ACT_DENY))
7995 return 0;
7996
7997 cur_idx = txn->hdr_idx.v[old_idx].next;
7998 if (!cur_idx)
7999 break;
8000
8001 cur_hdr = &txn->hdr_idx.v[cur_idx];
8002 cur_ptr = cur_next;
8003 cur_end = cur_ptr + cur_hdr->len;
8004 cur_next = cur_end + cur_hdr->cr + 1;
8005
8006 /* Now we have one header between cur_ptr and cur_end,
8007 * and the next header starts at cur_next.
8008 */
8009
Willy Tarreau15a53a42015-01-21 13:39:42 +01008010 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011 switch (exp->action) {
8012 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008013 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008014 last_hdr = 1;
8015 break;
8016
8017 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008018 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 last_hdr = 1;
8020 break;
8021
8022 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008023 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8024 if (trash.len < 0)
8025 return -1;
8026
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008027 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028 /* FIXME: if the user adds a newline in the replacement, the
8029 * index will not be recalculated for now, and the new line
8030 * will not be counted as a new header.
8031 */
8032
8033 cur_end += delta;
8034 cur_next += delta;
8035 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008036 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008037 break;
8038
8039 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008040 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 cur_next += delta;
8042
Willy Tarreaufa355d42009-11-29 18:12:29 +01008043 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008044 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8045 txn->hdr_idx.used--;
8046 cur_hdr->len = 0;
8047 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008048 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008049 break;
8050
8051 }
8052 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008053
8054 /* keep the link from this header to next one in case of later
8055 * removal of next header.
8056 */
8057 old_idx = cur_idx;
8058 }
8059 return 0;
8060}
8061
8062
8063/* Apply the filter to the status line in the response buffer <rtr>.
8064 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8065 * or -1 if a replacement resulted in an invalid status line.
8066 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008067int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008068{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069 char *cur_ptr, *cur_end;
8070 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008071 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008072 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073
8074
Willy Tarreau3d300592007-03-18 18:34:41 +01008075 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008077 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078 (exp->action == ACT_ALLOW ||
8079 exp->action == ACT_DENY))
8080 return 0;
8081 else if (exp->action == ACT_REMOVE)
8082 return 0;
8083
8084 done = 0;
8085
Willy Tarreau9b28e032012-10-12 23:49:43 +02008086 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008087 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088
8089 /* Now we have the status line between cur_ptr and cur_end */
8090
Willy Tarreau15a53a42015-01-21 13:39:42 +01008091 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008092 switch (exp->action) {
8093 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008094 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008095 done = 1;
8096 break;
8097
8098 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008099 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008100 done = 1;
8101 break;
8102
8103 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008104 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8105 if (trash.len < 0)
8106 return -1;
8107
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008108 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008109 /* FIXME: if the user adds a newline in the replacement, the
8110 * index will not be recalculated for now, and the new line
8111 * will not be counted as a new header.
8112 */
8113
Willy Tarreaufa355d42009-11-29 18:12:29 +01008114 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008115 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008116 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008117 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118 cur_ptr, cur_end + 1,
8119 NULL, NULL);
8120 if (unlikely(!cur_end))
8121 return -1;
8122
8123 /* we have a full respnse and we know that we have either a CR
8124 * or an LF at <ptr>.
8125 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008126 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008127 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008128 /* there is no point trying this regex on headers */
8129 return 1;
8130 }
8131 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132 return done;
8133}
8134
8135
8136
8137/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008138 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8140 * unparsable response.
8141 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008142int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008143{
Willy Tarreau192252e2015-04-04 01:47:55 +02008144 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008145 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008146 struct hdr_exp *exp;
8147
8148 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008149 int ret;
8150
8151 /*
8152 * The interleaving of transformations and verdicts
8153 * makes it difficult to decide to continue or stop
8154 * the evaluation.
8155 */
8156
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008157 if (txn->flags & TX_SVDENY)
8158 break;
8159
Willy Tarreau3d300592007-03-18 18:34:41 +01008160 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008161 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8162 exp->action == ACT_PASS)) {
8163 exp = exp->next;
8164 continue;
8165 }
8166
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008167 /* if this filter had a condition, evaluate it now and skip to
8168 * next filter if the condition does not match.
8169 */
8170 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008171 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008172 ret = acl_pass(ret);
8173 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8174 ret = !ret;
8175 if (!ret)
8176 continue;
8177 }
8178
Willy Tarreaua15645d2007-03-18 16:22:39 +01008179 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008180 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008181 if (unlikely(ret < 0))
8182 return -1;
8183
8184 if (likely(ret == 0)) {
8185 /* The filter did not match the response, it can be
8186 * iterated through all headers.
8187 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008188 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8189 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008190 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008191 }
8192 return 0;
8193}
8194
8195
Willy Tarreaua15645d2007-03-18 16:22:39 +01008196/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008197 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008198 * desirable to call it only when needed. This function is also used when we
8199 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008200 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008201void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008202{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008203 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008204 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008205 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008206 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008208 char *hdr_beg, *hdr_end, *hdr_next;
8209 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211 /* Iterate through the headers.
8212 * we start with the start line.
8213 */
8214 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008215 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216
8217 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8218 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008219 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220
8221 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008222 hdr_beg = hdr_next;
8223 hdr_end = hdr_beg + cur_hdr->len;
8224 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225
Willy Tarreau24581ba2010-08-31 22:39:35 +02008226 /* We have one full header between hdr_beg and hdr_end, and the
8227 * next header starts at hdr_next. We're only interested in
8228 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229 */
8230
Willy Tarreau24581ba2010-08-31 22:39:35 +02008231 is_cookie2 = 0;
8232 prev = hdr_beg + 10;
8233 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008234 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008235 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8236 if (!val) {
8237 old_idx = cur_idx;
8238 continue;
8239 }
8240 is_cookie2 = 1;
8241 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242 }
8243
Willy Tarreau24581ba2010-08-31 22:39:35 +02008244 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8245 * <prev> points to the colon.
8246 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008247 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008248
Willy Tarreau24581ba2010-08-31 22:39:35 +02008249 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8250 * check-cache is enabled) and we are not interested in checking
8251 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008252 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008253 if (s->be->cookie_name == NULL &&
8254 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008255 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256 return;
8257
Willy Tarreau24581ba2010-08-31 22:39:35 +02008258 /* OK so now we know we have to process this response cookie.
8259 * The format of the Set-Cookie header is slightly different
8260 * from the format of the Cookie header in that it does not
8261 * support the comma as a cookie delimiter (thus the header
8262 * cannot be folded) because the Expires attribute described in
8263 * the original Netscape's spec may contain an unquoted date
8264 * with a comma inside. We have to live with this because
8265 * many browsers don't support Max-Age and some browsers don't
8266 * support quoted strings. However the Set-Cookie2 header is
8267 * clean.
8268 *
8269 * We have to keep multiple pointers in order to support cookie
8270 * removal at the beginning, middle or end of header without
8271 * corrupting the header (in case of set-cookie2). A special
8272 * pointer, <scav> points to the beginning of the set-cookie-av
8273 * fields after the first semi-colon. The <next> pointer points
8274 * either to the end of line (set-cookie) or next unquoted comma
8275 * (set-cookie2). All of these headers are valid :
8276 *
8277 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8278 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8279 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8280 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8281 * | | | | | | | | | |
8282 * | | | | | | | | +-> next hdr_end <--+
8283 * | | | | | | | +------------> scav
8284 * | | | | | | +--------------> val_end
8285 * | | | | | +--------------------> val_beg
8286 * | | | | +----------------------> equal
8287 * | | | +------------------------> att_end
8288 * | | +----------------------------> att_beg
8289 * | +------------------------------> prev
8290 * +-----------------------------------------> hdr_beg
8291 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008292
Willy Tarreau24581ba2010-08-31 22:39:35 +02008293 for (; prev < hdr_end; prev = next) {
8294 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008295
Willy Tarreau24581ba2010-08-31 22:39:35 +02008296 /* find att_beg */
8297 att_beg = prev + 1;
8298 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8299 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008300
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 /* find att_end : this is the first character after the last non
8302 * space before the equal. It may be equal to hdr_end.
8303 */
8304 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008305
Willy Tarreau24581ba2010-08-31 22:39:35 +02008306 while (equal < hdr_end) {
8307 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8308 break;
8309 if (http_is_spht[(unsigned char)*equal++])
8310 continue;
8311 att_end = equal;
8312 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313
Willy Tarreau24581ba2010-08-31 22:39:35 +02008314 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8315 * is between <att_beg> and <equal>, both may be identical.
8316 */
8317
8318 /* look for end of cookie if there is an equal sign */
8319 if (equal < hdr_end && *equal == '=') {
8320 /* look for the beginning of the value */
8321 val_beg = equal + 1;
8322 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8323 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324
Willy Tarreau24581ba2010-08-31 22:39:35 +02008325 /* find the end of the value, respecting quotes */
8326 next = find_cookie_value_end(val_beg, hdr_end);
8327
8328 /* make val_end point to the first white space or delimitor after the value */
8329 val_end = next;
8330 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8331 val_end--;
8332 } else {
8333 /* <equal> points to next comma, semi-colon or EOL */
8334 val_beg = val_end = next = equal;
8335 }
8336
8337 if (next < hdr_end) {
8338 /* Set-Cookie2 supports multiple cookies, and <next> points to
8339 * a colon or semi-colon before the end. So skip all attr-value
8340 * pairs and look for the next comma. For Set-Cookie, since
8341 * commas are permitted in values, skip to the end.
8342 */
8343 if (is_cookie2)
8344 next = find_hdr_value_end(next, hdr_end);
8345 else
8346 next = hdr_end;
8347 }
8348
8349 /* Now everything is as on the diagram above */
8350
8351 /* Ignore cookies with no equal sign */
8352 if (equal == val_end)
8353 continue;
8354
8355 /* If there are spaces around the equal sign, we need to
8356 * strip them otherwise we'll get trouble for cookie captures,
8357 * or even for rewrites. Since this happens extremely rarely,
8358 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008359 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008360 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8361 int stripped_before = 0;
8362 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008363
Willy Tarreau24581ba2010-08-31 22:39:35 +02008364 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008365 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008366 equal += stripped_before;
8367 val_beg += stripped_before;
8368 }
8369
8370 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008371 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008372 val_beg += stripped_after;
8373 stripped_before += stripped_after;
8374 }
8375
8376 val_end += stripped_before;
8377 next += stripped_before;
8378 hdr_end += stripped_before;
8379 hdr_next += stripped_before;
8380 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008381 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008382 }
8383
8384 /* First, let's see if we want to capture this cookie. We check
8385 * that we don't already have a server side cookie, because we
8386 * can only capture one. Also as an optimisation, we ignore
8387 * cookies shorter than the declared name.
8388 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008389 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008390 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008391 (val_end - att_beg >= sess->fe->capture_namelen) &&
8392 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008393 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008394 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008395 Alert("HTTP logging : out of memory.\n");
8396 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008397 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008398 if (log_len > sess->fe->capture_len)
8399 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008400 memcpy(txn->srv_cookie, att_beg, log_len);
8401 txn->srv_cookie[log_len] = 0;
8402 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008403 }
8404
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008405 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008407 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008408 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8409 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008410 /* assume passive cookie by default */
8411 txn->flags &= ~TX_SCK_MASK;
8412 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008413
8414 /* If the cookie is in insert mode on a known server, we'll delete
8415 * this occurrence because we'll insert another one later.
8416 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008417 * a direct access.
8418 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008419 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008420 /* The "preserve" flag was set, we don't want to touch the
8421 * server's cookie.
8422 */
8423 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008424 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008425 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008426 /* this cookie must be deleted */
8427 if (*prev == ':' && next == hdr_end) {
8428 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008429 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008430 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8431 txn->hdr_idx.used--;
8432 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008433 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008434 hdr_next += delta;
8435 http_msg_move_end(&txn->rsp, delta);
8436 /* note: while both invalid now, <next> and <hdr_end>
8437 * are still equal, so the for() will stop as expected.
8438 */
8439 } else {
8440 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008441 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008442 next = prev;
8443 hdr_end += delta;
8444 hdr_next += delta;
8445 cur_hdr->len += delta;
8446 http_msg_move_end(&txn->rsp, delta);
8447 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008448 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008449 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008450 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008451 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008452 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008453 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008454 * with this server since we know it.
8455 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008456 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008457 next += delta;
8458 hdr_end += delta;
8459 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008460 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008461 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008462
Willy Tarreauf1348312010-10-07 15:54:11 +02008463 txn->flags &= ~TX_SCK_MASK;
8464 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008465 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008466 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008467 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008468 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008469 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008470 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008471 next += delta;
8472 hdr_end += delta;
8473 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008475 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008476
Willy Tarreau827aee92011-03-10 16:55:02 +01008477 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008478 txn->flags &= ~TX_SCK_MASK;
8479 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008480 }
8481 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008482 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008483 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008484 int cmp_len, value_len;
8485 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008486
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008487 if (s->be->options2 & PR_O2_AS_PFX) {
8488 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8489 value_begin = att_beg + s->be->appsession_name_len;
8490 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008491 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008492 cmp_len = att_end - att_beg;
8493 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008494 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008495 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008496
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008497 if ((cmp_len == s->be->appsession_name_len) &&
8498 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008499 /* free a possibly previously allocated memory */
8500 pool_free2(apools.sessid, txn->sessid);
8501
Willy Tarreau87b09662015-04-03 00:22:06 +02008502 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008503 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008504 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008505 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008506 return;
8507 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008508 memcpy(txn->sessid, value_begin, value_len);
8509 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008511 }
8512 /* that's done for this cookie, check the next one on the same
8513 * line when next != hdr_end (only if is_cookie2).
8514 */
8515 }
8516 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008517 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008518 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008519
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008520 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008521 appsess *asession = NULL;
8522 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008523 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008524 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008525 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008526 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8527 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008528 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008529 return;
8530 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008531 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8532
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008533 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8534 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008535 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8536 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008537 return;
8538 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008539 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8540 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008541
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008542 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008543 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008544 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008545 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8546 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008547 return;
8548 }
8549 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008550 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008551
8552 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008553 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008554 }
8555
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008556 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008557 asession->request_count++;
8558 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008559}
8560
8561
Willy Tarreaua15645d2007-03-18 16:22:39 +01008562/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008563 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008564 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008565void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008566{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008567 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008568 char *p1, *p2;
8569
8570 char *cur_ptr, *cur_end, *cur_next;
8571 int cur_idx;
8572
Willy Tarreau5df51872007-11-25 16:20:08 +01008573 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008574 return;
8575
8576 /* Iterate through the headers.
8577 * we start with the start line.
8578 */
8579 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008580 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008581
8582 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8583 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008584 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008585
8586 cur_hdr = &txn->hdr_idx.v[cur_idx];
8587 cur_ptr = cur_next;
8588 cur_end = cur_ptr + cur_hdr->len;
8589 cur_next = cur_end + cur_hdr->cr + 1;
8590
8591 /* We have one full header between cur_ptr and cur_end, and the
8592 * next header starts at cur_next. We're only interested in
8593 * "Cookie:" headers.
8594 */
8595
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008596 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8597 if (val) {
8598 if ((cur_end - (cur_ptr + val) >= 8) &&
8599 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8600 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8601 return;
8602 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008603 }
8604
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008605 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8606 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008607 continue;
8608
8609 /* OK, right now we know we have a cache-control header at cur_ptr */
8610
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008611 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008612
8613 if (p1 >= cur_end) /* no more info */
8614 continue;
8615
8616 /* p1 is at the beginning of the value */
8617 p2 = p1;
8618
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008619 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008620 p2++;
8621
8622 /* we have a complete value between p1 and p2 */
8623 if (p2 < cur_end && *p2 == '=') {
8624 /* we have something of the form no-cache="set-cookie" */
8625 if ((cur_end - p1 >= 21) &&
8626 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8627 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008628 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008629 continue;
8630 }
8631
8632 /* OK, so we know that either p2 points to the end of string or to a comma */
8633 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008634 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008635 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8636 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8637 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008638 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008639 return;
8640 }
8641
8642 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008643 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008644 continue;
8645 }
8646 }
8647}
8648
8649
Willy Tarreau58f10d72006-12-04 02:26:12 +01008650/*
8651 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008652 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008653 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008654void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008655{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008656 char *end_params, *first_param, *cur_param, *next_param;
8657 char separator;
8658 int value_len;
8659
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008660 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008661
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008662 if (s->be->appsession_name == NULL ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02008663 (s->txn->meth != HTTP_METH_GET && s->txn->meth != HTTP_METH_POST && s->txn->meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008664 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008665 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008666
Cyril Bontéb21570a2009-11-29 20:04:48 +01008667 first_param = NULL;
8668 switch (mode) {
8669 case PR_O2_AS_M_PP:
8670 first_param = memchr(begin, ';', len);
8671 break;
8672 case PR_O2_AS_M_QS:
8673 first_param = memchr(begin, '?', len);
8674 break;
8675 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008676
Cyril Bontéb21570a2009-11-29 20:04:48 +01008677 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008678 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008679 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008680
Cyril Bontéb21570a2009-11-29 20:04:48 +01008681 switch (mode) {
8682 case PR_O2_AS_M_PP:
8683 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8684 end_params = (char *) begin + len;
8685 }
8686 separator = ';';
8687 break;
8688 case PR_O2_AS_M_QS:
8689 end_params = (char *) begin + len;
8690 separator = '&';
8691 break;
8692 default:
8693 /* unknown mode, shouldn't happen */
8694 return;
8695 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008696
Cyril Bontéb21570a2009-11-29 20:04:48 +01008697 cur_param = next_param = end_params;
8698 while (cur_param > first_param) {
8699 cur_param--;
8700 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8701 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008702 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8703 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8704 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008705 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008706 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8707 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008708 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008709 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008710 }
8711 break;
8712 }
8713 next_param = cur_param;
8714 }
8715 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008716#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008717 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008718 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008719#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008720}
8721
Willy Tarreaub2513902006-12-17 14:52:38 +01008722/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008723 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008724 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008725 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008726 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008727 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008728 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008729 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008730 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008731int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008732{
8733 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008734 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008735 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008736
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008737 if (!uri_auth)
8738 return 0;
8739
Cyril Bonté70be45d2010-10-12 00:14:35 +02008740 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008741 return 0;
8742
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008743 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008744 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008745 return 0;
8746
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008747 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008748 return 0;
8749
Willy Tarreaub2513902006-12-17 14:52:38 +01008750 return 1;
8751}
8752
Willy Tarreau4076a152009-04-02 15:18:36 +02008753/*
8754 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008755 * By default it tries to report the error position as msg->err_pos. However if
8756 * this one is not set, it will then report msg->next, which is the last known
8757 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008758 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008759 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008760void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008761 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008762 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008763{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008764 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008765 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008766 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008767
Willy Tarreau9b28e032012-10-12 23:49:43 +02008768 es->len = MIN(chn->buf->i, sizeof(es->buf));
8769 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008770 len1 = MIN(len1, es->len);
8771 len2 = es->len - len1; /* remaining data if buffer wraps */
8772
Willy Tarreau9b28e032012-10-12 23:49:43 +02008773 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008774 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008775 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008776
Willy Tarreau4076a152009-04-02 15:18:36 +02008777 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008778 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008779 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008780 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008781
Willy Tarreau4076a152009-04-02 15:18:36 +02008782 es->when = date; // user-visible date
8783 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008784 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008785 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008786 if (objt_conn(sess->origin))
8787 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008788 else
8789 memset(&es->src, 0, sizeof(es->src));
8790
Willy Tarreau078272e2010-12-12 12:46:33 +01008791 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008792 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008793 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008794 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008795 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008796 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008797 es->b_out = chn->buf->o;
8798 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008799 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008800 es->m_clen = msg->chunk_len;
8801 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008802}
Willy Tarreaub2513902006-12-17 14:52:38 +01008803
Willy Tarreau294c4732011-12-16 21:35:50 +01008804/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8805 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8806 * performed over the whole headers. Otherwise it must contain a valid header
8807 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8808 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8809 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8810 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008811 * -1. The value fetch stops at commas, so this function is suited for use with
8812 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008813 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008814 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008815unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008816 struct hdr_idx *idx, int occ,
8817 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008818{
Willy Tarreau294c4732011-12-16 21:35:50 +01008819 struct hdr_ctx local_ctx;
8820 char *ptr_hist[MAX_HDR_HISTORY];
8821 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008822 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008823 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008824
Willy Tarreau294c4732011-12-16 21:35:50 +01008825 if (!ctx) {
8826 local_ctx.idx = 0;
8827 ctx = &local_ctx;
8828 }
8829
Willy Tarreaubce70882009-09-07 11:51:47 +02008830 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008831 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008832 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008833 occ--;
8834 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008835 *vptr = ctx->line + ctx->val;
8836 *vlen = ctx->vlen;
8837 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008838 }
8839 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008840 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008841 }
8842
8843 /* negative occurrence, we scan all the list then walk back */
8844 if (-occ > MAX_HDR_HISTORY)
8845 return 0;
8846
Willy Tarreau294c4732011-12-16 21:35:50 +01008847 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008848 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008849 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8850 len_hist[hist_ptr] = ctx->vlen;
8851 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008852 hist_ptr = 0;
8853 found++;
8854 }
8855 if (-occ > found)
8856 return 0;
8857 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008858 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8859 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8860 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008861 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008862 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008863 if (hist_ptr >= MAX_HDR_HISTORY)
8864 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008865 *vptr = ptr_hist[hist_ptr];
8866 *vlen = len_hist[hist_ptr];
8867 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008868}
8869
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008870/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8871 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8872 * performed over the whole headers. Otherwise it must contain a valid header
8873 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8874 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8875 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8876 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8877 * -1. This function differs from http_get_hdr() in that it only returns full
8878 * line header values and does not stop at commas.
8879 * The return value is 0 if nothing was found, or non-zero otherwise.
8880 */
8881unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8882 struct hdr_idx *idx, int occ,
8883 struct hdr_ctx *ctx, char **vptr, int *vlen)
8884{
8885 struct hdr_ctx local_ctx;
8886 char *ptr_hist[MAX_HDR_HISTORY];
8887 int len_hist[MAX_HDR_HISTORY];
8888 unsigned int hist_ptr;
8889 int found;
8890
8891 if (!ctx) {
8892 local_ctx.idx = 0;
8893 ctx = &local_ctx;
8894 }
8895
8896 if (occ >= 0) {
8897 /* search from the beginning */
8898 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8899 occ--;
8900 if (occ <= 0) {
8901 *vptr = ctx->line + ctx->val;
8902 *vlen = ctx->vlen;
8903 return 1;
8904 }
8905 }
8906 return 0;
8907 }
8908
8909 /* negative occurrence, we scan all the list then walk back */
8910 if (-occ > MAX_HDR_HISTORY)
8911 return 0;
8912
8913 found = hist_ptr = 0;
8914 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8915 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8916 len_hist[hist_ptr] = ctx->vlen;
8917 if (++hist_ptr >= MAX_HDR_HISTORY)
8918 hist_ptr = 0;
8919 found++;
8920 }
8921 if (-occ > found)
8922 return 0;
8923 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8924 * find occurrence -occ, so we have to check [hist_ptr+occ].
8925 */
8926 hist_ptr += occ;
8927 if (hist_ptr >= MAX_HDR_HISTORY)
8928 hist_ptr -= MAX_HDR_HISTORY;
8929 *vptr = ptr_hist[hist_ptr];
8930 *vlen = len_hist[hist_ptr];
8931 return 1;
8932}
8933
Willy Tarreaubaaee002006-06-26 02:48:02 +02008934/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008935 * Print a debug line with a header. Always stop at the first CR or LF char,
8936 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8937 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008938 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008939void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008940{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008941 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008942 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008943
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008944 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008945 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008946 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008947 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 +02008948
8949 for (max = 0; start + max < end; max++)
8950 if (start[max] == '\r' || start[max] == '\n')
8951 break;
8952
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008953 UBOUND(max, trash.size - trash.len - 3);
8954 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8955 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008956 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008957}
8958
Willy Tarreaueee5b512015-04-03 23:46:31 +02008959
8960/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8961 * The hdr_idx is allocated as well. In case of allocation failure, everything
8962 * allocated is freed and NULL is returned. Otherwise the new transaction is
8963 * assigned to the stream and returned.
8964 */
8965struct http_txn *http_alloc_txn(struct stream *s)
8966{
8967 struct http_txn *txn = s->txn;
8968
8969 if (txn)
8970 return txn;
8971
8972 txn = pool_alloc2(pool2_http_txn);
8973 if (!txn)
8974 return txn;
8975
8976 txn->hdr_idx.size = global.tune.max_http_hdr;
8977 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8978 if (!txn->hdr_idx.v) {
8979 pool_free2(pool2_http_txn, txn);
8980 return NULL;
8981 }
8982
8983 s->txn = txn;
8984 return txn;
8985}
8986
Willy Tarreau0937bc42009-12-22 15:03:09 +01008987/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008988 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008989 * the required fields are properly allocated and that we only need to (re)init
8990 * them. This should be used before processing any new request.
8991 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008992void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008993{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008994 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008995 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008996
8997 txn->flags = 0;
8998 txn->status = -1;
8999
Willy Tarreauf64d1412010-10-07 20:06:11 +02009000 txn->cookie_first_date = 0;
9001 txn->cookie_last_date = 0;
9002
Willy Tarreaueee5b512015-04-03 23:46:31 +02009003 txn->sessid = NULL;
9004 txn->srv_cookie = NULL;
9005 txn->cli_cookie = NULL;
9006 txn->uri = NULL;
9007
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01009008 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02009009 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01009010 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01009011 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02009012 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01009013 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01009014 txn->req.chunk_len = 0LL;
9015 txn->req.body_len = 0LL;
9016 txn->rsp.chunk_len = 0LL;
9017 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009018 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
9019 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009020 txn->req.chn = &s->req;
9021 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009022
9023 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009024
9025 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
9026 if (fe->options2 & PR_O2_REQBUG_OK)
9027 txn->req.err_pos = -1; /* let buggy requests pass */
9028
Willy Tarreau0937bc42009-12-22 15:03:09 +01009029 if (txn->hdr_idx.v)
9030 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009031
9032 vars_init(&s->vars_txn, SCOPE_TXN);
9033 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01009034}
9035
9036/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02009037void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01009038{
Willy Tarreaueee5b512015-04-03 23:46:31 +02009039 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009040 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01009041
Willy Tarreau75195602014-03-11 15:48:55 +01009042 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02009043 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01009044 s->comp_algo->end(&s->comp_ctx);
9045 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02009046 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01009047
Willy Tarreau0937bc42009-12-22 15:03:09 +01009048 /* these ones will have been dynamically allocated */
9049 pool_free2(pool2_requri, txn->uri);
9050 pool_free2(pool2_capture, txn->cli_cookie);
9051 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01009052 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01009053 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009054
William Lallemanda73203e2012-03-12 12:48:57 +01009055 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01009056 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009057 txn->uri = NULL;
9058 txn->srv_cookie = NULL;
9059 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01009060
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009061 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01009062 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02009063 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009064 pool_free2(h->pool, s->req_cap[h->index]);
9065 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01009066 }
9067
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009068 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01009069 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02009070 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02009071 pool_free2(h->pool, s->res_cap[h->index]);
9072 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01009073 }
9074
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009075 vars_prune(&s->vars_txn, s);
9076 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01009077}
9078
9079/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02009080void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01009081{
9082 http_end_txn(s);
9083 http_init_txn(s);
9084
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01009085 /* reinitialise the current rule list pointer to NULL. We are sure that
9086 * any rulelist match the NULL pointer.
9087 */
9088 s->current_rule_list = NULL;
9089
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009090 s->be = strm_fe(s);
9091 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02009092 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02009093 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01009094 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009095 /* re-init store persistence */
9096 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01009097 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01009098
Willy Tarreau0937bc42009-12-22 15:03:09 +01009099 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009100
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009101 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01009102
Willy Tarreau739cfba2010-01-25 23:11:14 +01009103 /* We must trim any excess data from the response buffer, because we
9104 * may have blocked an invalid response from a server that we don't
9105 * want to accidentely forward once we disable the analysers, nor do
9106 * we want those data to come along with next response. A typical
9107 * example of such data would be from a buggy server responding to
9108 * a HEAD with some data, or sending more than the advertised
9109 * content-length.
9110 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009111 if (unlikely(s->res.buf->i))
9112 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009113
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009114 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009115 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009116
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009117 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009118 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009119
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009120 s->req.rex = TICK_ETERNITY;
9121 s->req.wex = TICK_ETERNITY;
9122 s->req.analyse_exp = TICK_ETERNITY;
9123 s->res.rex = TICK_ETERNITY;
9124 s->res.wex = TICK_ETERNITY;
9125 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009126}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009127
Sasha Pachev218f0642014-06-16 12:05:59 -06009128void free_http_res_rules(struct list *r)
9129{
9130 struct http_res_rule *tr, *pr;
9131
9132 list_for_each_entry_safe(pr, tr, r, list) {
9133 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009134 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009135 free(pr);
9136 }
9137}
9138
9139void free_http_req_rules(struct list *r)
9140{
Willy Tarreauff011f22011-01-06 17:51:27 +01009141 struct http_req_rule *tr, *pr;
9142
9143 list_for_each_entry_safe(pr, tr, r, list) {
9144 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01009145 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009146 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009147
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009148 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009149 free(pr);
9150 }
9151}
9152
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009153/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01009154struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9155{
9156 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009157 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009158 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009159 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009160
9161 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
9162 if (!rule) {
9163 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009164 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009165 }
9166
CJ Ess108b1dd2015-04-07 12:03:37 -04009167 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009168 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01009169 rule->action = HTTP_REQ_ACT_ALLOW;
9170 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009171 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009172 int code;
9173 int hc;
9174
Willy Tarreauff011f22011-01-06 17:51:27 +01009175 rule->action = HTTP_REQ_ACT_DENY;
9176 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009177 if (strcmp(args[cur_arg], "deny_status") == 0) {
9178 cur_arg++;
9179 if (!args[cur_arg]) {
9180 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9181 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9182 goto out_err;
9183 }
9184
9185 code = atol(args[cur_arg]);
9186 cur_arg++;
9187 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9188 if (http_err_codes[hc] == code) {
9189 rule->deny_status = hc;
9190 break;
9191 }
9192 }
9193
9194 if (hc >= HTTP_ERR_SIZE) {
9195 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9196 file, linenum, code);
9197 }
9198 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009199 } else if (!strcmp(args[0], "tarpit")) {
9200 rule->action = HTTP_REQ_ACT_TARPIT;
9201 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009202 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009203 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009204 cur_arg = 1;
9205
9206 while(*args[cur_arg]) {
9207 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009208 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009209 cur_arg+=2;
9210 continue;
9211 } else
9212 break;
9213 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009214 } else if (!strcmp(args[0], "set-nice")) {
9215 rule->action = HTTP_REQ_ACT_SET_NICE;
9216 cur_arg = 1;
9217
9218 if (!*args[cur_arg] ||
9219 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9220 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9221 file, linenum, args[0]);
9222 goto out_err;
9223 }
9224 rule->arg.nice = atoi(args[cur_arg]);
9225 if (rule->arg.nice < -1024)
9226 rule->arg.nice = -1024;
9227 else if (rule->arg.nice > 1024)
9228 rule->arg.nice = 1024;
9229 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009230 } else if (!strcmp(args[0], "set-tos")) {
9231#ifdef IP_TOS
9232 char *err;
9233 rule->action = HTTP_REQ_ACT_SET_TOS;
9234 cur_arg = 1;
9235
9236 if (!*args[cur_arg] ||
9237 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9238 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9239 file, linenum, args[0]);
9240 goto out_err;
9241 }
9242
9243 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9244 if (err && *err != '\0') {
9245 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9246 file, linenum, err, args[0]);
9247 goto out_err;
9248 }
9249 cur_arg++;
9250#else
9251 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9252 goto out_err;
9253#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009254 } else if (!strcmp(args[0], "set-mark")) {
9255#ifdef SO_MARK
9256 char *err;
9257 rule->action = HTTP_REQ_ACT_SET_MARK;
9258 cur_arg = 1;
9259
9260 if (!*args[cur_arg] ||
9261 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9262 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9263 file, linenum, args[0]);
9264 goto out_err;
9265 }
9266
9267 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9268 if (err && *err != '\0') {
9269 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9270 file, linenum, err, args[0]);
9271 goto out_err;
9272 }
9273 cur_arg++;
9274 global.last_checks |= LSTCHK_NETADM;
9275#else
9276 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9277 goto out_err;
9278#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009279 } else if (!strcmp(args[0], "set-log-level")) {
9280 rule->action = HTTP_REQ_ACT_SET_LOGL;
9281 cur_arg = 1;
9282
9283 if (!*args[cur_arg] ||
9284 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9285 bad_log_level:
9286 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9287 file, linenum, args[0]);
9288 goto out_err;
9289 }
9290 if (strcmp(args[cur_arg], "silent") == 0)
9291 rule->arg.loglevel = -1;
9292 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9293 goto bad_log_level;
9294 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009295 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9296 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9297 cur_arg = 1;
9298
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009299 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9300 (*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 +01009301 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9302 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009303 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009304 }
9305
9306 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9307 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9308 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009309
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009310 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009311 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009312 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9313 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009314 free(proxy->conf.lfs_file);
9315 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9316 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009317 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009318 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9319 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009320 cur_arg = 1;
9321
9322 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009323 (*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 -06009324 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9325 file, linenum, args[0]);
9326 goto out_err;
9327 }
9328
9329 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9330 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9331 LIST_INIT(&rule->arg.hdr_add.fmt);
9332
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009333 error = NULL;
9334 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9335 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9336 args[cur_arg + 1], error);
9337 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009338 goto out_err;
9339 }
9340
9341 proxy->conf.args.ctx = ARGC_HRQ;
9342 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9343 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9344 file, linenum);
9345
9346 free(proxy->conf.lfs_file);
9347 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9348 proxy->conf.lfs_line = proxy->conf.args.line;
9349 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009350 } else if (strcmp(args[0], "del-header") == 0) {
9351 rule->action = HTTP_REQ_ACT_DEL_HDR;
9352 cur_arg = 1;
9353
9354 if (!*args[cur_arg] ||
9355 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9356 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9357 file, linenum, args[0]);
9358 goto out_err;
9359 }
9360
9361 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9362 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9363
9364 proxy->conf.args.ctx = ARGC_HRQ;
9365 free(proxy->conf.lfs_file);
9366 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9367 proxy->conf.lfs_line = proxy->conf.args.line;
9368 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009369 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9370 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009371 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009372 struct sample_expr *expr;
9373 unsigned int where;
9374 char *err = NULL;
9375
9376 cur_arg = 1;
9377 proxy->conf.args.ctx = ARGC_TRK;
9378
9379 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9380 if (!expr) {
9381 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9382 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9383 free(err);
9384 goto out_err;
9385 }
9386
9387 where = 0;
9388 if (proxy->cap & PR_CAP_FE)
9389 where |= SMP_VAL_FE_HRQ_HDR;
9390 if (proxy->cap & PR_CAP_BE)
9391 where |= SMP_VAL_BE_HRQ_HDR;
9392
9393 if (!(expr->fetch->val & where)) {
9394 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9395 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9396 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9397 args[cur_arg-1], sample_src_names(expr->fetch->use));
9398 free(expr);
9399 goto out_err;
9400 }
9401
9402 if (strcmp(args[cur_arg], "table") == 0) {
9403 cur_arg++;
9404 if (!args[cur_arg]) {
9405 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9406 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9407 free(expr);
9408 goto out_err;
9409 }
9410 /* we copy the table name for now, it will be resolved later */
9411 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9412 cur_arg++;
9413 }
9414 rule->act_prm.trk_ctr.expr = expr;
9415 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009416 } else if (strcmp(args[0], "redirect") == 0) {
9417 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009418 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009419
Willy Tarreaube4653b2015-05-28 15:26:58 +02009420 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009421 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9422 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9423 goto out_err;
9424 }
9425
9426 /* this redirect rule might already contain a parsed condition which
9427 * we'll pass to the http-request rule.
9428 */
9429 rule->action = HTTP_REQ_ACT_REDIR;
9430 rule->arg.redir = redir;
9431 rule->cond = redir->cond;
9432 redir->cond = NULL;
9433 cur_arg = 2;
9434 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009435 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9436 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9437 rule->action = HTTP_REQ_ACT_ADD_ACL;
9438 /*
9439 * '+ 8' for 'add-acl('
9440 * '- 9' for 'add-acl(' + trailing ')'
9441 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009442 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009443
9444 cur_arg = 1;
9445
9446 if (!*args[cur_arg] ||
9447 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9448 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9449 file, linenum, args[0]);
9450 goto out_err;
9451 }
9452
9453 LIST_INIT(&rule->arg.map.key);
9454 proxy->conf.args.ctx = ARGC_HRQ;
9455 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9456 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9457 file, linenum);
9458 free(proxy->conf.lfs_file);
9459 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9460 proxy->conf.lfs_line = proxy->conf.args.line;
9461 cur_arg += 1;
9462 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9463 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9464 rule->action = HTTP_REQ_ACT_DEL_ACL;
9465 /*
9466 * '+ 8' for 'del-acl('
9467 * '- 9' for 'del-acl(' + trailing ')'
9468 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009469 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009470
9471 cur_arg = 1;
9472
9473 if (!*args[cur_arg] ||
9474 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9475 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9476 file, linenum, args[0]);
9477 goto out_err;
9478 }
9479
9480 LIST_INIT(&rule->arg.map.key);
9481 proxy->conf.args.ctx = ARGC_HRQ;
9482 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9483 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9484 file, linenum);
9485 free(proxy->conf.lfs_file);
9486 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9487 proxy->conf.lfs_line = proxy->conf.args.line;
9488 cur_arg += 1;
9489 } else if (strncmp(args[0], "del-map", 7) == 0) {
9490 /* http-request del-map(<reference (map name)>) <key pattern> */
9491 rule->action = HTTP_REQ_ACT_DEL_MAP;
9492 /*
9493 * '+ 8' for 'del-map('
9494 * '- 9' for 'del-map(' + trailing ')'
9495 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009496 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009497
9498 cur_arg = 1;
9499
9500 if (!*args[cur_arg] ||
9501 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9502 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9503 file, linenum, args[0]);
9504 goto out_err;
9505 }
9506
9507 LIST_INIT(&rule->arg.map.key);
9508 proxy->conf.args.ctx = ARGC_HRQ;
9509 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9510 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9511 file, linenum);
9512 free(proxy->conf.lfs_file);
9513 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9514 proxy->conf.lfs_line = proxy->conf.args.line;
9515 cur_arg += 1;
9516 } else if (strncmp(args[0], "set-map", 7) == 0) {
9517 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9518 rule->action = HTTP_REQ_ACT_SET_MAP;
9519 /*
9520 * '+ 8' for 'set-map('
9521 * '- 9' for 'set-map(' + trailing ')'
9522 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009523 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009524
9525 cur_arg = 1;
9526
9527 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9528 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9529 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9530 file, linenum, args[0]);
9531 goto out_err;
9532 }
9533
9534 LIST_INIT(&rule->arg.map.key);
9535 LIST_INIT(&rule->arg.map.value);
9536 proxy->conf.args.ctx = ARGC_HRQ;
9537
9538 /* key pattern */
9539 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9540 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9541 file, linenum);
9542
9543 /* value pattern */
9544 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9545 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9546 file, linenum);
9547 free(proxy->conf.lfs_file);
9548 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9549 proxy->conf.lfs_line = proxy->conf.args.line;
9550
9551 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009552 } else if (strncmp(args[0], "set-src", 7) == 0) {
9553 struct sample_expr *expr;
9554 unsigned int where;
9555 char *err = NULL;
9556
9557 cur_arg = 1;
9558 proxy->conf.args.ctx = ARGC_HRQ;
9559
9560 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9561 if (!expr) {
9562 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9563 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9564 free(err);
9565 goto out_err;
9566 }
9567
9568 where = 0;
9569 if (proxy->cap & PR_CAP_FE)
9570 where |= SMP_VAL_FE_HRQ_HDR;
9571 if (proxy->cap & PR_CAP_BE)
9572 where |= SMP_VAL_BE_HRQ_HDR;
9573
9574 if (!(expr->fetch->val & where)) {
9575 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9576 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9577 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9578 args[cur_arg-1], sample_src_names(expr->fetch->use));
9579 free(expr);
9580 goto out_err;
9581 }
9582
9583 rule->arg.act.p[0] = expr;
9584 rule->action = HTTP_REQ_ACT_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009585 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9586 char *errmsg = NULL;
9587 cur_arg = 1;
9588 /* try in the module list */
9589 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9590 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9591 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9592 free(errmsg);
9593 goto out_err;
9594 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009595 } else {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009596 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 +01009597 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009598 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009599 }
9600
9601 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9602 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009603 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009604
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009605 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9606 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9607 file, linenum, args[0], errmsg);
9608 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009609 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009610 }
9611 rule->cond = cond;
9612 }
9613 else if (*args[cur_arg]) {
9614 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9615 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9616 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009617 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009618 }
9619
9620 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009621 out_err:
9622 free(rule);
9623 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009624}
9625
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009626/* parse an "http-respose" rule */
9627struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9628{
9629 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009630 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009631 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009632 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009633
9634 rule = calloc(1, sizeof(*rule));
9635 if (!rule) {
9636 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9637 goto out_err;
9638 }
9639
9640 if (!strcmp(args[0], "allow")) {
9641 rule->action = HTTP_RES_ACT_ALLOW;
9642 cur_arg = 1;
9643 } else if (!strcmp(args[0], "deny")) {
9644 rule->action = HTTP_RES_ACT_DENY;
9645 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009646 } else if (!strcmp(args[0], "set-nice")) {
9647 rule->action = HTTP_RES_ACT_SET_NICE;
9648 cur_arg = 1;
9649
9650 if (!*args[cur_arg] ||
9651 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9652 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9653 file, linenum, args[0]);
9654 goto out_err;
9655 }
9656 rule->arg.nice = atoi(args[cur_arg]);
9657 if (rule->arg.nice < -1024)
9658 rule->arg.nice = -1024;
9659 else if (rule->arg.nice > 1024)
9660 rule->arg.nice = 1024;
9661 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009662 } else if (!strcmp(args[0], "set-tos")) {
9663#ifdef IP_TOS
9664 char *err;
9665 rule->action = HTTP_RES_ACT_SET_TOS;
9666 cur_arg = 1;
9667
9668 if (!*args[cur_arg] ||
9669 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9670 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9671 file, linenum, args[0]);
9672 goto out_err;
9673 }
9674
9675 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9676 if (err && *err != '\0') {
9677 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9678 file, linenum, err, args[0]);
9679 goto out_err;
9680 }
9681 cur_arg++;
9682#else
9683 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9684 goto out_err;
9685#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009686 } else if (!strcmp(args[0], "set-mark")) {
9687#ifdef SO_MARK
9688 char *err;
9689 rule->action = HTTP_RES_ACT_SET_MARK;
9690 cur_arg = 1;
9691
9692 if (!*args[cur_arg] ||
9693 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9694 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9695 file, linenum, args[0]);
9696 goto out_err;
9697 }
9698
9699 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9700 if (err && *err != '\0') {
9701 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9702 file, linenum, err, args[0]);
9703 goto out_err;
9704 }
9705 cur_arg++;
9706 global.last_checks |= LSTCHK_NETADM;
9707#else
9708 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9709 goto out_err;
9710#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009711 } else if (!strcmp(args[0], "set-log-level")) {
9712 rule->action = HTTP_RES_ACT_SET_LOGL;
9713 cur_arg = 1;
9714
9715 if (!*args[cur_arg] ||
9716 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9717 bad_log_level:
9718 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9719 file, linenum, args[0]);
9720 goto out_err;
9721 }
9722 if (strcmp(args[cur_arg], "silent") == 0)
9723 rule->arg.loglevel = -1;
9724 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9725 goto bad_log_level;
9726 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009727 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9728 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9729 cur_arg = 1;
9730
9731 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9732 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9733 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9734 file, linenum, args[0]);
9735 goto out_err;
9736 }
9737
9738 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9739 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9740 LIST_INIT(&rule->arg.hdr_add.fmt);
9741
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009742 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009743 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009744 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9745 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009746 free(proxy->conf.lfs_file);
9747 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9748 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009749 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009750 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009751 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009752 cur_arg = 1;
9753
9754 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009755 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9756 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009757 file, linenum, args[0]);
9758 goto out_err;
9759 }
9760
9761 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9762 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9763 LIST_INIT(&rule->arg.hdr_add.fmt);
9764
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009765 error = NULL;
9766 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9767 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9768 args[cur_arg + 1], error);
9769 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009770 goto out_err;
9771 }
9772
9773 proxy->conf.args.ctx = ARGC_HRQ;
9774 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9775 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9776 file, linenum);
9777
9778 free(proxy->conf.lfs_file);
9779 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9780 proxy->conf.lfs_line = proxy->conf.args.line;
9781 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009782 } else if (strcmp(args[0], "del-header") == 0) {
9783 rule->action = HTTP_RES_ACT_DEL_HDR;
9784 cur_arg = 1;
9785
9786 if (!*args[cur_arg] ||
9787 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9788 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9789 file, linenum, args[0]);
9790 goto out_err;
9791 }
9792
9793 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9794 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9795
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009796 proxy->conf.args.ctx = ARGC_HRS;
9797 free(proxy->conf.lfs_file);
9798 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9799 proxy->conf.lfs_line = proxy->conf.args.line;
9800 cur_arg += 1;
9801 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9802 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9803 rule->action = HTTP_RES_ACT_ADD_ACL;
9804 /*
9805 * '+ 8' for 'add-acl('
9806 * '- 9' for 'add-acl(' + trailing ')'
9807 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009808 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009809
9810 cur_arg = 1;
9811
9812 if (!*args[cur_arg] ||
9813 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9814 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9815 file, linenum, args[0]);
9816 goto out_err;
9817 }
9818
9819 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009820 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009821 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9822 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9823 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009824 free(proxy->conf.lfs_file);
9825 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9826 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009827
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009828 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009829 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9830 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9831 rule->action = HTTP_RES_ACT_DEL_ACL;
9832 /*
9833 * '+ 8' for 'del-acl('
9834 * '- 9' for 'del-acl(' + trailing ')'
9835 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009836 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009837
9838 cur_arg = 1;
9839
9840 if (!*args[cur_arg] ||
9841 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9842 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9843 file, linenum, args[0]);
9844 goto out_err;
9845 }
9846
9847 LIST_INIT(&rule->arg.map.key);
9848 proxy->conf.args.ctx = ARGC_HRS;
9849 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9850 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9851 file, linenum);
9852 free(proxy->conf.lfs_file);
9853 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9854 proxy->conf.lfs_line = proxy->conf.args.line;
9855 cur_arg += 1;
9856 } else if (strncmp(args[0], "del-map", 7) == 0) {
9857 /* http-response del-map(<reference (map name)>) <key pattern> */
9858 rule->action = HTTP_RES_ACT_DEL_MAP;
9859 /*
9860 * '+ 8' for 'del-map('
9861 * '- 9' for 'del-map(' + trailing ')'
9862 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009863 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009864
9865 cur_arg = 1;
9866
9867 if (!*args[cur_arg] ||
9868 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9869 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9870 file, linenum, args[0]);
9871 goto out_err;
9872 }
9873
9874 LIST_INIT(&rule->arg.map.key);
9875 proxy->conf.args.ctx = ARGC_HRS;
9876 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9877 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9878 file, linenum);
9879 free(proxy->conf.lfs_file);
9880 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9881 proxy->conf.lfs_line = proxy->conf.args.line;
9882 cur_arg += 1;
9883 } else if (strncmp(args[0], "set-map", 7) == 0) {
9884 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9885 rule->action = HTTP_RES_ACT_SET_MAP;
9886 /*
9887 * '+ 8' for 'set-map('
9888 * '- 9' for 'set-map(' + trailing ')'
9889 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009890 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009891
9892 cur_arg = 1;
9893
9894 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9895 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9896 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9897 file, linenum, args[0]);
9898 goto out_err;
9899 }
9900
9901 LIST_INIT(&rule->arg.map.key);
9902 LIST_INIT(&rule->arg.map.value);
9903
9904 proxy->conf.args.ctx = ARGC_HRS;
9905
9906 /* key pattern */
9907 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9908 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9909 file, linenum);
9910
9911 /* value pattern */
9912 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9913 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9914 file, linenum);
9915
9916 free(proxy->conf.lfs_file);
9917 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9918 proxy->conf.lfs_line = proxy->conf.args.line;
9919
9920 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009921 } else if (strcmp(args[0], "redirect") == 0) {
9922 struct redirect_rule *redir;
9923 char *errmsg = NULL;
9924
9925 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9926 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9927 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9928 goto out_err;
9929 }
9930
9931 /* this redirect rule might already contain a parsed condition which
9932 * we'll pass to the http-request rule.
9933 */
9934 rule->action = HTTP_RES_ACT_REDIR;
9935 rule->arg.redir = redir;
9936 rule->cond = redir->cond;
9937 redir->cond = NULL;
9938 cur_arg = 2;
9939 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009940 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9941 char *errmsg = NULL;
9942 cur_arg = 1;
9943 /* try in the module list */
9944 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9945 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9946 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9947 free(errmsg);
9948 goto out_err;
9949 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009950 } else {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009951 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 +02009952 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9953 goto out_err;
9954 }
9955
9956 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9957 struct acl_cond *cond;
9958 char *errmsg = NULL;
9959
9960 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9961 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9962 file, linenum, args[0], errmsg);
9963 free(errmsg);
9964 goto out_err;
9965 }
9966 rule->cond = cond;
9967 }
9968 else if (*args[cur_arg]) {
9969 Alert("parsing [%s:%d]: 'http-response %s' expects"
9970 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9971 file, linenum, args[0], args[cur_arg]);
9972 goto out_err;
9973 }
9974
9975 return rule;
9976 out_err:
9977 free(rule);
9978 return NULL;
9979}
9980
Willy Tarreau4baae242012-12-27 12:00:31 +01009981/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009982 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009983 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9984 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009985 */
9986struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009987 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009988{
9989 struct redirect_rule *rule;
9990 int cur_arg;
9991 int type = REDIRECT_TYPE_NONE;
9992 int code = 302;
9993 const char *destination = NULL;
9994 const char *cookie = NULL;
9995 int cookie_set = 0;
9996 unsigned int flags = REDIRECT_FLAG_NONE;
9997 struct acl_cond *cond = NULL;
9998
9999 cur_arg = 0;
10000 while (*(args[cur_arg])) {
10001 if (strcmp(args[cur_arg], "location") == 0) {
10002 if (!*args[cur_arg + 1])
10003 goto missing_arg;
10004
10005 type = REDIRECT_TYPE_LOCATION;
10006 cur_arg++;
10007 destination = args[cur_arg];
10008 }
10009 else if (strcmp(args[cur_arg], "prefix") == 0) {
10010 if (!*args[cur_arg + 1])
10011 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +010010012 type = REDIRECT_TYPE_PREFIX;
10013 cur_arg++;
10014 destination = args[cur_arg];
10015 }
10016 else if (strcmp(args[cur_arg], "scheme") == 0) {
10017 if (!*args[cur_arg + 1])
10018 goto missing_arg;
10019
10020 type = REDIRECT_TYPE_SCHEME;
10021 cur_arg++;
10022 destination = args[cur_arg];
10023 }
10024 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10025 if (!*args[cur_arg + 1])
10026 goto missing_arg;
10027
10028 cur_arg++;
10029 cookie = args[cur_arg];
10030 cookie_set = 1;
10031 }
10032 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10033 if (!*args[cur_arg + 1])
10034 goto missing_arg;
10035
10036 cur_arg++;
10037 cookie = args[cur_arg];
10038 cookie_set = 0;
10039 }
10040 else if (strcmp(args[cur_arg], "code") == 0) {
10041 if (!*args[cur_arg + 1])
10042 goto missing_arg;
10043
10044 cur_arg++;
10045 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010046 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010047 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010048 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010049 args[cur_arg - 1], args[cur_arg]);
10050 return NULL;
10051 }
10052 }
10053 else if (!strcmp(args[cur_arg],"drop-query")) {
10054 flags |= REDIRECT_FLAG_DROP_QS;
10055 }
10056 else if (!strcmp(args[cur_arg],"append-slash")) {
10057 flags |= REDIRECT_FLAG_APPEND_SLASH;
10058 }
10059 else if (strcmp(args[cur_arg], "if") == 0 ||
10060 strcmp(args[cur_arg], "unless") == 0) {
10061 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10062 if (!cond) {
10063 memprintf(errmsg, "error in condition: %s", *errmsg);
10064 return NULL;
10065 }
10066 break;
10067 }
10068 else {
10069 memprintf(errmsg,
10070 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10071 args[cur_arg]);
10072 return NULL;
10073 }
10074 cur_arg++;
10075 }
10076
10077 if (type == REDIRECT_TYPE_NONE) {
10078 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10079 return NULL;
10080 }
10081
Willy Tarreaube4653b2015-05-28 15:26:58 +020010082 if (dir && type != REDIRECT_TYPE_LOCATION) {
10083 memprintf(errmsg, "response only supports redirect type 'location'");
10084 return NULL;
10085 }
10086
Willy Tarreau4baae242012-12-27 12:00:31 +010010087 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
10088 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010089 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010090
10091 if (!use_fmt) {
10092 /* old-style static redirect rule */
10093 rule->rdr_str = strdup(destination);
10094 rule->rdr_len = strlen(destination);
10095 }
10096 else {
10097 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010098
10099 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10100 * if prefix == "/", we don't want to add anything, otherwise it
10101 * makes it hard for the user to configure a self-redirection.
10102 */
Godbachd9722032014-12-18 15:44:58 +080010103 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010104 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +010010105 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +020010106 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
10107 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +010010108 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010109 free(curproxy->conf.lfs_file);
10110 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10111 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010112 }
10113 }
10114
Willy Tarreau4baae242012-12-27 12:00:31 +010010115 if (cookie) {
10116 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10117 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10118 */
10119 rule->cookie_len = strlen(cookie);
10120 if (cookie_set) {
10121 rule->cookie_str = malloc(rule->cookie_len + 10);
10122 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10123 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10124 rule->cookie_len += 9;
10125 } else {
10126 rule->cookie_str = malloc(rule->cookie_len + 21);
10127 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10128 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10129 rule->cookie_len += 20;
10130 }
10131 }
10132 rule->type = type;
10133 rule->code = code;
10134 rule->flags = flags;
10135 LIST_INIT(&rule->list);
10136 return rule;
10137
10138 missing_arg:
10139 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10140 return NULL;
10141}
10142
Willy Tarreau8797c062007-05-07 00:55:35 +020010143/************************************************************************/
10144/* The code below is dedicated to ACL parsing and matching */
10145/************************************************************************/
10146
10147
Willy Tarreau14174bc2012-04-16 14:34:04 +020010148/* This function ensures that the prerequisites for an L7 fetch are ready,
10149 * which means that a request or response is ready. If some data is missing,
10150 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010151 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10152 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010153 *
10154 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010155 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10156 * decide whether or not an HTTP message is present ;
10157 * 0 if the requested data cannot be fetched or if it is certain that
10158 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010159 * 1 if an HTTP message is ready
10160 */
10161static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010162smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010163 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010164{
Willy Tarreau192252e2015-04-04 01:47:55 +020010165 struct http_txn *txn;
10166 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010167
Willy Tarreaueee5b512015-04-03 23:46:31 +020010168 /* Note: this function may only be used from places where
10169 * http_init_txn() has already been done, and implies that <s>,
10170 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10171 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010172 */
10173
Willy Tarreau192252e2015-04-04 01:47:55 +020010174 if (!s)
10175 return 0;
10176 txn = s->txn;
10177
10178 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010179 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +020010180 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010181
10182 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +020010183 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010184
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010185 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010186 /* If the buffer does not leave enough free space at the end,
10187 * we must first realign it.
10188 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010189 if (s->req.buf->p > s->req.buf->data &&
10190 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10191 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010192
Willy Tarreau14174bc2012-04-16 14:34:04 +020010193 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010194 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010195 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010196
10197 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010198 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010199 http_msg_analyzer(msg, &txn->hdr_idx);
10200
10201 /* Still no valid request ? */
10202 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010203 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010204 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010205 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010206 }
10207 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010208 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010209 return 0;
10210 }
10211
10212 /* OK we just got a valid HTTP request. We have some minor
10213 * preparation to perform so that further checks can rely
10214 * on HTTP tests.
10215 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010216
10217 /* If the request was parsed but was too large, we must absolutely
10218 * return an error so that it is not processed. At the moment this
10219 * cannot happen, but if the parsers are to change in the future,
10220 * we want this check to be maintained.
10221 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010222 if (unlikely(s->req.buf->i + s->req.buf->p >
10223 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010224 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010225 smp->data.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010226 return 1;
10227 }
10228
Willy Tarreau9b28e032012-10-12 23:49:43 +020010229 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010230 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010231 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010232
Willy Tarreau506d0502013-07-06 13:29:24 +020010233 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10234 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010235 }
10236
Willy Tarreau506d0502013-07-06 13:29:24 +020010237 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010238 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010239 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010240
10241 /* otherwise everything's ready for the request */
10242 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010243 else {
10244 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010245 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10246 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010247 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010248 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010249 }
10250
10251 /* everything's OK */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010252 smp->data.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010253 return 1;
10254}
Willy Tarreau8797c062007-05-07 00:55:35 +020010255
Willy Tarreau6c616e02014-06-25 16:56:41 +020010256/* Note: these functinos *do* modify the sample. Even in case of success, at
10257 * least the type and uint value are modified.
10258 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010259#define CHECK_HTTP_MESSAGE_FIRST() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010260 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 +020010261
Willy Tarreau24e32d82012-04-23 23:55:44 +020010262#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010263 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 +020010264
Willy Tarreau8797c062007-05-07 00:55:35 +020010265
10266/* 1. Check on METHOD
10267 * We use the pre-parsed method if it is known, and store its number as an
10268 * integer. If it is unknown, we use the pointer and the length.
10269 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010270static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010271{
10272 int len, meth;
10273
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010274 len = strlen(text);
10275 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010276
10277 pattern->val.i = meth;
10278 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010279 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010280 pattern->len = len;
10281 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010282 else {
10283 pattern->ptr.str = NULL;
10284 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010285 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010286 return 1;
10287}
10288
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010289/* This function fetches the method of current HTTP request and stores
10290 * it in the global pattern struct as a chunk. There are two possibilities :
10291 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10292 * in <len> and <ptr> is NULL ;
10293 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10294 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010295 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010296 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010297static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010298smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010299{
10300 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010301 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010302
Willy Tarreau24e32d82012-04-23 23:55:44 +020010303 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010304
Willy Tarreau8797c062007-05-07 00:55:35 +020010305 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010306 smp->type = SMP_T_METH;
10307 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010308 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010309 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10310 /* ensure the indexes are not affected */
10311 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010312 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010313 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10314 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010315 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010316 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010317 return 1;
10318}
10319
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010320/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010321static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010322{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010323 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010324 struct pattern_list *lst;
10325 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010326
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010327 list_for_each_entry(lst, &expr->patterns, list) {
10328 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010329
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010330 /* well-known method */
10331 if (pattern->val.i != HTTP_METH_OTHER) {
10332 if (smp->data.meth.meth == pattern->val.i)
10333 return pattern;
10334 else
10335 continue;
10336 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010337
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010338 /* Other method, we must compare the strings */
10339 if (pattern->len != smp->data.meth.str.len)
10340 continue;
10341
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010342 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010343 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10344 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010345 return pattern;
10346 }
10347 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010348}
10349
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010350static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010351smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010352{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010353 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010354 char *ptr;
10355 int len;
10356
Willy Tarreauc0239e02012-04-16 14:42:55 +020010357 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010358
Willy Tarreau8797c062007-05-07 00:55:35 +020010359 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010360 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010361
10362 while ((len-- > 0) && (*ptr++ != '/'));
10363 if (len <= 0)
10364 return 0;
10365
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010366 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010367 smp->data.str.str = ptr;
10368 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010369
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010370 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010371 return 1;
10372}
10373
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010374static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010375smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010376{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010377 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010378 char *ptr;
10379 int len;
10380
Willy Tarreauc0239e02012-04-16 14:42:55 +020010381 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010382
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010383 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010384 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10385 return 0;
10386
Willy Tarreau8797c062007-05-07 00:55:35 +020010387 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010388 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010389
10390 while ((len-- > 0) && (*ptr++ != '/'));
10391 if (len <= 0)
10392 return 0;
10393
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010394 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010395 smp->data.str.str = ptr;
10396 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010397
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010398 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010399 return 1;
10400}
10401
10402/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010403static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010404smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010405{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010406 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010407 char *ptr;
10408 int len;
10409
Willy Tarreauc0239e02012-04-16 14:42:55 +020010410 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010411
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010412 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010413 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10414 return 0;
10415
Willy Tarreau8797c062007-05-07 00:55:35 +020010416 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010417 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010418
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010419 smp->type = SMP_T_SINT;
10420 smp->data.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010421 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010422 return 1;
10423}
10424
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010425/* returns the longest available part of the body. This requires that the body
10426 * has been waited for using http-buffer-request.
10427 */
10428static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010429smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010430{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010431 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010432 struct http_msg *msg;
10433 unsigned long len;
10434 unsigned long block1;
10435 char *body;
10436 struct chunk *temp;
10437
10438 CHECK_HTTP_MESSAGE_FIRST();
10439
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010440 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010441 msg = &txn->req;
10442 else
10443 msg = &txn->rsp;
10444
10445 len = http_body_bytes(msg);
10446 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10447
10448 block1 = len;
10449 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10450 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10451
10452 if (block1 == len) {
10453 /* buffer is not wrapped (or empty) */
10454 smp->type = SMP_T_BIN;
10455 smp->data.str.str = body;
10456 smp->data.str.len = len;
10457 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10458 }
10459 else {
10460 /* buffer is wrapped, we need to defragment it */
10461 temp = get_trash_chunk();
10462 memcpy(temp->str, body, block1);
10463 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
10464 smp->type = SMP_T_BIN;
10465 smp->data.str.str = temp->str;
10466 smp->data.str.len = len;
10467 smp->flags = SMP_F_VOL_TEST;
10468 }
10469 return 1;
10470}
10471
10472
10473/* returns the available length of the body. This requires that the body
10474 * has been waited for using http-buffer-request.
10475 */
10476static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010477smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010478{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010479 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010480 struct http_msg *msg;
10481
10482 CHECK_HTTP_MESSAGE_FIRST();
10483
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010484 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010485 msg = &txn->req;
10486 else
10487 msg = &txn->rsp;
10488
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010489 smp->type = SMP_T_SINT;
10490 smp->data.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010491
10492 smp->flags = SMP_F_VOL_TEST;
10493 return 1;
10494}
10495
10496
10497/* returns the advertised length of the body, or the advertised size of the
10498 * chunks available in the buffer. This requires that the body has been waited
10499 * for using http-buffer-request.
10500 */
10501static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010502smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010503{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010504 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010505 struct http_msg *msg;
10506
10507 CHECK_HTTP_MESSAGE_FIRST();
10508
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010509 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010510 msg = &txn->req;
10511 else
10512 msg = &txn->rsp;
10513
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010514 smp->type = SMP_T_SINT;
10515 smp->data.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010516
10517 smp->flags = SMP_F_VOL_TEST;
10518 return 1;
10519}
10520
10521
Willy Tarreau8797c062007-05-07 00:55:35 +020010522/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010523static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010524smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010525{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010526 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010527
Willy Tarreauc0239e02012-04-16 14:42:55 +020010528 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010529
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010530 txn = smp->strm->txn;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010531 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010532 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010533 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010534 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010535 return 1;
10536}
10537
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010538static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010539smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010540{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010541 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010542 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010543
Willy Tarreauc0239e02012-04-16 14:42:55 +020010544 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010545
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010546 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010547 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 +020010548 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010549 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010550
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010551 smp->type = SMP_T_IPV4;
10552 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010553 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010554 return 1;
10555}
10556
10557static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010558smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010559{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010560 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010561 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010562
Willy Tarreauc0239e02012-04-16 14:42:55 +020010563 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010564
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010565 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010566 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 +020010567 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10568 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010569
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010570 smp->type = SMP_T_SINT;
10571 smp->data.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010572 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010573 return 1;
10574}
10575
Willy Tarreau185b5c42012-04-26 15:11:51 +020010576/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10577 * Accepts an optional argument of type string containing the header field name,
10578 * and an optional argument of type signed or unsigned integer to request an
10579 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010580 * headers are considered from the first one. It does not stop on commas and
10581 * returns full lines instead (useful for User-Agent or Date for example).
10582 */
10583static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010584smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010585{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010586 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010587 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010588 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010589 int occ = 0;
10590 const char *name_str = NULL;
10591 int name_len = 0;
10592
10593 if (!ctx) {
10594 /* first call */
10595 ctx = &static_hdr_ctx;
10596 ctx->idx = 0;
10597 smp->ctx.a[0] = ctx;
10598 }
10599
10600 if (args) {
10601 if (args[0].type != ARGT_STR)
10602 return 0;
10603 name_str = args[0].data.str.str;
10604 name_len = args[0].data.str.len;
10605
10606 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10607 occ = args[1].data.uint;
10608 }
10609
10610 CHECK_HTTP_MESSAGE_FIRST();
10611
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010612 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010613 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 +020010614
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010615 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10616 /* search for header from the beginning */
10617 ctx->idx = 0;
10618
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010619 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010620 /* no explicit occurrence and single fetch => last header by default */
10621 occ = -1;
10622
10623 if (!occ)
10624 /* prepare to report multiple occurrences for ACL fetches */
10625 smp->flags |= SMP_F_NOT_LAST;
10626
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010627 smp->type = SMP_T_STR;
10628 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010629 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10630 return 1;
10631
10632 smp->flags &= ~SMP_F_NOT_LAST;
10633 return 0;
10634}
10635
10636/* 6. Check on HTTP header count. The number of occurrences is returned.
10637 * Accepts exactly 1 argument of type string. It does not stop on commas and
10638 * returns full lines instead (useful for User-Agent or Date for example).
10639 */
10640static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010641smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010642{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010643 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010644 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010645 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010646 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010647 const char *name = NULL;
10648 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010649
Willy Tarreau601a4d12015-04-01 19:16:09 +020010650 if (args && args->type == ARGT_STR) {
10651 name = args->data.str.str;
10652 len = args->data.str.len;
10653 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010654
10655 CHECK_HTTP_MESSAGE_FIRST();
10656
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010657 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010658 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 +020010659
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010660 ctx.idx = 0;
10661 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010662 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010663 cnt++;
10664
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010665 smp->type = SMP_T_SINT;
10666 smp->data.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010667 smp->flags = SMP_F_VOL_HDR;
10668 return 1;
10669}
10670
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010671static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010672smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010673{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010674 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010675 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010676 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010677 struct chunk *temp;
10678 char del = ',';
10679
10680 if (args && args->type == ARGT_STR)
10681 del = *args[0].data.str.str;
10682
10683 CHECK_HTTP_MESSAGE_FIRST();
10684
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010685 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010686 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 +020010687
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010688 temp = get_trash_chunk();
10689
10690 ctx.idx = 0;
10691 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10692 if (temp->len)
10693 temp->str[temp->len++] = del;
10694 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10695 temp->len += ctx.del;
10696 }
10697
10698 smp->type = SMP_T_STR;
10699 smp->data.str.str = temp->str;
10700 smp->data.str.len = temp->len;
10701 smp->flags = SMP_F_VOL_HDR;
10702 return 1;
10703}
10704
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010705/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10706 * Accepts an optional argument of type string containing the header field name,
10707 * and an optional argument of type signed or unsigned integer to request an
10708 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010709 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010710 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010711static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010712smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010713{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010714 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010715 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010716 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010717 int occ = 0;
10718 const char *name_str = NULL;
10719 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010720
Willy Tarreaua890d072013-04-02 12:01:06 +020010721 if (!ctx) {
10722 /* first call */
10723 ctx = &static_hdr_ctx;
10724 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010725 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010726 }
10727
Willy Tarreau185b5c42012-04-26 15:11:51 +020010728 if (args) {
10729 if (args[0].type != ARGT_STR)
10730 return 0;
10731 name_str = args[0].data.str.str;
10732 name_len = args[0].data.str.len;
10733
10734 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10735 occ = args[1].data.uint;
10736 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010737
Willy Tarreaue333ec92012-04-16 16:26:40 +020010738 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010739
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010740 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010741 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 +020010742
Willy Tarreau185b5c42012-04-26 15:11:51 +020010743 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010744 /* search for header from the beginning */
10745 ctx->idx = 0;
10746
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010747 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010748 /* no explicit occurrence and single fetch => last header by default */
10749 occ = -1;
10750
10751 if (!occ)
10752 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010753 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010754
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010755 smp->type = SMP_T_STR;
10756 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010757 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010758 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010759
Willy Tarreau37406352012-04-23 16:16:37 +020010760 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010761 return 0;
10762}
10763
Willy Tarreauc11416f2007-06-17 16:58:38 +020010764/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010765 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010766 */
10767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010768smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010769{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010770 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010771 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010772 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010773 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010774 const char *name = NULL;
10775 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010776
Willy Tarreau601a4d12015-04-01 19:16:09 +020010777 if (args && args->type == ARGT_STR) {
10778 name = args->data.str.str;
10779 len = args->data.str.len;
10780 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010781
Willy Tarreaue333ec92012-04-16 16:26:40 +020010782 CHECK_HTTP_MESSAGE_FIRST();
10783
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010784 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010785 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 +020010786
Willy Tarreau33a7e692007-06-10 19:45:56 +020010787 ctx.idx = 0;
10788 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010789 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010790 cnt++;
10791
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010792 smp->type = SMP_T_SINT;
10793 smp->data.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010794 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010795 return 1;
10796}
10797
Willy Tarreau185b5c42012-04-26 15:11:51 +020010798/* Fetch an HTTP header's integer value. The integer value is returned. It
10799 * takes a mandatory argument of type string and an optional one of type int
10800 * to designate a specific occurrence. It returns an unsigned integer, which
10801 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010802 */
10803static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010804smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010805{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010806 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010807
Willy Tarreauf853c462012-04-23 18:53:56 +020010808 if (ret > 0) {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010809 smp->type = SMP_T_SINT;
10810 smp->data.sint = strl2ic(smp->data.str.str, smp->data.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010811 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010812
Willy Tarreaud53e2422012-04-16 17:21:11 +020010813 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010814}
10815
Cyril Bonté69fa9922012-10-25 00:01:06 +020010816/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10817 * and an optional one of type int to designate a specific occurrence.
10818 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010819 */
10820static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010821smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010822{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010823 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010824
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010825 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010826 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10827 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010828 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010829 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010830 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010831 if (smp->data.str.len < temp->size - 1) {
10832 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10833 temp->str[smp->data.str.len] = '\0';
10834 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10835 smp->type = SMP_T_IPV6;
10836 break;
10837 }
10838 }
10839 }
10840
Willy Tarreaud53e2422012-04-16 17:21:11 +020010841 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010842 if (!(smp->flags & SMP_F_NOT_LAST))
10843 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010844 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010845 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010846}
10847
Willy Tarreau737b0c12007-06-10 21:28:46 +020010848/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10849 * the first '/' after the possible hostname, and ends before the possible '?'.
10850 */
10851static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010852smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010853{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010854 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010855 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010856
Willy Tarreauc0239e02012-04-16 14:42:55 +020010857 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010858
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010859 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010860 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010861 ptr = http_get_path(txn);
10862 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010863 return 0;
10864
10865 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010866 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010867 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010868
10869 while (ptr < end && *ptr != '?')
10870 ptr++;
10871
Willy Tarreauf853c462012-04-23 18:53:56 +020010872 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010873 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010874 return 1;
10875}
10876
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010877/* This produces a concatenation of the first occurrence of the Host header
10878 * followed by the path component if it begins with a slash ('/'). This means
10879 * that '*' will not be added, resulting in exactly the first Host entry.
10880 * If no Host header is found, then the path is returned as-is. The returned
10881 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010882 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010883 */
10884static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010885smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010886{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010887 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010888 char *ptr, *end, *beg;
10889 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010890 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010891
10892 CHECK_HTTP_MESSAGE_FIRST();
10893
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010894 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010895 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010896 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010897 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010898
10899 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010900 temp = get_trash_chunk();
10901 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010902 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010903 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010904 smp->data.str.len = ctx.vlen;
10905
10906 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010907 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010908 beg = http_get_path(txn);
10909 if (!beg)
10910 beg = end;
10911
10912 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10913
10914 if (beg < ptr && *beg == '/') {
10915 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10916 smp->data.str.len += ptr - beg;
10917 }
10918
10919 smp->flags = SMP_F_VOL_1ST;
10920 return 1;
10921}
10922
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010923/* This produces a 32-bit hash of the concatenation of the first occurrence of
10924 * the Host header followed by the path component if it begins with a slash ('/').
10925 * This means that '*' will not be added, resulting in exactly the first Host
10926 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010927 * is hashed using the path hash followed by a full avalanche hash and provides a
10928 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010929 * high-traffic sites without having to store whole paths.
10930 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010931int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010932smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010933{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010934 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010935 struct hdr_ctx ctx;
10936 unsigned int hash = 0;
10937 char *ptr, *beg, *end;
10938 int len;
10939
10940 CHECK_HTTP_MESSAGE_FIRST();
10941
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010942 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010943 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010944 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010945 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10946 ptr = ctx.line + ctx.val;
10947 len = ctx.vlen;
10948 while (len--)
10949 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10950 }
10951
10952 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010953 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010954 beg = http_get_path(txn);
10955 if (!beg)
10956 beg = end;
10957
10958 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10959
10960 if (beg < ptr && *beg == '/') {
10961 while (beg < ptr)
10962 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10963 }
10964 hash = full_hash(hash);
10965
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010966 smp->type = SMP_T_SINT;
10967 smp->data.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010968 smp->flags = SMP_F_VOL_1ST;
10969 return 1;
10970}
10971
Willy Tarreau4a550602012-12-09 14:53:32 +010010972/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010973 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10974 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10975 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010976 * that in environments where IPv6 is insignificant, truncating the output to
10977 * 8 bytes would still work.
10978 */
10979static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010980smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010981{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010982 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010983 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010984
10985 if (!cli_conn)
10986 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010987
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010988 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010989 return 0;
10990
Willy Tarreau47ca5452012-12-23 20:22:19 +010010991 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010992 *(unsigned int *)temp->str = htonl(smp->data.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010993 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010994
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010995 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010996 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010997 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010998 temp->len += 4;
10999 break;
11000 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011001 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011002 temp->len += 16;
11003 break;
11004 default:
11005 return 0;
11006 }
11007
11008 smp->data.str = *temp;
11009 smp->type = SMP_T_BIN;
11010 return 1;
11011}
11012
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011013/* Extracts the query string, which comes after the question mark '?'. If no
11014 * question mark is found, nothing is returned. Otherwise it returns a sample
11015 * of type string carrying the whole query string.
11016 */
11017static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011018smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011019{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011020 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011021 char *ptr, *end;
11022
11023 CHECK_HTTP_MESSAGE_FIRST();
11024
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011025 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011026 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11027 end = ptr + txn->req.sl.rq.u_l;
11028
11029 /* look up the '?' */
11030 do {
11031 if (ptr == end)
11032 return 0;
11033 } while (*ptr++ != '?');
11034
11035 smp->type = SMP_T_STR;
11036 smp->data.str.str = ptr;
11037 smp->data.str.len = end - ptr;
11038 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11039 return 1;
11040}
11041
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011042static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011043smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011044{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011045 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11046 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11047 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011048
Willy Tarreau24e32d82012-04-23 23:55:44 +020011049 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011050
Willy Tarreauf853c462012-04-23 18:53:56 +020011051 smp->type = SMP_T_BOOL;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011052 smp->data.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011053 return 1;
11054}
11055
Willy Tarreau7f18e522010-10-22 20:04:13 +020011056/* return a valid test if the current request is the first one on the connection */
11057static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011058smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011059{
Willy Tarreauf853c462012-04-23 18:53:56 +020011060 smp->type = SMP_T_BOOL;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011061 smp->data.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011062 return 1;
11063}
11064
Willy Tarreau34db1082012-04-19 17:16:54 +020011065/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011066static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011067smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011068{
11069
Willy Tarreau24e32d82012-04-23 23:55:44 +020011070 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011071 return 0;
11072
Willy Tarreauc0239e02012-04-16 14:42:55 +020011073 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011074
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011075 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011076 return 0;
11077
Willy Tarreauf853c462012-04-23 18:53:56 +020011078 smp->type = SMP_T_BOOL;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011079 smp->data.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011080 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011081 return 1;
11082}
Willy Tarreau8797c062007-05-07 00:55:35 +020011083
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011084/* Accepts exactly 1 argument of type userlist */
11085static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011086smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011087{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011088 if (!args || args->type != ARGT_USR)
11089 return 0;
11090
11091 CHECK_HTTP_MESSAGE_FIRST();
11092
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011093 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011094 return 0;
11095
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011096 /* if the user does not belong to the userlist or has a wrong password,
11097 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011098 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011099 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011100 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11101 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011102 return 0;
11103
11104 /* pat_match_auth() will need the user list */
11105 smp->ctx.a[0] = args->data.usr;
11106
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011107 smp->type = SMP_T_STR;
11108 smp->flags = SMP_F_CONST;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011109 smp->data.str.str = smp->strm->txn->auth.user;
11110 smp->data.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011111
11112 return 1;
11113}
11114
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011115/* Try to find the next occurrence of a cookie name in a cookie header value.
11116 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11117 * the cookie value is returned into *value and *value_l, and the function
11118 * returns a pointer to the next pointer to search from if the value was found.
11119 * Otherwise if the cookie was not found, NULL is returned and neither value
11120 * nor value_l are touched. The input <hdr> string should first point to the
11121 * header's value, and the <hdr_end> pointer must point to the first character
11122 * not part of the value. <list> must be non-zero if value may represent a list
11123 * of values (cookie headers). This makes it faster to abort parsing when no
11124 * list is expected.
11125 */
11126static char *
11127extract_cookie_value(char *hdr, const char *hdr_end,
11128 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011129 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011130{
11131 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11132 char *next;
11133
11134 /* we search at least a cookie name followed by an equal, and more
11135 * generally something like this :
11136 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11137 */
11138 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11139 /* Iterate through all cookies on this line */
11140
11141 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11142 att_beg++;
11143
11144 /* find att_end : this is the first character after the last non
11145 * space before the equal. It may be equal to hdr_end.
11146 */
11147 equal = att_end = att_beg;
11148
11149 while (equal < hdr_end) {
11150 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11151 break;
11152 if (http_is_spht[(unsigned char)*equal++])
11153 continue;
11154 att_end = equal;
11155 }
11156
11157 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11158 * is between <att_beg> and <equal>, both may be identical.
11159 */
11160
11161 /* look for end of cookie if there is an equal sign */
11162 if (equal < hdr_end && *equal == '=') {
11163 /* look for the beginning of the value */
11164 val_beg = equal + 1;
11165 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11166 val_beg++;
11167
11168 /* find the end of the value, respecting quotes */
11169 next = find_cookie_value_end(val_beg, hdr_end);
11170
11171 /* make val_end point to the first white space or delimitor after the value */
11172 val_end = next;
11173 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11174 val_end--;
11175 } else {
11176 val_beg = val_end = next = equal;
11177 }
11178
11179 /* We have nothing to do with attributes beginning with '$'. However,
11180 * they will automatically be removed if a header before them is removed,
11181 * since they're supposed to be linked together.
11182 */
11183 if (*att_beg == '$')
11184 continue;
11185
11186 /* Ignore cookies with no equal sign */
11187 if (equal == next)
11188 continue;
11189
11190 /* Now we have the cookie name between att_beg and att_end, and
11191 * its value between val_beg and val_end.
11192 */
11193
11194 if (att_end - att_beg == cookie_name_l &&
11195 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11196 /* let's return this value and indicate where to go on from */
11197 *value = val_beg;
11198 *value_l = val_end - val_beg;
11199 return next + 1;
11200 }
11201
11202 /* Set-Cookie headers only have the name in the first attr=value part */
11203 if (!list)
11204 break;
11205 }
11206
11207 return NULL;
11208}
11209
William Lallemanda43ba4e2014-01-28 18:14:25 +010011210/* Fetch a captured HTTP request header. The index is the position of
11211 * the "capture" option in the configuration file
11212 */
11213static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011214smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011215{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011216 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011217 int idx;
11218
11219 if (!args || args->type != ARGT_UINT)
11220 return 0;
11221
11222 idx = args->data.uint;
11223
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011224 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 +010011225 return 0;
11226
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011227 smp->type = SMP_T_STR;
11228 smp->flags |= SMP_F_CONST;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011229 smp->data.str.str = smp->strm->req_cap[idx];
11230 smp->data.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011231
11232 return 1;
11233}
11234
11235/* Fetch a captured HTTP response header. The index is the position of
11236 * the "capture" option in the configuration file
11237 */
11238static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011239smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011240{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011241 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011242 int idx;
11243
11244 if (!args || args->type != ARGT_UINT)
11245 return 0;
11246
11247 idx = args->data.uint;
11248
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011249 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 +010011250 return 0;
11251
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011252 smp->type = SMP_T_STR;
11253 smp->flags |= SMP_F_CONST;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011254 smp->data.str.str = smp->strm->res_cap[idx];
11255 smp->data.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011256
11257 return 1;
11258}
11259
William Lallemand65ad6e12014-01-31 15:08:02 +010011260/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11261static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011262smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011263{
11264 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011265 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011266 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011267
Willy Tarreau15e91e12015-04-04 00:52:09 +020011268 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011269 return 0;
11270
William Lallemand96a77852014-02-05 00:30:02 +010011271 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011272
William Lallemand96a77852014-02-05 00:30:02 +010011273 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11274 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011275
William Lallemand96a77852014-02-05 00:30:02 +010011276 temp = get_trash_chunk();
11277 temp->str = txn->uri;
11278 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011279 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011280 smp->type = SMP_T_STR;
11281 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011282
11283 return 1;
11284
11285}
11286
11287/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11288static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011289smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011290{
11291 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011292 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011293 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011294
Willy Tarreau15e91e12015-04-04 00:52:09 +020011295 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011296 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011297
William Lallemand65ad6e12014-01-31 15:08:02 +010011298 ptr = txn->uri;
11299
11300 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11301 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011302
William Lallemand65ad6e12014-01-31 15:08:02 +010011303 if (!*ptr)
11304 return 0;
11305
11306 ptr++; /* skip the space */
11307
11308 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011309 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011310 if (!ptr)
11311 return 0;
11312 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11313 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011314
11315 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010011316 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011317 smp->type = SMP_T_STR;
11318 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011319
11320 return 1;
11321}
11322
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011323/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11324 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11325 */
11326static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011327smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011328{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011329 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011330
Willy Tarreau15e91e12015-04-04 00:52:09 +020011331 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011332 return 0;
11333
11334 if (txn->req.flags & HTTP_MSGF_VER_11)
11335 smp->data.str.str = "HTTP/1.1";
11336 else
11337 smp->data.str.str = "HTTP/1.0";
11338
11339 smp->data.str.len = 8;
11340 smp->type = SMP_T_STR;
11341 smp->flags = SMP_F_CONST;
11342 return 1;
11343
11344}
11345
11346/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11347 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11348 */
11349static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011350smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011351{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011352 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011353
Willy Tarreau15e91e12015-04-04 00:52:09 +020011354 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011355 return 0;
11356
11357 if (txn->rsp.flags & HTTP_MSGF_VER_11)
11358 smp->data.str.str = "HTTP/1.1";
11359 else
11360 smp->data.str.str = "HTTP/1.0";
11361
11362 smp->data.str.len = 8;
11363 smp->type = SMP_T_STR;
11364 smp->flags = SMP_F_CONST;
11365 return 1;
11366
11367}
11368
William Lallemand65ad6e12014-01-31 15:08:02 +010011369
Willy Tarreaue333ec92012-04-16 16:26:40 +020011370/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011371 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011372 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011373 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011374 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011375 * Accepts exactly 1 argument of type string. If the input options indicate
11376 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011377 * The returned sample is of type CSTR. Can be used to parse cookies in other
11378 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011379 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011380int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011381{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011382 struct http_txn *txn;
11383 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011384 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011385 const struct http_msg *msg;
11386 const char *hdr_name;
11387 int hdr_name_len;
11388 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011389 int occ = 0;
11390 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011391
Willy Tarreau24e32d82012-04-23 23:55:44 +020011392 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011393 return 0;
11394
Willy Tarreaua890d072013-04-02 12:01:06 +020011395 if (!ctx) {
11396 /* first call */
11397 ctx = &static_hdr_ctx;
11398 ctx->idx = 0;
11399 smp->ctx.a[2] = ctx;
11400 }
11401
Willy Tarreaue333ec92012-04-16 16:26:40 +020011402 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011403
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011404 txn = smp->strm->txn;
11405 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011406
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011407 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011408 msg = &txn->req;
11409 hdr_name = "Cookie";
11410 hdr_name_len = 6;
11411 } else {
11412 msg = &txn->rsp;
11413 hdr_name = "Set-Cookie";
11414 hdr_name_len = 10;
11415 }
11416
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011417 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011418 /* no explicit occurrence and single fetch => last cookie by default */
11419 occ = -1;
11420
11421 /* OK so basically here, either we want only one value and it's the
11422 * last one, or we want to iterate over all of them and we fetch the
11423 * next one.
11424 */
11425
Willy Tarreau9b28e032012-10-12 23:49:43 +020011426 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011427 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011428 /* search for the header from the beginning, we must first initialize
11429 * the search parameters.
11430 */
Willy Tarreau37406352012-04-23 16:16:37 +020011431 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011432 ctx->idx = 0;
11433 }
11434
Willy Tarreau28376d62012-04-26 21:26:10 +020011435 smp->flags |= SMP_F_VOL_HDR;
11436
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011437 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011438 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11439 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011440 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11441 goto out;
11442
Willy Tarreau24e32d82012-04-23 23:55:44 +020011443 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011444 continue;
11445
Willy Tarreau37406352012-04-23 16:16:37 +020011446 smp->ctx.a[0] = ctx->line + ctx->val;
11447 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011448 }
11449
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011450 smp->type = SMP_T_STR;
11451 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011452 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011453 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011454 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011455 &smp->data.str.str,
11456 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011457 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011458 found = 1;
11459 if (occ >= 0) {
11460 /* one value was returned into smp->data.str.{str,len} */
11461 smp->flags |= SMP_F_NOT_LAST;
11462 return 1;
11463 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011464 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011465 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011466 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011467 /* all cookie headers and values were scanned. If we're looking for the
11468 * last occurrence, we may return it now.
11469 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011470 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011471 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011472 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011473}
11474
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011475/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011476 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011477 * multiple cookies may be parsed on the same line. The returned sample is of
11478 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011479 */
11480static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011481smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011482{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011483 struct http_txn *txn;
11484 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011485 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011486 const struct http_msg *msg;
11487 const char *hdr_name;
11488 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011489 int cnt;
11490 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011491 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011492
Willy Tarreau24e32d82012-04-23 23:55:44 +020011493 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011494 return 0;
11495
Willy Tarreaue333ec92012-04-16 16:26:40 +020011496 CHECK_HTTP_MESSAGE_FIRST();
11497
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011498 txn = smp->strm->txn;
11499 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011500
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011501 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011502 msg = &txn->req;
11503 hdr_name = "Cookie";
11504 hdr_name_len = 6;
11505 } else {
11506 msg = &txn->rsp;
11507 hdr_name = "Set-Cookie";
11508 hdr_name_len = 10;
11509 }
11510
Willy Tarreau9b28e032012-10-12 23:49:43 +020011511 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011512 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011513 ctx.idx = 0;
11514 cnt = 0;
11515
11516 while (1) {
11517 /* Note: val_beg == NULL every time we need to fetch a new header */
11518 if (!val_beg) {
11519 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11520 break;
11521
Willy Tarreau24e32d82012-04-23 23:55:44 +020011522 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011523 continue;
11524
11525 val_beg = ctx.line + ctx.val;
11526 val_end = val_beg + ctx.vlen;
11527 }
11528
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011529 smp->type = SMP_T_STR;
11530 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011531 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011532 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011533 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011534 &smp->data.str.str,
11535 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011536 cnt++;
11537 }
11538 }
11539
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011540 smp->type = SMP_T_SINT;
11541 smp->data.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011542 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011543 return 1;
11544}
11545
Willy Tarreau51539362012-05-08 12:46:28 +020011546/* Fetch an cookie's integer value. The integer value is returned. It
11547 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11548 */
11549static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011550smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011551{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011552 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011553
11554 if (ret > 0) {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011555 smp->type = SMP_T_SINT;
11556 smp->data.sint = strl2ic(smp->data.str.str, smp->data.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011557 }
11558
11559 return ret;
11560}
11561
Willy Tarreau8797c062007-05-07 00:55:35 +020011562/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011563/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011564/************************************************************************/
11565
David Cournapeau16023ee2010-12-23 20:55:41 +090011566/*
11567 * Given a path string and its length, find the position of beginning of the
11568 * query string. Returns NULL if no query string is found in the path.
11569 *
11570 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11571 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011572 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011573 */
bedis4c75cca2012-10-05 08:38:24 +020011574static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011575{
11576 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011577
bedis4c75cca2012-10-05 08:38:24 +020011578 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011579 return p ? p + 1 : NULL;
11580}
11581
bedis4c75cca2012-10-05 08:38:24 +020011582static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011583{
bedis4c75cca2012-10-05 08:38:24 +020011584 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011585}
11586
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011587/* after increasing a pointer value, it can exceed the first buffer
11588 * size. This function transform the value of <ptr> according with
11589 * the expected position. <chunks> is an array of the one or two
11590 * avalaible chunks. The first value is the start of the first chunk,
11591 * the second value if the end+1 of the first chunks. The third value
11592 * is NULL or the start of the second chunk and the fourth value is
11593 * the end+1 of the second chunk. The function returns 1 if does a
11594 * wrap, else returns 0.
11595 */
11596static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11597{
11598 if (*ptr < chunks[1])
11599 return 0;
11600 if (!chunks[2])
11601 return 0;
11602 *ptr = chunks[2] + ( *ptr - chunks[1] );
11603 return 1;
11604}
11605
David Cournapeau16023ee2010-12-23 20:55:41 +090011606/*
11607 * Given a url parameter, find the starting position of the first occurence,
11608 * or NULL if the parameter is not found.
11609 *
11610 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11611 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011612 *
11613 * Warning:this function returns a pointer that can be point to the first chunk
11614 * or the second chunk. The caller must be check the position before using the
11615 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011616 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011617static const char *
11618find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011619 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011620 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011621{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011622 const char *pos, *last, *equal;
11623 const char **bufs = chunks;
11624 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011625
David Cournapeau16023ee2010-12-23 20:55:41 +090011626
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011627 pos = bufs[0];
11628 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011629 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011630 /* Check the equal. */
11631 equal = pos + url_param_name_l;
11632 if (fix_pointer_if_wrap(chunks, &equal)) {
11633 if (equal >= chunks[3])
11634 return NULL;
11635 } else {
11636 if (equal >= chunks[1])
11637 return NULL;
11638 }
11639 if (*equal == '=') {
11640 if (pos + url_param_name_l > last) {
11641 /* process wrap case, we detect a wrap. In this case, the
11642 * comparison is performed in two parts.
11643 */
11644
11645 /* This is the end, we dont have any other chunk. */
11646 if (bufs != chunks || !bufs[2])
11647 return NULL;
11648
11649 /* Compute the length of each part of the comparison. */
11650 l1 = last - pos;
11651 l2 = url_param_name_l - l1;
11652
11653 /* The second buffer is too short to contain the compared string. */
11654 if (bufs[2] + l2 > bufs[3])
11655 return NULL;
11656
11657 if (memcmp(pos, url_param_name, l1) == 0 &&
11658 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11659 return pos;
11660
11661 /* Perform wrapping and jump the string who fail the comparison. */
11662 bufs += 2;
11663 pos = bufs[0] + l2;
11664 last = bufs[1];
11665
11666 } else {
11667 /* process a simple comparison. */
11668 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11669 return pos; }
11670 pos += url_param_name_l + 1;
11671 if (fix_pointer_if_wrap(chunks, &pos))
11672 last = bufs[2];
11673 }
11674 }
11675
11676 while (1) {
11677 /* Look for the next delimiter. */
11678 while (pos <= last && !is_param_delimiter(*pos, delim))
11679 pos++;
11680 if (pos < last)
11681 break;
11682 /* process buffer wrapping. */
11683 if (bufs != chunks || !bufs[2])
11684 return NULL;
11685 bufs += 2;
11686 pos = bufs[0];
11687 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011688 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011689 pos++;
11690 }
11691 return NULL;
11692}
11693
11694/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011695 * Given a url parameter name and a query string, returns its value and size
11696 * into *value and *value_l respectively, and returns non-zero. An empty
11697 * url_param_name matches the first available parameter. If the parameter is
11698 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011699 */
11700static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011701find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011702 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011703 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011704{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011705 const char *arg_start, *qs_end;
11706 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011707
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011708 arg_start = chunks[0];
11709 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011710 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011711 /* Looks for an argument name. */
11712 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011713 url_param_name, url_param_name_l,
11714 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011715 /* Check for wrapping. */
11716 if (arg_start > qs_end)
11717 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011718 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011719 if (!arg_start)
11720 return 0;
11721
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011722 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011723 while (1) {
11724 /* looks for the first argument. */
11725 value_start = memchr(arg_start, '=', qs_end - arg_start);
11726 if (!value_start) {
11727
11728 /* Check for wrapping. */
11729 if (arg_start >= chunks[0] &&
11730 arg_start <= chunks[1] &&
11731 chunks[2]) {
11732 arg_start = chunks[2];
11733 qs_end = chunks[3];
11734 continue;
11735 }
11736 return 0;
11737 }
11738 break;
11739 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011740 value_start++;
11741 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011742 else {
11743 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011744 value_start = arg_start + url_param_name_l + 1;
11745
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011746 /* Check for pointer wrapping. */
11747 if (fix_pointer_if_wrap(chunks, &value_start)) {
11748 /* Update the end pointer. */
11749 qs_end = chunks[3];
11750
11751 /* Check for overflow. */
11752 if (value_start > qs_end)
11753 return 0;
11754 }
11755 }
11756
David Cournapeau16023ee2010-12-23 20:55:41 +090011757 value_end = value_start;
11758
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011759 while (1) {
11760 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11761 value_end++;
11762 if (value_end < qs_end)
11763 break;
11764 /* process buffer wrapping. */
11765 if (value_end >= chunks[0] &&
11766 value_end <= chunks[1] &&
11767 chunks[2]) {
11768 value_end = chunks[2];
11769 qs_end = chunks[3];
11770 continue;
11771 }
11772 break;
11773 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011774
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011775 *vstart = value_start;
11776 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011777 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011778}
11779
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011780/* This scans a URL-encoded query string. It takes an optionally wrapping
11781 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11782 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11783 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011784 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011785static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011786smp_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 +090011787{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011788 const char *vstart, *vend;
11789 struct chunk *temp;
11790 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011791
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011792 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011793 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011794 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011795 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011796 return 0;
11797
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011798 /* Create sample. If the value is contiguous, return the pointer as CONST,
11799 * if the value is wrapped, copy-it in a buffer.
11800 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011801 smp->type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011802 if (chunks[2] &&
11803 vstart >= chunks[0] && vstart <= chunks[1] &&
11804 vend >= chunks[2] && vend <= chunks[3]) {
11805 /* Wrapped case. */
11806 temp = get_trash_chunk();
11807 memcpy(temp->str, vstart, chunks[1] - vstart);
11808 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
11809 smp->data.str.str = temp->str;
11810 smp->data.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
11811 } else {
11812 /* Contiguous case. */
11813 smp->data.str.str = (char *)vstart;
11814 smp->data.str.len = vend - vstart;
11815 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11816 }
11817
11818 /* Update context, check wrapping. */
11819 chunks[0] = vend;
11820 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11821 chunks[1] = chunks[3];
11822 chunks[2] = NULL;
11823 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011824
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011825 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011826 smp->flags |= SMP_F_NOT_LAST;
11827
David Cournapeau16023ee2010-12-23 20:55:41 +090011828 return 1;
11829}
11830
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011831/* This function iterates over each parameter of the query string. It uses
11832 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011833 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11834 * An optional parameter name is passed in args[0], otherwise any parameter is
11835 * considered. It supports an optional delimiter argument for the beginning of
11836 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011837 */
11838static int
11839smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11840{
11841 struct http_msg *msg;
11842 char delim = '?';
11843 const char *name;
11844 int name_len;
11845
Dragan Dosen26f77e52015-05-25 10:02:11 +020011846 if (!args ||
11847 (args[0].type && args[0].type != ARGT_STR) ||
11848 (args[1].type && args[1].type != ARGT_STR))
11849 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011850
Dragan Dosen26f77e52015-05-25 10:02:11 +020011851 name = "";
11852 name_len = 0;
11853 if (args->type == ARGT_STR) {
11854 name = args->data.str.str;
11855 name_len = args->data.str.len;
11856 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011857
Dragan Dosen26f77e52015-05-25 10:02:11 +020011858 if (args[1].type)
11859 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011860
Dragan Dosen26f77e52015-05-25 10:02:11 +020011861 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011862 CHECK_HTTP_MESSAGE_FIRST();
11863
11864 msg = &smp->strm->txn->req;
11865
11866 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11867 msg->sl.rq.u_l, delim);
11868 if (!smp->ctx.a[0])
11869 return 0;
11870
11871 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011872
11873 /* Assume that the context is filled with NULL pointer
11874 * before the first call.
11875 * smp->ctx.a[2] = NULL;
11876 * smp->ctx.a[3] = NULL;
11877 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011878 }
11879
11880 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11881}
11882
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011883/* This function iterates over each parameter of the body. This requires
11884 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011885 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11886 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11887 * optional second part if the body wraps at the end of the buffer. An optional
11888 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011889 */
11890static int
11891smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11892{
11893 struct http_txn *txn = smp->strm->txn;
11894 struct http_msg *msg;
11895 unsigned long len;
11896 unsigned long block1;
11897 char *body;
11898 const char *name;
11899 int name_len;
11900
11901 if (!args || (args[0].type && args[0].type != ARGT_STR))
11902 return 0;
11903
11904 name = "";
11905 name_len = 0;
11906 if (args[0].type == ARGT_STR) {
11907 name = args[0].data.str.str;
11908 name_len = args[0].data.str.len;
11909 }
11910
11911 if (!smp->ctx.a[0]) { // first call, find the query string
11912 CHECK_HTTP_MESSAGE_FIRST();
11913
11914 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11915 msg = &txn->req;
11916 else
11917 msg = &txn->rsp;
11918
11919 len = http_body_bytes(msg);
11920 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11921
11922 block1 = len;
11923 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11924 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11925
11926 if (block1 == len) {
11927 /* buffer is not wrapped (or empty) */
11928 smp->ctx.a[0] = body;
11929 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011930
11931 /* Assume that the context is filled with NULL pointer
11932 * before the first call.
11933 * smp->ctx.a[2] = NULL;
11934 * smp->ctx.a[3] = NULL;
11935 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011936 }
11937 else {
11938 /* buffer is wrapped, we need to defragment it */
11939 smp->ctx.a[0] = body;
11940 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011941 smp->ctx.a[2] = msg->chn->buf->data;
11942 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011943 }
11944 }
11945 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11946}
11947
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011948/* Return the signed integer value for the specified url parameter (see url_param
11949 * above).
11950 */
11951static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011952smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011953{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011954 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011955
11956 if (ret > 0) {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011957 smp->type = SMP_T_SINT;
11958 smp->data.sint = strl2ic(smp->data.str.str, smp->data.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011959 }
11960
11961 return ret;
11962}
11963
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011964/* This produces a 32-bit hash of the concatenation of the first occurrence of
11965 * the Host header followed by the path component if it begins with a slash ('/').
11966 * This means that '*' will not be added, resulting in exactly the first Host
11967 * entry. If no Host header is found, then the path is used. The resulting value
11968 * is hashed using the url hash followed by a full avalanche hash and provides a
11969 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11970 * high-traffic sites without having to store whole paths.
11971 * this differs from the base32 functions in that it includes the url parameters
11972 * as well as the path
11973 */
11974static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011975smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011976{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011977 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011978 struct hdr_ctx ctx;
11979 unsigned int hash = 0;
11980 char *ptr, *beg, *end;
11981 int len;
11982
11983 CHECK_HTTP_MESSAGE_FIRST();
11984
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011985 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011986 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011987 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011988 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11989 ptr = ctx.line + ctx.val;
11990 len = ctx.vlen;
11991 while (len--)
11992 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11993 }
11994
11995 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011996 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 +000011997 beg = http_get_path(txn);
11998 if (!beg)
11999 beg = end;
12000
12001 for (ptr = beg; ptr < end ; ptr++);
12002
12003 if (beg < ptr && *beg == '/') {
12004 while (beg < ptr)
12005 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12006 }
12007 hash = full_hash(hash);
12008
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012009 smp->type = SMP_T_SINT;
12010 smp->data.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012011 smp->flags = SMP_F_VOL_1ST;
12012 return 1;
12013}
12014
12015/* This concatenates the source address with the 32-bit hash of the Host and
12016 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12017 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12018 * on the source address length. The URL hash is stored before the address so
12019 * that in environments where IPv6 is insignificant, truncating the output to
12020 * 8 bytes would still work.
12021 */
12022static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012023smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012024{
12025 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012026 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012027 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012028
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012029 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012030 return 0;
12031
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012032 /* The returned hash is a 32 bytes integer. */
12033 hash = smp->data.sint;
12034
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012035 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012036 memcpy(temp->str + temp->len, &hash, sizeof(hash));
12037 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012038
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012039 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012040 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012041 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012042 temp->len += 4;
12043 break;
12044 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012045 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012046 temp->len += 16;
12047 break;
12048 default:
12049 return 0;
12050 }
12051
12052 smp->data.str = *temp;
12053 smp->type = SMP_T_BIN;
12054 return 1;
12055}
12056
Willy Tarreau185b5c42012-04-26 15:11:51 +020012057/* This function is used to validate the arguments passed to any "hdr" fetch
12058 * keyword. These keywords support an optional positive or negative occurrence
12059 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12060 * is assumed that the types are already the correct ones. Returns 0 on error,
12061 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12062 * error message in case of error, that the caller is responsible for freeing.
12063 * The initial location must either be freeable or NULL.
12064 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012065int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012066{
12067 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012068 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012069 return 0;
12070 }
12071 return 1;
12072}
12073
Willy Tarreau276fae92013-07-25 14:36:01 +020012074/* takes an UINT value on input supposed to represent the time since EPOCH,
12075 * adds an optional offset found in args[0] and emits a string representing
12076 * the date in RFC-1123/5322 format.
12077 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012078static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012079{
12080 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
12081 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12082 struct chunk *temp;
12083 struct tm *tm;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012084 time_t curr_date = smp->data.sint;
Willy Tarreau276fae92013-07-25 14:36:01 +020012085
12086 /* add offset */
12087 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
12088 curr_date += args[0].data.sint;
12089
12090 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012091 if (!tm)
12092 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012093
12094 temp = get_trash_chunk();
12095 temp->len = snprintf(temp->str, temp->size - temp->len,
12096 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12097 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12098 tm->tm_hour, tm->tm_min, tm->tm_sec);
12099
12100 smp->data.str = *temp;
12101 smp->type = SMP_T_STR;
12102 return 1;
12103}
12104
Thierry FOURNIERad903512014-04-11 17:51:01 +020012105/* Match language range with language tag. RFC2616 14.4:
12106 *
12107 * A language-range matches a language-tag if it exactly equals
12108 * the tag, or if it exactly equals a prefix of the tag such
12109 * that the first tag character following the prefix is "-".
12110 *
12111 * Return 1 if the strings match, else return 0.
12112 */
12113static inline int language_range_match(const char *range, int range_len,
12114 const char *tag, int tag_len)
12115{
12116 const char *end = range + range_len;
12117 const char *tend = tag + tag_len;
12118 while (range < end) {
12119 if (*range == '-' && tag == tend)
12120 return 1;
12121 if (*range != *tag || tag == tend)
12122 return 0;
12123 range++;
12124 tag++;
12125 }
12126 /* Return true only if the last char of the tag is matched. */
12127 return tag == tend;
12128}
12129
12130/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012131static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012132{
12133 const char *al = smp->data.str.str;
12134 const char *end = al + smp->data.str.len;
12135 const char *token;
12136 int toklen;
12137 int qvalue;
12138 const char *str;
12139 const char *w;
12140 int best_q = 0;
12141
12142 /* Set the constant to the sample, because the output of the
12143 * function will be peek in the constant configuration string.
12144 */
12145 smp->flags |= SMP_F_CONST;
12146 smp->data.str.size = 0;
12147 smp->data.str.str = "";
12148 smp->data.str.len = 0;
12149
12150 /* Parse the accept language */
12151 while (1) {
12152
12153 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012154 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012155 al++;
12156 if (al >= end)
12157 break;
12158
12159 /* Start of the fisrt word. */
12160 token = al;
12161
12162 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012163 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012164 al++;
12165 if (al == token)
12166 goto expect_comma;
12167
12168 /* Length of the token. */
12169 toklen = al - token;
12170 qvalue = 1000;
12171
12172 /* Check if the token exists in the list. If the token not exists,
12173 * jump to the next token.
12174 */
12175 str = args[0].data.str.str;
12176 w = str;
12177 while (1) {
12178 if (*str == ';' || *str == '\0') {
12179 if (language_range_match(token, toklen, w, str-w))
12180 goto look_for_q;
12181 if (*str == '\0')
12182 goto expect_comma;
12183 w = str + 1;
12184 }
12185 str++;
12186 }
12187 goto expect_comma;
12188
12189look_for_q:
12190
12191 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012192 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012193 al++;
12194 if (al >= end)
12195 goto process_value;
12196
12197 /* If ',' is found, process the result */
12198 if (*al == ',')
12199 goto process_value;
12200
12201 /* If the character is different from ';', look
12202 * for the end of the header part in best effort.
12203 */
12204 if (*al != ';')
12205 goto expect_comma;
12206
12207 /* Assumes that the char is ';', now expect "q=". */
12208 al++;
12209
12210 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012211 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012212 al++;
12213 if (al >= end)
12214 goto process_value;
12215
12216 /* Expect 'q'. If no 'q', continue in best effort */
12217 if (*al != 'q')
12218 goto process_value;
12219 al++;
12220
12221 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012222 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012223 al++;
12224 if (al >= end)
12225 goto process_value;
12226
12227 /* Expect '='. If no '=', continue in best effort */
12228 if (*al != '=')
12229 goto process_value;
12230 al++;
12231
12232 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012233 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012234 al++;
12235 if (al >= end)
12236 goto process_value;
12237
12238 /* Parse the q value. */
12239 qvalue = parse_qvalue(al, &al);
12240
12241process_value:
12242
12243 /* If the new q value is the best q value, then store the associated
12244 * language in the response. If qvalue is the biggest value (1000),
12245 * break the process.
12246 */
12247 if (qvalue > best_q) {
12248 smp->data.str.str = (char *)w;
12249 smp->data.str.len = str - w;
12250 if (qvalue >= 1000)
12251 break;
12252 best_q = qvalue;
12253 }
12254
12255expect_comma:
12256
12257 /* Expect comma or end. If the end is detected, quit the loop. */
12258 while (al < end && *al != ',')
12259 al++;
12260 if (al >= end)
12261 break;
12262
12263 /* Comma is found, jump it and restart the analyzer. */
12264 al++;
12265 }
12266
12267 /* Set default value if required. */
12268 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
12269 smp->data.str.str = args[1].data.str.str;
12270 smp->data.str.len = args[1].data.str.len;
12271 }
12272
12273 /* Return true only if a matching language was found. */
12274 return smp->data.str.len != 0;
12275}
12276
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012277/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012278static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012279{
12280 /* If the constant flag is set or if not size is avalaible at
12281 * the end of the buffer, copy the string in other buffer
12282 * before decoding.
12283 */
12284 if (smp->flags & SMP_F_CONST || smp->data.str.size <= smp->data.str.len) {
12285 struct chunk *str = get_trash_chunk();
12286 memcpy(str->str, smp->data.str.str, smp->data.str.len);
12287 smp->data.str.str = str->str;
12288 smp->data.str.size = str->size;
12289 smp->flags &= ~SMP_F_CONST;
12290 }
12291
12292 /* Add final \0 required by url_decode(), and convert the input string. */
12293 smp->data.str.str[smp->data.str.len] = '\0';
12294 smp->data.str.len = url_decode(smp->data.str.str);
12295 return 1;
12296}
12297
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012298static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12299{
12300 struct proxy *fe = strm_fe(smp->strm);
12301 int idx, i;
12302 struct cap_hdr *hdr;
12303 int len;
12304
12305 if (!args || args->type != ARGT_UINT)
12306 return 0;
12307
12308 idx = args->data.uint;
12309
12310 /* Check the availibity of the capture id. */
12311 if (idx > fe->nb_req_cap - 1)
12312 return 0;
12313
12314 /* Look for the original configuration. */
12315 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12316 hdr != NULL && i != idx ;
12317 i--, hdr = hdr->next);
12318 if (!hdr)
12319 return 0;
12320
12321 /* check for the memory allocation */
12322 if (smp->strm->req_cap[hdr->index] == NULL)
12323 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12324 if (smp->strm->req_cap[hdr->index] == NULL)
12325 return 0;
12326
12327 /* Check length. */
12328 len = smp->data.str.len;
12329 if (len > hdr->len)
12330 len = hdr->len;
12331
12332 /* Capture input data. */
12333 memcpy(smp->strm->req_cap[idx], smp->data.str.str, len);
12334 smp->strm->req_cap[idx][len] = '\0';
12335
12336 return 1;
12337}
12338
12339static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12340{
12341 struct proxy *fe = strm_fe(smp->strm);
12342 int idx, i;
12343 struct cap_hdr *hdr;
12344 int len;
12345
12346 if (!args || args->type != ARGT_UINT)
12347 return 0;
12348
12349 idx = args->data.uint;
12350
12351 /* Check the availibity of the capture id. */
12352 if (idx > fe->nb_rsp_cap - 1)
12353 return 0;
12354
12355 /* Look for the original configuration. */
12356 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12357 hdr != NULL && i != idx ;
12358 i--, hdr = hdr->next);
12359 if (!hdr)
12360 return 0;
12361
12362 /* check for the memory allocation */
12363 if (smp->strm->res_cap[hdr->index] == NULL)
12364 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12365 if (smp->strm->res_cap[hdr->index] == NULL)
12366 return 0;
12367
12368 /* Check length. */
12369 len = smp->data.str.len;
12370 if (len > hdr->len)
12371 len = hdr->len;
12372
12373 /* Capture input data. */
12374 memcpy(smp->strm->res_cap[idx], smp->data.str.str, len);
12375 smp->strm->res_cap[idx][len] = '\0';
12376
12377 return 1;
12378}
12379
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012380/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012381 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012382 * the relevant part of the request line accordingly. Then it updates various
12383 * pointers to the next elements which were moved, and the total buffer length.
12384 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012385 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12386 * error, though this can be revisited when this code is finally exploited.
12387 *
12388 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12389 * query string and 3 to replace uri.
12390 *
12391 * In query string case, the mark question '?' must be set at the start of the
12392 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012393 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012394int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012395 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012396{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012397 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012398 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012399 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012400 int delta;
12401
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012402 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012403 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012404 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012405 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12406
12407 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012408 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012409 txn->req.sl.rq.m_l += delta;
12410 txn->req.sl.rq.u += delta;
12411 txn->req.sl.rq.v += delta;
12412 break;
12413
12414 case 1: // path
12415 cur_ptr = http_get_path(txn);
12416 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012417 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012418
12419 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012420 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 +010012421 cur_end++;
12422
12423 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012424 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012425 txn->req.sl.rq.u_l += delta;
12426 txn->req.sl.rq.v += delta;
12427 break;
12428
12429 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012430 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012431 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012432 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12433 while (cur_ptr < cur_end && *cur_ptr != '?')
12434 cur_ptr++;
12435
12436 /* skip the question mark or indicate that we must insert it
12437 * (but only if the format string is not empty then).
12438 */
12439 if (cur_ptr < cur_end)
12440 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012441 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012442 offset = 0;
12443
12444 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012445 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012446 txn->req.sl.rq.u_l += delta;
12447 txn->req.sl.rq.v += delta;
12448 break;
12449
12450 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012451 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012452 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12453
12454 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012455 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012456 txn->req.sl.rq.u_l += delta;
12457 txn->req.sl.rq.v += delta;
12458 break;
12459
12460 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012461 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012462 }
12463
12464 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012465 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012466 txn->req.sl.rq.l += delta;
12467 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012468 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012469 return 0;
12470}
12471
12472/* This function executes one of the set-{method,path,query,uri} actions. It
12473 * builds a string in the trash from the specified format string. It finds
12474 * the action to be performed in p[2], previously filled by function
12475 * parse_set_req_line(). The replacement action is excuted by the function
12476 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
12477 * the action is canceled, but the rule processing continue.
12478 */
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012479int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012480{
12481 chunk_reset(&trash);
12482
12483 /* If we have to create a query string, prepare a '?'. */
12484 if (*(int *)&rule->arg.act.p[2] == 2)
12485 trash.str[trash.len++] = '?';
12486 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
12487
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012488 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010012489 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012490}
12491
12492/* parse an http-request action among :
12493 * set-method
12494 * set-path
12495 * set-query
12496 * set-uri
12497 *
12498 * All of them accept a single argument of type string representing a log-format.
12499 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12500 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
12501 * It returns 0 on success, < 0 on error.
12502 */
12503int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
12504{
12505 int cur_arg = *orig_arg;
12506
12507 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
12508
12509 switch (args[0][4]) {
12510 case 'm' :
12511 *(int *)&rule->arg.act.p[2] = 0;
12512 rule->action_ptr = http_action_set_req_line;
12513 break;
12514 case 'p' :
12515 *(int *)&rule->arg.act.p[2] = 1;
12516 rule->action_ptr = http_action_set_req_line;
12517 break;
12518 case 'q' :
12519 *(int *)&rule->arg.act.p[2] = 2;
12520 rule->action_ptr = http_action_set_req_line;
12521 break;
12522 case 'u' :
12523 *(int *)&rule->arg.act.p[2] = 3;
12524 rule->action_ptr = http_action_set_req_line;
12525 break;
12526 default:
12527 memprintf(err, "internal error: unhandled action '%s'", args[0]);
12528 return -1;
12529 }
12530
12531 if (!*args[cur_arg] ||
12532 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12533 memprintf(err, "expects exactly 1 argument <format>");
12534 return -1;
12535 }
12536
12537 LIST_INIT((struct list *)&rule->arg.act.p[0]);
12538 proxy->conf.args.ctx = ARGC_HRQ;
12539 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
12540 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12541 proxy->conf.args.file, proxy->conf.args.line);
12542
12543 (*orig_arg)++;
12544 return 0;
12545}
12546
Willy Tarreaua9083d02015-05-08 15:27:59 +020012547/* This function executes the "capture" action. It executes a fetch expression,
12548 * turns the result into a string and puts it in a capture slot. It always
12549 * returns 1. If an error occurs the action is cancelled, but the rule
12550 * processing continues.
12551 */
12552int http_action_req_capture(struct http_req_rule *rule, struct proxy *px, struct stream *s)
12553{
12554 struct session *sess = s->sess;
12555 struct sample *key;
12556 struct sample_expr *expr = rule->arg.act.p[0];
12557 struct cap_hdr *h = rule->arg.act.p[1];
12558 char **cap = s->req_cap;
12559 int len;
12560
Adis Nezirovic79beb242015-07-06 15:41:02 +020012561 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr, SMP_T_STR);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012562 if (!key)
12563 return 1;
12564
12565 if (cap[h->index] == NULL)
12566 cap[h->index] = pool_alloc2(h->pool);
12567
12568 if (cap[h->index] == NULL) /* no more capture memory */
12569 return 1;
12570
12571 len = key->data.str.len;
12572 if (len > h->len)
12573 len = h->len;
12574
12575 memcpy(cap[h->index], key->data.str.str, len);
12576 cap[h->index][len] = 0;
12577 return 1;
12578}
12579
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012580/* This function executes the "capture" action and store the result in a
12581 * capture slot if exists. It executes a fetch expression, turns the result
12582 * into a string and puts it in a capture slot. It always returns 1. If an
12583 * error occurs the action is cancelled, but the rule processing continues.
12584 */
12585int http_action_req_capture_by_id(struct http_req_rule *rule, struct proxy *px, struct stream *s)
12586{
12587 struct session *sess = s->sess;
12588 struct sample *key;
12589 struct sample_expr *expr = rule->arg.act.p[0];
12590 struct cap_hdr *h;
12591 int idx = (long)rule->arg.act.p[1];
12592 char **cap = s->req_cap;
12593 struct proxy *fe = strm_fe(s);
12594 int len;
12595 int i;
12596
12597 /* Look for the original configuration. */
12598 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
12599 h != NULL && i != idx ;
12600 i--, h = h->next);
12601 if (!h)
12602 return 1;
12603
Adis Nezirovic79beb242015-07-06 15:41:02 +020012604 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, expr, SMP_T_STR);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012605 if (!key)
12606 return 1;
12607
12608 if (cap[h->index] == NULL)
12609 cap[h->index] = pool_alloc2(h->pool);
12610
12611 if (cap[h->index] == NULL) /* no more capture memory */
12612 return 1;
12613
12614 len = key->data.str.len;
12615 if (len > h->len)
12616 len = h->len;
12617
12618 memcpy(cap[h->index], key->data.str.str, len);
12619 cap[h->index][len] = 0;
12620 return 1;
12621}
12622
Willy Tarreaua9083d02015-05-08 15:27:59 +020012623/* parse an "http-request capture" action. It takes a single argument which is
12624 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012625 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Willy Tarreaua9083d02015-05-08 15:27:59 +020012626 * It returns 0 on success, < 0 on error.
12627 */
12628int parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
12629{
12630 struct sample_expr *expr;
12631 struct cap_hdr *hdr;
12632 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012633 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012634
12635 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12636 if (strcmp(args[cur_arg], "if") == 0 ||
12637 strcmp(args[cur_arg], "unless") == 0)
12638 break;
12639
12640 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012641 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Willy Tarreaua9083d02015-05-08 15:27:59 +020012642 return -1;
12643 }
12644
Willy Tarreaua9083d02015-05-08 15:27:59 +020012645 cur_arg = *orig_arg;
12646 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12647 if (!expr)
12648 return -1;
12649
12650 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12651 memprintf(err,
12652 "fetch method '%s' extracts information from '%s', none of which is available here",
12653 args[cur_arg-1], sample_src_names(expr->fetch->use));
12654 free(expr);
12655 return -1;
12656 }
12657
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012658 if (!args[cur_arg] || !*args[cur_arg]) {
12659 memprintf(err, "expects 'len or 'id'");
12660 free(expr);
12661 return -1;
12662 }
12663
Willy Tarreaua9083d02015-05-08 15:27:59 +020012664 if (strcmp(args[cur_arg], "len") == 0) {
12665 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012666
12667 if (!(px->cap & PR_CAP_FE)) {
12668 memprintf(err, "proxy '%s' has no frontend capability", px->id);
12669 return -1;
12670 }
12671
12672 proxy->conf.args.ctx = ARGC_CAP;
12673
Willy Tarreaua9083d02015-05-08 15:27:59 +020012674 if (!args[cur_arg]) {
12675 memprintf(err, "missing length value");
12676 free(expr);
12677 return -1;
12678 }
12679 /* we copy the table name for now, it will be resolved later */
12680 len = atoi(args[cur_arg]);
12681 if (len <= 0) {
12682 memprintf(err, "length must be > 0");
12683 free(expr);
12684 return -1;
12685 }
12686 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012687
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012688 if (!len) {
12689 memprintf(err, "a positive 'len' argument is mandatory");
12690 free(expr);
12691 return -1;
12692 }
12693
12694 hdr = calloc(sizeof(struct cap_hdr), 1);
12695 hdr->next = px->req_cap;
12696 hdr->name = NULL; /* not a header capture */
12697 hdr->namelen = 0;
12698 hdr->len = len;
12699 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12700 hdr->index = px->nb_req_cap++;
12701
12702 px->req_cap = hdr;
12703 px->to_log |= LW_REQHDR;
12704
12705 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
12706 rule->action_ptr = http_action_req_capture;
12707 rule->arg.act.p[0] = expr;
12708 rule->arg.act.p[1] = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012709 }
12710
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012711 else if (strcmp(args[cur_arg], "id") == 0) {
12712 int id;
12713 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012714
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012715 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012716
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012717 if (!args[cur_arg]) {
12718 memprintf(err, "missing id value");
12719 free(expr);
12720 return -1;
12721 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012722
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012723 id = strtol(args[cur_arg], &error, 10);
12724 if (*error != '\0') {
12725 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12726 free(expr);
12727 return -1;
12728 }
12729 cur_arg++;
12730
12731 proxy->conf.args.ctx = ARGC_CAP;
12732
12733 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
12734 rule->action_ptr = http_action_req_capture_by_id;
12735 rule->arg.act.p[0] = expr;
12736 rule->arg.act.p[1] = (void *)(long)id;
12737 }
12738
12739 else {
12740 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12741 free(expr);
12742 return -1;
12743 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012744
12745 *orig_arg = cur_arg;
12746 return 0;
12747}
12748
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012749/* This function executes the "capture" action and store the result in a
12750 * capture slot if exists. It executes a fetch expression, turns the result
12751 * into a string and puts it in a capture slot. It always returns 1. If an
12752 * error occurs the action is cancelled, but the rule processing continues.
12753 */
12754int http_action_res_capture_by_id(struct http_res_rule *rule, struct proxy *px, struct stream *s)
12755{
12756 struct session *sess = s->sess;
12757 struct sample *key;
12758 struct sample_expr *expr = rule->arg.act.p[0];
12759 struct cap_hdr *h;
12760 int idx = (long)rule->arg.act.p[1];
12761 char **cap = s->res_cap;
12762 struct proxy *fe = strm_fe(s);
12763 int len;
12764 int i;
12765
12766 /* Look for the original configuration. */
12767 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12768 h != NULL && i != idx ;
12769 i--, h = h->next);
12770 if (!h)
12771 return 1;
12772
Adis Nezirovic79beb242015-07-06 15:41:02 +020012773 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, expr, SMP_T_STR);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012774 if (!key)
12775 return 1;
12776
12777 if (cap[h->index] == NULL)
12778 cap[h->index] = pool_alloc2(h->pool);
12779
12780 if (cap[h->index] == NULL) /* no more capture memory */
12781 return 1;
12782
12783 len = key->data.str.len;
12784 if (len > h->len)
12785 len = h->len;
12786
12787 memcpy(cap[h->index], key->data.str.str, len);
12788 cap[h->index][len] = 0;
12789 return 1;
12790}
12791
12792/* parse an "http-response capture" action. It takes a single argument which is
12793 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12794 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
12795 * It returns 0 on success, < 0 on error.
12796 */
12797int parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px, struct http_res_rule *rule, char **err)
12798{
12799 struct sample_expr *expr;
12800 int cur_arg;
12801 int id;
12802 char *error;
12803
12804 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12805 if (strcmp(args[cur_arg], "if") == 0 ||
12806 strcmp(args[cur_arg], "unless") == 0)
12807 break;
12808
12809 if (cur_arg < *orig_arg + 3) {
12810 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
12811 return -1;
12812 }
12813
12814 cur_arg = *orig_arg;
12815 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12816 if (!expr)
12817 return -1;
12818
12819 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12820 memprintf(err,
12821 "fetch method '%s' extracts information from '%s', none of which is available here",
12822 args[cur_arg-1], sample_src_names(expr->fetch->use));
12823 free(expr);
12824 return -1;
12825 }
12826
12827 if (!args[cur_arg] || !*args[cur_arg]) {
12828 memprintf(err, "expects 'len or 'id'");
12829 free(expr);
12830 return -1;
12831 }
12832
12833 if (strcmp(args[cur_arg], "id") != 0) {
12834 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12835 free(expr);
12836 return -1;
12837 }
12838
12839 cur_arg++;
12840
12841 if (!args[cur_arg]) {
12842 memprintf(err, "missing id value");
12843 free(expr);
12844 return -1;
12845 }
12846
12847 id = strtol(args[cur_arg], &error, 10);
12848 if (*error != '\0') {
12849 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12850 free(expr);
12851 return -1;
12852 }
12853 cur_arg++;
12854
12855 LIST_INIT((struct list *)&rule->arg.act.p[0]);
12856 proxy->conf.args.ctx = ARGC_CAP;
12857
12858 rule->action = HTTP_RES_ACT_CUSTOM_CONT;
12859 rule->action_ptr = http_action_res_capture_by_id;
12860 rule->arg.act.p[0] = expr;
12861 rule->arg.act.p[1] = (void *)(long)id;
12862
12863 *orig_arg = cur_arg;
12864 return 0;
12865}
12866
William Lallemand73025dd2014-04-24 14:38:37 +020012867/*
12868 * Return the struct http_req_action_kw associated to a keyword.
12869 */
12870struct http_req_action_kw *action_http_req_custom(const char *kw)
12871{
12872 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
12873 struct http_req_action_kw_list *kw_list;
12874 int i;
12875
12876 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
12877 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
Thierry FOURNIER0e118632015-06-04 11:44:06 +020012878 if (kw_list->kw[i].match_pfx &&
12879 strncmp(kw, kw_list->kw[i].kw, strlen(kw_list->kw[i].kw)) == 0)
12880 return &kw_list->kw[i];
William Lallemand73025dd2014-04-24 14:38:37 +020012881 if (!strcmp(kw, kw_list->kw[i].kw))
12882 return &kw_list->kw[i];
12883 }
12884 }
12885 }
12886 return NULL;
12887}
12888
12889/*
12890 * Return the struct http_res_action_kw associated to a keyword.
12891 */
12892struct http_res_action_kw *action_http_res_custom(const char *kw)
12893{
12894 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
12895 struct http_res_action_kw_list *kw_list;
12896 int i;
12897
12898 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
12899 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
Thierry FOURNIER0e118632015-06-04 11:44:06 +020012900 if (kw_list->kw[i].match_pfx &&
12901 strncmp(kw, kw_list->kw[i].kw, strlen(kw_list->kw[i].kw)) == 0)
12902 return &kw_list->kw[i];
William Lallemand73025dd2014-04-24 14:38:37 +020012903 if (!strcmp(kw, kw_list->kw[i].kw))
12904 return &kw_list->kw[i];
12905 }
12906 }
12907 }
12908 return NULL;
12909}
12910
Willy Tarreau4a568972010-05-12 08:08:50 +020012911/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012912/* All supported ACL keywords must be declared here. */
12913/************************************************************************/
12914
12915/* Note: must not be declared <const> as its list will be overwritten.
12916 * Please take care of keeping this list alphabetically sorted.
12917 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012918static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012919 { "base", "base", PAT_MATCH_STR },
12920 { "base_beg", "base", PAT_MATCH_BEG },
12921 { "base_dir", "base", PAT_MATCH_DIR },
12922 { "base_dom", "base", PAT_MATCH_DOM },
12923 { "base_end", "base", PAT_MATCH_END },
12924 { "base_len", "base", PAT_MATCH_LEN },
12925 { "base_reg", "base", PAT_MATCH_REG },
12926 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012927
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012928 { "cook", "req.cook", PAT_MATCH_STR },
12929 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12930 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12931 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12932 { "cook_end", "req.cook", PAT_MATCH_END },
12933 { "cook_len", "req.cook", PAT_MATCH_LEN },
12934 { "cook_reg", "req.cook", PAT_MATCH_REG },
12935 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012936
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012937 { "hdr", "req.hdr", PAT_MATCH_STR },
12938 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12939 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12940 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12941 { "hdr_end", "req.hdr", PAT_MATCH_END },
12942 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12943 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12944 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012945
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012946 /* these two declarations uses strings with list storage (in place
12947 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12948 * and delete functions are relative to the list management. The parse
12949 * and match method are related to the corresponding fetch methods. This
12950 * is very particular ACL declaration mode.
12951 */
12952 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12953 { "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 +020012954
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012955 { "path", "path", PAT_MATCH_STR },
12956 { "path_beg", "path", PAT_MATCH_BEG },
12957 { "path_dir", "path", PAT_MATCH_DIR },
12958 { "path_dom", "path", PAT_MATCH_DOM },
12959 { "path_end", "path", PAT_MATCH_END },
12960 { "path_len", "path", PAT_MATCH_LEN },
12961 { "path_reg", "path", PAT_MATCH_REG },
12962 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012963
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012964 { "req_ver", "req.ver", PAT_MATCH_STR },
12965 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012966
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012967 { "scook", "res.cook", PAT_MATCH_STR },
12968 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12969 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12970 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12971 { "scook_end", "res.cook", PAT_MATCH_END },
12972 { "scook_len", "res.cook", PAT_MATCH_LEN },
12973 { "scook_reg", "res.cook", PAT_MATCH_REG },
12974 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012975
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012976 { "shdr", "res.hdr", PAT_MATCH_STR },
12977 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12978 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12979 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12980 { "shdr_end", "res.hdr", PAT_MATCH_END },
12981 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12982 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12983 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012984
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012985 { "url", "url", PAT_MATCH_STR },
12986 { "url_beg", "url", PAT_MATCH_BEG },
12987 { "url_dir", "url", PAT_MATCH_DIR },
12988 { "url_dom", "url", PAT_MATCH_DOM },
12989 { "url_end", "url", PAT_MATCH_END },
12990 { "url_len", "url", PAT_MATCH_LEN },
12991 { "url_reg", "url", PAT_MATCH_REG },
12992 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012993
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012994 { "urlp", "urlp", PAT_MATCH_STR },
12995 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12996 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12997 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12998 { "urlp_end", "urlp", PAT_MATCH_END },
12999 { "urlp_len", "urlp", PAT_MATCH_LEN },
13000 { "urlp_reg", "urlp", PAT_MATCH_REG },
13001 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013002
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013003 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013004}};
13005
13006/************************************************************************/
13007/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013008/************************************************************************/
13009/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013010static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013011 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013012 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013013 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13014
Willy Tarreau87b09662015-04-03 00:22:06 +020013015 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013016 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013017
13018 /* retrieve these captures from the HTTP logs */
13019 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13020 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13021 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13022
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013023 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013024 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013025
Willy Tarreau409bcde2013-01-08 00:31:00 +010013026 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13027 * are only here to match the ACL's name, are request-only and are used
13028 * for ACL compatibility only.
13029 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013030 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13031 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013032 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13033 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013034
13035 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13036 * only here to match the ACL's name, are request-only and are used for
13037 * ACL compatibility only.
13038 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013039 { "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 +020013040 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013041 { "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 +020013042 { "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 +010013043
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013044 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013045 { "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 +010013046 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013047 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013048 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013049 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013050
13051 /* HTTP protocol on the request path */
13052 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013053 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013054
13055 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013056 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13057 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013058
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013059 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013060 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13061 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013062 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013063
Willy Tarreau18ed2562013-01-14 15:56:36 +010013064 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013065 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13066 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013067
Willy Tarreau18ed2562013-01-14 15:56:36 +010013068 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013069 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013070 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13071 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013072
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013073 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013074 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013075 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013076 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013077 { "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 +010013078 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013079 { "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 +010013080
13081 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013082 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013083 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13084 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013085
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013086 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013087 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013088 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013089 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013090 { "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 +010013091 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013092 { "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 +010013093
Willy Tarreau409bcde2013-01-08 00:31:00 +010013094 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013095 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013096 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13097 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013098 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013099
13100 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013101 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013102 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013103 { "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 +020013104 { "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 +010013105
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013106 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013107 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013108 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013109 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013110 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013111 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013112 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13113 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013114 { "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 +010013115 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013116}};
13117
Willy Tarreau8797c062007-05-07 00:55:35 +020013118
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013119/************************************************************************/
13120/* All supported converter keywords must be declared here. */
13121/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013122/* Note: must not be declared <const> as its list will be overwritten */
13123static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013124 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013125 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013126 { "capture-req", smp_conv_req_capture, ARG1(1,UINT), NULL, SMP_T_STR, SMP_T_STR},
13127 { "capture-res", smp_conv_res_capture, ARG1(1,UINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013128 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013129 { NULL, NULL, 0, 0, 0 },
13130}};
13131
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013132
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013133/************************************************************************/
13134/* All supported http-request action keywords must be declared here. */
13135/************************************************************************/
13136struct http_req_action_kw_list http_req_actions = {
13137 .scope = "http",
13138 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013139 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013140 { "set-method", parse_set_req_line },
13141 { "set-path", parse_set_req_line },
13142 { "set-query", parse_set_req_line },
13143 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013144 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013145 }
13146};
13147
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013148struct http_res_action_kw_list http_res_actions = {
13149 .scope = "http",
13150 .kw = {
13151 { "capture", parse_http_res_capture },
13152 { NULL, NULL }
13153 }
13154};
13155
Willy Tarreau8797c062007-05-07 00:55:35 +020013156__attribute__((constructor))
13157static void __http_protocol_init(void)
13158{
13159 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013160 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013161 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013162 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013163 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013164}
13165
13166
Willy Tarreau58f10d72006-12-04 02:26:12 +010013167/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013168 * Local variables:
13169 * c-indent-level: 8
13170 * c-basic-offset: 8
13171 * End:
13172 */