blob: 338ae9e73261de7ddb3af7364dcab24b4c4d8aae [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 Tarreaud0d8da92015-04-04 02:10:38 +0200854 else if (strm_fe(s)->errmsg[msgnum].str)
855 return &strm_fe(s)->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 Tarreaud0d8da92015-04-04 02:10:38 +02002267 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +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 Tarreaud0d8da92015-04-04 02:10:38 +02002325 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->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 Tarreaud0d8da92015-04-04 02:10:38 +02002336 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_fe(s)->comp && (comp_algo_back = strm_fe(s)->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +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 Tarreaud0d8da92015-04-04 02:10:38 +02002403 (strm_fe(s)->comp && (comp_type = strm_fe(s)->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 Tarreaud0d8da92015-04-04 02:10:38 +02002416 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->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 Tarreaud0d8da92015-04-04 02:10:38 +02002467 struct proxy *fe = strm_fe(s);
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 Tarreau192252e2015-04-04 01:47:55 +02002879 int ret = acl_exec_cond(cond, sess->fe, sess, 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 Tarreau192252e2015-04-04 01:47:55 +02003106 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003107 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003108 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003109 struct uri_auth *uri_auth = s->be->uri_auth;
3110 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003111 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003112
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003113 appctx = si_appctx(si);
3114 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3115 appctx->st1 = appctx->st2 = 0;
3116 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3117 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003118 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003119 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003120
3121 uri = msg->chn->buf->p + msg->sl.rq.u;
3122 lookup = uri + uri_auth->uri_len;
3123
3124 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3125 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003126 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003127 break;
3128 }
3129 }
3130
3131 if (uri_auth->refresh) {
3132 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3133 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003134 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003135 break;
3136 }
3137 }
3138 }
3139
3140 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3141 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003142 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003143 break;
3144 }
3145 }
3146
3147 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3148 if (memcmp(h, ";st=", 4) == 0) {
3149 int i;
3150 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003151 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003152 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3153 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003154 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003155 break;
3156 }
3157 }
3158 break;
3159 }
3160 }
3161
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003162 appctx->ctx.stats.scope_str = 0;
3163 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003164 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3165 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3166 int itx = 0;
3167 const char *h2;
3168 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3169 const char *err;
3170
3171 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3172 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003173 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003174 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3175 itx++;
3176 h++;
3177 }
3178
3179 if (itx > STAT_SCOPE_TXT_MAXLEN)
3180 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003181 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003182
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003183 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003184 memcpy(scope_txt, h2, itx);
3185 scope_txt[itx] = '\0';
3186 err = invalid_char(scope_txt);
3187 if (err) {
3188 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003189 appctx->ctx.stats.scope_str = 0;
3190 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003191 }
3192 break;
3193 }
3194 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003195
3196 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003197 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003198 int ret = 1;
3199
3200 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003201 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003202 ret = acl_pass(ret);
3203 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3204 ret = !ret;
3205 }
3206
3207 if (ret) {
3208 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003209 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003210 break;
3211 }
3212 }
3213
3214 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003215 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003216 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003217 /* we'll need the request body, possibly after sending 100-continue */
3218 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003219 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003220 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003221 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003222 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3223 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003224 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003225 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003226 else {
3227 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003228 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003229 }
3230
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003231 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003232 return 1;
3233}
3234
Lukas Tribus67db8df2013-06-23 17:37:13 +02003235/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3236 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3237 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003238void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003239{
3240#ifdef IP_TOS
3241 if (from.ss_family == AF_INET)
3242 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3243#endif
3244#ifdef IPV6_TCLASS
3245 if (from.ss_family == AF_INET6) {
3246 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3247 /* v4-mapped addresses need IP_TOS */
3248 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3249 else
3250 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3251 }
3252#endif
3253}
3254
Willy Tarreau87b09662015-04-03 00:22:06 +02003255int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003256 const char* name, unsigned int name_len,
3257 const char *str, struct my_regex *re,
3258 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003259{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003260 struct hdr_ctx ctx;
3261 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003262 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003263 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3264 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003265 struct chunk *output = get_trash_chunk();
3266
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003267 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003268
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003269 /* Choose the header browsing function. */
3270 switch (action) {
3271 case HTTP_REQ_ACT_REPLACE_VAL:
3272 case HTTP_RES_ACT_REPLACE_VAL:
3273 http_find_hdr_func = http_find_header2;
3274 break;
3275 case HTTP_REQ_ACT_REPLACE_HDR:
3276 case HTTP_RES_ACT_REPLACE_HDR:
3277 http_find_hdr_func = http_find_full_header2;
3278 break;
3279 default: /* impossible */
3280 return -1;
3281 }
3282
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003283 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3284 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003285 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003286 char *val = ctx.line + ctx.val;
3287 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003288
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003289 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3290 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003291
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003292 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003293 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003294 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003295
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003296 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003297
3298 hdr->len += delta;
3299 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003300
3301 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003302 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003303 }
3304
3305 return 0;
3306}
3307
Willy Tarreau87b09662015-04-03 00:22:06 +02003308static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003309 const char* name, unsigned int name_len,
3310 struct list *fmt, struct my_regex *re,
3311 int action)
3312{
3313 struct chunk *replace = get_trash_chunk();
3314
3315 replace->len = build_logline(s, replace->str, replace->size, fmt);
3316 if (replace->len >= replace->size - 1)
3317 return -1;
3318
3319 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3320}
3321
Willy Tarreau87b09662015-04-03 00:22:06 +02003322/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003323 * transaction <txn>. Returns the verdict of the first rule that prevents
3324 * further processing of the request (auth, deny, ...), and defaults to
3325 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3326 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3327 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003328 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003329enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003330http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003331{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003332 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003333 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003334 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003335 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003336 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003337 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003338
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003339 /* If "the current_rule_list" match the executed rule list, we are in
3340 * resume condition. If a resume is needed it is always in the action
3341 * and never in the ACL or converters. In this case, we initialise the
3342 * current rule, and go to the action execution point.
3343 */
3344 if (s->current_rule_list == rules) {
3345 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3346 goto resume_execution;
3347 }
3348 s->current_rule_list = rules;
Willy Tarreauff011f22011-01-06 17:51:27 +01003349 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003350 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003351 continue;
3352
Willy Tarreau96257ec2012-12-27 10:46:37 +01003353 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003354 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003355 int ret;
3356
Willy Tarreau192252e2015-04-04 01:47:55 +02003357 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003358 ret = acl_pass(ret);
3359
Willy Tarreauff011f22011-01-06 17:51:27 +01003360 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003361 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003362
3363 if (!ret) /* condition not matched */
3364 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003365 }
3366
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003367resume_execution:
Willy Tarreau20b0de52012-12-24 15:45:22 +01003368
Willy Tarreau96257ec2012-12-27 10:46:37 +01003369 switch (rule->action) {
3370 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003371 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003372
3373 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003374 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003375
Willy Tarreauccbcc372012-12-27 12:37:57 +01003376 case HTTP_REQ_ACT_TARPIT:
3377 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003378 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003379
Willy Tarreau96257ec2012-12-27 10:46:37 +01003380 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003381 /* Auth might be performed on regular http-req rules as well as on stats */
3382 auth_realm = rule->arg.auth.realm;
3383 if (!auth_realm) {
3384 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3385 auth_realm = STATS_DEFAULT_REALM;
3386 else
3387 auth_realm = px->id;
3388 }
3389 /* send 401/407 depending on whether we use a proxy or not. We still
3390 * count one error, because normal browsing won't significantly
3391 * increase the counter but brute force attempts will.
3392 */
3393 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3394 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3395 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003396 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003397 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003398
Willy Tarreau81499eb2012-12-27 12:19:02 +01003399 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003400 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3401 return HTTP_RULE_RES_BADREQ;
3402 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003403
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003404 case HTTP_REQ_ACT_SET_NICE:
3405 s->task->nice = rule->arg.nice;
3406 break;
3407
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003408 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003409 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003410 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003411 break;
3412
Willy Tarreau51347ed2013-06-11 19:34:13 +02003413 case HTTP_REQ_ACT_SET_MARK:
3414#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003415 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003416 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003417#endif
3418 break;
3419
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003420 case HTTP_REQ_ACT_SET_LOGL:
3421 s->logs.level = rule->arg.loglevel;
3422 break;
3423
Sasha Pachev218f0642014-06-16 12:05:59 -06003424 case HTTP_REQ_ACT_REPLACE_HDR:
3425 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003426 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3427 rule->arg.hdr_add.name_len,
3428 &rule->arg.hdr_add.fmt,
3429 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003430 return HTTP_RULE_RES_BADREQ;
3431 break;
3432
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003433 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003434 ctx.idx = 0;
3435 /* remove all occurrences of the header */
3436 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3437 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3438 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003439 }
Willy Tarreau85603282015-01-21 20:39:27 +01003440 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003441
Willy Tarreau85603282015-01-21 20:39:27 +01003442 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003443 case HTTP_REQ_ACT_ADD_HDR:
3444 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3445 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3446 trash.len = rule->arg.hdr_add.name_len;
3447 trash.str[trash.len++] = ':';
3448 trash.str[trash.len++] = ' ';
3449 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 +01003450
3451 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3452 /* remove all occurrences of the header */
3453 ctx.idx = 0;
3454 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3455 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3456 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3457 }
3458 }
3459
Willy Tarreau96257ec2012-12-27 10:46:37 +01003460 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3461 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003462
3463 case HTTP_REQ_ACT_DEL_ACL:
3464 case HTTP_REQ_ACT_DEL_MAP: {
3465 struct pat_ref *ref;
3466 char *key;
3467 int len;
3468
3469 /* collect reference */
3470 ref = pat_ref_lookup(rule->arg.map.ref);
3471 if (!ref)
3472 continue;
3473
3474 /* collect key */
3475 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3476 key = trash.str;
3477 key[len] = '\0';
3478
3479 /* perform update */
3480 /* returned code: 1=ok, 0=ko */
3481 pat_ref_delete(ref, key);
3482
3483 break;
3484 }
3485
3486 case HTTP_REQ_ACT_ADD_ACL: {
3487 struct pat_ref *ref;
3488 char *key;
3489 struct chunk *trash_key;
3490 int len;
3491
3492 trash_key = get_trash_chunk();
3493
3494 /* collect reference */
3495 ref = pat_ref_lookup(rule->arg.map.ref);
3496 if (!ref)
3497 continue;
3498
3499 /* collect key */
3500 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3501 key = trash_key->str;
3502 key[len] = '\0';
3503
3504 /* perform update */
3505 /* add entry only if it does not already exist */
3506 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003507 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003508
3509 break;
3510 }
3511
3512 case HTTP_REQ_ACT_SET_MAP: {
3513 struct pat_ref *ref;
3514 char *key, *value;
3515 struct chunk *trash_key, *trash_value;
3516 int len;
3517
3518 trash_key = get_trash_chunk();
3519 trash_value = get_trash_chunk();
3520
3521 /* collect reference */
3522 ref = pat_ref_lookup(rule->arg.map.ref);
3523 if (!ref)
3524 continue;
3525
3526 /* collect key */
3527 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3528 key = trash_key->str;
3529 key[len] = '\0';
3530
3531 /* collect value */
3532 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3533 value = trash_value->str;
3534 value[len] = '\0';
3535
3536 /* perform update */
3537 if (pat_ref_find_elt(ref, key) != NULL)
3538 /* update entry if it exists */
3539 pat_ref_set(ref, key, value, NULL);
3540 else
3541 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003542 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003543
3544 break;
3545 }
William Lallemand73025dd2014-04-24 14:38:37 +02003546
3547 case HTTP_REQ_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003548 if (!rule->action_ptr(rule, px, s)) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003549 s->current_rule = &rule->list;
3550 return HTTP_RULE_RES_YIELD;
3551 }
William Lallemand73025dd2014-04-24 14:38:37 +02003552 break;
3553
3554 case HTTP_REQ_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003555 rule->action_ptr(rule, px, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003556 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003557
3558 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3559 /* Note: only the first valid tracking parameter of each
3560 * applies.
3561 */
3562
3563 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3564 struct stktable *t;
3565 struct stksess *ts;
3566 struct stktable_key *key;
3567 void *ptr;
3568
3569 t = rule->act_prm.trk_ctr.table.t;
Willy Tarreau192252e2015-04-04 01:47:55 +02003570 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02003571
3572 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003573 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003574
3575 /* let's count a new HTTP request as it's the first time we do it */
3576 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3577 if (ptr)
3578 stktable_data_cast(ptr, http_req_cnt)++;
3579
3580 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3581 if (ptr)
3582 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3583 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3584
3585 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003586 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003587 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3588 }
3589 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003590 }
3591 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003592
3593 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003594 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003595}
3596
Willy Tarreau71241ab2012-12-27 11:30:54 +01003597
Willy Tarreau87b09662015-04-03 00:22:06 +02003598/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003599 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3600 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3601 * evaluation of next rule list. If *STOP is returned, the process must stop
3602 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3603 * rule. If *YIELD is returned, the czller must call again the function with
3604 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003605 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003606static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003607http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003608{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003609 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003610 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003611 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003612 struct http_res_rule *rule;
3613 struct hdr_ctx ctx;
3614
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003615 /* If "the current_rule_list" match the executed rule list, we are in
3616 * resume condition. If a resume is needed it is always in the action
3617 * and never in the ACL or converters. In this case, we initialise the
3618 * current rule, and go to the action execution point.
3619 */
3620 if (s->current_rule_list == rules) {
3621 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3622 goto resume_execution;
3623 }
3624 s->current_rule_list = rules;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003625 list_for_each_entry(rule, rules, list) {
3626 if (rule->action >= HTTP_RES_ACT_MAX)
3627 continue;
3628
3629 /* check optional condition */
3630 if (rule->cond) {
3631 int ret;
3632
Willy Tarreau192252e2015-04-04 01:47:55 +02003633 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003634 ret = acl_pass(ret);
3635
3636 if (rule->cond->pol == ACL_COND_UNLESS)
3637 ret = !ret;
3638
3639 if (!ret) /* condition not matched */
3640 continue;
3641 }
3642
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003643resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003644
3645 switch (rule->action) {
3646 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003647 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003648
3649 case HTTP_RES_ACT_DENY:
3650 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003651 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003652
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003653 case HTTP_RES_ACT_SET_NICE:
3654 s->task->nice = rule->arg.nice;
3655 break;
3656
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003657 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003658 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003659 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003660 break;
3661
Willy Tarreau51347ed2013-06-11 19:34:13 +02003662 case HTTP_RES_ACT_SET_MARK:
3663#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003664 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003665 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003666#endif
3667 break;
3668
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003669 case HTTP_RES_ACT_SET_LOGL:
3670 s->logs.level = rule->arg.loglevel;
3671 break;
3672
Sasha Pachev218f0642014-06-16 12:05:59 -06003673 case HTTP_RES_ACT_REPLACE_HDR:
3674 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003675 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3676 rule->arg.hdr_add.name_len,
3677 &rule->arg.hdr_add.fmt,
3678 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003679 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003680 break;
3681
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003682 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003683 ctx.idx = 0;
3684 /* remove all occurrences of the header */
3685 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3686 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3687 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3688 }
Willy Tarreau85603282015-01-21 20:39:27 +01003689 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003690
Willy Tarreau85603282015-01-21 20:39:27 +01003691 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003692 case HTTP_RES_ACT_ADD_HDR:
3693 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3694 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3695 trash.len = rule->arg.hdr_add.name_len;
3696 trash.str[trash.len++] = ':';
3697 trash.str[trash.len++] = ' ';
3698 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 +01003699
3700 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3701 /* remove all occurrences of the header */
3702 ctx.idx = 0;
3703 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3704 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3705 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3706 }
3707 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003708 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3709 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003710
3711 case HTTP_RES_ACT_DEL_ACL:
3712 case HTTP_RES_ACT_DEL_MAP: {
3713 struct pat_ref *ref;
3714 char *key;
3715 int len;
3716
3717 /* collect reference */
3718 ref = pat_ref_lookup(rule->arg.map.ref);
3719 if (!ref)
3720 continue;
3721
3722 /* collect key */
3723 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3724 key = trash.str;
3725 key[len] = '\0';
3726
3727 /* perform update */
3728 /* returned code: 1=ok, 0=ko */
3729 pat_ref_delete(ref, key);
3730
3731 break;
3732 }
3733
3734 case HTTP_RES_ACT_ADD_ACL: {
3735 struct pat_ref *ref;
3736 char *key;
3737 struct chunk *trash_key;
3738 int len;
3739
3740 trash_key = get_trash_chunk();
3741
3742 /* collect reference */
3743 ref = pat_ref_lookup(rule->arg.map.ref);
3744 if (!ref)
3745 continue;
3746
3747 /* collect key */
3748 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3749 key = trash_key->str;
3750 key[len] = '\0';
3751
3752 /* perform update */
3753 /* check if the entry already exists */
3754 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003755 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003756
3757 break;
3758 }
3759
3760 case HTTP_RES_ACT_SET_MAP: {
3761 struct pat_ref *ref;
3762 char *key, *value;
3763 struct chunk *trash_key, *trash_value;
3764 int len;
3765
3766 trash_key = get_trash_chunk();
3767 trash_value = get_trash_chunk();
3768
3769 /* collect reference */
3770 ref = pat_ref_lookup(rule->arg.map.ref);
3771 if (!ref)
3772 continue;
3773
3774 /* collect key */
3775 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3776 key = trash_key->str;
3777 key[len] = '\0';
3778
3779 /* collect value */
3780 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3781 value = trash_value->str;
3782 value[len] = '\0';
3783
3784 /* perform update */
3785 if (pat_ref_find_elt(ref, key) != NULL)
3786 /* update entry if it exists */
3787 pat_ref_set(ref, key, value, NULL);
3788 else
3789 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003790 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003791
3792 break;
3793 }
William Lallemand73025dd2014-04-24 14:38:37 +02003794
3795 case HTTP_RES_ACT_CUSTOM_CONT:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003796 if (!rule->action_ptr(rule, px, s)) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003797 s->current_rule = &rule->list;
3798 return HTTP_RULE_RES_YIELD;
3799 }
William Lallemand73025dd2014-04-24 14:38:37 +02003800 break;
3801
3802 case HTTP_RES_ACT_CUSTOM_STOP:
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003803 rule->action_ptr(rule, px, s);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003804 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003805 }
3806 }
3807
3808 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003809 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003810}
3811
3812
Willy Tarreau71241ab2012-12-27 11:30:54 +01003813/* Perform an HTTP redirect based on the information in <rule>. The function
3814 * returns non-zero on success, or zero in case of a, irrecoverable error such
3815 * as too large a request to build a valid response.
3816 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003817static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003818{
3819 struct http_msg *msg = &txn->req;
3820 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003821 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003822
3823 /* build redirect message */
3824 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003825 case 308:
3826 msg_fmt = HTTP_308;
3827 break;
3828 case 307:
3829 msg_fmt = HTTP_307;
3830 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003831 case 303:
3832 msg_fmt = HTTP_303;
3833 break;
3834 case 301:
3835 msg_fmt = HTTP_301;
3836 break;
3837 case 302:
3838 default:
3839 msg_fmt = HTTP_302;
3840 break;
3841 }
3842
3843 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3844 return 0;
3845
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003846 location = trash.str + trash.len;
3847
Willy Tarreau71241ab2012-12-27 11:30:54 +01003848 switch(rule->type) {
3849 case REDIRECT_TYPE_SCHEME: {
3850 const char *path;
3851 const char *host;
3852 struct hdr_ctx ctx;
3853 int pathlen;
3854 int hostlen;
3855
3856 host = "";
3857 hostlen = 0;
3858 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003859 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003860 host = ctx.line + ctx.val;
3861 hostlen = ctx.vlen;
3862 }
3863
3864 path = http_get_path(txn);
3865 /* build message using path */
3866 if (path) {
3867 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3868 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3869 int qs = 0;
3870 while (qs < pathlen) {
3871 if (path[qs] == '?') {
3872 pathlen = qs;
3873 break;
3874 }
3875 qs++;
3876 }
3877 }
3878 } else {
3879 path = "/";
3880 pathlen = 1;
3881 }
3882
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003883 if (rule->rdr_str) { /* this is an old "redirect" rule */
3884 /* check if we can add scheme + "://" + host + path */
3885 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3886 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003887
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003888 /* add scheme */
3889 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3890 trash.len += rule->rdr_len;
3891 }
3892 else {
3893 /* add scheme with executing log format */
3894 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003895
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003896 /* check if we can add scheme + "://" + host + path */
3897 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3898 return 0;
3899 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003900 /* add "://" */
3901 memcpy(trash.str + trash.len, "://", 3);
3902 trash.len += 3;
3903
3904 /* add host */
3905 memcpy(trash.str + trash.len, host, hostlen);
3906 trash.len += hostlen;
3907
3908 /* add path */
3909 memcpy(trash.str + trash.len, path, pathlen);
3910 trash.len += pathlen;
3911
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003912 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003913 if (trash.len && trash.str[trash.len - 1] != '/' &&
3914 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3915 if (trash.len > trash.size - 5)
3916 return 0;
3917 trash.str[trash.len] = '/';
3918 trash.len++;
3919 }
3920
3921 break;
3922 }
3923 case REDIRECT_TYPE_PREFIX: {
3924 const char *path;
3925 int pathlen;
3926
3927 path = http_get_path(txn);
3928 /* build message using path */
3929 if (path) {
3930 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3931 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3932 int qs = 0;
3933 while (qs < pathlen) {
3934 if (path[qs] == '?') {
3935 pathlen = qs;
3936 break;
3937 }
3938 qs++;
3939 }
3940 }
3941 } else {
3942 path = "/";
3943 pathlen = 1;
3944 }
3945
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003946 if (rule->rdr_str) { /* this is an old "redirect" rule */
3947 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3948 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003949
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003950 /* add prefix. Note that if prefix == "/", we don't want to
3951 * add anything, otherwise it makes it hard for the user to
3952 * configure a self-redirection.
3953 */
3954 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3955 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3956 trash.len += rule->rdr_len;
3957 }
3958 }
3959 else {
3960 /* add prefix with executing log format */
3961 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3962
3963 /* Check length */
3964 if (trash.len + pathlen > trash.size - 4)
3965 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003966 }
3967
3968 /* add path */
3969 memcpy(trash.str + trash.len, path, pathlen);
3970 trash.len += pathlen;
3971
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003972 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003973 if (trash.len && trash.str[trash.len - 1] != '/' &&
3974 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3975 if (trash.len > trash.size - 5)
3976 return 0;
3977 trash.str[trash.len] = '/';
3978 trash.len++;
3979 }
3980
3981 break;
3982 }
3983 case REDIRECT_TYPE_LOCATION:
3984 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003985 if (rule->rdr_str) { /* this is an old "redirect" rule */
3986 if (trash.len + rule->rdr_len > trash.size - 4)
3987 return 0;
3988
3989 /* add location */
3990 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3991 trash.len += rule->rdr_len;
3992 }
3993 else {
3994 /* add location with executing log format */
3995 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003996
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003997 /* Check left length */
3998 if (trash.len > trash.size - 4)
3999 return 0;
4000 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004001 break;
4002 }
4003
4004 if (rule->cookie_len) {
4005 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4006 trash.len += 14;
4007 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4008 trash.len += rule->cookie_len;
4009 memcpy(trash.str + trash.len, "\r\n", 2);
4010 trash.len += 2;
4011 }
4012
4013 /* add end of headers and the keep-alive/close status.
4014 * We may choose to set keep-alive if the Location begins
4015 * with a slash, because the client will come back to the
4016 * same server.
4017 */
4018 txn->status = rule->code;
4019 /* let's log the request time */
4020 s->logs.tv_request = now;
4021
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004022 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004023 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4024 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4025 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4026 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4027 /* keep-alive possible */
4028 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4029 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4030 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4031 trash.len += 30;
4032 } else {
4033 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4034 trash.len += 24;
4035 }
4036 }
4037 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4038 trash.len += 4;
4039 bo_inject(txn->rsp.chn, trash.str, trash.len);
4040 /* "eat" the request */
4041 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004042 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004043 msg->sov = 0;
4044 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004045 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004046 txn->req.msg_state = HTTP_MSG_CLOSED;
4047 txn->rsp.msg_state = HTTP_MSG_DONE;
4048 } else {
4049 /* keep-alive not possible */
4050 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4051 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4052 trash.len += 29;
4053 } else {
4054 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4055 trash.len += 23;
4056 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004057 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004058 txn->req.chn->analysers = 0;
4059 }
4060
Willy Tarreaue7dff022015-04-03 01:14:29 +02004061 if (!(s->flags & SF_ERR_MASK))
4062 s->flags |= SF_ERR_LOCAL;
4063 if (!(s->flags & SF_FINST_MASK))
4064 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004065
4066 return 1;
4067}
4068
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004069/* This stream analyser runs all HTTP request processing which is common to
4070 * frontends and backends, which means blocking ACLs, filters, connection-close,
4071 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004072 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004073 * either needs more data or wants to immediately abort the request (eg: deny,
4074 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004075 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004076int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004077{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004078 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004079 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004080 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004081 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004082 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004083 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004084
Willy Tarreau655dce92009-11-08 13:10:58 +01004085 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004086 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004087 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004088 }
4089
Willy Tarreau87b09662015-04-03 00:22:06 +02004090 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 +02004091 now_ms, __FUNCTION__,
4092 s,
4093 req,
4094 req->rex, req->wex,
4095 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004096 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004097 req->analysers);
4098
Willy Tarreau65410832014-04-28 21:25:43 +02004099 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004100 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004101
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004102 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004103 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004104 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004105
Willy Tarreau0b748332014-04-29 00:13:29 +02004106 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004107 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4108 goto return_prx_yield;
4109
Willy Tarreau0b748332014-04-29 00:13:29 +02004110 case HTTP_RULE_RES_CONT:
4111 case HTTP_RULE_RES_STOP: /* nothing to do */
4112 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004113
Willy Tarreau0b748332014-04-29 00:13:29 +02004114 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4115 if (txn->flags & TX_CLTARPIT)
4116 goto tarpit;
4117 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004118
Willy Tarreau0b748332014-04-29 00:13:29 +02004119 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4120 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004121
Willy Tarreau0b748332014-04-29 00:13:29 +02004122 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004123 goto done;
4124
Willy Tarreau0b748332014-04-29 00:13:29 +02004125 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4126 goto return_bad_req;
4127 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004128 }
4129
Willy Tarreau52542592014-04-28 18:33:26 +02004130 /* OK at this stage, we know that the request was accepted according to
4131 * the http-request rules, we can check for the stats. Note that the
4132 * URI is detected *before* the req* rules in order not to be affected
4133 * by a possible reqrep, while they are processed *after* so that a
4134 * reqdeny can still block them. This clearly needs to change in 1.6!
4135 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004136 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004137 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004138 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004139 txn->status = 500;
4140 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004141 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004142
Willy Tarreaue7dff022015-04-03 01:14:29 +02004143 if (!(s->flags & SF_ERR_MASK))
4144 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004145 goto return_prx_cond;
4146 }
4147
4148 /* parse the whole stats request and extract the relevant information */
4149 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004150 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004151 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004152
Willy Tarreau0b748332014-04-29 00:13:29 +02004153 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4154 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004155
Willy Tarreau0b748332014-04-29 00:13:29 +02004156 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4157 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004158 }
4159
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004160 /* evaluate the req* rules except reqadd */
4161 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004162 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004163 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004164
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004165 if (txn->flags & TX_CLDENY)
4166 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004167
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004168 if (txn->flags & TX_CLTARPIT)
4169 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004170 }
Willy Tarreau06619262006-12-17 08:37:22 +01004171
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004172 /* add request headers from the rule sets in the same order */
4173 list_for_each_entry(wl, &px->req_add, list) {
4174 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004175 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004176 ret = acl_pass(ret);
4177 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4178 ret = !ret;
4179 if (!ret)
4180 continue;
4181 }
4182
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004183 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004184 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004185 }
4186
Willy Tarreau52542592014-04-28 18:33:26 +02004187
4188 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004189 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004190 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004191 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4192 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004193
Willy Tarreaue7dff022015-04-03 01:14:29 +02004194 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4195 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4196 if (!(s->flags & SF_FINST_MASK))
4197 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004198
Willy Tarreau70730dd2014-04-24 18:06:27 +02004199 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004200 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004201 select_compression_request_header(s, req->buf);
4202
4203 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004204 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004205 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004206 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004207
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004208 /* check whether we have some ACLs set to redirect this request */
4209 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004210 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004211 int ret;
4212
Willy Tarreau192252e2015-04-04 01:47:55 +02004213 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004214 ret = acl_pass(ret);
4215 if (rule->cond->pol == ACL_COND_UNLESS)
4216 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004217 if (!ret)
4218 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004219 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004220 if (!http_apply_redirect_rule(rule, s, txn))
4221 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004222 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004223 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004224
Willy Tarreau2be39392010-01-03 17:24:51 +01004225 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4226 * If this happens, then the data will not come immediately, so we must
4227 * send all what we have without waiting. Note that due to the small gain
4228 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004229 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004230 * itself once used.
4231 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004232 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004233
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004234 done: /* done with this analyser, continue with next ones that the calling
4235 * points will have set, if any.
4236 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004237 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004238 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4239 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004240 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004241
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004242 tarpit:
4243 /* When a connection is tarpitted, we use the tarpit timeout,
4244 * which may be the same as the connect timeout if unspecified.
4245 * If unset, then set it to zero because we really want it to
4246 * eventually expire. We build the tarpit as an analyser.
4247 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004248 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004249
4250 /* wipe the request out so that we can drop the connection early
4251 * if the client closes first.
4252 */
4253 channel_dont_connect(req);
4254 req->analysers = 0; /* remove switching rules etc... */
4255 req->analysers |= AN_REQ_HTTP_TARPIT;
4256 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4257 if (!req->analyse_exp)
4258 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004259 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004260 sess->fe->fe_counters.denied_req++;
4261 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004262 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004263 if (sess->listener->counters)
4264 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004265 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004266
4267 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004268 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004269 txn->status = 403;
4270 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004271 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_403));
Willy Tarreau87b09662015-04-03 00:22:06 +02004272 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004273 sess->fe->fe_counters.denied_req++;
4274 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004275 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004276 if (sess->listener->counters)
4277 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004278 goto return_prx_cond;
4279
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004280 return_bad_req:
4281 /* We centralize bad requests processing here */
4282 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4283 /* we detected a parsing error. We want to archive this request
4284 * in the dedicated proxy area for later troubleshooting.
4285 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004286 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004287 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004288
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004289 txn->req.msg_state = HTTP_MSG_ERROR;
4290 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004291 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004292
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004293 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004294 if (sess->listener->counters)
4295 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004296
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004297 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004298 if (!(s->flags & SF_ERR_MASK))
4299 s->flags |= SF_ERR_PRXCOND;
4300 if (!(s->flags & SF_FINST_MASK))
4301 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004302
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004303 req->analysers = 0;
4304 req->analyse_exp = TICK_ETERNITY;
4305 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004306
4307 return_prx_yield:
4308 channel_dont_connect(req);
4309 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004310}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004311
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004312/* This function performs all the processing enabled for the current request.
4313 * It returns 1 if the processing can continue on next analysers, or zero if it
4314 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004315 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004316 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004317int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004318{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004319 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004320 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004321 struct http_msg *msg = &txn->req;
Willy Tarreau350f4872014-11-28 14:42:25 +01004322 struct connection *cli_conn = objt_conn(s->si[1].end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004323
Willy Tarreau655dce92009-11-08 13:10:58 +01004324 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004325 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004326 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004327 return 0;
4328 }
4329
Willy Tarreau87b09662015-04-03 00:22:06 +02004330 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 +02004331 now_ms, __FUNCTION__,
4332 s,
4333 req,
4334 req->rex, req->wex,
4335 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004336 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004337 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004338
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004339 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004340 select_compression_request_header(s, req->buf);
4341
Willy Tarreau59234e92008-11-30 23:51:27 +01004342 /*
4343 * Right now, we know that we have processed the entire headers
4344 * and that unwanted requests have been filtered out. We can do
4345 * whatever we want with the remaining request. Also, now we
4346 * may have separate values for ->fe, ->be.
4347 */
Willy Tarreau06619262006-12-17 08:37:22 +01004348
Willy Tarreau59234e92008-11-30 23:51:27 +01004349 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004350 * If HTTP PROXY is set we simply get remote server address parsing
4351 * incoming request. Note that this requires that a connection is
4352 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004353 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004354 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004355 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004356 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004357
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004358 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004359 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004360 txn->req.msg_state = HTTP_MSG_ERROR;
4361 txn->status = 500;
4362 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004363 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004364
Willy Tarreaue7dff022015-04-03 01:14:29 +02004365 if (!(s->flags & SF_ERR_MASK))
4366 s->flags |= SF_ERR_RESOURCE;
4367 if (!(s->flags & SF_FINST_MASK))
4368 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004369
4370 return 0;
4371 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004372
4373 path = http_get_path(txn);
4374 url2sa(req->buf->p + msg->sl.rq.u,
4375 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004376 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004377 /* if the path was found, we have to remove everything between
4378 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4379 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4380 * u_l characters by a single "/".
4381 */
4382 if (path) {
4383 char *cur_ptr = req->buf->p;
4384 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4385 int delta;
4386
4387 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4388 http_msg_move_end(&txn->req, delta);
4389 cur_end += delta;
4390 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4391 goto return_bad_req;
4392 }
4393 else {
4394 char *cur_ptr = req->buf->p;
4395 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4396 int delta;
4397
4398 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4399 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4400 http_msg_move_end(&txn->req, delta);
4401 cur_end += delta;
4402 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4403 goto return_bad_req;
4404 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004405 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004406
Willy Tarreau59234e92008-11-30 23:51:27 +01004407 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004408 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004409 * Note that doing so might move headers in the request, but
4410 * the fields will stay coherent and the URI will not move.
4411 * This should only be performed in the backend.
4412 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004413 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004414 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4415 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004416
Willy Tarreau59234e92008-11-30 23:51:27 +01004417 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004418 * 8: the appsession cookie was looked up very early in 1.2,
4419 * so let's do the same now.
4420 */
4421
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004422 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004423 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004424 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 +01004425 }
4426
William Lallemanda73203e2012-03-12 12:48:57 +01004427 /* add unique-id if "header-unique-id" is specified */
4428
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004429 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004430 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4431 goto return_bad_req;
4432 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004433 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004434 }
William Lallemanda73203e2012-03-12 12:48:57 +01004435
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004436 if (sess->fe->header_unique_id && s->unique_id) {
4437 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004438 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004439 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004440 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004441 goto return_bad_req;
4442 }
4443
Cyril Bontéb21570a2009-11-29 20:04:48 +01004444 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004445 * 9: add X-Forwarded-For if either the frontend or the backend
4446 * asks for it.
4447 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004448 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004449 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004450 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4451 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4452 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004453 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004454 /* The header is set to be added only if none is present
4455 * and we found it, so don't do anything.
4456 */
4457 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004458 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004459 /* Add an X-Forwarded-For header unless the source IP is
4460 * in the 'except' network range.
4461 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004462 if ((!sess->fe->except_mask.s_addr ||
4463 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4464 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004465 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004466 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004467 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004468 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004469 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004470 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004471
4472 /* Note: we rely on the backend to get the header name to be used for
4473 * x-forwarded-for, because the header is really meant for the backends.
4474 * However, if the backend did not specify any option, we have to rely
4475 * on the frontend's header name.
4476 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004477 if (s->be->fwdfor_hdr_len) {
4478 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004479 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004480 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004481 len = sess->fe->fwdfor_hdr_len;
4482 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004483 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004484 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 +01004485
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004486 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004487 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004488 }
4489 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004490 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004491 /* FIXME: for the sake of completeness, we should also support
4492 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004493 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004494 int len;
4495 char pn[INET6_ADDRSTRLEN];
4496 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004497 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004498 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004499
Willy Tarreau59234e92008-11-30 23:51:27 +01004500 /* Note: we rely on the backend to get the header name to be used for
4501 * x-forwarded-for, because the header is really meant for the backends.
4502 * However, if the backend did not specify any option, we have to rely
4503 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004504 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004505 if (s->be->fwdfor_hdr_len) {
4506 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004507 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004508 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004509 len = sess->fe->fwdfor_hdr_len;
4510 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004511 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004512 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004513
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004514 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004515 goto return_bad_req;
4516 }
4517 }
4518
4519 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004520 * 10: add X-Original-To if either the frontend or the backend
4521 * asks for it.
4522 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004523 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004524
4525 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004526 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004527 /* Add an X-Original-To header unless the destination IP is
4528 * in the 'except' network range.
4529 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004530 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004531
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004532 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004533 ((!sess->fe->except_mask_to.s_addr ||
4534 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4535 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004536 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004537 (((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 +02004538 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004539 int len;
4540 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004541 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004542
4543 /* Note: we rely on the backend to get the header name to be used for
4544 * x-original-to, because the header is really meant for the backends.
4545 * However, if the backend did not specify any option, we have to rely
4546 * on the frontend's header name.
4547 */
4548 if (s->be->orgto_hdr_len) {
4549 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004550 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004551 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004552 len = sess->fe->orgto_hdr_len;
4553 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004554 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004555 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 +02004556
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004557 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004558 goto return_bad_req;
4559 }
4560 }
4561 }
4562
Willy Tarreau50fc7772012-11-11 22:19:57 +01004563 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4564 * If an "Upgrade" token is found, the header is left untouched in order not to have
4565 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4566 * "Upgrade" is present in the Connection header.
4567 */
4568 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4569 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004570 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004571 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004572 unsigned int want_flags = 0;
4573
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004574 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004575 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004576 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004577 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004578 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004579 want_flags |= TX_CON_CLO_SET;
4580 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004581 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004582 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004583 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004584 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004585 want_flags |= TX_CON_KAL_SET;
4586 }
4587
4588 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004589 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004590 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004591
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004592
Willy Tarreau522d6c02009-12-06 18:49:18 +01004593 /* If we have no server assigned yet and we're balancing on url_param
4594 * with a POST request, we may be interested in checking the body for
4595 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004596 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004597 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004598 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004599 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004600 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004601 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004602 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004603
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004604 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004605 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004606#ifdef TCP_QUICKACK
4607 /* We expect some data from the client. Unless we know for sure
4608 * we already have a full request, we have to re-enable quick-ack
4609 * in case we previously disabled it, otherwise we might cause
4610 * the client to delay further data.
4611 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004612 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004613 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004614 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004615 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004616 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004617#endif
4618 }
Willy Tarreau03945942009-12-22 16:50:27 +01004619
Willy Tarreau59234e92008-11-30 23:51:27 +01004620 /*************************************************************
4621 * OK, that's finished for the headers. We have done what we *
4622 * could. Let's switch to the DATA state. *
4623 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004624 req->analyse_exp = TICK_ETERNITY;
4625 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004626
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004627 /* if the server closes the connection, we want to immediately react
4628 * and close the socket to save packets and syscalls.
4629 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004630 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004631 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004632
Willy Tarreau59234e92008-11-30 23:51:27 +01004633 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004634 /* OK let's go on with the BODY now */
4635 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004636
Willy Tarreau59234e92008-11-30 23:51:27 +01004637 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004638 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004639 /* we detected a parsing error. We want to archive this request
4640 * in the dedicated proxy area for later troubleshooting.
4641 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004642 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004643 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004644
Willy Tarreau59234e92008-11-30 23:51:27 +01004645 txn->req.msg_state = HTTP_MSG_ERROR;
4646 txn->status = 400;
4647 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004648 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004649
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004650 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004651 if (sess->listener->counters)
4652 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004653
Willy Tarreaue7dff022015-04-03 01:14:29 +02004654 if (!(s->flags & SF_ERR_MASK))
4655 s->flags |= SF_ERR_PRXCOND;
4656 if (!(s->flags & SF_FINST_MASK))
4657 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004658 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004659}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004660
Willy Tarreau60b85b02008-11-30 23:28:40 +01004661/* This function is an analyser which processes the HTTP tarpit. It always
4662 * returns zero, at the beginning because it prevents any other processing
4663 * from occurring, and at the end because it terminates the request.
4664 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004665int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004666{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004667 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004668
4669 /* This connection is being tarpitted. The CLIENT side has
4670 * already set the connect expiration date to the right
4671 * timeout. We just have to check that the client is still
4672 * there and that the timeout has not expired.
4673 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004674 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004675 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004676 !tick_is_expired(req->analyse_exp, now_ms))
4677 return 0;
4678
4679 /* We will set the queue timer to the time spent, just for
4680 * logging purposes. We fake a 500 server error, so that the
4681 * attacker will not suspect his connection has been tarpitted.
4682 * It will not cause trouble to the logs because we can exclude
4683 * the tarpitted connections by filtering on the 'PT' status flags.
4684 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004685 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4686
4687 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004688 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004689 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004690
4691 req->analysers = 0;
4692 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004693
Willy Tarreaue7dff022015-04-03 01:14:29 +02004694 if (!(s->flags & SF_ERR_MASK))
4695 s->flags |= SF_ERR_PRXCOND;
4696 if (!(s->flags & SF_FINST_MASK))
4697 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004698 return 0;
4699}
4700
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004701/* This function is an analyser which waits for the HTTP request body. It waits
4702 * for either the buffer to be full, or the full advertised contents to have
4703 * reached the buffer. It must only be called after the standard HTTP request
4704 * processing has occurred, because it expects the request to be parsed and will
4705 * look for the Expect header. It may send a 100-Continue interim response. It
4706 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4707 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4708 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004709 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004710int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004711{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004712 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004713 struct http_txn *txn = s->txn;
4714 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004715
4716 /* We have to parse the HTTP request body to find any required data.
4717 * "balance url_param check_post" should have been the only way to get
4718 * into this. We were brought here after HTTP header analysis, so all
4719 * related structures are ready.
4720 */
4721
Willy Tarreau890988f2014-04-10 11:59:33 +02004722 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4723 /* This is the first call */
4724 if (msg->msg_state < HTTP_MSG_BODY)
4725 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004726
Willy Tarreau890988f2014-04-10 11:59:33 +02004727 if (msg->msg_state < HTTP_MSG_100_SENT) {
4728 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4729 * send an HTTP/1.1 100 Continue intermediate response.
4730 */
4731 if (msg->flags & HTTP_MSGF_VER_11) {
4732 struct hdr_ctx ctx;
4733 ctx.idx = 0;
4734 /* Expect is allowed in 1.1, look for it */
4735 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4736 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004737 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004738 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004739 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004740 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004741 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004742
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004743 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004744 * req->buf->p still points to the beginning of the message. We
4745 * must save the body in msg->next because it survives buffer
4746 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004747 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004748 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004749
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004750 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004751 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4752 else
4753 msg->msg_state = HTTP_MSG_DATA;
4754 }
4755
Willy Tarreau890988f2014-04-10 11:59:33 +02004756 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4757 /* We're in content-length mode, we just have to wait for enough data. */
4758 if (req->buf->i - msg->sov < msg->body_len)
4759 goto missing_data;
4760
4761 /* OK we have everything we need now */
4762 goto http_end;
4763 }
4764
4765 /* OK here we're parsing a chunked-encoded message */
4766
Willy Tarreau522d6c02009-12-06 18:49:18 +01004767 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004768 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004769 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004770 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004771 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004772 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004773
Willy Tarreau115acb92009-12-26 13:56:06 +01004774 if (!ret)
4775 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004776 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004777 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004778 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004779 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004780 }
4781
Willy Tarreaud98cf932009-12-27 22:54:55 +01004782 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004783 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004784 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004785 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004786 if (msg->msg_state == HTTP_MSG_TRAILERS)
4787 goto http_end;
4788
Willy Tarreau226071e2014-04-10 11:55:45 +02004789 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004790 goto http_end;
4791
4792 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004793 /* we get here if we need to wait for more data. If the buffer is full,
4794 * we have the maximum we can expect.
4795 */
4796 if (buffer_full(req->buf, global.tune.maxrewrite))
4797 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004798
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004799 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004800 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004801 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004802
Willy Tarreaue7dff022015-04-03 01:14:29 +02004803 if (!(s->flags & SF_ERR_MASK))
4804 s->flags |= SF_ERR_CLITO;
4805 if (!(s->flags & SF_FINST_MASK))
4806 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004807 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004808 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004809
4810 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004811 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004812 /* Not enough data. We'll re-use the http-request
4813 * timeout here. Ideally, we should set the timeout
4814 * relative to the accept() date. We just set the
4815 * request timeout once at the beginning of the
4816 * request.
4817 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004818 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004819 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004820 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004821 return 0;
4822 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004823
4824 http_end:
4825 /* The situation will not evolve, so let's give up on the analysis. */
4826 s->logs.tv_request = now; /* update the request timer to reflect full request */
4827 req->analysers &= ~an_bit;
4828 req->analyse_exp = TICK_ETERNITY;
4829 return 1;
4830
4831 return_bad_req: /* let's centralize all bad requests */
4832 txn->req.msg_state = HTTP_MSG_ERROR;
4833 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004834 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004835
Willy Tarreaue7dff022015-04-03 01:14:29 +02004836 if (!(s->flags & SF_ERR_MASK))
4837 s->flags |= SF_ERR_PRXCOND;
4838 if (!(s->flags & SF_FINST_MASK))
4839 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004840
Willy Tarreau522d6c02009-12-06 18:49:18 +01004841 return_err_msg:
4842 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004843 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004844 if (sess->listener->counters)
4845 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004846 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004847}
4848
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004849/* send a server's name with an outgoing request over an established connection.
4850 * Note: this function is designed to be called once the request has been scheduled
4851 * for being forwarded. This is the reason why it rewinds the buffer before
4852 * proceeding.
4853 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004854int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004855
4856 struct hdr_ctx ctx;
4857
Mark Lamourinec2247f02012-01-04 13:02:01 -05004858 char *hdr_name = be->server_id_hdr_name;
4859 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004860 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004861 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004862 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004863
William Lallemandd9e90662012-01-30 17:27:17 +01004864 ctx.idx = 0;
4865
Willy Tarreau211cdec2014-04-17 20:18:08 +02004866 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004867 if (old_o) {
4868 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004869 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004870 txn->req.next += old_o;
4871 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004872 }
4873
Willy Tarreau9b28e032012-10-12 23:49:43 +02004874 old_i = chn->buf->i;
4875 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 -05004876 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004877 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004878 }
4879
4880 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004881 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004882 memcpy(hdr_val, hdr_name, hdr_name_len);
4883 hdr_val += hdr_name_len;
4884 *hdr_val++ = ':';
4885 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004886 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4887 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004888
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004889 if (old_o) {
4890 /* If this was a forwarded request, we must readjust the amount of
4891 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004892 * variations. Note that the current state is >= HTTP_MSG_BODY,
4893 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004894 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004895 old_o += chn->buf->i - old_i;
4896 b_adv(chn->buf, old_o);
4897 txn->req.next -= old_o;
4898 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004899 }
4900
Mark Lamourinec2247f02012-01-04 13:02:01 -05004901 return 0;
4902}
4903
Willy Tarreau610ecce2010-01-04 21:15:02 +01004904/* Terminate current transaction and prepare a new one. This is very tricky
4905 * right now but it works.
4906 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004907void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004908{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004909 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004910 struct proxy *fe = strm_fe(s);
Willy Tarreau068621e2013-12-23 15:11:25 +01004911
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912 /* FIXME: We need a more portable way of releasing a backend's and a
4913 * server's connections. We need a safer way to reinitialize buffer
4914 * flags. We also need a more accurate method for computing per-request
4915 * data.
4916 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004917
Willy Tarreau4213a112013-12-15 10:25:42 +01004918 /* unless we're doing keep-alive, we want to quickly close the connection
4919 * to the server.
4920 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004921 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004922 !si_conn_ready(&s->si[1])) {
4923 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4924 si_shutr(&s->si[1]);
4925 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004926 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004927
Willy Tarreaue7dff022015-04-03 01:14:29 +02004928 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004929 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004930 if (unlikely(s->srv_conn))
4931 sess_change_server(s, NULL);
4932 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004933
4934 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004935 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004936
Willy Tarreaueee5b512015-04-03 23:46:31 +02004937 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004938 int n;
4939
Willy Tarreaueee5b512015-04-03 23:46:31 +02004940 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 if (n < 1 || n > 5)
4942 n = 0;
4943
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004944 if (fe->mode == PR_MODE_HTTP) {
4945 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004946 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004947 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004948 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004949 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004950 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004951 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004952 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004953 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004954 s->be->be_counters.p.http.comp_rsp++;
4955 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004956 }
4957
4958 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004959 s->logs.bytes_in -= s->req.buf->i;
4960 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004961
4962 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004963 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004964 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004965 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004966 s->do_log(s);
4967 }
4968
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004969 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004970 stream_stop_content_counters(s);
4971 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004972
Willy Tarreau610ecce2010-01-04 21:15:02 +01004973 s->logs.accept_date = date; /* user-visible date for logging */
4974 s->logs.tv_accept = now; /* corrected date for internal use */
4975 tv_zero(&s->logs.tv_request);
4976 s->logs.t_queue = -1;
4977 s->logs.t_connect = -1;
4978 s->logs.t_data = -1;
4979 s->logs.t_close = 0;
4980 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4981 s->logs.srv_queue_size = 0; /* we will get this number soon */
4982
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004983 s->logs.bytes_in = s->req.total = s->req.buf->i;
4984 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004985
4986 if (s->pend_pos)
4987 pendconn_free(s->pend_pos);
4988
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004989 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004990 if (s->flags & SF_CURR_SESS) {
4991 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004992 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004994 if (may_dequeue_tasks(objt_server(s->target), s->be))
4995 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004996 }
4997
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004998 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999
Willy Tarreau4213a112013-12-15 10:25:42 +01005000 /* only release our endpoint if we don't intend to reuse the
5001 * connection.
5002 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005003 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005004 !si_conn_ready(&s->si[1])) {
5005 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005006 }
5007
Willy Tarreau350f4872014-11-28 14:42:25 +01005008 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5009 s->si[1].err_type = SI_ET_NONE;
5010 s->si[1].conn_retries = 0; /* used for logging too */
5011 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005012 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 +01005013 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);
5014 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 +02005015 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5016 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5017 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005018
Willy Tarreaueee5b512015-04-03 23:46:31 +02005019 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005020 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005021 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005022
5023 if (prev_status == 401 || prev_status == 407) {
5024 /* In HTTP keep-alive mode, if we receive a 401, we still have
5025 * a chance of being able to send the visitor again to the same
5026 * server over the same connection. This is required by some
5027 * broken protocols such as NTLM, and anyway whenever there is
5028 * an opportunity for sending the challenge to the proper place,
5029 * it's better to do it (at least it helps with debugging).
5030 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005031 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01005032 }
5033
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005034 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005035 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005036
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005037 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005038 s->req.flags |= CF_NEVER_WAIT;
5039 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005040 }
5041
Willy Tarreau610ecce2010-01-04 21:15:02 +01005042 /* if the request buffer is not empty, it means we're
5043 * about to process another request, so send pending
5044 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005045 * Just don't do this if the buffer is close to be full,
5046 * because the request will wait for it to flush a little
5047 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005048 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005049 if (s->req.buf->i) {
5050 if (s->res.buf->o &&
5051 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5052 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5053 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005054 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005055
5056 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005057 channel_auto_read(&s->req);
5058 channel_auto_close(&s->req);
5059 channel_auto_read(&s->res);
5060 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005061
Willy Tarreau27375622013-12-17 00:00:28 +01005062 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005063 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005064
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005065 s->req.analysers = strm_li(s)->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005066 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005067}
5068
5069
5070/* This function updates the request state machine according to the response
5071 * state machine and buffer flags. It returns 1 if it changes anything (flag
5072 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5073 * it is only used to find when a request/response couple is complete. Both
5074 * this function and its equivalent should loop until both return zero. It
5075 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5076 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005077int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005079 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005080 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005081 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005082 unsigned int old_state = txn->req.msg_state;
5083
Willy Tarreau610ecce2010-01-04 21:15:02 +01005084 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5085 return 0;
5086
5087 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005088 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005089 * We can shut the read side unless we want to abort_on_close,
5090 * or we have a POST request. The issue with POST requests is
5091 * that some browsers still send a CRLF after the request, and
5092 * this CRLF must be read so that it does not remain in the kernel
5093 * buffers, otherwise a close could cause an RST on some systems
5094 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005095 * Note that if we're using keep-alive on the client side, we'd
5096 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005097 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005098 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005099 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005100 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5101 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5102 !(s->be->options & PR_O_ABRT_CLOSE) &&
5103 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005104 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005105
Willy Tarreau40f151a2012-12-20 12:10:09 +01005106 /* if the server closes the connection, we want to immediately react
5107 * and close the socket to save packets and syscalls.
5108 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005109 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005110
Willy Tarreau610ecce2010-01-04 21:15:02 +01005111 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5112 goto wait_other_side;
5113
5114 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5115 /* The server has not finished to respond, so we
5116 * don't want to move in order not to upset it.
5117 */
5118 goto wait_other_side;
5119 }
5120
5121 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5122 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005123 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005124 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005125 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005126 goto wait_other_side;
5127 }
5128
5129 /* When we get here, it means that both the request and the
5130 * response have finished receiving. Depending on the connection
5131 * mode, we'll have to wait for the last bytes to leave in either
5132 * direction, and sometimes for a close to be effective.
5133 */
5134
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005135 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5136 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005137 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5138 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005139 }
5140 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5141 /* Option forceclose is set, or either side wants to close,
5142 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005143 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005144 * once both states are CLOSED.
5145 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005146 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5147 channel_shutr_now(chn);
5148 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005149 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005150 }
5151 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005152 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5153 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005154 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005155 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5156 channel_auto_read(chn);
5157 txn->req.msg_state = HTTP_MSG_TUNNEL;
5158 chn->flags |= CF_NEVER_WAIT;
5159 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005160 }
5161
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005162 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005163 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005164 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005165
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005166 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005167 txn->req.msg_state = HTTP_MSG_CLOSING;
5168 goto http_msg_closing;
5169 }
5170 else {
5171 txn->req.msg_state = HTTP_MSG_CLOSED;
5172 goto http_msg_closed;
5173 }
5174 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005175 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005176 }
5177
5178 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5179 http_msg_closing:
5180 /* nothing else to forward, just waiting for the output buffer
5181 * to be empty and for the shutw_now to take effect.
5182 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005183 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184 txn->req.msg_state = HTTP_MSG_CLOSED;
5185 goto http_msg_closed;
5186 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005187 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188 txn->req.msg_state = HTTP_MSG_ERROR;
5189 goto wait_other_side;
5190 }
5191 }
5192
5193 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5194 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005195 /* see above in MSG_DONE why we only do this in these states */
5196 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5197 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5198 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005199 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200 goto wait_other_side;
5201 }
5202
5203 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005204 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005205}
5206
5207
5208/* This function updates the response state machine according to the request
5209 * state machine and buffer flags. It returns 1 if it changes anything (flag
5210 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5211 * it is only used to find when a request/response couple is complete. Both
5212 * this function and its equivalent should loop until both return zero. It
5213 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5214 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005215int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005217 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005218 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005219 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005220 unsigned int old_state = txn->rsp.msg_state;
5221
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5223 return 0;
5224
5225 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5226 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005227 * still monitor the server connection for a possible close
5228 * while the request is being uploaded, so we don't disable
5229 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005230 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005231 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005232
5233 if (txn->req.msg_state == HTTP_MSG_ERROR)
5234 goto wait_other_side;
5235
5236 if (txn->req.msg_state < HTTP_MSG_DONE) {
5237 /* The client seems to still be sending data, probably
5238 * because we got an error response during an upload.
5239 * We have the choice of either breaking the connection
5240 * or letting it pass through. Let's do the later.
5241 */
5242 goto wait_other_side;
5243 }
5244
5245 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5246 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005247 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005249 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005250 goto wait_other_side;
5251 }
5252
5253 /* When we get here, it means that both the request and the
5254 * response have finished receiving. Depending on the connection
5255 * mode, we'll have to wait for the last bytes to leave in either
5256 * direction, and sometimes for a close to be effective.
5257 */
5258
5259 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5260 /* Server-close mode : shut read and wait for the request
5261 * side to close its output buffer. The caller will detect
5262 * when we're in DONE and the other is in CLOSED and will
5263 * catch that for the final cleanup.
5264 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005265 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5266 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005267 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005268 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5269 /* Option forceclose is set, or either side wants to close,
5270 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005271 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005272 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005273 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005274 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5275 channel_shutr_now(chn);
5276 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005277 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005278 }
5279 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005280 /* The last possible modes are keep-alive and tunnel. Tunnel will
5281 * need to forward remaining data. Keep-alive will need to monitor
5282 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005283 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005284 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005285 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005286 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5287 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005288 }
5289
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005290 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005291 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005292 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005293 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5294 goto http_msg_closing;
5295 }
5296 else {
5297 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5298 goto http_msg_closed;
5299 }
5300 }
5301 goto wait_other_side;
5302 }
5303
5304 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5305 http_msg_closing:
5306 /* nothing else to forward, just waiting for the output buffer
5307 * to be empty and for the shutw_now to take effect.
5308 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005309 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005310 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5311 goto http_msg_closed;
5312 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005313 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005315 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005316 if (objt_server(s->target))
5317 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005318 goto wait_other_side;
5319 }
5320 }
5321
5322 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5323 http_msg_closed:
5324 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005325 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005326 channel_auto_close(chn);
5327 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005328 goto wait_other_side;
5329 }
5330
5331 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005332 /* We force the response to leave immediately if we're waiting for the
5333 * other side, since there is no pending shutdown to push it out.
5334 */
5335 if (!channel_is_empty(chn))
5336 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005337 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005338}
5339
5340
5341/* Resync the request and response state machines. Return 1 if either state
5342 * changes.
5343 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005344int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005346 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 int old_req_state = txn->req.msg_state;
5348 int old_res_state = txn->rsp.msg_state;
5349
Willy Tarreau610ecce2010-01-04 21:15:02 +01005350 http_sync_req_state(s);
5351 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005352 if (!http_sync_res_state(s))
5353 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005354 if (!http_sync_req_state(s))
5355 break;
5356 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005357
Willy Tarreau610ecce2010-01-04 21:15:02 +01005358 /* OK, both state machines agree on a compatible state.
5359 * There are a few cases we're interested in :
5360 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5361 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5362 * directions, so let's simply disable both analysers.
5363 * - HTTP_MSG_CLOSED on the response only means we must abort the
5364 * request.
5365 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5366 * with server-close mode means we've completed one request and we
5367 * must re-initialize the server connection.
5368 */
5369
5370 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5371 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5372 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5373 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005374 s->req.analysers = 0;
5375 channel_auto_close(&s->req);
5376 channel_auto_read(&s->req);
5377 s->res.analysers = 0;
5378 channel_auto_close(&s->res);
5379 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005381 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005382 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005383 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005384 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005385 s->res.analysers = 0;
5386 channel_auto_close(&s->res);
5387 channel_auto_read(&s->res);
5388 s->req.analysers = 0;
5389 channel_abort(&s->req);
5390 channel_auto_close(&s->req);
5391 channel_auto_read(&s->req);
5392 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005393 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005394 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5395 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005396 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005397 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5398 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5399 /* server-close/keep-alive: terminate this transaction,
5400 * possibly killing the server connection and reinitialize
5401 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005402 */
5403 http_end_txn_clean_session(s);
5404 }
5405
Willy Tarreau610ecce2010-01-04 21:15:02 +01005406 return txn->req.msg_state != old_req_state ||
5407 txn->rsp.msg_state != old_res_state;
5408}
5409
Willy Tarreaud98cf932009-12-27 22:54:55 +01005410/* This function is an analyser which forwards request body (including chunk
5411 * sizes if any). It is called as soon as we must forward, even if we forward
5412 * zero byte. The only situation where it must not be called is when we're in
5413 * tunnel mode and we want to forward till the close. It's used both to forward
5414 * remaining data and to resync after end of body. It expects the msg_state to
5415 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005416 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005417 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005418 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005419 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005420int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005421{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005422 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005423 struct http_txn *txn = s->txn;
5424 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005425
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005426 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5427 return 0;
5428
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005429 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005430 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005431 /* Output closed while we were sending data. We must abort and
5432 * wake the other side up.
5433 */
5434 msg->msg_state = HTTP_MSG_ERROR;
5435 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005436 return 1;
5437 }
5438
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439 /* Note that we don't have to send 100-continue back because we don't
5440 * need the data to complete our job, and it's up to the server to
5441 * decide whether to return 100, 417 or anything else in return of
5442 * an "Expect: 100-continue" header.
5443 */
5444
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005445 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005446 /* we have msg->sov which points to the first byte of message
5447 * body, and req->buf.p still points to the beginning of the
5448 * message. We forward the headers now, as we don't need them
5449 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005450 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005451 b_adv(req->buf, msg->sov);
5452 msg->next -= msg->sov;
5453 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005454
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005455 /* The previous analysers guarantee that the state is somewhere
5456 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5457 * msg->next are always correct.
5458 */
5459 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5460 if (msg->flags & HTTP_MSGF_TE_CHNK)
5461 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5462 else
5463 msg->msg_state = HTTP_MSG_DATA;
5464 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005465 }
5466
Willy Tarreau7ba23542014-04-17 21:50:00 +02005467 /* Some post-connect processing might want us to refrain from starting to
5468 * forward data. Currently, the only reason for this is "balance url_param"
5469 * whichs need to parse/process the request after we've enabled forwarding.
5470 */
5471 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005472 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005473 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005474 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005475 goto missing_data;
5476 }
5477 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5478 }
5479
Willy Tarreau80a92c02014-03-12 10:41:13 +01005480 /* in most states, we should abort in case of early close */
5481 channel_auto_close(req);
5482
Willy Tarreauefdf0942014-04-24 20:08:57 +02005483 if (req->to_forward) {
5484 /* We can't process the buffer's contents yet */
5485 req->flags |= CF_WAKE_WRITE;
5486 goto missing_data;
5487 }
5488
Willy Tarreaud98cf932009-12-27 22:54:55 +01005489 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005490 if (msg->msg_state == HTTP_MSG_DATA) {
5491 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005492 /* we may have some pending data starting at req->buf->p */
5493 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005494 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005495 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005496 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005497 msg->next += msg->chunk_len;
5498 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005499
5500 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005501 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005502 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005503 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005504 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005505 }
5506 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005507 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005508 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005509 * TRAILERS state.
5510 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005511 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005512
Willy Tarreau54d23df2012-10-25 19:04:45 +02005513 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005514 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005515 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005516 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005517 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005518 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005519 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005520 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005521 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005522 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005523 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005525 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005526
5527 if (ret == 0)
5528 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005529 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005530 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005531 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005532 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005533 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005534 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005535 /* we're in MSG_CHUNK_SIZE now */
5536 }
5537 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005538 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005539
5540 if (ret == 0)
5541 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005542 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005543 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005544 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005545 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005546 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005547 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005548 /* we're in HTTP_MSG_DONE now */
5549 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005550 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005551 int old_state = msg->msg_state;
5552
Willy Tarreau610ecce2010-01-04 21:15:02 +01005553 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005554
5555 /* we may have some pending data starting at req->buf->p
5556 * such as last chunk of data or trailers.
5557 */
5558 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005559 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005560 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005561 msg->next = 0;
5562
Willy Tarreau4fe41902010-06-07 22:27:41 +02005563 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005564 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5565 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005566 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005567 if (http_resync_states(s)) {
5568 /* some state changes occurred, maybe the analyser
5569 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005570 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005571 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005572 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005573 /* request errors are most likely due to
5574 * the server aborting the transfer.
5575 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005576 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005577 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005578 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005579 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005580 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005581 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005582 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005583 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005584
5585 /* If "option abortonclose" is set on the backend, we
5586 * want to monitor the client's connection and forward
5587 * any shutdown notification to the server, which will
5588 * decide whether to close or to go on processing the
5589 * request.
5590 */
5591 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005592 channel_auto_read(req);
5593 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005594 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005595 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005596 /* POST requests may require to read extra CRLF
5597 * sent by broken browsers and which could cause
5598 * an RST to be sent upon close on some systems
5599 * (eg: Linux).
5600 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005601 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005602 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005603
Willy Tarreau610ecce2010-01-04 21:15:02 +01005604 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005605 }
5606 }
5607
Willy Tarreaud98cf932009-12-27 22:54:55 +01005608 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005609 /* we may have some pending data starting at req->buf->p */
5610 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005611 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005612 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5613
Willy Tarreaubed410e2014-04-22 08:19:34 +02005614 msg->next = 0;
5615 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5616
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005617 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005618 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005619 if (!(s->flags & SF_ERR_MASK))
5620 s->flags |= SF_ERR_CLICL;
5621 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005622 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005623 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005624 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005625 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005626 }
5627
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005628 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005629 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005630 if (objt_server(s->target))
5631 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005632
5633 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005634 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005635
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005636 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005637 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005638 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005639
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005640 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005641 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005642 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005643 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005644 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005645
Willy Tarreau5c620922011-05-11 19:56:11 +02005646 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005647 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005648 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005649 * modes are already handled by the stream sock layer. We must not do
5650 * this in content-length mode because it could present the MSG_MORE
5651 * flag with the last block of forwarded data, which would cause an
5652 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005653 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005654 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005655 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005656
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005657 return 0;
5658
Willy Tarreaud98cf932009-12-27 22:54:55 +01005659 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005660 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005661 if (sess->listener->counters)
5662 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005663
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005664 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005665 /* we may have some pending data starting at req->buf->p */
5666 b_adv(req->buf, msg->next);
5667 msg->next = 0;
5668
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005669 txn->req.msg_state = HTTP_MSG_ERROR;
5670 if (txn->status) {
5671 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005672 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005673 } else {
5674 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005675 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005676 }
5677 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005678 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005679
Willy Tarreaue7dff022015-04-03 01:14:29 +02005680 if (!(s->flags & SF_ERR_MASK))
5681 s->flags |= SF_ERR_PRXCOND;
5682 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005683 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005684 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005685 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005686 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005687 }
5688 return 0;
5689
5690 aborted_xfer:
5691 txn->req.msg_state = HTTP_MSG_ERROR;
5692 if (txn->status) {
5693 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005694 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005695 } else {
5696 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005697 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005698 }
5699 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005700 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005701
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005702 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005703 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005704 if (objt_server(s->target))
5705 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005706
Willy Tarreaue7dff022015-04-03 01:14:29 +02005707 if (!(s->flags & SF_ERR_MASK))
5708 s->flags |= SF_ERR_SRVCL;
5709 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005710 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005711 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005712 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005713 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005714 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005715 return 0;
5716}
5717
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005718/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5719 * processing can continue on next analysers, or zero if it either needs more
5720 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005721 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005722 * when it has nothing left to do, and may remove any analyser when it wants to
5723 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005724 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005725int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005726{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005727 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005728 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005729 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005730 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005731 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005732 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005733 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005734
Willy Tarreau87b09662015-04-03 00:22:06 +02005735 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 +02005736 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005737 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005738 rep,
5739 rep->rex, rep->wex,
5740 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005741 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005742 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005743
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005744 /*
5745 * Now parse the partial (or complete) lines.
5746 * We will check the response syntax, and also join multi-line
5747 * headers. An index of all the lines will be elaborated while
5748 * parsing.
5749 *
5750 * For the parsing, we use a 28 states FSM.
5751 *
5752 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005753 * rep->buf->p = beginning of response
5754 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5755 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005756 * msg->eol = end of current header or line (LF or CRLF)
5757 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005758 */
5759
Willy Tarreau628c40c2014-04-24 19:11:26 +02005760 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005761 /* There's a protected area at the end of the buffer for rewriting
5762 * purposes. We don't want to start to parse the request if the
5763 * protected area is affected, because we may have to move processed
5764 * data later, which is much more complicated.
5765 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005766 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005767 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005768 /* some data has still not left the buffer, wake us once that's done */
5769 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5770 goto abort_response;
5771 channel_dont_close(rep);
5772 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005773 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005774 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005775 }
5776
Willy Tarreau379357a2013-06-08 12:55:46 +02005777 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5778 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5779 buffer_slow_realign(rep->buf);
5780
Willy Tarreau9b28e032012-10-12 23:49:43 +02005781 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005782 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005783 }
5784
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005785 /* 1: we might have to print this header in debug mode */
5786 if (unlikely((global.mode & MODE_DEBUG) &&
5787 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005788 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005789 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005790
Willy Tarreau9b28e032012-10-12 23:49:43 +02005791 sol = rep->buf->p;
5792 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005793 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005794
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005795 sol += hdr_idx_first_pos(&txn->hdr_idx);
5796 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005797
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005798 while (cur_idx) {
5799 eol = sol + txn->hdr_idx.v[cur_idx].len;
5800 debug_hdr("srvhdr", s, sol, eol);
5801 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5802 cur_idx = txn->hdr_idx.v[cur_idx].next;
5803 }
5804 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005805
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005806 /*
5807 * Now we quickly check if we have found a full valid response.
5808 * If not so, we check the FD and buffer states before leaving.
5809 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005810 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005811 * responses are checked first.
5812 *
5813 * Depending on whether the client is still there or not, we
5814 * may send an error response back or not. Note that normally
5815 * we should only check for HTTP status there, and check I/O
5816 * errors somewhere else.
5817 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005818
Willy Tarreau655dce92009-11-08 13:10:58 +01005819 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005820 /* Invalid response */
5821 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5822 /* we detected a parsing error. We want to archive this response
5823 * in the dedicated proxy area for later troubleshooting.
5824 */
5825 hdr_response_bad:
5826 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005827 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005828
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005829 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005830 if (objt_server(s->target)) {
5831 objt_server(s->target)->counters.failed_resp++;
5832 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005833 }
Willy Tarreau64648412010-03-05 10:41:54 +01005834 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005835 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005836 rep->analysers = 0;
5837 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005838 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005839 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005840 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005841
Willy Tarreaue7dff022015-04-03 01:14:29 +02005842 if (!(s->flags & SF_ERR_MASK))
5843 s->flags |= SF_ERR_PRXCOND;
5844 if (!(s->flags & SF_FINST_MASK))
5845 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005846
5847 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005848 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005849
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005850 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005851 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005852 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005853 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005854 goto hdr_response_bad;
5855 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005856
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005857 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005858 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005859 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005860 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005861 else if (txn->flags & TX_NOT_FIRST)
5862 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005863
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005864 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005865 if (objt_server(s->target)) {
5866 objt_server(s->target)->counters.failed_resp++;
5867 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005868 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005869
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005870 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005871 rep->analysers = 0;
5872 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005873 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005874 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005875 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005876
Willy Tarreaue7dff022015-04-03 01:14:29 +02005877 if (!(s->flags & SF_ERR_MASK))
5878 s->flags |= SF_ERR_SRVCL;
5879 if (!(s->flags & SF_FINST_MASK))
5880 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005881 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005882 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005883
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005884 /* read timeout : return a 504 to the client. */
5885 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005886 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005887 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005888 else if (txn->flags & TX_NOT_FIRST)
5889 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005890
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005891 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005892 if (objt_server(s->target)) {
5893 objt_server(s->target)->counters.failed_resp++;
5894 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005895 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005896
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005897 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005898 rep->analysers = 0;
5899 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005900 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005901 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005902 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005903
Willy Tarreaue7dff022015-04-03 01:14:29 +02005904 if (!(s->flags & SF_ERR_MASK))
5905 s->flags |= SF_ERR_SRVTO;
5906 if (!(s->flags & SF_FINST_MASK))
5907 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005908 return 0;
5909 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005910
Willy Tarreauf003d372012-11-26 13:35:37 +01005911 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005912 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005913 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005914 s->be->be_counters.cli_aborts++;
5915 if (objt_server(s->target))
5916 objt_server(s->target)->counters.cli_aborts++;
5917
5918 rep->analysers = 0;
5919 channel_auto_close(rep);
5920
5921 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005922 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005923 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005924
Willy Tarreaue7dff022015-04-03 01:14:29 +02005925 if (!(s->flags & SF_ERR_MASK))
5926 s->flags |= SF_ERR_CLICL;
5927 if (!(s->flags & SF_FINST_MASK))
5928 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005929
Willy Tarreau87b09662015-04-03 00:22:06 +02005930 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005931 return 0;
5932 }
5933
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005934 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005935 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005936 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005937 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005938 else if (txn->flags & TX_NOT_FIRST)
5939 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005940
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005941 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005942 if (objt_server(s->target)) {
5943 objt_server(s->target)->counters.failed_resp++;
5944 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005945 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005946
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005947 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005948 rep->analysers = 0;
5949 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005950 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005951 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005952 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005953
Willy Tarreaue7dff022015-04-03 01:14:29 +02005954 if (!(s->flags & SF_ERR_MASK))
5955 s->flags |= SF_ERR_SRVCL;
5956 if (!(s->flags & SF_FINST_MASK))
5957 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005958 return 0;
5959 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005960
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005961 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005962 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005963 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005964 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005965 else if (txn->flags & TX_NOT_FIRST)
5966 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005967
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005968 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005969 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005970 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005971
Willy Tarreaue7dff022015-04-03 01:14:29 +02005972 if (!(s->flags & SF_ERR_MASK))
5973 s->flags |= SF_ERR_CLICL;
5974 if (!(s->flags & SF_FINST_MASK))
5975 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976
Willy Tarreau87b09662015-04-03 00:22:06 +02005977 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005978 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005979 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005980
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005981 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005982 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005983 return 0;
5984 }
5985
5986 /* More interesting part now : we know that we have a complete
5987 * response which at least looks like HTTP. We have an indicator
5988 * of each header's length, so we can parse them quickly.
5989 */
5990
5991 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005992 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005993
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005994 /*
5995 * 1: get the status code
5996 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005997 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005998 if (n < 1 || n > 5)
5999 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006000 /* when the client triggers a 4xx from the server, it's most often due
6001 * to a missing object or permission. These events should be tracked
6002 * because if they happen often, it may indicate a brute force or a
6003 * vulnerability scan.
6004 */
6005 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006006 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006007
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006008 if (objt_server(s->target))
6009 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006010
Willy Tarreau5b154472009-12-21 20:11:07 +01006011 /* check if the response is HTTP/1.1 or above */
6012 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006013 ((rep->buf->p[5] > '1') ||
6014 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006015 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006016
6017 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006018 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 +01006019
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006020 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006021 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006022
Willy Tarreau9b28e032012-10-12 23:49:43 +02006023 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024
Willy Tarreau39650402010-03-15 19:44:39 +01006025 /* Adjust server's health based on status code. Note: status codes 501
6026 * and 505 are triggered on demand by client request, so we must not
6027 * count them as server failures.
6028 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006029 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006030 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006031 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006032 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006033 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006034 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006035
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006036 /*
6037 * 2: check for cacheability.
6038 */
6039
6040 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006041 case 100:
6042 /*
6043 * We may be facing a 100-continue response, in which case this
6044 * is not the right response, and we're waiting for the next one.
6045 * Let's allow this response to go to the client and wait for the
6046 * next one.
6047 */
6048 hdr_idx_init(&txn->hdr_idx);
6049 msg->next -= channel_forward(rep, msg->next);
6050 msg->msg_state = HTTP_MSG_RPBEFORE;
6051 txn->status = 0;
6052 s->logs.t_data = -1; /* was not a response yet */
6053 goto next_one;
6054
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055 case 200:
6056 case 203:
6057 case 206:
6058 case 300:
6059 case 301:
6060 case 410:
6061 /* RFC2616 @13.4:
6062 * "A response received with a status code of
6063 * 200, 203, 206, 300, 301 or 410 MAY be stored
6064 * by a cache (...) unless a cache-control
6065 * directive prohibits caching."
6066 *
6067 * RFC2616 @9.5: POST method :
6068 * "Responses to this method are not cacheable,
6069 * unless the response includes appropriate
6070 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006071 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006072 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006073 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006074 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6075 break;
6076 default:
6077 break;
6078 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006079
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006080 /*
6081 * 3: we may need to capture headers
6082 */
6083 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006084 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006085 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006086 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006087
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006088 /* 4: determine the transfer-length.
6089 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6090 * the presence of a message-body in a RESPONSE and its transfer length
6091 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006092 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006093 * All responses to the HEAD request method MUST NOT include a
6094 * message-body, even though the presence of entity-header fields
6095 * might lead one to believe they do. All 1xx (informational), 204
6096 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6097 * message-body. All other responses do include a message-body,
6098 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006099 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006100 * 1. Any response which "MUST NOT" include a message-body (such as the
6101 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6102 * always terminated by the first empty line after the header fields,
6103 * regardless of the entity-header fields present in the message.
6104 *
6105 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6106 * the "chunked" transfer-coding (Section 6.2) is used, the
6107 * transfer-length is defined by the use of this transfer-coding.
6108 * If a Transfer-Encoding header field is present and the "chunked"
6109 * transfer-coding is not present, the transfer-length is defined by
6110 * the sender closing the connection.
6111 *
6112 * 3. If a Content-Length header field is present, its decimal value in
6113 * OCTETs represents both the entity-length and the transfer-length.
6114 * If a message is received with both a Transfer-Encoding header
6115 * field and a Content-Length header field, the latter MUST be ignored.
6116 *
6117 * 4. If the message uses the media type "multipart/byteranges", and
6118 * the transfer-length is not otherwise specified, then this self-
6119 * delimiting media type defines the transfer-length. This media
6120 * type MUST NOT be used unless the sender knows that the recipient
6121 * can parse it; the presence in a request of a Range header with
6122 * multiple byte-range specifiers from a 1.1 client implies that the
6123 * client can parse multipart/byteranges responses.
6124 *
6125 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006126 */
6127
6128 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006129 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006130 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006131 * FIXME: should we parse anyway and return an error on chunked encoding ?
6132 */
6133 if (txn->meth == HTTP_METH_HEAD ||
6134 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006135 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006136 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006137 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006138 goto skip_content_length;
6139 }
6140
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006141 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006142 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006143 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006144 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006145 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006146 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6147 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006148 /* bad transfer-encoding (chunked followed by something else) */
6149 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006150 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006151 break;
6152 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006153 }
6154
6155 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6156 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006157 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006158 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006159 signed long long cl;
6160
Willy Tarreauad14f752011-09-02 20:33:27 +02006161 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006162 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006163 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006164 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006165
Willy Tarreauad14f752011-09-02 20:33:27 +02006166 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006167 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006168 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006169 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006170
Willy Tarreauad14f752011-09-02 20:33:27 +02006171 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006172 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006173 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006174 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006175
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006176 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006177 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006178 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006179 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006180
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006181 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006182 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006183 }
6184
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006185 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006186 select_compression_response_header(s, rep->buf);
6187
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006188skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006189 /* Now we have to check if we need to modify the Connection header.
6190 * This is more difficult on the response than it is on the request,
6191 * because we can have two different HTTP versions and we don't know
6192 * how the client will interprete a response. For instance, let's say
6193 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6194 * HTTP/1.1 response without any header. Maybe it will bound itself to
6195 * HTTP/1.0 because it only knows about it, and will consider the lack
6196 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6197 * the lack of header as a keep-alive. Thus we will use two flags
6198 * indicating how a request MAY be understood by the client. In case
6199 * of multiple possibilities, we'll fix the header to be explicit. If
6200 * ambiguous cases such as both close and keepalive are seen, then we
6201 * will fall back to explicit close. Note that we won't take risks with
6202 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006203 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006204 */
6205
Willy Tarreaudc008c52010-02-01 16:20:08 +01006206 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6207 txn->status == 101)) {
6208 /* Either we've established an explicit tunnel, or we're
6209 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006210 * to understand the next protocols. We have to switch to tunnel
6211 * mode, so that we transfer the request and responses then let
6212 * this protocol pass unmodified. When we later implement specific
6213 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006214 * header which contains information about that protocol for
6215 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006216 */
6217 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6218 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006219 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6220 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006221 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006222 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006223 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006224
Willy Tarreau70dffda2014-01-30 03:07:23 +01006225 /* this situation happens when combining pretend-keepalive with httpclose. */
6226 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006227 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006228 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006229 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6230
Willy Tarreau60466522010-01-18 19:08:45 +01006231 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006232 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006233 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6234 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006235
Willy Tarreau60466522010-01-18 19:08:45 +01006236 /* now adjust header transformations depending on current state */
6237 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6238 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6239 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006240 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006241 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006242 }
Willy Tarreau60466522010-01-18 19:08:45 +01006243 else { /* SCL / KAL */
6244 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006245 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006246 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006247 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006248
Willy Tarreau60466522010-01-18 19:08:45 +01006249 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006250 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006251
Willy Tarreau60466522010-01-18 19:08:45 +01006252 /* Some keep-alive responses are converted to Server-close if
6253 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006254 */
Willy Tarreau60466522010-01-18 19:08:45 +01006255 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6256 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006257 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006258 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006259 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006260 }
6261
Willy Tarreau7959a552013-09-23 16:44:27 +02006262 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006263 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006264
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006265 /* end of job, return OK */
6266 rep->analysers &= ~an_bit;
6267 rep->analyse_exp = TICK_ETERNITY;
6268 channel_auto_close(rep);
6269 return 1;
6270
6271 abort_keep_alive:
6272 /* A keep-alive request to the server failed on a network error.
6273 * The client is required to retry. We need to close without returning
6274 * any other information so that the client retries.
6275 */
6276 txn->status = 0;
6277 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006278 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006279 channel_auto_close(rep);
6280 s->logs.logwait = 0;
6281 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006282 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006283 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006284 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006285 return 0;
6286}
6287
6288/* This function performs all the processing enabled for the current response.
6289 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006290 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006291 * other functions. It works like process_request (see indications above).
6292 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006293int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006294{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006295 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006296 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006297 struct http_msg *msg = &txn->rsp;
6298 struct proxy *cur_proxy;
6299 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006300 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006301
Willy Tarreau87b09662015-04-03 00:22:06 +02006302 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 +02006303 now_ms, __FUNCTION__,
6304 s,
6305 rep,
6306 rep->rex, rep->wex,
6307 rep->flags,
6308 rep->buf->i,
6309 rep->analysers);
6310
6311 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6312 return 0;
6313
Willy Tarreau70730dd2014-04-24 18:06:27 +02006314 /* The stats applet needs to adjust the Connection header but we don't
6315 * apply any filter there.
6316 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006317 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6318 rep->analysers &= ~an_bit;
6319 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006320 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006321 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006322
Willy Tarreau58975672014-04-24 21:13:57 +02006323 /*
6324 * We will have to evaluate the filters.
6325 * As opposed to version 1.2, now they will be evaluated in the
6326 * filters order and not in the header order. This means that
6327 * each filter has to be validated among all headers.
6328 *
6329 * Filters are tried with ->be first, then with ->fe if it is
6330 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006331 *
6332 * Maybe we are in resume condiion. In this case I choose the
6333 * "struct proxy" which contains the rule list matching the resume
6334 * pointer. If none of theses "struct proxy" match, I initialise
6335 * the process with the first one.
6336 *
6337 * In fact, I check only correspondance betwwen the current list
6338 * pointer and the ->fe rule list. If it doesn't match, I initialize
6339 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006340 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006341 if (s->current_rule_list == &sess->fe->http_res_rules)
6342 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006343 else
6344 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006345 while (1) {
6346 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006347
Willy Tarreau58975672014-04-24 21:13:57 +02006348 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006349 if (ret == HTTP_RULE_RES_CONT)
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006350 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006351
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006352 /* we need to be called again. */
6353 if (ret == HTTP_RULE_RES_YIELD) {
6354 channel_dont_close(rep);
6355 return 0;
6356 }
6357
Willy Tarreau58975672014-04-24 21:13:57 +02006358 /* try headers filters */
6359 if (rule_set->rsp_exp != NULL) {
6360 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6361 return_bad_resp:
6362 if (objt_server(s->target)) {
6363 objt_server(s->target)->counters.failed_resp++;
6364 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006365 }
Willy Tarreau58975672014-04-24 21:13:57 +02006366 s->be->be_counters.failed_resp++;
6367 return_srv_prx_502:
6368 rep->analysers = 0;
6369 txn->status = 502;
6370 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006371 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006372 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006373 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006374 if (!(s->flags & SF_ERR_MASK))
6375 s->flags |= SF_ERR_PRXCOND;
6376 if (!(s->flags & SF_FINST_MASK))
6377 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006378 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006379 }
Willy Tarreau58975672014-04-24 21:13:57 +02006380 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006381
Willy Tarreau58975672014-04-24 21:13:57 +02006382 /* has the response been denied ? */
6383 if (txn->flags & TX_SVDENY) {
6384 if (objt_server(s->target))
6385 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006386
Willy Tarreau58975672014-04-24 21:13:57 +02006387 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006388 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006389 if (sess->listener->counters)
6390 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006391
Willy Tarreau58975672014-04-24 21:13:57 +02006392 goto return_srv_prx_502;
6393 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006394
Willy Tarreau58975672014-04-24 21:13:57 +02006395 /* add response headers from the rule sets in the same order */
6396 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006397 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006398 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006399 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006400 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006401 ret = acl_pass(ret);
6402 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6403 ret = !ret;
6404 if (!ret)
6405 continue;
6406 }
6407 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6408 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006409 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006410
Willy Tarreau58975672014-04-24 21:13:57 +02006411 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006412 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006413 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006414 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006415 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006416
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006417 /* After this point, this anayzer can't return yield, so we can
6418 * remove the bit corresponding to this analyzer from the list.
6419 *
6420 * Note that the intermediate returns and goto found previously
6421 * reset the analyzers.
6422 */
6423 rep->analysers &= ~an_bit;
6424 rep->analyse_exp = TICK_ETERNITY;
6425
Willy Tarreau58975672014-04-24 21:13:57 +02006426 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006427 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006428 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006429
Willy Tarreau58975672014-04-24 21:13:57 +02006430 /*
6431 * Now check for a server cookie.
6432 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006433 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006434 (s->be->options & PR_O_CHK_CACHE))
6435 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006436
Willy Tarreau58975672014-04-24 21:13:57 +02006437 /*
6438 * Check for cache-control or pragma headers if required.
6439 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006440 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 +02006441 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006442
Willy Tarreau58975672014-04-24 21:13:57 +02006443 /*
6444 * Add server cookie in the response if needed
6445 */
6446 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6447 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006448 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006449 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6450 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6451 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6452 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6453 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006454 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006455 /* the server is known, it's not the one the client requested, or the
6456 * cookie's last seen date needs to be refreshed. We have to
6457 * insert a set-cookie here, except if we want to insert only on POST
6458 * requests and this one isn't. Note that servers which don't have cookies
6459 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006460 */
Willy Tarreau58975672014-04-24 21:13:57 +02006461 if (!objt_server(s->target)->cookie) {
6462 chunk_printf(&trash,
6463 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6464 s->be->cookie_name);
6465 }
6466 else {
6467 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006468
Willy Tarreau58975672014-04-24 21:13:57 +02006469 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6470 /* emit last_date, which is mandatory */
6471 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6472 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6473 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006474
Willy Tarreau58975672014-04-24 21:13:57 +02006475 if (s->be->cookie_maxlife) {
6476 /* emit first_date, which is either the original one or
6477 * the current date.
6478 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006479 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006480 s30tob64(txn->cookie_first_date ?
6481 txn->cookie_first_date >> 2 :
6482 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006483 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006484 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006485 }
Willy Tarreau58975672014-04-24 21:13:57 +02006486 chunk_appendf(&trash, "; path=/");
6487 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006488
Willy Tarreau58975672014-04-24 21:13:57 +02006489 if (s->be->cookie_domain)
6490 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006491
Willy Tarreau58975672014-04-24 21:13:57 +02006492 if (s->be->ck_opts & PR_CK_HTTPONLY)
6493 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006494
Willy Tarreau58975672014-04-24 21:13:57 +02006495 if (s->be->ck_opts & PR_CK_SECURE)
6496 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006497
Willy Tarreau58975672014-04-24 21:13:57 +02006498 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6499 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006500
Willy Tarreau58975672014-04-24 21:13:57 +02006501 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006502 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006503 /* the server did not change, only the date was updated */
6504 txn->flags |= TX_SCK_UPDATED;
6505 else
6506 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006507
Willy Tarreau58975672014-04-24 21:13:57 +02006508 /* Here, we will tell an eventual cache on the client side that we don't
6509 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6510 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6511 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006512 */
Willy Tarreau58975672014-04-24 21:13:57 +02006513 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006514
Willy Tarreau58975672014-04-24 21:13:57 +02006515 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006516
Willy Tarreau58975672014-04-24 21:13:57 +02006517 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6518 "Cache-control: private", 22) < 0))
6519 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006520 }
Willy Tarreau58975672014-04-24 21:13:57 +02006521 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006522
Willy Tarreau58975672014-04-24 21:13:57 +02006523 /*
6524 * Check if result will be cacheable with a cookie.
6525 * We'll block the response if security checks have caught
6526 * nasty things such as a cacheable cookie.
6527 */
6528 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6529 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6530 (s->be->options & PR_O_CHK_CACHE)) {
6531 /* we're in presence of a cacheable response containing
6532 * a set-cookie header. We'll block it as requested by
6533 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006534 */
Willy Tarreau58975672014-04-24 21:13:57 +02006535 if (objt_server(s->target))
6536 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006537
Willy Tarreau58975672014-04-24 21:13:57 +02006538 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006539 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006540 if (sess->listener->counters)
6541 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006542
Willy Tarreau58975672014-04-24 21:13:57 +02006543 Alert("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 send_log(s->be, LOG_ALERT,
6546 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6547 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6548 goto return_srv_prx_502;
6549 }
Willy Tarreau03945942009-12-22 16:50:27 +01006550
Willy Tarreau70730dd2014-04-24 18:06:27 +02006551 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006552 /*
6553 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6554 * If an "Upgrade" token is found, the header is left untouched in order
6555 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006556 * if anything but "Upgrade" is present in the Connection header. We don't
6557 * want to touch any 101 response either since it's switching to another
6558 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006559 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006560 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006561 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006562 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006563 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6564 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006565
Willy Tarreau58975672014-04-24 21:13:57 +02006566 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6567 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6568 /* we want a keep-alive response here. Keep-alive header
6569 * required if either side is not 1.1.
6570 */
6571 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6572 want_flags |= TX_CON_KAL_SET;
6573 }
6574 else {
6575 /* we want a close response here. Close header required if
6576 * the server is 1.1, regardless of the client.
6577 */
6578 if (msg->flags & HTTP_MSGF_VER_11)
6579 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006580 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006581
Willy Tarreau58975672014-04-24 21:13:57 +02006582 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6583 http_change_connection_header(txn, msg, want_flags);
6584 }
6585
6586 skip_header_mangling:
6587 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6588 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6589 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006590
Willy Tarreau58975672014-04-24 21:13:57 +02006591 /* if the user wants to log as soon as possible, without counting
6592 * bytes from the server, then this is the right moment. We have
6593 * to temporarily assign bytes_out to log what we currently have.
6594 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006595 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006596 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6597 s->logs.bytes_out = txn->rsp.eoh;
6598 s->do_log(s);
6599 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006600 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006601 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006602}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603
Willy Tarreaud98cf932009-12-27 22:54:55 +01006604/* This function is an analyser which forwards response body (including chunk
6605 * sizes if any). It is called as soon as we must forward, even if we forward
6606 * zero byte. The only situation where it must not be called is when we're in
6607 * tunnel mode and we want to forward till the close. It's used both to forward
6608 * remaining data and to resync after end of body. It expects the msg_state to
6609 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006610 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006611 *
6612 * It is capable of compressing response data both in content-length mode and
6613 * in chunked mode. The state machines follows different flows depending on
6614 * whether content-length and chunked modes are used, since there are no
6615 * trailers in content-length :
6616 *
6617 * chk-mode cl-mode
6618 * ,----- BODY -----.
6619 * / \
6620 * V size > 0 V chk-mode
6621 * .--> SIZE -------------> DATA -------------> CRLF
6622 * | | size == 0 | last byte |
6623 * | v final crlf v inspected |
6624 * | TRAILERS -----------> DONE |
6625 * | |
6626 * `----------------------------------------------'
6627 *
6628 * Compression only happens in the DATA state, and must be flushed in final
6629 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6630 * is performed at once on final states for all bytes parsed, or when leaving
6631 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006632 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006633int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006634{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006635 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006636 struct http_txn *txn = s->txn;
6637 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006638 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006639 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006640 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006641
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006642 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6643 return 0;
6644
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006645 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006646 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006647 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006648 /* Output closed while we were sending data. We must abort and
6649 * wake the other side up.
6650 */
6651 msg->msg_state = HTTP_MSG_ERROR;
6652 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006653 return 1;
6654 }
6655
Willy Tarreau4fe41902010-06-07 22:27:41 +02006656 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006657 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006658
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006659 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006660 /* we have msg->sov which points to the first byte of message
6661 * body, and res->buf.p still points to the beginning of the
6662 * message. We forward the headers now, as we don't need them
6663 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006664 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006665 b_adv(res->buf, msg->sov);
6666 msg->next -= msg->sov;
6667 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006668
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006669 /* The previous analysers guarantee that the state is somewhere
6670 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6671 * msg->next are always correct.
6672 */
6673 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6674 if (msg->flags & HTTP_MSGF_TE_CHNK)
6675 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6676 else
6677 msg->msg_state = HTTP_MSG_DATA;
6678 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006679 }
6680
Willy Tarreauefdf0942014-04-24 20:08:57 +02006681 if (res->to_forward) {
6682 /* We can't process the buffer's contents yet */
6683 res->flags |= CF_WAKE_WRITE;
6684 goto missing_data;
6685 }
6686
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006687 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6688 /* We need a compression buffer in the DATA state to put the
6689 * output of compressed data, and in CRLF state to let the
6690 * TRAILERS state finish the job of removing the trailing CRLF.
6691 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006692 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006693 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006694 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006695 goto aborted_xfer; /* no memory */
6696 }
6697
6698 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006699 if (ret < 0) {
6700 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006701 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006702 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006703 compressing = 1;
6704 }
6705
Willy Tarreaud98cf932009-12-27 22:54:55 +01006706 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006707 switch (msg->msg_state - HTTP_MSG_DATA) {
6708 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006709 /* we may have some pending data starting at res->buf->p */
6710 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006711 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006712 if (ret < 0)
6713 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006714
Willy Tarreaud5a67832014-04-21 10:54:27 +02006715 if (msg->chunk_len) {
6716 /* input empty or output full */
6717 if (res->buf->i > msg->next)
6718 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006719 goto missing_data;
6720 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006721 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006722 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006723 if (msg->chunk_len > res->buf->i - msg->next) {
6724 /* output full */
6725 res->flags |= CF_WAKE_WRITE;
6726 goto missing_data;
6727 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006728 msg->next += msg->chunk_len;
6729 msg->chunk_len = 0;
6730 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006731
6732 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006733 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006734 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006735 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006736 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006737 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006738 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006739 /* fall through for HTTP_MSG_CHUNK_CRLF */
6740
6741 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6742 /* we want the CRLF after the data */
6743
6744 ret = http_skip_chunk_crlf(msg);
6745 if (ret == 0)
6746 goto missing_data;
6747 else if (ret < 0) {
6748 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006749 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006750 goto return_bad_res;
6751 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006752 /* we're in MSG_CHUNK_SIZE now, fall through */
6753
6754 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006755 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006756 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006757 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006758 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006759
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006760 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006761 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006762 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006763 else if (ret < 0) {
6764 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006765 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006766 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006767 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006768 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006769 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006770
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006771 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006772 if (unlikely(compressing)) {
6773 /* we need to flush output contents before syncing FSMs */
6774 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6775 compressing = 0;
6776 }
6777
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006778 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006779 if (ret == 0)
6780 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006781 else if (ret < 0) {
6782 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006783 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006784 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006785 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006786 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006787
6788 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006789 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006790 if (unlikely(compressing)) {
6791 /* we need to flush output contents before syncing FSMs */
6792 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6793 compressing = 0;
6794 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006795
Willy Tarreauc623c172014-04-18 09:53:50 +02006796 /* we may have some pending data starting at res->buf->p
6797 * such as a last chunk of data or trailers.
6798 */
6799 b_adv(res->buf, msg->next);
6800 msg->next = 0;
6801
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006802 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006803 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006804 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6805 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006806 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006807
Willy Tarreau610ecce2010-01-04 21:15:02 +01006808 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006809 /* some state changes occurred, maybe the analyser
6810 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006811 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006812 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006813 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006814 /* response errors are most likely due to
6815 * the client aborting the transfer.
6816 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006817 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006818 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006819 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006820 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006821 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006822 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006823 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006824 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006825 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006826 }
6827 }
6828
Willy Tarreaud98cf932009-12-27 22:54:55 +01006829 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006830 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006831 if (unlikely(compressing)) {
6832 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006833 compressing = 0;
6834 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006835
Willy Tarreauc623c172014-04-18 09:53:50 +02006836 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6837 b_adv(res->buf, msg->next);
6838 msg->next = 0;
6839 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6840 }
6841
Willy Tarreauf003d372012-11-26 13:35:37 +01006842 if (res->flags & CF_SHUTW)
6843 goto aborted_xfer;
6844
6845 /* stop waiting for data if the input is closed before the end. If the
6846 * client side was already closed, it means that the client has aborted,
6847 * so we don't want to count this as a server abort. Otherwise it's a
6848 * server abort.
6849 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006850 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006851 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006852 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006853 if (!(s->flags & SF_ERR_MASK))
6854 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006855 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006856 if (objt_server(s->target))
6857 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006858 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006859 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006860
Willy Tarreau40dba092010-03-04 18:14:51 +01006861 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006862 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006863 goto return_bad_res;
6864
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006865 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006866 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006867 * Similarly, with keep-alive on the client side, we don't want to forward a
6868 * close.
6869 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006870 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006871 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6872 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006873 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006874
Willy Tarreau5c620922011-05-11 19:56:11 +02006875 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006876 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006877 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006878 * modes are already handled by the stream sock layer. We must not do
6879 * this in content-length mode because it could present the MSG_MORE
6880 * flag with the last block of forwarded data, which would cause an
6881 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006882 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006883 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006884 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006885
Willy Tarreau87b09662015-04-03 00:22:06 +02006886 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006887 return 0;
6888
Willy Tarreau40dba092010-03-04 18:14:51 +01006889 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006890 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006891 if (objt_server(s->target))
6892 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006893
6894 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006895 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006896 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006897 compressing = 0;
6898 }
6899
Willy Tarreauc623c172014-04-18 09:53:50 +02006900 /* we may have some pending data starting at res->buf->p */
6901 if (s->comp_algo == NULL) {
6902 b_adv(res->buf, msg->next);
6903 msg->next = 0;
6904 }
6905
Willy Tarreaud98cf932009-12-27 22:54:55 +01006906 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006907 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006908 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006909 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006910 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006911 if (objt_server(s->target))
6912 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006913
Willy Tarreaue7dff022015-04-03 01:14:29 +02006914 if (!(s->flags & SF_ERR_MASK))
6915 s->flags |= SF_ERR_PRXCOND;
6916 if (!(s->flags & SF_FINST_MASK))
6917 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006918 return 0;
6919
6920 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006921 if (unlikely(compressing)) {
6922 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6923 compressing = 0;
6924 }
6925
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006926 txn->rsp.msg_state = HTTP_MSG_ERROR;
6927 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006928 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006929 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006930 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006931
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006932 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006933 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006934 if (objt_server(s->target))
6935 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006936
Willy Tarreaue7dff022015-04-03 01:14:29 +02006937 if (!(s->flags & SF_ERR_MASK))
6938 s->flags |= SF_ERR_CLICL;
6939 if (!(s->flags & SF_FINST_MASK))
6940 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006941 return 0;
6942}
6943
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006944/* Iterate the same filter through all request headers.
6945 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006946 * Since it can manage the switch to another backend, it updates the per-proxy
6947 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006948 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006949int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006950{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006951 char *cur_ptr, *cur_end, *cur_next;
6952 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006953 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006954 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006955 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006956
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006957 last_hdr = 0;
6958
Willy Tarreau9b28e032012-10-12 23:49:43 +02006959 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006960 old_idx = 0;
6961
6962 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006963 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006964 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006965 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966 (exp->action == ACT_ALLOW ||
6967 exp->action == ACT_DENY ||
6968 exp->action == ACT_TARPIT))
6969 return 0;
6970
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006971 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006972 if (!cur_idx)
6973 break;
6974
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006975 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006976 cur_ptr = cur_next;
6977 cur_end = cur_ptr + cur_hdr->len;
6978 cur_next = cur_end + cur_hdr->cr + 1;
6979
6980 /* Now we have one header between cur_ptr and cur_end,
6981 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006982 */
6983
Willy Tarreau15a53a42015-01-21 13:39:42 +01006984 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006985 switch (exp->action) {
6986 case ACT_SETBE:
6987 /* It is not possible to jump a second time.
6988 * FIXME: should we return an HTTP/500 here so that
6989 * the admin knows there's a problem ?
6990 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006991 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006992 break;
6993
6994 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02006995 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006996 last_hdr = 1;
6997 break;
6998
6999 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007000 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007001 last_hdr = 1;
7002 break;
7003
7004 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007005 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007006 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007007 break;
7008
7009 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007010 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007011 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007012 break;
7013
7014 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007015 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7016 if (trash.len < 0)
7017 return -1;
7018
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007019 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007020 /* FIXME: if the user adds a newline in the replacement, the
7021 * index will not be recalculated for now, and the new line
7022 * will not be counted as a new header.
7023 */
7024
7025 cur_end += delta;
7026 cur_next += delta;
7027 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007028 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029 break;
7030
7031 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007032 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007033 cur_next += delta;
7034
Willy Tarreaufa355d42009-11-29 18:12:29 +01007035 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007036 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7037 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007038 cur_hdr->len = 0;
7039 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007040 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007041 break;
7042
7043 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007044 }
7045
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007046 /* keep the link from this header to next one in case of later
7047 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007049 old_idx = cur_idx;
7050 }
7051 return 0;
7052}
7053
7054
7055/* Apply the filter to the request line.
7056 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7057 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007058 * Since it can manage the switch to another backend, it updates the per-proxy
7059 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007060 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007061int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007062{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007063 char *cur_ptr, *cur_end;
7064 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007065 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007066 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067
Willy Tarreau3d300592007-03-18 18:34:41 +01007068 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007069 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007070 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007071 (exp->action == ACT_ALLOW ||
7072 exp->action == ACT_DENY ||
7073 exp->action == ACT_TARPIT))
7074 return 0;
7075 else if (exp->action == ACT_REMOVE)
7076 return 0;
7077
7078 done = 0;
7079
Willy Tarreau9b28e032012-10-12 23:49:43 +02007080 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007081 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007082
7083 /* Now we have the request line between cur_ptr and cur_end */
7084
Willy Tarreau15a53a42015-01-21 13:39:42 +01007085 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007086 switch (exp->action) {
7087 case ACT_SETBE:
7088 /* It is not possible to jump a second time.
7089 * FIXME: should we return an HTTP/500 here so that
7090 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091 */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007092 if (s->be != strm_fe(s))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007093 break;
7094
7095 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007096 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007097 done = 1;
7098 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007100 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007101 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 done = 1;
7103 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007105 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007106 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007107 done = 1;
7108 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007110 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007111 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007112 done = 1;
7113 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007115 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007116 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7117 if (trash.len < 0)
7118 return -1;
7119
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007120 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007121 /* FIXME: if the user adds a newline in the replacement, the
7122 * index will not be recalculated for now, and the new line
7123 * will not be counted as a new header.
7124 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007125
Willy Tarreaufa355d42009-11-29 18:12:29 +01007126 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007127 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007128 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007129 HTTP_MSG_RQMETH,
7130 cur_ptr, cur_end + 1,
7131 NULL, NULL);
7132 if (unlikely(!cur_end))
7133 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007134
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007135 /* we have a full request and we know that we have either a CR
7136 * or an LF at <ptr>.
7137 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007138 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7139 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007140 /* there is no point trying this regex on headers */
7141 return 1;
7142 }
7143 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007144 return done;
7145}
Willy Tarreau97de6242006-12-27 17:18:38 +01007146
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147
Willy Tarreau58f10d72006-12-04 02:26:12 +01007148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007149/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007150 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007151 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007152 * unparsable request. Since it can manage the switch to another backend, it
7153 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007154 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007155int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007156{
Willy Tarreau192252e2015-04-04 01:47:55 +02007157 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007158 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007159 struct hdr_exp *exp;
7160
7161 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007162 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007163
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007164 /*
7165 * The interleaving of transformations and verdicts
7166 * makes it difficult to decide to continue or stop
7167 * the evaluation.
7168 */
7169
Willy Tarreau6c123b12010-01-28 20:22:06 +01007170 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7171 break;
7172
Willy Tarreau3d300592007-03-18 18:34:41 +01007173 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007174 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007175 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007176 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007177
7178 /* if this filter had a condition, evaluate it now and skip to
7179 * next filter if the condition does not match.
7180 */
7181 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007182 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007183 ret = acl_pass(ret);
7184 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7185 ret = !ret;
7186
7187 if (!ret)
7188 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007189 }
7190
7191 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007192 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007193 if (unlikely(ret < 0))
7194 return -1;
7195
7196 if (likely(ret == 0)) {
7197 /* The filter did not match the request, it can be
7198 * iterated through all headers.
7199 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007200 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7201 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007202 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007203 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007204 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007205}
7206
7207
Willy Tarreaua15645d2007-03-18 16:22:39 +01007208
Willy Tarreau58f10d72006-12-04 02:26:12 +01007209/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007210 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007211 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007212 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007213void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02007214 struct http_txn *txn = s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007215 appsess *asession = NULL;
7216 char *sessid_temp = NULL;
7217
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007218 if (len > s->be->appsession_len) {
7219 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007220 }
7221
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007222 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007223 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007224 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007225 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007226 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007227 }
7228
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007229 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007230 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007231 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007232 return;
7233 }
7234
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007235 memcpy(txn->sessid, buf, len);
7236 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007237 }
7238
7239 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7240 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007241 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007242 return;
7243 }
7244
Cyril Bontéb21570a2009-11-29 20:04:48 +01007245 memcpy(sessid_temp, buf, len);
7246 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007247
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007248 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007249 /* free previously allocated memory */
7250 pool_free2(apools.sessid, sessid_temp);
7251
7252 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007253 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7254 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007255 asession->request_count++;
7256
7257 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007258 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007259
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007260 while (srv) {
7261 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007262 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007263 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007264 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007265 /* we found the server and it's usable */
7266 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007267 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007268 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007269 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007270
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007271 break;
7272 } else {
7273 txn->flags &= ~TX_CK_MASK;
7274 txn->flags |= TX_CK_DOWN;
7275 }
7276 }
7277 srv = srv->next;
7278 }
7279 }
7280 }
7281}
7282
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007283/* Find the end of a cookie value contained between <s> and <e>. It works the
7284 * same way as with headers above except that the semi-colon also ends a token.
7285 * See RFC2965 for more information. Note that it requires a valid header to
7286 * return a valid result.
7287 */
7288char *find_cookie_value_end(char *s, const char *e)
7289{
7290 int quoted, qdpair;
7291
7292 quoted = qdpair = 0;
7293 for (; s < e; s++) {
7294 if (qdpair) qdpair = 0;
7295 else if (quoted) {
7296 if (*s == '\\') qdpair = 1;
7297 else if (*s == '"') quoted = 0;
7298 }
7299 else if (*s == '"') quoted = 1;
7300 else if (*s == ',' || *s == ';') return s;
7301 }
7302 return s;
7303}
7304
7305/* Delete a value in a header between delimiters <from> and <next> in buffer
7306 * <buf>. The number of characters displaced is returned, and the pointer to
7307 * the first delimiter is updated if required. The function tries as much as
7308 * possible to respect the following principles :
7309 * - replace <from> delimiter by the <next> one unless <from> points to a
7310 * colon, in which case <next> is simply removed
7311 * - set exactly one space character after the new first delimiter, unless
7312 * there are not enough characters in the block being moved to do so.
7313 * - remove unneeded spaces before the previous delimiter and after the new
7314 * one.
7315 *
7316 * It is the caller's responsibility to ensure that :
7317 * - <from> points to a valid delimiter or the colon ;
7318 * - <next> points to a valid delimiter or the final CR/LF ;
7319 * - there are non-space chars before <from> ;
7320 * - there is a CR/LF at or after <next>.
7321 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007322int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007323{
7324 char *prev = *from;
7325
7326 if (*prev == ':') {
7327 /* We're removing the first value, preserve the colon and add a
7328 * space if possible.
7329 */
7330 if (!http_is_crlf[(unsigned char)*next])
7331 next++;
7332 prev++;
7333 if (prev < next)
7334 *prev++ = ' ';
7335
7336 while (http_is_spht[(unsigned char)*next])
7337 next++;
7338 } else {
7339 /* Remove useless spaces before the old delimiter. */
7340 while (http_is_spht[(unsigned char)*(prev-1)])
7341 prev--;
7342 *from = prev;
7343
7344 /* copy the delimiter and if possible a space if we're
7345 * not at the end of the line.
7346 */
7347 if (!http_is_crlf[(unsigned char)*next]) {
7348 *prev++ = *next++;
7349 if (prev + 1 < next)
7350 *prev++ = ' ';
7351 while (http_is_spht[(unsigned char)*next])
7352 next++;
7353 }
7354 }
7355 return buffer_replace2(buf, prev, next, NULL, 0);
7356}
7357
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007358/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007359 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007360 * desirable to call it only when needed. This code is quite complex because
7361 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7362 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007363 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007364void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007365{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007366 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007367 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007368 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007369 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007370 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7371 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007372
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007373 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007374 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007375 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007376
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007377 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007378 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007379 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007380
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007381 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007382 hdr_beg = hdr_next;
7383 hdr_end = hdr_beg + cur_hdr->len;
7384 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007385
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007386 /* We have one full header between hdr_beg and hdr_end, and the
7387 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007388 * "Cookie:" headers.
7389 */
7390
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007391 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007392 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007393 old_idx = cur_idx;
7394 continue;
7395 }
7396
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007397 del_from = NULL; /* nothing to be deleted */
7398 preserve_hdr = 0; /* assume we may kill the whole header */
7399
Willy Tarreau58f10d72006-12-04 02:26:12 +01007400 /* Now look for cookies. Conforming to RFC2109, we have to support
7401 * attributes whose name begin with a '$', and associate them with
7402 * the right cookie, if we want to delete this cookie.
7403 * So there are 3 cases for each cookie read :
7404 * 1) it's a special attribute, beginning with a '$' : ignore it.
7405 * 2) it's a server id cookie that we *MAY* want to delete : save
7406 * some pointers on it (last semi-colon, beginning of cookie...)
7407 * 3) it's an application cookie : we *MAY* have to delete a previous
7408 * "special" cookie.
7409 * At the end of loop, if a "special" cookie remains, we may have to
7410 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007411 * *MUST* delete it.
7412 *
7413 * Note: RFC2965 is unclear about the processing of spaces around
7414 * the equal sign in the ATTR=VALUE form. A careful inspection of
7415 * the RFC explicitly allows spaces before it, and not within the
7416 * tokens (attrs or values). An inspection of RFC2109 allows that
7417 * too but section 10.1.3 lets one think that spaces may be allowed
7418 * after the equal sign too, resulting in some (rare) buggy
7419 * implementations trying to do that. So let's do what servers do.
7420 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7421 * allowed quoted strings in values, with any possible character
7422 * after a backslash, including control chars and delimitors, which
7423 * causes parsing to become ambiguous. Browsers also allow spaces
7424 * within values even without quotes.
7425 *
7426 * We have to keep multiple pointers in order to support cookie
7427 * removal at the beginning, middle or end of header without
7428 * corrupting the header. All of these headers are valid :
7429 *
7430 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7431 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7432 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7433 * | | | | | | | | |
7434 * | | | | | | | | hdr_end <--+
7435 * | | | | | | | +--> next
7436 * | | | | | | +----> val_end
7437 * | | | | | +-----------> val_beg
7438 * | | | | +--------------> equal
7439 * | | | +----------------> att_end
7440 * | | +---------------------> att_beg
7441 * | +--------------------------> prev
7442 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007443 */
7444
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007445 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7446 /* Iterate through all cookies on this line */
7447
7448 /* find att_beg */
7449 att_beg = prev + 1;
7450 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7451 att_beg++;
7452
7453 /* find att_end : this is the first character after the last non
7454 * space before the equal. It may be equal to hdr_end.
7455 */
7456 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007457
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007458 while (equal < hdr_end) {
7459 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007460 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007461 if (http_is_spht[(unsigned char)*equal++])
7462 continue;
7463 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007464 }
7465
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007466 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7467 * is between <att_beg> and <equal>, both may be identical.
7468 */
7469
7470 /* look for end of cookie if there is an equal sign */
7471 if (equal < hdr_end && *equal == '=') {
7472 /* look for the beginning of the value */
7473 val_beg = equal + 1;
7474 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7475 val_beg++;
7476
7477 /* find the end of the value, respecting quotes */
7478 next = find_cookie_value_end(val_beg, hdr_end);
7479
7480 /* make val_end point to the first white space or delimitor after the value */
7481 val_end = next;
7482 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7483 val_end--;
7484 } else {
7485 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007486 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007487
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007488 /* We have nothing to do with attributes beginning with '$'. However,
7489 * they will automatically be removed if a header before them is removed,
7490 * since they're supposed to be linked together.
7491 */
7492 if (*att_beg == '$')
7493 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007494
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007495 /* Ignore cookies with no equal sign */
7496 if (equal == next) {
7497 /* This is not our cookie, so we must preserve it. But if we already
7498 * scheduled another cookie for removal, we cannot remove the
7499 * complete header, but we can remove the previous block itself.
7500 */
7501 preserve_hdr = 1;
7502 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007503 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 val_end += delta;
7505 next += delta;
7506 hdr_end += delta;
7507 hdr_next += delta;
7508 cur_hdr->len += delta;
7509 http_msg_move_end(&txn->req, delta);
7510 prev = del_from;
7511 del_from = NULL;
7512 }
7513 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007514 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007516 /* if there are spaces around the equal sign, we need to
7517 * strip them otherwise we'll get trouble for cookie captures,
7518 * or even for rewrites. Since this happens extremely rarely,
7519 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007520 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007521 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7522 int stripped_before = 0;
7523 int stripped_after = 0;
7524
7525 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007526 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007527 equal += stripped_before;
7528 val_beg += stripped_before;
7529 }
7530
7531 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007532 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007533 val_beg += stripped_after;
7534 stripped_before += stripped_after;
7535 }
7536
7537 val_end += stripped_before;
7538 next += stripped_before;
7539 hdr_end += stripped_before;
7540 hdr_next += stripped_before;
7541 cur_hdr->len += stripped_before;
7542 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007543 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007544 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007545
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007546 /* First, let's see if we want to capture this cookie. We check
7547 * that we don't already have a client side cookie, because we
7548 * can only capture one. Also as an optimisation, we ignore
7549 * cookies shorter than the declared name.
7550 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007551 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7552 (val_end - att_beg >= sess->fe->capture_namelen) &&
7553 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007554 int log_len = val_end - att_beg;
7555
7556 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7557 Alert("HTTP logging : out of memory.\n");
7558 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007559 if (log_len > sess->fe->capture_len)
7560 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007561 memcpy(txn->cli_cookie, att_beg, log_len);
7562 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007563 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007564 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007565
Willy Tarreaubca99692010-10-06 19:25:55 +02007566 /* Persistence cookies in passive, rewrite or insert mode have the
7567 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007569 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007571 * For cookies in prefix mode, the form is :
7572 *
7573 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007574 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007575 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7576 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7577 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007578 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7581 * have the server ID between val_beg and delim, and the original cookie between
7582 * delim+1 and val_end. Otherwise, delim==val_end :
7583 *
7584 * Cookie: NAME=SRV; # in all but prefix modes
7585 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7586 * | || || | |+-> next
7587 * | || || | +--> val_end
7588 * | || || +---------> delim
7589 * | || |+------------> val_beg
7590 * | || +-------------> att_end = equal
7591 * | |+-----------------> att_beg
7592 * | +------------------> prev
7593 * +-------------------------> hdr_beg
7594 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007595
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007596 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007597 for (delim = val_beg; delim < val_end; delim++)
7598 if (*delim == COOKIE_DELIM)
7599 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007600 } else {
7601 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007602 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007603 /* Now check if the cookie contains a date field, which would
7604 * appear after a vertical bar ('|') just after the server name
7605 * and before the delimiter.
7606 */
7607 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7608 if (vbar1) {
7609 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007610 * right is the last seen date. It is a base64 encoded
7611 * 30-bit value representing the UNIX date since the
7612 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007613 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007614 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007615 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007616 if (val_end - vbar1 >= 5) {
7617 val = b64tos30(vbar1);
7618 if (val > 0)
7619 txn->cookie_last_date = val << 2;
7620 }
7621 /* look for a second vertical bar */
7622 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7623 if (vbar1 && (val_end - vbar1 > 5)) {
7624 val = b64tos30(vbar1 + 1);
7625 if (val > 0)
7626 txn->cookie_first_date = val << 2;
7627 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007628 }
7629 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007630
Willy Tarreauf64d1412010-10-07 20:06:11 +02007631 /* if the cookie has an expiration date and the proxy wants to check
7632 * it, then we do that now. We first check if the cookie is too old,
7633 * then only if it has expired. We detect strict overflow because the
7634 * time resolution here is not great (4 seconds). Cookies with dates
7635 * in the future are ignored if their offset is beyond one day. This
7636 * allows an admin to fix timezone issues without expiring everyone
7637 * and at the same time avoids keeping unwanted side effects for too
7638 * long.
7639 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007640 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7641 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007642 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007643 txn->flags &= ~TX_CK_MASK;
7644 txn->flags |= TX_CK_OLD;
7645 delim = val_beg; // let's pretend we have not found the cookie
7646 txn->cookie_first_date = 0;
7647 txn->cookie_last_date = 0;
7648 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007649 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7650 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007651 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007652 txn->flags &= ~TX_CK_MASK;
7653 txn->flags |= TX_CK_EXPIRED;
7654 delim = val_beg; // let's pretend we have not found the cookie
7655 txn->cookie_first_date = 0;
7656 txn->cookie_last_date = 0;
7657 }
7658
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007659 /* Here, we'll look for the first running server which supports the cookie.
7660 * This allows to share a same cookie between several servers, for example
7661 * to dedicate backup servers to specific servers only.
7662 * However, to prevent clients from sticking to cookie-less backup server
7663 * when they have incidentely learned an empty cookie, we simply ignore
7664 * empty cookies and mark them as invalid.
7665 * The same behaviour is applied when persistence must be ignored.
7666 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007667 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007668 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007669
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007670 while (srv) {
7671 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7672 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007673 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007674 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007675 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 /* we found the server and we can use it */
7677 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007678 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007679 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007680 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007681 break;
7682 } else {
7683 /* we found a server, but it's down,
7684 * mark it as such and go on in case
7685 * another one is available.
7686 */
7687 txn->flags &= ~TX_CK_MASK;
7688 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007690 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007691 srv = srv->next;
7692 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007693
Willy Tarreauf64d1412010-10-07 20:06:11 +02007694 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007695 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007696 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007697 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007698 txn->flags |= TX_CK_UNUSED;
7699 else
7700 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007702
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007703 /* depending on the cookie mode, we may have to either :
7704 * - delete the complete cookie if we're in insert+indirect mode, so that
7705 * the server never sees it ;
7706 * - remove the server id from the cookie value, and tag the cookie as an
7707 * application cookie so that it does not get accidentely removed later,
7708 * if we're in cookie prefix mode
7709 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007710 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007711 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007712
Willy Tarreau9b28e032012-10-12 23:49:43 +02007713 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007714 val_end += delta;
7715 next += delta;
7716 hdr_end += delta;
7717 hdr_next += delta;
7718 cur_hdr->len += delta;
7719 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007720
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007721 del_from = NULL;
7722 preserve_hdr = 1; /* we want to keep this cookie */
7723 }
7724 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007725 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007726 del_from = prev;
7727 }
7728 } else {
7729 /* This is not our cookie, so we must preserve it. But if we already
7730 * scheduled another cookie for removal, we cannot remove the
7731 * complete header, but we can remove the previous block itself.
7732 */
7733 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007734
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007736 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007737 if (att_beg >= del_from)
7738 att_beg += delta;
7739 if (att_end >= del_from)
7740 att_end += delta;
7741 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007742 val_end += delta;
7743 next += delta;
7744 hdr_end += delta;
7745 hdr_next += delta;
7746 cur_hdr->len += delta;
7747 http_msg_move_end(&txn->req, delta);
7748 prev = del_from;
7749 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007750 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007751 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007752
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007753 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007754 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 int cmp_len, value_len;
7756 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007757
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007758 if (s->be->options2 & PR_O2_AS_PFX) {
7759 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7760 value_begin = att_beg + s->be->appsession_name_len;
7761 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007762 } else {
7763 cmp_len = att_end - att_beg;
7764 value_begin = val_beg;
7765 value_len = val_end - val_beg;
7766 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007767
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007768 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007769 if (cmp_len == s->be->appsession_name_len &&
7770 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7771 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007772 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007773 }
7774
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 /* continue with next cookie on this header line */
7776 att_beg = next;
7777 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007778
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007779 /* There are no more cookies on this line.
7780 * We may still have one (or several) marked for deletion at the
7781 * end of the line. We must do this now in two ways :
7782 * - if some cookies must be preserved, we only delete from the
7783 * mark to the end of line ;
7784 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007785 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007786 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007787 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007789 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007790 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007791 cur_hdr->len += delta;
7792 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007793 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007794
7795 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007796 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7797 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007798 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007799 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007800 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007801 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007802 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007803 }
7804
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007805 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007806 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007807 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007808}
7809
7810
Willy Tarreaua15645d2007-03-18 16:22:39 +01007811/* Iterate the same filter through all response headers contained in <rtr>.
7812 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7813 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007814int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007815{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007816 char *cur_ptr, *cur_end, *cur_next;
7817 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007818 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007819 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007820 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007821
7822 last_hdr = 0;
7823
Willy Tarreau9b28e032012-10-12 23:49:43 +02007824 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825 old_idx = 0;
7826
7827 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007828 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007830 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831 (exp->action == ACT_ALLOW ||
7832 exp->action == ACT_DENY))
7833 return 0;
7834
7835 cur_idx = txn->hdr_idx.v[old_idx].next;
7836 if (!cur_idx)
7837 break;
7838
7839 cur_hdr = &txn->hdr_idx.v[cur_idx];
7840 cur_ptr = cur_next;
7841 cur_end = cur_ptr + cur_hdr->len;
7842 cur_next = cur_end + cur_hdr->cr + 1;
7843
7844 /* Now we have one header between cur_ptr and cur_end,
7845 * and the next header starts at cur_next.
7846 */
7847
Willy Tarreau15a53a42015-01-21 13:39:42 +01007848 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849 switch (exp->action) {
7850 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007851 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007852 last_hdr = 1;
7853 break;
7854
7855 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007856 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007857 last_hdr = 1;
7858 break;
7859
7860 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007861 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7862 if (trash.len < 0)
7863 return -1;
7864
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007865 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007866 /* FIXME: if the user adds a newline in the replacement, the
7867 * index will not be recalculated for now, and the new line
7868 * will not be counted as a new header.
7869 */
7870
7871 cur_end += delta;
7872 cur_next += delta;
7873 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007874 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007875 break;
7876
7877 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007878 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007879 cur_next += delta;
7880
Willy Tarreaufa355d42009-11-29 18:12:29 +01007881 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007882 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7883 txn->hdr_idx.used--;
7884 cur_hdr->len = 0;
7885 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007886 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007887 break;
7888
7889 }
7890 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007891
7892 /* keep the link from this header to next one in case of later
7893 * removal of next header.
7894 */
7895 old_idx = cur_idx;
7896 }
7897 return 0;
7898}
7899
7900
7901/* Apply the filter to the status line in the response buffer <rtr>.
7902 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7903 * or -1 if a replacement resulted in an invalid status line.
7904 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007905int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007906{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007907 char *cur_ptr, *cur_end;
7908 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007909 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007910 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007911
7912
Willy Tarreau3d300592007-03-18 18:34:41 +01007913 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007914 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007915 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007916 (exp->action == ACT_ALLOW ||
7917 exp->action == ACT_DENY))
7918 return 0;
7919 else if (exp->action == ACT_REMOVE)
7920 return 0;
7921
7922 done = 0;
7923
Willy Tarreau9b28e032012-10-12 23:49:43 +02007924 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007925 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007926
7927 /* Now we have the status line between cur_ptr and cur_end */
7928
Willy Tarreau15a53a42015-01-21 13:39:42 +01007929 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007930 switch (exp->action) {
7931 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007932 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007933 done = 1;
7934 break;
7935
7936 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007937 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007938 done = 1;
7939 break;
7940
7941 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007942 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7943 if (trash.len < 0)
7944 return -1;
7945
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007946 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947 /* FIXME: if the user adds a newline in the replacement, the
7948 * index will not be recalculated for now, and the new line
7949 * will not be counted as a new header.
7950 */
7951
Willy Tarreaufa355d42009-11-29 18:12:29 +01007952 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007953 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007954 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007955 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007956 cur_ptr, cur_end + 1,
7957 NULL, NULL);
7958 if (unlikely(!cur_end))
7959 return -1;
7960
7961 /* we have a full respnse and we know that we have either a CR
7962 * or an LF at <ptr>.
7963 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007964 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007965 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007966 /* there is no point trying this regex on headers */
7967 return 1;
7968 }
7969 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007970 return done;
7971}
7972
7973
7974
7975/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007976 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007977 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7978 * unparsable response.
7979 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007980int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981{
Willy Tarreau192252e2015-04-04 01:47:55 +02007982 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007983 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007984 struct hdr_exp *exp;
7985
7986 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007987 int ret;
7988
7989 /*
7990 * The interleaving of transformations and verdicts
7991 * makes it difficult to decide to continue or stop
7992 * the evaluation.
7993 */
7994
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007995 if (txn->flags & TX_SVDENY)
7996 break;
7997
Willy Tarreau3d300592007-03-18 18:34:41 +01007998 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8000 exp->action == ACT_PASS)) {
8001 exp = exp->next;
8002 continue;
8003 }
8004
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008005 /* if this filter had a condition, evaluate it now and skip to
8006 * next filter if the condition does not match.
8007 */
8008 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008009 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008010 ret = acl_pass(ret);
8011 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8012 ret = !ret;
8013 if (!ret)
8014 continue;
8015 }
8016
Willy Tarreaua15645d2007-03-18 16:22:39 +01008017 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008018 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 if (unlikely(ret < 0))
8020 return -1;
8021
8022 if (likely(ret == 0)) {
8023 /* The filter did not match the response, it can be
8024 * iterated through all headers.
8025 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008026 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8027 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008029 }
8030 return 0;
8031}
8032
8033
Willy Tarreaua15645d2007-03-18 16:22:39 +01008034/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008035 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008036 * desirable to call it only when needed. This function is also used when we
8037 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008039void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008040{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008041 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008042 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008043 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008044 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008046 char *hdr_beg, *hdr_end, *hdr_next;
8047 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048
Willy Tarreaua15645d2007-03-18 16:22:39 +01008049 /* Iterate through the headers.
8050 * we start with the start line.
8051 */
8052 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008053 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054
8055 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8056 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008057 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058
8059 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008060 hdr_beg = hdr_next;
8061 hdr_end = hdr_beg + cur_hdr->len;
8062 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008063
Willy Tarreau24581ba2010-08-31 22:39:35 +02008064 /* We have one full header between hdr_beg and hdr_end, and the
8065 * next header starts at hdr_next. We're only interested in
8066 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067 */
8068
Willy Tarreau24581ba2010-08-31 22:39:35 +02008069 is_cookie2 = 0;
8070 prev = hdr_beg + 10;
8071 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008072 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008073 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8074 if (!val) {
8075 old_idx = cur_idx;
8076 continue;
8077 }
8078 is_cookie2 = 1;
8079 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008080 }
8081
Willy Tarreau24581ba2010-08-31 22:39:35 +02008082 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8083 * <prev> points to the colon.
8084 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008085 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008086
Willy Tarreau24581ba2010-08-31 22:39:35 +02008087 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8088 * check-cache is enabled) and we are not interested in checking
8089 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008090 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008091 if (s->be->cookie_name == NULL &&
8092 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008093 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008094 return;
8095
Willy Tarreau24581ba2010-08-31 22:39:35 +02008096 /* OK so now we know we have to process this response cookie.
8097 * The format of the Set-Cookie header is slightly different
8098 * from the format of the Cookie header in that it does not
8099 * support the comma as a cookie delimiter (thus the header
8100 * cannot be folded) because the Expires attribute described in
8101 * the original Netscape's spec may contain an unquoted date
8102 * with a comma inside. We have to live with this because
8103 * many browsers don't support Max-Age and some browsers don't
8104 * support quoted strings. However the Set-Cookie2 header is
8105 * clean.
8106 *
8107 * We have to keep multiple pointers in order to support cookie
8108 * removal at the beginning, middle or end of header without
8109 * corrupting the header (in case of set-cookie2). A special
8110 * pointer, <scav> points to the beginning of the set-cookie-av
8111 * fields after the first semi-colon. The <next> pointer points
8112 * either to the end of line (set-cookie) or next unquoted comma
8113 * (set-cookie2). All of these headers are valid :
8114 *
8115 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8116 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8117 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8118 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8119 * | | | | | | | | | |
8120 * | | | | | | | | +-> next hdr_end <--+
8121 * | | | | | | | +------------> scav
8122 * | | | | | | +--------------> val_end
8123 * | | | | | +--------------------> val_beg
8124 * | | | | +----------------------> equal
8125 * | | | +------------------------> att_end
8126 * | | +----------------------------> att_beg
8127 * | +------------------------------> prev
8128 * +-----------------------------------------> hdr_beg
8129 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130
Willy Tarreau24581ba2010-08-31 22:39:35 +02008131 for (; prev < hdr_end; prev = next) {
8132 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133
Willy Tarreau24581ba2010-08-31 22:39:35 +02008134 /* find att_beg */
8135 att_beg = prev + 1;
8136 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8137 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008138
Willy Tarreau24581ba2010-08-31 22:39:35 +02008139 /* find att_end : this is the first character after the last non
8140 * space before the equal. It may be equal to hdr_end.
8141 */
8142 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008143
Willy Tarreau24581ba2010-08-31 22:39:35 +02008144 while (equal < hdr_end) {
8145 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8146 break;
8147 if (http_is_spht[(unsigned char)*equal++])
8148 continue;
8149 att_end = equal;
8150 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151
Willy Tarreau24581ba2010-08-31 22:39:35 +02008152 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8153 * is between <att_beg> and <equal>, both may be identical.
8154 */
8155
8156 /* look for end of cookie if there is an equal sign */
8157 if (equal < hdr_end && *equal == '=') {
8158 /* look for the beginning of the value */
8159 val_beg = equal + 1;
8160 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8161 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162
Willy Tarreau24581ba2010-08-31 22:39:35 +02008163 /* find the end of the value, respecting quotes */
8164 next = find_cookie_value_end(val_beg, hdr_end);
8165
8166 /* make val_end point to the first white space or delimitor after the value */
8167 val_end = next;
8168 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8169 val_end--;
8170 } else {
8171 /* <equal> points to next comma, semi-colon or EOL */
8172 val_beg = val_end = next = equal;
8173 }
8174
8175 if (next < hdr_end) {
8176 /* Set-Cookie2 supports multiple cookies, and <next> points to
8177 * a colon or semi-colon before the end. So skip all attr-value
8178 * pairs and look for the next comma. For Set-Cookie, since
8179 * commas are permitted in values, skip to the end.
8180 */
8181 if (is_cookie2)
8182 next = find_hdr_value_end(next, hdr_end);
8183 else
8184 next = hdr_end;
8185 }
8186
8187 /* Now everything is as on the diagram above */
8188
8189 /* Ignore cookies with no equal sign */
8190 if (equal == val_end)
8191 continue;
8192
8193 /* If there are spaces around the equal sign, we need to
8194 * strip them otherwise we'll get trouble for cookie captures,
8195 * or even for rewrites. Since this happens extremely rarely,
8196 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008198 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8199 int stripped_before = 0;
8200 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201
Willy Tarreau24581ba2010-08-31 22:39:35 +02008202 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008203 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008204 equal += stripped_before;
8205 val_beg += stripped_before;
8206 }
8207
8208 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008209 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008210 val_beg += stripped_after;
8211 stripped_before += stripped_after;
8212 }
8213
8214 val_end += stripped_before;
8215 next += stripped_before;
8216 hdr_end += stripped_before;
8217 hdr_next += stripped_before;
8218 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008219 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008220 }
8221
8222 /* First, let's see if we want to capture this cookie. We check
8223 * that we don't already have a server side cookie, because we
8224 * can only capture one. Also as an optimisation, we ignore
8225 * cookies shorter than the declared name.
8226 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008227 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008228 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008229 (val_end - att_beg >= sess->fe->capture_namelen) &&
8230 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008231 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008232 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233 Alert("HTTP logging : out of memory.\n");
8234 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008235 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008236 if (log_len > sess->fe->capture_len)
8237 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008238 memcpy(txn->srv_cookie, att_beg, log_len);
8239 txn->srv_cookie[log_len] = 0;
8240 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008241 }
8242
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008243 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008244 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008245 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008246 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8247 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008248 /* assume passive cookie by default */
8249 txn->flags &= ~TX_SCK_MASK;
8250 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008251
8252 /* If the cookie is in insert mode on a known server, we'll delete
8253 * this occurrence because we'll insert another one later.
8254 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008255 * a direct access.
8256 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008257 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008258 /* The "preserve" flag was set, we don't want to touch the
8259 * server's cookie.
8260 */
8261 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008262 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008263 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008264 /* this cookie must be deleted */
8265 if (*prev == ':' && next == hdr_end) {
8266 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008267 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008268 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8269 txn->hdr_idx.used--;
8270 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008271 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008272 hdr_next += delta;
8273 http_msg_move_end(&txn->rsp, delta);
8274 /* note: while both invalid now, <next> and <hdr_end>
8275 * are still equal, so the for() will stop as expected.
8276 */
8277 } else {
8278 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008279 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008280 next = prev;
8281 hdr_end += delta;
8282 hdr_next += delta;
8283 cur_hdr->len += delta;
8284 http_msg_move_end(&txn->rsp, delta);
8285 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008286 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008287 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008288 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008289 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008290 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008291 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008292 * with this server since we know it.
8293 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008294 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008295 next += delta;
8296 hdr_end += delta;
8297 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008298 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008299 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008300
Willy Tarreauf1348312010-10-07 15:54:11 +02008301 txn->flags &= ~TX_SCK_MASK;
8302 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008303 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008304 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008305 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008306 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008307 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008308 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008309 next += delta;
8310 hdr_end += delta;
8311 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008312 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008313 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008314
Willy Tarreau827aee92011-03-10 16:55:02 +01008315 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008316 txn->flags &= ~TX_SCK_MASK;
8317 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008318 }
8319 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008320 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008321 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008322 int cmp_len, value_len;
8323 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008325 if (s->be->options2 & PR_O2_AS_PFX) {
8326 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8327 value_begin = att_beg + s->be->appsession_name_len;
8328 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008329 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 cmp_len = att_end - att_beg;
8331 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008332 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008333 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008334
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008335 if ((cmp_len == s->be->appsession_name_len) &&
8336 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008337 /* free a possibly previously allocated memory */
8338 pool_free2(apools.sessid, txn->sessid);
8339
Willy Tarreau87b09662015-04-03 00:22:06 +02008340 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008341 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008342 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008343 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008344 return;
8345 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008346 memcpy(txn->sessid, value_begin, value_len);
8347 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008348 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008349 }
8350 /* that's done for this cookie, check the next one on the same
8351 * line when next != hdr_end (only if is_cookie2).
8352 */
8353 }
8354 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008355 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008356 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008357
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008358 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008359 appsess *asession = NULL;
8360 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008361 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008362 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008363 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008364 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8365 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008366 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008367 return;
8368 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008369 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8370
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008371 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8372 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008373 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8374 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008375 return;
8376 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008377 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8378 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008379
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008380 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008381 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008382 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008383 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8384 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008385 return;
8386 }
8387 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008388 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008389
8390 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008391 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008392 }
8393
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008394 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008395 asession->request_count++;
8396 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008397}
8398
8399
Willy Tarreaua15645d2007-03-18 16:22:39 +01008400/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008401 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008402 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008403void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008404{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008405 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008406 char *p1, *p2;
8407
8408 char *cur_ptr, *cur_end, *cur_next;
8409 int cur_idx;
8410
Willy Tarreau5df51872007-11-25 16:20:08 +01008411 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008412 return;
8413
8414 /* Iterate through the headers.
8415 * we start with the start line.
8416 */
8417 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008418 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008419
8420 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8421 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008422 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008423
8424 cur_hdr = &txn->hdr_idx.v[cur_idx];
8425 cur_ptr = cur_next;
8426 cur_end = cur_ptr + cur_hdr->len;
8427 cur_next = cur_end + cur_hdr->cr + 1;
8428
8429 /* We have one full header between cur_ptr and cur_end, and the
8430 * next header starts at cur_next. We're only interested in
8431 * "Cookie:" headers.
8432 */
8433
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008434 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8435 if (val) {
8436 if ((cur_end - (cur_ptr + val) >= 8) &&
8437 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8438 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8439 return;
8440 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008441 }
8442
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008443 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8444 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008445 continue;
8446
8447 /* OK, right now we know we have a cache-control header at cur_ptr */
8448
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008449 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008450
8451 if (p1 >= cur_end) /* no more info */
8452 continue;
8453
8454 /* p1 is at the beginning of the value */
8455 p2 = p1;
8456
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008457 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008458 p2++;
8459
8460 /* we have a complete value between p1 and p2 */
8461 if (p2 < cur_end && *p2 == '=') {
8462 /* we have something of the form no-cache="set-cookie" */
8463 if ((cur_end - p1 >= 21) &&
8464 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8465 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008466 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008467 continue;
8468 }
8469
8470 /* OK, so we know that either p2 points to the end of string or to a comma */
8471 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008472 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008473 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8474 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8475 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008476 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008477 return;
8478 }
8479
8480 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008481 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008482 continue;
8483 }
8484 }
8485}
8486
8487
Willy Tarreau58f10d72006-12-04 02:26:12 +01008488/*
8489 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008490 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008491 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008492void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008493{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008494 char *end_params, *first_param, *cur_param, *next_param;
8495 char separator;
8496 int value_len;
8497
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008498 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008499
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008500 if (s->be->appsession_name == NULL ||
Willy Tarreaueee5b512015-04-03 23:46:31 +02008501 (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 +01008502 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008503 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008504
Cyril Bontéb21570a2009-11-29 20:04:48 +01008505 first_param = NULL;
8506 switch (mode) {
8507 case PR_O2_AS_M_PP:
8508 first_param = memchr(begin, ';', len);
8509 break;
8510 case PR_O2_AS_M_QS:
8511 first_param = memchr(begin, '?', len);
8512 break;
8513 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008514
Cyril Bontéb21570a2009-11-29 20:04:48 +01008515 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008516 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008517 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008518
Cyril Bontéb21570a2009-11-29 20:04:48 +01008519 switch (mode) {
8520 case PR_O2_AS_M_PP:
8521 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8522 end_params = (char *) begin + len;
8523 }
8524 separator = ';';
8525 break;
8526 case PR_O2_AS_M_QS:
8527 end_params = (char *) begin + len;
8528 separator = '&';
8529 break;
8530 default:
8531 /* unknown mode, shouldn't happen */
8532 return;
8533 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008534
Cyril Bontéb21570a2009-11-29 20:04:48 +01008535 cur_param = next_param = end_params;
8536 while (cur_param > first_param) {
8537 cur_param--;
8538 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8539 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008540 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8541 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8542 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008543 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008544 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8545 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008546 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008547 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008548 }
8549 break;
8550 }
8551 next_param = cur_param;
8552 }
8553 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008554#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008555 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008556 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008557#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008558}
8559
Willy Tarreaub2513902006-12-17 14:52:38 +01008560/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008561 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008562 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008563 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008564 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008565 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008566 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008567 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008568 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008569int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008570{
8571 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008572 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008573 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008574
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008575 if (!uri_auth)
8576 return 0;
8577
Cyril Bonté70be45d2010-10-12 00:14:35 +02008578 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008579 return 0;
8580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008581 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008582 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008583 return 0;
8584
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008585 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008586 return 0;
8587
Willy Tarreaub2513902006-12-17 14:52:38 +01008588 return 1;
8589}
8590
Willy Tarreau4076a152009-04-02 15:18:36 +02008591/*
8592 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008593 * By default it tries to report the error position as msg->err_pos. However if
8594 * this one is not set, it will then report msg->next, which is the last known
8595 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008596 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008597 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008598void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008599 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008600 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008601{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008602 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008603 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008604 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008605
Willy Tarreau9b28e032012-10-12 23:49:43 +02008606 es->len = MIN(chn->buf->i, sizeof(es->buf));
8607 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008608 len1 = MIN(len1, es->len);
8609 len2 = es->len - len1; /* remaining data if buffer wraps */
8610
Willy Tarreau9b28e032012-10-12 23:49:43 +02008611 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008612 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008613 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008614
Willy Tarreau4076a152009-04-02 15:18:36 +02008615 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008616 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008617 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008618 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008619
Willy Tarreau4076a152009-04-02 15:18:36 +02008620 es->when = date; // user-visible date
8621 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008622 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008623 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008624 if (objt_conn(sess->origin))
8625 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008626 else
8627 memset(&es->src, 0, sizeof(es->src));
8628
Willy Tarreau078272e2010-12-12 12:46:33 +01008629 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008630 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008631 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008632 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008633 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008634 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008635 es->b_out = chn->buf->o;
8636 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008637 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008638 es->m_clen = msg->chunk_len;
8639 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008640}
Willy Tarreaub2513902006-12-17 14:52:38 +01008641
Willy Tarreau294c4732011-12-16 21:35:50 +01008642/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8643 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8644 * performed over the whole headers. Otherwise it must contain a valid header
8645 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8646 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8647 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8648 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008649 * -1. The value fetch stops at commas, so this function is suited for use with
8650 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008651 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008652 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008653unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008654 struct hdr_idx *idx, int occ,
8655 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008656{
Willy Tarreau294c4732011-12-16 21:35:50 +01008657 struct hdr_ctx local_ctx;
8658 char *ptr_hist[MAX_HDR_HISTORY];
8659 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008660 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008661 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008662
Willy Tarreau294c4732011-12-16 21:35:50 +01008663 if (!ctx) {
8664 local_ctx.idx = 0;
8665 ctx = &local_ctx;
8666 }
8667
Willy Tarreaubce70882009-09-07 11:51:47 +02008668 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008669 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008670 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008671 occ--;
8672 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008673 *vptr = ctx->line + ctx->val;
8674 *vlen = ctx->vlen;
8675 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008676 }
8677 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008678 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008679 }
8680
8681 /* negative occurrence, we scan all the list then walk back */
8682 if (-occ > MAX_HDR_HISTORY)
8683 return 0;
8684
Willy Tarreau294c4732011-12-16 21:35:50 +01008685 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008686 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008687 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8688 len_hist[hist_ptr] = ctx->vlen;
8689 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008690 hist_ptr = 0;
8691 found++;
8692 }
8693 if (-occ > found)
8694 return 0;
8695 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008696 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8697 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8698 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008699 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008700 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008701 if (hist_ptr >= MAX_HDR_HISTORY)
8702 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008703 *vptr = ptr_hist[hist_ptr];
8704 *vlen = len_hist[hist_ptr];
8705 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008706}
8707
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008708/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8709 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8710 * performed over the whole headers. Otherwise it must contain a valid header
8711 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8712 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8713 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8714 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8715 * -1. This function differs from http_get_hdr() in that it only returns full
8716 * line header values and does not stop at commas.
8717 * The return value is 0 if nothing was found, or non-zero otherwise.
8718 */
8719unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8720 struct hdr_idx *idx, int occ,
8721 struct hdr_ctx *ctx, char **vptr, int *vlen)
8722{
8723 struct hdr_ctx local_ctx;
8724 char *ptr_hist[MAX_HDR_HISTORY];
8725 int len_hist[MAX_HDR_HISTORY];
8726 unsigned int hist_ptr;
8727 int found;
8728
8729 if (!ctx) {
8730 local_ctx.idx = 0;
8731 ctx = &local_ctx;
8732 }
8733
8734 if (occ >= 0) {
8735 /* search from the beginning */
8736 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8737 occ--;
8738 if (occ <= 0) {
8739 *vptr = ctx->line + ctx->val;
8740 *vlen = ctx->vlen;
8741 return 1;
8742 }
8743 }
8744 return 0;
8745 }
8746
8747 /* negative occurrence, we scan all the list then walk back */
8748 if (-occ > MAX_HDR_HISTORY)
8749 return 0;
8750
8751 found = hist_ptr = 0;
8752 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8753 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8754 len_hist[hist_ptr] = ctx->vlen;
8755 if (++hist_ptr >= MAX_HDR_HISTORY)
8756 hist_ptr = 0;
8757 found++;
8758 }
8759 if (-occ > found)
8760 return 0;
8761 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8762 * find occurrence -occ, so we have to check [hist_ptr+occ].
8763 */
8764 hist_ptr += occ;
8765 if (hist_ptr >= MAX_HDR_HISTORY)
8766 hist_ptr -= MAX_HDR_HISTORY;
8767 *vptr = ptr_hist[hist_ptr];
8768 *vlen = len_hist[hist_ptr];
8769 return 1;
8770}
8771
Willy Tarreaubaaee002006-06-26 02:48:02 +02008772/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008773 * Print a debug line with a header. Always stop at the first CR or LF char,
8774 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8775 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008776 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008777void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008778{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008779 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008780 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008781
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008782 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008783 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008784 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008785 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 +02008786
8787 for (max = 0; start + max < end; max++)
8788 if (start[max] == '\r' || start[max] == '\n')
8789 break;
8790
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008791 UBOUND(max, trash.size - trash.len - 3);
8792 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8793 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008794 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008795}
8796
Willy Tarreaueee5b512015-04-03 23:46:31 +02008797
8798/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8799 * The hdr_idx is allocated as well. In case of allocation failure, everything
8800 * allocated is freed and NULL is returned. Otherwise the new transaction is
8801 * assigned to the stream and returned.
8802 */
8803struct http_txn *http_alloc_txn(struct stream *s)
8804{
8805 struct http_txn *txn = s->txn;
8806
8807 if (txn)
8808 return txn;
8809
8810 txn = pool_alloc2(pool2_http_txn);
8811 if (!txn)
8812 return txn;
8813
8814 txn->hdr_idx.size = global.tune.max_http_hdr;
8815 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8816 if (!txn->hdr_idx.v) {
8817 pool_free2(pool2_http_txn, txn);
8818 return NULL;
8819 }
8820
8821 s->txn = txn;
8822 return txn;
8823}
8824
Willy Tarreau0937bc42009-12-22 15:03:09 +01008825/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008826 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008827 * the required fields are properly allocated and that we only need to (re)init
8828 * them. This should be used before processing any new request.
8829 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008830void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008831{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008832 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008833 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008834
8835 txn->flags = 0;
8836 txn->status = -1;
8837
Willy Tarreauf64d1412010-10-07 20:06:11 +02008838 txn->cookie_first_date = 0;
8839 txn->cookie_last_date = 0;
8840
Willy Tarreaueee5b512015-04-03 23:46:31 +02008841 txn->sessid = NULL;
8842 txn->srv_cookie = NULL;
8843 txn->cli_cookie = NULL;
8844 txn->uri = NULL;
8845
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008846 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008847 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008848 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008849 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008850 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008851 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008852 txn->req.chunk_len = 0LL;
8853 txn->req.body_len = 0LL;
8854 txn->rsp.chunk_len = 0LL;
8855 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008856 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8857 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008858 txn->req.chn = &s->req;
8859 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008860
8861 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008862
8863 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8864 if (fe->options2 & PR_O2_REQBUG_OK)
8865 txn->req.err_pos = -1; /* let buggy requests pass */
8866
Willy Tarreau0937bc42009-12-22 15:03:09 +01008867 if (txn->hdr_idx.v)
8868 hdr_idx_init(&txn->hdr_idx);
8869}
8870
8871/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008872void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008873{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008874 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008875 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008876
Willy Tarreau75195602014-03-11 15:48:55 +01008877 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008878 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008879 s->comp_algo->end(&s->comp_ctx);
8880 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008881 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008882
Willy Tarreau0937bc42009-12-22 15:03:09 +01008883 /* these ones will have been dynamically allocated */
8884 pool_free2(pool2_requri, txn->uri);
8885 pool_free2(pool2_capture, txn->cli_cookie);
8886 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008887 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008888 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008889
William Lallemanda73203e2012-03-12 12:48:57 +01008890 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008891 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008892 txn->uri = NULL;
8893 txn->srv_cookie = NULL;
8894 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008895
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008896 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008897 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008898 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008899 pool_free2(h->pool, s->req_cap[h->index]);
8900 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008901 }
8902
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008903 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008904 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008905 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008906 pool_free2(h->pool, s->res_cap[h->index]);
8907 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008908 }
8909
Willy Tarreau0937bc42009-12-22 15:03:09 +01008910}
8911
8912/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008913void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008914{
8915 http_end_txn(s);
8916 http_init_txn(s);
8917
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008918 /* reinitialise the current rule list pointer to NULL. We are sure that
8919 * any rulelist match the NULL pointer.
8920 */
8921 s->current_rule_list = NULL;
8922
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008923 s->be = strm_fe(s);
8924 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008925 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008926 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008927 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008928 /* re-init store persistence */
8929 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008930 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008931
Willy Tarreau0937bc42009-12-22 15:03:09 +01008932 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008933
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008934 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008935
Willy Tarreau739cfba2010-01-25 23:11:14 +01008936 /* We must trim any excess data from the response buffer, because we
8937 * may have blocked an invalid response from a server that we don't
8938 * want to accidentely forward once we disable the analysers, nor do
8939 * we want those data to come along with next response. A typical
8940 * example of such data would be from a buggy server responding to
8941 * a HEAD with some data, or sending more than the advertised
8942 * content-length.
8943 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008944 if (unlikely(s->res.buf->i))
8945 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008946
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008947 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008948 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008949
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008950 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008951 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008952
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008953 s->req.rex = TICK_ETERNITY;
8954 s->req.wex = TICK_ETERNITY;
8955 s->req.analyse_exp = TICK_ETERNITY;
8956 s->res.rex = TICK_ETERNITY;
8957 s->res.wex = TICK_ETERNITY;
8958 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008959}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008960
Sasha Pachev218f0642014-06-16 12:05:59 -06008961void free_http_res_rules(struct list *r)
8962{
8963 struct http_res_rule *tr, *pr;
8964
8965 list_for_each_entry_safe(pr, tr, r, list) {
8966 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008967 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008968 free(pr);
8969 }
8970}
8971
8972void free_http_req_rules(struct list *r)
8973{
Willy Tarreauff011f22011-01-06 17:51:27 +01008974 struct http_req_rule *tr, *pr;
8975
8976 list_for_each_entry_safe(pr, tr, r, list) {
8977 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008978 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008979 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008980
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008981 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008982 free(pr);
8983 }
8984}
8985
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008986/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008987struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8988{
8989 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008990 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008991 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008992 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008993
8994 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8995 if (!rule) {
8996 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008997 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008998 }
8999
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009000 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01009001 rule->action = HTTP_REQ_ACT_ALLOW;
9002 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009003 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01009004 rule->action = HTTP_REQ_ACT_DENY;
9005 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009006 } else if (!strcmp(args[0], "tarpit")) {
9007 rule->action = HTTP_REQ_ACT_TARPIT;
9008 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009009 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01009010 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009011 cur_arg = 1;
9012
9013 while(*args[cur_arg]) {
9014 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009015 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009016 cur_arg+=2;
9017 continue;
9018 } else
9019 break;
9020 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009021 } else if (!strcmp(args[0], "set-nice")) {
9022 rule->action = HTTP_REQ_ACT_SET_NICE;
9023 cur_arg = 1;
9024
9025 if (!*args[cur_arg] ||
9026 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9027 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9028 file, linenum, args[0]);
9029 goto out_err;
9030 }
9031 rule->arg.nice = atoi(args[cur_arg]);
9032 if (rule->arg.nice < -1024)
9033 rule->arg.nice = -1024;
9034 else if (rule->arg.nice > 1024)
9035 rule->arg.nice = 1024;
9036 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009037 } else if (!strcmp(args[0], "set-tos")) {
9038#ifdef IP_TOS
9039 char *err;
9040 rule->action = HTTP_REQ_ACT_SET_TOS;
9041 cur_arg = 1;
9042
9043 if (!*args[cur_arg] ||
9044 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9045 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9046 file, linenum, args[0]);
9047 goto out_err;
9048 }
9049
9050 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9051 if (err && *err != '\0') {
9052 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9053 file, linenum, err, args[0]);
9054 goto out_err;
9055 }
9056 cur_arg++;
9057#else
9058 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9059 goto out_err;
9060#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009061 } else if (!strcmp(args[0], "set-mark")) {
9062#ifdef SO_MARK
9063 char *err;
9064 rule->action = HTTP_REQ_ACT_SET_MARK;
9065 cur_arg = 1;
9066
9067 if (!*args[cur_arg] ||
9068 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9069 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9070 file, linenum, args[0]);
9071 goto out_err;
9072 }
9073
9074 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9075 if (err && *err != '\0') {
9076 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9077 file, linenum, err, args[0]);
9078 goto out_err;
9079 }
9080 cur_arg++;
9081 global.last_checks |= LSTCHK_NETADM;
9082#else
9083 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9084 goto out_err;
9085#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009086 } else if (!strcmp(args[0], "set-log-level")) {
9087 rule->action = HTTP_REQ_ACT_SET_LOGL;
9088 cur_arg = 1;
9089
9090 if (!*args[cur_arg] ||
9091 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9092 bad_log_level:
9093 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9094 file, linenum, args[0]);
9095 goto out_err;
9096 }
9097 if (strcmp(args[cur_arg], "silent") == 0)
9098 rule->arg.loglevel = -1;
9099 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9100 goto bad_log_level;
9101 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009102 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9103 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9104 cur_arg = 1;
9105
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009106 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9107 (*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 +01009108 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9109 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009110 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009111 }
9112
9113 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9114 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9115 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009116
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009117 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009118 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009119 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9120 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009121 free(proxy->conf.lfs_file);
9122 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9123 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009124 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009125 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9126 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009127 cur_arg = 1;
9128
9129 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009130 (*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 -06009131 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9132 file, linenum, args[0]);
9133 goto out_err;
9134 }
9135
9136 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9137 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9138 LIST_INIT(&rule->arg.hdr_add.fmt);
9139
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009140 error = NULL;
9141 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9142 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9143 args[cur_arg + 1], error);
9144 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009145 goto out_err;
9146 }
9147
9148 proxy->conf.args.ctx = ARGC_HRQ;
9149 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9150 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9151 file, linenum);
9152
9153 free(proxy->conf.lfs_file);
9154 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9155 proxy->conf.lfs_line = proxy->conf.args.line;
9156 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009157 } else if (strcmp(args[0], "del-header") == 0) {
9158 rule->action = HTTP_REQ_ACT_DEL_HDR;
9159 cur_arg = 1;
9160
9161 if (!*args[cur_arg] ||
9162 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9163 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9164 file, linenum, args[0]);
9165 goto out_err;
9166 }
9167
9168 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9169 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9170
9171 proxy->conf.args.ctx = ARGC_HRQ;
9172 free(proxy->conf.lfs_file);
9173 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9174 proxy->conf.lfs_line = proxy->conf.args.line;
9175 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009176 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9177 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009178 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009179 struct sample_expr *expr;
9180 unsigned int where;
9181 char *err = NULL;
9182
9183 cur_arg = 1;
9184 proxy->conf.args.ctx = ARGC_TRK;
9185
9186 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9187 if (!expr) {
9188 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9189 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9190 free(err);
9191 goto out_err;
9192 }
9193
9194 where = 0;
9195 if (proxy->cap & PR_CAP_FE)
9196 where |= SMP_VAL_FE_HRQ_HDR;
9197 if (proxy->cap & PR_CAP_BE)
9198 where |= SMP_VAL_BE_HRQ_HDR;
9199
9200 if (!(expr->fetch->val & where)) {
9201 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9202 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9203 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9204 args[cur_arg-1], sample_src_names(expr->fetch->use));
9205 free(expr);
9206 goto out_err;
9207 }
9208
9209 if (strcmp(args[cur_arg], "table") == 0) {
9210 cur_arg++;
9211 if (!args[cur_arg]) {
9212 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9213 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9214 free(expr);
9215 goto out_err;
9216 }
9217 /* we copy the table name for now, it will be resolved later */
9218 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9219 cur_arg++;
9220 }
9221 rule->act_prm.trk_ctr.expr = expr;
9222 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009223 } else if (strcmp(args[0], "redirect") == 0) {
9224 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009225 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009226
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009227 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009228 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9229 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9230 goto out_err;
9231 }
9232
9233 /* this redirect rule might already contain a parsed condition which
9234 * we'll pass to the http-request rule.
9235 */
9236 rule->action = HTTP_REQ_ACT_REDIR;
9237 rule->arg.redir = redir;
9238 rule->cond = redir->cond;
9239 redir->cond = NULL;
9240 cur_arg = 2;
9241 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009242 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9243 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9244 rule->action = HTTP_REQ_ACT_ADD_ACL;
9245 /*
9246 * '+ 8' for 'add-acl('
9247 * '- 9' for 'add-acl(' + trailing ')'
9248 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009249 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009250
9251 cur_arg = 1;
9252
9253 if (!*args[cur_arg] ||
9254 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9255 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9256 file, linenum, args[0]);
9257 goto out_err;
9258 }
9259
9260 LIST_INIT(&rule->arg.map.key);
9261 proxy->conf.args.ctx = ARGC_HRQ;
9262 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9263 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9264 file, linenum);
9265 free(proxy->conf.lfs_file);
9266 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9267 proxy->conf.lfs_line = proxy->conf.args.line;
9268 cur_arg += 1;
9269 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9270 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9271 rule->action = HTTP_REQ_ACT_DEL_ACL;
9272 /*
9273 * '+ 8' for 'del-acl('
9274 * '- 9' for 'del-acl(' + trailing ')'
9275 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009276 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009277
9278 cur_arg = 1;
9279
9280 if (!*args[cur_arg] ||
9281 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9282 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9283 file, linenum, args[0]);
9284 goto out_err;
9285 }
9286
9287 LIST_INIT(&rule->arg.map.key);
9288 proxy->conf.args.ctx = ARGC_HRQ;
9289 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9290 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9291 file, linenum);
9292 free(proxy->conf.lfs_file);
9293 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9294 proxy->conf.lfs_line = proxy->conf.args.line;
9295 cur_arg += 1;
9296 } else if (strncmp(args[0], "del-map", 7) == 0) {
9297 /* http-request del-map(<reference (map name)>) <key pattern> */
9298 rule->action = HTTP_REQ_ACT_DEL_MAP;
9299 /*
9300 * '+ 8' for 'del-map('
9301 * '- 9' for 'del-map(' + trailing ')'
9302 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009303 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009304
9305 cur_arg = 1;
9306
9307 if (!*args[cur_arg] ||
9308 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9309 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9310 file, linenum, args[0]);
9311 goto out_err;
9312 }
9313
9314 LIST_INIT(&rule->arg.map.key);
9315 proxy->conf.args.ctx = ARGC_HRQ;
9316 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9317 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9318 file, linenum);
9319 free(proxy->conf.lfs_file);
9320 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9321 proxy->conf.lfs_line = proxy->conf.args.line;
9322 cur_arg += 1;
9323 } else if (strncmp(args[0], "set-map", 7) == 0) {
9324 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9325 rule->action = HTTP_REQ_ACT_SET_MAP;
9326 /*
9327 * '+ 8' for 'set-map('
9328 * '- 9' for 'set-map(' + trailing ')'
9329 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009330 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009331
9332 cur_arg = 1;
9333
9334 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9335 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9336 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9337 file, linenum, args[0]);
9338 goto out_err;
9339 }
9340
9341 LIST_INIT(&rule->arg.map.key);
9342 LIST_INIT(&rule->arg.map.value);
9343 proxy->conf.args.ctx = ARGC_HRQ;
9344
9345 /* key pattern */
9346 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9347 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9348 file, linenum);
9349
9350 /* value pattern */
9351 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9352 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9353 file, linenum);
9354 free(proxy->conf.lfs_file);
9355 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9356 proxy->conf.lfs_line = proxy->conf.args.line;
9357
9358 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009359 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9360 char *errmsg = NULL;
9361 cur_arg = 1;
9362 /* try in the module list */
9363 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9364 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9365 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9366 free(errmsg);
9367 goto out_err;
9368 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009369 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009370 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 +01009371 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009372 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009373 }
9374
9375 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9376 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009377 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009378
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009379 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9380 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9381 file, linenum, args[0], errmsg);
9382 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009383 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009384 }
9385 rule->cond = cond;
9386 }
9387 else if (*args[cur_arg]) {
9388 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9389 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9390 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009391 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009392 }
9393
9394 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009395 out_err:
9396 free(rule);
9397 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009398}
9399
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009400/* parse an "http-respose" rule */
9401struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9402{
9403 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009404 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009405 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009406 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009407
9408 rule = calloc(1, sizeof(*rule));
9409 if (!rule) {
9410 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9411 goto out_err;
9412 }
9413
9414 if (!strcmp(args[0], "allow")) {
9415 rule->action = HTTP_RES_ACT_ALLOW;
9416 cur_arg = 1;
9417 } else if (!strcmp(args[0], "deny")) {
9418 rule->action = HTTP_RES_ACT_DENY;
9419 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009420 } else if (!strcmp(args[0], "set-nice")) {
9421 rule->action = HTTP_RES_ACT_SET_NICE;
9422 cur_arg = 1;
9423
9424 if (!*args[cur_arg] ||
9425 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9426 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9427 file, linenum, args[0]);
9428 goto out_err;
9429 }
9430 rule->arg.nice = atoi(args[cur_arg]);
9431 if (rule->arg.nice < -1024)
9432 rule->arg.nice = -1024;
9433 else if (rule->arg.nice > 1024)
9434 rule->arg.nice = 1024;
9435 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009436 } else if (!strcmp(args[0], "set-tos")) {
9437#ifdef IP_TOS
9438 char *err;
9439 rule->action = HTTP_RES_ACT_SET_TOS;
9440 cur_arg = 1;
9441
9442 if (!*args[cur_arg] ||
9443 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9444 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9445 file, linenum, args[0]);
9446 goto out_err;
9447 }
9448
9449 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9450 if (err && *err != '\0') {
9451 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9452 file, linenum, err, args[0]);
9453 goto out_err;
9454 }
9455 cur_arg++;
9456#else
9457 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9458 goto out_err;
9459#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009460 } else if (!strcmp(args[0], "set-mark")) {
9461#ifdef SO_MARK
9462 char *err;
9463 rule->action = HTTP_RES_ACT_SET_MARK;
9464 cur_arg = 1;
9465
9466 if (!*args[cur_arg] ||
9467 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9468 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9469 file, linenum, args[0]);
9470 goto out_err;
9471 }
9472
9473 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9474 if (err && *err != '\0') {
9475 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9476 file, linenum, err, args[0]);
9477 goto out_err;
9478 }
9479 cur_arg++;
9480 global.last_checks |= LSTCHK_NETADM;
9481#else
9482 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9483 goto out_err;
9484#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009485 } else if (!strcmp(args[0], "set-log-level")) {
9486 rule->action = HTTP_RES_ACT_SET_LOGL;
9487 cur_arg = 1;
9488
9489 if (!*args[cur_arg] ||
9490 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9491 bad_log_level:
9492 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9493 file, linenum, args[0]);
9494 goto out_err;
9495 }
9496 if (strcmp(args[cur_arg], "silent") == 0)
9497 rule->arg.loglevel = -1;
9498 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9499 goto bad_log_level;
9500 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009501 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9502 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9503 cur_arg = 1;
9504
9505 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9506 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9507 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9508 file, linenum, args[0]);
9509 goto out_err;
9510 }
9511
9512 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9513 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9514 LIST_INIT(&rule->arg.hdr_add.fmt);
9515
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009516 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009517 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009518 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9519 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009520 free(proxy->conf.lfs_file);
9521 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9522 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009523 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009524 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009525 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009526 cur_arg = 1;
9527
9528 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009529 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9530 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009531 file, linenum, args[0]);
9532 goto out_err;
9533 }
9534
9535 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9536 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9537 LIST_INIT(&rule->arg.hdr_add.fmt);
9538
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009539 error = NULL;
9540 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9541 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9542 args[cur_arg + 1], error);
9543 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009544 goto out_err;
9545 }
9546
9547 proxy->conf.args.ctx = ARGC_HRQ;
9548 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9549 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9550 file, linenum);
9551
9552 free(proxy->conf.lfs_file);
9553 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9554 proxy->conf.lfs_line = proxy->conf.args.line;
9555 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009556 } else if (strcmp(args[0], "del-header") == 0) {
9557 rule->action = HTTP_RES_ACT_DEL_HDR;
9558 cur_arg = 1;
9559
9560 if (!*args[cur_arg] ||
9561 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9562 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9563 file, linenum, args[0]);
9564 goto out_err;
9565 }
9566
9567 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9568 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9569
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009570 proxy->conf.args.ctx = ARGC_HRS;
9571 free(proxy->conf.lfs_file);
9572 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9573 proxy->conf.lfs_line = proxy->conf.args.line;
9574 cur_arg += 1;
9575 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9576 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9577 rule->action = HTTP_RES_ACT_ADD_ACL;
9578 /*
9579 * '+ 8' for 'add-acl('
9580 * '- 9' for 'add-acl(' + trailing ')'
9581 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009582 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009583
9584 cur_arg = 1;
9585
9586 if (!*args[cur_arg] ||
9587 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9588 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9589 file, linenum, args[0]);
9590 goto out_err;
9591 }
9592
9593 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009594 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009595 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9596 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9597 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009598 free(proxy->conf.lfs_file);
9599 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9600 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009601
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009602 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009603 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9604 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9605 rule->action = HTTP_RES_ACT_DEL_ACL;
9606 /*
9607 * '+ 8' for 'del-acl('
9608 * '- 9' for 'del-acl(' + trailing ')'
9609 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009610 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009611
9612 cur_arg = 1;
9613
9614 if (!*args[cur_arg] ||
9615 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9616 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9617 file, linenum, args[0]);
9618 goto out_err;
9619 }
9620
9621 LIST_INIT(&rule->arg.map.key);
9622 proxy->conf.args.ctx = ARGC_HRS;
9623 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9624 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9625 file, linenum);
9626 free(proxy->conf.lfs_file);
9627 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9628 proxy->conf.lfs_line = proxy->conf.args.line;
9629 cur_arg += 1;
9630 } else if (strncmp(args[0], "del-map", 7) == 0) {
9631 /* http-response del-map(<reference (map name)>) <key pattern> */
9632 rule->action = HTTP_RES_ACT_DEL_MAP;
9633 /*
9634 * '+ 8' for 'del-map('
9635 * '- 9' for 'del-map(' + trailing ')'
9636 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009637 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009638
9639 cur_arg = 1;
9640
9641 if (!*args[cur_arg] ||
9642 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9643 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9644 file, linenum, args[0]);
9645 goto out_err;
9646 }
9647
9648 LIST_INIT(&rule->arg.map.key);
9649 proxy->conf.args.ctx = ARGC_HRS;
9650 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9651 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9652 file, linenum);
9653 free(proxy->conf.lfs_file);
9654 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9655 proxy->conf.lfs_line = proxy->conf.args.line;
9656 cur_arg += 1;
9657 } else if (strncmp(args[0], "set-map", 7) == 0) {
9658 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9659 rule->action = HTTP_RES_ACT_SET_MAP;
9660 /*
9661 * '+ 8' for 'set-map('
9662 * '- 9' for 'set-map(' + trailing ')'
9663 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009664 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009665
9666 cur_arg = 1;
9667
9668 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9669 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9670 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9671 file, linenum, args[0]);
9672 goto out_err;
9673 }
9674
9675 LIST_INIT(&rule->arg.map.key);
9676 LIST_INIT(&rule->arg.map.value);
9677
9678 proxy->conf.args.ctx = ARGC_HRS;
9679
9680 /* key pattern */
9681 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9682 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9683 file, linenum);
9684
9685 /* value pattern */
9686 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9687 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9688 file, linenum);
9689
9690 free(proxy->conf.lfs_file);
9691 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9692 proxy->conf.lfs_line = proxy->conf.args.line;
9693
9694 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009695 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9696 char *errmsg = NULL;
9697 cur_arg = 1;
9698 /* try in the module list */
9699 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9700 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9701 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9702 free(errmsg);
9703 goto out_err;
9704 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009705 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009706 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 +02009707 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9708 goto out_err;
9709 }
9710
9711 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9712 struct acl_cond *cond;
9713 char *errmsg = NULL;
9714
9715 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9716 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9717 file, linenum, args[0], errmsg);
9718 free(errmsg);
9719 goto out_err;
9720 }
9721 rule->cond = cond;
9722 }
9723 else if (*args[cur_arg]) {
9724 Alert("parsing [%s:%d]: 'http-response %s' expects"
9725 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9726 file, linenum, args[0], args[cur_arg]);
9727 goto out_err;
9728 }
9729
9730 return rule;
9731 out_err:
9732 free(rule);
9733 return NULL;
9734}
9735
Willy Tarreau4baae242012-12-27 12:00:31 +01009736/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009737 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9738 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009739 */
9740struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009741 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009742{
9743 struct redirect_rule *rule;
9744 int cur_arg;
9745 int type = REDIRECT_TYPE_NONE;
9746 int code = 302;
9747 const char *destination = NULL;
9748 const char *cookie = NULL;
9749 int cookie_set = 0;
9750 unsigned int flags = REDIRECT_FLAG_NONE;
9751 struct acl_cond *cond = NULL;
9752
9753 cur_arg = 0;
9754 while (*(args[cur_arg])) {
9755 if (strcmp(args[cur_arg], "location") == 0) {
9756 if (!*args[cur_arg + 1])
9757 goto missing_arg;
9758
9759 type = REDIRECT_TYPE_LOCATION;
9760 cur_arg++;
9761 destination = args[cur_arg];
9762 }
9763 else if (strcmp(args[cur_arg], "prefix") == 0) {
9764 if (!*args[cur_arg + 1])
9765 goto missing_arg;
9766
9767 type = REDIRECT_TYPE_PREFIX;
9768 cur_arg++;
9769 destination = args[cur_arg];
9770 }
9771 else if (strcmp(args[cur_arg], "scheme") == 0) {
9772 if (!*args[cur_arg + 1])
9773 goto missing_arg;
9774
9775 type = REDIRECT_TYPE_SCHEME;
9776 cur_arg++;
9777 destination = args[cur_arg];
9778 }
9779 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9780 if (!*args[cur_arg + 1])
9781 goto missing_arg;
9782
9783 cur_arg++;
9784 cookie = args[cur_arg];
9785 cookie_set = 1;
9786 }
9787 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9788 if (!*args[cur_arg + 1])
9789 goto missing_arg;
9790
9791 cur_arg++;
9792 cookie = args[cur_arg];
9793 cookie_set = 0;
9794 }
9795 else if (strcmp(args[cur_arg], "code") == 0) {
9796 if (!*args[cur_arg + 1])
9797 goto missing_arg;
9798
9799 cur_arg++;
9800 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009801 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009802 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009803 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009804 args[cur_arg - 1], args[cur_arg]);
9805 return NULL;
9806 }
9807 }
9808 else if (!strcmp(args[cur_arg],"drop-query")) {
9809 flags |= REDIRECT_FLAG_DROP_QS;
9810 }
9811 else if (!strcmp(args[cur_arg],"append-slash")) {
9812 flags |= REDIRECT_FLAG_APPEND_SLASH;
9813 }
9814 else if (strcmp(args[cur_arg], "if") == 0 ||
9815 strcmp(args[cur_arg], "unless") == 0) {
9816 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9817 if (!cond) {
9818 memprintf(errmsg, "error in condition: %s", *errmsg);
9819 return NULL;
9820 }
9821 break;
9822 }
9823 else {
9824 memprintf(errmsg,
9825 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9826 args[cur_arg]);
9827 return NULL;
9828 }
9829 cur_arg++;
9830 }
9831
9832 if (type == REDIRECT_TYPE_NONE) {
9833 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9834 return NULL;
9835 }
9836
9837 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9838 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009839 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009840
9841 if (!use_fmt) {
9842 /* old-style static redirect rule */
9843 rule->rdr_str = strdup(destination);
9844 rule->rdr_len = strlen(destination);
9845 }
9846 else {
9847 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009848
9849 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9850 * if prefix == "/", we don't want to add anything, otherwise it
9851 * makes it hard for the user to configure a self-redirection.
9852 */
Godbachd9722032014-12-18 15:44:58 +08009853 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009854 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009855 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009856 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9857 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009858 free(curproxy->conf.lfs_file);
9859 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9860 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009861 }
9862 }
9863
Willy Tarreau4baae242012-12-27 12:00:31 +01009864 if (cookie) {
9865 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9866 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9867 */
9868 rule->cookie_len = strlen(cookie);
9869 if (cookie_set) {
9870 rule->cookie_str = malloc(rule->cookie_len + 10);
9871 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9872 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9873 rule->cookie_len += 9;
9874 } else {
9875 rule->cookie_str = malloc(rule->cookie_len + 21);
9876 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9877 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9878 rule->cookie_len += 20;
9879 }
9880 }
9881 rule->type = type;
9882 rule->code = code;
9883 rule->flags = flags;
9884 LIST_INIT(&rule->list);
9885 return rule;
9886
9887 missing_arg:
9888 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9889 return NULL;
9890}
9891
Willy Tarreau8797c062007-05-07 00:55:35 +02009892/************************************************************************/
9893/* The code below is dedicated to ACL parsing and matching */
9894/************************************************************************/
9895
9896
Willy Tarreau14174bc2012-04-16 14:34:04 +02009897/* This function ensures that the prerequisites for an L7 fetch are ready,
9898 * which means that a request or response is ready. If some data is missing,
9899 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009900 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9901 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009902 *
9903 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009904 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9905 * decide whether or not an HTTP message is present ;
9906 * 0 if the requested data cannot be fetched or if it is certain that
9907 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009908 * 1 if an HTTP message is ready
9909 */
9910static int
Willy Tarreau15e91e12015-04-04 00:52:09 +02009911smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009912 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009913{
Willy Tarreau192252e2015-04-04 01:47:55 +02009914 struct http_txn *txn;
9915 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009916
Willy Tarreaueee5b512015-04-03 23:46:31 +02009917 /* Note: this function may only be used from places where
9918 * http_init_txn() has already been done, and implies that <s>,
9919 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
9920 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009921 */
9922
Willy Tarreau192252e2015-04-04 01:47:55 +02009923 if (!s)
9924 return 0;
9925 txn = s->txn;
9926
9927 if (!txn)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009928 return 0;
Willy Tarreau192252e2015-04-04 01:47:55 +02009929 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009930
9931 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009932 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009933
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009934 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009935 /* If the buffer does not leave enough free space at the end,
9936 * we must first realign it.
9937 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009938 if (s->req.buf->p > s->req.buf->data &&
9939 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9940 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009941
Willy Tarreau14174bc2012-04-16 14:34:04 +02009942 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009943 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009944 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009945
9946 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009947 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009948 http_msg_analyzer(msg, &txn->hdr_idx);
9949
9950 /* Still no valid request ? */
9951 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009952 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009953 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009954 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009955 }
9956 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009957 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009958 return 0;
9959 }
9960
9961 /* OK we just got a valid HTTP request. We have some minor
9962 * preparation to perform so that further checks can rely
9963 * on HTTP tests.
9964 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009965
9966 /* If the request was parsed but was too large, we must absolutely
9967 * return an error so that it is not processed. At the moment this
9968 * cannot happen, but if the parsers are to change in the future,
9969 * we want this check to be maintained.
9970 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009971 if (unlikely(s->req.buf->i + s->req.buf->p >
9972 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009973 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009974 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009975 return 1;
9976 }
9977
Willy Tarreau9b28e032012-10-12 23:49:43 +02009978 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009979 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009980 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009981
Willy Tarreau506d0502013-07-06 13:29:24 +02009982 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9983 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009984 }
9985
Willy Tarreau506d0502013-07-06 13:29:24 +02009986 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009987 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009988 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009989
9990 /* otherwise everything's ready for the request */
9991 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009992 else {
9993 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009994 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9995 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009996 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009997 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009998 }
9999
10000 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +020010001 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010002 return 1;
10003}
Willy Tarreau8797c062007-05-07 00:55:35 +020010004
Willy Tarreau6c616e02014-06-25 16:56:41 +020010005/* Note: these functinos *do* modify the sample. Even in case of success, at
10006 * least the type and uint value are modified.
10007 */
Willy Tarreauc0239e02012-04-16 14:42:55 +020010008#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010009 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 +020010010
Willy Tarreau24e32d82012-04-23 23:55:44 +020010011#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau15e91e12015-04-04 00:52:09 +020010012 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 +020010013
Willy Tarreau8797c062007-05-07 00:55:35 +020010014
10015/* 1. Check on METHOD
10016 * We use the pre-parsed method if it is known, and store its number as an
10017 * integer. If it is unknown, we use the pointer and the length.
10018 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010019static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010020{
10021 int len, meth;
10022
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010023 len = strlen(text);
10024 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010025
10026 pattern->val.i = meth;
10027 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010028 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010029 pattern->len = len;
10030 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010031 else {
10032 pattern->ptr.str = NULL;
10033 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010034 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010035 return 1;
10036}
10037
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010038/* This function fetches the method of current HTTP request and stores
10039 * it in the global pattern struct as a chunk. There are two possibilities :
10040 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10041 * in <len> and <ptr> is NULL ;
10042 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10043 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010044 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010045 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010046static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010047smp_fetch_meth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010048 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010049{
10050 int meth;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010051 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010052
Willy Tarreau24e32d82012-04-23 23:55:44 +020010053 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010054
Willy Tarreau8797c062007-05-07 00:55:35 +020010055 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010056 smp->type = SMP_T_METH;
10057 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010058 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010059 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10060 /* ensure the indexes are not affected */
10061 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010062 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010063 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10064 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010065 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010066 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010067 return 1;
10068}
10069
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010070/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010071static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010072{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010073 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010074 struct pattern_list *lst;
10075 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010076
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010077 list_for_each_entry(lst, &expr->patterns, list) {
10078 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010079
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010080 /* well-known method */
10081 if (pattern->val.i != HTTP_METH_OTHER) {
10082 if (smp->data.meth.meth == pattern->val.i)
10083 return pattern;
10084 else
10085 continue;
10086 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010087
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010088 /* Other method, we must compare the strings */
10089 if (pattern->len != smp->data.meth.str.len)
10090 continue;
10091
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010092 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010093 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10094 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010095 return pattern;
10096 }
10097 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010098}
10099
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010100static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010101smp_fetch_rqver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010102 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010103{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010104 struct http_txn *txn = strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010105 char *ptr;
10106 int len;
10107
Willy Tarreauc0239e02012-04-16 14:42:55 +020010108 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010109
Willy Tarreau8797c062007-05-07 00:55:35 +020010110 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010111 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010112
10113 while ((len-- > 0) && (*ptr++ != '/'));
10114 if (len <= 0)
10115 return 0;
10116
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010117 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010118 smp->data.str.str = ptr;
10119 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010120
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010121 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010122 return 1;
10123}
10124
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010125static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010126smp_fetch_stver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010127 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010128{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010129 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010130 char *ptr;
10131 int len;
10132
Willy Tarreauc0239e02012-04-16 14:42:55 +020010133 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010134
Willy Tarreau15e91e12015-04-04 00:52:09 +020010135 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010136 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10137 return 0;
10138
Willy Tarreau8797c062007-05-07 00:55:35 +020010139 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010140 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010141
10142 while ((len-- > 0) && (*ptr++ != '/'));
10143 if (len <= 0)
10144 return 0;
10145
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010146 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010147 smp->data.str.str = ptr;
10148 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010149
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010150 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010151 return 1;
10152}
10153
10154/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010155static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010156smp_fetch_stcode(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010157 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010158{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010159 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010160 char *ptr;
10161 int len;
10162
Willy Tarreauc0239e02012-04-16 14:42:55 +020010163 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010164
Willy Tarreau15e91e12015-04-04 00:52:09 +020010165 txn = strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010166 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10167 return 0;
10168
Willy Tarreau8797c062007-05-07 00:55:35 +020010169 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010170 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010171
Willy Tarreauf853c462012-04-23 18:53:56 +020010172 smp->type = SMP_T_UINT;
10173 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010174 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010175 return 1;
10176}
10177
10178/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010179static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010180smp_fetch_url(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010181 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010182{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010183 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010184
Willy Tarreauc0239e02012-04-16 14:42:55 +020010185 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010186
Willy Tarreau15e91e12015-04-04 00:52:09 +020010187 txn = strm->txn;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010188 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010189 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010190 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010191 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010192 return 1;
10193}
10194
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010195static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010196smp_fetch_url_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010197 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010198{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010199 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010200 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010201
Willy Tarreauc0239e02012-04-16 14:42:55 +020010202 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010203
Willy Tarreau15e91e12015-04-04 00:52:09 +020010204 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010205 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 +020010206 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010207 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010208
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010209 smp->type = SMP_T_IPV4;
10210 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010211 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010212 return 1;
10213}
10214
10215static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010216smp_fetch_url_port(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010217 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010218{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010219 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010220 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010221
Willy Tarreauc0239e02012-04-16 14:42:55 +020010222 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010223
Willy Tarreau15e91e12015-04-04 00:52:09 +020010224 txn = strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010225 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 +020010226 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10227 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010228
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010229 smp->type = SMP_T_UINT;
10230 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010231 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010232 return 1;
10233}
10234
Willy Tarreau185b5c42012-04-26 15:11:51 +020010235/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10236 * Accepts an optional argument of type string containing the header field name,
10237 * and an optional argument of type signed or unsigned integer to request an
10238 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010239 * headers are considered from the first one. It does not stop on commas and
10240 * returns full lines instead (useful for User-Agent or Date for example).
10241 */
10242static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010243smp_fetch_fhdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010244 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010245{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010246 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010247 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010248 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010249 int occ = 0;
10250 const char *name_str = NULL;
10251 int name_len = 0;
10252
10253 if (!ctx) {
10254 /* first call */
10255 ctx = &static_hdr_ctx;
10256 ctx->idx = 0;
10257 smp->ctx.a[0] = ctx;
10258 }
10259
10260 if (args) {
10261 if (args[0].type != ARGT_STR)
10262 return 0;
10263 name_str = args[0].data.str.str;
10264 name_len = args[0].data.str.len;
10265
10266 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10267 occ = args[1].data.uint;
10268 }
10269
10270 CHECK_HTTP_MESSAGE_FIRST();
10271
Willy Tarreau15e91e12015-04-04 00:52:09 +020010272 idx = &strm->txn->hdr_idx;
10273 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10274
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010275 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10276 /* search for header from the beginning */
10277 ctx->idx = 0;
10278
10279 if (!occ && !(opt & SMP_OPT_ITERATE))
10280 /* no explicit occurrence and single fetch => last header by default */
10281 occ = -1;
10282
10283 if (!occ)
10284 /* prepare to report multiple occurrences for ACL fetches */
10285 smp->flags |= SMP_F_NOT_LAST;
10286
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010287 smp->type = SMP_T_STR;
10288 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010289 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10290 return 1;
10291
10292 smp->flags &= ~SMP_F_NOT_LAST;
10293 return 0;
10294}
10295
10296/* 6. Check on HTTP header count. The number of occurrences is returned.
10297 * Accepts exactly 1 argument of type string. It does not stop on commas and
10298 * returns full lines instead (useful for User-Agent or Date for example).
10299 */
10300static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010301smp_fetch_fhdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010302 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010303{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010304 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010305 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010306 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010307 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010308 const char *name = NULL;
10309 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010310
Willy Tarreau601a4d12015-04-01 19:16:09 +020010311 if (args && args->type == ARGT_STR) {
10312 name = args->data.str.str;
10313 len = args->data.str.len;
10314 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010315
10316 CHECK_HTTP_MESSAGE_FIRST();
10317
Willy Tarreau15e91e12015-04-04 00:52:09 +020010318 idx = &strm->txn->hdr_idx;
10319 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10320
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010321 ctx.idx = 0;
10322 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010323 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010324 cnt++;
10325
10326 smp->type = SMP_T_UINT;
10327 smp->data.uint = cnt;
10328 smp->flags = SMP_F_VOL_HDR;
10329 return 1;
10330}
10331
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010332static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010333smp_fetch_hdr_names(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010334 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010335{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010336 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010337 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010338 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010339 struct chunk *temp;
10340 char del = ',';
10341
10342 if (args && args->type == ARGT_STR)
10343 del = *args[0].data.str.str;
10344
10345 CHECK_HTTP_MESSAGE_FIRST();
10346
Willy Tarreau15e91e12015-04-04 00:52:09 +020010347 idx = &strm->txn->hdr_idx;
10348 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10349
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010350 temp = get_trash_chunk();
10351
10352 ctx.idx = 0;
10353 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10354 if (temp->len)
10355 temp->str[temp->len++] = del;
10356 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10357 temp->len += ctx.del;
10358 }
10359
10360 smp->type = SMP_T_STR;
10361 smp->data.str.str = temp->str;
10362 smp->data.str.len = temp->len;
10363 smp->flags = SMP_F_VOL_HDR;
10364 return 1;
10365}
10366
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010367/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10368 * Accepts an optional argument of type string containing the header field name,
10369 * and an optional argument of type signed or unsigned integer to request an
10370 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010371 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010372 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010373static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010374smp_fetch_hdr(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010375 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010376{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010377 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010378 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010379 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010380 int occ = 0;
10381 const char *name_str = NULL;
10382 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010383
Willy Tarreaua890d072013-04-02 12:01:06 +020010384 if (!ctx) {
10385 /* first call */
10386 ctx = &static_hdr_ctx;
10387 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010388 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010389 }
10390
Willy Tarreau185b5c42012-04-26 15:11:51 +020010391 if (args) {
10392 if (args[0].type != ARGT_STR)
10393 return 0;
10394 name_str = args[0].data.str.str;
10395 name_len = args[0].data.str.len;
10396
10397 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10398 occ = args[1].data.uint;
10399 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010400
Willy Tarreaue333ec92012-04-16 16:26:40 +020010401 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010402
Willy Tarreau15e91e12015-04-04 00:52:09 +020010403 idx = &strm->txn->hdr_idx;
10404 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10405
Willy Tarreau185b5c42012-04-26 15:11:51 +020010406 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010407 /* search for header from the beginning */
10408 ctx->idx = 0;
10409
Willy Tarreau185b5c42012-04-26 15:11:51 +020010410 if (!occ && !(opt & SMP_OPT_ITERATE))
10411 /* no explicit occurrence and single fetch => last header by default */
10412 occ = -1;
10413
10414 if (!occ)
10415 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010416 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010417
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010418 smp->type = SMP_T_STR;
10419 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010420 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 +020010421 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010422
Willy Tarreau37406352012-04-23 16:16:37 +020010423 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010424 return 0;
10425}
10426
Willy Tarreauc11416f2007-06-17 16:58:38 +020010427/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010428 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010429 */
10430static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010431smp_fetch_hdr_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010432 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010433{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010434 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010435 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010436 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010437 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010438 const char *name = NULL;
10439 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010440
Willy Tarreau601a4d12015-04-01 19:16:09 +020010441 if (args && args->type == ARGT_STR) {
10442 name = args->data.str.str;
10443 len = args->data.str.len;
10444 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010445
Willy Tarreaue333ec92012-04-16 16:26:40 +020010446 CHECK_HTTP_MESSAGE_FIRST();
10447
Willy Tarreau15e91e12015-04-04 00:52:09 +020010448 idx = &strm->txn->hdr_idx;
10449 msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &strm->txn->req : &strm->txn->rsp;
10450
Willy Tarreau33a7e692007-06-10 19:45:56 +020010451 ctx.idx = 0;
10452 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010453 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010454 cnt++;
10455
Willy Tarreauf853c462012-04-23 18:53:56 +020010456 smp->type = SMP_T_UINT;
10457 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010458 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010459 return 1;
10460}
10461
Willy Tarreau185b5c42012-04-26 15:11:51 +020010462/* Fetch an HTTP header's integer value. The integer value is returned. It
10463 * takes a mandatory argument of type string and an optional one of type int
10464 * to designate a specific occurrence. It returns an unsigned integer, which
10465 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010466 */
10467static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010468smp_fetch_hdr_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010469 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010470{
Willy Tarreau192252e2015-04-04 01:47:55 +020010471 int ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010472
Willy Tarreauf853c462012-04-23 18:53:56 +020010473 if (ret > 0) {
10474 smp->type = SMP_T_UINT;
10475 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10476 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010477
Willy Tarreaud53e2422012-04-16 17:21:11 +020010478 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010479}
10480
Cyril Bonté69fa9922012-10-25 00:01:06 +020010481/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10482 * and an optional one of type int to designate a specific occurrence.
10483 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010484 */
10485static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010486smp_fetch_hdr_ip(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010487 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010488{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010489 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010490
Willy Tarreau192252e2015-04-04 01:47:55 +020010491 while ((ret = smp_fetch_hdr(px, sess, strm, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010492 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10493 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010494 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010495 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010496 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010497 if (smp->data.str.len < temp->size - 1) {
10498 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10499 temp->str[smp->data.str.len] = '\0';
10500 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10501 smp->type = SMP_T_IPV6;
10502 break;
10503 }
10504 }
10505 }
10506
Willy Tarreaud53e2422012-04-16 17:21:11 +020010507 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010508 if (!(smp->flags & SMP_F_NOT_LAST))
10509 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010510 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010511 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010512}
10513
Willy Tarreau737b0c12007-06-10 21:28:46 +020010514/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10515 * the first '/' after the possible hostname, and ends before the possible '?'.
10516 */
10517static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010518smp_fetch_path(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010519 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010520{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010521 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010522 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010523
Willy Tarreauc0239e02012-04-16 14:42:55 +020010524 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010525
Willy Tarreau15e91e12015-04-04 00:52:09 +020010526 txn = strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010527 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010528 ptr = http_get_path(txn);
10529 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010530 return 0;
10531
10532 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010533 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010534 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010535
10536 while (ptr < end && *ptr != '?')
10537 ptr++;
10538
Willy Tarreauf853c462012-04-23 18:53:56 +020010539 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010540 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010541 return 1;
10542}
10543
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010544/* This produces a concatenation of the first occurrence of the Host header
10545 * followed by the path component if it begins with a slash ('/'). This means
10546 * that '*' will not be added, resulting in exactly the first Host entry.
10547 * If no Host header is found, then the path is returned as-is. The returned
10548 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010549 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010550 */
10551static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010552smp_fetch_base(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010553 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010554{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010555 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010556 char *ptr, *end, *beg;
10557 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010558 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010559
10560 CHECK_HTTP_MESSAGE_FIRST();
10561
Willy Tarreau15e91e12015-04-04 00:52:09 +020010562 txn = strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010563 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010564 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreau192252e2015-04-04 01:47:55 +020010565 return smp_fetch_path(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010566
10567 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010568 temp = get_trash_chunk();
10569 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010570 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010571 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010572 smp->data.str.len = ctx.vlen;
10573
10574 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010575 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010576 beg = http_get_path(txn);
10577 if (!beg)
10578 beg = end;
10579
10580 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10581
10582 if (beg < ptr && *beg == '/') {
10583 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10584 smp->data.str.len += ptr - beg;
10585 }
10586
10587 smp->flags = SMP_F_VOL_1ST;
10588 return 1;
10589}
10590
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010591/* This produces a 32-bit hash of the concatenation of the first occurrence of
10592 * the Host header followed by the path component if it begins with a slash ('/').
10593 * This means that '*' will not be added, resulting in exactly the first Host
10594 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010595 * is hashed using the path hash followed by a full avalanche hash and provides a
10596 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010597 * high-traffic sites without having to store whole paths.
10598 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010599int
Willy Tarreau192252e2015-04-04 01:47:55 +020010600smp_fetch_base32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010601 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010602{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010603 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010604 struct hdr_ctx ctx;
10605 unsigned int hash = 0;
10606 char *ptr, *beg, *end;
10607 int len;
10608
10609 CHECK_HTTP_MESSAGE_FIRST();
10610
Willy Tarreau15e91e12015-04-04 00:52:09 +020010611 txn = strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010612 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010613 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010614 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10615 ptr = ctx.line + ctx.val;
10616 len = ctx.vlen;
10617 while (len--)
10618 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10619 }
10620
10621 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010622 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010623 beg = http_get_path(txn);
10624 if (!beg)
10625 beg = end;
10626
10627 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10628
10629 if (beg < ptr && *beg == '/') {
10630 while (beg < ptr)
10631 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10632 }
10633 hash = full_hash(hash);
10634
10635 smp->type = SMP_T_UINT;
10636 smp->data.uint = hash;
10637 smp->flags = SMP_F_VOL_1ST;
10638 return 1;
10639}
10640
Willy Tarreau4a550602012-12-09 14:53:32 +010010641/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010642 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10643 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10644 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010645 * that in environments where IPv6 is insignificant, truncating the output to
10646 * 8 bytes would still work.
10647 */
10648static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010649smp_fetch_base32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010650 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010651{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010652 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020010653 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010654
10655 if (!cli_conn)
10656 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010657
Willy Tarreau192252e2015-04-04 01:47:55 +020010658 if (!smp_fetch_base32(px, sess, strm, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010659 return 0;
10660
Willy Tarreau47ca5452012-12-23 20:22:19 +010010661 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010662 *(unsigned int *)temp->str = htonl(smp->data.uint);
10663 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010664
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010665 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010666 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010667 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010668 temp->len += 4;
10669 break;
10670 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010671 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010672 temp->len += 16;
10673 break;
10674 default:
10675 return 0;
10676 }
10677
10678 smp->data.str = *temp;
10679 smp->type = SMP_T_BIN;
10680 return 1;
10681}
10682
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010683/* Extracts the query string, which comes after the question mark '?'. If no
10684 * question mark is found, nothing is returned. Otherwise it returns a sample
10685 * of type string carrying the whole query string.
10686 */
10687static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010688smp_fetch_query(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010689 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010690{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010691 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010692 char *ptr, *end;
10693
10694 CHECK_HTTP_MESSAGE_FIRST();
10695
Willy Tarreau15e91e12015-04-04 00:52:09 +020010696 txn = strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010697 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10698 end = ptr + txn->req.sl.rq.u_l;
10699
10700 /* look up the '?' */
10701 do {
10702 if (ptr == end)
10703 return 0;
10704 } while (*ptr++ != '?');
10705
10706 smp->type = SMP_T_STR;
10707 smp->data.str.str = ptr;
10708 smp->data.str.len = end - ptr;
10709 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10710 return 1;
10711}
10712
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010713static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010714smp_fetch_proto_http(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010715 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010716{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010717 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10718 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10719 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010720
Willy Tarreau24e32d82012-04-23 23:55:44 +020010721 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010722
Willy Tarreauf853c462012-04-23 18:53:56 +020010723 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010724 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010725 return 1;
10726}
10727
Willy Tarreau7f18e522010-10-22 20:04:13 +020010728/* return a valid test if the current request is the first one on the connection */
10729static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010730smp_fetch_http_first_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010731 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010732{
Willy Tarreauf853c462012-04-23 18:53:56 +020010733 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010734 smp->data.uint = !(strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010735 return 1;
10736}
10737
Willy Tarreau34db1082012-04-19 17:16:54 +020010738/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010739static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010740smp_fetch_http_auth(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010741 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010742{
10743
Willy Tarreau24e32d82012-04-23 23:55:44 +020010744 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010745 return 0;
10746
Willy Tarreauc0239e02012-04-16 14:42:55 +020010747 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010748
Willy Tarreau15e91e12015-04-04 00:52:09 +020010749 if (!get_http_auth(strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010750 return 0;
10751
Willy Tarreauf853c462012-04-23 18:53:56 +020010752 smp->type = SMP_T_BOOL;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010753 smp->data.uint = check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010754 return 1;
10755}
Willy Tarreau8797c062007-05-07 00:55:35 +020010756
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010757/* Accepts exactly 1 argument of type userlist */
10758static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010759smp_fetch_http_auth_grp(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010760 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010761{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010762 if (!args || args->type != ARGT_USR)
10763 return 0;
10764
10765 CHECK_HTTP_MESSAGE_FIRST();
10766
Willy Tarreau15e91e12015-04-04 00:52:09 +020010767 if (!get_http_auth(strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010768 return 0;
10769
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010770 /* if the user does not belong to the userlist or has a wrong password,
10771 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010772 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010773 */
Willy Tarreau15e91e12015-04-04 00:52:09 +020010774 if (!check_user(args->data.usr, strm->txn->auth.user, strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010775 return 0;
10776
10777 /* pat_match_auth() will need the user list */
10778 smp->ctx.a[0] = args->data.usr;
10779
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010780 smp->type = SMP_T_STR;
10781 smp->flags = SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010782 smp->data.str.str = strm->txn->auth.user;
10783 smp->data.str.len = strlen(strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010784
10785 return 1;
10786}
10787
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010788/* Try to find the next occurrence of a cookie name in a cookie header value.
10789 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10790 * the cookie value is returned into *value and *value_l, and the function
10791 * returns a pointer to the next pointer to search from if the value was found.
10792 * Otherwise if the cookie was not found, NULL is returned and neither value
10793 * nor value_l are touched. The input <hdr> string should first point to the
10794 * header's value, and the <hdr_end> pointer must point to the first character
10795 * not part of the value. <list> must be non-zero if value may represent a list
10796 * of values (cookie headers). This makes it faster to abort parsing when no
10797 * list is expected.
10798 */
10799static char *
10800extract_cookie_value(char *hdr, const char *hdr_end,
10801 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010802 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010803{
10804 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10805 char *next;
10806
10807 /* we search at least a cookie name followed by an equal, and more
10808 * generally something like this :
10809 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10810 */
10811 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10812 /* Iterate through all cookies on this line */
10813
10814 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10815 att_beg++;
10816
10817 /* find att_end : this is the first character after the last non
10818 * space before the equal. It may be equal to hdr_end.
10819 */
10820 equal = att_end = att_beg;
10821
10822 while (equal < hdr_end) {
10823 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10824 break;
10825 if (http_is_spht[(unsigned char)*equal++])
10826 continue;
10827 att_end = equal;
10828 }
10829
10830 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10831 * is between <att_beg> and <equal>, both may be identical.
10832 */
10833
10834 /* look for end of cookie if there is an equal sign */
10835 if (equal < hdr_end && *equal == '=') {
10836 /* look for the beginning of the value */
10837 val_beg = equal + 1;
10838 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10839 val_beg++;
10840
10841 /* find the end of the value, respecting quotes */
10842 next = find_cookie_value_end(val_beg, hdr_end);
10843
10844 /* make val_end point to the first white space or delimitor after the value */
10845 val_end = next;
10846 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10847 val_end--;
10848 } else {
10849 val_beg = val_end = next = equal;
10850 }
10851
10852 /* We have nothing to do with attributes beginning with '$'. However,
10853 * they will automatically be removed if a header before them is removed,
10854 * since they're supposed to be linked together.
10855 */
10856 if (*att_beg == '$')
10857 continue;
10858
10859 /* Ignore cookies with no equal sign */
10860 if (equal == next)
10861 continue;
10862
10863 /* Now we have the cookie name between att_beg and att_end, and
10864 * its value between val_beg and val_end.
10865 */
10866
10867 if (att_end - att_beg == cookie_name_l &&
10868 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10869 /* let's return this value and indicate where to go on from */
10870 *value = val_beg;
10871 *value_l = val_end - val_beg;
10872 return next + 1;
10873 }
10874
10875 /* Set-Cookie headers only have the name in the first attr=value part */
10876 if (!list)
10877 break;
10878 }
10879
10880 return NULL;
10881}
10882
William Lallemanda43ba4e2014-01-28 18:14:25 +010010883/* Fetch a captured HTTP request header. The index is the position of
10884 * the "capture" option in the configuration file
10885 */
10886static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010887smp_fetch_capture_header_req(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010888 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010889{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020010890 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010891 int idx;
10892
10893 if (!args || args->type != ARGT_UINT)
10894 return 0;
10895
10896 idx = args->data.uint;
10897
Willy Tarreau15e91e12015-04-04 00:52:09 +020010898 if (idx > (fe->nb_req_cap - 1) || strm->req_cap == NULL || strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010899 return 0;
10900
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010901 smp->type = SMP_T_STR;
10902 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010903 smp->data.str.str = strm->req_cap[idx];
10904 smp->data.str.len = strlen(strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010905
10906 return 1;
10907}
10908
10909/* Fetch a captured HTTP response header. The index is the position of
10910 * the "capture" option in the configuration file
10911 */
10912static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010913smp_fetch_capture_header_res(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010914 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010915{
Willy Tarreaud0d8da92015-04-04 02:10:38 +020010916 struct proxy *fe = strm_fe(strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010917 int idx;
10918
10919 if (!args || args->type != ARGT_UINT)
10920 return 0;
10921
10922 idx = args->data.uint;
10923
Willy Tarreau15e91e12015-04-04 00:52:09 +020010924 if (idx > (fe->nb_rsp_cap - 1) || strm->res_cap == NULL || strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010925 return 0;
10926
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010927 smp->type = SMP_T_STR;
10928 smp->flags |= SMP_F_CONST;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010929 smp->data.str.str = strm->res_cap[idx];
10930 smp->data.str.len = strlen(strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010931
10932 return 1;
10933}
10934
William Lallemand65ad6e12014-01-31 15:08:02 +010010935/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10936static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010937smp_fetch_capture_req_method(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010938 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010939{
10940 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010941 struct http_txn *txn = strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010942 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010943
Willy Tarreau15e91e12015-04-04 00:52:09 +020010944 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010945 return 0;
10946
William Lallemand96a77852014-02-05 00:30:02 +010010947 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010948
William Lallemand96a77852014-02-05 00:30:02 +010010949 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10950 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010951
William Lallemand96a77852014-02-05 00:30:02 +010010952 temp = get_trash_chunk();
10953 temp->str = txn->uri;
10954 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010955 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010956 smp->type = SMP_T_STR;
10957 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010958
10959 return 1;
10960
10961}
10962
10963/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10964static int
Willy Tarreau192252e2015-04-04 01:47:55 +020010965smp_fetch_capture_req_uri(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020010966 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010967{
10968 struct chunk *temp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010969 struct http_txn *txn = strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010970 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010971
Willy Tarreau15e91e12015-04-04 00:52:09 +020010972 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010973 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010974
William Lallemand65ad6e12014-01-31 15:08:02 +010010975 ptr = txn->uri;
10976
10977 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10978 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010979
William Lallemand65ad6e12014-01-31 15:08:02 +010010980 if (!*ptr)
10981 return 0;
10982
10983 ptr++; /* skip the space */
10984
10985 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010986 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010987 if (!ptr)
10988 return 0;
10989 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10990 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010991
10992 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010993 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010994 smp->type = SMP_T_STR;
10995 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010996
10997 return 1;
10998}
10999
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011000/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11001 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11002 */
11003static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011004smp_fetch_capture_req_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011005 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011006{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011007 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011008
Willy Tarreau15e91e12015-04-04 00:52:09 +020011009 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011010 return 0;
11011
11012 if (txn->req.flags & HTTP_MSGF_VER_11)
11013 smp->data.str.str = "HTTP/1.1";
11014 else
11015 smp->data.str.str = "HTTP/1.0";
11016
11017 smp->data.str.len = 8;
11018 smp->type = SMP_T_STR;
11019 smp->flags = SMP_F_CONST;
11020 return 1;
11021
11022}
11023
11024/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11025 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11026 */
11027static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011028smp_fetch_capture_res_ver(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011029 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011030{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011031 struct http_txn *txn = strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011032
Willy Tarreau15e91e12015-04-04 00:52:09 +020011033 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011034 return 0;
11035
11036 if (txn->rsp.flags & HTTP_MSGF_VER_11)
11037 smp->data.str.str = "HTTP/1.1";
11038 else
11039 smp->data.str.str = "HTTP/1.0";
11040
11041 smp->data.str.len = 8;
11042 smp->type = SMP_T_STR;
11043 smp->flags = SMP_F_CONST;
11044 return 1;
11045
11046}
11047
William Lallemand65ad6e12014-01-31 15:08:02 +010011048
Willy Tarreaue333ec92012-04-16 16:26:40 +020011049/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011050 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011051 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011052 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011053 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011054 * Accepts exactly 1 argument of type string. If the input options indicate
11055 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011056 * The returned sample is of type CSTR. Can be used to parse cookies in other
11057 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011058 */
Willy Tarreau192252e2015-04-04 01:47:55 +020011059int smp_fetch_cookie(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011060 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011061{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011062 struct http_txn *txn;
11063 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011064 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011065 const struct http_msg *msg;
11066 const char *hdr_name;
11067 int hdr_name_len;
11068 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011069 int occ = 0;
11070 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011071
Willy Tarreau24e32d82012-04-23 23:55:44 +020011072 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011073 return 0;
11074
Willy Tarreaua890d072013-04-02 12:01:06 +020011075 if (!ctx) {
11076 /* first call */
11077 ctx = &static_hdr_ctx;
11078 ctx->idx = 0;
11079 smp->ctx.a[2] = ctx;
11080 }
11081
Willy Tarreaue333ec92012-04-16 16:26:40 +020011082 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011083
Willy Tarreau15e91e12015-04-04 00:52:09 +020011084 txn = strm->txn;
11085 idx = &strm->txn->hdr_idx;
11086
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011087 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011088 msg = &txn->req;
11089 hdr_name = "Cookie";
11090 hdr_name_len = 6;
11091 } else {
11092 msg = &txn->rsp;
11093 hdr_name = "Set-Cookie";
11094 hdr_name_len = 10;
11095 }
11096
Willy Tarreau28376d62012-04-26 21:26:10 +020011097 if (!occ && !(opt & SMP_OPT_ITERATE))
11098 /* no explicit occurrence and single fetch => last cookie by default */
11099 occ = -1;
11100
11101 /* OK so basically here, either we want only one value and it's the
11102 * last one, or we want to iterate over all of them and we fetch the
11103 * next one.
11104 */
11105
Willy Tarreau9b28e032012-10-12 23:49:43 +020011106 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011107 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011108 /* search for the header from the beginning, we must first initialize
11109 * the search parameters.
11110 */
Willy Tarreau37406352012-04-23 16:16:37 +020011111 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011112 ctx->idx = 0;
11113 }
11114
Willy Tarreau28376d62012-04-26 21:26:10 +020011115 smp->flags |= SMP_F_VOL_HDR;
11116
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011117 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011118 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11119 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011120 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11121 goto out;
11122
Willy Tarreau24e32d82012-04-23 23:55:44 +020011123 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011124 continue;
11125
Willy Tarreau37406352012-04-23 16:16:37 +020011126 smp->ctx.a[0] = ctx->line + ctx->val;
11127 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011128 }
11129
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011130 smp->type = SMP_T_STR;
11131 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011132 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011133 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011134 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011135 &smp->data.str.str,
11136 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011137 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011138 found = 1;
11139 if (occ >= 0) {
11140 /* one value was returned into smp->data.str.{str,len} */
11141 smp->flags |= SMP_F_NOT_LAST;
11142 return 1;
11143 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011144 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011145 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011146 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011147 /* all cookie headers and values were scanned. If we're looking for the
11148 * last occurrence, we may return it now.
11149 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011150 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011151 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011152 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011153}
11154
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011155/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011156 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011157 * multiple cookies may be parsed on the same line. The returned sample is of
11158 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011159 */
11160static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011161smp_fetch_cookie_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011162 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011163{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011164 struct http_txn *txn;
11165 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011166 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011167 const struct http_msg *msg;
11168 const char *hdr_name;
11169 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011170 int cnt;
11171 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011172 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011173
Willy Tarreau24e32d82012-04-23 23:55:44 +020011174 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011175 return 0;
11176
Willy Tarreaue333ec92012-04-16 16:26:40 +020011177 CHECK_HTTP_MESSAGE_FIRST();
11178
Willy Tarreau15e91e12015-04-04 00:52:09 +020011179 txn = strm->txn;
11180 idx = &strm->txn->hdr_idx;
11181
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011182 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011183 msg = &txn->req;
11184 hdr_name = "Cookie";
11185 hdr_name_len = 6;
11186 } else {
11187 msg = &txn->rsp;
11188 hdr_name = "Set-Cookie";
11189 hdr_name_len = 10;
11190 }
11191
Willy Tarreau9b28e032012-10-12 23:49:43 +020011192 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011193 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011194 ctx.idx = 0;
11195 cnt = 0;
11196
11197 while (1) {
11198 /* Note: val_beg == NULL every time we need to fetch a new header */
11199 if (!val_beg) {
11200 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11201 break;
11202
Willy Tarreau24e32d82012-04-23 23:55:44 +020011203 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011204 continue;
11205
11206 val_beg = ctx.line + ctx.val;
11207 val_end = val_beg + ctx.vlen;
11208 }
11209
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011210 smp->type = SMP_T_STR;
11211 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011212 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011213 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011214 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011215 &smp->data.str.str,
11216 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011217 cnt++;
11218 }
11219 }
11220
Willy Tarreaub169eba2013-12-16 15:14:43 +010011221 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011222 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011223 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011224 return 1;
11225}
11226
Willy Tarreau51539362012-05-08 12:46:28 +020011227/* Fetch an cookie's integer value. The integer value is returned. It
11228 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11229 */
11230static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011231smp_fetch_cookie_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011232 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011233{
Willy Tarreau192252e2015-04-04 01:47:55 +020011234 int ret = smp_fetch_cookie(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011235
11236 if (ret > 0) {
11237 smp->type = SMP_T_UINT;
11238 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11239 }
11240
11241 return ret;
11242}
11243
Willy Tarreau8797c062007-05-07 00:55:35 +020011244/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011245/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011246/************************************************************************/
11247
David Cournapeau16023ee2010-12-23 20:55:41 +090011248/*
11249 * Given a path string and its length, find the position of beginning of the
11250 * query string. Returns NULL if no query string is found in the path.
11251 *
11252 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11253 *
11254 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11255 */
bedis4c75cca2012-10-05 08:38:24 +020011256static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011257{
11258 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011259
bedis4c75cca2012-10-05 08:38:24 +020011260 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011261 return p ? p + 1 : NULL;
11262}
11263
bedis4c75cca2012-10-05 08:38:24 +020011264static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011265{
bedis4c75cca2012-10-05 08:38:24 +020011266 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011267}
11268
11269/*
11270 * Given a url parameter, find the starting position of the first occurence,
11271 * or NULL if the parameter is not found.
11272 *
11273 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11274 * the function will return query_string+8.
11275 */
11276static char*
11277find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011278 char* url_param_name, size_t url_param_name_l,
11279 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011280{
11281 char *pos, *last;
11282
11283 pos = query_string;
11284 last = query_string + query_string_l - url_param_name_l - 1;
11285
11286 while (pos <= last) {
11287 if (pos[url_param_name_l] == '=') {
11288 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11289 return pos;
11290 pos += url_param_name_l + 1;
11291 }
bedis4c75cca2012-10-05 08:38:24 +020011292 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011293 pos++;
11294 pos++;
11295 }
11296 return NULL;
11297}
11298
11299/*
11300 * Given a url parameter name, returns its value and size into *value and
11301 * *value_l respectively, and returns non-zero. If the parameter is not found,
11302 * zero is returned and value/value_l are not touched.
11303 */
11304static int
11305find_url_param_value(char* path, size_t path_l,
11306 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011307 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011308{
11309 char *query_string, *qs_end;
11310 char *arg_start;
11311 char *value_start, *value_end;
11312
bedis4c75cca2012-10-05 08:38:24 +020011313 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011314 if (!query_string)
11315 return 0;
11316
11317 qs_end = path + path_l;
11318 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011319 url_param_name, url_param_name_l,
11320 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011321 if (!arg_start)
11322 return 0;
11323
11324 value_start = arg_start + url_param_name_l + 1;
11325 value_end = value_start;
11326
bedis4c75cca2012-10-05 08:38:24 +020011327 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011328 value_end++;
11329
11330 *value = value_start;
11331 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011332 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011333}
11334
11335static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011336smp_fetch_url_param(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011337 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011338{
bedis4c75cca2012-10-05 08:38:24 +020011339 char delim = '?';
Willy Tarreau15e91e12015-04-04 00:52:09 +020011340 struct http_msg *msg;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011341
bedis4c75cca2012-10-05 08:38:24 +020011342 if (!args || args[0].type != ARGT_STR ||
11343 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011344 return 0;
11345
11346 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011347
Willy Tarreau15e91e12015-04-04 00:52:09 +020011348 msg = &strm->txn->req;
11349
bedis4c75cca2012-10-05 08:38:24 +020011350 if (args[1].type)
11351 delim = *args[1].data.str.str;
11352
Willy Tarreau9b28e032012-10-12 23:49:43 +020011353 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011354 args->data.str.str, args->data.str.len,
11355 &smp->data.str.str, &smp->data.str.len,
11356 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011357 return 0;
11358
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011359 smp->type = SMP_T_STR;
11360 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011361 return 1;
11362}
11363
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011364/* Return the signed integer value for the specified url parameter (see url_param
11365 * above).
11366 */
11367static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011368smp_fetch_url_param_val(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011369 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011370{
Willy Tarreau192252e2015-04-04 01:47:55 +020011371 int ret = smp_fetch_url_param(px, sess, strm, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011372
11373 if (ret > 0) {
11374 smp->type = SMP_T_UINT;
11375 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11376 }
11377
11378 return ret;
11379}
11380
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011381/* This produces a 32-bit hash of the concatenation of the first occurrence of
11382 * the Host header followed by the path component if it begins with a slash ('/').
11383 * This means that '*' will not be added, resulting in exactly the first Host
11384 * entry. If no Host header is found, then the path is used. The resulting value
11385 * is hashed using the url hash followed by a full avalanche hash and provides a
11386 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11387 * high-traffic sites without having to store whole paths.
11388 * this differs from the base32 functions in that it includes the url parameters
11389 * as well as the path
11390 */
11391static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011392smp_fetch_url32(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011393 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011394{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011395 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011396 struct hdr_ctx ctx;
11397 unsigned int hash = 0;
11398 char *ptr, *beg, *end;
11399 int len;
11400
11401 CHECK_HTTP_MESSAGE_FIRST();
11402
Willy Tarreau15e91e12015-04-04 00:52:09 +020011403 txn = strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011404 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011405 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011406 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11407 ptr = ctx.line + ctx.val;
11408 len = ctx.vlen;
11409 while (len--)
11410 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11411 }
11412
11413 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011414 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 +000011415 beg = http_get_path(txn);
11416 if (!beg)
11417 beg = end;
11418
11419 for (ptr = beg; ptr < end ; ptr++);
11420
11421 if (beg < ptr && *beg == '/') {
11422 while (beg < ptr)
11423 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11424 }
11425 hash = full_hash(hash);
11426
11427 smp->type = SMP_T_UINT;
11428 smp->data.uint = hash;
11429 smp->flags = SMP_F_VOL_1ST;
11430 return 1;
11431}
11432
11433/* This concatenates the source address with the 32-bit hash of the Host and
11434 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11435 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11436 * on the source address length. The URL hash is stored before the address so
11437 * that in environments where IPv6 is insignificant, truncating the output to
11438 * 8 bytes would still work.
11439 */
11440static int
Willy Tarreau192252e2015-04-04 01:47:55 +020011441smp_fetch_url32_src(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +020011442 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011443{
11444 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020011445 struct connection *cli_conn = objt_conn(sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011446
Willy Tarreau192252e2015-04-04 01:47:55 +020011447 if (!smp_fetch_url32(px, sess, strm, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011448 return 0;
11449
11450 temp = get_trash_chunk();
11451 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11452 temp->len += sizeof(smp->data.uint);
11453
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011454 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011455 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011456 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011457 temp->len += 4;
11458 break;
11459 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011460 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011461 temp->len += 16;
11462 break;
11463 default:
11464 return 0;
11465 }
11466
11467 smp->data.str = *temp;
11468 smp->type = SMP_T_BIN;
11469 return 1;
11470}
11471
Willy Tarreau185b5c42012-04-26 15:11:51 +020011472/* This function is used to validate the arguments passed to any "hdr" fetch
11473 * keyword. These keywords support an optional positive or negative occurrence
11474 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11475 * is assumed that the types are already the correct ones. Returns 0 on error,
11476 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11477 * error message in case of error, that the caller is responsible for freeing.
11478 * The initial location must either be freeable or NULL.
11479 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011480int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011481{
11482 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011483 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011484 return 0;
11485 }
11486 return 1;
11487}
11488
Willy Tarreau276fae92013-07-25 14:36:01 +020011489/* takes an UINT value on input supposed to represent the time since EPOCH,
11490 * adds an optional offset found in args[0] and emits a string representing
11491 * the date in RFC-1123/5322 format.
11492 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011493static int sample_conv_http_date(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011494 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011495{
11496 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11497 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11498 struct chunk *temp;
11499 struct tm *tm;
11500 time_t curr_date = smp->data.uint;
11501
11502 /* add offset */
11503 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11504 curr_date += args[0].data.sint;
11505
11506 tm = gmtime(&curr_date);
11507
11508 temp = get_trash_chunk();
11509 temp->len = snprintf(temp->str, temp->size - temp->len,
11510 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11511 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11512 tm->tm_hour, tm->tm_min, tm->tm_sec);
11513
11514 smp->data.str = *temp;
11515 smp->type = SMP_T_STR;
11516 return 1;
11517}
11518
Thierry FOURNIERad903512014-04-11 17:51:01 +020011519/* Match language range with language tag. RFC2616 14.4:
11520 *
11521 * A language-range matches a language-tag if it exactly equals
11522 * the tag, or if it exactly equals a prefix of the tag such
11523 * that the first tag character following the prefix is "-".
11524 *
11525 * Return 1 if the strings match, else return 0.
11526 */
11527static inline int language_range_match(const char *range, int range_len,
11528 const char *tag, int tag_len)
11529{
11530 const char *end = range + range_len;
11531 const char *tend = tag + tag_len;
11532 while (range < end) {
11533 if (*range == '-' && tag == tend)
11534 return 1;
11535 if (*range != *tag || tag == tend)
11536 return 0;
11537 range++;
11538 tag++;
11539 }
11540 /* Return true only if the last char of the tag is matched. */
11541 return tag == tend;
11542}
11543
11544/* Arguments: The list of expected value, the number of parts returned and the separator */
Willy Tarreau87b09662015-04-03 00:22:06 +020011545static int sample_conv_q_prefered(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011546 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011547{
11548 const char *al = smp->data.str.str;
11549 const char *end = al + smp->data.str.len;
11550 const char *token;
11551 int toklen;
11552 int qvalue;
11553 const char *str;
11554 const char *w;
11555 int best_q = 0;
11556
11557 /* Set the constant to the sample, because the output of the
11558 * function will be peek in the constant configuration string.
11559 */
11560 smp->flags |= SMP_F_CONST;
11561 smp->data.str.size = 0;
11562 smp->data.str.str = "";
11563 smp->data.str.len = 0;
11564
11565 /* Parse the accept language */
11566 while (1) {
11567
11568 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011569 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011570 al++;
11571 if (al >= end)
11572 break;
11573
11574 /* Start of the fisrt word. */
11575 token = al;
11576
11577 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011578 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011579 al++;
11580 if (al == token)
11581 goto expect_comma;
11582
11583 /* Length of the token. */
11584 toklen = al - token;
11585 qvalue = 1000;
11586
11587 /* Check if the token exists in the list. If the token not exists,
11588 * jump to the next token.
11589 */
11590 str = args[0].data.str.str;
11591 w = str;
11592 while (1) {
11593 if (*str == ';' || *str == '\0') {
11594 if (language_range_match(token, toklen, w, str-w))
11595 goto look_for_q;
11596 if (*str == '\0')
11597 goto expect_comma;
11598 w = str + 1;
11599 }
11600 str++;
11601 }
11602 goto expect_comma;
11603
11604look_for_q:
11605
11606 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011607 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011608 al++;
11609 if (al >= end)
11610 goto process_value;
11611
11612 /* If ',' is found, process the result */
11613 if (*al == ',')
11614 goto process_value;
11615
11616 /* If the character is different from ';', look
11617 * for the end of the header part in best effort.
11618 */
11619 if (*al != ';')
11620 goto expect_comma;
11621
11622 /* Assumes that the char is ';', now expect "q=". */
11623 al++;
11624
11625 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011626 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011627 al++;
11628 if (al >= end)
11629 goto process_value;
11630
11631 /* Expect 'q'. If no 'q', continue in best effort */
11632 if (*al != 'q')
11633 goto process_value;
11634 al++;
11635
11636 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011637 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011638 al++;
11639 if (al >= end)
11640 goto process_value;
11641
11642 /* Expect '='. If no '=', continue in best effort */
11643 if (*al != '=')
11644 goto process_value;
11645 al++;
11646
11647 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011648 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011649 al++;
11650 if (al >= end)
11651 goto process_value;
11652
11653 /* Parse the q value. */
11654 qvalue = parse_qvalue(al, &al);
11655
11656process_value:
11657
11658 /* If the new q value is the best q value, then store the associated
11659 * language in the response. If qvalue is the biggest value (1000),
11660 * break the process.
11661 */
11662 if (qvalue > best_q) {
11663 smp->data.str.str = (char *)w;
11664 smp->data.str.len = str - w;
11665 if (qvalue >= 1000)
11666 break;
11667 best_q = qvalue;
11668 }
11669
11670expect_comma:
11671
11672 /* Expect comma or end. If the end is detected, quit the loop. */
11673 while (al < end && *al != ',')
11674 al++;
11675 if (al >= end)
11676 break;
11677
11678 /* Comma is found, jump it and restart the analyzer. */
11679 al++;
11680 }
11681
11682 /* Set default value if required. */
11683 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11684 smp->data.str.str = args[1].data.str.str;
11685 smp->data.str.len = args[1].data.str.len;
11686 }
11687
11688 /* Return true only if a matching language was found. */
11689 return smp->data.str.len != 0;
11690}
11691
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011692/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011693 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011694 * the relevant part of the request line accordingly. Then it updates various
11695 * pointers to the next elements which were moved, and the total buffer length.
11696 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011697 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11698 * error, though this can be revisited when this code is finally exploited.
11699 *
11700 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11701 * query string and 3 to replace uri.
11702 *
11703 * In query string case, the mark question '?' must be set at the start of the
11704 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011705 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011706int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011707 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011708{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011709 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011710 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011711 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011712 int delta;
11713
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011714 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011715 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011716 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011717 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11718
11719 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011720 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011721 txn->req.sl.rq.m_l += delta;
11722 txn->req.sl.rq.u += delta;
11723 txn->req.sl.rq.v += delta;
11724 break;
11725
11726 case 1: // path
11727 cur_ptr = http_get_path(txn);
11728 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011729 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011730
11731 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011732 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 +010011733 cur_end++;
11734
11735 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011736 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011737 txn->req.sl.rq.u_l += delta;
11738 txn->req.sl.rq.v += delta;
11739 break;
11740
11741 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011742 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011743 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011744 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11745 while (cur_ptr < cur_end && *cur_ptr != '?')
11746 cur_ptr++;
11747
11748 /* skip the question mark or indicate that we must insert it
11749 * (but only if the format string is not empty then).
11750 */
11751 if (cur_ptr < cur_end)
11752 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011753 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011754 offset = 0;
11755
11756 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011757 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011758 txn->req.sl.rq.u_l += delta;
11759 txn->req.sl.rq.v += delta;
11760 break;
11761
11762 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011763 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011764 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11765
11766 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011767 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011768 txn->req.sl.rq.u_l += delta;
11769 txn->req.sl.rq.v += delta;
11770 break;
11771
11772 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011773 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011774 }
11775
11776 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011777 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011778 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011779 return 0;
11780}
11781
11782/* This function executes one of the set-{method,path,query,uri} actions. It
11783 * builds a string in the trash from the specified format string. It finds
11784 * the action to be performed in p[2], previously filled by function
11785 * parse_set_req_line(). The replacement action is excuted by the function
11786 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
11787 * the action is canceled, but the rule processing continue.
11788 */
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011789int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011790{
11791 chunk_reset(&trash);
11792
11793 /* If we have to create a query string, prepare a '?'. */
11794 if (*(int *)&rule->arg.act.p[2] == 2)
11795 trash.str[trash.len++] = '?';
11796 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11797
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011798 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011799 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011800}
11801
11802/* parse an http-request action among :
11803 * set-method
11804 * set-path
11805 * set-query
11806 * set-uri
11807 *
11808 * All of them accept a single argument of type string representing a log-format.
11809 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11810 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11811 * It returns 0 on success, < 0 on error.
11812 */
11813int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11814{
11815 int cur_arg = *orig_arg;
11816
11817 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11818
11819 switch (args[0][4]) {
11820 case 'm' :
11821 *(int *)&rule->arg.act.p[2] = 0;
11822 rule->action_ptr = http_action_set_req_line;
11823 break;
11824 case 'p' :
11825 *(int *)&rule->arg.act.p[2] = 1;
11826 rule->action_ptr = http_action_set_req_line;
11827 break;
11828 case 'q' :
11829 *(int *)&rule->arg.act.p[2] = 2;
11830 rule->action_ptr = http_action_set_req_line;
11831 break;
11832 case 'u' :
11833 *(int *)&rule->arg.act.p[2] = 3;
11834 rule->action_ptr = http_action_set_req_line;
11835 break;
11836 default:
11837 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11838 return -1;
11839 }
11840
11841 if (!*args[cur_arg] ||
11842 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11843 memprintf(err, "expects exactly 1 argument <format>");
11844 return -1;
11845 }
11846
11847 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11848 proxy->conf.args.ctx = ARGC_HRQ;
11849 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11850 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11851 proxy->conf.args.file, proxy->conf.args.line);
11852
11853 (*orig_arg)++;
11854 return 0;
11855}
11856
William Lallemand73025dd2014-04-24 14:38:37 +020011857/*
11858 * Return the struct http_req_action_kw associated to a keyword.
11859 */
11860struct http_req_action_kw *action_http_req_custom(const char *kw)
11861{
11862 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11863 struct http_req_action_kw_list *kw_list;
11864 int i;
11865
11866 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11867 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11868 if (!strcmp(kw, kw_list->kw[i].kw))
11869 return &kw_list->kw[i];
11870 }
11871 }
11872 }
11873 return NULL;
11874}
11875
11876/*
11877 * Return the struct http_res_action_kw associated to a keyword.
11878 */
11879struct http_res_action_kw *action_http_res_custom(const char *kw)
11880{
11881 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11882 struct http_res_action_kw_list *kw_list;
11883 int i;
11884
11885 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11886 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11887 if (!strcmp(kw, kw_list->kw[i].kw))
11888 return &kw_list->kw[i];
11889 }
11890 }
11891 }
11892 return NULL;
11893}
11894
Willy Tarreau4a568972010-05-12 08:08:50 +020011895/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011896/* All supported ACL keywords must be declared here. */
11897/************************************************************************/
11898
11899/* Note: must not be declared <const> as its list will be overwritten.
11900 * Please take care of keeping this list alphabetically sorted.
11901 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011902static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011903 { "base", "base", PAT_MATCH_STR },
11904 { "base_beg", "base", PAT_MATCH_BEG },
11905 { "base_dir", "base", PAT_MATCH_DIR },
11906 { "base_dom", "base", PAT_MATCH_DOM },
11907 { "base_end", "base", PAT_MATCH_END },
11908 { "base_len", "base", PAT_MATCH_LEN },
11909 { "base_reg", "base", PAT_MATCH_REG },
11910 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011911
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011912 { "cook", "req.cook", PAT_MATCH_STR },
11913 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11914 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11915 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11916 { "cook_end", "req.cook", PAT_MATCH_END },
11917 { "cook_len", "req.cook", PAT_MATCH_LEN },
11918 { "cook_reg", "req.cook", PAT_MATCH_REG },
11919 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011920
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011921 { "hdr", "req.hdr", PAT_MATCH_STR },
11922 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11923 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11924 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11925 { "hdr_end", "req.hdr", PAT_MATCH_END },
11926 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11927 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11928 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011929
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011930 /* these two declarations uses strings with list storage (in place
11931 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11932 * and delete functions are relative to the list management. The parse
11933 * and match method are related to the corresponding fetch methods. This
11934 * is very particular ACL declaration mode.
11935 */
11936 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11937 { "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 +020011938
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011939 { "path", "path", PAT_MATCH_STR },
11940 { "path_beg", "path", PAT_MATCH_BEG },
11941 { "path_dir", "path", PAT_MATCH_DIR },
11942 { "path_dom", "path", PAT_MATCH_DOM },
11943 { "path_end", "path", PAT_MATCH_END },
11944 { "path_len", "path", PAT_MATCH_LEN },
11945 { "path_reg", "path", PAT_MATCH_REG },
11946 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011947
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011948 { "req_ver", "req.ver", PAT_MATCH_STR },
11949 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011950
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011951 { "scook", "res.cook", PAT_MATCH_STR },
11952 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11953 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11954 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11955 { "scook_end", "res.cook", PAT_MATCH_END },
11956 { "scook_len", "res.cook", PAT_MATCH_LEN },
11957 { "scook_reg", "res.cook", PAT_MATCH_REG },
11958 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011959
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011960 { "shdr", "res.hdr", PAT_MATCH_STR },
11961 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11962 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11963 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11964 { "shdr_end", "res.hdr", PAT_MATCH_END },
11965 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11966 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11967 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011968
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011969 { "url", "url", PAT_MATCH_STR },
11970 { "url_beg", "url", PAT_MATCH_BEG },
11971 { "url_dir", "url", PAT_MATCH_DIR },
11972 { "url_dom", "url", PAT_MATCH_DOM },
11973 { "url_end", "url", PAT_MATCH_END },
11974 { "url_len", "url", PAT_MATCH_LEN },
11975 { "url_reg", "url", PAT_MATCH_REG },
11976 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011977
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011978 { "urlp", "urlp", PAT_MATCH_STR },
11979 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11980 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11981 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11982 { "urlp_end", "urlp", PAT_MATCH_END },
11983 { "urlp_len", "urlp", PAT_MATCH_LEN },
11984 { "urlp_reg", "urlp", PAT_MATCH_REG },
11985 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011986
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011987 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011988}};
11989
11990/************************************************************************/
11991/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011992/************************************************************************/
11993/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011994static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011995 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011996 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11997 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11998
Willy Tarreau87b09662015-04-03 00:22:06 +020011999 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012000 { "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 +020012001
12002 /* retrieve these captures from the HTTP logs */
12003 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12004 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12005 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12006
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012007 { "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 +020012008 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012009
Willy Tarreau409bcde2013-01-08 00:31:00 +010012010 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12011 * are only here to match the ACL's name, are request-only and are used
12012 * for ACL compatibility only.
12013 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012014 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12015 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012016 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12017 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12018
12019 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12020 * only here to match the ACL's name, are request-only and are used for
12021 * ACL compatibility only.
12022 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012023 { "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 +010012024 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12025 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
12026 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12027
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012028 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012029 { "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 +010012030 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012031 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012032 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012033 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012034
12035 /* HTTP protocol on the request path */
12036 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012037 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012038
12039 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012040 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12041 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012042
12043 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012044 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12045 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012046
Willy Tarreau18ed2562013-01-14 15:56:36 +010012047 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012048 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012049 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12050 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12051
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012052 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012053 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012054 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012055 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12056 { "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 +010012057 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012058 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
12059
12060 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012061 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012062 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12063 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12064
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012065 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012066 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012067 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012068 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12069 { "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 +010012070 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012071 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12072
Willy Tarreau409bcde2013-01-08 00:31:00 +010012073 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012074 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012075 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12076 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012077 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012078
12079 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012080 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012081 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12082 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12083 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12084
12085 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012086 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012087 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12088 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012089 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12090 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012091 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12092 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012093 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12094 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012095}};
12096
Willy Tarreau8797c062007-05-07 00:55:35 +020012097
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012098/************************************************************************/
12099/* All supported converter keywords must be declared here. */
12100/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012101/* Note: must not be declared <const> as its list will be overwritten */
12102static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012103 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12104 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012105 { NULL, NULL, 0, 0, 0 },
12106}};
12107
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012108/************************************************************************/
12109/* All supported http-request action keywords must be declared here. */
12110/************************************************************************/
12111struct http_req_action_kw_list http_req_actions = {
12112 .scope = "http",
12113 .kw = {
12114 { "set-method", parse_set_req_line },
12115 { "set-path", parse_set_req_line },
12116 { "set-query", parse_set_req_line },
12117 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012118 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012119 }
12120};
12121
Willy Tarreau8797c062007-05-07 00:55:35 +020012122__attribute__((constructor))
12123static void __http_protocol_init(void)
12124{
12125 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012126 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012127 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012128 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012129}
12130
12131
Willy Tarreau58f10d72006-12-04 02:26:12 +010012132/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012133 * Local variables:
12134 * c-indent-level: 8
12135 * c-basic-offset: 8
12136 * End:
12137 */