blob: 61b29a1ed3ec314992a135a2315f8f87f43572b2 [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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800127 "<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 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau87b09662015-04-03 00:22:06 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200329 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200330 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100331 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100332}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333
Willy Tarreau53b6c742006-12-17 13:37:46 +0100334/*
335 * We have 26 list of methods (1 per first letter), each of which can have
336 * up to 3 entries (2 valid, 1 null).
337 */
338struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100339 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100340 int len;
341 const char text[8];
342};
343
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100344const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100345 ['C' - 'A'] = {
346 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
347 },
348 ['D' - 'A'] = {
349 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
350 },
351 ['G' - 'A'] = {
352 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
353 },
354 ['H' - 'A'] = {
355 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
356 },
357 ['P' - 'A'] = {
358 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
359 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
360 },
361 ['T' - 'A'] = {
362 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
363 },
364 /* rest is empty like this :
365 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
366 */
367};
368
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100369const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
370 [HTTP_METH_NONE] = { "", 0 },
371 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
372 [HTTP_METH_GET] = { "GET", 3 },
373 [HTTP_METH_HEAD] = { "HEAD", 4 },
374 [HTTP_METH_POST] = { "POST", 4 },
375 [HTTP_METH_PUT] = { "PUT", 3 },
376 [HTTP_METH_DELETE] = { "DELETE", 6 },
377 [HTTP_METH_TRACE] = { "TRACE", 5 },
378 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
379};
380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100381/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200382 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100383 * RFC2616 for those chars.
384 */
385
386const char http_is_spht[256] = {
387 [' '] = 1, ['\t'] = 1,
388};
389
390const char http_is_crlf[256] = {
391 ['\r'] = 1, ['\n'] = 1,
392};
393
394const char http_is_lws[256] = {
395 [' '] = 1, ['\t'] = 1,
396 ['\r'] = 1, ['\n'] = 1,
397};
398
399const char http_is_sep[256] = {
400 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
401 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
402 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
403 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
404 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
405};
406
407const char http_is_ctl[256] = {
408 [0 ... 31] = 1,
409 [127] = 1,
410};
411
412/*
413 * A token is any ASCII char that is neither a separator nor a CTL char.
414 * Do not overwrite values in assignment since gcc-2.95 will not handle
415 * them correctly. Instead, define every non-CTL char's status.
416 */
417const char http_is_token[256] = {
418 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
419 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
420 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
421 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
422 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
423 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
424 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
425 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
426 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
427 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
428 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
429 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
430 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
431 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
432 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
433 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
434 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
435 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
436 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
437 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
438 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
439 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
440 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
441 ['|'] = 1, ['}'] = 0, ['~'] = 1,
442};
443
444
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100445/*
446 * An http ver_token is any ASCII which can be found in an HTTP version,
447 * which includes 'H', 'T', 'P', '/', '.' and any digit.
448 */
449const char http_is_ver_token[256] = {
450 ['.'] = 1, ['/'] = 1,
451 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
452 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100453 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100454};
455
456
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100457/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100458 * Adds a header and its CRLF at the tail of the message's buffer, just before
459 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100460 * The header is also automatically added to the index <hdr_idx>, and the end
461 * of headers is automatically adjusted. The number of bytes added is returned
462 * on success, otherwise <0 is returned indicating an error.
463 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100464int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100465{
466 int bytes, len;
467
468 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200469 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100470 if (!bytes)
471 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100472 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100473 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
474}
475
476/*
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. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100479 * the buffer is only opened and the space reserved, but nothing is copied.
480 * The header is also automatically added to the index <hdr_idx>, and the end
481 * of headers is automatically adjusted. The number of bytes added is returned
482 * on success, otherwise <0 is returned indicating an error.
483 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100484int http_header_add_tail2(struct http_msg *msg,
485 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100486{
487 int bytes;
488
Willy Tarreau9b28e032012-10-12 23:49:43 +0200489 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100490 if (!bytes)
491 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100492 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100493 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
494}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495
496/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100497 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
498 * If so, returns the position of the first non-space character relative to
499 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
500 * to return a pointer to the place after the first space. Returns 0 if the
501 * header name does not match. Checks are case-insensitive.
502 */
503int http_header_match2(const char *hdr, const char *end,
504 const char *name, int len)
505{
506 const char *val;
507
508 if (hdr + len >= end)
509 return 0;
510 if (hdr[len] != ':')
511 return 0;
512 if (strncasecmp(hdr, name, len) != 0)
513 return 0;
514 val = hdr + len + 1;
515 while (val < end && HTTP_IS_SPHT(*val))
516 val++;
517 if ((val >= end) && (len + 2 <= end - hdr))
518 return len + 2; /* we may replace starting from second space */
519 return val - hdr;
520}
521
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200522/* Find the first or next occurrence of header <name> in message buffer <sol>
523 * using headers index <idx>, and return it in the <ctx> structure. This
524 * structure holds everything necessary to use the header and find next
525 * occurrence. If its <idx> member is 0, the header is searched from the
526 * beginning. Otherwise, the next occurrence is returned. The function returns
527 * 1 when it finds a value, and 0 when there is no more. It is very similar to
528 * http_find_header2() except that it is designed to work with full-line headers
529 * whose comma is not a delimiter but is part of the syntax. As a special case,
530 * if ctx->val is NULL when searching for a new values of a header, the current
531 * header is rescanned. This allows rescanning after a header deletion.
532 */
533int http_find_full_header2(const char *name, int len,
534 char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 char *eol, *sov;
538 int cur_idx, old_idx;
539
540 cur_idx = ctx->idx;
541 if (cur_idx) {
542 /* We have previously returned a header, let's search another one */
543 sol = ctx->line;
544 eol = sol + idx->v[cur_idx].len;
545 goto next_hdr;
546 }
547
548 /* first request for this header */
549 sol += hdr_idx_first_pos(idx);
550 old_idx = 0;
551 cur_idx = hdr_idx_first_idx(idx);
552 while (cur_idx) {
553 eol = sol + idx->v[cur_idx].len;
554
555 if (len == 0) {
556 /* No argument was passed, we want any header.
557 * To achieve this, we simply build a fake request. */
558 while (sol + len < eol && sol[len] != ':')
559 len++;
560 name = sol;
561 }
562
563 if ((len < eol - sol) &&
564 (sol[len] == ':') &&
565 (strncasecmp(sol, name, len) == 0)) {
566 ctx->del = len;
567 sov = sol + len + 1;
568 while (sov < eol && http_is_lws[(unsigned char)*sov])
569 sov++;
570
571 ctx->line = sol;
572 ctx->prev = old_idx;
573 ctx->idx = cur_idx;
574 ctx->val = sov - sol;
575 ctx->tws = 0;
576 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
577 eol--;
578 ctx->tws++;
579 }
580 ctx->vlen = eol - sov;
581 return 1;
582 }
583 next_hdr:
584 sol = eol + idx->v[cur_idx].cr + 1;
585 old_idx = cur_idx;
586 cur_idx = idx->v[cur_idx].next;
587 }
588 return 0;
589}
590
Willy Tarreauc90dc232015-02-20 13:51:36 +0100591/* Find the first or next header field in message buffer <sol> using headers
592 * index <idx>, and return it in the <ctx> structure. This structure holds
593 * everything necessary to use the header and find next occurrence. If its
594 * <idx> member is 0, the first header is retrieved. Otherwise, the next
595 * occurrence is returned. The function returns 1 when it finds a value, and
596 * 0 when there is no more. It is equivalent to http_find_full_header2() with
597 * no header name.
598 */
599int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
600{
601 char *eol, *sov;
602 int cur_idx, old_idx;
603 int len;
604
605 cur_idx = ctx->idx;
606 if (cur_idx) {
607 /* We have previously returned a header, let's search another one */
608 sol = ctx->line;
609 eol = sol + idx->v[cur_idx].len;
610 goto next_hdr;
611 }
612
613 /* first request for this header */
614 sol += hdr_idx_first_pos(idx);
615 old_idx = 0;
616 cur_idx = hdr_idx_first_idx(idx);
617 while (cur_idx) {
618 eol = sol + idx->v[cur_idx].len;
619
620 len = 0;
621 while (1) {
622 if (len >= eol - sol)
623 goto next_hdr;
624 if (sol[len] == ':')
625 break;
626 len++;
627 }
628
629 ctx->del = len;
630 sov = sol + len + 1;
631 while (sov < eol && http_is_lws[(unsigned char)*sov])
632 sov++;
633
634 ctx->line = sol;
635 ctx->prev = old_idx;
636 ctx->idx = cur_idx;
637 ctx->val = sov - sol;
638 ctx->tws = 0;
639
640 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
641 eol--;
642 ctx->tws++;
643 }
644 ctx->vlen = eol - sov;
645 return 1;
646
647 next_hdr:
648 sol = eol + idx->v[cur_idx].cr + 1;
649 old_idx = cur_idx;
650 cur_idx = idx->v[cur_idx].next;
651 }
652 return 0;
653}
654
Willy Tarreau68085d82010-01-18 14:54:04 +0100655/* Find the end of the header value contained between <s> and <e>. See RFC2616,
656 * par 2.2 for more information. Note that it requires a valid header to return
657 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200658 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100659char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200660{
661 int quoted, qdpair;
662
663 quoted = qdpair = 0;
664 for (; s < e; s++) {
665 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200666 else if (quoted) {
667 if (*s == '\\') qdpair = 1;
668 else if (*s == '"') quoted = 0;
669 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200670 else if (*s == '"') quoted = 1;
671 else if (*s == ',') return s;
672 }
673 return s;
674}
675
676/* Find the first or next occurrence of header <name> in message buffer <sol>
677 * using headers index <idx>, and return it in the <ctx> structure. This
678 * structure holds everything necessary to use the header and find next
679 * occurrence. If its <idx> member is 0, the header is searched from the
680 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100681 * 1 when it finds a value, and 0 when there is no more. It is designed to work
682 * with headers defined as comma-separated lists. As a special case, if ctx->val
683 * is NULL when searching for a new values of a header, the current header is
684 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200685 */
686int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100687 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200688 struct hdr_ctx *ctx)
689{
Willy Tarreau68085d82010-01-18 14:54:04 +0100690 char *eol, *sov;
691 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200692
Willy Tarreau68085d82010-01-18 14:54:04 +0100693 cur_idx = ctx->idx;
694 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200695 /* We have previously returned a value, let's search
696 * another one on the same line.
697 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200698 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200699 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100700 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200701 eol = sol + idx->v[cur_idx].len;
702
703 if (sov >= eol)
704 /* no more values in this header */
705 goto next_hdr;
706
Willy Tarreau68085d82010-01-18 14:54:04 +0100707 /* values remaining for this header, skip the comma but save it
708 * for later use (eg: for header deletion).
709 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200710 sov++;
711 while (sov < eol && http_is_lws[(unsigned char)*sov])
712 sov++;
713
714 goto return_hdr;
715 }
716
717 /* first request for this header */
718 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100719 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200720 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200721 while (cur_idx) {
722 eol = sol + idx->v[cur_idx].len;
723
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200724 if (len == 0) {
725 /* No argument was passed, we want any header.
726 * To achieve this, we simply build a fake request. */
727 while (sol + len < eol && sol[len] != ':')
728 len++;
729 name = sol;
730 }
731
Willy Tarreau33a7e692007-06-10 19:45:56 +0200732 if ((len < eol - sol) &&
733 (sol[len] == ':') &&
734 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100735 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200736 sov = sol + len + 1;
737 while (sov < eol && http_is_lws[(unsigned char)*sov])
738 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100739
Willy Tarreau33a7e692007-06-10 19:45:56 +0200740 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100741 ctx->prev = old_idx;
742 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200743 ctx->idx = cur_idx;
744 ctx->val = sov - sol;
745
746 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200748 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200749 eol--;
750 ctx->tws++;
751 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200752 ctx->vlen = eol - sov;
753 return 1;
754 }
755 next_hdr:
756 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100757 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200758 cur_idx = idx->v[cur_idx].next;
759 }
760 return 0;
761}
762
763int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100764 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200765 struct hdr_ctx *ctx)
766{
767 return http_find_header2(name, strlen(name), sol, idx, ctx);
768}
769
Willy Tarreau68085d82010-01-18 14:54:04 +0100770/* Remove one value of a header. This only works on a <ctx> returned by one of
771 * the http_find_header functions. The value is removed, as well as surrounding
772 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100773 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100774 * message <msg>. The new index is returned. If it is zero, it means there is
775 * no more header, so any processing may stop. The ctx is always left in a form
776 * that can be handled by http_find_header2() to find next occurrence.
777 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100778int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100779{
780 int cur_idx = ctx->idx;
781 char *sol = ctx->line;
782 struct hdr_idx_elem *hdr;
783 int delta, skip_comma;
784
785 if (!cur_idx)
786 return 0;
787
788 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200789 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100790 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200791 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100792 http_msg_move_end(msg, delta);
793 idx->used--;
794 hdr->len = 0; /* unused entry */
795 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100796 if (idx->tail == ctx->idx)
797 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100798 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100799 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100800 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200801 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100802 return ctx->idx;
803 }
804
805 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200806 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
807 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100808 */
809
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200810 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200811 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200812 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100813 NULL, 0);
814 hdr->len += delta;
815 http_msg_move_end(msg, delta);
816 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200817 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100818 return ctx->idx;
819}
820
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821/* This function handles a server error at the stream interface level. The
822 * stream interface is assumed to be already in a closed state. An optional
823 * message is copied into the input buffer, and an HTTP status code stored.
824 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100825 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200827static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100828 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100830 channel_auto_read(si_oc(si));
831 channel_abort(si_oc(si));
832 channel_auto_close(si_oc(si));
833 channel_erase(si_oc(si));
834 channel_auto_close(si_ic(si));
835 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100836 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200837 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100838 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200840 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200841 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200842 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200843 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844}
845
Willy Tarreau87b09662015-04-03 00:22:06 +0200846/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100847 * and message.
848 */
849
Willy Tarreau87b09662015-04-03 00:22:06 +0200850struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100851{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200852 if (s->be->errmsg[msgnum].str)
853 return &s->be->errmsg[msgnum];
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200854 else if (strm_sess(s)->fe->errmsg[msgnum].str)
855 return &strm_sess(s)->fe->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100856 else
857 return &http_err_chunks[msgnum];
858}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200859
Willy Tarreau53b6c742006-12-17 13:37:46 +0100860/*
861 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
862 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
863 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100864enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100865{
866 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100867 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100868
869 m = ((unsigned)*str - 'A');
870
871 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100872 for (h = http_methods[m]; h->len > 0; h++) {
873 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100874 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100875 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100876 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100877 };
878 return HTTP_METH_OTHER;
879 }
880 return HTTP_METH_NONE;
881
882}
883
Willy Tarreau21d2af32008-02-14 20:25:24 +0100884/* Parse the URI from the given transaction (which is assumed to be in request
885 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
886 * It is returned otherwise.
887 */
888static char *
889http_get_path(struct http_txn *txn)
890{
891 char *ptr, *end;
892
Willy Tarreau9b28e032012-10-12 23:49:43 +0200893 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100894 end = ptr + txn->req.sl.rq.u_l;
895
896 if (ptr >= end)
897 return NULL;
898
899 /* RFC2616, par. 5.1.2 :
900 * Request-URI = "*" | absuri | abspath | authority
901 */
902
903 if (*ptr == '*')
904 return NULL;
905
906 if (isalpha((unsigned char)*ptr)) {
907 /* this is a scheme as described by RFC3986, par. 3.1 */
908 ptr++;
909 while (ptr < end &&
910 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
911 ptr++;
912 /* skip '://' */
913 if (ptr == end || *ptr++ != ':')
914 return NULL;
915 if (ptr == end || *ptr++ != '/')
916 return NULL;
917 if (ptr == end || *ptr++ != '/')
918 return NULL;
919 }
920 /* skip [user[:passwd]@]host[:[port]] */
921
922 while (ptr < end && *ptr != '/')
923 ptr++;
924
925 if (ptr == end)
926 return NULL;
927
928 /* OK, we got the '/' ! */
929 return ptr;
930}
931
William Lallemand65ad6e12014-01-31 15:08:02 +0100932/* Parse the URI from the given string and look for the "/" beginning the PATH.
933 * If not found, return NULL. It is returned otherwise.
934 */
935static char *
936http_get_path_from_string(char *str)
937{
938 char *ptr = str;
939
940 /* RFC2616, par. 5.1.2 :
941 * Request-URI = "*" | absuri | abspath | authority
942 */
943
944 if (*ptr == '*')
945 return NULL;
946
947 if (isalpha((unsigned char)*ptr)) {
948 /* this is a scheme as described by RFC3986, par. 3.1 */
949 ptr++;
950 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
951 ptr++;
952 /* skip '://' */
953 if (*ptr == '\0' || *ptr++ != ':')
954 return NULL;
955 if (*ptr == '\0' || *ptr++ != '/')
956 return NULL;
957 if (*ptr == '\0' || *ptr++ != '/')
958 return NULL;
959 }
960 /* skip [user[:passwd]@]host[:[port]] */
961
962 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
963 ptr++;
964
965 if (*ptr == '\0' || *ptr == ' ')
966 return NULL;
967
968 /* OK, we got the '/' ! */
969 return ptr;
970}
971
Willy Tarreau71241ab2012-12-27 11:30:54 +0100972/* Returns a 302 for a redirectable request that reaches a server working in
973 * in redirect mode. This may only be called just after the stream interface
974 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
975 * follow normal proxy processing. NOTE: this function is designed to support
976 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100977 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200978void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979{
980 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100981 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100982 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200983 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100984
985 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100986 trash.len = strlen(HTTP_302);
987 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100989 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100990
Willy Tarreauefb453c2008-10-26 20:49:47 +0100991 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100992 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100993 return;
994
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100995 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100996 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100997 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
998 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100999 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001001 /* 3: add the request URI. Since it was already forwarded, we need
1002 * to temporarily rewind the buffer.
1003 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001004 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001005 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001006
Willy Tarreauefb453c2008-10-26 20:49:47 +01001007 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001008 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 +02001009
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001010 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001011
Willy Tarreauefb453c2008-10-26 20:49:47 +01001012 if (!path)
1013 return;
1014
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001015 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001016 return;
1017
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001018 memcpy(trash.str + trash.len, path, len);
1019 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001020
1021 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001022 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1023 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001024 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001025 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1026 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001027 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001028
1029 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001030 si_shutr(si);
1031 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001033 si->state = SI_ST_CLO;
1034
1035 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001036 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001037
1038 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001039 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001040 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001041}
1042
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001043/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001044 * that the server side is closed. Note that err_type is actually a
1045 * bitmask, where almost only aborts may be cumulated with other
1046 * values. We consider that aborted operations are more important
1047 * than timeouts or errors due to the fact that nobody else in the
1048 * logs might explain incomplete retries. All others should avoid
1049 * being cumulated. It should normally not be possible to have multiple
1050 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001051 * Note that connection errors appearing on the second request of a keep-alive
1052 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001053 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001054void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001055{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001056 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001057
1058 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001059 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001060 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001061 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001062 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001063 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001064 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001065 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001066 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001067 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001068 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001069 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001070 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001071 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001072 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001073 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001074 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001075 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001076 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1077 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001078 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001079 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001080 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001081 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001082 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001084 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001085 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086}
1087
Willy Tarreau42250582007-04-01 01:30:43 +02001088extern const char sess_term_cond[8];
1089extern const char sess_fin_state[8];
1090extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001091struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001092struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001093struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001094struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095
Willy Tarreau117f59e2007-03-04 18:17:17 +01001096/*
1097 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001098 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001099 */
1100void capture_headers(char *som, struct hdr_idx *idx,
1101 char **cap, struct cap_hdr *cap_hdr)
1102{
1103 char *eol, *sol, *col, *sov;
1104 int cur_idx;
1105 struct cap_hdr *h;
1106 int len;
1107
1108 sol = som + hdr_idx_first_pos(idx);
1109 cur_idx = hdr_idx_first_idx(idx);
1110
1111 while (cur_idx) {
1112 eol = sol + idx->v[cur_idx].len;
1113
1114 col = sol;
1115 while (col < eol && *col != ':')
1116 col++;
1117
1118 sov = col + 1;
1119 while (sov < eol && http_is_lws[(unsigned char)*sov])
1120 sov++;
1121
1122 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001123 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001124 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1125 if (cap[h->index] == NULL)
1126 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001127 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001128
1129 if (cap[h->index] == NULL) {
1130 Alert("HTTP capture : out of memory.\n");
1131 continue;
1132 }
1133
1134 len = eol - sov;
1135 if (len > h->len)
1136 len = h->len;
1137
1138 memcpy(cap[h->index], sov, len);
1139 cap[h->index][len]=0;
1140 }
1141 }
1142 sol = eol + idx->v[cur_idx].cr + 1;
1143 cur_idx = idx->v[cur_idx].next;
1144 }
1145}
1146
1147
Willy Tarreau42250582007-04-01 01:30:43 +02001148/* either we find an LF at <ptr> or we jump to <bad>.
1149 */
1150#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1151
1152/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1153 * otherwise to <http_msg_ood> with <state> set to <st>.
1154 */
1155#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1156 ptr++; \
1157 if (likely(ptr < end)) \
1158 goto good; \
1159 else { \
1160 state = (st); \
1161 goto http_msg_ood; \
1162 } \
1163 } while (0)
1164
1165
Willy Tarreaubaaee002006-06-26 02:48:02 +02001166/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001167 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1169 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1170 * will give undefined results.
1171 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1172 * and that msg->sol points to the beginning of the response.
1173 * If a complete line is found (which implies that at least one CR or LF is
1174 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1175 * returned indicating an incomplete line (which does not mean that parts have
1176 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1177 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1178 * upon next call.
1179 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001180 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1182 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001183 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001184 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001185const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001186 enum ht_state state, const char *ptr, const char *end,
1187 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001188{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001189 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001190
Willy Tarreau8973c702007-01-21 23:58:29 +01001191 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001193 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001194 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001195 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1196
1197 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001198 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001199 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1200 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001201 state = HTTP_MSG_ERROR;
1202 break;
1203
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001205 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001207 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001208 goto http_msg_rpcode;
1209 }
1210 if (likely(HTTP_IS_SPHT(*ptr)))
1211 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1212 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001213 state = HTTP_MSG_ERROR;
1214 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001215
Willy Tarreau8973c702007-01-21 23:58:29 +01001216 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001217 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001218 if (likely(!HTTP_IS_LWS(*ptr)))
1219 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1220
1221 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001222 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001223 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1224 }
1225
1226 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001227 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001228 http_msg_rsp_reason:
1229 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001230 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001231 msg->sl.st.r_l = 0;
1232 goto http_msg_rpline_eol;
1233
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001235 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001237 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001238 goto http_msg_rpreason;
1239 }
1240 if (likely(HTTP_IS_SPHT(*ptr)))
1241 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1242 /* so it's a CR/LF, so there is no reason phrase */
1243 goto http_msg_rsp_reason;
1244
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001246 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001247 if (likely(!HTTP_IS_CRLF(*ptr)))
1248 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001250 http_msg_rpline_eol:
1251 /* We have seen the end of line. Note that we do not
1252 * necessarily have the \n yet, but at least we know that we
1253 * have EITHER \r OR \n, otherwise the response would not be
1254 * complete. We can then record the response length and return
1255 * to the caller which will be able to register it.
1256 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001257 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001258 return ptr;
1259
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001261#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001262 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1263 exit(1);
1264#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001265 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001266 }
1267
1268 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001269 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001270 if (ret_state)
1271 *ret_state = state;
1272 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001273 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001275}
1276
Willy Tarreau8973c702007-01-21 23:58:29 +01001277/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 * This function parses a request line between <ptr> and <end>, starting with
1279 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1280 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1281 * will give undefined results.
1282 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1283 * and that msg->sol points to the beginning of the request.
1284 * If a complete line is found (which implies that at least one CR or LF is
1285 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1286 * returned indicating an incomplete line (which does not mean that parts have
1287 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1288 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1289 * upon next call.
1290 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001291 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1293 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001294 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001295 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001296const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001297 enum ht_state state, const char *ptr, const char *end,
1298 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001299{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001300 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001304 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 if (likely(HTTP_IS_TOKEN(*ptr)))
1306 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001307
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001309 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1311 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 if (likely(HTTP_IS_CRLF(*ptr))) {
1314 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001315 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001317 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001319 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001321 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 msg->sl.rq.v_l = 0;
1323 goto http_msg_rqline_eol;
1324 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001325 state = HTTP_MSG_ERROR;
1326 break;
1327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001329 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001331 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 goto http_msg_rquri;
1333 }
1334 if (likely(HTTP_IS_SPHT(*ptr)))
1335 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1336 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1337 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001340 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001341 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001343
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001345 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1347 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001348
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001349 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001350 /* non-ASCII chars are forbidden unless option
1351 * accept-invalid-http-request is enabled in the frontend.
1352 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001353 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001354 if (msg->err_pos < -1)
1355 goto invalid_char;
1356 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001357 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001358 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1359 }
1360
1361 if (likely(HTTP_IS_CRLF(*ptr))) {
1362 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1363 goto http_msg_req09_uri_e;
1364 }
1365
1366 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001367 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001368 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001369 state = HTTP_MSG_ERROR;
1370 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001371
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001373 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001375 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 goto http_msg_rqver;
1377 }
1378 if (likely(HTTP_IS_SPHT(*ptr)))
1379 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1380 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1381 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001382
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001384 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001385 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001387
1388 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001389 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001390 http_msg_rqline_eol:
1391 /* We have seen the end of line. Note that we do not
1392 * necessarily have the \n yet, but at least we know that we
1393 * have EITHER \r OR \n, otherwise the request would not be
1394 * complete. We can then record the request length and return
1395 * to the caller which will be able to register it.
1396 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001397 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001398 return ptr;
1399 }
1400
1401 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001402 state = HTTP_MSG_ERROR;
1403 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001404
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001406#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1408 exit(1);
1409#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001410 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001414 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 if (ret_state)
1416 *ret_state = state;
1417 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001418 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001421
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001422/*
1423 * Returns the data from Authorization header. Function may be called more
1424 * than once so data is stored in txn->auth_data. When no header is found
1425 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001426 * searching again for something we are unable to find anyway. However, if
1427 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001428 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001429 */
1430
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001431/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1432 * set according to global.tune.bufsize.
1433 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001434char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001435
1436int
Willy Tarreau87b09662015-04-03 00:22:06 +02001437get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001438{
1439
Willy Tarreaueee5b512015-04-03 23:46:31 +02001440 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001441 struct chunk auth_method;
1442 struct hdr_ctx ctx;
1443 char *h, *p;
1444 int len;
1445
1446#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001447 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001448#endif
1449
1450 if (txn->auth.method == HTTP_AUTH_WRONG)
1451 return 0;
1452
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001453 txn->auth.method = HTTP_AUTH_WRONG;
1454
1455 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001456
1457 if (txn->flags & TX_USE_PX_CONN) {
1458 h = "Proxy-Authorization";
1459 len = strlen(h);
1460 } else {
1461 h = "Authorization";
1462 len = strlen(h);
1463 }
1464
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001465 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001466 return 0;
1467
1468 h = ctx.line + ctx.val;
1469
1470 p = memchr(h, ' ', ctx.vlen);
1471 if (!p || p == h)
1472 return 0;
1473
1474 chunk_initlen(&auth_method, h, 0, p-h);
1475 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1476
1477 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1478
1479 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001480 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001481
1482 if (len < 0)
1483 return 0;
1484
1485
1486 get_http_auth_buff[len] = '\0';
1487
1488 p = strchr(get_http_auth_buff, ':');
1489
1490 if (!p)
1491 return 0;
1492
1493 txn->auth.user = get_http_auth_buff;
1494 *p = '\0';
1495 txn->auth.pass = p+1;
1496
1497 txn->auth.method = HTTP_AUTH_BASIC;
1498 return 1;
1499 }
1500
1501 return 0;
1502}
1503
Willy Tarreau58f10d72006-12-04 02:26:12 +01001504
Willy Tarreau8973c702007-01-21 23:58:29 +01001505/*
1506 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001507 * depending on the initial msg->msg_state. The caller is responsible for
1508 * ensuring that the message does not wrap. The function can be preempted
1509 * everywhere when data are missing and recalled at the exact same location
1510 * with no information loss. The message may even be realigned between two
1511 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001512 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001513 * fields. Note that msg->sol will be initialized after completing the first
1514 * state, so that none of the msg pointers has to be initialized prior to the
1515 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001516 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001517void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518{
Willy Tarreau3770f232013-12-07 00:01:53 +01001519 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001521 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001523 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001524 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001525 ptr = buf->p + msg->next;
1526 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 if (unlikely(ptr >= end))
1529 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001532 /*
1533 * First, states that are specific to the response only.
1534 * We check them first so that request and headers are
1535 * closer to each other (accessed more often).
1536 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001537 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001538 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001539 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001540 /* we have a start of message, but we have to check
1541 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001542 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001543 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 if (unlikely(ptr != buf->p)) {
1545 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001546 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001547 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001549 }
Willy Tarreau26927362012-05-18 23:22:52 +02001550 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001551 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001552 hdr_idx_init(idx);
1553 state = HTTP_MSG_RPVER;
1554 goto http_msg_rpver;
1555 }
1556
1557 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1558 goto http_msg_invalid;
1559
1560 if (unlikely(*ptr == '\n'))
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1562 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1563 /* stop here */
1564
Willy Tarreau8973c702007-01-21 23:58:29 +01001565 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001566 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001567 EXPECT_LF_HERE(ptr, http_msg_invalid);
1568 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1569 /* stop here */
1570
Willy Tarreau8973c702007-01-21 23:58:29 +01001571 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001572 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001573 case HTTP_MSG_RPVER_SP:
1574 case HTTP_MSG_RPCODE:
1575 case HTTP_MSG_RPCODE_SP:
1576 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001577 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001578 state, ptr, end,
1579 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001580 if (unlikely(!ptr))
1581 return;
1582
1583 /* we have a full response and we know that we have either a CR
1584 * or an LF at <ptr>.
1585 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001586 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1587
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001588 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 if (likely(*ptr == '\r'))
1590 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1591 goto http_msg_rpline_end;
1592
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001594 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001595 /* msg->sol must point to the first of CR or LF. */
1596 EXPECT_LF_HERE(ptr, http_msg_invalid);
1597 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1598 /* stop here */
1599
1600 /*
1601 * Second, states that are specific to the request only
1602 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001604 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001606 /* we have a start of message, but we have to check
1607 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001608 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001609 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001610 if (likely(ptr != buf->p)) {
1611 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001612 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001613 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001614 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 }
Willy Tarreau26927362012-05-18 23:22:52 +02001616 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001617 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001618 state = HTTP_MSG_RQMETH;
1619 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001621
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1623 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 if (unlikely(*ptr == '\n'))
1626 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1627 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001628 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001629
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001631 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 EXPECT_LF_HERE(ptr, http_msg_invalid);
1633 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001634 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001637 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 case HTTP_MSG_RQMETH_SP:
1639 case HTTP_MSG_RQURI:
1640 case HTTP_MSG_RQURI_SP:
1641 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001642 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001643 state, ptr, end,
1644 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 if (unlikely(!ptr))
1646 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 /* we have a full request and we know that we have either a CR
1649 * or an LF at <ptr>.
1650 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001652
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001653 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 if (likely(*ptr == '\r'))
1655 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001659 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 /* check for HTTP/0.9 request : no version information available.
1661 * msg->sol must point to the first of CR or LF.
1662 */
1663 if (unlikely(msg->sl.rq.v_l == 0))
1664 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 EXPECT_LF_HERE(ptr, http_msg_invalid);
1667 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001669
Willy Tarreau8973c702007-01-21 23:58:29 +01001670 /*
1671 * Common states below
1672 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001674 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001675 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 if (likely(!HTTP_IS_CRLF(*ptr))) {
1677 goto http_msg_hdr_name;
1678 }
1679
1680 if (likely(*ptr == '\r'))
1681 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1682 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001685 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001686 /* assumes msg->sol points to the first char */
1687 if (likely(HTTP_IS_TOKEN(*ptr)))
1688 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001689
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001690 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001693 if (likely(msg->err_pos < -1) || *ptr == '\n')
1694 goto http_msg_invalid;
1695
1696 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001697 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001698
1699 /* and we still accept this non-token character */
1700 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001703 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001704 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 if (likely(HTTP_IS_SPHT(*ptr)))
1706 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001707
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001709 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001710
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 if (likely(!HTTP_IS_CRLF(*ptr))) {
1712 goto http_msg_hdr_val;
1713 }
1714
1715 if (likely(*ptr == '\r'))
1716 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1717 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001720 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 EXPECT_LF_HERE(ptr, http_msg_invalid);
1722 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001723
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001725 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 if (likely(HTTP_IS_SPHT(*ptr))) {
1727 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001728 for (; buf->p + msg->sov < ptr; msg->sov++)
1729 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 goto http_msg_hdr_l1_sp;
1731 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001732 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001733 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 goto http_msg_complete_header;
1735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001737 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001738 /* assumes msg->sol points to the first char, and msg->sov
1739 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 */
1741 if (likely(!HTTP_IS_CRLF(*ptr)))
1742 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001743
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001744 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 /* Note: we could also copy eol into ->eoh so that we have the
1746 * real header end in case it ends with lots of LWS, but is this
1747 * really needed ?
1748 */
1749 if (likely(*ptr == '\r'))
1750 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1751 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001754 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 EXPECT_LF_HERE(ptr, http_msg_invalid);
1756 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001759 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1761 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001762 for (; buf->p + msg->eol < ptr; msg->eol++)
1763 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 goto http_msg_hdr_val;
1765 }
1766 http_msg_complete_header:
1767 /*
1768 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001769 * Assumes msg->sol points to the first char, msg->sov points
1770 * to the first character of the value and msg->eol to the
1771 * first CR or LF so we know how the line ends. We insert last
1772 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001774 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 idx, idx->tail) < 0))
1776 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001777
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001778 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779 if (likely(!HTTP_IS_CRLF(*ptr))) {
1780 goto http_msg_hdr_name;
1781 }
1782
1783 if (likely(*ptr == '\r'))
1784 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1785 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001786
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001788 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001789 /* Assumes msg->sol points to the first of either CR or LF.
1790 * Sets ->sov and ->next to the total header length, ->eoh to
1791 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1792 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 EXPECT_LF_HERE(ptr, http_msg_invalid);
1794 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001795 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001796 msg->eoh = msg->sol;
1797 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001798 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001799 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001800 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001801
1802 case HTTP_MSG_ERROR:
1803 /* this may only happen if we call http_msg_analyser() twice with an error */
1804 break;
1805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001807#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1809 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001810#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001811 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 }
1813 http_msg_ood:
1814 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001815 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001816 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 http_msg_invalid:
1820 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001821 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001822 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 return;
1824}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001825
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001826/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1827 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1828 * nothing is done and 1 is returned.
1829 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001830static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001831{
1832 int delta;
1833 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001834 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001835
1836 if (msg->sl.rq.v_l != 0)
1837 return 1;
1838
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001839 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1840 if (txn->meth != HTTP_METH_GET)
1841 return 0;
1842
Willy Tarreau9b28e032012-10-12 23:49:43 +02001843 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001844 delta = 0;
1845
1846 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001847 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1848 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001849 }
1850 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001851 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001852 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001854 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001855 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001856 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001857 NULL, NULL);
1858 if (unlikely(!cur_end))
1859 return 0;
1860
1861 /* we have a full HTTP/1.0 request now and we know that
1862 * we have either a CR or an LF at <ptr>.
1863 */
1864 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1865 return 1;
1866}
1867
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001868/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001869 * and "keep-alive" values. If we already know that some headers may safely
1870 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001871 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1872 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001873 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1875 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1876 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001877 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001878 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001879void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001880{
Willy Tarreau5b154472009-12-21 20:11:07 +01001881 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001882 const char *hdr_val = "Connection";
1883 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001884
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001885 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 return;
1887
Willy Tarreau88d349d2010-01-25 12:15:43 +01001888 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1889 hdr_val = "Proxy-Connection";
1890 hdr_len = 16;
1891 }
1892
Willy Tarreau5b154472009-12-21 20:11:07 +01001893 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001894 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001895 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001896 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1897 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001898 if (to_del & 2)
1899 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001900 else
1901 txn->flags |= TX_CON_KAL_SET;
1902 }
1903 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1904 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001905 if (to_del & 1)
1906 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001907 else
1908 txn->flags |= TX_CON_CLO_SET;
1909 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001910 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1911 txn->flags |= TX_HDR_CONN_UPG;
1912 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001913 }
1914
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001915 txn->flags |= TX_HDR_CONN_PRS;
1916 return;
1917}
Willy Tarreau5b154472009-12-21 20:11:07 +01001918
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001919/* Apply desired changes on the Connection: header. Values may be removed and/or
1920 * added depending on the <wanted> flags, which are exclusively composed of
1921 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1922 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1923 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001924void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001925{
1926 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001927 const char *hdr_val = "Connection";
1928 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001929
1930 ctx.idx = 0;
1931
Willy Tarreau88d349d2010-01-25 12:15:43 +01001932
1933 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1934 hdr_val = "Proxy-Connection";
1935 hdr_len = 16;
1936 }
1937
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001938 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001939 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001940 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1941 if (wanted & TX_CON_KAL_SET)
1942 txn->flags |= TX_CON_KAL_SET;
1943 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001944 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001945 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001946 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1947 if (wanted & TX_CON_CLO_SET)
1948 txn->flags |= TX_CON_CLO_SET;
1949 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001950 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001951 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001952 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001953
1954 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1955 return;
1956
1957 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1958 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001959 hdr_val = "Connection: close";
1960 hdr_len = 17;
1961 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1962 hdr_val = "Proxy-Connection: close";
1963 hdr_len = 23;
1964 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001965 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001966 }
1967
1968 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1969 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001970 hdr_val = "Connection: keep-alive";
1971 hdr_len = 22;
1972 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1973 hdr_val = "Proxy-Connection: keep-alive";
1974 hdr_len = 28;
1975 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001976 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001977 }
1978 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001979}
1980
Willy Tarreauc24715e2014-04-17 15:21:20 +02001981/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1982 * the first byte of data after the chunk size, so that we know we can forward
1983 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1984 * the chunk size. That way it is always possible to differentiate between the
1985 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001988 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001989static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001990{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001991 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001993 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001994 const char *end = buf->data + buf->size;
1995 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001996 unsigned int chunk = 0;
1997
1998 /* The chunk size is in the following form, though we are only
1999 * interested in the size and CRLF :
2000 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2001 */
2002 while (1) {
2003 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002004 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002006 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002007 if (c < 0) /* not a hex digit anymore */
2008 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002009 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002010 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002011 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002012 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002013 chunk = (chunk << 4) + c;
2014 }
2015
Willy Tarreaud98cf932009-12-27 22:54:55 +01002016 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002017 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002018 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002019
2020 while (http_is_spht[(unsigned char)*ptr]) {
2021 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002022 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002023 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002024 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002025 }
2026
Willy Tarreaud98cf932009-12-27 22:54:55 +01002027 /* Up to there, we know that at least one byte is present at *ptr. Check
2028 * for the end of chunk size.
2029 */
2030 while (1) {
2031 if (likely(HTTP_IS_CRLF(*ptr))) {
2032 /* we now have a CR or an LF at ptr */
2033 if (likely(*ptr == '\r')) {
2034 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002035 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002036 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 return 0;
2038 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002039
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002041 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002043 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002044 /* done */
2045 break;
2046 }
2047 else if (*ptr == ';') {
2048 /* chunk extension, ends at next CRLF */
2049 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002050 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002051 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002052 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002053
2054 while (!HTTP_IS_CRLF(*ptr)) {
2055 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002056 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002057 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 return 0;
2059 }
2060 /* we have a CRLF now, loop above */
2061 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002062 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002063 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002064 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002065 }
2066
Willy Tarreaud98cf932009-12-27 22:54:55 +01002067 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002068 * which may or may not be present. We save that into ->next,
2069 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002070 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002071 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002072 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002073 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002075 msg->chunk_len = chunk;
2076 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002077 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002078 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002079 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002080 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002081 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002082}
2083
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002084/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002085 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 * the trailers is found, it is automatically scheduled to be forwarded,
2087 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2088 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002089 * except maybe msg->next if it could parse some lines, and returns zero.
2090 * If a parse error is encountered, the function returns < 0 and does not
2091 * change anything except maybe msg->next. Note that the message must
2092 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002093 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002094 * forwarding, and that msg->next exactly matches the length of trailers
2095 * already parsed and not forwarded. It is also important to note that this
2096 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002098static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002100 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002101
Willy Tarreaua458b672012-03-05 11:17:50 +01002102 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002104 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002105 const char *ptr = b_ptr(buf, msg->next);
2106 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002107 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002108
2109 /* scan current line and stop at LF or CRLF */
2110 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002111 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002112 return 0;
2113
2114 if (*ptr == '\n') {
2115 if (!p1)
2116 p1 = ptr;
2117 p2 = ptr;
2118 break;
2119 }
2120
2121 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002122 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002123 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002125 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002126 p1 = ptr;
2127 }
2128
2129 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002130 if (ptr >= buf->data + buf->size)
2131 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002132 }
2133
2134 /* after LF; point to beginning of next line */
2135 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002136 if (p2 >= buf->data + buf->size)
2137 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002138
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002140 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002142
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002143 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002144 /* LF/CRLF at beginning of line => end of trailers at p2.
2145 * Everything was scheduled for forwarding, there's nothing
2146 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002147 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002148 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002149 msg->msg_state = HTTP_MSG_DONE;
2150 return 1;
2151 }
2152 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002153 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002154 }
2155}
2156
Willy Tarreauc24715e2014-04-17 15:21:20 +02002157/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2158 * or a possible LF alone at the end of a chunk. It automatically adjusts
2159 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002160 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002161 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2162 * not enough data are available, the function does not change anything and
2163 * returns zero. If a parse error is encountered, the function returns < 0 and
2164 * does not change anything. Note: this function is designed to parse wrapped
2165 * CRLF at the end of the buffer.
2166 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002167static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002168{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002169 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002170 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002171 int bytes;
2172
2173 /* NB: we'll check data availabilty at the end. It's not a
2174 * problem because whatever we match first will be checked
2175 * against the correct length.
2176 */
2177 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002178 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002179 if (*ptr == '\r') {
2180 bytes++;
2181 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002182 if (ptr >= buf->data + buf->size)
2183 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002184 }
2185
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002186 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002187 return 0;
2188
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002189 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002190 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002191 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002192 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002193
2194 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002195 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002196 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002197 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002198 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002199 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2200 return 1;
2201}
Willy Tarreau5b154472009-12-21 20:11:07 +01002202
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002203/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2204 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2205 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2206 * Unparsable qvalues return 1000 as "q=1.000".
2207 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002208int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002209{
2210 int q = 1000;
2211
Willy Tarreau506c69a2014-07-08 00:59:48 +02002212 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002213 goto out;
2214 q = (*qvalue++ - '0') * 1000;
2215
2216 if (*qvalue++ != '.')
2217 goto out;
2218
Willy Tarreau506c69a2014-07-08 00:59:48 +02002219 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002220 goto out;
2221 q += (*qvalue++ - '0') * 100;
2222
Willy Tarreau506c69a2014-07-08 00:59:48 +02002223 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002224 goto out;
2225 q += (*qvalue++ - '0') * 10;
2226
Willy Tarreau506c69a2014-07-08 00:59:48 +02002227 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002228 goto out;
2229 q += (*qvalue++ - '0') * 1;
2230 out:
2231 if (q > 1000)
2232 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002233 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002234 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002235 return q;
2236}
William Lallemand82fe75c2012-10-23 10:25:10 +02002237
2238/*
2239 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002240 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002241int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002242{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002243 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002244 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002245 struct hdr_ctx ctx;
2246 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002247 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002248
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002249 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2250 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002251 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2252 */
2253 ctx.idx = 0;
2254 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2255 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002256 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2257 (ctx.vlen < 31 ||
2258 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2259 ctx.line[ctx.val + 30] < '6' ||
2260 (ctx.line[ctx.val + 30] == '6' &&
2261 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2262 s->comp_algo = NULL;
2263 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002264 }
2265
William Lallemand82fe75c2012-10-23 10:25:10 +02002266 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002267 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_sess(s)->fe->comp && (comp_algo_back = strm_sess(s)->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002268 int best_q = 0;
2269
William Lallemand82fe75c2012-10-23 10:25:10 +02002270 ctx.idx = 0;
2271 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002272 const char *qval;
2273 int q;
2274 int toklen;
2275
2276 /* try to isolate the token from the optional q-value */
2277 toklen = 0;
2278 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2279 toklen++;
2280
2281 qval = ctx.line + ctx.val + toklen;
2282 while (1) {
2283 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2284 qval++;
2285
2286 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2287 qval = NULL;
2288 break;
2289 }
2290 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002291
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002292 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2293 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002294
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002295 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2296 qval = NULL;
2297 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002298 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002299 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2300 break;
2301
2302 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2303 qval++;
2304 }
2305
2306 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002307 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002308
2309 if (q <= best_q)
2310 continue;
2311
2312 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2313 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002314 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002315 s->comp_algo = comp_algo;
2316 best_q = q;
2317 break;
2318 }
2319 }
2320 }
2321 }
2322
2323 /* remove all occurrences of the header when "compression offload" is set */
2324 if (s->comp_algo) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002325 if ((s->be->comp && s->be->comp->offload) || (strm_sess(s)->fe->comp && strm_sess(s)->fe->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002326 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2327 ctx.idx = 0;
2328 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2329 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002330 }
2331 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002332 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002333 }
2334
2335 /* identity is implicit does not require headers */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002336 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_sess(s)->fe->comp && (comp_algo_back = strm_sess(s)->fe->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002337 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002338 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002339 s->comp_algo = comp_algo;
2340 return 1;
2341 }
2342 }
2343 }
2344
2345 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002346 return 0;
2347}
2348
2349/*
2350 * Selects a comression algorithm depending of the server response.
2351 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002352int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002353{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002354 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002355 struct http_msg *msg = &txn->rsp;
2356 struct hdr_ctx ctx;
2357 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002358
2359 /* no common compression algorithm was found in request header */
2360 if (s->comp_algo == NULL)
2361 goto fail;
2362
2363 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002364 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002365 goto fail;
2366
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002367 /* compress 200,201,202,203 responses only */
2368 if ((txn->status != 200) &&
2369 (txn->status != 201) &&
2370 (txn->status != 202) &&
2371 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002372 goto fail;
2373
William Lallemand82fe75c2012-10-23 10:25:10 +02002374 /* Content-Length is null */
2375 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2376 goto fail;
2377
2378 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002379 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002380 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2381 goto fail;
2382
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002383 /* no compression when Cache-Control: no-transform is present in the message */
2384 ctx.idx = 0;
2385 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2386 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2387 goto fail;
2388 }
2389
William Lallemand82fe75c2012-10-23 10:25:10 +02002390 comp_type = NULL;
2391
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002392 /* we don't want to compress multipart content-types, nor content-types that are
2393 * not listed in the "compression type" directive if any. If no content-type was
2394 * found but configuration requires one, we don't compress either. Backend has
2395 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002396 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002397 ctx.idx = 0;
2398 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2399 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2400 goto fail;
2401
2402 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002403 (strm_sess(s)->fe->comp && (comp_type = strm_sess(s)->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002404 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002405 if (ctx.vlen >= comp_type->name_len &&
2406 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002407 /* this Content-Type should be compressed */
2408 break;
2409 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002410 /* this Content-Type should not be compressed */
2411 if (comp_type == NULL)
2412 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002413 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002414 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002415 else { /* no content-type header */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002416 if ((s->be->comp && s->be->comp->types) || (strm_sess(s)->fe->comp && strm_sess(s)->fe->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002417 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002418 }
2419
William Lallemandd85f9172012-11-09 17:05:39 +01002420 /* limit compression rate */
2421 if (global.comp_rate_lim > 0)
2422 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2423 goto fail;
2424
William Lallemand072a2bf2012-11-20 17:01:01 +01002425 /* limit cpu usage */
2426 if (idle_pct < compress_min_idle)
2427 goto fail;
2428
William Lallemand4c49fae2012-11-07 15:00:23 +01002429 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002430 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002431 goto fail;
2432
Willy Tarreaue7dff022015-04-03 01:14:29 +02002433 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002434
William Lallemand82fe75c2012-10-23 10:25:10 +02002435 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002436 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002437 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2438 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2439
2440 /* add Transfer-Encoding header */
2441 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2442 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2443
2444 /*
2445 * Add Content-Encoding header when it's not identity encoding.
2446 * RFC 2616 : Identity encoding: This content-coding is used only in the
2447 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2448 * header.
2449 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002450 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002451 trash.len = 18;
2452 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002453 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2454 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002455 trash.str[trash.len] = '\0';
2456 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002457 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002458 return 1;
2459
2460fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002461 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002462 return 0;
2463}
2464
Willy Tarreau87b09662015-04-03 00:22:06 +02002465void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002466{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002467 struct proxy *fe = strm_sess(s)->fe;
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002468 int tmp = TX_CON_WANT_KAL;
2469
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002470 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2471 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002472 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2473 tmp = TX_CON_WANT_TUN;
2474
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002475 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002476 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2477 tmp = TX_CON_WANT_TUN;
2478 }
2479
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002480 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002481 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2482 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002483 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002484 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2485 tmp = TX_CON_WANT_CLO;
2486 else
2487 tmp = TX_CON_WANT_SCL;
2488 }
2489
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002490 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002491 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2492 tmp = TX_CON_WANT_CLO;
2493
2494 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2495 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2496
2497 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2498 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2499 /* parse the Connection header and possibly clean it */
2500 int to_del = 0;
2501 if ((msg->flags & HTTP_MSGF_VER_11) ||
2502 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002503 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002504 to_del |= 2; /* remove "keep-alive" */
2505 if (!(msg->flags & HTTP_MSGF_VER_11))
2506 to_del |= 1; /* remove "close" */
2507 http_parse_connection_header(txn, msg, to_del);
2508 }
2509
2510 /* check if client or config asks for explicit close in KAL/SCL */
2511 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2512 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2513 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2514 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2515 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002516 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002517 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2518}
William Lallemand82fe75c2012-10-23 10:25:10 +02002519
Willy Tarreaud787e662009-07-07 10:14:51 +02002520/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2521 * processing can continue on next analysers, or zero if it either needs more
2522 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002523 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002524 * when it has nothing left to do, and may remove any analyser when it wants to
2525 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002526 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002527int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002528{
Willy Tarreau59234e92008-11-30 23:51:27 +01002529 /*
2530 * We will parse the partial (or complete) lines.
2531 * We will check the request syntax, and also join multi-line
2532 * headers. An index of all the lines will be elaborated while
2533 * parsing.
2534 *
2535 * For the parsing, we use a 28 states FSM.
2536 *
2537 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002538 * req->buf->p = beginning of request
2539 * req->buf->p + msg->eoh = end of processed headers / start of current one
2540 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002541 * msg->eol = end of current header or line (LF or CRLF)
2542 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002543 *
2544 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002545 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002546 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2547 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002549
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002551 int use_close_only;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002552 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002553 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002554 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002555 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002556
Willy Tarreau87b09662015-04-03 00:22:06 +02002557 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 +01002558 now_ms, __FUNCTION__,
2559 s,
2560 req,
2561 req->rex, req->wex,
2562 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002563 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002564 req->analysers);
2565
Willy Tarreau52a0c602009-08-16 22:45:38 +02002566 /* we're speaking HTTP here, so let's speak HTTP to the client */
2567 s->srv_error = http_return_srv_error;
2568
Willy Tarreau83e3af02009-12-28 17:39:57 +01002569 /* There's a protected area at the end of the buffer for rewriting
2570 * purposes. We don't want to start to parse the request if the
2571 * protected area is affected, because we may have to move processed
2572 * data later, which is much more complicated.
2573 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002574 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002575 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002576 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002577 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002578 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002579 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002580 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002581 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002582 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002583 return 0;
2584 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002585 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2586 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2587 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002588 }
2589
Willy Tarreau065e8332010-01-08 00:30:20 +01002590 /* Note that we have the same problem with the response ; we
2591 * may want to send a redirect, error or anything which requires
2592 * some spare space. So we'll ensure that we have at least
2593 * maxrewrite bytes available in the response buffer before
2594 * processing that one. This will only affect pipelined
2595 * keep-alive requests.
2596 */
2597 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002598 unlikely(!channel_is_rewritable(&s->res) ||
2599 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2600 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2601 if (s->res.buf->o) {
2602 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002603 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002604 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002605 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002606 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2607 s->res.flags |= CF_WAKE_WRITE;
2608 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002609 return 0;
2610 }
2611 }
2612
Willy Tarreau9b28e032012-10-12 23:49:43 +02002613 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002614 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002615 }
2616
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 /* 1: we might have to print this header in debug mode */
2618 if (unlikely((global.mode & MODE_DEBUG) &&
2619 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002620 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002621 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002622
Willy Tarreau9b28e032012-10-12 23:49:43 +02002623 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002624 /* this is a bit complex : in case of error on the request line,
2625 * we know that rq.l is still zero, so we display only the part
2626 * up to the end of the line (truncated by debug_hdr).
2627 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002628 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002630
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 sol += hdr_idx_first_pos(&txn->hdr_idx);
2632 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002633
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 while (cur_idx) {
2635 eol = sol + txn->hdr_idx.v[cur_idx].len;
2636 debug_hdr("clihdr", s, sol, eol);
2637 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2638 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002639 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002640 }
2641
Willy Tarreau58f10d72006-12-04 02:26:12 +01002642
Willy Tarreau59234e92008-11-30 23:51:27 +01002643 /*
2644 * Now we quickly check if we have found a full valid request.
2645 * If not so, we check the FD and buffer states before leaving.
2646 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002647 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002648 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002649 * on a keep-alive stream, if we encounter and error, close, t/o,
2650 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002651 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002652 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002653 * Last, we may increase some tracked counters' http request errors on
2654 * the cases that are deliberately the client's fault. For instance,
2655 * a timeout or connection reset is not counted as an error. However
2656 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002658
Willy Tarreau655dce92009-11-08 13:10:58 +01002659 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002660 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002661 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002662 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002663 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002664 stream_inc_http_req_ctr(s);
2665 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002666 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002668 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002669
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 /* 1: Since we are in header mode, if there's no space
2671 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002672 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002673 * must terminate it now.
2674 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002675 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 /* FIXME: check if URI is set and return Status
2677 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002678 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002679 stream_inc_http_req_ctr(s);
2680 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002681 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002682 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002683 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 goto return_bad_req;
2685 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002686
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002688 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002689 if (!(s->flags & SF_ERR_MASK))
2690 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002691
Willy Tarreaufcffa692010-01-10 14:21:19 +01002692 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002693 goto failed_keep_alive;
2694
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002696 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002697 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002698 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002699 }
2700
Willy Tarreaudc979f22012-12-04 10:39:01 +01002701 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002702 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002703 msg->msg_state = HTTP_MSG_ERROR;
2704 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002705
Willy Tarreau87b09662015-04-03 00:22:06 +02002706 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002707 proxy_inc_fe_req_ctr(sess->fe);
2708 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002709 if (sess->listener->counters)
2710 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002711
Willy Tarreaue7dff022015-04-03 01:14:29 +02002712 if (!(s->flags & SF_FINST_MASK))
2713 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002714 return 0;
2715 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002716
Willy Tarreau59234e92008-11-30 23:51:27 +01002717 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002718 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002719 if (!(s->flags & SF_ERR_MASK))
2720 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002721
Willy Tarreaufcffa692010-01-10 14:21:19 +01002722 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002723 goto failed_keep_alive;
2724
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002726 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002727 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002728 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002729 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002731 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 msg->msg_state = HTTP_MSG_ERROR;
2733 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002734
Willy Tarreau87b09662015-04-03 00:22:06 +02002735 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002736 proxy_inc_fe_req_ctr(sess->fe);
2737 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002738 if (sess->listener->counters)
2739 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002740
Willy Tarreaue7dff022015-04-03 01:14:29 +02002741 if (!(s->flags & SF_FINST_MASK))
2742 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 return 0;
2744 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002745
Willy Tarreau59234e92008-11-30 23:51:27 +01002746 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002747 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002748 if (!(s->flags & SF_ERR_MASK))
2749 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002750
Willy Tarreaufcffa692010-01-10 14:21:19 +01002751 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002752 goto failed_keep_alive;
2753
Willy Tarreau4076a152009-04-02 15:18:36 +02002754 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002755 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002757 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002758 msg->msg_state = HTTP_MSG_ERROR;
2759 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002760
Willy Tarreau87b09662015-04-03 00:22:06 +02002761 stream_inc_http_err_ctr(s);
2762 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002763 proxy_inc_fe_req_ctr(sess->fe);
2764 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002765 if (sess->listener->counters)
2766 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002767
Willy Tarreaue7dff022015-04-03 01:14:29 +02002768 if (!(s->flags & SF_FINST_MASK))
2769 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002770 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002771 }
2772
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002773 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002774 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002775 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002776#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002777 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2778 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002779 /* We need more data, we have to re-enable quick-ack in case we
2780 * previously disabled it, otherwise we might cause the client
2781 * to delay next data.
2782 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002783 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002784 }
2785#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002786
Willy Tarreaufcffa692010-01-10 14:21:19 +01002787 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2788 /* If the client starts to talk, let's fall back to
2789 * request timeout processing.
2790 */
2791 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002792 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002793 }
2794
Willy Tarreau59234e92008-11-30 23:51:27 +01002795 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002796 if (!tick_isset(req->analyse_exp)) {
2797 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2798 (txn->flags & TX_WAIT_NEXT_RQ) &&
2799 tick_isset(s->be->timeout.httpka))
2800 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2801 else
2802 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2803 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002804
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 /* we're not ready yet */
2806 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002807
2808 failed_keep_alive:
2809 /* Here we process low-level errors for keep-alive requests. In
2810 * short, if the request is not the first one and it experiences
2811 * a timeout, read error or shutdown, we just silently close so
2812 * that the client can try again.
2813 */
2814 txn->status = 0;
2815 msg->msg_state = HTTP_MSG_RQBEFORE;
2816 req->analysers = 0;
2817 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002818 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002819 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002820 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002821 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002823
Willy Tarreaud787e662009-07-07 10:14:51 +02002824 /* OK now we have a complete HTTP request with indexed headers. Let's
2825 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002826 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002827 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002828 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002829 * byte after the last LF. msg->sov points to the first byte of data.
2830 * msg->eol cannot be trusted because it may have been left uninitialized
2831 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002832 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002833
Willy Tarreau87b09662015-04-03 00:22:06 +02002834 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002835 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002836
Willy Tarreaub16a5742010-01-10 14:46:16 +01002837 if (txn->flags & TX_WAIT_NEXT_RQ) {
2838 /* kill the pending keep-alive timeout */
2839 txn->flags &= ~TX_WAIT_NEXT_RQ;
2840 req->analyse_exp = TICK_ETERNITY;
2841 }
2842
2843
Willy Tarreaud787e662009-07-07 10:14:51 +02002844 /* Maybe we found in invalid header name while we were configured not
2845 * to block on that, so we have to capture it now.
2846 */
2847 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002848 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002849
Willy Tarreau59234e92008-11-30 23:51:27 +01002850 /*
2851 * 1: identify the method
2852 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002853 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002854
2855 /* we can make use of server redirect on GET and HEAD */
2856 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002857 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002858
Willy Tarreau59234e92008-11-30 23:51:27 +01002859 /*
2860 * 2: check if the URI matches the monitor_uri.
2861 * We have to do this for every request which gets in, because
2862 * the monitor-uri is defined by the frontend.
2863 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002864 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2865 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002866 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002867 sess->fe->monitor_uri,
2868 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002869 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002870 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002871 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002872 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002873
Willy Tarreaue7dff022015-04-03 01:14:29 +02002874 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002875 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002876
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002878 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau15e91e12015-04-04 00:52:09 +02002879 int ret = acl_exec_cond(cond, sess->fe, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002880
Willy Tarreau59234e92008-11-30 23:51:27 +01002881 ret = acl_pass(ret);
2882 if (cond->pol == ACL_COND_UNLESS)
2883 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002884
Willy Tarreau59234e92008-11-30 23:51:27 +01002885 if (ret) {
2886 /* we fail this request, let's return 503 service unavail */
2887 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002888 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002889 if (!(s->flags & SF_ERR_MASK))
2890 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002891 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002892 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002893 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002894
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 /* nothing to fail, let's reply normaly */
2896 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002897 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002898 if (!(s->flags & SF_ERR_MASK))
2899 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002900 goto return_prx_cond;
2901 }
2902
2903 /*
2904 * 3: Maybe we have to copy the original REQURI for the logs ?
2905 * Note: we cannot log anymore if the request has been
2906 * classified as invalid.
2907 */
2908 if (unlikely(s->logs.logwait & LW_REQ)) {
2909 /* we have a complete HTTP request that we must log */
2910 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2911 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002912
Willy Tarreau59234e92008-11-30 23:51:27 +01002913 if (urilen >= REQURI_LEN)
2914 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002915 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002916 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002917
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002918 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002919 s->do_log(s);
2920 } else {
2921 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002922 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 }
Willy Tarreau06619262006-12-17 08:37:22 +01002924
Willy Tarreau59234e92008-11-30 23:51:27 +01002925 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002926 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002927 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002928
Willy Tarreau5b154472009-12-21 20:11:07 +01002929 /* ... and check if the request is HTTP/1.1 or above */
2930 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002931 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2932 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2933 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002934 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002935
2936 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002937 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 +01002938
Willy Tarreau88d349d2010-01-25 12:15:43 +01002939 /* if the frontend has "option http-use-proxy-header", we'll check if
2940 * we have what looks like a proxied connection instead of a connection,
2941 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2942 * Note that this is *not* RFC-compliant, however browsers and proxies
2943 * happen to do that despite being non-standard :-(
2944 * We consider that a request not beginning with either '/' or '*' is
2945 * a proxied connection, which covers both "scheme://location" and
2946 * CONNECT ip:port.
2947 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002948 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002949 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002950 txn->flags |= TX_USE_PX_CONN;
2951
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002952 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002953 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002954
Willy Tarreau59234e92008-11-30 23:51:27 +01002955 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002956 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002957 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002958 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002959
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002960 /* 6: determine the transfer-length.
2961 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2962 * the presence of a message-body in a REQUEST and its transfer length
2963 * must be determined that way (in order of precedence) :
2964 * 1. The presence of a message-body in a request is signaled by the
2965 * inclusion of a Content-Length or Transfer-Encoding header field
2966 * in the request's header fields. When a request message contains
2967 * both a message-body of non-zero length and a method that does
2968 * not define any semantics for that request message-body, then an
2969 * origin server SHOULD either ignore the message-body or respond
2970 * with an appropriate error message (e.g., 413). A proxy or
2971 * gateway, when presented the same request, SHOULD either forward
2972 * the request inbound with the message- body or ignore the
2973 * message-body when determining a response.
2974 *
2975 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2976 * and the "chunked" transfer-coding (Section 6.2) is used, the
2977 * transfer-length is defined by the use of this transfer-coding.
2978 * If a Transfer-Encoding header field is present and the "chunked"
2979 * transfer-coding is not present, the transfer-length is defined
2980 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002981 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002982 * 3. If a Content-Length header field is present, its decimal value in
2983 * OCTETs represents both the entity-length and the transfer-length.
2984 * If a message is received with both a Transfer-Encoding header
2985 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002986 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002987 * 4. By the server closing the connection. (Closing the connection
2988 * cannot be used to indicate the end of a request body, since that
2989 * would leave no possibility for the server to send back a response.)
2990 *
2991 * Whenever a transfer-coding is applied to a message-body, the set of
2992 * transfer-codings MUST include "chunked", unless the message indicates
2993 * it is terminated by closing the connection. When the "chunked"
2994 * transfer-coding is used, it MUST be the last transfer-coding applied
2995 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002996 */
2997
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002999 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003000 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003001 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003002 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003003 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003004 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3005 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003006 /* bad transfer-encoding (chunked followed by something else) */
3007 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003008 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003009 break;
3010 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003011 }
3012
Willy Tarreau32b47f42009-10-18 20:55:02 +02003013 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003014 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003015 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003016 signed long long cl;
3017
Willy Tarreauad14f752011-09-02 20:33:27 +02003018 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003019 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003020 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003021 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003022
Willy Tarreauad14f752011-09-02 20:33:27 +02003023 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003024 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003025 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003026 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003027
Willy Tarreauad14f752011-09-02 20:33:27 +02003028 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003029 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003030 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003031 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003032
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003033 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003034 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003035 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003036 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003037
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003038 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003039 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003040 }
3041
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003042 /* bodyless requests have a known length */
3043 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003044 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003045
Willy Tarreau179085c2014-04-28 16:48:56 +02003046 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3047 * only change if both the request and the config reference something else.
3048 * Option httpclose by itself sets tunnel mode where headers are mangled.
3049 * However, if another mode is set, it will affect it (eg: server-close/
3050 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3051 * if FE and BE have the same settings (common). The method consists in
3052 * checking if options changed between the two calls (implying that either
3053 * one is non-null, or one of them is non-null and we are there for the first
3054 * time.
3055 */
3056 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003057 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003058 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003059
Willy Tarreaud787e662009-07-07 10:14:51 +02003060 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003061 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003062 req->analyse_exp = TICK_ETERNITY;
3063 return 1;
3064
3065 return_bad_req:
3066 /* We centralize bad requests processing here */
3067 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3068 /* we detected a parsing error. We want to archive this request
3069 * in the dedicated proxy area for later troubleshooting.
3070 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003071 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003072 }
3073
3074 txn->req.msg_state = HTTP_MSG_ERROR;
3075 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003076 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003077
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003078 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003079 if (sess->listener->counters)
3080 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003081
3082 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003083 if (!(s->flags & SF_ERR_MASK))
3084 s->flags |= SF_ERR_PRXCOND;
3085 if (!(s->flags & SF_FINST_MASK))
3086 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003087
3088 req->analysers = 0;
3089 req->analyse_exp = TICK_ETERNITY;
3090 return 0;
3091}
3092
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003093
Willy Tarreau347a35d2013-11-22 17:51:09 +01003094/* This function prepares an applet to handle the stats. It can deal with the
3095 * "100-continue" expectation, check that admin rules are met for POST requests,
3096 * and program a response message if something was unexpected. It cannot fail
3097 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003098 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003099 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003100 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003101 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003102int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003103{
3104 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003105 struct stream_interface *si = &s->si[1];
Willy Tarreaueee5b512015-04-03 23:46:31 +02003106 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003107 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003108 struct uri_auth *uri_auth = s->be->uri_auth;
3109 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003110 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003111
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003112 appctx = si_appctx(si);
3113 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3114 appctx->st1 = appctx->st2 = 0;
3115 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3116 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003117 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003118 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003119
3120 uri = msg->chn->buf->p + msg->sl.rq.u;
3121 lookup = uri + uri_auth->uri_len;
3122
3123 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3124 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003125 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003126 break;
3127 }
3128 }
3129
3130 if (uri_auth->refresh) {
3131 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3132 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003133 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003134 break;
3135 }
3136 }
3137 }
3138
3139 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3140 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003141 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003142 break;
3143 }
3144 }
3145
3146 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3147 if (memcmp(h, ";st=", 4) == 0) {
3148 int i;
3149 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003150 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003151 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3152 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003153 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003154 break;
3155 }
3156 }
3157 break;
3158 }
3159 }
3160
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003161 appctx->ctx.stats.scope_str = 0;
3162 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003163 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3164 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3165 int itx = 0;
3166 const char *h2;
3167 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3168 const char *err;
3169
3170 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3171 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003172 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003173 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3174 itx++;
3175 h++;
3176 }
3177
3178 if (itx > STAT_SCOPE_TXT_MAXLEN)
3179 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003181
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003182 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003183 memcpy(scope_txt, h2, itx);
3184 scope_txt[itx] = '\0';
3185 err = invalid_char(scope_txt);
3186 if (err) {
3187 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003188 appctx->ctx.stats.scope_str = 0;
3189 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003190 }
3191 break;
3192 }
3193 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003194
3195 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003196 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003197 int ret = 1;
3198
3199 if (stats_admin_rule->cond) {
Willy Tarreau15e91e12015-04-04 00:52:09 +02003200 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003201 ret = acl_pass(ret);
3202 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3203 ret = !ret;
3204 }
3205
3206 if (ret) {
3207 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003208 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003209 break;
3210 }
3211 }
3212
3213 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003214 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003215 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003216 /* we'll need the request body, possibly after sending 100-continue */
3217 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003218 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003219 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003220 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003221 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3222 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003223 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003224 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003225 else {
3226 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003227 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003228 }
3229
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003230 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003231 return 1;
3232}
3233
Lukas Tribus67db8df2013-06-23 17:37:13 +02003234/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3235 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3236 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003237void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003238{
3239#ifdef IP_TOS
3240 if (from.ss_family == AF_INET)
3241 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3242#endif
3243#ifdef IPV6_TCLASS
3244 if (from.ss_family == AF_INET6) {
3245 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3246 /* v4-mapped addresses need IP_TOS */
3247 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3248 else
3249 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3250 }
3251#endif
3252}
3253
Willy Tarreau87b09662015-04-03 00:22:06 +02003254int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003255 const char* name, unsigned int name_len,
3256 const char *str, struct my_regex *re,
3257 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003258{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003259 struct hdr_ctx ctx;
3260 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003261 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003262 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3263 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003264 struct chunk *output = get_trash_chunk();
3265
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003266 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003267
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003268 /* Choose the header browsing function. */
3269 switch (action) {
3270 case HTTP_REQ_ACT_REPLACE_VAL:
3271 case HTTP_RES_ACT_REPLACE_VAL:
3272 http_find_hdr_func = http_find_header2;
3273 break;
3274 case HTTP_REQ_ACT_REPLACE_HDR:
3275 case HTTP_RES_ACT_REPLACE_HDR:
3276 http_find_hdr_func = http_find_full_header2;
3277 break;
3278 default: /* impossible */
3279 return -1;
3280 }
3281
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003282 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3283 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003284 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003285 char *val = ctx.line + ctx.val;
3286 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003287
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003288 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3289 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003290
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003291 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003292 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003293 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003294
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003295 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003296
3297 hdr->len += delta;
3298 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003299
3300 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003301 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003302 }
3303
3304 return 0;
3305}
3306
Willy Tarreau87b09662015-04-03 00:22:06 +02003307static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003308 const char* name, unsigned int name_len,
3309 struct list *fmt, struct my_regex *re,
3310 int action)
3311{
3312 struct chunk *replace = get_trash_chunk();
3313
3314 replace->len = build_logline(s, replace->str, replace->size, fmt);
3315 if (replace->len >= replace->size - 1)
3316 return -1;
3317
3318 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3319}
3320
Willy Tarreau87b09662015-04-03 00:22:06 +02003321/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003322 * transaction <txn>. Returns the verdict of the first rule that prevents
3323 * further processing of the request (auth, deny, ...), and defaults to
3324 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3325 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3326 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003327 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003328enum rule_result
Willy Tarreau87b09662015-04-03 00:22:06 +02003329http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003330{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003331 struct session *sess = strm_sess(s);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003332 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003333 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003334 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003335 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003336
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003337 /* If "the current_rule_list" match the executed rule list, we are in
3338 * resume condition. If a resume is needed it is always in the action
3339 * and never in the ACL or converters. In this case, we initialise the
3340 * current rule, and go to the action execution point.
3341 */
3342 if (s->current_rule_list == rules) {
3343 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3344 goto resume_execution;
3345 }
3346 s->current_rule_list = rules;
Willy Tarreauff011f22011-01-06 17:51:27 +01003347 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003348 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003349 continue;
3350
Willy Tarreau96257ec2012-12-27 10:46:37 +01003351 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003352 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003353 int ret;
3354
Willy Tarreau15e91e12015-04-04 00:52:09 +02003355 ret = acl_exec_cond(rule->cond, px, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003356 ret = acl_pass(ret);
3357
Willy Tarreauff011f22011-01-06 17:51:27 +01003358 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003359 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003360
3361 if (!ret) /* condition not matched */
3362 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003363 }
3364
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003365resume_execution:
Willy Tarreau20b0de52012-12-24 15:45:22 +01003366
Willy Tarreau96257ec2012-12-27 10:46:37 +01003367 switch (rule->action) {
3368 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003369 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003370
3371 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003372 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003373
Willy Tarreauccbcc372012-12-27 12:37:57 +01003374 case HTTP_REQ_ACT_TARPIT:
3375 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003376 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003377
Willy Tarreau96257ec2012-12-27 10:46:37 +01003378 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003379 /* Auth might be performed on regular http-req rules as well as on stats */
3380 auth_realm = rule->arg.auth.realm;
3381 if (!auth_realm) {
3382 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3383 auth_realm = STATS_DEFAULT_REALM;
3384 else
3385 auth_realm = px->id;
3386 }
3387 /* send 401/407 depending on whether we use a proxy or not. We still
3388 * count one error, because normal browsing won't significantly
3389 * increase the counter but brute force attempts will.
3390 */
3391 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3392 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3393 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003394 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003395 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003396
Willy Tarreau81499eb2012-12-27 12:19:02 +01003397 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003398 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3399 return HTTP_RULE_RES_BADREQ;
3400 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003401
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003402 case HTTP_REQ_ACT_SET_NICE:
3403 s->task->nice = rule->arg.nice;
3404 break;
3405
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003406 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003407 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003408 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003409 break;
3410
Willy Tarreau51347ed2013-06-11 19:34:13 +02003411 case HTTP_REQ_ACT_SET_MARK:
3412#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003413 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003414 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003415#endif
3416 break;
3417
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003418 case HTTP_REQ_ACT_SET_LOGL:
3419 s->logs.level = rule->arg.loglevel;
3420 break;
3421
Sasha Pachev218f0642014-06-16 12:05:59 -06003422 case HTTP_REQ_ACT_REPLACE_HDR:
3423 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003424 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3425 rule->arg.hdr_add.name_len,
3426 &rule->arg.hdr_add.fmt,
3427 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003428 return HTTP_RULE_RES_BADREQ;
3429 break;
3430
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003431 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003432 ctx.idx = 0;
3433 /* remove all occurrences of the header */
3434 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3435 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3436 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003437 }
Willy Tarreau85603282015-01-21 20:39:27 +01003438 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003439
Willy Tarreau85603282015-01-21 20:39:27 +01003440 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003441 case HTTP_REQ_ACT_ADD_HDR:
3442 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3443 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3444 trash.len = rule->arg.hdr_add.name_len;
3445 trash.str[trash.len++] = ':';
3446 trash.str[trash.len++] = ' ';
3447 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 +01003448
3449 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3450 /* remove all occurrences of the header */
3451 ctx.idx = 0;
3452 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3453 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3454 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3455 }
3456 }
3457
Willy Tarreau96257ec2012-12-27 10:46:37 +01003458 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3459 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003460
3461 case HTTP_REQ_ACT_DEL_ACL:
3462 case HTTP_REQ_ACT_DEL_MAP: {
3463 struct pat_ref *ref;
3464 char *key;
3465 int len;
3466
3467 /* collect reference */
3468 ref = pat_ref_lookup(rule->arg.map.ref);
3469 if (!ref)
3470 continue;
3471
3472 /* collect key */
3473 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3474 key = trash.str;
3475 key[len] = '\0';
3476
3477 /* perform update */
3478 /* returned code: 1=ok, 0=ko */
3479 pat_ref_delete(ref, key);
3480
3481 break;
3482 }
3483
3484 case HTTP_REQ_ACT_ADD_ACL: {
3485 struct pat_ref *ref;
3486 char *key;
3487 struct chunk *trash_key;
3488 int len;
3489
3490 trash_key = get_trash_chunk();
3491
3492 /* collect reference */
3493 ref = pat_ref_lookup(rule->arg.map.ref);
3494 if (!ref)
3495 continue;
3496
3497 /* collect key */
3498 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3499 key = trash_key->str;
3500 key[len] = '\0';
3501
3502 /* perform update */
3503 /* add entry only if it does not already exist */
3504 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003505 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003506
3507 break;
3508 }
3509
3510 case HTTP_REQ_ACT_SET_MAP: {
3511 struct pat_ref *ref;
3512 char *key, *value;
3513 struct chunk *trash_key, *trash_value;
3514 int len;
3515
3516 trash_key = get_trash_chunk();
3517 trash_value = get_trash_chunk();
3518
3519 /* collect reference */
3520 ref = pat_ref_lookup(rule->arg.map.ref);
3521 if (!ref)
3522 continue;
3523
3524 /* collect key */
3525 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3526 key = trash_key->str;
3527 key[len] = '\0';
3528
3529 /* collect value */
3530 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3531 value = trash_value->str;
3532 value[len] = '\0';
3533
3534 /* perform update */
3535 if (pat_ref_find_elt(ref, key) != NULL)
3536 /* update entry if it exists */
3537 pat_ref_set(ref, key, value, NULL);
3538 else
3539 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003540 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003541
3542 break;
3543 }
William Lallemand73025dd2014-04-24 14:38:37 +02003544
3545 case HTTP_REQ_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003546 if (!rule->action_ptr(rule, px, s, txn)) {
3547 s->current_rule = &rule->list;
3548 return HTTP_RULE_RES_YIELD;
3549 }
William Lallemand73025dd2014-04-24 14:38:37 +02003550 break;
3551
3552 case HTTP_REQ_ACT_CUSTOM_STOP:
3553 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003554 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003555
3556 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3557 /* Note: only the first valid tracking parameter of each
3558 * applies.
3559 */
3560
3561 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3562 struct stktable *t;
3563 struct stksess *ts;
3564 struct stktable_key *key;
3565 void *ptr;
3566
3567 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau15e91e12015-04-04 00:52:09 +02003568 key = stktable_fetch_key(t, s->be, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02003569
3570 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003571 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003572
3573 /* let's count a new HTTP request as it's the first time we do it */
3574 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3575 if (ptr)
3576 stktable_data_cast(ptr, http_req_cnt)++;
3577
3578 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3579 if (ptr)
3580 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3581 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3582
3583 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003584 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003585 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3586 }
3587 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003588 }
3589 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003590
3591 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003592 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003593}
3594
Willy Tarreau71241ab2012-12-27 11:30:54 +01003595
Willy Tarreau87b09662015-04-03 00:22:06 +02003596/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003597 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3598 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3599 * evaluation of next rule list. If *STOP is returned, the process must stop
3600 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3601 * rule. If *YIELD is returned, the czller must call again the function with
3602 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003603 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003604static enum rule_result
Willy Tarreau87b09662015-04-03 00:22:06 +02003605http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, struct http_txn *txn)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003606{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003607 struct session *sess = strm_sess(s);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003608 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003609 struct http_res_rule *rule;
3610 struct hdr_ctx ctx;
3611
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003612 /* If "the current_rule_list" match the executed rule list, we are in
3613 * resume condition. If a resume is needed it is always in the action
3614 * and never in the ACL or converters. In this case, we initialise the
3615 * current rule, and go to the action execution point.
3616 */
3617 if (s->current_rule_list == rules) {
3618 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3619 goto resume_execution;
3620 }
3621 s->current_rule_list = rules;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003622 list_for_each_entry(rule, rules, list) {
3623 if (rule->action >= HTTP_RES_ACT_MAX)
3624 continue;
3625
3626 /* check optional condition */
3627 if (rule->cond) {
3628 int ret;
3629
Willy Tarreau15e91e12015-04-04 00:52:09 +02003630 ret = acl_exec_cond(rule->cond, px, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003631 ret = acl_pass(ret);
3632
3633 if (rule->cond->pol == ACL_COND_UNLESS)
3634 ret = !ret;
3635
3636 if (!ret) /* condition not matched */
3637 continue;
3638 }
3639
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003640resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003641
3642 switch (rule->action) {
3643 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003644 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003645
3646 case HTTP_RES_ACT_DENY:
3647 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003648 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003649
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003650 case HTTP_RES_ACT_SET_NICE:
3651 s->task->nice = rule->arg.nice;
3652 break;
3653
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003654 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003655 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003656 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003657 break;
3658
Willy Tarreau51347ed2013-06-11 19:34:13 +02003659 case HTTP_RES_ACT_SET_MARK:
3660#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003661 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003662 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003663#endif
3664 break;
3665
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003666 case HTTP_RES_ACT_SET_LOGL:
3667 s->logs.level = rule->arg.loglevel;
3668 break;
3669
Sasha Pachev218f0642014-06-16 12:05:59 -06003670 case HTTP_RES_ACT_REPLACE_HDR:
3671 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003672 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3673 rule->arg.hdr_add.name_len,
3674 &rule->arg.hdr_add.fmt,
3675 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003676 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003677 break;
3678
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003679 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003680 ctx.idx = 0;
3681 /* remove all occurrences of the header */
3682 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3683 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3684 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3685 }
Willy Tarreau85603282015-01-21 20:39:27 +01003686 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003687
Willy Tarreau85603282015-01-21 20:39:27 +01003688 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003689 case HTTP_RES_ACT_ADD_HDR:
3690 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3691 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3692 trash.len = rule->arg.hdr_add.name_len;
3693 trash.str[trash.len++] = ':';
3694 trash.str[trash.len++] = ' ';
3695 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 +01003696
3697 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3698 /* remove all occurrences of the header */
3699 ctx.idx = 0;
3700 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3701 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3702 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3703 }
3704 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003705 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3706 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003707
3708 case HTTP_RES_ACT_DEL_ACL:
3709 case HTTP_RES_ACT_DEL_MAP: {
3710 struct pat_ref *ref;
3711 char *key;
3712 int len;
3713
3714 /* collect reference */
3715 ref = pat_ref_lookup(rule->arg.map.ref);
3716 if (!ref)
3717 continue;
3718
3719 /* collect key */
3720 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3721 key = trash.str;
3722 key[len] = '\0';
3723
3724 /* perform update */
3725 /* returned code: 1=ok, 0=ko */
3726 pat_ref_delete(ref, key);
3727
3728 break;
3729 }
3730
3731 case HTTP_RES_ACT_ADD_ACL: {
3732 struct pat_ref *ref;
3733 char *key;
3734 struct chunk *trash_key;
3735 int len;
3736
3737 trash_key = get_trash_chunk();
3738
3739 /* collect reference */
3740 ref = pat_ref_lookup(rule->arg.map.ref);
3741 if (!ref)
3742 continue;
3743
3744 /* collect key */
3745 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3746 key = trash_key->str;
3747 key[len] = '\0';
3748
3749 /* perform update */
3750 /* check if the entry already exists */
3751 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003752 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003753
3754 break;
3755 }
3756
3757 case HTTP_RES_ACT_SET_MAP: {
3758 struct pat_ref *ref;
3759 char *key, *value;
3760 struct chunk *trash_key, *trash_value;
3761 int len;
3762
3763 trash_key = get_trash_chunk();
3764 trash_value = get_trash_chunk();
3765
3766 /* collect reference */
3767 ref = pat_ref_lookup(rule->arg.map.ref);
3768 if (!ref)
3769 continue;
3770
3771 /* collect key */
3772 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3773 key = trash_key->str;
3774 key[len] = '\0';
3775
3776 /* collect value */
3777 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3778 value = trash_value->str;
3779 value[len] = '\0';
3780
3781 /* perform update */
3782 if (pat_ref_find_elt(ref, key) != NULL)
3783 /* update entry if it exists */
3784 pat_ref_set(ref, key, value, NULL);
3785 else
3786 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003787 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003788
3789 break;
3790 }
William Lallemand73025dd2014-04-24 14:38:37 +02003791
3792 case HTTP_RES_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003793 if (!rule->action_ptr(rule, px, s, txn)) {
3794 s->current_rule = &rule->list;
3795 return HTTP_RULE_RES_YIELD;
3796 }
William Lallemand73025dd2014-04-24 14:38:37 +02003797 break;
3798
3799 case HTTP_RES_ACT_CUSTOM_STOP:
3800 rule->action_ptr(rule, px, s, txn);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003801 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802 }
3803 }
3804
3805 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003806 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003807}
3808
3809
Willy Tarreau71241ab2012-12-27 11:30:54 +01003810/* Perform an HTTP redirect based on the information in <rule>. The function
3811 * returns non-zero on success, or zero in case of a, irrecoverable error such
3812 * as too large a request to build a valid response.
3813 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003814static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003815{
3816 struct http_msg *msg = &txn->req;
3817 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003818 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003819
3820 /* build redirect message */
3821 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003822 case 308:
3823 msg_fmt = HTTP_308;
3824 break;
3825 case 307:
3826 msg_fmt = HTTP_307;
3827 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003828 case 303:
3829 msg_fmt = HTTP_303;
3830 break;
3831 case 301:
3832 msg_fmt = HTTP_301;
3833 break;
3834 case 302:
3835 default:
3836 msg_fmt = HTTP_302;
3837 break;
3838 }
3839
3840 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3841 return 0;
3842
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003843 location = trash.str + trash.len;
3844
Willy Tarreau71241ab2012-12-27 11:30:54 +01003845 switch(rule->type) {
3846 case REDIRECT_TYPE_SCHEME: {
3847 const char *path;
3848 const char *host;
3849 struct hdr_ctx ctx;
3850 int pathlen;
3851 int hostlen;
3852
3853 host = "";
3854 hostlen = 0;
3855 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003856 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003857 host = ctx.line + ctx.val;
3858 hostlen = ctx.vlen;
3859 }
3860
3861 path = http_get_path(txn);
3862 /* build message using path */
3863 if (path) {
3864 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3865 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3866 int qs = 0;
3867 while (qs < pathlen) {
3868 if (path[qs] == '?') {
3869 pathlen = qs;
3870 break;
3871 }
3872 qs++;
3873 }
3874 }
3875 } else {
3876 path = "/";
3877 pathlen = 1;
3878 }
3879
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003880 if (rule->rdr_str) { /* this is an old "redirect" rule */
3881 /* check if we can add scheme + "://" + host + path */
3882 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3883 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003884
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003885 /* add scheme */
3886 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3887 trash.len += rule->rdr_len;
3888 }
3889 else {
3890 /* add scheme with executing log format */
3891 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003892
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003893 /* check if we can add scheme + "://" + host + path */
3894 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3895 return 0;
3896 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003897 /* add "://" */
3898 memcpy(trash.str + trash.len, "://", 3);
3899 trash.len += 3;
3900
3901 /* add host */
3902 memcpy(trash.str + trash.len, host, hostlen);
3903 trash.len += hostlen;
3904
3905 /* add path */
3906 memcpy(trash.str + trash.len, path, pathlen);
3907 trash.len += pathlen;
3908
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003909 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003910 if (trash.len && trash.str[trash.len - 1] != '/' &&
3911 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3912 if (trash.len > trash.size - 5)
3913 return 0;
3914 trash.str[trash.len] = '/';
3915 trash.len++;
3916 }
3917
3918 break;
3919 }
3920 case REDIRECT_TYPE_PREFIX: {
3921 const char *path;
3922 int pathlen;
3923
3924 path = http_get_path(txn);
3925 /* build message using path */
3926 if (path) {
3927 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3928 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3929 int qs = 0;
3930 while (qs < pathlen) {
3931 if (path[qs] == '?') {
3932 pathlen = qs;
3933 break;
3934 }
3935 qs++;
3936 }
3937 }
3938 } else {
3939 path = "/";
3940 pathlen = 1;
3941 }
3942
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003943 if (rule->rdr_str) { /* this is an old "redirect" rule */
3944 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3945 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003946
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003947 /* add prefix. Note that if prefix == "/", we don't want to
3948 * add anything, otherwise it makes it hard for the user to
3949 * configure a self-redirection.
3950 */
3951 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3952 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3953 trash.len += rule->rdr_len;
3954 }
3955 }
3956 else {
3957 /* add prefix with executing log format */
3958 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3959
3960 /* Check length */
3961 if (trash.len + pathlen > trash.size - 4)
3962 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003963 }
3964
3965 /* add path */
3966 memcpy(trash.str + trash.len, path, pathlen);
3967 trash.len += pathlen;
3968
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003969 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003970 if (trash.len && trash.str[trash.len - 1] != '/' &&
3971 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3972 if (trash.len > trash.size - 5)
3973 return 0;
3974 trash.str[trash.len] = '/';
3975 trash.len++;
3976 }
3977
3978 break;
3979 }
3980 case REDIRECT_TYPE_LOCATION:
3981 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003982 if (rule->rdr_str) { /* this is an old "redirect" rule */
3983 if (trash.len + rule->rdr_len > trash.size - 4)
3984 return 0;
3985
3986 /* add location */
3987 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3988 trash.len += rule->rdr_len;
3989 }
3990 else {
3991 /* add location with executing log format */
3992 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003993
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003994 /* Check left length */
3995 if (trash.len > trash.size - 4)
3996 return 0;
3997 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003998 break;
3999 }
4000
4001 if (rule->cookie_len) {
4002 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4003 trash.len += 14;
4004 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4005 trash.len += rule->cookie_len;
4006 memcpy(trash.str + trash.len, "\r\n", 2);
4007 trash.len += 2;
4008 }
4009
4010 /* add end of headers and the keep-alive/close status.
4011 * We may choose to set keep-alive if the Location begins
4012 * with a slash, because the client will come back to the
4013 * same server.
4014 */
4015 txn->status = rule->code;
4016 /* let's log the request time */
4017 s->logs.tv_request = now;
4018
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004019 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004020 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4021 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4022 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4023 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4024 /* keep-alive possible */
4025 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4026 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4027 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4028 trash.len += 30;
4029 } else {
4030 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4031 trash.len += 24;
4032 }
4033 }
4034 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4035 trash.len += 4;
4036 bo_inject(txn->rsp.chn, trash.str, trash.len);
4037 /* "eat" the request */
4038 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004039 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004040 msg->sov = 0;
4041 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004042 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004043 txn->req.msg_state = HTTP_MSG_CLOSED;
4044 txn->rsp.msg_state = HTTP_MSG_DONE;
4045 } else {
4046 /* keep-alive not possible */
4047 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4048 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4049 trash.len += 29;
4050 } else {
4051 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4052 trash.len += 23;
4053 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004054 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004055 txn->req.chn->analysers = 0;
4056 }
4057
Willy Tarreaue7dff022015-04-03 01:14:29 +02004058 if (!(s->flags & SF_ERR_MASK))
4059 s->flags |= SF_ERR_LOCAL;
4060 if (!(s->flags & SF_FINST_MASK))
4061 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004062
4063 return 1;
4064}
4065
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004066/* This stream analyser runs all HTTP request processing which is common to
4067 * frontends and backends, which means blocking ACLs, filters, connection-close,
4068 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004069 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004070 * either needs more data or wants to immediately abort the request (eg: deny,
4071 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004072 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004073int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004074{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004075 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004076 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004077 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004078 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004079 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004080 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004081
Willy Tarreau655dce92009-11-08 13:10:58 +01004082 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004083 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004084 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004085 }
4086
Willy Tarreau87b09662015-04-03 00:22:06 +02004087 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 +02004088 now_ms, __FUNCTION__,
4089 s,
4090 req,
4091 req->rex, req->wex,
4092 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004093 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004094 req->analysers);
4095
Willy Tarreau65410832014-04-28 21:25:43 +02004096 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004097 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004098
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004099 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004100 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4101 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004102
Willy Tarreau0b748332014-04-29 00:13:29 +02004103 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004104 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4105 goto return_prx_yield;
4106
Willy Tarreau0b748332014-04-29 00:13:29 +02004107 case HTTP_RULE_RES_CONT:
4108 case HTTP_RULE_RES_STOP: /* nothing to do */
4109 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004110
Willy Tarreau0b748332014-04-29 00:13:29 +02004111 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4112 if (txn->flags & TX_CLTARPIT)
4113 goto tarpit;
4114 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004115
Willy Tarreau0b748332014-04-29 00:13:29 +02004116 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4117 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004118
Willy Tarreau0b748332014-04-29 00:13:29 +02004119 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004120 goto done;
4121
Willy Tarreau0b748332014-04-29 00:13:29 +02004122 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4123 goto return_bad_req;
4124 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004125 }
4126
Willy Tarreau52542592014-04-28 18:33:26 +02004127 /* OK at this stage, we know that the request was accepted according to
4128 * the http-request rules, we can check for the stats. Note that the
4129 * URI is detected *before* the req* rules in order not to be affected
4130 * by a possible reqrep, while they are processed *after* so that a
4131 * reqdeny can still block them. This clearly needs to change in 1.6!
4132 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004133 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004134 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004135 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004136 txn->status = 500;
4137 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004138 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004139
Willy Tarreaue7dff022015-04-03 01:14:29 +02004140 if (!(s->flags & SF_ERR_MASK))
4141 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004142 goto return_prx_cond;
4143 }
4144
4145 /* parse the whole stats request and extract the relevant information */
4146 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004147 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4148 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004149
Willy Tarreau0b748332014-04-29 00:13:29 +02004150 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4151 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004152
Willy Tarreau0b748332014-04-29 00:13:29 +02004153 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4154 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004155 }
4156
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004157 /* evaluate the req* rules except reqadd */
4158 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004159 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004160 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004161
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004162 if (txn->flags & TX_CLDENY)
4163 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004164
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004165 if (txn->flags & TX_CLTARPIT)
4166 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004167 }
Willy Tarreau06619262006-12-17 08:37:22 +01004168
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004169 /* add request headers from the rule sets in the same order */
4170 list_for_each_entry(wl, &px->req_add, list) {
4171 if (wl->cond) {
Willy Tarreau15e91e12015-04-04 00:52:09 +02004172 int ret = acl_exec_cond(wl->cond, px, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004173 ret = acl_pass(ret);
4174 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4175 ret = !ret;
4176 if (!ret)
4177 continue;
4178 }
4179
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004180 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004181 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004182 }
4183
Willy Tarreau52542592014-04-28 18:33:26 +02004184
4185 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004186 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004187 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004188 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4189 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004190
Willy Tarreaue7dff022015-04-03 01:14:29 +02004191 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4192 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4193 if (!(s->flags & SF_FINST_MASK))
4194 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004195
Willy Tarreau70730dd2014-04-24 18:06:27 +02004196 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004197 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004198 select_compression_request_header(s, req->buf);
4199
4200 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004201 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004202 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004203 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004204
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004205 /* check whether we have some ACLs set to redirect this request */
4206 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004207 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004208 int ret;
4209
Willy Tarreau15e91e12015-04-04 00:52:09 +02004210 ret = acl_exec_cond(rule->cond, px, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004211 ret = acl_pass(ret);
4212 if (rule->cond->pol == ACL_COND_UNLESS)
4213 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004214 if (!ret)
4215 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004216 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004217 if (!http_apply_redirect_rule(rule, s, txn))
4218 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004219 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004220 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004221
Willy Tarreau2be39392010-01-03 17:24:51 +01004222 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4223 * If this happens, then the data will not come immediately, so we must
4224 * send all what we have without waiting. Note that due to the small gain
4225 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004226 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004227 * itself once used.
4228 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004229 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004230
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004231 done: /* done with this analyser, continue with next ones that the calling
4232 * points will have set, if any.
4233 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004234 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004235 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4236 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004237 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004238
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004239 tarpit:
4240 /* When a connection is tarpitted, we use the tarpit timeout,
4241 * which may be the same as the connect timeout if unspecified.
4242 * If unset, then set it to zero because we really want it to
4243 * eventually expire. We build the tarpit as an analyser.
4244 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004245 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004246
4247 /* wipe the request out so that we can drop the connection early
4248 * if the client closes first.
4249 */
4250 channel_dont_connect(req);
4251 req->analysers = 0; /* remove switching rules etc... */
4252 req->analysers |= AN_REQ_HTTP_TARPIT;
4253 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4254 if (!req->analyse_exp)
4255 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004256 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004257 sess->fe->fe_counters.denied_req++;
4258 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004259 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004260 if (sess->listener->counters)
4261 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004262 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004263
4264 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004265 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004266 txn->status = 403;
4267 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004268 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_403));
Willy Tarreau87b09662015-04-03 00:22:06 +02004269 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004270 sess->fe->fe_counters.denied_req++;
4271 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004272 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004273 if (sess->listener->counters)
4274 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004275 goto return_prx_cond;
4276
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004277 return_bad_req:
4278 /* We centralize bad requests processing here */
4279 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4280 /* we detected a parsing error. We want to archive this request
4281 * in the dedicated proxy area for later troubleshooting.
4282 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004283 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004284 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004285
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004286 txn->req.msg_state = HTTP_MSG_ERROR;
4287 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004288 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004289
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004290 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004291 if (sess->listener->counters)
4292 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004293
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004294 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004295 if (!(s->flags & SF_ERR_MASK))
4296 s->flags |= SF_ERR_PRXCOND;
4297 if (!(s->flags & SF_FINST_MASK))
4298 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004299
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004300 req->analysers = 0;
4301 req->analyse_exp = TICK_ETERNITY;
4302 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004303
4304 return_prx_yield:
4305 channel_dont_connect(req);
4306 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004307}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004308
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004309/* This function performs all the processing enabled for the current request.
4310 * It returns 1 if the processing can continue on next analysers, or zero if it
4311 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004312 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004313 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004314int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004315{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004316 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004317 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004318 struct http_msg *msg = &txn->req;
Willy Tarreau350f4872014-11-28 14:42:25 +01004319 struct connection *cli_conn = objt_conn(s->si[1].end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004320
Willy Tarreau655dce92009-11-08 13:10:58 +01004321 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004322 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004323 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004324 return 0;
4325 }
4326
Willy Tarreau87b09662015-04-03 00:22:06 +02004327 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 +02004328 now_ms, __FUNCTION__,
4329 s,
4330 req,
4331 req->rex, req->wex,
4332 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004333 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004334 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004335
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004336 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004337 select_compression_request_header(s, req->buf);
4338
Willy Tarreau59234e92008-11-30 23:51:27 +01004339 /*
4340 * Right now, we know that we have processed the entire headers
4341 * and that unwanted requests have been filtered out. We can do
4342 * whatever we want with the remaining request. Also, now we
4343 * may have separate values for ->fe, ->be.
4344 */
Willy Tarreau06619262006-12-17 08:37:22 +01004345
Willy Tarreau59234e92008-11-30 23:51:27 +01004346 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004347 * If HTTP PROXY is set we simply get remote server address parsing
4348 * incoming request. Note that this requires that a connection is
4349 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004350 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004351 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004352 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004353 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004354
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004355 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004356 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004357 txn->req.msg_state = HTTP_MSG_ERROR;
4358 txn->status = 500;
4359 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004360 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004361
Willy Tarreaue7dff022015-04-03 01:14:29 +02004362 if (!(s->flags & SF_ERR_MASK))
4363 s->flags |= SF_ERR_RESOURCE;
4364 if (!(s->flags & SF_FINST_MASK))
4365 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004366
4367 return 0;
4368 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004369
4370 path = http_get_path(txn);
4371 url2sa(req->buf->p + msg->sl.rq.u,
4372 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004373 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004374 /* if the path was found, we have to remove everything between
4375 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4376 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4377 * u_l characters by a single "/".
4378 */
4379 if (path) {
4380 char *cur_ptr = req->buf->p;
4381 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4382 int delta;
4383
4384 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4385 http_msg_move_end(&txn->req, delta);
4386 cur_end += delta;
4387 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4388 goto return_bad_req;
4389 }
4390 else {
4391 char *cur_ptr = req->buf->p;
4392 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4393 int delta;
4394
4395 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4396 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4397 http_msg_move_end(&txn->req, delta);
4398 cur_end += delta;
4399 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4400 goto return_bad_req;
4401 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004402 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004403
Willy Tarreau59234e92008-11-30 23:51:27 +01004404 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004405 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004406 * Note that doing so might move headers in the request, but
4407 * the fields will stay coherent and the URI will not move.
4408 * This should only be performed in the backend.
4409 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004410 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004411 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4412 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004413
Willy Tarreau59234e92008-11-30 23:51:27 +01004414 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004415 * 8: the appsession cookie was looked up very early in 1.2,
4416 * so let's do the same now.
4417 */
4418
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004419 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004420 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004421 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 +01004422 }
4423
William Lallemanda73203e2012-03-12 12:48:57 +01004424 /* add unique-id if "header-unique-id" is specified */
4425
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004426 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004427 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4428 goto return_bad_req;
4429 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004430 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004431 }
William Lallemanda73203e2012-03-12 12:48:57 +01004432
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004433 if (sess->fe->header_unique_id && s->unique_id) {
4434 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004435 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004436 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004437 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004438 goto return_bad_req;
4439 }
4440
Cyril Bontéb21570a2009-11-29 20:04:48 +01004441 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004442 * 9: add X-Forwarded-For if either the frontend or the backend
4443 * asks for it.
4444 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004445 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004446 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004447 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4448 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4449 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004450 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004451 /* The header is set to be added only if none is present
4452 * and we found it, so don't do anything.
4453 */
4454 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004455 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004456 /* Add an X-Forwarded-For header unless the source IP is
4457 * in the 'except' network range.
4458 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004459 if ((!sess->fe->except_mask.s_addr ||
4460 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4461 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004462 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004463 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004464 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004465 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004466 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004467 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004468
4469 /* Note: we rely on the backend to get the header name to be used for
4470 * x-forwarded-for, because the header is really meant for the backends.
4471 * However, if the backend did not specify any option, we have to rely
4472 * on the frontend's header name.
4473 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004474 if (s->be->fwdfor_hdr_len) {
4475 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004476 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004477 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004478 len = sess->fe->fwdfor_hdr_len;
4479 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004480 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004481 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 +01004482
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004483 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004484 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004485 }
4486 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004487 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004488 /* FIXME: for the sake of completeness, we should also support
4489 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004490 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004491 int len;
4492 char pn[INET6_ADDRSTRLEN];
4493 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004494 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004495 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004496
Willy Tarreau59234e92008-11-30 23:51:27 +01004497 /* Note: we rely on the backend to get the header name to be used for
4498 * x-forwarded-for, because the header is really meant for the backends.
4499 * However, if the backend did not specify any option, we have to rely
4500 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004501 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004502 if (s->be->fwdfor_hdr_len) {
4503 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004504 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004505 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004506 len = sess->fe->fwdfor_hdr_len;
4507 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004508 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004509 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004510
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004511 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004512 goto return_bad_req;
4513 }
4514 }
4515
4516 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004517 * 10: add X-Original-To if either the frontend or the backend
4518 * asks for it.
4519 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004520 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004521
4522 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004523 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004524 /* Add an X-Original-To header unless the destination IP is
4525 * in the 'except' network range.
4526 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004527 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004528
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004529 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004530 ((!sess->fe->except_mask_to.s_addr ||
4531 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4532 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004533 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004534 (((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 +02004535 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004536 int len;
4537 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004538 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004539
4540 /* Note: we rely on the backend to get the header name to be used for
4541 * x-original-to, because the header is really meant for the backends.
4542 * However, if the backend did not specify any option, we have to rely
4543 * on the frontend's header name.
4544 */
4545 if (s->be->orgto_hdr_len) {
4546 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004547 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004548 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004549 len = sess->fe->orgto_hdr_len;
4550 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004551 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004552 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 +02004553
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004554 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004555 goto return_bad_req;
4556 }
4557 }
4558 }
4559
Willy Tarreau50fc7772012-11-11 22:19:57 +01004560 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4561 * If an "Upgrade" token is found, the header is left untouched in order not to have
4562 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4563 * "Upgrade" is present in the Connection header.
4564 */
4565 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4566 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004567 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004568 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004569 unsigned int want_flags = 0;
4570
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004571 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004572 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004573 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004574 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004575 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004576 want_flags |= TX_CON_CLO_SET;
4577 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004578 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004579 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004580 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004581 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004582 want_flags |= TX_CON_KAL_SET;
4583 }
4584
4585 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004586 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004587 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004588
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004589
Willy Tarreau522d6c02009-12-06 18:49:18 +01004590 /* If we have no server assigned yet and we're balancing on url_param
4591 * with a POST request, we may be interested in checking the body for
4592 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004593 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004594 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004595 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004596 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004597 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004598 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004599 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004600
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004601 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004602 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004603#ifdef TCP_QUICKACK
4604 /* We expect some data from the client. Unless we know for sure
4605 * we already have a full request, we have to re-enable quick-ack
4606 * in case we previously disabled it, otherwise we might cause
4607 * the client to delay further data.
4608 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004609 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004610 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004611 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004612 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004613 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004614#endif
4615 }
Willy Tarreau03945942009-12-22 16:50:27 +01004616
Willy Tarreau59234e92008-11-30 23:51:27 +01004617 /*************************************************************
4618 * OK, that's finished for the headers. We have done what we *
4619 * could. Let's switch to the DATA state. *
4620 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004621 req->analyse_exp = TICK_ETERNITY;
4622 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004623
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004624 /* if the server closes the connection, we want to immediately react
4625 * and close the socket to save packets and syscalls.
4626 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004627 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004628 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004629
Willy Tarreau59234e92008-11-30 23:51:27 +01004630 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004631 /* OK let's go on with the BODY now */
4632 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004633
Willy Tarreau59234e92008-11-30 23:51:27 +01004634 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004635 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004636 /* we detected a parsing error. We want to archive this request
4637 * in the dedicated proxy area for later troubleshooting.
4638 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004639 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004640 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004641
Willy Tarreau59234e92008-11-30 23:51:27 +01004642 txn->req.msg_state = HTTP_MSG_ERROR;
4643 txn->status = 400;
4644 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004645 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004646
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004647 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004648 if (sess->listener->counters)
4649 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004650
Willy Tarreaue7dff022015-04-03 01:14:29 +02004651 if (!(s->flags & SF_ERR_MASK))
4652 s->flags |= SF_ERR_PRXCOND;
4653 if (!(s->flags & SF_FINST_MASK))
4654 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004655 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004656}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004657
Willy Tarreau60b85b02008-11-30 23:28:40 +01004658/* This function is an analyser which processes the HTTP tarpit. It always
4659 * returns zero, at the beginning because it prevents any other processing
4660 * from occurring, and at the end because it terminates the request.
4661 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004662int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004663{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004664 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004665
4666 /* This connection is being tarpitted. The CLIENT side has
4667 * already set the connect expiration date to the right
4668 * timeout. We just have to check that the client is still
4669 * there and that the timeout has not expired.
4670 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004671 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004672 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004673 !tick_is_expired(req->analyse_exp, now_ms))
4674 return 0;
4675
4676 /* We will set the queue timer to the time spent, just for
4677 * logging purposes. We fake a 500 server error, so that the
4678 * attacker will not suspect his connection has been tarpitted.
4679 * It will not cause trouble to the logs because we can exclude
4680 * the tarpitted connections by filtering on the 'PT' status flags.
4681 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004682 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4683
4684 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004685 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004686 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004687
4688 req->analysers = 0;
4689 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004690
Willy Tarreaue7dff022015-04-03 01:14:29 +02004691 if (!(s->flags & SF_ERR_MASK))
4692 s->flags |= SF_ERR_PRXCOND;
4693 if (!(s->flags & SF_FINST_MASK))
4694 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004695 return 0;
4696}
4697
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004698/* This function is an analyser which waits for the HTTP request body. It waits
4699 * for either the buffer to be full, or the full advertised contents to have
4700 * reached the buffer. It must only be called after the standard HTTP request
4701 * processing has occurred, because it expects the request to be parsed and will
4702 * look for the Expect header. It may send a 100-Continue interim response. It
4703 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4704 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4705 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004706 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004707int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004708{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004709 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004710 struct http_txn *txn = s->txn;
4711 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004712
4713 /* We have to parse the HTTP request body to find any required data.
4714 * "balance url_param check_post" should have been the only way to get
4715 * into this. We were brought here after HTTP header analysis, so all
4716 * related structures are ready.
4717 */
4718
Willy Tarreau890988f2014-04-10 11:59:33 +02004719 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4720 /* This is the first call */
4721 if (msg->msg_state < HTTP_MSG_BODY)
4722 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004723
Willy Tarreau890988f2014-04-10 11:59:33 +02004724 if (msg->msg_state < HTTP_MSG_100_SENT) {
4725 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4726 * send an HTTP/1.1 100 Continue intermediate response.
4727 */
4728 if (msg->flags & HTTP_MSGF_VER_11) {
4729 struct hdr_ctx ctx;
4730 ctx.idx = 0;
4731 /* Expect is allowed in 1.1, look for it */
4732 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4733 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004734 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004735 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004736 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004737 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004738 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004739
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004740 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004741 * req->buf->p still points to the beginning of the message. We
4742 * must save the body in msg->next because it survives buffer
4743 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004744 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004745 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004746
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004747 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004748 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4749 else
4750 msg->msg_state = HTTP_MSG_DATA;
4751 }
4752
Willy Tarreau890988f2014-04-10 11:59:33 +02004753 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4754 /* We're in content-length mode, we just have to wait for enough data. */
4755 if (req->buf->i - msg->sov < msg->body_len)
4756 goto missing_data;
4757
4758 /* OK we have everything we need now */
4759 goto http_end;
4760 }
4761
4762 /* OK here we're parsing a chunked-encoded message */
4763
Willy Tarreau522d6c02009-12-06 18:49:18 +01004764 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004765 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004766 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004767 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004768 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004769 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004770
Willy Tarreau115acb92009-12-26 13:56:06 +01004771 if (!ret)
4772 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004773 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004774 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004775 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004776 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004777 }
4778
Willy Tarreaud98cf932009-12-27 22:54:55 +01004779 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004780 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004781 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004782 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004783 if (msg->msg_state == HTTP_MSG_TRAILERS)
4784 goto http_end;
4785
Willy Tarreau226071e2014-04-10 11:55:45 +02004786 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004787 goto http_end;
4788
4789 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004790 /* we get here if we need to wait for more data. If the buffer is full,
4791 * we have the maximum we can expect.
4792 */
4793 if (buffer_full(req->buf, global.tune.maxrewrite))
4794 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004795
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004796 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004797 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004798 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004799
Willy Tarreaue7dff022015-04-03 01:14:29 +02004800 if (!(s->flags & SF_ERR_MASK))
4801 s->flags |= SF_ERR_CLITO;
4802 if (!(s->flags & SF_FINST_MASK))
4803 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004804 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004805 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004806
4807 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004808 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004809 /* Not enough data. We'll re-use the http-request
4810 * timeout here. Ideally, we should set the timeout
4811 * relative to the accept() date. We just set the
4812 * request timeout once at the beginning of the
4813 * request.
4814 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004815 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004816 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004817 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004818 return 0;
4819 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004820
4821 http_end:
4822 /* The situation will not evolve, so let's give up on the analysis. */
4823 s->logs.tv_request = now; /* update the request timer to reflect full request */
4824 req->analysers &= ~an_bit;
4825 req->analyse_exp = TICK_ETERNITY;
4826 return 1;
4827
4828 return_bad_req: /* let's centralize all bad requests */
4829 txn->req.msg_state = HTTP_MSG_ERROR;
4830 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004831 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004832
Willy Tarreaue7dff022015-04-03 01:14:29 +02004833 if (!(s->flags & SF_ERR_MASK))
4834 s->flags |= SF_ERR_PRXCOND;
4835 if (!(s->flags & SF_FINST_MASK))
4836 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004837
Willy Tarreau522d6c02009-12-06 18:49:18 +01004838 return_err_msg:
4839 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004840 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004841 if (sess->listener->counters)
4842 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004843 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004844}
4845
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004846/* send a server's name with an outgoing request over an established connection.
4847 * Note: this function is designed to be called once the request has been scheduled
4848 * for being forwarded. This is the reason why it rewinds the buffer before
4849 * proceeding.
4850 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004851int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004852
4853 struct hdr_ctx ctx;
4854
Mark Lamourinec2247f02012-01-04 13:02:01 -05004855 char *hdr_name = be->server_id_hdr_name;
4856 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004857 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004858 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004859 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004860
William Lallemandd9e90662012-01-30 17:27:17 +01004861 ctx.idx = 0;
4862
Willy Tarreau211cdec2014-04-17 20:18:08 +02004863 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004864 if (old_o) {
4865 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004866 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004867 txn->req.next += old_o;
4868 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004869 }
4870
Willy Tarreau9b28e032012-10-12 23:49:43 +02004871 old_i = chn->buf->i;
4872 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 -05004873 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004874 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004875 }
4876
4877 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004878 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004879 memcpy(hdr_val, hdr_name, hdr_name_len);
4880 hdr_val += hdr_name_len;
4881 *hdr_val++ = ':';
4882 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004883 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4884 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004885
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004886 if (old_o) {
4887 /* If this was a forwarded request, we must readjust the amount of
4888 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004889 * variations. Note that the current state is >= HTTP_MSG_BODY,
4890 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004891 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004892 old_o += chn->buf->i - old_i;
4893 b_adv(chn->buf, old_o);
4894 txn->req.next -= old_o;
4895 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004896 }
4897
Mark Lamourinec2247f02012-01-04 13:02:01 -05004898 return 0;
4899}
4900
Willy Tarreau610ecce2010-01-04 21:15:02 +01004901/* Terminate current transaction and prepare a new one. This is very tricky
4902 * right now but it works.
4903 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004904void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004905{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004906 int prev_status = s->txn->status;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004907 struct proxy *fe = strm_sess(s)->fe;
Willy Tarreau068621e2013-12-23 15:11:25 +01004908
Willy Tarreau610ecce2010-01-04 21:15:02 +01004909 /* FIXME: We need a more portable way of releasing a backend's and a
4910 * server's connections. We need a safer way to reinitialize buffer
4911 * flags. We also need a more accurate method for computing per-request
4912 * data.
4913 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004914
Willy Tarreau4213a112013-12-15 10:25:42 +01004915 /* unless we're doing keep-alive, we want to quickly close the connection
4916 * to the server.
4917 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004918 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004919 !si_conn_ready(&s->si[1])) {
4920 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4921 si_shutr(&s->si[1]);
4922 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004923 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004924
Willy Tarreaue7dff022015-04-03 01:14:29 +02004925 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004926 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004927 if (unlikely(s->srv_conn))
4928 sess_change_server(s, NULL);
4929 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004930
4931 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004932 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004933
Willy Tarreaueee5b512015-04-03 23:46:31 +02004934 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004935 int n;
4936
Willy Tarreaueee5b512015-04-03 23:46:31 +02004937 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004938 if (n < 1 || n > 5)
4939 n = 0;
4940
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004941 if (fe->mode == PR_MODE_HTTP) {
4942 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004943 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004944 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004945 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004946 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004947 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004948 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004949 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004950 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004951 s->be->be_counters.p.http.comp_rsp++;
4952 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004953 }
4954
4955 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004956 s->logs.bytes_in -= s->req.buf->i;
4957 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004958
4959 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004960 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004961 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004962 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963 s->do_log(s);
4964 }
4965
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004966 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004967 stream_stop_content_counters(s);
4968 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004969
Willy Tarreau610ecce2010-01-04 21:15:02 +01004970 s->logs.accept_date = date; /* user-visible date for logging */
4971 s->logs.tv_accept = now; /* corrected date for internal use */
4972 tv_zero(&s->logs.tv_request);
4973 s->logs.t_queue = -1;
4974 s->logs.t_connect = -1;
4975 s->logs.t_data = -1;
4976 s->logs.t_close = 0;
4977 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4978 s->logs.srv_queue_size = 0; /* we will get this number soon */
4979
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004980 s->logs.bytes_in = s->req.total = s->req.buf->i;
4981 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004982
4983 if (s->pend_pos)
4984 pendconn_free(s->pend_pos);
4985
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004986 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004987 if (s->flags & SF_CURR_SESS) {
4988 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004989 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004991 if (may_dequeue_tasks(objt_server(s->target), s->be))
4992 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993 }
4994
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004995 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004996
Willy Tarreau4213a112013-12-15 10:25:42 +01004997 /* only release our endpoint if we don't intend to reuse the
4998 * connection.
4999 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005000 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005001 !si_conn_ready(&s->si[1])) {
5002 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005003 }
5004
Willy Tarreau350f4872014-11-28 14:42:25 +01005005 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5006 s->si[1].err_type = SI_ET_NONE;
5007 s->si[1].conn_retries = 0; /* used for logging too */
5008 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005009 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 +01005010 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);
5011 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 +02005012 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5013 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5014 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005015
Willy Tarreaueee5b512015-04-03 23:46:31 +02005016 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005017 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005018 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005019
5020 if (prev_status == 401 || prev_status == 407) {
5021 /* In HTTP keep-alive mode, if we receive a 401, we still have
5022 * a chance of being able to send the visitor again to the same
5023 * server over the same connection. This is required by some
5024 * broken protocols such as NTLM, and anyway whenever there is
5025 * an opportunity for sending the challenge to the proper place,
5026 * it's better to do it (at least it helps with debugging).
5027 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005028 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01005029 }
5030
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005031 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005032 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005033
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005034 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005035 s->req.flags |= CF_NEVER_WAIT;
5036 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005037 }
5038
Willy Tarreau610ecce2010-01-04 21:15:02 +01005039 /* if the request buffer is not empty, it means we're
5040 * about to process another request, so send pending
5041 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005042 * Just don't do this if the buffer is close to be full,
5043 * because the request will wait for it to flush a little
5044 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005045 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005046 if (s->req.buf->i) {
5047 if (s->res.buf->o &&
5048 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5049 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5050 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005051 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005052
5053 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005054 channel_auto_read(&s->req);
5055 channel_auto_close(&s->req);
5056 channel_auto_read(&s->res);
5057 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005058
Willy Tarreau27375622013-12-17 00:00:28 +01005059 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005060 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005061
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005062 s->req.analysers = strm_sess(s)->listener->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005063 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005064}
5065
5066
5067/* This function updates the request state machine according to the response
5068 * state machine and buffer flags. It returns 1 if it changes anything (flag
5069 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5070 * it is only used to find when a request/response couple is complete. Both
5071 * this function and its equivalent should loop until both return zero. It
5072 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5073 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005074int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005075{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005076 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005077 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005078 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005079 unsigned int old_state = txn->req.msg_state;
5080
Willy Tarreau610ecce2010-01-04 21:15:02 +01005081 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5082 return 0;
5083
5084 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005085 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005086 * We can shut the read side unless we want to abort_on_close,
5087 * or we have a POST request. The issue with POST requests is
5088 * that some browsers still send a CRLF after the request, and
5089 * this CRLF must be read so that it does not remain in the kernel
5090 * buffers, otherwise a close could cause an RST on some systems
5091 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005092 * Note that if we're using keep-alive on the client side, we'd
5093 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005094 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005095 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005096 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005097 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5098 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5099 !(s->be->options & PR_O_ABRT_CLOSE) &&
5100 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005101 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005102
Willy Tarreau40f151a2012-12-20 12:10:09 +01005103 /* if the server closes the connection, we want to immediately react
5104 * and close the socket to save packets and syscalls.
5105 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005106 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005107
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5109 goto wait_other_side;
5110
5111 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5112 /* The server has not finished to respond, so we
5113 * don't want to move in order not to upset it.
5114 */
5115 goto wait_other_side;
5116 }
5117
5118 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5119 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005120 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005122 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005123 goto wait_other_side;
5124 }
5125
5126 /* When we get here, it means that both the request and the
5127 * response have finished receiving. Depending on the connection
5128 * mode, we'll have to wait for the last bytes to leave in either
5129 * direction, and sometimes for a close to be effective.
5130 */
5131
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005132 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5133 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005134 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5135 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005136 }
5137 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5138 /* Option forceclose is set, or either side wants to close,
5139 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005140 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005141 * once both states are CLOSED.
5142 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005143 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5144 channel_shutr_now(chn);
5145 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005146 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005147 }
5148 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005149 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5150 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005151 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005152 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5153 channel_auto_read(chn);
5154 txn->req.msg_state = HTTP_MSG_TUNNEL;
5155 chn->flags |= CF_NEVER_WAIT;
5156 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005157 }
5158
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005159 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005160 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005161 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005162
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005163 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164 txn->req.msg_state = HTTP_MSG_CLOSING;
5165 goto http_msg_closing;
5166 }
5167 else {
5168 txn->req.msg_state = HTTP_MSG_CLOSED;
5169 goto http_msg_closed;
5170 }
5171 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005172 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005173 }
5174
5175 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5176 http_msg_closing:
5177 /* nothing else to forward, just waiting for the output buffer
5178 * to be empty and for the shutw_now to take effect.
5179 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005180 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005181 txn->req.msg_state = HTTP_MSG_CLOSED;
5182 goto http_msg_closed;
5183 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005184 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005185 txn->req.msg_state = HTTP_MSG_ERROR;
5186 goto wait_other_side;
5187 }
5188 }
5189
5190 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5191 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005192 /* see above in MSG_DONE why we only do this in these states */
5193 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5194 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5195 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005196 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005197 goto wait_other_side;
5198 }
5199
5200 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005201 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202}
5203
5204
5205/* This function updates the response state machine according to the request
5206 * state machine and buffer flags. It returns 1 if it changes anything (flag
5207 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5208 * it is only used to find when a request/response couple is complete. Both
5209 * this function and its equivalent should loop until both return zero. It
5210 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5211 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005212int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005213{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005214 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005215 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005216 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005217 unsigned int old_state = txn->rsp.msg_state;
5218
Willy Tarreau610ecce2010-01-04 21:15:02 +01005219 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5220 return 0;
5221
5222 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5223 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005224 * still monitor the server connection for a possible close
5225 * while the request is being uploaded, so we don't disable
5226 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005227 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005228 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005229
5230 if (txn->req.msg_state == HTTP_MSG_ERROR)
5231 goto wait_other_side;
5232
5233 if (txn->req.msg_state < HTTP_MSG_DONE) {
5234 /* The client seems to still be sending data, probably
5235 * because we got an error response during an upload.
5236 * We have the choice of either breaking the connection
5237 * or letting it pass through. Let's do the later.
5238 */
5239 goto wait_other_side;
5240 }
5241
5242 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5243 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005244 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005245 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005246 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005247 goto wait_other_side;
5248 }
5249
5250 /* When we get here, it means that both the request and the
5251 * response have finished receiving. Depending on the connection
5252 * mode, we'll have to wait for the last bytes to leave in either
5253 * direction, and sometimes for a close to be effective.
5254 */
5255
5256 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5257 /* Server-close mode : shut read and wait for the request
5258 * side to close its output buffer. The caller will detect
5259 * when we're in DONE and the other is in CLOSED and will
5260 * catch that for the final cleanup.
5261 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005262 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5263 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005264 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005265 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5266 /* Option forceclose is set, or either side wants to close,
5267 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005268 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005269 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005270 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005271 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5272 channel_shutr_now(chn);
5273 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005274 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005275 }
5276 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005277 /* The last possible modes are keep-alive and tunnel. Tunnel will
5278 * need to forward remaining data. Keep-alive will need to monitor
5279 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005280 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005281 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005282 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005283 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5284 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005285 }
5286
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005287 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005288 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005289 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005290 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5291 goto http_msg_closing;
5292 }
5293 else {
5294 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5295 goto http_msg_closed;
5296 }
5297 }
5298 goto wait_other_side;
5299 }
5300
5301 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5302 http_msg_closing:
5303 /* nothing else to forward, just waiting for the output buffer
5304 * to be empty and for the shutw_now to take effect.
5305 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005306 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005307 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5308 goto http_msg_closed;
5309 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005310 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005311 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005312 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005313 if (objt_server(s->target))
5314 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 goto wait_other_side;
5316 }
5317 }
5318
5319 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5320 http_msg_closed:
5321 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005322 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005323 channel_auto_close(chn);
5324 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005325 goto wait_other_side;
5326 }
5327
5328 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005329 /* We force the response to leave immediately if we're waiting for the
5330 * other side, since there is no pending shutdown to push it out.
5331 */
5332 if (!channel_is_empty(chn))
5333 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005334 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005335}
5336
5337
5338/* Resync the request and response state machines. Return 1 if either state
5339 * changes.
5340 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005341int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005343 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005344 int old_req_state = txn->req.msg_state;
5345 int old_res_state = txn->rsp.msg_state;
5346
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 http_sync_req_state(s);
5348 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 if (!http_sync_res_state(s))
5350 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005351 if (!http_sync_req_state(s))
5352 break;
5353 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005354
Willy Tarreau610ecce2010-01-04 21:15:02 +01005355 /* OK, both state machines agree on a compatible state.
5356 * There are a few cases we're interested in :
5357 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5358 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5359 * directions, so let's simply disable both analysers.
5360 * - HTTP_MSG_CLOSED on the response only means we must abort the
5361 * request.
5362 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5363 * with server-close mode means we've completed one request and we
5364 * must re-initialize the server connection.
5365 */
5366
5367 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5368 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5369 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5370 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005371 s->req.analysers = 0;
5372 channel_auto_close(&s->req);
5373 channel_auto_read(&s->req);
5374 s->res.analysers = 0;
5375 channel_auto_close(&s->res);
5376 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005377 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005378 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005379 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005380 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005381 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005382 s->res.analysers = 0;
5383 channel_auto_close(&s->res);
5384 channel_auto_read(&s->res);
5385 s->req.analysers = 0;
5386 channel_abort(&s->req);
5387 channel_auto_close(&s->req);
5388 channel_auto_read(&s->req);
5389 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005390 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005391 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5392 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005393 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005394 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5395 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5396 /* server-close/keep-alive: terminate this transaction,
5397 * possibly killing the server connection and reinitialize
5398 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005399 */
5400 http_end_txn_clean_session(s);
5401 }
5402
Willy Tarreau610ecce2010-01-04 21:15:02 +01005403 return txn->req.msg_state != old_req_state ||
5404 txn->rsp.msg_state != old_res_state;
5405}
5406
Willy Tarreaud98cf932009-12-27 22:54:55 +01005407/* This function is an analyser which forwards request body (including chunk
5408 * sizes if any). It is called as soon as we must forward, even if we forward
5409 * zero byte. The only situation where it must not be called is when we're in
5410 * tunnel mode and we want to forward till the close. It's used both to forward
5411 * remaining data and to resync after end of body. It expects the msg_state to
5412 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005413 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005414 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005415 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005416 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005417int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005418{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005419 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005420 struct http_txn *txn = s->txn;
5421 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005422
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005423 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5424 return 0;
5425
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005426 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005427 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005428 /* Output closed while we were sending data. We must abort and
5429 * wake the other side up.
5430 */
5431 msg->msg_state = HTTP_MSG_ERROR;
5432 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005433 return 1;
5434 }
5435
Willy Tarreaud98cf932009-12-27 22:54:55 +01005436 /* Note that we don't have to send 100-continue back because we don't
5437 * need the data to complete our job, and it's up to the server to
5438 * decide whether to return 100, 417 or anything else in return of
5439 * an "Expect: 100-continue" header.
5440 */
5441
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005442 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005443 /* we have msg->sov which points to the first byte of message
5444 * body, and req->buf.p still points to the beginning of the
5445 * message. We forward the headers now, as we don't need them
5446 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005447 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005448 b_adv(req->buf, msg->sov);
5449 msg->next -= msg->sov;
5450 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005451
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005452 /* The previous analysers guarantee that the state is somewhere
5453 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5454 * msg->next are always correct.
5455 */
5456 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5457 if (msg->flags & HTTP_MSGF_TE_CHNK)
5458 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5459 else
5460 msg->msg_state = HTTP_MSG_DATA;
5461 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005462 }
5463
Willy Tarreau7ba23542014-04-17 21:50:00 +02005464 /* Some post-connect processing might want us to refrain from starting to
5465 * forward data. Currently, the only reason for this is "balance url_param"
5466 * whichs need to parse/process the request after we've enabled forwarding.
5467 */
5468 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005469 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005470 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005471 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005472 goto missing_data;
5473 }
5474 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5475 }
5476
Willy Tarreau80a92c02014-03-12 10:41:13 +01005477 /* in most states, we should abort in case of early close */
5478 channel_auto_close(req);
5479
Willy Tarreauefdf0942014-04-24 20:08:57 +02005480 if (req->to_forward) {
5481 /* We can't process the buffer's contents yet */
5482 req->flags |= CF_WAKE_WRITE;
5483 goto missing_data;
5484 }
5485
Willy Tarreaud98cf932009-12-27 22:54:55 +01005486 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005487 if (msg->msg_state == HTTP_MSG_DATA) {
5488 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005489 /* we may have some pending data starting at req->buf->p */
5490 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005491 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005492 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005493 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005494 msg->next += msg->chunk_len;
5495 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005496
5497 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005498 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005499 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005500 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005501 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005502 }
5503 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005504 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005505 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005506 * TRAILERS state.
5507 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005508 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005509
Willy Tarreau54d23df2012-10-25 19:04:45 +02005510 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005511 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005512 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005513 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005514 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005515 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005516 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005517 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005518 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005519 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005520 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005521 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005522 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005523
5524 if (ret == 0)
5525 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005526 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005527 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005528 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005529 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005530 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005531 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005532 /* we're in MSG_CHUNK_SIZE now */
5533 }
5534 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005535 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005536
5537 if (ret == 0)
5538 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005539 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005540 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005541 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005542 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005544 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005545 /* we're in HTTP_MSG_DONE now */
5546 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005547 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005548 int old_state = msg->msg_state;
5549
Willy Tarreau610ecce2010-01-04 21:15:02 +01005550 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005551
5552 /* we may have some pending data starting at req->buf->p
5553 * such as last chunk of data or trailers.
5554 */
5555 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005556 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005557 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005558 msg->next = 0;
5559
Willy Tarreau4fe41902010-06-07 22:27:41 +02005560 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005561 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5562 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005563 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005564 if (http_resync_states(s)) {
5565 /* some state changes occurred, maybe the analyser
5566 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005567 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005568 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005569 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005570 /* request errors are most likely due to
5571 * the server aborting the transfer.
5572 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005573 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005574 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005575 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005576 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005577 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005578 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005580 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005581
5582 /* If "option abortonclose" is set on the backend, we
5583 * want to monitor the client's connection and forward
5584 * any shutdown notification to the server, which will
5585 * decide whether to close or to go on processing the
5586 * request.
5587 */
5588 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005589 channel_auto_read(req);
5590 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005591 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005592 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005593 /* POST requests may require to read extra CRLF
5594 * sent by broken browsers and which could cause
5595 * an RST to be sent upon close on some systems
5596 * (eg: Linux).
5597 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005598 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005599 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005600
Willy Tarreau610ecce2010-01-04 21:15:02 +01005601 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005602 }
5603 }
5604
Willy Tarreaud98cf932009-12-27 22:54:55 +01005605 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005606 /* we may have some pending data starting at req->buf->p */
5607 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005608 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005609 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5610
Willy Tarreaubed410e2014-04-22 08:19:34 +02005611 msg->next = 0;
5612 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5613
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005614 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005615 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005616 if (!(s->flags & SF_ERR_MASK))
5617 s->flags |= SF_ERR_CLICL;
5618 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005619 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005620 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005621 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005622 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005623 }
5624
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005625 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005626 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005627 if (objt_server(s->target))
5628 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005629
5630 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005631 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005632
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005633 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005634 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005635 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005636
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005637 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005638 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005639 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005640 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005641 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005642
Willy Tarreau5c620922011-05-11 19:56:11 +02005643 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005644 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005645 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005646 * modes are already handled by the stream sock layer. We must not do
5647 * this in content-length mode because it could present the MSG_MORE
5648 * flag with the last block of forwarded data, which would cause an
5649 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005650 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005651 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005652 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005653
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005654 return 0;
5655
Willy Tarreaud98cf932009-12-27 22:54:55 +01005656 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005657 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005658 if (sess->listener->counters)
5659 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005660
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005661 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005662 /* we may have some pending data starting at req->buf->p */
5663 b_adv(req->buf, msg->next);
5664 msg->next = 0;
5665
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005666 txn->req.msg_state = HTTP_MSG_ERROR;
5667 if (txn->status) {
5668 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005669 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005670 } else {
5671 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005672 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005673 }
5674 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005675 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005676
Willy Tarreaue7dff022015-04-03 01:14:29 +02005677 if (!(s->flags & SF_ERR_MASK))
5678 s->flags |= SF_ERR_PRXCOND;
5679 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005680 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005681 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005682 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005683 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005684 }
5685 return 0;
5686
5687 aborted_xfer:
5688 txn->req.msg_state = HTTP_MSG_ERROR;
5689 if (txn->status) {
5690 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005691 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005692 } else {
5693 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005694 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005695 }
5696 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005697 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005698
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005699 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005700 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005701 if (objt_server(s->target))
5702 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005703
Willy Tarreaue7dff022015-04-03 01:14:29 +02005704 if (!(s->flags & SF_ERR_MASK))
5705 s->flags |= SF_ERR_SRVCL;
5706 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005707 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005708 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005709 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005710 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005711 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005712 return 0;
5713}
5714
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005715/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5716 * processing can continue on next analysers, or zero if it either needs more
5717 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005718 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005719 * when it has nothing left to do, and may remove any analyser when it wants to
5720 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005721 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005722int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005723{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005724 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005725 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005726 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005727 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005728 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005729 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005730 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005731
Willy Tarreau87b09662015-04-03 00:22:06 +02005732 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 +02005733 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005734 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005735 rep,
5736 rep->rex, rep->wex,
5737 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005738 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005739 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005740
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005741 /*
5742 * Now parse the partial (or complete) lines.
5743 * We will check the response syntax, and also join multi-line
5744 * headers. An index of all the lines will be elaborated while
5745 * parsing.
5746 *
5747 * For the parsing, we use a 28 states FSM.
5748 *
5749 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005750 * rep->buf->p = beginning of response
5751 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5752 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005753 * msg->eol = end of current header or line (LF or CRLF)
5754 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005755 */
5756
Willy Tarreau628c40c2014-04-24 19:11:26 +02005757 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005758 /* There's a protected area at the end of the buffer for rewriting
5759 * purposes. We don't want to start to parse the request if the
5760 * protected area is affected, because we may have to move processed
5761 * data later, which is much more complicated.
5762 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005763 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005764 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005765 /* some data has still not left the buffer, wake us once that's done */
5766 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5767 goto abort_response;
5768 channel_dont_close(rep);
5769 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005770 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005771 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005772 }
5773
Willy Tarreau379357a2013-06-08 12:55:46 +02005774 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5775 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5776 buffer_slow_realign(rep->buf);
5777
Willy Tarreau9b28e032012-10-12 23:49:43 +02005778 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005779 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005780 }
5781
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005782 /* 1: we might have to print this header in debug mode */
5783 if (unlikely((global.mode & MODE_DEBUG) &&
5784 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005785 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005786 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005787
Willy Tarreau9b28e032012-10-12 23:49:43 +02005788 sol = rep->buf->p;
5789 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005790 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005791
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005792 sol += hdr_idx_first_pos(&txn->hdr_idx);
5793 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005794
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005795 while (cur_idx) {
5796 eol = sol + txn->hdr_idx.v[cur_idx].len;
5797 debug_hdr("srvhdr", s, sol, eol);
5798 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5799 cur_idx = txn->hdr_idx.v[cur_idx].next;
5800 }
5801 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005802
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005803 /*
5804 * Now we quickly check if we have found a full valid response.
5805 * If not so, we check the FD and buffer states before leaving.
5806 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005807 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005808 * responses are checked first.
5809 *
5810 * Depending on whether the client is still there or not, we
5811 * may send an error response back or not. Note that normally
5812 * we should only check for HTTP status there, and check I/O
5813 * errors somewhere else.
5814 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005815
Willy Tarreau655dce92009-11-08 13:10:58 +01005816 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005817 /* Invalid response */
5818 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5819 /* we detected a parsing error. We want to archive this response
5820 * in the dedicated proxy area for later troubleshooting.
5821 */
5822 hdr_response_bad:
5823 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005824 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005825
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005826 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005827 if (objt_server(s->target)) {
5828 objt_server(s->target)->counters.failed_resp++;
5829 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005830 }
Willy Tarreau64648412010-03-05 10:41:54 +01005831 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005832 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005833 rep->analysers = 0;
5834 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005835 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005836 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005837 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005838
Willy Tarreaue7dff022015-04-03 01:14:29 +02005839 if (!(s->flags & SF_ERR_MASK))
5840 s->flags |= SF_ERR_PRXCOND;
5841 if (!(s->flags & SF_FINST_MASK))
5842 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005843
5844 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005845 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005846
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005847 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005848 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005849 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005850 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005851 goto hdr_response_bad;
5852 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005853
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005854 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005855 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005856 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005857 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005858 else if (txn->flags & TX_NOT_FIRST)
5859 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005860
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005861 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005862 if (objt_server(s->target)) {
5863 objt_server(s->target)->counters.failed_resp++;
5864 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005865 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005866
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005867 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005868 rep->analysers = 0;
5869 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005870 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005871 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005872 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005873
Willy Tarreaue7dff022015-04-03 01:14:29 +02005874 if (!(s->flags & SF_ERR_MASK))
5875 s->flags |= SF_ERR_SRVCL;
5876 if (!(s->flags & SF_FINST_MASK))
5877 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005878 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005879 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005880
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005881 /* read timeout : return a 504 to the client. */
5882 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005883 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005884 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005885 else if (txn->flags & TX_NOT_FIRST)
5886 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005887
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005888 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005889 if (objt_server(s->target)) {
5890 objt_server(s->target)->counters.failed_resp++;
5891 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005892 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005893
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005894 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005895 rep->analysers = 0;
5896 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005897 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005898 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005899 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005900
Willy Tarreaue7dff022015-04-03 01:14:29 +02005901 if (!(s->flags & SF_ERR_MASK))
5902 s->flags |= SF_ERR_SRVTO;
5903 if (!(s->flags & SF_FINST_MASK))
5904 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005905 return 0;
5906 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005907
Willy Tarreauf003d372012-11-26 13:35:37 +01005908 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005909 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005910 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005911 s->be->be_counters.cli_aborts++;
5912 if (objt_server(s->target))
5913 objt_server(s->target)->counters.cli_aborts++;
5914
5915 rep->analysers = 0;
5916 channel_auto_close(rep);
5917
5918 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005919 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005920 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005921
Willy Tarreaue7dff022015-04-03 01:14:29 +02005922 if (!(s->flags & SF_ERR_MASK))
5923 s->flags |= SF_ERR_CLICL;
5924 if (!(s->flags & SF_FINST_MASK))
5925 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005926
Willy Tarreau87b09662015-04-03 00:22:06 +02005927 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005928 return 0;
5929 }
5930
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005931 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005932 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005933 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005934 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005935 else if (txn->flags & TX_NOT_FIRST)
5936 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005937
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005938 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005939 if (objt_server(s->target)) {
5940 objt_server(s->target)->counters.failed_resp++;
5941 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005942 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005943
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005944 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005945 rep->analysers = 0;
5946 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005947 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005948 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005949 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005950
Willy Tarreaue7dff022015-04-03 01:14:29 +02005951 if (!(s->flags & SF_ERR_MASK))
5952 s->flags |= SF_ERR_SRVCL;
5953 if (!(s->flags & SF_FINST_MASK))
5954 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005955 return 0;
5956 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005957
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005958 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005959 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005960 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005961 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005962 else if (txn->flags & TX_NOT_FIRST)
5963 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005964
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005965 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005966 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005967 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005968
Willy Tarreaue7dff022015-04-03 01:14:29 +02005969 if (!(s->flags & SF_ERR_MASK))
5970 s->flags |= SF_ERR_CLICL;
5971 if (!(s->flags & SF_FINST_MASK))
5972 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005973
Willy Tarreau87b09662015-04-03 00:22:06 +02005974 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005975 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005976 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005977
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005978 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005979 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005980 return 0;
5981 }
5982
5983 /* More interesting part now : we know that we have a complete
5984 * response which at least looks like HTTP. We have an indicator
5985 * of each header's length, so we can parse them quickly.
5986 */
5987
5988 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005989 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005990
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005991 /*
5992 * 1: get the status code
5993 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005994 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995 if (n < 1 || n > 5)
5996 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005997 /* when the client triggers a 4xx from the server, it's most often due
5998 * to a missing object or permission. These events should be tracked
5999 * because if they happen often, it may indicate a brute force or a
6000 * vulnerability scan.
6001 */
6002 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006003 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006004
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006005 if (objt_server(s->target))
6006 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007
Willy Tarreau5b154472009-12-21 20:11:07 +01006008 /* check if the response is HTTP/1.1 or above */
6009 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006010 ((rep->buf->p[5] > '1') ||
6011 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006012 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006013
6014 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006015 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 +01006016
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006017 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006018 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006019
Willy Tarreau9b28e032012-10-12 23:49:43 +02006020 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006021
Willy Tarreau39650402010-03-15 19:44:39 +01006022 /* Adjust server's health based on status code. Note: status codes 501
6023 * and 505 are triggered on demand by client request, so we must not
6024 * count them as server failures.
6025 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006026 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006027 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006028 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006029 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006030 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006031 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006032
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006033 /*
6034 * 2: check for cacheability.
6035 */
6036
6037 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006038 case 100:
6039 /*
6040 * We may be facing a 100-continue response, in which case this
6041 * is not the right response, and we're waiting for the next one.
6042 * Let's allow this response to go to the client and wait for the
6043 * next one.
6044 */
6045 hdr_idx_init(&txn->hdr_idx);
6046 msg->next -= channel_forward(rep, msg->next);
6047 msg->msg_state = HTTP_MSG_RPBEFORE;
6048 txn->status = 0;
6049 s->logs.t_data = -1; /* was not a response yet */
6050 goto next_one;
6051
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006052 case 200:
6053 case 203:
6054 case 206:
6055 case 300:
6056 case 301:
6057 case 410:
6058 /* RFC2616 @13.4:
6059 * "A response received with a status code of
6060 * 200, 203, 206, 300, 301 or 410 MAY be stored
6061 * by a cache (...) unless a cache-control
6062 * directive prohibits caching."
6063 *
6064 * RFC2616 @9.5: POST method :
6065 * "Responses to this method are not cacheable,
6066 * unless the response includes appropriate
6067 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006068 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006069 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006070 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006071 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6072 break;
6073 default:
6074 break;
6075 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006076
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006077 /*
6078 * 3: we may need to capture headers
6079 */
6080 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006081 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006082 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006083 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006084
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006085 /* 4: determine the transfer-length.
6086 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6087 * the presence of a message-body in a RESPONSE and its transfer length
6088 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006089 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006090 * All responses to the HEAD request method MUST NOT include a
6091 * message-body, even though the presence of entity-header fields
6092 * might lead one to believe they do. All 1xx (informational), 204
6093 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6094 * message-body. All other responses do include a message-body,
6095 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006096 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006097 * 1. Any response which "MUST NOT" include a message-body (such as the
6098 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6099 * always terminated by the first empty line after the header fields,
6100 * regardless of the entity-header fields present in the message.
6101 *
6102 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6103 * the "chunked" transfer-coding (Section 6.2) is used, the
6104 * transfer-length is defined by the use of this transfer-coding.
6105 * If a Transfer-Encoding header field is present and the "chunked"
6106 * transfer-coding is not present, the transfer-length is defined by
6107 * the sender closing the connection.
6108 *
6109 * 3. If a Content-Length header field is present, its decimal value in
6110 * OCTETs represents both the entity-length and the transfer-length.
6111 * If a message is received with both a Transfer-Encoding header
6112 * field and a Content-Length header field, the latter MUST be ignored.
6113 *
6114 * 4. If the message uses the media type "multipart/byteranges", and
6115 * the transfer-length is not otherwise specified, then this self-
6116 * delimiting media type defines the transfer-length. This media
6117 * type MUST NOT be used unless the sender knows that the recipient
6118 * can parse it; the presence in a request of a Range header with
6119 * multiple byte-range specifiers from a 1.1 client implies that the
6120 * client can parse multipart/byteranges responses.
6121 *
6122 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006123 */
6124
6125 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006126 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006127 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006128 * FIXME: should we parse anyway and return an error on chunked encoding ?
6129 */
6130 if (txn->meth == HTTP_METH_HEAD ||
6131 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006132 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006133 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006134 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006135 goto skip_content_length;
6136 }
6137
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006138 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006139 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006140 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006141 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006142 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006143 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6144 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006145 /* bad transfer-encoding (chunked followed by something else) */
6146 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006147 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006148 break;
6149 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006150 }
6151
6152 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6153 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006154 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006155 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006156 signed long long cl;
6157
Willy Tarreauad14f752011-09-02 20:33:27 +02006158 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006159 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006160 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006161 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006162
Willy Tarreauad14f752011-09-02 20:33:27 +02006163 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006164 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006165 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006166 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006167
Willy Tarreauad14f752011-09-02 20:33:27 +02006168 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006169 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006170 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006171 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006172
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006173 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006174 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006175 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006176 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006177
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006178 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006179 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006180 }
6181
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006182 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006183 select_compression_response_header(s, rep->buf);
6184
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006185skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006186 /* Now we have to check if we need to modify the Connection header.
6187 * This is more difficult on the response than it is on the request,
6188 * because we can have two different HTTP versions and we don't know
6189 * how the client will interprete a response. For instance, let's say
6190 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6191 * HTTP/1.1 response without any header. Maybe it will bound itself to
6192 * HTTP/1.0 because it only knows about it, and will consider the lack
6193 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6194 * the lack of header as a keep-alive. Thus we will use two flags
6195 * indicating how a request MAY be understood by the client. In case
6196 * of multiple possibilities, we'll fix the header to be explicit. If
6197 * ambiguous cases such as both close and keepalive are seen, then we
6198 * will fall back to explicit close. Note that we won't take risks with
6199 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006200 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006201 */
6202
Willy Tarreaudc008c52010-02-01 16:20:08 +01006203 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6204 txn->status == 101)) {
6205 /* Either we've established an explicit tunnel, or we're
6206 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006207 * to understand the next protocols. We have to switch to tunnel
6208 * mode, so that we transfer the request and responses then let
6209 * this protocol pass unmodified. When we later implement specific
6210 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006211 * header which contains information about that protocol for
6212 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006213 */
6214 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6215 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006216 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6217 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006218 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006219 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006220 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006221
Willy Tarreau70dffda2014-01-30 03:07:23 +01006222 /* this situation happens when combining pretend-keepalive with httpclose. */
6223 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006224 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006225 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006226 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6227
Willy Tarreau60466522010-01-18 19:08:45 +01006228 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006229 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006230 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6231 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006232
Willy Tarreau60466522010-01-18 19:08:45 +01006233 /* now adjust header transformations depending on current state */
6234 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6235 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6236 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006237 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006238 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006239 }
Willy Tarreau60466522010-01-18 19:08:45 +01006240 else { /* SCL / KAL */
6241 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006242 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006243 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006244 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006245
Willy Tarreau60466522010-01-18 19:08:45 +01006246 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006247 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006248
Willy Tarreau60466522010-01-18 19:08:45 +01006249 /* Some keep-alive responses are converted to Server-close if
6250 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006251 */
Willy Tarreau60466522010-01-18 19:08:45 +01006252 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6253 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006254 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006255 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006256 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006257 }
6258
Willy Tarreau7959a552013-09-23 16:44:27 +02006259 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006260 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006261
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006262 /* end of job, return OK */
6263 rep->analysers &= ~an_bit;
6264 rep->analyse_exp = TICK_ETERNITY;
6265 channel_auto_close(rep);
6266 return 1;
6267
6268 abort_keep_alive:
6269 /* A keep-alive request to the server failed on a network error.
6270 * The client is required to retry. We need to close without returning
6271 * any other information so that the client retries.
6272 */
6273 txn->status = 0;
6274 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006275 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006276 channel_auto_close(rep);
6277 s->logs.logwait = 0;
6278 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006279 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006280 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006281 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006282 return 0;
6283}
6284
6285/* This function performs all the processing enabled for the current response.
6286 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006287 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006288 * other functions. It works like process_request (see indications above).
6289 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006290int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006291{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006292 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006293 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006294 struct http_msg *msg = &txn->rsp;
6295 struct proxy *cur_proxy;
6296 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006297 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006298
Willy Tarreau87b09662015-04-03 00:22:06 +02006299 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 +02006300 now_ms, __FUNCTION__,
6301 s,
6302 rep,
6303 rep->rex, rep->wex,
6304 rep->flags,
6305 rep->buf->i,
6306 rep->analysers);
6307
6308 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6309 return 0;
6310
Willy Tarreau70730dd2014-04-24 18:06:27 +02006311 /* The stats applet needs to adjust the Connection header but we don't
6312 * apply any filter there.
6313 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006314 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6315 rep->analysers &= ~an_bit;
6316 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006317 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006318 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006319
Willy Tarreau58975672014-04-24 21:13:57 +02006320 /*
6321 * We will have to evaluate the filters.
6322 * As opposed to version 1.2, now they will be evaluated in the
6323 * filters order and not in the header order. This means that
6324 * each filter has to be validated among all headers.
6325 *
6326 * Filters are tried with ->be first, then with ->fe if it is
6327 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006328 *
6329 * Maybe we are in resume condiion. In this case I choose the
6330 * "struct proxy" which contains the rule list matching the resume
6331 * pointer. If none of theses "struct proxy" match, I initialise
6332 * the process with the first one.
6333 *
6334 * In fact, I check only correspondance betwwen the current list
6335 * pointer and the ->fe rule list. If it doesn't match, I initialize
6336 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006337 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006338 if (s->current_rule_list == &sess->fe->http_res_rules)
6339 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006340 else
6341 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006342 while (1) {
6343 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006344
Willy Tarreau58975672014-04-24 21:13:57 +02006345 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006346 if (ret == HTTP_RULE_RES_CONT)
6347 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006348
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006349 /* we need to be called again. */
6350 if (ret == HTTP_RULE_RES_YIELD) {
6351 channel_dont_close(rep);
6352 return 0;
6353 }
6354
Willy Tarreau58975672014-04-24 21:13:57 +02006355 /* try headers filters */
6356 if (rule_set->rsp_exp != NULL) {
6357 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6358 return_bad_resp:
6359 if (objt_server(s->target)) {
6360 objt_server(s->target)->counters.failed_resp++;
6361 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006362 }
Willy Tarreau58975672014-04-24 21:13:57 +02006363 s->be->be_counters.failed_resp++;
6364 return_srv_prx_502:
6365 rep->analysers = 0;
6366 txn->status = 502;
6367 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006368 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006369 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006370 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006371 if (!(s->flags & SF_ERR_MASK))
6372 s->flags |= SF_ERR_PRXCOND;
6373 if (!(s->flags & SF_FINST_MASK))
6374 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006375 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006376 }
Willy Tarreau58975672014-04-24 21:13:57 +02006377 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006378
Willy Tarreau58975672014-04-24 21:13:57 +02006379 /* has the response been denied ? */
6380 if (txn->flags & TX_SVDENY) {
6381 if (objt_server(s->target))
6382 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006383
Willy Tarreau58975672014-04-24 21:13:57 +02006384 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006385 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006386 if (sess->listener->counters)
6387 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006388
Willy Tarreau58975672014-04-24 21:13:57 +02006389 goto return_srv_prx_502;
6390 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006391
Willy Tarreau58975672014-04-24 21:13:57 +02006392 /* add response headers from the rule sets in the same order */
6393 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006394 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006395 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006396 if (wl->cond) {
Willy Tarreau15e91e12015-04-04 00:52:09 +02006397 int ret = acl_exec_cond(wl->cond, px, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006398 ret = acl_pass(ret);
6399 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6400 ret = !ret;
6401 if (!ret)
6402 continue;
6403 }
6404 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6405 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006406 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006407
Willy Tarreau58975672014-04-24 21:13:57 +02006408 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006409 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006410 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006411 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006412 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006413
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006414 /* After this point, this anayzer can't return yield, so we can
6415 * remove the bit corresponding to this analyzer from the list.
6416 *
6417 * Note that the intermediate returns and goto found previously
6418 * reset the analyzers.
6419 */
6420 rep->analysers &= ~an_bit;
6421 rep->analyse_exp = TICK_ETERNITY;
6422
Willy Tarreau58975672014-04-24 21:13:57 +02006423 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006424 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006425 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006426
Willy Tarreau58975672014-04-24 21:13:57 +02006427 /*
6428 * Now check for a server cookie.
6429 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006430 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006431 (s->be->options & PR_O_CHK_CACHE))
6432 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006433
Willy Tarreau58975672014-04-24 21:13:57 +02006434 /*
6435 * Check for cache-control or pragma headers if required.
6436 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006437 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 +02006438 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006439
Willy Tarreau58975672014-04-24 21:13:57 +02006440 /*
6441 * Add server cookie in the response if needed
6442 */
6443 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6444 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006445 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006446 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6447 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6448 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6449 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6450 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006451 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006452 /* the server is known, it's not the one the client requested, or the
6453 * cookie's last seen date needs to be refreshed. We have to
6454 * insert a set-cookie here, except if we want to insert only on POST
6455 * requests and this one isn't. Note that servers which don't have cookies
6456 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006457 */
Willy Tarreau58975672014-04-24 21:13:57 +02006458 if (!objt_server(s->target)->cookie) {
6459 chunk_printf(&trash,
6460 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6461 s->be->cookie_name);
6462 }
6463 else {
6464 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006465
Willy Tarreau58975672014-04-24 21:13:57 +02006466 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6467 /* emit last_date, which is mandatory */
6468 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6469 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6470 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006471
Willy Tarreau58975672014-04-24 21:13:57 +02006472 if (s->be->cookie_maxlife) {
6473 /* emit first_date, which is either the original one or
6474 * the current date.
6475 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006476 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006477 s30tob64(txn->cookie_first_date ?
6478 txn->cookie_first_date >> 2 :
6479 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006480 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006481 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006482 }
Willy Tarreau58975672014-04-24 21:13:57 +02006483 chunk_appendf(&trash, "; path=/");
6484 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006485
Willy Tarreau58975672014-04-24 21:13:57 +02006486 if (s->be->cookie_domain)
6487 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006488
Willy Tarreau58975672014-04-24 21:13:57 +02006489 if (s->be->ck_opts & PR_CK_HTTPONLY)
6490 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006491
Willy Tarreau58975672014-04-24 21:13:57 +02006492 if (s->be->ck_opts & PR_CK_SECURE)
6493 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006494
Willy Tarreau58975672014-04-24 21:13:57 +02006495 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6496 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006497
Willy Tarreau58975672014-04-24 21:13:57 +02006498 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006499 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006500 /* the server did not change, only the date was updated */
6501 txn->flags |= TX_SCK_UPDATED;
6502 else
6503 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006504
Willy Tarreau58975672014-04-24 21:13:57 +02006505 /* Here, we will tell an eventual cache on the client side that we don't
6506 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6507 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6508 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006509 */
Willy Tarreau58975672014-04-24 21:13:57 +02006510 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006511
Willy Tarreau58975672014-04-24 21:13:57 +02006512 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006513
Willy Tarreau58975672014-04-24 21:13:57 +02006514 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6515 "Cache-control: private", 22) < 0))
6516 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006517 }
Willy Tarreau58975672014-04-24 21:13:57 +02006518 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006519
Willy Tarreau58975672014-04-24 21:13:57 +02006520 /*
6521 * Check if result will be cacheable with a cookie.
6522 * We'll block the response if security checks have caught
6523 * nasty things such as a cacheable cookie.
6524 */
6525 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6526 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6527 (s->be->options & PR_O_CHK_CACHE)) {
6528 /* we're in presence of a cacheable response containing
6529 * a set-cookie header. We'll block it as requested by
6530 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006531 */
Willy Tarreau58975672014-04-24 21:13:57 +02006532 if (objt_server(s->target))
6533 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006534
Willy Tarreau58975672014-04-24 21:13:57 +02006535 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006536 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006537 if (sess->listener->counters)
6538 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006539
Willy Tarreau58975672014-04-24 21:13:57 +02006540 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6541 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6542 send_log(s->be, LOG_ALERT,
6543 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6544 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6545 goto return_srv_prx_502;
6546 }
Willy Tarreau03945942009-12-22 16:50:27 +01006547
Willy Tarreau70730dd2014-04-24 18:06:27 +02006548 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006549 /*
6550 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6551 * If an "Upgrade" token is found, the header is left untouched in order
6552 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006553 * if anything but "Upgrade" is present in the Connection header. We don't
6554 * want to touch any 101 response either since it's switching to another
6555 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006556 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006557 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006558 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006559 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006560 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6561 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006562
Willy Tarreau58975672014-04-24 21:13:57 +02006563 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6564 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6565 /* we want a keep-alive response here. Keep-alive header
6566 * required if either side is not 1.1.
6567 */
6568 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6569 want_flags |= TX_CON_KAL_SET;
6570 }
6571 else {
6572 /* we want a close response here. Close header required if
6573 * the server is 1.1, regardless of the client.
6574 */
6575 if (msg->flags & HTTP_MSGF_VER_11)
6576 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006577 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578
Willy Tarreau58975672014-04-24 21:13:57 +02006579 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6580 http_change_connection_header(txn, msg, want_flags);
6581 }
6582
6583 skip_header_mangling:
6584 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6585 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6586 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006587
Willy Tarreau58975672014-04-24 21:13:57 +02006588 /* if the user wants to log as soon as possible, without counting
6589 * bytes from the server, then this is the right moment. We have
6590 * to temporarily assign bytes_out to log what we currently have.
6591 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006592 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006593 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6594 s->logs.bytes_out = txn->rsp.eoh;
6595 s->do_log(s);
6596 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006597 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006598 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006599}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006600
Willy Tarreaud98cf932009-12-27 22:54:55 +01006601/* This function is an analyser which forwards response body (including chunk
6602 * sizes if any). It is called as soon as we must forward, even if we forward
6603 * zero byte. The only situation where it must not be called is when we're in
6604 * tunnel mode and we want to forward till the close. It's used both to forward
6605 * remaining data and to resync after end of body. It expects the msg_state to
6606 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006607 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006608 *
6609 * It is capable of compressing response data both in content-length mode and
6610 * in chunked mode. The state machines follows different flows depending on
6611 * whether content-length and chunked modes are used, since there are no
6612 * trailers in content-length :
6613 *
6614 * chk-mode cl-mode
6615 * ,----- BODY -----.
6616 * / \
6617 * V size > 0 V chk-mode
6618 * .--> SIZE -------------> DATA -------------> CRLF
6619 * | | size == 0 | last byte |
6620 * | v final crlf v inspected |
6621 * | TRAILERS -----------> DONE |
6622 * | |
6623 * `----------------------------------------------'
6624 *
6625 * Compression only happens in the DATA state, and must be flushed in final
6626 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6627 * is performed at once on final states for all bytes parsed, or when leaving
6628 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006629 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006630int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006631{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006632 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006633 struct http_txn *txn = s->txn;
6634 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006635 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006636 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006637 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006638
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006639 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6640 return 0;
6641
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006642 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006643 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006644 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006645 /* Output closed while we were sending data. We must abort and
6646 * wake the other side up.
6647 */
6648 msg->msg_state = HTTP_MSG_ERROR;
6649 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006650 return 1;
6651 }
6652
Willy Tarreau4fe41902010-06-07 22:27:41 +02006653 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006654 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006655
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006656 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006657 /* we have msg->sov which points to the first byte of message
6658 * body, and res->buf.p still points to the beginning of the
6659 * message. We forward the headers now, as we don't need them
6660 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006661 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006662 b_adv(res->buf, msg->sov);
6663 msg->next -= msg->sov;
6664 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006665
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006666 /* The previous analysers guarantee that the state is somewhere
6667 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6668 * msg->next are always correct.
6669 */
6670 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6671 if (msg->flags & HTTP_MSGF_TE_CHNK)
6672 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6673 else
6674 msg->msg_state = HTTP_MSG_DATA;
6675 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006676 }
6677
Willy Tarreauefdf0942014-04-24 20:08:57 +02006678 if (res->to_forward) {
6679 /* We can't process the buffer's contents yet */
6680 res->flags |= CF_WAKE_WRITE;
6681 goto missing_data;
6682 }
6683
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006684 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6685 /* We need a compression buffer in the DATA state to put the
6686 * output of compressed data, and in CRLF state to let the
6687 * TRAILERS state finish the job of removing the trailing CRLF.
6688 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006689 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006690 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006691 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006692 goto aborted_xfer; /* no memory */
6693 }
6694
6695 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006696 if (ret < 0) {
6697 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006698 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006699 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006700 compressing = 1;
6701 }
6702
Willy Tarreaud98cf932009-12-27 22:54:55 +01006703 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006704 switch (msg->msg_state - HTTP_MSG_DATA) {
6705 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006706 /* we may have some pending data starting at res->buf->p */
6707 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006708 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006709 if (ret < 0)
6710 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006711
Willy Tarreaud5a67832014-04-21 10:54:27 +02006712 if (msg->chunk_len) {
6713 /* input empty or output full */
6714 if (res->buf->i > msg->next)
6715 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006716 goto missing_data;
6717 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006718 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006719 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006720 if (msg->chunk_len > res->buf->i - msg->next) {
6721 /* output full */
6722 res->flags |= CF_WAKE_WRITE;
6723 goto missing_data;
6724 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006725 msg->next += msg->chunk_len;
6726 msg->chunk_len = 0;
6727 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006728
6729 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006730 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006731 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006732 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006733 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006734 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006735 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006736 /* fall through for HTTP_MSG_CHUNK_CRLF */
6737
6738 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6739 /* we want the CRLF after the data */
6740
6741 ret = http_skip_chunk_crlf(msg);
6742 if (ret == 0)
6743 goto missing_data;
6744 else if (ret < 0) {
6745 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006746 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006747 goto return_bad_res;
6748 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006749 /* we're in MSG_CHUNK_SIZE now, fall through */
6750
6751 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006752 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006753 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006754 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006755 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006756
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006757 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006758 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006759 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006760 else if (ret < 0) {
6761 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006762 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006763 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006764 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006765 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006766 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006767
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006768 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006769 if (unlikely(compressing)) {
6770 /* we need to flush output contents before syncing FSMs */
6771 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6772 compressing = 0;
6773 }
6774
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006775 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006776 if (ret == 0)
6777 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006778 else if (ret < 0) {
6779 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006780 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006781 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006782 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006783 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006784
6785 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006786 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006787 if (unlikely(compressing)) {
6788 /* we need to flush output contents before syncing FSMs */
6789 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6790 compressing = 0;
6791 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006792
Willy Tarreauc623c172014-04-18 09:53:50 +02006793 /* we may have some pending data starting at res->buf->p
6794 * such as a last chunk of data or trailers.
6795 */
6796 b_adv(res->buf, msg->next);
6797 msg->next = 0;
6798
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006799 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006800 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006801 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6802 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006803 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006804
Willy Tarreau610ecce2010-01-04 21:15:02 +01006805 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006806 /* some state changes occurred, maybe the analyser
6807 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006808 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006809 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006810 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006811 /* response errors are most likely due to
6812 * the client aborting the transfer.
6813 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006814 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006815 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006816 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006817 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006818 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006819 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006820 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006821 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006822 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006823 }
6824 }
6825
Willy Tarreaud98cf932009-12-27 22:54:55 +01006826 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006827 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006828 if (unlikely(compressing)) {
6829 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006830 compressing = 0;
6831 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006832
Willy Tarreauc623c172014-04-18 09:53:50 +02006833 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6834 b_adv(res->buf, msg->next);
6835 msg->next = 0;
6836 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6837 }
6838
Willy Tarreauf003d372012-11-26 13:35:37 +01006839 if (res->flags & CF_SHUTW)
6840 goto aborted_xfer;
6841
6842 /* stop waiting for data if the input is closed before the end. If the
6843 * client side was already closed, it means that the client has aborted,
6844 * so we don't want to count this as a server abort. Otherwise it's a
6845 * server abort.
6846 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006847 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006848 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006849 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006850 if (!(s->flags & SF_ERR_MASK))
6851 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006852 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006853 if (objt_server(s->target))
6854 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006855 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006856 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006857
Willy Tarreau40dba092010-03-04 18:14:51 +01006858 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006859 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006860 goto return_bad_res;
6861
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006862 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006863 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006864 * Similarly, with keep-alive on the client side, we don't want to forward a
6865 * close.
6866 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006867 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006868 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6869 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006870 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006871
Willy Tarreau5c620922011-05-11 19:56:11 +02006872 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006873 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006874 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006875 * modes are already handled by the stream sock layer. We must not do
6876 * this in content-length mode because it could present the MSG_MORE
6877 * flag with the last block of forwarded data, which would cause an
6878 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006879 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006880 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006881 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006882
Willy Tarreau87b09662015-04-03 00:22:06 +02006883 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006884 return 0;
6885
Willy Tarreau40dba092010-03-04 18:14:51 +01006886 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006887 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006888 if (objt_server(s->target))
6889 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006890
6891 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006892 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006893 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006894 compressing = 0;
6895 }
6896
Willy Tarreauc623c172014-04-18 09:53:50 +02006897 /* we may have some pending data starting at res->buf->p */
6898 if (s->comp_algo == NULL) {
6899 b_adv(res->buf, msg->next);
6900 msg->next = 0;
6901 }
6902
Willy Tarreaud98cf932009-12-27 22:54:55 +01006903 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006904 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006905 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006906 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006907 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006908 if (objt_server(s->target))
6909 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006910
Willy Tarreaue7dff022015-04-03 01:14:29 +02006911 if (!(s->flags & SF_ERR_MASK))
6912 s->flags |= SF_ERR_PRXCOND;
6913 if (!(s->flags & SF_FINST_MASK))
6914 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006915 return 0;
6916
6917 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006918 if (unlikely(compressing)) {
6919 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6920 compressing = 0;
6921 }
6922
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006923 txn->rsp.msg_state = HTTP_MSG_ERROR;
6924 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006925 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006926 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006927 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006928
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006929 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006930 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006931 if (objt_server(s->target))
6932 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006933
Willy Tarreaue7dff022015-04-03 01:14:29 +02006934 if (!(s->flags & SF_ERR_MASK))
6935 s->flags |= SF_ERR_CLICL;
6936 if (!(s->flags & SF_FINST_MASK))
6937 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006938 return 0;
6939}
6940
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006941/* Iterate the same filter through all request headers.
6942 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006943 * Since it can manage the switch to another backend, it updates the per-proxy
6944 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006946int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006947{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006948 char *cur_ptr, *cur_end, *cur_next;
6949 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006950 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006951 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006952 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006953
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006954 last_hdr = 0;
6955
Willy Tarreau9b28e032012-10-12 23:49:43 +02006956 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006957 old_idx = 0;
6958
6959 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006960 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006961 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006962 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006963 (exp->action == ACT_ALLOW ||
6964 exp->action == ACT_DENY ||
6965 exp->action == ACT_TARPIT))
6966 return 0;
6967
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006968 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006969 if (!cur_idx)
6970 break;
6971
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006972 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006973 cur_ptr = cur_next;
6974 cur_end = cur_ptr + cur_hdr->len;
6975 cur_next = cur_end + cur_hdr->cr + 1;
6976
6977 /* Now we have one header between cur_ptr and cur_end,
6978 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006979 */
6980
Willy Tarreau15a53a42015-01-21 13:39:42 +01006981 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006982 switch (exp->action) {
6983 case ACT_SETBE:
6984 /* It is not possible to jump a second time.
6985 * FIXME: should we return an HTTP/500 here so that
6986 * the admin knows there's a problem ?
6987 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006988 if (s->be != strm_sess(s)->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989 break;
6990
6991 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02006992 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006993 last_hdr = 1;
6994 break;
6995
6996 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006997 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006998 last_hdr = 1;
6999 break;
7000
7001 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007002 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007003 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007004 break;
7005
7006 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007007 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007008 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007009 break;
7010
7011 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007012 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7013 if (trash.len < 0)
7014 return -1;
7015
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007016 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007017 /* FIXME: if the user adds a newline in the replacement, the
7018 * index will not be recalculated for now, and the new line
7019 * will not be counted as a new header.
7020 */
7021
7022 cur_end += delta;
7023 cur_next += delta;
7024 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007025 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007026 break;
7027
7028 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007029 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007030 cur_next += delta;
7031
Willy Tarreaufa355d42009-11-29 18:12:29 +01007032 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007033 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7034 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007035 cur_hdr->len = 0;
7036 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007037 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007038 break;
7039
7040 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007041 }
7042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007043 /* keep the link from this header to next one in case of later
7044 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007045 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007046 old_idx = cur_idx;
7047 }
7048 return 0;
7049}
7050
7051
7052/* Apply the filter to the request line.
7053 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7054 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007055 * Since it can manage the switch to another backend, it updates the per-proxy
7056 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007057 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007058int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007059{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007060 char *cur_ptr, *cur_end;
7061 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007062 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007063 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064
Willy Tarreau3d300592007-03-18 18:34:41 +01007065 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007066 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007067 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007068 (exp->action == ACT_ALLOW ||
7069 exp->action == ACT_DENY ||
7070 exp->action == ACT_TARPIT))
7071 return 0;
7072 else if (exp->action == ACT_REMOVE)
7073 return 0;
7074
7075 done = 0;
7076
Willy Tarreau9b28e032012-10-12 23:49:43 +02007077 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007078 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007079
7080 /* Now we have the request line between cur_ptr and cur_end */
7081
Willy Tarreau15a53a42015-01-21 13:39:42 +01007082 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007083 switch (exp->action) {
7084 case ACT_SETBE:
7085 /* It is not possible to jump a second time.
7086 * FIXME: should we return an HTTP/500 here so that
7087 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007088 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007089 if (s->be != strm_sess(s)->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007090 break;
7091
7092 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007093 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007094 done = 1;
7095 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007097 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007098 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007099 done = 1;
7100 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007103 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007104 done = 1;
7105 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007107 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007108 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007109 done = 1;
7110 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007112 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007113 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7114 if (trash.len < 0)
7115 return -1;
7116
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007117 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007118 /* FIXME: if the user adds a newline in the replacement, the
7119 * index will not be recalculated for now, and the new line
7120 * will not be counted as a new header.
7121 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007122
Willy Tarreaufa355d42009-11-29 18:12:29 +01007123 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007124 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007125 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007126 HTTP_MSG_RQMETH,
7127 cur_ptr, cur_end + 1,
7128 NULL, NULL);
7129 if (unlikely(!cur_end))
7130 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132 /* we have a full request and we know that we have either a CR
7133 * or an LF at <ptr>.
7134 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007135 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7136 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007137 /* there is no point trying this regex on headers */
7138 return 1;
7139 }
7140 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007141 return done;
7142}
Willy Tarreau97de6242006-12-27 17:18:38 +01007143
Willy Tarreau58f10d72006-12-04 02:26:12 +01007144
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007146/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007147 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007148 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149 * unparsable request. Since it can manage the switch to another backend, it
7150 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007151 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007152int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007153{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007154 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007155 struct hdr_exp *exp;
7156
7157 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007158 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007159
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007160 /*
7161 * The interleaving of transformations and verdicts
7162 * makes it difficult to decide to continue or stop
7163 * the evaluation.
7164 */
7165
Willy Tarreau6c123b12010-01-28 20:22:06 +01007166 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7167 break;
7168
Willy Tarreau3d300592007-03-18 18:34:41 +01007169 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007170 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007171 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007172 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007173
7174 /* if this filter had a condition, evaluate it now and skip to
7175 * next filter if the condition does not match.
7176 */
7177 if (exp->cond) {
Willy Tarreau15e91e12015-04-04 00:52:09 +02007178 ret = acl_exec_cond(exp->cond, px, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007179 ret = acl_pass(ret);
7180 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7181 ret = !ret;
7182
7183 if (!ret)
7184 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007185 }
7186
7187 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007188 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007189 if (unlikely(ret < 0))
7190 return -1;
7191
7192 if (likely(ret == 0)) {
7193 /* The filter did not match the request, it can be
7194 * iterated through all headers.
7195 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007196 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7197 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007198 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007199 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007200 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007201}
7202
7203
Willy Tarreaua15645d2007-03-18 16:22:39 +01007204
Willy Tarreau58f10d72006-12-04 02:26:12 +01007205/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007206 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007207 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007208 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007209void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02007210 struct http_txn *txn = s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007211 appsess *asession = NULL;
7212 char *sessid_temp = NULL;
7213
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007214 if (len > s->be->appsession_len) {
7215 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007216 }
7217
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007218 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007219 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007220 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007221 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007222 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007223 }
7224
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007225 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007226 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007227 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007228 return;
7229 }
7230
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007231 memcpy(txn->sessid, buf, len);
7232 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007233 }
7234
7235 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7236 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007237 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007238 return;
7239 }
7240
Cyril Bontéb21570a2009-11-29 20:04:48 +01007241 memcpy(sessid_temp, buf, len);
7242 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007243
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007244 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007245 /* free previously allocated memory */
7246 pool_free2(apools.sessid, sessid_temp);
7247
7248 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007249 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7250 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007251 asession->request_count++;
7252
7253 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007254 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007255
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007256 while (srv) {
7257 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007258 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007259 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007260 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007261 /* we found the server and it's usable */
7262 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007263 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007264 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007265 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007266
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007267 break;
7268 } else {
7269 txn->flags &= ~TX_CK_MASK;
7270 txn->flags |= TX_CK_DOWN;
7271 }
7272 }
7273 srv = srv->next;
7274 }
7275 }
7276 }
7277}
7278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007279/* Find the end of a cookie value contained between <s> and <e>. It works the
7280 * same way as with headers above except that the semi-colon also ends a token.
7281 * See RFC2965 for more information. Note that it requires a valid header to
7282 * return a valid result.
7283 */
7284char *find_cookie_value_end(char *s, const char *e)
7285{
7286 int quoted, qdpair;
7287
7288 quoted = qdpair = 0;
7289 for (; s < e; s++) {
7290 if (qdpair) qdpair = 0;
7291 else if (quoted) {
7292 if (*s == '\\') qdpair = 1;
7293 else if (*s == '"') quoted = 0;
7294 }
7295 else if (*s == '"') quoted = 1;
7296 else if (*s == ',' || *s == ';') return s;
7297 }
7298 return s;
7299}
7300
7301/* Delete a value in a header between delimiters <from> and <next> in buffer
7302 * <buf>. The number of characters displaced is returned, and the pointer to
7303 * the first delimiter is updated if required. The function tries as much as
7304 * possible to respect the following principles :
7305 * - replace <from> delimiter by the <next> one unless <from> points to a
7306 * colon, in which case <next> is simply removed
7307 * - set exactly one space character after the new first delimiter, unless
7308 * there are not enough characters in the block being moved to do so.
7309 * - remove unneeded spaces before the previous delimiter and after the new
7310 * one.
7311 *
7312 * It is the caller's responsibility to ensure that :
7313 * - <from> points to a valid delimiter or the colon ;
7314 * - <next> points to a valid delimiter or the final CR/LF ;
7315 * - there are non-space chars before <from> ;
7316 * - there is a CR/LF at or after <next>.
7317 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007318int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007319{
7320 char *prev = *from;
7321
7322 if (*prev == ':') {
7323 /* We're removing the first value, preserve the colon and add a
7324 * space if possible.
7325 */
7326 if (!http_is_crlf[(unsigned char)*next])
7327 next++;
7328 prev++;
7329 if (prev < next)
7330 *prev++ = ' ';
7331
7332 while (http_is_spht[(unsigned char)*next])
7333 next++;
7334 } else {
7335 /* Remove useless spaces before the old delimiter. */
7336 while (http_is_spht[(unsigned char)*(prev-1)])
7337 prev--;
7338 *from = prev;
7339
7340 /* copy the delimiter and if possible a space if we're
7341 * not at the end of the line.
7342 */
7343 if (!http_is_crlf[(unsigned char)*next]) {
7344 *prev++ = *next++;
7345 if (prev + 1 < next)
7346 *prev++ = ' ';
7347 while (http_is_spht[(unsigned char)*next])
7348 next++;
7349 }
7350 }
7351 return buffer_replace2(buf, prev, next, NULL, 0);
7352}
7353
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007354/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007355 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007356 * desirable to call it only when needed. This code is quite complex because
7357 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7358 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007359 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007360void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007361{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007362 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007363 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007364 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007365 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007366 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7367 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007368
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007369 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007370 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007371 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007372
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007373 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007374 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007375 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007376
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007377 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007378 hdr_beg = hdr_next;
7379 hdr_end = hdr_beg + cur_hdr->len;
7380 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007381
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007382 /* We have one full header between hdr_beg and hdr_end, and the
7383 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007384 * "Cookie:" headers.
7385 */
7386
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007387 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007388 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007389 old_idx = cur_idx;
7390 continue;
7391 }
7392
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007393 del_from = NULL; /* nothing to be deleted */
7394 preserve_hdr = 0; /* assume we may kill the whole header */
7395
Willy Tarreau58f10d72006-12-04 02:26:12 +01007396 /* Now look for cookies. Conforming to RFC2109, we have to support
7397 * attributes whose name begin with a '$', and associate them with
7398 * the right cookie, if we want to delete this cookie.
7399 * So there are 3 cases for each cookie read :
7400 * 1) it's a special attribute, beginning with a '$' : ignore it.
7401 * 2) it's a server id cookie that we *MAY* want to delete : save
7402 * some pointers on it (last semi-colon, beginning of cookie...)
7403 * 3) it's an application cookie : we *MAY* have to delete a previous
7404 * "special" cookie.
7405 * At the end of loop, if a "special" cookie remains, we may have to
7406 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007407 * *MUST* delete it.
7408 *
7409 * Note: RFC2965 is unclear about the processing of spaces around
7410 * the equal sign in the ATTR=VALUE form. A careful inspection of
7411 * the RFC explicitly allows spaces before it, and not within the
7412 * tokens (attrs or values). An inspection of RFC2109 allows that
7413 * too but section 10.1.3 lets one think that spaces may be allowed
7414 * after the equal sign too, resulting in some (rare) buggy
7415 * implementations trying to do that. So let's do what servers do.
7416 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7417 * allowed quoted strings in values, with any possible character
7418 * after a backslash, including control chars and delimitors, which
7419 * causes parsing to become ambiguous. Browsers also allow spaces
7420 * within values even without quotes.
7421 *
7422 * We have to keep multiple pointers in order to support cookie
7423 * removal at the beginning, middle or end of header without
7424 * corrupting the header. All of these headers are valid :
7425 *
7426 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7427 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7428 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7429 * | | | | | | | | |
7430 * | | | | | | | | hdr_end <--+
7431 * | | | | | | | +--> next
7432 * | | | | | | +----> val_end
7433 * | | | | | +-----------> val_beg
7434 * | | | | +--------------> equal
7435 * | | | +----------------> att_end
7436 * | | +---------------------> att_beg
7437 * | +--------------------------> prev
7438 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007439 */
7440
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007441 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7442 /* Iterate through all cookies on this line */
7443
7444 /* find att_beg */
7445 att_beg = prev + 1;
7446 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7447 att_beg++;
7448
7449 /* find att_end : this is the first character after the last non
7450 * space before the equal. It may be equal to hdr_end.
7451 */
7452 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007453
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007454 while (equal < hdr_end) {
7455 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007456 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007457 if (http_is_spht[(unsigned char)*equal++])
7458 continue;
7459 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007460 }
7461
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007462 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7463 * is between <att_beg> and <equal>, both may be identical.
7464 */
7465
7466 /* look for end of cookie if there is an equal sign */
7467 if (equal < hdr_end && *equal == '=') {
7468 /* look for the beginning of the value */
7469 val_beg = equal + 1;
7470 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7471 val_beg++;
7472
7473 /* find the end of the value, respecting quotes */
7474 next = find_cookie_value_end(val_beg, hdr_end);
7475
7476 /* make val_end point to the first white space or delimitor after the value */
7477 val_end = next;
7478 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7479 val_end--;
7480 } else {
7481 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007482 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007483
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007484 /* We have nothing to do with attributes beginning with '$'. However,
7485 * they will automatically be removed if a header before them is removed,
7486 * since they're supposed to be linked together.
7487 */
7488 if (*att_beg == '$')
7489 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007491 /* Ignore cookies with no equal sign */
7492 if (equal == next) {
7493 /* This is not our cookie, so we must preserve it. But if we already
7494 * scheduled another cookie for removal, we cannot remove the
7495 * complete header, but we can remove the previous block itself.
7496 */
7497 preserve_hdr = 1;
7498 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007499 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007500 val_end += delta;
7501 next += delta;
7502 hdr_end += delta;
7503 hdr_next += delta;
7504 cur_hdr->len += delta;
7505 http_msg_move_end(&txn->req, delta);
7506 prev = del_from;
7507 del_from = NULL;
7508 }
7509 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007510 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007511
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007512 /* if there are spaces around the equal sign, we need to
7513 * strip them otherwise we'll get trouble for cookie captures,
7514 * or even for rewrites. Since this happens extremely rarely,
7515 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007516 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007517 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7518 int stripped_before = 0;
7519 int stripped_after = 0;
7520
7521 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007522 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007523 equal += stripped_before;
7524 val_beg += stripped_before;
7525 }
7526
7527 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007528 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007529 val_beg += stripped_after;
7530 stripped_before += stripped_after;
7531 }
7532
7533 val_end += stripped_before;
7534 next += stripped_before;
7535 hdr_end += stripped_before;
7536 hdr_next += stripped_before;
7537 cur_hdr->len += stripped_before;
7538 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007539 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007540 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007541
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007542 /* First, let's see if we want to capture this cookie. We check
7543 * that we don't already have a client side cookie, because we
7544 * can only capture one. Also as an optimisation, we ignore
7545 * cookies shorter than the declared name.
7546 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007547 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7548 (val_end - att_beg >= sess->fe->capture_namelen) &&
7549 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007550 int log_len = val_end - att_beg;
7551
7552 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7553 Alert("HTTP logging : out of memory.\n");
7554 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007555 if (log_len > sess->fe->capture_len)
7556 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007557 memcpy(txn->cli_cookie, att_beg, log_len);
7558 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007559 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007561
Willy Tarreaubca99692010-10-06 19:25:55 +02007562 /* Persistence cookies in passive, rewrite or insert mode have the
7563 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007564 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007565 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007566 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007567 * For cookies in prefix mode, the form is :
7568 *
7569 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007571 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7572 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7573 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007574 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007576 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7577 * have the server ID between val_beg and delim, and the original cookie between
7578 * delim+1 and val_end. Otherwise, delim==val_end :
7579 *
7580 * Cookie: NAME=SRV; # in all but prefix modes
7581 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7582 * | || || | |+-> next
7583 * | || || | +--> val_end
7584 * | || || +---------> delim
7585 * | || |+------------> val_beg
7586 * | || +-------------> att_end = equal
7587 * | |+-----------------> att_beg
7588 * | +------------------> prev
7589 * +-------------------------> hdr_beg
7590 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007592 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007593 for (delim = val_beg; delim < val_end; delim++)
7594 if (*delim == COOKIE_DELIM)
7595 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007596 } else {
7597 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007599 /* Now check if the cookie contains a date field, which would
7600 * appear after a vertical bar ('|') just after the server name
7601 * and before the delimiter.
7602 */
7603 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7604 if (vbar1) {
7605 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007606 * right is the last seen date. It is a base64 encoded
7607 * 30-bit value representing the UNIX date since the
7608 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007609 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007610 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007611 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007612 if (val_end - vbar1 >= 5) {
7613 val = b64tos30(vbar1);
7614 if (val > 0)
7615 txn->cookie_last_date = val << 2;
7616 }
7617 /* look for a second vertical bar */
7618 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7619 if (vbar1 && (val_end - vbar1 > 5)) {
7620 val = b64tos30(vbar1 + 1);
7621 if (val > 0)
7622 txn->cookie_first_date = val << 2;
7623 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007624 }
7625 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007626
Willy Tarreauf64d1412010-10-07 20:06:11 +02007627 /* if the cookie has an expiration date and the proxy wants to check
7628 * it, then we do that now. We first check if the cookie is too old,
7629 * then only if it has expired. We detect strict overflow because the
7630 * time resolution here is not great (4 seconds). Cookies with dates
7631 * in the future are ignored if their offset is beyond one day. This
7632 * allows an admin to fix timezone issues without expiring everyone
7633 * and at the same time avoids keeping unwanted side effects for too
7634 * long.
7635 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007636 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7637 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007638 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007639 txn->flags &= ~TX_CK_MASK;
7640 txn->flags |= TX_CK_OLD;
7641 delim = val_beg; // let's pretend we have not found the cookie
7642 txn->cookie_first_date = 0;
7643 txn->cookie_last_date = 0;
7644 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007645 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7646 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007647 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007648 txn->flags &= ~TX_CK_MASK;
7649 txn->flags |= TX_CK_EXPIRED;
7650 delim = val_beg; // let's pretend we have not found the cookie
7651 txn->cookie_first_date = 0;
7652 txn->cookie_last_date = 0;
7653 }
7654
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 /* Here, we'll look for the first running server which supports the cookie.
7656 * This allows to share a same cookie between several servers, for example
7657 * to dedicate backup servers to specific servers only.
7658 * However, to prevent clients from sticking to cookie-less backup server
7659 * when they have incidentely learned an empty cookie, we simply ignore
7660 * empty cookies and mark them as invalid.
7661 * The same behaviour is applied when persistence must be ignored.
7662 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007663 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007664 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007665
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007666 while (srv) {
7667 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7668 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007669 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007670 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007671 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007672 /* we found the server and we can use it */
7673 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007674 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007675 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007676 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007677 break;
7678 } else {
7679 /* we found a server, but it's down,
7680 * mark it as such and go on in case
7681 * another one is available.
7682 */
7683 txn->flags &= ~TX_CK_MASK;
7684 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007685 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007686 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007687 srv = srv->next;
7688 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689
Willy Tarreauf64d1412010-10-07 20:06:11 +02007690 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007691 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007693 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007694 txn->flags |= TX_CK_UNUSED;
7695 else
7696 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007698
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007699 /* depending on the cookie mode, we may have to either :
7700 * - delete the complete cookie if we're in insert+indirect mode, so that
7701 * the server never sees it ;
7702 * - remove the server id from the cookie value, and tag the cookie as an
7703 * application cookie so that it does not get accidentely removed later,
7704 * if we're in cookie prefix mode
7705 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007706 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007708
Willy Tarreau9b28e032012-10-12 23:49:43 +02007709 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007710 val_end += delta;
7711 next += delta;
7712 hdr_end += delta;
7713 hdr_next += delta;
7714 cur_hdr->len += delta;
7715 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007716
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007717 del_from = NULL;
7718 preserve_hdr = 1; /* we want to keep this cookie */
7719 }
7720 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007721 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 del_from = prev;
7723 }
7724 } else {
7725 /* This is not our cookie, so we must preserve it. But if we already
7726 * scheduled another cookie for removal, we cannot remove the
7727 * complete header, but we can remove the previous block itself.
7728 */
7729 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007730
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007732 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007733 if (att_beg >= del_from)
7734 att_beg += delta;
7735 if (att_end >= del_from)
7736 att_end += delta;
7737 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007738 val_end += delta;
7739 next += delta;
7740 hdr_end += delta;
7741 hdr_next += delta;
7742 cur_hdr->len += delta;
7743 http_msg_move_end(&txn->req, delta);
7744 prev = del_from;
7745 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007746 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007747 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007748
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007750 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007751 int cmp_len, value_len;
7752 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007753
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007754 if (s->be->options2 & PR_O2_AS_PFX) {
7755 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7756 value_begin = att_beg + s->be->appsession_name_len;
7757 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007758 } else {
7759 cmp_len = att_end - att_beg;
7760 value_begin = val_beg;
7761 value_len = val_end - val_beg;
7762 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007763
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007764 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007765 if (cmp_len == s->be->appsession_name_len &&
7766 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7767 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007768 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007769 }
7770
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007771 /* continue with next cookie on this header line */
7772 att_beg = next;
7773 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007774
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 /* There are no more cookies on this line.
7776 * We may still have one (or several) marked for deletion at the
7777 * end of the line. We must do this now in two ways :
7778 * - if some cookies must be preserved, we only delete from the
7779 * mark to the end of line ;
7780 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007781 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007783 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007784 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007785 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007787 cur_hdr->len += delta;
7788 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007789 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007790
7791 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007792 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7793 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007794 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007795 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007796 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007797 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007798 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007799 }
7800
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007801 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007802 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007803 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007804}
7805
7806
Willy Tarreaua15645d2007-03-18 16:22:39 +01007807/* Iterate the same filter through all response headers contained in <rtr>.
7808 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7809 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007810int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007811{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007812 char *cur_ptr, *cur_end, *cur_next;
7813 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007814 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007815 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007816 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007817
7818 last_hdr = 0;
7819
Willy Tarreau9b28e032012-10-12 23:49:43 +02007820 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007821 old_idx = 0;
7822
7823 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007824 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007826 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007827 (exp->action == ACT_ALLOW ||
7828 exp->action == ACT_DENY))
7829 return 0;
7830
7831 cur_idx = txn->hdr_idx.v[old_idx].next;
7832 if (!cur_idx)
7833 break;
7834
7835 cur_hdr = &txn->hdr_idx.v[cur_idx];
7836 cur_ptr = cur_next;
7837 cur_end = cur_ptr + cur_hdr->len;
7838 cur_next = cur_end + cur_hdr->cr + 1;
7839
7840 /* Now we have one header between cur_ptr and cur_end,
7841 * and the next header starts at cur_next.
7842 */
7843
Willy Tarreau15a53a42015-01-21 13:39:42 +01007844 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845 switch (exp->action) {
7846 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007847 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848 last_hdr = 1;
7849 break;
7850
7851 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007852 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 last_hdr = 1;
7854 break;
7855
7856 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007857 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7858 if (trash.len < 0)
7859 return -1;
7860
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007861 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 /* FIXME: if the user adds a newline in the replacement, the
7863 * index will not be recalculated for now, and the new line
7864 * will not be counted as a new header.
7865 */
7866
7867 cur_end += delta;
7868 cur_next += delta;
7869 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007870 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007871 break;
7872
7873 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007874 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007875 cur_next += delta;
7876
Willy Tarreaufa355d42009-11-29 18:12:29 +01007877 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007878 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7879 txn->hdr_idx.used--;
7880 cur_hdr->len = 0;
7881 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007882 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883 break;
7884
7885 }
7886 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007887
7888 /* keep the link from this header to next one in case of later
7889 * removal of next header.
7890 */
7891 old_idx = cur_idx;
7892 }
7893 return 0;
7894}
7895
7896
7897/* Apply the filter to the status line in the response buffer <rtr>.
7898 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7899 * or -1 if a replacement resulted in an invalid status line.
7900 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007901int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007902{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007903 char *cur_ptr, *cur_end;
7904 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007905 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007906 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007907
7908
Willy Tarreau3d300592007-03-18 18:34:41 +01007909 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007911 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007912 (exp->action == ACT_ALLOW ||
7913 exp->action == ACT_DENY))
7914 return 0;
7915 else if (exp->action == ACT_REMOVE)
7916 return 0;
7917
7918 done = 0;
7919
Willy Tarreau9b28e032012-10-12 23:49:43 +02007920 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007921 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007922
7923 /* Now we have the status line between cur_ptr and cur_end */
7924
Willy Tarreau15a53a42015-01-21 13:39:42 +01007925 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007926 switch (exp->action) {
7927 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007928 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007929 done = 1;
7930 break;
7931
7932 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007933 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007934 done = 1;
7935 break;
7936
7937 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007938 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7939 if (trash.len < 0)
7940 return -1;
7941
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007942 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007943 /* FIXME: if the user adds a newline in the replacement, the
7944 * index will not be recalculated for now, and the new line
7945 * will not be counted as a new header.
7946 */
7947
Willy Tarreaufa355d42009-11-29 18:12:29 +01007948 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007950 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007951 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007952 cur_ptr, cur_end + 1,
7953 NULL, NULL);
7954 if (unlikely(!cur_end))
7955 return -1;
7956
7957 /* we have a full respnse and we know that we have either a CR
7958 * or an LF at <ptr>.
7959 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007960 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007961 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962 /* there is no point trying this regex on headers */
7963 return 1;
7964 }
7965 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007966 return done;
7967}
7968
7969
7970
7971/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007972 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007973 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7974 * unparsable response.
7975 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007976int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007977{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007978 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007979 struct hdr_exp *exp;
7980
7981 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007982 int ret;
7983
7984 /*
7985 * The interleaving of transformations and verdicts
7986 * makes it difficult to decide to continue or stop
7987 * the evaluation.
7988 */
7989
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007990 if (txn->flags & TX_SVDENY)
7991 break;
7992
Willy Tarreau3d300592007-03-18 18:34:41 +01007993 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7995 exp->action == ACT_PASS)) {
7996 exp = exp->next;
7997 continue;
7998 }
7999
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008000 /* if this filter had a condition, evaluate it now and skip to
8001 * next filter if the condition does not match.
8002 */
8003 if (exp->cond) {
Willy Tarreau15e91e12015-04-04 00:52:09 +02008004 ret = acl_exec_cond(exp->cond, px, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008005 ret = acl_pass(ret);
8006 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8007 ret = !ret;
8008 if (!ret)
8009 continue;
8010 }
8011
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008013 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008014 if (unlikely(ret < 0))
8015 return -1;
8016
8017 if (likely(ret == 0)) {
8018 /* The filter did not match the response, it can be
8019 * iterated through all headers.
8020 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008021 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8022 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008024 }
8025 return 0;
8026}
8027
8028
Willy Tarreaua15645d2007-03-18 16:22:39 +01008029/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008030 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008031 * desirable to call it only when needed. This function is also used when we
8032 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008034void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008036 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008037 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008038 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008039 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008040 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008041 char *hdr_beg, *hdr_end, *hdr_next;
8042 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043
Willy Tarreaua15645d2007-03-18 16:22:39 +01008044 /* Iterate through the headers.
8045 * we start with the start line.
8046 */
8047 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008048 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008049
8050 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8051 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008052 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008053
8054 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008055 hdr_beg = hdr_next;
8056 hdr_end = hdr_beg + cur_hdr->len;
8057 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058
Willy Tarreau24581ba2010-08-31 22:39:35 +02008059 /* We have one full header between hdr_beg and hdr_end, and the
8060 * next header starts at hdr_next. We're only interested in
8061 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062 */
8063
Willy Tarreau24581ba2010-08-31 22:39:35 +02008064 is_cookie2 = 0;
8065 prev = hdr_beg + 10;
8066 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008067 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008068 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8069 if (!val) {
8070 old_idx = cur_idx;
8071 continue;
8072 }
8073 is_cookie2 = 1;
8074 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075 }
8076
Willy Tarreau24581ba2010-08-31 22:39:35 +02008077 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8078 * <prev> points to the colon.
8079 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008080 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081
Willy Tarreau24581ba2010-08-31 22:39:35 +02008082 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8083 * check-cache is enabled) and we are not interested in checking
8084 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008085 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008086 if (s->be->cookie_name == NULL &&
8087 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008088 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 return;
8090
Willy Tarreau24581ba2010-08-31 22:39:35 +02008091 /* OK so now we know we have to process this response cookie.
8092 * The format of the Set-Cookie header is slightly different
8093 * from the format of the Cookie header in that it does not
8094 * support the comma as a cookie delimiter (thus the header
8095 * cannot be folded) because the Expires attribute described in
8096 * the original Netscape's spec may contain an unquoted date
8097 * with a comma inside. We have to live with this because
8098 * many browsers don't support Max-Age and some browsers don't
8099 * support quoted strings. However the Set-Cookie2 header is
8100 * clean.
8101 *
8102 * We have to keep multiple pointers in order to support cookie
8103 * removal at the beginning, middle or end of header without
8104 * corrupting the header (in case of set-cookie2). A special
8105 * pointer, <scav> points to the beginning of the set-cookie-av
8106 * fields after the first semi-colon. The <next> pointer points
8107 * either to the end of line (set-cookie) or next unquoted comma
8108 * (set-cookie2). All of these headers are valid :
8109 *
8110 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8111 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8112 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8113 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8114 * | | | | | | | | | |
8115 * | | | | | | | | +-> next hdr_end <--+
8116 * | | | | | | | +------------> scav
8117 * | | | | | | +--------------> val_end
8118 * | | | | | +--------------------> val_beg
8119 * | | | | +----------------------> equal
8120 * | | | +------------------------> att_end
8121 * | | +----------------------------> att_beg
8122 * | +------------------------------> prev
8123 * +-----------------------------------------> hdr_beg
8124 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008125
Willy Tarreau24581ba2010-08-31 22:39:35 +02008126 for (; prev < hdr_end; prev = next) {
8127 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008128
Willy Tarreau24581ba2010-08-31 22:39:35 +02008129 /* find att_beg */
8130 att_beg = prev + 1;
8131 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8132 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133
Willy Tarreau24581ba2010-08-31 22:39:35 +02008134 /* find att_end : this is the first character after the last non
8135 * space before the equal. It may be equal to hdr_end.
8136 */
8137 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008138
Willy Tarreau24581ba2010-08-31 22:39:35 +02008139 while (equal < hdr_end) {
8140 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8141 break;
8142 if (http_is_spht[(unsigned char)*equal++])
8143 continue;
8144 att_end = equal;
8145 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008146
Willy Tarreau24581ba2010-08-31 22:39:35 +02008147 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8148 * is between <att_beg> and <equal>, both may be identical.
8149 */
8150
8151 /* look for end of cookie if there is an equal sign */
8152 if (equal < hdr_end && *equal == '=') {
8153 /* look for the beginning of the value */
8154 val_beg = equal + 1;
8155 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8156 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157
Willy Tarreau24581ba2010-08-31 22:39:35 +02008158 /* find the end of the value, respecting quotes */
8159 next = find_cookie_value_end(val_beg, hdr_end);
8160
8161 /* make val_end point to the first white space or delimitor after the value */
8162 val_end = next;
8163 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8164 val_end--;
8165 } else {
8166 /* <equal> points to next comma, semi-colon or EOL */
8167 val_beg = val_end = next = equal;
8168 }
8169
8170 if (next < hdr_end) {
8171 /* Set-Cookie2 supports multiple cookies, and <next> points to
8172 * a colon or semi-colon before the end. So skip all attr-value
8173 * pairs and look for the next comma. For Set-Cookie, since
8174 * commas are permitted in values, skip to the end.
8175 */
8176 if (is_cookie2)
8177 next = find_hdr_value_end(next, hdr_end);
8178 else
8179 next = hdr_end;
8180 }
8181
8182 /* Now everything is as on the diagram above */
8183
8184 /* Ignore cookies with no equal sign */
8185 if (equal == val_end)
8186 continue;
8187
8188 /* If there are spaces around the equal sign, we need to
8189 * strip them otherwise we'll get trouble for cookie captures,
8190 * or even for rewrites. Since this happens extremely rarely,
8191 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008193 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8194 int stripped_before = 0;
8195 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008196
Willy Tarreau24581ba2010-08-31 22:39:35 +02008197 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008198 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008199 equal += stripped_before;
8200 val_beg += stripped_before;
8201 }
8202
8203 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008204 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008205 val_beg += stripped_after;
8206 stripped_before += stripped_after;
8207 }
8208
8209 val_end += stripped_before;
8210 next += stripped_before;
8211 hdr_end += stripped_before;
8212 hdr_next += stripped_before;
8213 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008214 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008215 }
8216
8217 /* First, let's see if we want to capture this cookie. We check
8218 * that we don't already have a server side cookie, because we
8219 * can only capture one. Also as an optimisation, we ignore
8220 * cookies shorter than the declared name.
8221 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008222 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008223 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008224 (val_end - att_beg >= sess->fe->capture_namelen) &&
8225 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008226 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008227 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228 Alert("HTTP logging : out of memory.\n");
8229 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008230 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008231 if (log_len > sess->fe->capture_len)
8232 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008233 memcpy(txn->srv_cookie, att_beg, log_len);
8234 txn->srv_cookie[log_len] = 0;
8235 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236 }
8237
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008238 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008240 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008241 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8242 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008243 /* assume passive cookie by default */
8244 txn->flags &= ~TX_SCK_MASK;
8245 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246
8247 /* If the cookie is in insert mode on a known server, we'll delete
8248 * this occurrence because we'll insert another one later.
8249 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 * a direct access.
8251 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008252 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008253 /* The "preserve" flag was set, we don't want to touch the
8254 * server's cookie.
8255 */
8256 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008257 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008258 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008259 /* this cookie must be deleted */
8260 if (*prev == ':' && next == hdr_end) {
8261 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008262 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008263 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8264 txn->hdr_idx.used--;
8265 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008266 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008267 hdr_next += delta;
8268 http_msg_move_end(&txn->rsp, delta);
8269 /* note: while both invalid now, <next> and <hdr_end>
8270 * are still equal, so the for() will stop as expected.
8271 */
8272 } else {
8273 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008274 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008275 next = prev;
8276 hdr_end += delta;
8277 hdr_next += delta;
8278 cur_hdr->len += delta;
8279 http_msg_move_end(&txn->rsp, delta);
8280 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008281 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008282 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008283 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008284 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008285 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008286 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008287 * with this server since we know it.
8288 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008289 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008290 next += delta;
8291 hdr_end += delta;
8292 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008293 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008294 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008295
Willy Tarreauf1348312010-10-07 15:54:11 +02008296 txn->flags &= ~TX_SCK_MASK;
8297 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008298 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008299 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008300 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008302 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008303 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008304 next += delta;
8305 hdr_end += delta;
8306 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008307 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008308 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008309
Willy Tarreau827aee92011-03-10 16:55:02 +01008310 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008311 txn->flags &= ~TX_SCK_MASK;
8312 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313 }
8314 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008315 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008316 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008317 int cmp_len, value_len;
8318 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008320 if (s->be->options2 & PR_O2_AS_PFX) {
8321 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8322 value_begin = att_beg + s->be->appsession_name_len;
8323 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008324 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008325 cmp_len = att_end - att_beg;
8326 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008327 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008328 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008329
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008330 if ((cmp_len == s->be->appsession_name_len) &&
8331 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008332 /* free a possibly previously allocated memory */
8333 pool_free2(apools.sessid, txn->sessid);
8334
Willy Tarreau87b09662015-04-03 00:22:06 +02008335 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008336 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008337 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008338 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008339 return;
8340 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008341 memcpy(txn->sessid, value_begin, value_len);
8342 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008343 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008344 }
8345 /* that's done for this cookie, check the next one on the same
8346 * line when next != hdr_end (only if is_cookie2).
8347 */
8348 }
8349 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008350 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008351 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008352
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008353 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008354 appsess *asession = NULL;
8355 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008356 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008357 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008358 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008359 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8360 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008361 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008362 return;
8363 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008364 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8365
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008366 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8367 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008368 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8369 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008370 return;
8371 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008372 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8373 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008374
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008375 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008376 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008377 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008378 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8379 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008380 return;
8381 }
8382 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008383 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008384
8385 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008386 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008387 }
8388
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008389 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008390 asession->request_count++;
8391 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008392}
8393
8394
Willy Tarreaua15645d2007-03-18 16:22:39 +01008395/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008396 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008397 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008398void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008399{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008400 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008401 char *p1, *p2;
8402
8403 char *cur_ptr, *cur_end, *cur_next;
8404 int cur_idx;
8405
Willy Tarreau5df51872007-11-25 16:20:08 +01008406 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008407 return;
8408
8409 /* Iterate through the headers.
8410 * we start with the start line.
8411 */
8412 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008413 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008414
8415 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8416 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008417 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008418
8419 cur_hdr = &txn->hdr_idx.v[cur_idx];
8420 cur_ptr = cur_next;
8421 cur_end = cur_ptr + cur_hdr->len;
8422 cur_next = cur_end + cur_hdr->cr + 1;
8423
8424 /* We have one full header between cur_ptr and cur_end, and the
8425 * next header starts at cur_next. We're only interested in
8426 * "Cookie:" headers.
8427 */
8428
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008429 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8430 if (val) {
8431 if ((cur_end - (cur_ptr + val) >= 8) &&
8432 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8433 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8434 return;
8435 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008436 }
8437
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008438 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8439 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008440 continue;
8441
8442 /* OK, right now we know we have a cache-control header at cur_ptr */
8443
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008444 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008445
8446 if (p1 >= cur_end) /* no more info */
8447 continue;
8448
8449 /* p1 is at the beginning of the value */
8450 p2 = p1;
8451
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008452 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008453 p2++;
8454
8455 /* we have a complete value between p1 and p2 */
8456 if (p2 < cur_end && *p2 == '=') {
8457 /* we have something of the form no-cache="set-cookie" */
8458 if ((cur_end - p1 >= 21) &&
8459 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8460 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008461 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008462 continue;
8463 }
8464
8465 /* OK, so we know that either p2 points to the end of string or to a comma */
8466 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008467 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8469 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8470 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008471 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008472 return;
8473 }
8474
8475 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008476 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008477 continue;
8478 }
8479 }
8480}
8481
8482
Willy Tarreau58f10d72006-12-04 02:26:12 +01008483/*
8484 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008485 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008486 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008487void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008488{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008489 char *end_params, *first_param, *cur_param, *next_param;
8490 char separator;
8491 int value_len;
8492
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008493 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008494
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008495 if (s->be->appsession_name == NULL ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02008496 (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 +01008497 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008498 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008499
Cyril Bontéb21570a2009-11-29 20:04:48 +01008500 first_param = NULL;
8501 switch (mode) {
8502 case PR_O2_AS_M_PP:
8503 first_param = memchr(begin, ';', len);
8504 break;
8505 case PR_O2_AS_M_QS:
8506 first_param = memchr(begin, '?', len);
8507 break;
8508 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008509
Cyril Bontéb21570a2009-11-29 20:04:48 +01008510 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008511 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008512 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008513
Cyril Bontéb21570a2009-11-29 20:04:48 +01008514 switch (mode) {
8515 case PR_O2_AS_M_PP:
8516 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8517 end_params = (char *) begin + len;
8518 }
8519 separator = ';';
8520 break;
8521 case PR_O2_AS_M_QS:
8522 end_params = (char *) begin + len;
8523 separator = '&';
8524 break;
8525 default:
8526 /* unknown mode, shouldn't happen */
8527 return;
8528 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008529
Cyril Bontéb21570a2009-11-29 20:04:48 +01008530 cur_param = next_param = end_params;
8531 while (cur_param > first_param) {
8532 cur_param--;
8533 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8534 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008535 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8536 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8537 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008538 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008539 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8540 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008541 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008542 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008543 }
8544 break;
8545 }
8546 next_param = cur_param;
8547 }
8548 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008549#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008550 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008551 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008552#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008553}
8554
Willy Tarreaub2513902006-12-17 14:52:38 +01008555/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008556 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008557 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008558 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008559 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008560 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008561 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008562 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008563 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008564int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008565{
8566 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008567 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008568 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008569
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008570 if (!uri_auth)
8571 return 0;
8572
Cyril Bonté70be45d2010-10-12 00:14:35 +02008573 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008574 return 0;
8575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008576 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008577 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008578 return 0;
8579
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008580 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008581 return 0;
8582
Willy Tarreaub2513902006-12-17 14:52:38 +01008583 return 1;
8584}
8585
Willy Tarreau4076a152009-04-02 15:18:36 +02008586/*
8587 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008588 * By default it tries to report the error position as msg->err_pos. However if
8589 * this one is not set, it will then report msg->next, which is the last known
8590 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008591 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008592 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008593void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008594 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008595 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008596{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008597 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008598 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008599 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008600
Willy Tarreau9b28e032012-10-12 23:49:43 +02008601 es->len = MIN(chn->buf->i, sizeof(es->buf));
8602 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008603 len1 = MIN(len1, es->len);
8604 len2 = es->len - len1; /* remaining data if buffer wraps */
8605
Willy Tarreau9b28e032012-10-12 23:49:43 +02008606 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008607 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008608 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008609
Willy Tarreau4076a152009-04-02 15:18:36 +02008610 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008611 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008612 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008613 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008614
Willy Tarreau4076a152009-04-02 15:18:36 +02008615 es->when = date; // user-visible date
8616 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008617 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008618 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008619 if (objt_conn(sess->origin))
8620 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008621 else
8622 memset(&es->src, 0, sizeof(es->src));
8623
Willy Tarreau078272e2010-12-12 12:46:33 +01008624 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008625 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008626 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008627 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008628 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008629 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008630 es->b_out = chn->buf->o;
8631 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008632 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008633 es->m_clen = msg->chunk_len;
8634 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008635}
Willy Tarreaub2513902006-12-17 14:52:38 +01008636
Willy Tarreau294c4732011-12-16 21:35:50 +01008637/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8638 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8639 * performed over the whole headers. Otherwise it must contain a valid header
8640 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8641 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8642 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8643 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008644 * -1. The value fetch stops at commas, so this function is suited for use with
8645 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008646 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008647 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008648unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008649 struct hdr_idx *idx, int occ,
8650 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008651{
Willy Tarreau294c4732011-12-16 21:35:50 +01008652 struct hdr_ctx local_ctx;
8653 char *ptr_hist[MAX_HDR_HISTORY];
8654 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008655 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008656 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008657
Willy Tarreau294c4732011-12-16 21:35:50 +01008658 if (!ctx) {
8659 local_ctx.idx = 0;
8660 ctx = &local_ctx;
8661 }
8662
Willy Tarreaubce70882009-09-07 11:51:47 +02008663 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008664 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008665 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008666 occ--;
8667 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008668 *vptr = ctx->line + ctx->val;
8669 *vlen = ctx->vlen;
8670 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008671 }
8672 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008673 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008674 }
8675
8676 /* negative occurrence, we scan all the list then walk back */
8677 if (-occ > MAX_HDR_HISTORY)
8678 return 0;
8679
Willy Tarreau294c4732011-12-16 21:35:50 +01008680 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008681 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008682 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8683 len_hist[hist_ptr] = ctx->vlen;
8684 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008685 hist_ptr = 0;
8686 found++;
8687 }
8688 if (-occ > found)
8689 return 0;
8690 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008691 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8692 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8693 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008694 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008695 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008696 if (hist_ptr >= MAX_HDR_HISTORY)
8697 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008698 *vptr = ptr_hist[hist_ptr];
8699 *vlen = len_hist[hist_ptr];
8700 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008701}
8702
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008703/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8704 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8705 * performed over the whole headers. Otherwise it must contain a valid header
8706 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8707 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8708 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8709 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8710 * -1. This function differs from http_get_hdr() in that it only returns full
8711 * line header values and does not stop at commas.
8712 * The return value is 0 if nothing was found, or non-zero otherwise.
8713 */
8714unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8715 struct hdr_idx *idx, int occ,
8716 struct hdr_ctx *ctx, char **vptr, int *vlen)
8717{
8718 struct hdr_ctx local_ctx;
8719 char *ptr_hist[MAX_HDR_HISTORY];
8720 int len_hist[MAX_HDR_HISTORY];
8721 unsigned int hist_ptr;
8722 int found;
8723
8724 if (!ctx) {
8725 local_ctx.idx = 0;
8726 ctx = &local_ctx;
8727 }
8728
8729 if (occ >= 0) {
8730 /* search from the beginning */
8731 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8732 occ--;
8733 if (occ <= 0) {
8734 *vptr = ctx->line + ctx->val;
8735 *vlen = ctx->vlen;
8736 return 1;
8737 }
8738 }
8739 return 0;
8740 }
8741
8742 /* negative occurrence, we scan all the list then walk back */
8743 if (-occ > MAX_HDR_HISTORY)
8744 return 0;
8745
8746 found = hist_ptr = 0;
8747 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8748 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8749 len_hist[hist_ptr] = ctx->vlen;
8750 if (++hist_ptr >= MAX_HDR_HISTORY)
8751 hist_ptr = 0;
8752 found++;
8753 }
8754 if (-occ > found)
8755 return 0;
8756 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8757 * find occurrence -occ, so we have to check [hist_ptr+occ].
8758 */
8759 hist_ptr += occ;
8760 if (hist_ptr >= MAX_HDR_HISTORY)
8761 hist_ptr -= MAX_HDR_HISTORY;
8762 *vptr = ptr_hist[hist_ptr];
8763 *vlen = len_hist[hist_ptr];
8764 return 1;
8765}
8766
Willy Tarreaubaaee002006-06-26 02:48:02 +02008767/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008768 * Print a debug line with a header. Always stop at the first CR or LF char,
8769 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8770 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008771 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008772void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008773{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008774 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008775 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008776
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008777 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008778 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008779 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008780 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 +02008781
8782 for (max = 0; start + max < end; max++)
8783 if (start[max] == '\r' || start[max] == '\n')
8784 break;
8785
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008786 UBOUND(max, trash.size - trash.len - 3);
8787 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8788 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008789 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008790}
8791
Willy Tarreaueee5b512015-04-03 23:46:31 +02008792
8793/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8794 * The hdr_idx is allocated as well. In case of allocation failure, everything
8795 * allocated is freed and NULL is returned. Otherwise the new transaction is
8796 * assigned to the stream and returned.
8797 */
8798struct http_txn *http_alloc_txn(struct stream *s)
8799{
8800 struct http_txn *txn = s->txn;
8801
8802 if (txn)
8803 return txn;
8804
8805 txn = pool_alloc2(pool2_http_txn);
8806 if (!txn)
8807 return txn;
8808
8809 txn->hdr_idx.size = global.tune.max_http_hdr;
8810 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8811 if (!txn->hdr_idx.v) {
8812 pool_free2(pool2_http_txn, txn);
8813 return NULL;
8814 }
8815
8816 s->txn = txn;
8817 return txn;
8818}
8819
Willy Tarreau0937bc42009-12-22 15:03:09 +01008820/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008821 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008822 * the required fields are properly allocated and that we only need to (re)init
8823 * them. This should be used before processing any new request.
8824 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008825void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008826{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008827 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008828 struct proxy *fe = strm_sess(s)->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008829
8830 txn->flags = 0;
8831 txn->status = -1;
8832
Willy Tarreauf64d1412010-10-07 20:06:11 +02008833 txn->cookie_first_date = 0;
8834 txn->cookie_last_date = 0;
8835
Willy Tarreaueee5b512015-04-03 23:46:31 +02008836 txn->sessid = NULL;
8837 txn->srv_cookie = NULL;
8838 txn->cli_cookie = NULL;
8839 txn->uri = NULL;
8840
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008841 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008842 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008843 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008844 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008845 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008846 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008847 txn->req.chunk_len = 0LL;
8848 txn->req.body_len = 0LL;
8849 txn->rsp.chunk_len = 0LL;
8850 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008851 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8852 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008853 txn->req.chn = &s->req;
8854 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008855
8856 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008857
8858 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8859 if (fe->options2 & PR_O2_REQBUG_OK)
8860 txn->req.err_pos = -1; /* let buggy requests pass */
8861
Willy Tarreau0937bc42009-12-22 15:03:09 +01008862 if (txn->hdr_idx.v)
8863 hdr_idx_init(&txn->hdr_idx);
8864}
8865
8866/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008867void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008868{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008869 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008870 struct proxy *fe = strm_sess(s)->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008871
Willy Tarreau75195602014-03-11 15:48:55 +01008872 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008873 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008874 s->comp_algo->end(&s->comp_ctx);
8875 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008876 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008877
Willy Tarreau0937bc42009-12-22 15:03:09 +01008878 /* these ones will have been dynamically allocated */
8879 pool_free2(pool2_requri, txn->uri);
8880 pool_free2(pool2_capture, txn->cli_cookie);
8881 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008882 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008883 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008884
William Lallemanda73203e2012-03-12 12:48:57 +01008885 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008886 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008887 txn->uri = NULL;
8888 txn->srv_cookie = NULL;
8889 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008890
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008891 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008892 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008893 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008894 pool_free2(h->pool, s->req_cap[h->index]);
8895 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008896 }
8897
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008898 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008899 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008900 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008901 pool_free2(h->pool, s->res_cap[h->index]);
8902 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008903 }
8904
Willy Tarreau0937bc42009-12-22 15:03:09 +01008905}
8906
8907/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008908void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008909{
8910 http_end_txn(s);
8911 http_init_txn(s);
8912
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008913 /* reinitialise the current rule list pointer to NULL. We are sure that
8914 * any rulelist match the NULL pointer.
8915 */
8916 s->current_rule_list = NULL;
8917
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008918 s->be = strm_sess(s)->fe;
8919 s->logs.logwait = strm_sess(s)->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008920 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008921 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008922 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008923 /* re-init store persistence */
8924 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008925 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008926
Willy Tarreau0937bc42009-12-22 15:03:09 +01008927 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008928
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008929 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008930
Willy Tarreau739cfba2010-01-25 23:11:14 +01008931 /* We must trim any excess data from the response buffer, because we
8932 * may have blocked an invalid response from a server that we don't
8933 * want to accidentely forward once we disable the analysers, nor do
8934 * we want those data to come along with next response. A typical
8935 * example of such data would be from a buggy server responding to
8936 * a HEAD with some data, or sending more than the advertised
8937 * content-length.
8938 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008939 if (unlikely(s->res.buf->i))
8940 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008941
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008942 s->req.rto = strm_sess(s)->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008943 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008944
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008945 s->res.rto = TICK_ETERNITY;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008946 s->res.wto = strm_sess(s)->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008947
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008948 s->req.rex = TICK_ETERNITY;
8949 s->req.wex = TICK_ETERNITY;
8950 s->req.analyse_exp = TICK_ETERNITY;
8951 s->res.rex = TICK_ETERNITY;
8952 s->res.wex = TICK_ETERNITY;
8953 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008954}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008955
Sasha Pachev218f0642014-06-16 12:05:59 -06008956void free_http_res_rules(struct list *r)
8957{
8958 struct http_res_rule *tr, *pr;
8959
8960 list_for_each_entry_safe(pr, tr, r, list) {
8961 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008962 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008963 free(pr);
8964 }
8965}
8966
8967void free_http_req_rules(struct list *r)
8968{
Willy Tarreauff011f22011-01-06 17:51:27 +01008969 struct http_req_rule *tr, *pr;
8970
8971 list_for_each_entry_safe(pr, tr, r, list) {
8972 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008973 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008974 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008975
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008976 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008977 free(pr);
8978 }
8979}
8980
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008981/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008982struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8983{
8984 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008985 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008986 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008987 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008988
8989 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8990 if (!rule) {
8991 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008992 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008993 }
8994
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008995 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008996 rule->action = HTTP_REQ_ACT_ALLOW;
8997 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008998 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008999 rule->action = HTTP_REQ_ACT_DENY;
9000 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009001 } else if (!strcmp(args[0], "tarpit")) {
9002 rule->action = HTTP_REQ_ACT_TARPIT;
9003 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009004 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009005 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009006 cur_arg = 1;
9007
9008 while(*args[cur_arg]) {
9009 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009010 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009011 cur_arg+=2;
9012 continue;
9013 } else
9014 break;
9015 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009016 } else if (!strcmp(args[0], "set-nice")) {
9017 rule->action = HTTP_REQ_ACT_SET_NICE;
9018 cur_arg = 1;
9019
9020 if (!*args[cur_arg] ||
9021 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9022 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9023 file, linenum, args[0]);
9024 goto out_err;
9025 }
9026 rule->arg.nice = atoi(args[cur_arg]);
9027 if (rule->arg.nice < -1024)
9028 rule->arg.nice = -1024;
9029 else if (rule->arg.nice > 1024)
9030 rule->arg.nice = 1024;
9031 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009032 } else if (!strcmp(args[0], "set-tos")) {
9033#ifdef IP_TOS
9034 char *err;
9035 rule->action = HTTP_REQ_ACT_SET_TOS;
9036 cur_arg = 1;
9037
9038 if (!*args[cur_arg] ||
9039 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9040 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9041 file, linenum, args[0]);
9042 goto out_err;
9043 }
9044
9045 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9046 if (err && *err != '\0') {
9047 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9048 file, linenum, err, args[0]);
9049 goto out_err;
9050 }
9051 cur_arg++;
9052#else
9053 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9054 goto out_err;
9055#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009056 } else if (!strcmp(args[0], "set-mark")) {
9057#ifdef SO_MARK
9058 char *err;
9059 rule->action = HTTP_REQ_ACT_SET_MARK;
9060 cur_arg = 1;
9061
9062 if (!*args[cur_arg] ||
9063 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9064 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9065 file, linenum, args[0]);
9066 goto out_err;
9067 }
9068
9069 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9070 if (err && *err != '\0') {
9071 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9072 file, linenum, err, args[0]);
9073 goto out_err;
9074 }
9075 cur_arg++;
9076 global.last_checks |= LSTCHK_NETADM;
9077#else
9078 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9079 goto out_err;
9080#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009081 } else if (!strcmp(args[0], "set-log-level")) {
9082 rule->action = HTTP_REQ_ACT_SET_LOGL;
9083 cur_arg = 1;
9084
9085 if (!*args[cur_arg] ||
9086 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9087 bad_log_level:
9088 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9089 file, linenum, args[0]);
9090 goto out_err;
9091 }
9092 if (strcmp(args[cur_arg], "silent") == 0)
9093 rule->arg.loglevel = -1;
9094 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9095 goto bad_log_level;
9096 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009097 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9098 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9099 cur_arg = 1;
9100
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009101 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9102 (*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 +01009103 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9104 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009105 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009106 }
9107
9108 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9109 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9110 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009111
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009112 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009113 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009114 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9115 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009116 free(proxy->conf.lfs_file);
9117 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9118 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009119 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009120 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9121 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009122 cur_arg = 1;
9123
9124 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009125 (*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 -06009126 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9127 file, linenum, args[0]);
9128 goto out_err;
9129 }
9130
9131 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9132 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9133 LIST_INIT(&rule->arg.hdr_add.fmt);
9134
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009135 error = NULL;
9136 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9137 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9138 args[cur_arg + 1], error);
9139 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009140 goto out_err;
9141 }
9142
9143 proxy->conf.args.ctx = ARGC_HRQ;
9144 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9145 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9146 file, linenum);
9147
9148 free(proxy->conf.lfs_file);
9149 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9150 proxy->conf.lfs_line = proxy->conf.args.line;
9151 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009152 } else if (strcmp(args[0], "del-header") == 0) {
9153 rule->action = HTTP_REQ_ACT_DEL_HDR;
9154 cur_arg = 1;
9155
9156 if (!*args[cur_arg] ||
9157 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9158 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9159 file, linenum, args[0]);
9160 goto out_err;
9161 }
9162
9163 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9164 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9165
9166 proxy->conf.args.ctx = ARGC_HRQ;
9167 free(proxy->conf.lfs_file);
9168 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9169 proxy->conf.lfs_line = proxy->conf.args.line;
9170 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009171 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9172 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009173 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009174 struct sample_expr *expr;
9175 unsigned int where;
9176 char *err = NULL;
9177
9178 cur_arg = 1;
9179 proxy->conf.args.ctx = ARGC_TRK;
9180
9181 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9182 if (!expr) {
9183 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9184 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9185 free(err);
9186 goto out_err;
9187 }
9188
9189 where = 0;
9190 if (proxy->cap & PR_CAP_FE)
9191 where |= SMP_VAL_FE_HRQ_HDR;
9192 if (proxy->cap & PR_CAP_BE)
9193 where |= SMP_VAL_BE_HRQ_HDR;
9194
9195 if (!(expr->fetch->val & where)) {
9196 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9197 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9198 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9199 args[cur_arg-1], sample_src_names(expr->fetch->use));
9200 free(expr);
9201 goto out_err;
9202 }
9203
9204 if (strcmp(args[cur_arg], "table") == 0) {
9205 cur_arg++;
9206 if (!args[cur_arg]) {
9207 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9208 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9209 free(expr);
9210 goto out_err;
9211 }
9212 /* we copy the table name for now, it will be resolved later */
9213 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9214 cur_arg++;
9215 }
9216 rule->act_prm.trk_ctr.expr = expr;
9217 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009218 } else if (strcmp(args[0], "redirect") == 0) {
9219 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009220 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009221
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009222 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009223 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9224 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9225 goto out_err;
9226 }
9227
9228 /* this redirect rule might already contain a parsed condition which
9229 * we'll pass to the http-request rule.
9230 */
9231 rule->action = HTTP_REQ_ACT_REDIR;
9232 rule->arg.redir = redir;
9233 rule->cond = redir->cond;
9234 redir->cond = NULL;
9235 cur_arg = 2;
9236 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009237 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9238 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9239 rule->action = HTTP_REQ_ACT_ADD_ACL;
9240 /*
9241 * '+ 8' for 'add-acl('
9242 * '- 9' for 'add-acl(' + trailing ')'
9243 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009244 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009245
9246 cur_arg = 1;
9247
9248 if (!*args[cur_arg] ||
9249 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9250 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9251 file, linenum, args[0]);
9252 goto out_err;
9253 }
9254
9255 LIST_INIT(&rule->arg.map.key);
9256 proxy->conf.args.ctx = ARGC_HRQ;
9257 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9258 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9259 file, linenum);
9260 free(proxy->conf.lfs_file);
9261 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9262 proxy->conf.lfs_line = proxy->conf.args.line;
9263 cur_arg += 1;
9264 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9265 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9266 rule->action = HTTP_REQ_ACT_DEL_ACL;
9267 /*
9268 * '+ 8' for 'del-acl('
9269 * '- 9' for 'del-acl(' + trailing ')'
9270 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009271 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009272
9273 cur_arg = 1;
9274
9275 if (!*args[cur_arg] ||
9276 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9277 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9278 file, linenum, args[0]);
9279 goto out_err;
9280 }
9281
9282 LIST_INIT(&rule->arg.map.key);
9283 proxy->conf.args.ctx = ARGC_HRQ;
9284 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9285 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9286 file, linenum);
9287 free(proxy->conf.lfs_file);
9288 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9289 proxy->conf.lfs_line = proxy->conf.args.line;
9290 cur_arg += 1;
9291 } else if (strncmp(args[0], "del-map", 7) == 0) {
9292 /* http-request del-map(<reference (map name)>) <key pattern> */
9293 rule->action = HTTP_REQ_ACT_DEL_MAP;
9294 /*
9295 * '+ 8' for 'del-map('
9296 * '- 9' for 'del-map(' + trailing ')'
9297 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009298 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009299
9300 cur_arg = 1;
9301
9302 if (!*args[cur_arg] ||
9303 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9304 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9305 file, linenum, args[0]);
9306 goto out_err;
9307 }
9308
9309 LIST_INIT(&rule->arg.map.key);
9310 proxy->conf.args.ctx = ARGC_HRQ;
9311 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9312 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9313 file, linenum);
9314 free(proxy->conf.lfs_file);
9315 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9316 proxy->conf.lfs_line = proxy->conf.args.line;
9317 cur_arg += 1;
9318 } else if (strncmp(args[0], "set-map", 7) == 0) {
9319 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9320 rule->action = HTTP_REQ_ACT_SET_MAP;
9321 /*
9322 * '+ 8' for 'set-map('
9323 * '- 9' for 'set-map(' + trailing ')'
9324 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009325 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009326
9327 cur_arg = 1;
9328
9329 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9330 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9331 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9332 file, linenum, args[0]);
9333 goto out_err;
9334 }
9335
9336 LIST_INIT(&rule->arg.map.key);
9337 LIST_INIT(&rule->arg.map.value);
9338 proxy->conf.args.ctx = ARGC_HRQ;
9339
9340 /* key pattern */
9341 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9342 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9343 file, linenum);
9344
9345 /* value pattern */
9346 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9347 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9348 file, linenum);
9349 free(proxy->conf.lfs_file);
9350 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9351 proxy->conf.lfs_line = proxy->conf.args.line;
9352
9353 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009354 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9355 char *errmsg = NULL;
9356 cur_arg = 1;
9357 /* try in the module list */
9358 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9359 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9360 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9361 free(errmsg);
9362 goto out_err;
9363 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009364 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009365 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', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009366 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009367 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009368 }
9369
9370 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9371 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009372 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009373
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009374 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9375 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9376 file, linenum, args[0], errmsg);
9377 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009378 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009379 }
9380 rule->cond = cond;
9381 }
9382 else if (*args[cur_arg]) {
9383 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9384 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9385 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009386 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009387 }
9388
9389 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009390 out_err:
9391 free(rule);
9392 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009393}
9394
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009395/* parse an "http-respose" rule */
9396struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9397{
9398 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009399 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009400 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009401 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009402
9403 rule = calloc(1, sizeof(*rule));
9404 if (!rule) {
9405 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9406 goto out_err;
9407 }
9408
9409 if (!strcmp(args[0], "allow")) {
9410 rule->action = HTTP_RES_ACT_ALLOW;
9411 cur_arg = 1;
9412 } else if (!strcmp(args[0], "deny")) {
9413 rule->action = HTTP_RES_ACT_DENY;
9414 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009415 } else if (!strcmp(args[0], "set-nice")) {
9416 rule->action = HTTP_RES_ACT_SET_NICE;
9417 cur_arg = 1;
9418
9419 if (!*args[cur_arg] ||
9420 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9421 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9422 file, linenum, args[0]);
9423 goto out_err;
9424 }
9425 rule->arg.nice = atoi(args[cur_arg]);
9426 if (rule->arg.nice < -1024)
9427 rule->arg.nice = -1024;
9428 else if (rule->arg.nice > 1024)
9429 rule->arg.nice = 1024;
9430 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009431 } else if (!strcmp(args[0], "set-tos")) {
9432#ifdef IP_TOS
9433 char *err;
9434 rule->action = HTTP_RES_ACT_SET_TOS;
9435 cur_arg = 1;
9436
9437 if (!*args[cur_arg] ||
9438 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9439 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9440 file, linenum, args[0]);
9441 goto out_err;
9442 }
9443
9444 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9445 if (err && *err != '\0') {
9446 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9447 file, linenum, err, args[0]);
9448 goto out_err;
9449 }
9450 cur_arg++;
9451#else
9452 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9453 goto out_err;
9454#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009455 } else if (!strcmp(args[0], "set-mark")) {
9456#ifdef SO_MARK
9457 char *err;
9458 rule->action = HTTP_RES_ACT_SET_MARK;
9459 cur_arg = 1;
9460
9461 if (!*args[cur_arg] ||
9462 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9463 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9464 file, linenum, args[0]);
9465 goto out_err;
9466 }
9467
9468 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9469 if (err && *err != '\0') {
9470 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9471 file, linenum, err, args[0]);
9472 goto out_err;
9473 }
9474 cur_arg++;
9475 global.last_checks |= LSTCHK_NETADM;
9476#else
9477 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9478 goto out_err;
9479#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009480 } else if (!strcmp(args[0], "set-log-level")) {
9481 rule->action = HTTP_RES_ACT_SET_LOGL;
9482 cur_arg = 1;
9483
9484 if (!*args[cur_arg] ||
9485 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9486 bad_log_level:
9487 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9488 file, linenum, args[0]);
9489 goto out_err;
9490 }
9491 if (strcmp(args[cur_arg], "silent") == 0)
9492 rule->arg.loglevel = -1;
9493 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9494 goto bad_log_level;
9495 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009496 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9497 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9498 cur_arg = 1;
9499
9500 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9501 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9502 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9503 file, linenum, args[0]);
9504 goto out_err;
9505 }
9506
9507 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9508 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9509 LIST_INIT(&rule->arg.hdr_add.fmt);
9510
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009511 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009512 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009513 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9514 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009515 free(proxy->conf.lfs_file);
9516 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9517 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009518 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009519 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009520 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009521 cur_arg = 1;
9522
9523 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009524 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9525 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009526 file, linenum, args[0]);
9527 goto out_err;
9528 }
9529
9530 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9531 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9532 LIST_INIT(&rule->arg.hdr_add.fmt);
9533
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009534 error = NULL;
9535 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9536 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9537 args[cur_arg + 1], error);
9538 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009539 goto out_err;
9540 }
9541
9542 proxy->conf.args.ctx = ARGC_HRQ;
9543 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9544 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9545 file, linenum);
9546
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 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009551 } else if (strcmp(args[0], "del-header") == 0) {
9552 rule->action = HTTP_RES_ACT_DEL_HDR;
9553 cur_arg = 1;
9554
9555 if (!*args[cur_arg] ||
9556 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9557 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9558 file, linenum, args[0]);
9559 goto out_err;
9560 }
9561
9562 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9563 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9564
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009565 proxy->conf.args.ctx = ARGC_HRS;
9566 free(proxy->conf.lfs_file);
9567 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9568 proxy->conf.lfs_line = proxy->conf.args.line;
9569 cur_arg += 1;
9570 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9571 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9572 rule->action = HTTP_RES_ACT_ADD_ACL;
9573 /*
9574 * '+ 8' for 'add-acl('
9575 * '- 9' for 'add-acl(' + trailing ')'
9576 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009577 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009578
9579 cur_arg = 1;
9580
9581 if (!*args[cur_arg] ||
9582 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9583 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9584 file, linenum, args[0]);
9585 goto out_err;
9586 }
9587
9588 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009589 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009590 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9591 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9592 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009593 free(proxy->conf.lfs_file);
9594 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9595 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009596
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009597 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009598 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9599 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9600 rule->action = HTTP_RES_ACT_DEL_ACL;
9601 /*
9602 * '+ 8' for 'del-acl('
9603 * '- 9' for 'del-acl(' + trailing ')'
9604 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009605 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009606
9607 cur_arg = 1;
9608
9609 if (!*args[cur_arg] ||
9610 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9611 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9612 file, linenum, args[0]);
9613 goto out_err;
9614 }
9615
9616 LIST_INIT(&rule->arg.map.key);
9617 proxy->conf.args.ctx = ARGC_HRS;
9618 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9619 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9620 file, linenum);
9621 free(proxy->conf.lfs_file);
9622 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9623 proxy->conf.lfs_line = proxy->conf.args.line;
9624 cur_arg += 1;
9625 } else if (strncmp(args[0], "del-map", 7) == 0) {
9626 /* http-response del-map(<reference (map name)>) <key pattern> */
9627 rule->action = HTTP_RES_ACT_DEL_MAP;
9628 /*
9629 * '+ 8' for 'del-map('
9630 * '- 9' for 'del-map(' + trailing ')'
9631 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009632 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009633
9634 cur_arg = 1;
9635
9636 if (!*args[cur_arg] ||
9637 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9638 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9639 file, linenum, args[0]);
9640 goto out_err;
9641 }
9642
9643 LIST_INIT(&rule->arg.map.key);
9644 proxy->conf.args.ctx = ARGC_HRS;
9645 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9646 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9647 file, linenum);
9648 free(proxy->conf.lfs_file);
9649 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9650 proxy->conf.lfs_line = proxy->conf.args.line;
9651 cur_arg += 1;
9652 } else if (strncmp(args[0], "set-map", 7) == 0) {
9653 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9654 rule->action = HTTP_RES_ACT_SET_MAP;
9655 /*
9656 * '+ 8' for 'set-map('
9657 * '- 9' for 'set-map(' + trailing ')'
9658 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009659 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009660
9661 cur_arg = 1;
9662
9663 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9664 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9665 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9666 file, linenum, args[0]);
9667 goto out_err;
9668 }
9669
9670 LIST_INIT(&rule->arg.map.key);
9671 LIST_INIT(&rule->arg.map.value);
9672
9673 proxy->conf.args.ctx = ARGC_HRS;
9674
9675 /* key pattern */
9676 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9677 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9678 file, linenum);
9679
9680 /* value pattern */
9681 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9682 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9683 file, linenum);
9684
9685 free(proxy->conf.lfs_file);
9686 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9687 proxy->conf.lfs_line = proxy->conf.args.line;
9688
9689 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009690 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9691 char *errmsg = NULL;
9692 cur_arg = 1;
9693 /* try in the module list */
9694 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9695 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9696 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9697 free(errmsg);
9698 goto out_err;
9699 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009700 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009701 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', but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009702 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9703 goto out_err;
9704 }
9705
9706 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9707 struct acl_cond *cond;
9708 char *errmsg = NULL;
9709
9710 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9711 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9712 file, linenum, args[0], errmsg);
9713 free(errmsg);
9714 goto out_err;
9715 }
9716 rule->cond = cond;
9717 }
9718 else if (*args[cur_arg]) {
9719 Alert("parsing [%s:%d]: 'http-response %s' expects"
9720 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9721 file, linenum, args[0], args[cur_arg]);
9722 goto out_err;
9723 }
9724
9725 return rule;
9726 out_err:
9727 free(rule);
9728 return NULL;
9729}
9730
Willy Tarreau4baae242012-12-27 12:00:31 +01009731/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009732 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9733 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009734 */
9735struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009736 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009737{
9738 struct redirect_rule *rule;
9739 int cur_arg;
9740 int type = REDIRECT_TYPE_NONE;
9741 int code = 302;
9742 const char *destination = NULL;
9743 const char *cookie = NULL;
9744 int cookie_set = 0;
9745 unsigned int flags = REDIRECT_FLAG_NONE;
9746 struct acl_cond *cond = NULL;
9747
9748 cur_arg = 0;
9749 while (*(args[cur_arg])) {
9750 if (strcmp(args[cur_arg], "location") == 0) {
9751 if (!*args[cur_arg + 1])
9752 goto missing_arg;
9753
9754 type = REDIRECT_TYPE_LOCATION;
9755 cur_arg++;
9756 destination = args[cur_arg];
9757 }
9758 else if (strcmp(args[cur_arg], "prefix") == 0) {
9759 if (!*args[cur_arg + 1])
9760 goto missing_arg;
9761
9762 type = REDIRECT_TYPE_PREFIX;
9763 cur_arg++;
9764 destination = args[cur_arg];
9765 }
9766 else if (strcmp(args[cur_arg], "scheme") == 0) {
9767 if (!*args[cur_arg + 1])
9768 goto missing_arg;
9769
9770 type = REDIRECT_TYPE_SCHEME;
9771 cur_arg++;
9772 destination = args[cur_arg];
9773 }
9774 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9775 if (!*args[cur_arg + 1])
9776 goto missing_arg;
9777
9778 cur_arg++;
9779 cookie = args[cur_arg];
9780 cookie_set = 1;
9781 }
9782 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9783 if (!*args[cur_arg + 1])
9784 goto missing_arg;
9785
9786 cur_arg++;
9787 cookie = args[cur_arg];
9788 cookie_set = 0;
9789 }
9790 else if (strcmp(args[cur_arg], "code") == 0) {
9791 if (!*args[cur_arg + 1])
9792 goto missing_arg;
9793
9794 cur_arg++;
9795 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009796 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009797 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009798 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009799 args[cur_arg - 1], args[cur_arg]);
9800 return NULL;
9801 }
9802 }
9803 else if (!strcmp(args[cur_arg],"drop-query")) {
9804 flags |= REDIRECT_FLAG_DROP_QS;
9805 }
9806 else if (!strcmp(args[cur_arg],"append-slash")) {
9807 flags |= REDIRECT_FLAG_APPEND_SLASH;
9808 }
9809 else if (strcmp(args[cur_arg], "if") == 0 ||
9810 strcmp(args[cur_arg], "unless") == 0) {
9811 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9812 if (!cond) {
9813 memprintf(errmsg, "error in condition: %s", *errmsg);
9814 return NULL;
9815 }
9816 break;
9817 }
9818 else {
9819 memprintf(errmsg,
9820 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9821 args[cur_arg]);
9822 return NULL;
9823 }
9824 cur_arg++;
9825 }
9826
9827 if (type == REDIRECT_TYPE_NONE) {
9828 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9829 return NULL;
9830 }
9831
9832 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9833 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009834 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009835
9836 if (!use_fmt) {
9837 /* old-style static redirect rule */
9838 rule->rdr_str = strdup(destination);
9839 rule->rdr_len = strlen(destination);
9840 }
9841 else {
9842 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009843
9844 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9845 * if prefix == "/", we don't want to add anything, otherwise it
9846 * makes it hard for the user to configure a self-redirection.
9847 */
Godbachd9722032014-12-18 15:44:58 +08009848 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009849 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009850 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009851 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9852 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009853 free(curproxy->conf.lfs_file);
9854 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9855 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009856 }
9857 }
9858
Willy Tarreau4baae242012-12-27 12:00:31 +01009859 if (cookie) {
9860 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9861 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9862 */
9863 rule->cookie_len = strlen(cookie);
9864 if (cookie_set) {
9865 rule->cookie_str = malloc(rule->cookie_len + 10);
9866 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9867 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9868 rule->cookie_len += 9;
9869 } else {
9870 rule->cookie_str = malloc(rule->cookie_len + 21);
9871 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9872 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9873 rule->cookie_len += 20;
9874 }
9875 }
9876 rule->type = type;
9877 rule->code = code;
9878 rule->flags = flags;
9879 LIST_INIT(&rule->list);
9880 return rule;
9881
9882 missing_arg:
9883 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9884 return NULL;
9885}
9886
Willy Tarreau8797c062007-05-07 00:55:35 +02009887/************************************************************************/
9888/* The code below is dedicated to ACL parsing and matching */
9889/************************************************************************/
9890
9891
Willy Tarreau14174bc2012-04-16 14:34:04 +02009892/* This function ensures that the prerequisites for an L7 fetch are ready,
9893 * which means that a request or response is ready. If some data is missing,
9894 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009895 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9896 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009897 *
9898 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009899 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9900 * decide whether or not an HTTP message is present ;
9901 * 0 if the requested data cannot be fetched or if it is certain that
9902 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009903 * 1 if an HTTP message is ready
9904 */
9905static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02009906smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009907 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009908{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009909 struct http_txn *txn = s->txn;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009910 struct http_msg *msg = &txn->req;
9911
Willy Tarreaueee5b512015-04-03 23:46:31 +02009912 /* Note: this function may only be used from places where
9913 * http_init_txn() has already been done, and implies that <s>,
9914 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
9915 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009916 */
9917
9918 if (unlikely(!s || !txn))
9919 return 0;
9920
9921 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009922 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009923
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009924 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009925 /* If the buffer does not leave enough free space at the end,
9926 * we must first realign it.
9927 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009928 if (s->req.buf->p > s->req.buf->data &&
9929 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9930 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009931
Willy Tarreau14174bc2012-04-16 14:34:04 +02009932 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009933 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009934 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009935
9936 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009937 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009938 http_msg_analyzer(msg, &txn->hdr_idx);
9939
9940 /* Still no valid request ? */
9941 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009942 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009943 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009944 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009945 }
9946 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009947 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009948 return 0;
9949 }
9950
9951 /* OK we just got a valid HTTP request. We have some minor
9952 * preparation to perform so that further checks can rely
9953 * on HTTP tests.
9954 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009955
9956 /* If the request was parsed but was too large, we must absolutely
9957 * return an error so that it is not processed. At the moment this
9958 * cannot happen, but if the parsers are to change in the future,
9959 * we want this check to be maintained.
9960 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009961 if (unlikely(s->req.buf->i + s->req.buf->p >
9962 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009963 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009964 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009965 return 1;
9966 }
9967
Willy Tarreau9b28e032012-10-12 23:49:43 +02009968 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009969 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009970 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009971
Willy Tarreau506d0502013-07-06 13:29:24 +02009972 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9973 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009974 }
9975
Willy Tarreau506d0502013-07-06 13:29:24 +02009976 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009977 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009978 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009979
9980 /* otherwise everything's ready for the request */
9981 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009982 else {
9983 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009984 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9985 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009986 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009987 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009988 }
9989
9990 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009991 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009992 return 1;
9993}
Willy Tarreau8797c062007-05-07 00:55:35 +02009994
Willy Tarreau6c616e02014-06-25 16:56:41 +02009995/* Note: these functinos *do* modify the sample. Even in case of success, at
9996 * least the type and uint value are modified.
9997 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009998#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau15e91e12015-04-04 00:52:09 +02009999 do { int r = smp_prefetch_http(px, strm, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +020010000
Willy Tarreau24e32d82012-04-23 23:55:44 +020010001#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010002 do { int r = smp_prefetch_http(px, strm, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +020010003
Willy Tarreau8797c062007-05-07 00:55:35 +020010004
10005/* 1. Check on METHOD
10006 * We use the pre-parsed method if it is known, and store its number as an
10007 * integer. If it is unknown, we use the pointer and the length.
10008 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010009static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010010{
10011 int len, meth;
10012
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010013 len = strlen(text);
10014 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010015
10016 pattern->val.i = meth;
10017 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010018 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010019 pattern->len = len;
10020 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010021 else {
10022 pattern->ptr.str = NULL;
10023 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010024 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010025 return 1;
10026}
10027
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010028/* This function fetches the method of current HTTP request and stores
10029 * it in the global pattern struct as a chunk. There are two possibilities :
10030 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10031 * in <len> and <ptr> is NULL ;
10032 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10033 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010034 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010035 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010036static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010037smp_fetch_meth(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010038 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010039{
10040 int meth;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010041 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010042
Willy Tarreau24e32d82012-04-23 23:55:44 +020010043 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010044
Willy Tarreau8797c062007-05-07 00:55:35 +020010045 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010046 smp->type = SMP_T_METH;
10047 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010048 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010049 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10050 /* ensure the indexes are not affected */
10051 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010052 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010053 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10054 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010055 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010056 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010057 return 1;
10058}
10059
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010060/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010061static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010062{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010063 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010064 struct pattern_list *lst;
10065 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010066
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010067 list_for_each_entry(lst, &expr->patterns, list) {
10068 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010069
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010070 /* well-known method */
10071 if (pattern->val.i != HTTP_METH_OTHER) {
10072 if (smp->data.meth.meth == pattern->val.i)
10073 return pattern;
10074 else
10075 continue;
10076 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010077
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010078 /* Other method, we must compare the strings */
10079 if (pattern->len != smp->data.meth.str.len)
10080 continue;
10081
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010082 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010083 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10084 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010085 return pattern;
10086 }
10087 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010088}
10089
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010090static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010091smp_fetch_rqver(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010092 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010093{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010094 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010095 char *ptr;
10096 int len;
10097
Willy Tarreauc0239e02012-04-16 14:42:55 +020010098 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010099
Willy Tarreau8797c062007-05-07 00:55:35 +020010100 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010101 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010102
10103 while ((len-- > 0) && (*ptr++ != '/'));
10104 if (len <= 0)
10105 return 0;
10106
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010107 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010108 smp->data.str.str = ptr;
10109 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010110
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010111 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010112 return 1;
10113}
10114
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010115static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010116smp_fetch_stver(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010117 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010118{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010119 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010120 char *ptr;
10121 int len;
10122
Willy Tarreauc0239e02012-04-16 14:42:55 +020010123 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010124
Willy Tarreau15e91e12015-04-04 00:52:09 +020010125 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010126 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10127 return 0;
10128
Willy Tarreau8797c062007-05-07 00:55:35 +020010129 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010130 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010131
10132 while ((len-- > 0) && (*ptr++ != '/'));
10133 if (len <= 0)
10134 return 0;
10135
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010136 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010137 smp->data.str.str = ptr;
10138 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010139
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010140 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010141 return 1;
10142}
10143
10144/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010145static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010146smp_fetch_stcode(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010147 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010148{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010149 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010150 char *ptr;
10151 int len;
10152
Willy Tarreauc0239e02012-04-16 14:42:55 +020010153 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010154
Willy Tarreau15e91e12015-04-04 00:52:09 +020010155 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010156 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10157 return 0;
10158
Willy Tarreau8797c062007-05-07 00:55:35 +020010159 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010160 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010161
Willy Tarreauf853c462012-04-23 18:53:56 +020010162 smp->type = SMP_T_UINT;
10163 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010164 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010165 return 1;
10166}
10167
10168/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010169static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010170smp_fetch_url(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010171 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010172{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010173 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010174
Willy Tarreauc0239e02012-04-16 14:42:55 +020010175 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010176
Willy Tarreau15e91e12015-04-04 00:52:09 +020010177 txn = strm->txn;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010178 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010179 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010180 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010181 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010182 return 1;
10183}
10184
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010185static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010186smp_fetch_url_ip(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010187 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010188{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010189 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010190 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010191
Willy Tarreauc0239e02012-04-16 14:42:55 +020010192 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010193
Willy Tarreau15e91e12015-04-04 00:52:09 +020010194 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010195 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010196 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010197 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010198
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010199 smp->type = SMP_T_IPV4;
10200 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010201 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010202 return 1;
10203}
10204
10205static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010206smp_fetch_url_port(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010207 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010208{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010209 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010210 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010211
Willy Tarreauc0239e02012-04-16 14:42:55 +020010212 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010213
Willy Tarreau15e91e12015-04-04 00:52:09 +020010214 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010215 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 +020010216 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10217 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010218
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010219 smp->type = SMP_T_UINT;
10220 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010221 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010222 return 1;
10223}
10224
Willy Tarreau185b5c42012-04-26 15:11:51 +020010225/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10226 * Accepts an optional argument of type string containing the header field name,
10227 * and an optional argument of type signed or unsigned integer to request an
10228 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010229 * headers are considered from the first one. It does not stop on commas and
10230 * returns full lines instead (useful for User-Agent or Date for example).
10231 */
10232static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010233smp_fetch_fhdr(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010234 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010235{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010236 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010237 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010238 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010239 int occ = 0;
10240 const char *name_str = NULL;
10241 int name_len = 0;
10242
10243 if (!ctx) {
10244 /* first call */
10245 ctx = &static_hdr_ctx;
10246 ctx->idx = 0;
10247 smp->ctx.a[0] = ctx;
10248 }
10249
10250 if (args) {
10251 if (args[0].type != ARGT_STR)
10252 return 0;
10253 name_str = args[0].data.str.str;
10254 name_len = args[0].data.str.len;
10255
10256 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10257 occ = args[1].data.uint;
10258 }
10259
10260 CHECK_HTTP_MESSAGE_FIRST();
10261
Willy Tarreau15e91e12015-04-04 00:52:09 +020010262 idx = &strm->txn->hdr_idx;
10263 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10264
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010265 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10266 /* search for header from the beginning */
10267 ctx->idx = 0;
10268
10269 if (!occ && !(opt & SMP_OPT_ITERATE))
10270 /* no explicit occurrence and single fetch => last header by default */
10271 occ = -1;
10272
10273 if (!occ)
10274 /* prepare to report multiple occurrences for ACL fetches */
10275 smp->flags |= SMP_F_NOT_LAST;
10276
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010277 smp->type = SMP_T_STR;
10278 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010279 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10280 return 1;
10281
10282 smp->flags &= ~SMP_F_NOT_LAST;
10283 return 0;
10284}
10285
10286/* 6. Check on HTTP header count. The number of occurrences is returned.
10287 * Accepts exactly 1 argument of type string. It does not stop on commas and
10288 * returns full lines instead (useful for User-Agent or Date for example).
10289 */
10290static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010291smp_fetch_fhdr_cnt(struct proxy *px, struct stream *strm, unsigned int opt,
10292 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010293{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010294 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010295 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010296 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010297 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010298 const char *name = NULL;
10299 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010300
Willy Tarreau601a4d12015-04-01 19:16:09 +020010301 if (args && args->type == ARGT_STR) {
10302 name = args->data.str.str;
10303 len = args->data.str.len;
10304 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010305
10306 CHECK_HTTP_MESSAGE_FIRST();
10307
Willy Tarreau15e91e12015-04-04 00:52:09 +020010308 idx = &strm->txn->hdr_idx;
10309 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10310
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010311 ctx.idx = 0;
10312 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010313 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010314 cnt++;
10315
10316 smp->type = SMP_T_UINT;
10317 smp->data.uint = cnt;
10318 smp->flags = SMP_F_VOL_HDR;
10319 return 1;
10320}
10321
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010322static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010323smp_fetch_hdr_names(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010324 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010325{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010326 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010327 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010328 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010329 struct chunk *temp;
10330 char del = ',';
10331
10332 if (args && args->type == ARGT_STR)
10333 del = *args[0].data.str.str;
10334
10335 CHECK_HTTP_MESSAGE_FIRST();
10336
Willy Tarreau15e91e12015-04-04 00:52:09 +020010337 idx = &strm->txn->hdr_idx;
10338 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10339
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010340 temp = get_trash_chunk();
10341
10342 ctx.idx = 0;
10343 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10344 if (temp->len)
10345 temp->str[temp->len++] = del;
10346 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10347 temp->len += ctx.del;
10348 }
10349
10350 smp->type = SMP_T_STR;
10351 smp->data.str.str = temp->str;
10352 smp->data.str.len = temp->len;
10353 smp->flags = SMP_F_VOL_HDR;
10354 return 1;
10355}
10356
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010357/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10358 * Accepts an optional argument of type string containing the header field name,
10359 * and an optional argument of type signed or unsigned integer to request an
10360 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010361 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010362 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010363static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010364smp_fetch_hdr(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010365 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010366{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010367 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010368 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010369 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010370 int occ = 0;
10371 const char *name_str = NULL;
10372 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010373
Willy Tarreaua890d072013-04-02 12:01:06 +020010374 if (!ctx) {
10375 /* first call */
10376 ctx = &static_hdr_ctx;
10377 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010378 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010379 }
10380
Willy Tarreau185b5c42012-04-26 15:11:51 +020010381 if (args) {
10382 if (args[0].type != ARGT_STR)
10383 return 0;
10384 name_str = args[0].data.str.str;
10385 name_len = args[0].data.str.len;
10386
10387 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10388 occ = args[1].data.uint;
10389 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010390
Willy Tarreaue333ec92012-04-16 16:26:40 +020010391 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010392
Willy Tarreau15e91e12015-04-04 00:52:09 +020010393 idx = &strm->txn->hdr_idx;
10394 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10395
Willy Tarreau185b5c42012-04-26 15:11:51 +020010396 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010397 /* search for header from the beginning */
10398 ctx->idx = 0;
10399
Willy Tarreau185b5c42012-04-26 15:11:51 +020010400 if (!occ && !(opt & SMP_OPT_ITERATE))
10401 /* no explicit occurrence and single fetch => last header by default */
10402 occ = -1;
10403
10404 if (!occ)
10405 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010406 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010407
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010408 smp->type = SMP_T_STR;
10409 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010410 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 +020010411 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010412
Willy Tarreau37406352012-04-23 16:16:37 +020010413 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010414 return 0;
10415}
10416
Willy Tarreauc11416f2007-06-17 16:58:38 +020010417/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010418 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010419 */
10420static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010421smp_fetch_hdr_cnt(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010422 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010423{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010424 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010425 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010426 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010427 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010428 const char *name = NULL;
10429 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010430
Willy Tarreau601a4d12015-04-01 19:16:09 +020010431 if (args && args->type == ARGT_STR) {
10432 name = args->data.str.str;
10433 len = args->data.str.len;
10434 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010435
Willy Tarreaue333ec92012-04-16 16:26:40 +020010436 CHECK_HTTP_MESSAGE_FIRST();
10437
Willy Tarreau15e91e12015-04-04 00:52:09 +020010438 idx = &strm->txn->hdr_idx;
10439 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10440
Willy Tarreau33a7e692007-06-10 19:45:56 +020010441 ctx.idx = 0;
10442 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010443 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010444 cnt++;
10445
Willy Tarreauf853c462012-04-23 18:53:56 +020010446 smp->type = SMP_T_UINT;
10447 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010448 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010449 return 1;
10450}
10451
Willy Tarreau185b5c42012-04-26 15:11:51 +020010452/* Fetch an HTTP header's integer value. The integer value is returned. It
10453 * takes a mandatory argument of type string and an optional one of type int
10454 * to designate a specific occurrence. It returns an unsigned integer, which
10455 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010456 */
10457static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010458smp_fetch_hdr_val(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010459 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010460{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010461 int ret = smp_fetch_hdr(px, strm, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010462
Willy Tarreauf853c462012-04-23 18:53:56 +020010463 if (ret > 0) {
10464 smp->type = SMP_T_UINT;
10465 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10466 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010467
Willy Tarreaud53e2422012-04-16 17:21:11 +020010468 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010469}
10470
Cyril Bonté69fa9922012-10-25 00:01:06 +020010471/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10472 * and an optional one of type int to designate a specific occurrence.
10473 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010474 */
10475static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010476smp_fetch_hdr_ip(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010477 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010478{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010479 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010480
Willy Tarreau15e91e12015-04-04 00:52:09 +020010481 while ((ret = smp_fetch_hdr(px, strm, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010482 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10483 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010484 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010485 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010486 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010487 if (smp->data.str.len < temp->size - 1) {
10488 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10489 temp->str[smp->data.str.len] = '\0';
10490 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10491 smp->type = SMP_T_IPV6;
10492 break;
10493 }
10494 }
10495 }
10496
Willy Tarreaud53e2422012-04-16 17:21:11 +020010497 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010498 if (!(smp->flags & SMP_F_NOT_LAST))
10499 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010500 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010501 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010502}
10503
Willy Tarreau737b0c12007-06-10 21:28:46 +020010504/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10505 * the first '/' after the possible hostname, and ends before the possible '?'.
10506 */
10507static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010508smp_fetch_path(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010509 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010510{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010511 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010512 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010513
Willy Tarreauc0239e02012-04-16 14:42:55 +020010514 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010515
Willy Tarreau15e91e12015-04-04 00:52:09 +020010516 txn = strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010517 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010518 ptr = http_get_path(txn);
10519 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010520 return 0;
10521
10522 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010523 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010524 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010525
10526 while (ptr < end && *ptr != '?')
10527 ptr++;
10528
Willy Tarreauf853c462012-04-23 18:53:56 +020010529 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010530 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010531 return 1;
10532}
10533
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010534/* This produces a concatenation of the first occurrence of the Host header
10535 * followed by the path component if it begins with a slash ('/'). This means
10536 * that '*' will not be added, resulting in exactly the first Host entry.
10537 * If no Host header is found, then the path is returned as-is. The returned
10538 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010539 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010540 */
10541static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010542smp_fetch_base(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010543 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010544{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010545 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010546 char *ptr, *end, *beg;
10547 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010548 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010549
10550 CHECK_HTTP_MESSAGE_FIRST();
10551
Willy Tarreau15e91e12015-04-04 00:52:09 +020010552 txn = strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010553 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010554 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreau15e91e12015-04-04 00:52:09 +020010555 return smp_fetch_path(px, strm, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010556
10557 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010558 temp = get_trash_chunk();
10559 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010560 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010561 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010562 smp->data.str.len = ctx.vlen;
10563
10564 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010565 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010566 beg = http_get_path(txn);
10567 if (!beg)
10568 beg = end;
10569
10570 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10571
10572 if (beg < ptr && *beg == '/') {
10573 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10574 smp->data.str.len += ptr - beg;
10575 }
10576
10577 smp->flags = SMP_F_VOL_1ST;
10578 return 1;
10579}
10580
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010581/* This produces a 32-bit hash of the concatenation of the first occurrence of
10582 * the Host header followed by the path component if it begins with a slash ('/').
10583 * This means that '*' will not be added, resulting in exactly the first Host
10584 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010585 * is hashed using the path hash followed by a full avalanche hash and provides a
10586 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010587 * high-traffic sites without having to store whole paths.
10588 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010589int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010590smp_fetch_base32(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010591 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010592{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010593 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010594 struct hdr_ctx ctx;
10595 unsigned int hash = 0;
10596 char *ptr, *beg, *end;
10597 int len;
10598
10599 CHECK_HTTP_MESSAGE_FIRST();
10600
Willy Tarreau15e91e12015-04-04 00:52:09 +020010601 txn = strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010602 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010603 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010604 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10605 ptr = ctx.line + ctx.val;
10606 len = ctx.vlen;
10607 while (len--)
10608 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10609 }
10610
10611 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010612 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010613 beg = http_get_path(txn);
10614 if (!beg)
10615 beg = end;
10616
10617 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10618
10619 if (beg < ptr && *beg == '/') {
10620 while (beg < ptr)
10621 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10622 }
10623 hash = full_hash(hash);
10624
10625 smp->type = SMP_T_UINT;
10626 smp->data.uint = hash;
10627 smp->flags = SMP_F_VOL_1ST;
10628 return 1;
10629}
10630
Willy Tarreau4a550602012-12-09 14:53:32 +010010631/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010632 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10633 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10634 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010635 * that in environments where IPv6 is insignificant, truncating the output to
10636 * 8 bytes would still work.
10637 */
10638static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010639smp_fetch_base32_src(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010640 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010641{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010642 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010643 struct session *sess = strm_sess(strm);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020010644 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010645
10646 if (!cli_conn)
10647 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010648
Willy Tarreau15e91e12015-04-04 00:52:09 +020010649 if (!smp_fetch_base32(px, strm, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010650 return 0;
10651
Willy Tarreau47ca5452012-12-23 20:22:19 +010010652 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010653 *(unsigned int *)temp->str = htonl(smp->data.uint);
10654 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010655
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010656 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010657 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010658 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010659 temp->len += 4;
10660 break;
10661 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010662 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010663 temp->len += 16;
10664 break;
10665 default:
10666 return 0;
10667 }
10668
10669 smp->data.str = *temp;
10670 smp->type = SMP_T_BIN;
10671 return 1;
10672}
10673
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010674/* Extracts the query string, which comes after the question mark '?'. If no
10675 * question mark is found, nothing is returned. Otherwise it returns a sample
10676 * of type string carrying the whole query string.
10677 */
10678static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010679smp_fetch_query(struct proxy *px, struct stream *strm, unsigned int opt,
10680 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010681{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010682 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010683 char *ptr, *end;
10684
10685 CHECK_HTTP_MESSAGE_FIRST();
10686
Willy Tarreau15e91e12015-04-04 00:52:09 +020010687 txn = strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010688 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10689 end = ptr + txn->req.sl.rq.u_l;
10690
10691 /* look up the '?' */
10692 do {
10693 if (ptr == end)
10694 return 0;
10695 } while (*ptr++ != '?');
10696
10697 smp->type = SMP_T_STR;
10698 smp->data.str.str = ptr;
10699 smp->data.str.len = end - ptr;
10700 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10701 return 1;
10702}
10703
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010704static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010705smp_fetch_proto_http(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010706 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010707{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010708 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10709 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10710 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010711
Willy Tarreau24e32d82012-04-23 23:55:44 +020010712 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010713
Willy Tarreauf853c462012-04-23 18:53:56 +020010714 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010715 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010716 return 1;
10717}
10718
Willy Tarreau7f18e522010-10-22 20:04:13 +020010719/* return a valid test if the current request is the first one on the connection */
10720static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010721smp_fetch_http_first_req(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010722 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010723{
Willy Tarreauf853c462012-04-23 18:53:56 +020010724 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010725 smp->data.uint = !(strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010726 return 1;
10727}
10728
Willy Tarreau34db1082012-04-19 17:16:54 +020010729/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010730static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010731smp_fetch_http_auth(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010732 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010733{
10734
Willy Tarreau24e32d82012-04-23 23:55:44 +020010735 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010736 return 0;
10737
Willy Tarreauc0239e02012-04-16 14:42:55 +020010738 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010739
Willy Tarreau15e91e12015-04-04 00:52:09 +020010740 if (!get_http_auth(strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010741 return 0;
10742
Willy Tarreauf853c462012-04-23 18:53:56 +020010743 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010744 smp->data.uint = check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010745 return 1;
10746}
Willy Tarreau8797c062007-05-07 00:55:35 +020010747
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010748/* Accepts exactly 1 argument of type userlist */
10749static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010750smp_fetch_http_auth_grp(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010751 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010752{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010753 if (!args || args->type != ARGT_USR)
10754 return 0;
10755
10756 CHECK_HTTP_MESSAGE_FIRST();
10757
Willy Tarreau15e91e12015-04-04 00:52:09 +020010758 if (!get_http_auth(strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010759 return 0;
10760
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010761 /* if the user does not belong to the userlist or has a wrong password,
10762 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010763 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010764 */
Willy Tarreau15e91e12015-04-04 00:52:09 +020010765 if (!check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010766 return 0;
10767
10768 /* pat_match_auth() will need the user list */
10769 smp->ctx.a[0] = args->data.usr;
10770
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010771 smp->type = SMP_T_STR;
10772 smp->flags = SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010773 smp->data.str.str = strm->txn->auth.user;
10774 smp->data.str.len = strlen(strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010775
10776 return 1;
10777}
10778
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010779/* Try to find the next occurrence of a cookie name in a cookie header value.
10780 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10781 * the cookie value is returned into *value and *value_l, and the function
10782 * returns a pointer to the next pointer to search from if the value was found.
10783 * Otherwise if the cookie was not found, NULL is returned and neither value
10784 * nor value_l are touched. The input <hdr> string should first point to the
10785 * header's value, and the <hdr_end> pointer must point to the first character
10786 * not part of the value. <list> must be non-zero if value may represent a list
10787 * of values (cookie headers). This makes it faster to abort parsing when no
10788 * list is expected.
10789 */
10790static char *
10791extract_cookie_value(char *hdr, const char *hdr_end,
10792 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010793 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010794{
10795 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10796 char *next;
10797
10798 /* we search at least a cookie name followed by an equal, and more
10799 * generally something like this :
10800 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10801 */
10802 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10803 /* Iterate through all cookies on this line */
10804
10805 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10806 att_beg++;
10807
10808 /* find att_end : this is the first character after the last non
10809 * space before the equal. It may be equal to hdr_end.
10810 */
10811 equal = att_end = att_beg;
10812
10813 while (equal < hdr_end) {
10814 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10815 break;
10816 if (http_is_spht[(unsigned char)*equal++])
10817 continue;
10818 att_end = equal;
10819 }
10820
10821 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10822 * is between <att_beg> and <equal>, both may be identical.
10823 */
10824
10825 /* look for end of cookie if there is an equal sign */
10826 if (equal < hdr_end && *equal == '=') {
10827 /* look for the beginning of the value */
10828 val_beg = equal + 1;
10829 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10830 val_beg++;
10831
10832 /* find the end of the value, respecting quotes */
10833 next = find_cookie_value_end(val_beg, hdr_end);
10834
10835 /* make val_end point to the first white space or delimitor after the value */
10836 val_end = next;
10837 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10838 val_end--;
10839 } else {
10840 val_beg = val_end = next = equal;
10841 }
10842
10843 /* We have nothing to do with attributes beginning with '$'. However,
10844 * they will automatically be removed if a header before them is removed,
10845 * since they're supposed to be linked together.
10846 */
10847 if (*att_beg == '$')
10848 continue;
10849
10850 /* Ignore cookies with no equal sign */
10851 if (equal == next)
10852 continue;
10853
10854 /* Now we have the cookie name between att_beg and att_end, and
10855 * its value between val_beg and val_end.
10856 */
10857
10858 if (att_end - att_beg == cookie_name_l &&
10859 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10860 /* let's return this value and indicate where to go on from */
10861 *value = val_beg;
10862 *value_l = val_end - val_beg;
10863 return next + 1;
10864 }
10865
10866 /* Set-Cookie headers only have the name in the first attr=value part */
10867 if (!list)
10868 break;
10869 }
10870
10871 return NULL;
10872}
10873
William Lallemanda43ba4e2014-01-28 18:14:25 +010010874/* Fetch a captured HTTP request header. The index is the position of
10875 * the "capture" option in the configuration file
10876 */
10877static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010878smp_fetch_capture_header_req(struct proxy *px, struct stream *strm, unsigned int opt,
10879 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010880{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010881 struct proxy *fe = strm_sess(strm)->fe;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010882 int idx;
10883
10884 if (!args || args->type != ARGT_UINT)
10885 return 0;
10886
10887 idx = args->data.uint;
10888
Willy Tarreau15e91e12015-04-04 00:52:09 +020010889 if (idx > (fe->nb_req_cap - 1) || strm->req_cap == NULL || strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010890 return 0;
10891
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010892 smp->type = SMP_T_STR;
10893 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010894 smp->data.str.str = strm->req_cap[idx];
10895 smp->data.str.len = strlen(strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010896
10897 return 1;
10898}
10899
10900/* Fetch a captured HTTP response header. The index is the position of
10901 * the "capture" option in the configuration file
10902 */
10903static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010904smp_fetch_capture_header_res(struct proxy *px, struct stream *strm, unsigned int opt,
10905 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010906{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010907 struct proxy *fe = strm_sess(strm)->fe;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010908 int idx;
10909
10910 if (!args || args->type != ARGT_UINT)
10911 return 0;
10912
10913 idx = args->data.uint;
10914
Willy Tarreau15e91e12015-04-04 00:52:09 +020010915 if (idx > (fe->nb_rsp_cap - 1) || strm->res_cap == NULL || strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010916 return 0;
10917
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010918 smp->type = SMP_T_STR;
10919 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010920 smp->data.str.str = strm->res_cap[idx];
10921 smp->data.str.len = strlen(strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010922
10923 return 1;
10924}
10925
William Lallemand65ad6e12014-01-31 15:08:02 +010010926/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10927static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010928smp_fetch_capture_req_method(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010929 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010930{
10931 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010932 struct http_txn *txn = strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010933 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010934
Willy Tarreau15e91e12015-04-04 00:52:09 +020010935 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010936 return 0;
10937
William Lallemand96a77852014-02-05 00:30:02 +010010938 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010939
William Lallemand96a77852014-02-05 00:30:02 +010010940 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10941 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010942
William Lallemand96a77852014-02-05 00:30:02 +010010943 temp = get_trash_chunk();
10944 temp->str = txn->uri;
10945 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010946 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010947 smp->type = SMP_T_STR;
10948 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010949
10950 return 1;
10951
10952}
10953
10954/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10955static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010956smp_fetch_capture_req_uri(struct proxy *px, struct stream *strm, unsigned int opt,
10957 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010958{
10959 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010960 struct http_txn *txn = strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010961 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010962
Willy Tarreau15e91e12015-04-04 00:52:09 +020010963 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010964 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010965
William Lallemand65ad6e12014-01-31 15:08:02 +010010966 ptr = txn->uri;
10967
10968 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10969 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010970
William Lallemand65ad6e12014-01-31 15:08:02 +010010971 if (!*ptr)
10972 return 0;
10973
10974 ptr++; /* skip the space */
10975
10976 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010977 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010978 if (!ptr)
10979 return 0;
10980 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10981 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010982
10983 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010984 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010985 smp->type = SMP_T_STR;
10986 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010987
10988 return 1;
10989}
10990
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010991/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10992 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10993 */
10994static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020010995smp_fetch_capture_req_ver(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010996 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010997{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010998 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010999
Willy Tarreau15e91e12015-04-04 00:52:09 +020011000 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011001 return 0;
11002
11003 if (txn->req.flags & HTTP_MSGF_VER_11)
11004 smp->data.str.str = "HTTP/1.1";
11005 else
11006 smp->data.str.str = "HTTP/1.0";
11007
11008 smp->data.str.len = 8;
11009 smp->type = SMP_T_STR;
11010 smp->flags = SMP_F_CONST;
11011 return 1;
11012
11013}
11014
11015/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11016 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11017 */
11018static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020011019smp_fetch_capture_res_ver(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011020 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011021{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011022 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011023
Willy Tarreau15e91e12015-04-04 00:52:09 +020011024 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011025 return 0;
11026
11027 if (txn->rsp.flags & HTTP_MSGF_VER_11)
11028 smp->data.str.str = "HTTP/1.1";
11029 else
11030 smp->data.str.str = "HTTP/1.0";
11031
11032 smp->data.str.len = 8;
11033 smp->type = SMP_T_STR;
11034 smp->flags = SMP_F_CONST;
11035 return 1;
11036
11037}
11038
William Lallemand65ad6e12014-01-31 15:08:02 +010011039
Willy Tarreaue333ec92012-04-16 16:26:40 +020011040/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011041 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011042 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011043 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011044 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011045 * Accepts exactly 1 argument of type string. If the input options indicate
11046 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011047 * The returned sample is of type CSTR. Can be used to parse cookies in other
11048 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011049 */
Willy Tarreau15e91e12015-04-04 00:52:09 +020011050int smp_fetch_cookie(struct proxy *px, struct stream *strm, unsigned int opt,
11051 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011052{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011053 struct http_txn *txn;
11054 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011055 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011056 const struct http_msg *msg;
11057 const char *hdr_name;
11058 int hdr_name_len;
11059 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011060 int occ = 0;
11061 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011062
Willy Tarreau24e32d82012-04-23 23:55:44 +020011063 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011064 return 0;
11065
Willy Tarreaua890d072013-04-02 12:01:06 +020011066 if (!ctx) {
11067 /* first call */
11068 ctx = &static_hdr_ctx;
11069 ctx->idx = 0;
11070 smp->ctx.a[2] = ctx;
11071 }
11072
Willy Tarreaue333ec92012-04-16 16:26:40 +020011073 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011074
Willy Tarreau15e91e12015-04-04 00:52:09 +020011075 txn = strm->txn;
11076 idx = &strm->txn->hdr_idx;
11077
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011078 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011079 msg = &txn->req;
11080 hdr_name = "Cookie";
11081 hdr_name_len = 6;
11082 } else {
11083 msg = &txn->rsp;
11084 hdr_name = "Set-Cookie";
11085 hdr_name_len = 10;
11086 }
11087
Willy Tarreau28376d62012-04-26 21:26:10 +020011088 if (!occ && !(opt & SMP_OPT_ITERATE))
11089 /* no explicit occurrence and single fetch => last cookie by default */
11090 occ = -1;
11091
11092 /* OK so basically here, either we want only one value and it's the
11093 * last one, or we want to iterate over all of them and we fetch the
11094 * next one.
11095 */
11096
Willy Tarreau9b28e032012-10-12 23:49:43 +020011097 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011098 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011099 /* search for the header from the beginning, we must first initialize
11100 * the search parameters.
11101 */
Willy Tarreau37406352012-04-23 16:16:37 +020011102 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011103 ctx->idx = 0;
11104 }
11105
Willy Tarreau28376d62012-04-26 21:26:10 +020011106 smp->flags |= SMP_F_VOL_HDR;
11107
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011108 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011109 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11110 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011111 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11112 goto out;
11113
Willy Tarreau24e32d82012-04-23 23:55:44 +020011114 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011115 continue;
11116
Willy Tarreau37406352012-04-23 16:16:37 +020011117 smp->ctx.a[0] = ctx->line + ctx->val;
11118 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011119 }
11120
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011121 smp->type = SMP_T_STR;
11122 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011123 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011124 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011125 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011126 &smp->data.str.str,
11127 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011128 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011129 found = 1;
11130 if (occ >= 0) {
11131 /* one value was returned into smp->data.str.{str,len} */
11132 smp->flags |= SMP_F_NOT_LAST;
11133 return 1;
11134 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011135 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011136 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011137 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011138 /* all cookie headers and values were scanned. If we're looking for the
11139 * last occurrence, we may return it now.
11140 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011141 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011142 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011143 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011144}
11145
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011146/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011147 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011148 * multiple cookies may be parsed on the same line. The returned sample is of
11149 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011150 */
11151static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020011152smp_fetch_cookie_cnt(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011153 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011154{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011155 struct http_txn *txn;
11156 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011157 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011158 const struct http_msg *msg;
11159 const char *hdr_name;
11160 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011161 int cnt;
11162 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011163 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011164
Willy Tarreau24e32d82012-04-23 23:55:44 +020011165 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011166 return 0;
11167
Willy Tarreaue333ec92012-04-16 16:26:40 +020011168 CHECK_HTTP_MESSAGE_FIRST();
11169
Willy Tarreau15e91e12015-04-04 00:52:09 +020011170 txn = strm->txn;
11171 idx = &strm->txn->hdr_idx;
11172
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011173 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011174 msg = &txn->req;
11175 hdr_name = "Cookie";
11176 hdr_name_len = 6;
11177 } else {
11178 msg = &txn->rsp;
11179 hdr_name = "Set-Cookie";
11180 hdr_name_len = 10;
11181 }
11182
Willy Tarreau9b28e032012-10-12 23:49:43 +020011183 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011184 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011185 ctx.idx = 0;
11186 cnt = 0;
11187
11188 while (1) {
11189 /* Note: val_beg == NULL every time we need to fetch a new header */
11190 if (!val_beg) {
11191 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11192 break;
11193
Willy Tarreau24e32d82012-04-23 23:55:44 +020011194 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011195 continue;
11196
11197 val_beg = ctx.line + ctx.val;
11198 val_end = val_beg + ctx.vlen;
11199 }
11200
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011201 smp->type = SMP_T_STR;
11202 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011203 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011204 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011205 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011206 &smp->data.str.str,
11207 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011208 cnt++;
11209 }
11210 }
11211
Willy Tarreaub169eba2013-12-16 15:14:43 +010011212 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011213 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011214 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011215 return 1;
11216}
11217
Willy Tarreau51539362012-05-08 12:46:28 +020011218/* Fetch an cookie's integer value. The integer value is returned. It
11219 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11220 */
11221static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020011222smp_fetch_cookie_val(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011223 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011224{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011225 int ret = smp_fetch_cookie(px, strm, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011226
11227 if (ret > 0) {
11228 smp->type = SMP_T_UINT;
11229 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11230 }
11231
11232 return ret;
11233}
11234
Willy Tarreau8797c062007-05-07 00:55:35 +020011235/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011236/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011237/************************************************************************/
11238
David Cournapeau16023ee2010-12-23 20:55:41 +090011239/*
11240 * Given a path string and its length, find the position of beginning of the
11241 * query string. Returns NULL if no query string is found in the path.
11242 *
11243 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11244 *
11245 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11246 */
bedis4c75cca2012-10-05 08:38:24 +020011247static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011248{
11249 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011250
bedis4c75cca2012-10-05 08:38:24 +020011251 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011252 return p ? p + 1 : NULL;
11253}
11254
bedis4c75cca2012-10-05 08:38:24 +020011255static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011256{
bedis4c75cca2012-10-05 08:38:24 +020011257 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011258}
11259
11260/*
11261 * Given a url parameter, find the starting position of the first occurence,
11262 * or NULL if the parameter is not found.
11263 *
11264 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11265 * the function will return query_string+8.
11266 */
11267static char*
11268find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011269 char* url_param_name, size_t url_param_name_l,
11270 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011271{
11272 char *pos, *last;
11273
11274 pos = query_string;
11275 last = query_string + query_string_l - url_param_name_l - 1;
11276
11277 while (pos <= last) {
11278 if (pos[url_param_name_l] == '=') {
11279 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11280 return pos;
11281 pos += url_param_name_l + 1;
11282 }
bedis4c75cca2012-10-05 08:38:24 +020011283 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011284 pos++;
11285 pos++;
11286 }
11287 return NULL;
11288}
11289
11290/*
11291 * Given a url parameter name, returns its value and size into *value and
11292 * *value_l respectively, and returns non-zero. If the parameter is not found,
11293 * zero is returned and value/value_l are not touched.
11294 */
11295static int
11296find_url_param_value(char* path, size_t path_l,
11297 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011298 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011299{
11300 char *query_string, *qs_end;
11301 char *arg_start;
11302 char *value_start, *value_end;
11303
bedis4c75cca2012-10-05 08:38:24 +020011304 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011305 if (!query_string)
11306 return 0;
11307
11308 qs_end = path + path_l;
11309 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011310 url_param_name, url_param_name_l,
11311 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011312 if (!arg_start)
11313 return 0;
11314
11315 value_start = arg_start + url_param_name_l + 1;
11316 value_end = value_start;
11317
bedis4c75cca2012-10-05 08:38:24 +020011318 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011319 value_end++;
11320
11321 *value = value_start;
11322 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011323 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011324}
11325
11326static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020011327smp_fetch_url_param(struct proxy *px, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011328 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011329{
bedis4c75cca2012-10-05 08:38:24 +020011330 char delim = '?';
Willy Tarreau15e91e12015-04-04 00:52:09 +020011331 struct http_msg *msg;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011332
bedis4c75cca2012-10-05 08:38:24 +020011333 if (!args || args[0].type != ARGT_STR ||
11334 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011335 return 0;
11336
11337 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011338
Willy Tarreau15e91e12015-04-04 00:52:09 +020011339 msg = &strm->txn->req;
11340
bedis4c75cca2012-10-05 08:38:24 +020011341 if (args[1].type)
11342 delim = *args[1].data.str.str;
11343
Willy Tarreau9b28e032012-10-12 23:49:43 +020011344 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011345 args->data.str.str, args->data.str.len,
11346 &smp->data.str.str, &smp->data.str.len,
11347 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011348 return 0;
11349
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011350 smp->type = SMP_T_STR;
11351 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011352 return 1;
11353}
11354
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011355/* Return the signed integer value for the specified url parameter (see url_param
11356 * above).
11357 */
11358static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020011359smp_fetch_url_param_val(struct proxy *px, struct stream *strm, unsigned int opt,
11360 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011361{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011362 int ret = smp_fetch_url_param(px, strm, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011363
11364 if (ret > 0) {
11365 smp->type = SMP_T_UINT;
11366 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11367 }
11368
11369 return ret;
11370}
11371
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011372/* This produces a 32-bit hash of the concatenation of the first occurrence of
11373 * the Host header followed by the path component if it begins with a slash ('/').
11374 * This means that '*' will not be added, resulting in exactly the first Host
11375 * entry. If no Host header is found, then the path is used. The resulting value
11376 * is hashed using the url hash followed by a full avalanche hash and provides a
11377 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11378 * high-traffic sites without having to store whole paths.
11379 * this differs from the base32 functions in that it includes the url parameters
11380 * as well as the path
11381 */
11382static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020011383smp_fetch_url32(struct proxy *px, struct stream *strm, unsigned int opt,
11384 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011385{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011386 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011387 struct hdr_ctx ctx;
11388 unsigned int hash = 0;
11389 char *ptr, *beg, *end;
11390 int len;
11391
11392 CHECK_HTTP_MESSAGE_FIRST();
11393
Willy Tarreau15e91e12015-04-04 00:52:09 +020011394 txn = strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011395 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011396 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011397 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11398 ptr = ctx.line + ctx.val;
11399 len = ctx.vlen;
11400 while (len--)
11401 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11402 }
11403
11404 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011405 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 +000011406 beg = http_get_path(txn);
11407 if (!beg)
11408 beg = end;
11409
11410 for (ptr = beg; ptr < end ; ptr++);
11411
11412 if (beg < ptr && *beg == '/') {
11413 while (beg < ptr)
11414 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11415 }
11416 hash = full_hash(hash);
11417
11418 smp->type = SMP_T_UINT;
11419 smp->data.uint = hash;
11420 smp->flags = SMP_F_VOL_1ST;
11421 return 1;
11422}
11423
11424/* This concatenates the source address with the 32-bit hash of the Host and
11425 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11426 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11427 * on the source address length. The URL hash is stored before the address so
11428 * that in environments where IPv6 is insignificant, truncating the output to
11429 * 8 bytes would still work.
11430 */
11431static int
Willy Tarreau15e91e12015-04-04 00:52:09 +020011432smp_fetch_url32_src(struct proxy *px, struct stream *strm, unsigned int opt,
11433 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011434{
11435 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011436 struct session *sess = strm_sess(strm);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020011437 struct connection *cli_conn = objt_conn(sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011438
Willy Tarreau15e91e12015-04-04 00:52:09 +020011439 if (!smp_fetch_url32(px, strm, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011440 return 0;
11441
11442 temp = get_trash_chunk();
11443 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11444 temp->len += sizeof(smp->data.uint);
11445
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011446 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011447 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011448 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011449 temp->len += 4;
11450 break;
11451 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011452 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011453 temp->len += 16;
11454 break;
11455 default:
11456 return 0;
11457 }
11458
11459 smp->data.str = *temp;
11460 smp->type = SMP_T_BIN;
11461 return 1;
11462}
11463
Willy Tarreau185b5c42012-04-26 15:11:51 +020011464/* This function is used to validate the arguments passed to any "hdr" fetch
11465 * keyword. These keywords support an optional positive or negative occurrence
11466 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11467 * is assumed that the types are already the correct ones. Returns 0 on error,
11468 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11469 * error message in case of error, that the caller is responsible for freeing.
11470 * The initial location must either be freeable or NULL.
11471 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011472int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011473{
11474 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011475 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011476 return 0;
11477 }
11478 return 1;
11479}
11480
Willy Tarreau276fae92013-07-25 14:36:01 +020011481/* takes an UINT value on input supposed to represent the time since EPOCH,
11482 * adds an optional offset found in args[0] and emits a string representing
11483 * the date in RFC-1123/5322 format.
11484 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011485static int sample_conv_http_date(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011486 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011487{
11488 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11489 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11490 struct chunk *temp;
11491 struct tm *tm;
11492 time_t curr_date = smp->data.uint;
11493
11494 /* add offset */
11495 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11496 curr_date += args[0].data.sint;
11497
11498 tm = gmtime(&curr_date);
11499
11500 temp = get_trash_chunk();
11501 temp->len = snprintf(temp->str, temp->size - temp->len,
11502 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11503 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11504 tm->tm_hour, tm->tm_min, tm->tm_sec);
11505
11506 smp->data.str = *temp;
11507 smp->type = SMP_T_STR;
11508 return 1;
11509}
11510
Thierry FOURNIERad903512014-04-11 17:51:01 +020011511/* Match language range with language tag. RFC2616 14.4:
11512 *
11513 * A language-range matches a language-tag if it exactly equals
11514 * the tag, or if it exactly equals a prefix of the tag such
11515 * that the first tag character following the prefix is "-".
11516 *
11517 * Return 1 if the strings match, else return 0.
11518 */
11519static inline int language_range_match(const char *range, int range_len,
11520 const char *tag, int tag_len)
11521{
11522 const char *end = range + range_len;
11523 const char *tend = tag + tag_len;
11524 while (range < end) {
11525 if (*range == '-' && tag == tend)
11526 return 1;
11527 if (*range != *tag || tag == tend)
11528 return 0;
11529 range++;
11530 tag++;
11531 }
11532 /* Return true only if the last char of the tag is matched. */
11533 return tag == tend;
11534}
11535
11536/* Arguments: The list of expected value, the number of parts returned and the separator */
Willy Tarreau87b09662015-04-03 00:22:06 +020011537static int sample_conv_q_prefered(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011538 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011539{
11540 const char *al = smp->data.str.str;
11541 const char *end = al + smp->data.str.len;
11542 const char *token;
11543 int toklen;
11544 int qvalue;
11545 const char *str;
11546 const char *w;
11547 int best_q = 0;
11548
11549 /* Set the constant to the sample, because the output of the
11550 * function will be peek in the constant configuration string.
11551 */
11552 smp->flags |= SMP_F_CONST;
11553 smp->data.str.size = 0;
11554 smp->data.str.str = "";
11555 smp->data.str.len = 0;
11556
11557 /* Parse the accept language */
11558 while (1) {
11559
11560 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011561 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011562 al++;
11563 if (al >= end)
11564 break;
11565
11566 /* Start of the fisrt word. */
11567 token = al;
11568
11569 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011570 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011571 al++;
11572 if (al == token)
11573 goto expect_comma;
11574
11575 /* Length of the token. */
11576 toklen = al - token;
11577 qvalue = 1000;
11578
11579 /* Check if the token exists in the list. If the token not exists,
11580 * jump to the next token.
11581 */
11582 str = args[0].data.str.str;
11583 w = str;
11584 while (1) {
11585 if (*str == ';' || *str == '\0') {
11586 if (language_range_match(token, toklen, w, str-w))
11587 goto look_for_q;
11588 if (*str == '\0')
11589 goto expect_comma;
11590 w = str + 1;
11591 }
11592 str++;
11593 }
11594 goto expect_comma;
11595
11596look_for_q:
11597
11598 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011599 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011600 al++;
11601 if (al >= end)
11602 goto process_value;
11603
11604 /* If ',' is found, process the result */
11605 if (*al == ',')
11606 goto process_value;
11607
11608 /* If the character is different from ';', look
11609 * for the end of the header part in best effort.
11610 */
11611 if (*al != ';')
11612 goto expect_comma;
11613
11614 /* Assumes that the char is ';', now expect "q=". */
11615 al++;
11616
11617 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011618 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011619 al++;
11620 if (al >= end)
11621 goto process_value;
11622
11623 /* Expect 'q'. If no 'q', continue in best effort */
11624 if (*al != 'q')
11625 goto process_value;
11626 al++;
11627
11628 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011629 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011630 al++;
11631 if (al >= end)
11632 goto process_value;
11633
11634 /* Expect '='. If no '=', continue in best effort */
11635 if (*al != '=')
11636 goto process_value;
11637 al++;
11638
11639 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011640 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011641 al++;
11642 if (al >= end)
11643 goto process_value;
11644
11645 /* Parse the q value. */
11646 qvalue = parse_qvalue(al, &al);
11647
11648process_value:
11649
11650 /* If the new q value is the best q value, then store the associated
11651 * language in the response. If qvalue is the biggest value (1000),
11652 * break the process.
11653 */
11654 if (qvalue > best_q) {
11655 smp->data.str.str = (char *)w;
11656 smp->data.str.len = str - w;
11657 if (qvalue >= 1000)
11658 break;
11659 best_q = qvalue;
11660 }
11661
11662expect_comma:
11663
11664 /* Expect comma or end. If the end is detected, quit the loop. */
11665 while (al < end && *al != ',')
11666 al++;
11667 if (al >= end)
11668 break;
11669
11670 /* Comma is found, jump it and restart the analyzer. */
11671 al++;
11672 }
11673
11674 /* Set default value if required. */
11675 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11676 smp->data.str.str = args[1].data.str.str;
11677 smp->data.str.len = args[1].data.str.len;
11678 }
11679
11680 /* Return true only if a matching language was found. */
11681 return smp->data.str.len != 0;
11682}
11683
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011684/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011685 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011686 * the relevant part of the request line accordingly. Then it updates various
11687 * pointers to the next elements which were moved, and the total buffer length.
11688 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011689 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11690 * error, though this can be revisited when this code is finally exploited.
11691 *
11692 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11693 * query string and 3 to replace uri.
11694 *
11695 * In query string case, the mark question '?' must be set at the start of the
11696 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011697 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011698int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau87b09662015-04-03 00:22:06 +020011699 struct proxy *px, struct stream *s, struct http_txn *txn)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011700{
11701 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011702 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011703 int delta;
11704
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011705 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011706 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011707 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011708 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11709
11710 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011711 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011712 txn->req.sl.rq.m_l += delta;
11713 txn->req.sl.rq.u += delta;
11714 txn->req.sl.rq.v += delta;
11715 break;
11716
11717 case 1: // path
11718 cur_ptr = http_get_path(txn);
11719 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011720 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011721
11722 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011723 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 +010011724 cur_end++;
11725
11726 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011727 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011728 txn->req.sl.rq.u_l += delta;
11729 txn->req.sl.rq.v += delta;
11730 break;
11731
11732 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011733 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011734 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011735 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11736 while (cur_ptr < cur_end && *cur_ptr != '?')
11737 cur_ptr++;
11738
11739 /* skip the question mark or indicate that we must insert it
11740 * (but only if the format string is not empty then).
11741 */
11742 if (cur_ptr < cur_end)
11743 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011744 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011745 offset = 0;
11746
11747 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011748 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011749 txn->req.sl.rq.u_l += delta;
11750 txn->req.sl.rq.v += delta;
11751 break;
11752
11753 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011754 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011755 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11756
11757 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011758 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011759 txn->req.sl.rq.u_l += delta;
11760 txn->req.sl.rq.v += delta;
11761 break;
11762
11763 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011764 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011765 }
11766
11767 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011768 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011769 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011770 return 0;
11771}
11772
11773/* This function executes one of the set-{method,path,query,uri} actions. It
11774 * builds a string in the trash from the specified format string. It finds
11775 * the action to be performed in p[2], previously filled by function
11776 * parse_set_req_line(). The replacement action is excuted by the function
11777 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
11778 * the action is canceled, but the rule processing continue.
11779 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011780int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s, struct http_txn *txn)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011781{
11782 chunk_reset(&trash);
11783
11784 /* If we have to create a query string, prepare a '?'. */
11785 if (*(int *)&rule->arg.act.p[2] == 2)
11786 trash.str[trash.len++] = '?';
11787 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11788
11789 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s, txn);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011790 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011791}
11792
11793/* parse an http-request action among :
11794 * set-method
11795 * set-path
11796 * set-query
11797 * set-uri
11798 *
11799 * All of them accept a single argument of type string representing a log-format.
11800 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11801 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11802 * It returns 0 on success, < 0 on error.
11803 */
11804int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11805{
11806 int cur_arg = *orig_arg;
11807
11808 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11809
11810 switch (args[0][4]) {
11811 case 'm' :
11812 *(int *)&rule->arg.act.p[2] = 0;
11813 rule->action_ptr = http_action_set_req_line;
11814 break;
11815 case 'p' :
11816 *(int *)&rule->arg.act.p[2] = 1;
11817 rule->action_ptr = http_action_set_req_line;
11818 break;
11819 case 'q' :
11820 *(int *)&rule->arg.act.p[2] = 2;
11821 rule->action_ptr = http_action_set_req_line;
11822 break;
11823 case 'u' :
11824 *(int *)&rule->arg.act.p[2] = 3;
11825 rule->action_ptr = http_action_set_req_line;
11826 break;
11827 default:
11828 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11829 return -1;
11830 }
11831
11832 if (!*args[cur_arg] ||
11833 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11834 memprintf(err, "expects exactly 1 argument <format>");
11835 return -1;
11836 }
11837
11838 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11839 proxy->conf.args.ctx = ARGC_HRQ;
11840 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11841 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11842 proxy->conf.args.file, proxy->conf.args.line);
11843
11844 (*orig_arg)++;
11845 return 0;
11846}
11847
William Lallemand73025dd2014-04-24 14:38:37 +020011848/*
11849 * Return the struct http_req_action_kw associated to a keyword.
11850 */
11851struct http_req_action_kw *action_http_req_custom(const char *kw)
11852{
11853 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11854 struct http_req_action_kw_list *kw_list;
11855 int i;
11856
11857 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11858 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11859 if (!strcmp(kw, kw_list->kw[i].kw))
11860 return &kw_list->kw[i];
11861 }
11862 }
11863 }
11864 return NULL;
11865}
11866
11867/*
11868 * Return the struct http_res_action_kw associated to a keyword.
11869 */
11870struct http_res_action_kw *action_http_res_custom(const char *kw)
11871{
11872 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11873 struct http_res_action_kw_list *kw_list;
11874 int i;
11875
11876 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11877 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11878 if (!strcmp(kw, kw_list->kw[i].kw))
11879 return &kw_list->kw[i];
11880 }
11881 }
11882 }
11883 return NULL;
11884}
11885
Willy Tarreau4a568972010-05-12 08:08:50 +020011886/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011887/* All supported ACL keywords must be declared here. */
11888/************************************************************************/
11889
11890/* Note: must not be declared <const> as its list will be overwritten.
11891 * Please take care of keeping this list alphabetically sorted.
11892 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011893static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011894 { "base", "base", PAT_MATCH_STR },
11895 { "base_beg", "base", PAT_MATCH_BEG },
11896 { "base_dir", "base", PAT_MATCH_DIR },
11897 { "base_dom", "base", PAT_MATCH_DOM },
11898 { "base_end", "base", PAT_MATCH_END },
11899 { "base_len", "base", PAT_MATCH_LEN },
11900 { "base_reg", "base", PAT_MATCH_REG },
11901 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011902
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011903 { "cook", "req.cook", PAT_MATCH_STR },
11904 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11905 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11906 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11907 { "cook_end", "req.cook", PAT_MATCH_END },
11908 { "cook_len", "req.cook", PAT_MATCH_LEN },
11909 { "cook_reg", "req.cook", PAT_MATCH_REG },
11910 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011911
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011912 { "hdr", "req.hdr", PAT_MATCH_STR },
11913 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11914 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11915 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11916 { "hdr_end", "req.hdr", PAT_MATCH_END },
11917 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11918 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11919 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011920
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011921 /* these two declarations uses strings with list storage (in place
11922 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11923 * and delete functions are relative to the list management. The parse
11924 * and match method are related to the corresponding fetch methods. This
11925 * is very particular ACL declaration mode.
11926 */
11927 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11928 { "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 +020011929
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011930 { "path", "path", PAT_MATCH_STR },
11931 { "path_beg", "path", PAT_MATCH_BEG },
11932 { "path_dir", "path", PAT_MATCH_DIR },
11933 { "path_dom", "path", PAT_MATCH_DOM },
11934 { "path_end", "path", PAT_MATCH_END },
11935 { "path_len", "path", PAT_MATCH_LEN },
11936 { "path_reg", "path", PAT_MATCH_REG },
11937 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011938
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011939 { "req_ver", "req.ver", PAT_MATCH_STR },
11940 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011941
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011942 { "scook", "res.cook", PAT_MATCH_STR },
11943 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11944 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11945 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11946 { "scook_end", "res.cook", PAT_MATCH_END },
11947 { "scook_len", "res.cook", PAT_MATCH_LEN },
11948 { "scook_reg", "res.cook", PAT_MATCH_REG },
11949 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011950
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011951 { "shdr", "res.hdr", PAT_MATCH_STR },
11952 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11953 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11954 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11955 { "shdr_end", "res.hdr", PAT_MATCH_END },
11956 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11957 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11958 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011959
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011960 { "url", "url", PAT_MATCH_STR },
11961 { "url_beg", "url", PAT_MATCH_BEG },
11962 { "url_dir", "url", PAT_MATCH_DIR },
11963 { "url_dom", "url", PAT_MATCH_DOM },
11964 { "url_end", "url", PAT_MATCH_END },
11965 { "url_len", "url", PAT_MATCH_LEN },
11966 { "url_reg", "url", PAT_MATCH_REG },
11967 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011968
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011969 { "urlp", "urlp", PAT_MATCH_STR },
11970 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11971 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11972 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11973 { "urlp_end", "urlp", PAT_MATCH_END },
11974 { "urlp_len", "urlp", PAT_MATCH_LEN },
11975 { "urlp_reg", "urlp", PAT_MATCH_REG },
11976 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011977
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011978 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011979}};
11980
11981/************************************************************************/
11982/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011983/************************************************************************/
11984/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011985static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011986 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011987 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11988 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11989
Willy Tarreau87b09662015-04-03 00:22:06 +020011990 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011991 { "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 +020011992
11993 /* retrieve these captures from the HTTP logs */
11994 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11995 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11996 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11997
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011998 { "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 +020011999 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012000
Willy Tarreau409bcde2013-01-08 00:31:00 +010012001 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12002 * are only here to match the ACL's name, are request-only and are used
12003 * for ACL compatibility only.
12004 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012005 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12006 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012007 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12008 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12009
12010 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12011 * only here to match the ACL's name, are request-only and are used for
12012 * ACL compatibility only.
12013 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012014 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012015 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12016 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
12017 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12018
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012019 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012020 { "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 +010012021 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012022 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012023 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012024 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012025
12026 /* HTTP protocol on the request path */
12027 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012028 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012029
12030 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012031 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12032 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012033
12034 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012035 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12036 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012037
Willy Tarreau18ed2562013-01-14 15:56:36 +010012038 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012039 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012040 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12041 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12042
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012043 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012044 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012045 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012046 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12047 { "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 +010012048 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012049 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12050
12051 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012052 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012053 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12054 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12055
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012056 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012057 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012058 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012059 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12060 { "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 +010012061 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012062 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12063
Willy Tarreau409bcde2013-01-08 00:31:00 +010012064 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012065 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012066 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12067 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012068 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012069
12070 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012071 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012072 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12073 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12074 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12075
12076 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012077 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012078 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12079 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012080 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12081 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012082 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12083 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012084 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12085 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012086}};
12087
Willy Tarreau8797c062007-05-07 00:55:35 +020012088
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012089/************************************************************************/
12090/* All supported converter keywords must be declared here. */
12091/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012092/* Note: must not be declared <const> as its list will be overwritten */
12093static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012094 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12095 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012096 { NULL, NULL, 0, 0, 0 },
12097}};
12098
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012099/************************************************************************/
12100/* All supported http-request action keywords must be declared here. */
12101/************************************************************************/
12102struct http_req_action_kw_list http_req_actions = {
12103 .scope = "http",
12104 .kw = {
12105 { "set-method", parse_set_req_line },
12106 { "set-path", parse_set_req_line },
12107 { "set-query", parse_set_req_line },
12108 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012109 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012110 }
12111};
12112
Willy Tarreau8797c062007-05-07 00:55:35 +020012113__attribute__((constructor))
12114static void __http_protocol_init(void)
12115{
12116 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012117 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012118 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012119 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012120}
12121
12122
Willy Tarreau58f10d72006-12-04 02:26:12 +010012123/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012124 * Local variables:
12125 * c-indent-level: 8
12126 * c-basic-offset: 8
12127 * End:
12128 */