blob: f5149282ec76a6e53caa91963c22f2556673fd1c [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 */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
356 ['P' - 'A'] = {
357 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
358 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
359 },
360 ['T' - 'A'] = {
361 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
362 },
363 /* rest is empty like this :
364 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
365 */
366};
367
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100368const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
369 [HTTP_METH_NONE] = { "", 0 },
370 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
371 [HTTP_METH_GET] = { "GET", 3 },
372 [HTTP_METH_HEAD] = { "HEAD", 4 },
373 [HTTP_METH_POST] = { "POST", 4 },
374 [HTTP_METH_PUT] = { "PUT", 3 },
375 [HTTP_METH_DELETE] = { "DELETE", 6 },
376 [HTTP_METH_TRACE] = { "TRACE", 5 },
377 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
378};
379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200381 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382 * RFC2616 for those chars.
383 */
384
385const char http_is_spht[256] = {
386 [' '] = 1, ['\t'] = 1,
387};
388
389const char http_is_crlf[256] = {
390 ['\r'] = 1, ['\n'] = 1,
391};
392
393const char http_is_lws[256] = {
394 [' '] = 1, ['\t'] = 1,
395 ['\r'] = 1, ['\n'] = 1,
396};
397
398const char http_is_sep[256] = {
399 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
400 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
401 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
402 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
403 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
404};
405
406const char http_is_ctl[256] = {
407 [0 ... 31] = 1,
408 [127] = 1,
409};
410
411/*
412 * A token is any ASCII char that is neither a separator nor a CTL char.
413 * Do not overwrite values in assignment since gcc-2.95 will not handle
414 * them correctly. Instead, define every non-CTL char's status.
415 */
416const char http_is_token[256] = {
417 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
418 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
419 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
420 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
421 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
422 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
423 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
424 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
425 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
426 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
427 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
428 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
429 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
430 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
431 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
432 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
433 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
434 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
435 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
436 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
437 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
438 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
439 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
440 ['|'] = 1, ['}'] = 0, ['~'] = 1,
441};
442
443
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100444/*
445 * An http ver_token is any ASCII which can be found in an HTTP version,
446 * which includes 'H', 'T', 'P', '/', '.' and any digit.
447 */
448const char http_is_ver_token[256] = {
449 ['.'] = 1, ['/'] = 1,
450 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
451 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100452 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100453};
454
455
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100457 * Adds a header and its CRLF at the tail of the message's buffer, just before
458 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 * The header is also automatically added to the index <hdr_idx>, and the end
460 * of headers is automatically adjusted. The number of bytes added is returned
461 * on success, otherwise <0 is returned indicating an error.
462 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100464{
465 int bytes, len;
466
467 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200468 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100469 if (!bytes)
470 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100471 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
473}
474
475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * the buffer is only opened and the space reserved, but nothing is copied.
479 * The header is also automatically added to the index <hdr_idx>, and the end
480 * of headers is automatically adjusted. The number of bytes added is returned
481 * on success, otherwise <0 is returned indicating an error.
482 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100483int http_header_add_tail2(struct http_msg *msg,
484 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100485{
486 int bytes;
487
Willy Tarreau9b28e032012-10-12 23:49:43 +0200488 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 if (!bytes)
490 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100491 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100492 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
493}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
495/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100496 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
497 * If so, returns the position of the first non-space character relative to
498 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
499 * to return a pointer to the place after the first space. Returns 0 if the
500 * header name does not match. Checks are case-insensitive.
501 */
502int http_header_match2(const char *hdr, const char *end,
503 const char *name, int len)
504{
505 const char *val;
506
507 if (hdr + len >= end)
508 return 0;
509 if (hdr[len] != ':')
510 return 0;
511 if (strncasecmp(hdr, name, len) != 0)
512 return 0;
513 val = hdr + len + 1;
514 while (val < end && HTTP_IS_SPHT(*val))
515 val++;
516 if ((val >= end) && (len + 2 <= end - hdr))
517 return len + 2; /* we may replace starting from second space */
518 return val - hdr;
519}
520
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200521/* Find the first or next occurrence of header <name> in message buffer <sol>
522 * using headers index <idx>, and return it in the <ctx> structure. This
523 * structure holds everything necessary to use the header and find next
524 * occurrence. If its <idx> member is 0, the header is searched from the
525 * beginning. Otherwise, the next occurrence is returned. The function returns
526 * 1 when it finds a value, and 0 when there is no more. It is very similar to
527 * http_find_header2() except that it is designed to work with full-line headers
528 * whose comma is not a delimiter but is part of the syntax. As a special case,
529 * if ctx->val is NULL when searching for a new values of a header, the current
530 * header is rescanned. This allows rescanning after a header deletion.
531 */
532int http_find_full_header2(const char *name, int len,
533 char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 char *eol, *sov;
537 int cur_idx, old_idx;
538
539 cur_idx = ctx->idx;
540 if (cur_idx) {
541 /* We have previously returned a header, let's search another one */
542 sol = ctx->line;
543 eol = sol + idx->v[cur_idx].len;
544 goto next_hdr;
545 }
546
547 /* first request for this header */
548 sol += hdr_idx_first_pos(idx);
549 old_idx = 0;
550 cur_idx = hdr_idx_first_idx(idx);
551 while (cur_idx) {
552 eol = sol + idx->v[cur_idx].len;
553
554 if (len == 0) {
555 /* No argument was passed, we want any header.
556 * To achieve this, we simply build a fake request. */
557 while (sol + len < eol && sol[len] != ':')
558 len++;
559 name = sol;
560 }
561
562 if ((len < eol - sol) &&
563 (sol[len] == ':') &&
564 (strncasecmp(sol, name, len) == 0)) {
565 ctx->del = len;
566 sov = sol + len + 1;
567 while (sov < eol && http_is_lws[(unsigned char)*sov])
568 sov++;
569
570 ctx->line = sol;
571 ctx->prev = old_idx;
572 ctx->idx = cur_idx;
573 ctx->val = sov - sol;
574 ctx->tws = 0;
575 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
576 eol--;
577 ctx->tws++;
578 }
579 ctx->vlen = eol - sov;
580 return 1;
581 }
582 next_hdr:
583 sol = eol + idx->v[cur_idx].cr + 1;
584 old_idx = cur_idx;
585 cur_idx = idx->v[cur_idx].next;
586 }
587 return 0;
588}
589
Willy Tarreauc90dc232015-02-20 13:51:36 +0100590/* Find the first or next header field in message buffer <sol> using headers
591 * index <idx>, and return it in the <ctx> structure. This structure holds
592 * everything necessary to use the header and find next occurrence. If its
593 * <idx> member is 0, the first header is retrieved. Otherwise, the next
594 * occurrence is returned. The function returns 1 when it finds a value, and
595 * 0 when there is no more. It is equivalent to http_find_full_header2() with
596 * no header name.
597 */
598int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
599{
600 char *eol, *sov;
601 int cur_idx, old_idx;
602 int len;
603
604 cur_idx = ctx->idx;
605 if (cur_idx) {
606 /* We have previously returned a header, let's search another one */
607 sol = ctx->line;
608 eol = sol + idx->v[cur_idx].len;
609 goto next_hdr;
610 }
611
612 /* first request for this header */
613 sol += hdr_idx_first_pos(idx);
614 old_idx = 0;
615 cur_idx = hdr_idx_first_idx(idx);
616 while (cur_idx) {
617 eol = sol + idx->v[cur_idx].len;
618
619 len = 0;
620 while (1) {
621 if (len >= eol - sol)
622 goto next_hdr;
623 if (sol[len] == ':')
624 break;
625 len++;
626 }
627
628 ctx->del = len;
629 sov = sol + len + 1;
630 while (sov < eol && http_is_lws[(unsigned char)*sov])
631 sov++;
632
633 ctx->line = sol;
634 ctx->prev = old_idx;
635 ctx->idx = cur_idx;
636 ctx->val = sov - sol;
637 ctx->tws = 0;
638
639 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
640 eol--;
641 ctx->tws++;
642 }
643 ctx->vlen = eol - sov;
644 return 1;
645
646 next_hdr:
647 sol = eol + idx->v[cur_idx].cr + 1;
648 old_idx = cur_idx;
649 cur_idx = idx->v[cur_idx].next;
650 }
651 return 0;
652}
653
Willy Tarreau68085d82010-01-18 14:54:04 +0100654/* Find the end of the header value contained between <s> and <e>. See RFC2616,
655 * par 2.2 for more information. Note that it requires a valid header to return
656 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200657 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100658char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200659{
660 int quoted, qdpair;
661
662 quoted = qdpair = 0;
663 for (; s < e; s++) {
664 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200665 else if (quoted) {
666 if (*s == '\\') qdpair = 1;
667 else if (*s == '"') quoted = 0;
668 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200669 else if (*s == '"') quoted = 1;
670 else if (*s == ',') return s;
671 }
672 return s;
673}
674
675/* Find the first or next occurrence of header <name> in message buffer <sol>
676 * using headers index <idx>, and return it in the <ctx> structure. This
677 * structure holds everything necessary to use the header and find next
678 * occurrence. If its <idx> member is 0, the header is searched from the
679 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100680 * 1 when it finds a value, and 0 when there is no more. It is designed to work
681 * with headers defined as comma-separated lists. As a special case, if ctx->val
682 * is NULL when searching for a new values of a header, the current header is
683 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200684 */
685int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100686 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 struct hdr_ctx *ctx)
688{
Willy Tarreau68085d82010-01-18 14:54:04 +0100689 char *eol, *sov;
690 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200691
Willy Tarreau68085d82010-01-18 14:54:04 +0100692 cur_idx = ctx->idx;
693 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200694 /* We have previously returned a value, let's search
695 * another one on the same line.
696 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200697 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200698 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200700 eol = sol + idx->v[cur_idx].len;
701
702 if (sov >= eol)
703 /* no more values in this header */
704 goto next_hdr;
705
Willy Tarreau68085d82010-01-18 14:54:04 +0100706 /* values remaining for this header, skip the comma but save it
707 * for later use (eg: for header deletion).
708 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200709 sov++;
710 while (sov < eol && http_is_lws[(unsigned char)*sov])
711 sov++;
712
713 goto return_hdr;
714 }
715
716 /* first request for this header */
717 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100718 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200719 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200720 while (cur_idx) {
721 eol = sol + idx->v[cur_idx].len;
722
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200723 if (len == 0) {
724 /* No argument was passed, we want any header.
725 * To achieve this, we simply build a fake request. */
726 while (sol + len < eol && sol[len] != ':')
727 len++;
728 name = sol;
729 }
730
Willy Tarreau33a7e692007-06-10 19:45:56 +0200731 if ((len < eol - sol) &&
732 (sol[len] == ':') &&
733 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100734 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200735 sov = sol + len + 1;
736 while (sov < eol && http_is_lws[(unsigned char)*sov])
737 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100738
Willy Tarreau33a7e692007-06-10 19:45:56 +0200739 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100740 ctx->prev = old_idx;
741 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200742 ctx->idx = cur_idx;
743 ctx->val = sov - sol;
744
745 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200746 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200747 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200748 eol--;
749 ctx->tws++;
750 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200751 ctx->vlen = eol - sov;
752 return 1;
753 }
754 next_hdr:
755 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100756 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200757 cur_idx = idx->v[cur_idx].next;
758 }
759 return 0;
760}
761
762int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100763 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200764 struct hdr_ctx *ctx)
765{
766 return http_find_header2(name, strlen(name), sol, idx, ctx);
767}
768
Willy Tarreau68085d82010-01-18 14:54:04 +0100769/* Remove one value of a header. This only works on a <ctx> returned by one of
770 * the http_find_header functions. The value is removed, as well as surrounding
771 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100772 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100773 * message <msg>. The new index is returned. If it is zero, it means there is
774 * no more header, so any processing may stop. The ctx is always left in a form
775 * that can be handled by http_find_header2() to find next occurrence.
776 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100777int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100778{
779 int cur_idx = ctx->idx;
780 char *sol = ctx->line;
781 struct hdr_idx_elem *hdr;
782 int delta, skip_comma;
783
784 if (!cur_idx)
785 return 0;
786
787 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200788 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100789 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100791 http_msg_move_end(msg, delta);
792 idx->used--;
793 hdr->len = 0; /* unused entry */
794 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100795 if (idx->tail == ctx->idx)
796 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100797 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100798 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100799 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200800 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100801 return ctx->idx;
802 }
803
804 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200805 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
806 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100807 */
808
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200809 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200810 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200811 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100812 NULL, 0);
813 hdr->len += delta;
814 http_msg_move_end(msg, delta);
815 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200816 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100817 return ctx->idx;
818}
819
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820/* This function handles a server error at the stream interface level. The
821 * stream interface is assumed to be already in a closed state. An optional
822 * message is copied into the input buffer, and an HTTP status code stored.
823 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100824 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200825 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200826static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100829 channel_auto_read(si_oc(si));
830 channel_abort(si_oc(si));
831 channel_auto_close(si_oc(si));
832 channel_erase(si_oc(si));
833 channel_auto_close(si_ic(si));
834 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100835 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200836 s->txn.status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100837 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200839 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200840 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200841 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200842 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200843}
844
Willy Tarreau87b09662015-04-03 00:22:06 +0200845/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100846 * and message.
847 */
848
Willy Tarreau87b09662015-04-03 00:22:06 +0200849struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100850{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200851 if (s->be->errmsg[msgnum].str)
852 return &s->be->errmsg[msgnum];
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200853 else if (strm_sess(s)->fe->errmsg[msgnum].str)
854 return &strm_sess(s)->fe->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100855 else
856 return &http_err_chunks[msgnum];
857}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858
Willy Tarreau53b6c742006-12-17 13:37:46 +0100859/*
860 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
861 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
862 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100863enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100864{
865 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100866 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100867
868 m = ((unsigned)*str - 'A');
869
870 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100871 for (h = http_methods[m]; h->len > 0; h++) {
872 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100873 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100874 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100875 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100876 };
877 return HTTP_METH_OTHER;
878 }
879 return HTTP_METH_NONE;
880
881}
882
Willy Tarreau21d2af32008-02-14 20:25:24 +0100883/* Parse the URI from the given transaction (which is assumed to be in request
884 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
885 * It is returned otherwise.
886 */
887static char *
888http_get_path(struct http_txn *txn)
889{
890 char *ptr, *end;
891
Willy Tarreau9b28e032012-10-12 23:49:43 +0200892 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100893 end = ptr + txn->req.sl.rq.u_l;
894
895 if (ptr >= end)
896 return NULL;
897
898 /* RFC2616, par. 5.1.2 :
899 * Request-URI = "*" | absuri | abspath | authority
900 */
901
902 if (*ptr == '*')
903 return NULL;
904
905 if (isalpha((unsigned char)*ptr)) {
906 /* this is a scheme as described by RFC3986, par. 3.1 */
907 ptr++;
908 while (ptr < end &&
909 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
910 ptr++;
911 /* skip '://' */
912 if (ptr == end || *ptr++ != ':')
913 return NULL;
914 if (ptr == end || *ptr++ != '/')
915 return NULL;
916 if (ptr == end || *ptr++ != '/')
917 return NULL;
918 }
919 /* skip [user[:passwd]@]host[:[port]] */
920
921 while (ptr < end && *ptr != '/')
922 ptr++;
923
924 if (ptr == end)
925 return NULL;
926
927 /* OK, we got the '/' ! */
928 return ptr;
929}
930
William Lallemand65ad6e12014-01-31 15:08:02 +0100931/* Parse the URI from the given string and look for the "/" beginning the PATH.
932 * If not found, return NULL. It is returned otherwise.
933 */
934static char *
935http_get_path_from_string(char *str)
936{
937 char *ptr = str;
938
939 /* RFC2616, par. 5.1.2 :
940 * Request-URI = "*" | absuri | abspath | authority
941 */
942
943 if (*ptr == '*')
944 return NULL;
945
946 if (isalpha((unsigned char)*ptr)) {
947 /* this is a scheme as described by RFC3986, par. 3.1 */
948 ptr++;
949 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
950 ptr++;
951 /* skip '://' */
952 if (*ptr == '\0' || *ptr++ != ':')
953 return NULL;
954 if (*ptr == '\0' || *ptr++ != '/')
955 return NULL;
956 if (*ptr == '\0' || *ptr++ != '/')
957 return NULL;
958 }
959 /* skip [user[:passwd]@]host[:[port]] */
960
961 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
962 ptr++;
963
964 if (*ptr == '\0' || *ptr == ' ')
965 return NULL;
966
967 /* OK, we got the '/' ! */
968 return ptr;
969}
970
Willy Tarreau71241ab2012-12-27 11:30:54 +0100971/* Returns a 302 for a redirectable request that reaches a server working in
972 * in redirect mode. This may only be called just after the stream interface
973 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
974 * follow normal proxy processing. NOTE: this function is designed to support
975 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200977void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978{
979 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100980 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100981 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200982 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983
984 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100985 trash.len = strlen(HTTP_302);
986 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100988 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100989
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100991 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992 return;
993
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100994 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100995 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100996 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
997 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100998 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001000 /* 3: add the request URI. Since it was already forwarded, we need
1001 * to temporarily rewind the buffer.
1002 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 txn = &s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001004 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001005
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001007 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 +02001008
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001009 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001010
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 if (!path)
1012 return;
1013
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001014 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001015 return;
1016
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001017 memcpy(trash.str + trash.len, path, len);
1018 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001019
1020 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001021 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1022 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001023 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001024 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1025 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001026 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001027
1028 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001029 si_shutr(si);
1030 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001031 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032 si->state = SI_ST_CLO;
1033
1034 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001035 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001036
1037 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001038 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001039 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001040}
1041
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001042/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001043 * that the server side is closed. Note that err_type is actually a
1044 * bitmask, where almost only aborts may be cumulated with other
1045 * values. We consider that aborted operations are more important
1046 * than timeouts or errors due to the fact that nobody else in the
1047 * logs might explain incomplete retries. All others should avoid
1048 * being cumulated. It should normally not be possible to have multiple
1049 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001050 * Note that connection errors appearing on the second request of a keep-alive
1051 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001052 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001053void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001054{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001055 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001056
1057 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001058 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001059 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001060 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001061 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001062 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1063 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001064 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001065 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001066 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001067 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001068 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001069 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001070 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001071 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001072 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1073 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001074 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001075 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1076 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001077 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001078 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001079 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001080 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1081 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001083 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001084 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001085}
1086
Willy Tarreau42250582007-04-01 01:30:43 +02001087extern const char sess_term_cond[8];
1088extern const char sess_fin_state[8];
1089extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001090struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001091struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001092struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093
Willy Tarreau117f59e2007-03-04 18:17:17 +01001094/*
1095 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001096 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001097 */
1098void capture_headers(char *som, struct hdr_idx *idx,
1099 char **cap, struct cap_hdr *cap_hdr)
1100{
1101 char *eol, *sol, *col, *sov;
1102 int cur_idx;
1103 struct cap_hdr *h;
1104 int len;
1105
1106 sol = som + hdr_idx_first_pos(idx);
1107 cur_idx = hdr_idx_first_idx(idx);
1108
1109 while (cur_idx) {
1110 eol = sol + idx->v[cur_idx].len;
1111
1112 col = sol;
1113 while (col < eol && *col != ':')
1114 col++;
1115
1116 sov = col + 1;
1117 while (sov < eol && http_is_lws[(unsigned char)*sov])
1118 sov++;
1119
1120 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001121 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001122 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1123 if (cap[h->index] == NULL)
1124 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001125 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001126
1127 if (cap[h->index] == NULL) {
1128 Alert("HTTP capture : out of memory.\n");
1129 continue;
1130 }
1131
1132 len = eol - sov;
1133 if (len > h->len)
1134 len = h->len;
1135
1136 memcpy(cap[h->index], sov, len);
1137 cap[h->index][len]=0;
1138 }
1139 }
1140 sol = eol + idx->v[cur_idx].cr + 1;
1141 cur_idx = idx->v[cur_idx].next;
1142 }
1143}
1144
1145
Willy Tarreau42250582007-04-01 01:30:43 +02001146/* either we find an LF at <ptr> or we jump to <bad>.
1147 */
1148#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1149
1150/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1151 * otherwise to <http_msg_ood> with <state> set to <st>.
1152 */
1153#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1154 ptr++; \
1155 if (likely(ptr < end)) \
1156 goto good; \
1157 else { \
1158 state = (st); \
1159 goto http_msg_ood; \
1160 } \
1161 } while (0)
1162
1163
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001165 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1167 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1168 * will give undefined results.
1169 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1170 * and that msg->sol points to the beginning of the response.
1171 * If a complete line is found (which implies that at least one CR or LF is
1172 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1173 * returned indicating an incomplete line (which does not mean that parts have
1174 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1175 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1176 * upon next call.
1177 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001178 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1180 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001181 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001183const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001184 enum ht_state state, const char *ptr, const char *end,
1185 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001186{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001187 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001188
Willy Tarreau8973c702007-01-21 23:58:29 +01001189 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001191 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001192 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1194
1195 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001196 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1198 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001199 state = HTTP_MSG_ERROR;
1200 break;
1201
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001203 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001205 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 goto http_msg_rpcode;
1207 }
1208 if (likely(HTTP_IS_SPHT(*ptr)))
1209 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1210 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001211 state = HTTP_MSG_ERROR;
1212 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001213
Willy Tarreau8973c702007-01-21 23:58:29 +01001214 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001215 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001216 if (likely(!HTTP_IS_LWS(*ptr)))
1217 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1218
1219 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001220 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001221 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1222 }
1223
1224 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001225 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 http_msg_rsp_reason:
1227 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001228 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001229 msg->sl.st.r_l = 0;
1230 goto http_msg_rpline_eol;
1231
Willy Tarreau8973c702007-01-21 23:58:29 +01001232 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001233 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001235 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 goto http_msg_rpreason;
1237 }
1238 if (likely(HTTP_IS_SPHT(*ptr)))
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1240 /* so it's a CR/LF, so there is no reason phrase */
1241 goto http_msg_rsp_reason;
1242
Willy Tarreau8973c702007-01-21 23:58:29 +01001243 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001244 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 if (likely(!HTTP_IS_CRLF(*ptr)))
1246 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 http_msg_rpline_eol:
1249 /* We have seen the end of line. Note that we do not
1250 * necessarily have the \n yet, but at least we know that we
1251 * have EITHER \r OR \n, otherwise the response would not be
1252 * complete. We can then record the response length and return
1253 * to the caller which will be able to register it.
1254 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001255 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001256 return ptr;
1257
Willy Tarreau8973c702007-01-21 23:58:29 +01001258 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001259#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1261 exit(1);
1262#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001263 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001264 }
1265
1266 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001267 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 if (ret_state)
1269 *ret_state = state;
1270 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001271 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001272 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001273}
1274
Willy Tarreau8973c702007-01-21 23:58:29 +01001275/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 * This function parses a request line between <ptr> and <end>, starting with
1277 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1278 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1279 * will give undefined results.
1280 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1281 * and that msg->sol points to the beginning of the request.
1282 * If a complete line is found (which implies that at least one CR or LF is
1283 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1284 * returned indicating an incomplete line (which does not mean that parts have
1285 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1286 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1287 * upon next call.
1288 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1291 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001292 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001294const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001295 enum ht_state state, const char *ptr, const char *end,
1296 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001297{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001298 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001302 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 if (likely(HTTP_IS_TOKEN(*ptr)))
1304 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(HTTP_IS_CRLF(*ptr))) {
1312 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001313 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001315 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001317 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001319 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 msg->sl.rq.v_l = 0;
1321 goto http_msg_rqline_eol;
1322 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001323 state = HTTP_MSG_ERROR;
1324 break;
1325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001327 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001329 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 goto http_msg_rquri;
1331 }
1332 if (likely(HTTP_IS_SPHT(*ptr)))
1333 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1334 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1335 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001339 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001343 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001347 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001348 /* non-ASCII chars are forbidden unless option
1349 * accept-invalid-http-request is enabled in the frontend.
1350 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001351 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001352 if (msg->err_pos < -1)
1353 goto invalid_char;
1354 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001355 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001356 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1357 }
1358
1359 if (likely(HTTP_IS_CRLF(*ptr))) {
1360 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1361 goto http_msg_req09_uri_e;
1362 }
1363
1364 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001365 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001366 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001367 state = HTTP_MSG_ERROR;
1368 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001371 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001373 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 goto http_msg_rqver;
1375 }
1376 if (likely(HTTP_IS_SPHT(*ptr)))
1377 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1378 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1379 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001382 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001383 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001385
1386 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001387 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001388 http_msg_rqline_eol:
1389 /* We have seen the end of line. Note that we do not
1390 * necessarily have the \n yet, but at least we know that we
1391 * have EITHER \r OR \n, otherwise the request would not be
1392 * complete. We can then record the request length and return
1393 * to the caller which will be able to register it.
1394 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001395 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001396 return ptr;
1397 }
1398
1399 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001400 state = HTTP_MSG_ERROR;
1401 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001404#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1406 exit(1);
1407#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001408 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001412 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 if (ret_state)
1414 *ret_state = state;
1415 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001416 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001419
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001420/*
1421 * Returns the data from Authorization header. Function may be called more
1422 * than once so data is stored in txn->auth_data. When no header is found
1423 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001424 * searching again for something we are unable to find anyway. However, if
1425 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001426 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001427 */
1428
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001429/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1430 * set according to global.tune.bufsize.
1431 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001432char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001433
1434int
Willy Tarreau87b09662015-04-03 00:22:06 +02001435get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001436{
1437
1438 struct http_txn *txn = &s->txn;
1439 struct chunk auth_method;
1440 struct hdr_ctx ctx;
1441 char *h, *p;
1442 int len;
1443
1444#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001445 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001446#endif
1447
1448 if (txn->auth.method == HTTP_AUTH_WRONG)
1449 return 0;
1450
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001451 txn->auth.method = HTTP_AUTH_WRONG;
1452
1453 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001454
1455 if (txn->flags & TX_USE_PX_CONN) {
1456 h = "Proxy-Authorization";
1457 len = strlen(h);
1458 } else {
1459 h = "Authorization";
1460 len = strlen(h);
1461 }
1462
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001463 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001464 return 0;
1465
1466 h = ctx.line + ctx.val;
1467
1468 p = memchr(h, ' ', ctx.vlen);
1469 if (!p || p == h)
1470 return 0;
1471
1472 chunk_initlen(&auth_method, h, 0, p-h);
1473 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1474
1475 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1476
1477 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001478 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001479
1480 if (len < 0)
1481 return 0;
1482
1483
1484 get_http_auth_buff[len] = '\0';
1485
1486 p = strchr(get_http_auth_buff, ':');
1487
1488 if (!p)
1489 return 0;
1490
1491 txn->auth.user = get_http_auth_buff;
1492 *p = '\0';
1493 txn->auth.pass = p+1;
1494
1495 txn->auth.method = HTTP_AUTH_BASIC;
1496 return 1;
1497 }
1498
1499 return 0;
1500}
1501
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Willy Tarreau8973c702007-01-21 23:58:29 +01001503/*
1504 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001505 * depending on the initial msg->msg_state. The caller is responsible for
1506 * ensuring that the message does not wrap. The function can be preempted
1507 * everywhere when data are missing and recalled at the exact same location
1508 * with no information loss. The message may even be realigned between two
1509 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001510 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001511 * fields. Note that msg->sol will be initialized after completing the first
1512 * state, so that none of the msg pointers has to be initialized prior to the
1513 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001515void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516{
Willy Tarreau3770f232013-12-07 00:01:53 +01001517 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001519 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001520
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001521 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001522 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001523 ptr = buf->p + msg->next;
1524 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (unlikely(ptr >= end))
1527 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001530 /*
1531 * First, states that are specific to the response only.
1532 * We check them first so that request and headers are
1533 * closer to each other (accessed more often).
1534 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001535 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001537 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001538 /* we have a start of message, but we have to check
1539 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001540 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001542 if (unlikely(ptr != buf->p)) {
1543 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001544 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001545 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001547 }
Willy Tarreau26927362012-05-18 23:22:52 +02001548 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001549 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 hdr_idx_init(idx);
1551 state = HTTP_MSG_RPVER;
1552 goto http_msg_rpver;
1553 }
1554
1555 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1556 goto http_msg_invalid;
1557
1558 if (unlikely(*ptr == '\n'))
1559 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1561 /* stop here */
1562
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001564 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001565 EXPECT_LF_HERE(ptr, http_msg_invalid);
1566 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1567 /* stop here */
1568
Willy Tarreau8973c702007-01-21 23:58:29 +01001569 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001570 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001571 case HTTP_MSG_RPVER_SP:
1572 case HTTP_MSG_RPCODE:
1573 case HTTP_MSG_RPCODE_SP:
1574 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001575 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001576 state, ptr, end,
1577 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001578 if (unlikely(!ptr))
1579 return;
1580
1581 /* we have a full response and we know that we have either a CR
1582 * or an LF at <ptr>.
1583 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001584 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1585
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001586 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001587 if (likely(*ptr == '\r'))
1588 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1589 goto http_msg_rpline_end;
1590
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001592 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 /* msg->sol must point to the first of CR or LF. */
1594 EXPECT_LF_HERE(ptr, http_msg_invalid);
1595 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1596 /* stop here */
1597
1598 /*
1599 * Second, states that are specific to the request only
1600 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001602 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001604 /* we have a start of message, but we have to check
1605 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001606 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001607 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001608 if (likely(ptr != buf->p)) {
1609 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001610 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001611 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001612 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 }
Willy Tarreau26927362012-05-18 23:22:52 +02001614 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001615 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 state = HTTP_MSG_RQMETH;
1617 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1621 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (unlikely(*ptr == '\n'))
1624 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1625 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001626 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001629 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 EXPECT_LF_HERE(ptr, http_msg_invalid);
1631 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001635 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_RQMETH_SP:
1637 case HTTP_MSG_RQURI:
1638 case HTTP_MSG_RQURI_SP:
1639 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001640 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001641 state, ptr, end,
1642 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (unlikely(!ptr))
1644 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 /* we have a full request and we know that we have either a CR
1647 * or an LF at <ptr>.
1648 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001650
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001651 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 if (likely(*ptr == '\r'))
1653 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001657 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 /* check for HTTP/0.9 request : no version information available.
1659 * msg->sol must point to the first of CR or LF.
1660 */
1661 if (unlikely(msg->sl.rq.v_l == 0))
1662 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 EXPECT_LF_HERE(ptr, http_msg_invalid);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001666 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001667
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 /*
1669 * Common states below
1670 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 if (likely(!HTTP_IS_CRLF(*ptr))) {
1675 goto http_msg_hdr_name;
1676 }
1677
1678 if (likely(*ptr == '\r'))
1679 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1680 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001683 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 /* assumes msg->sol points to the first char */
1685 if (likely(HTTP_IS_TOKEN(*ptr)))
1686 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001687
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001688 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001691 if (likely(msg->err_pos < -1) || *ptr == '\n')
1692 goto http_msg_invalid;
1693
1694 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001695 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001696
1697 /* and we still accept this non-token character */
1698 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001701 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001702 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 if (likely(HTTP_IS_SPHT(*ptr)))
1704 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (likely(!HTTP_IS_CRLF(*ptr))) {
1710 goto http_msg_hdr_val;
1711 }
1712
1713 if (likely(*ptr == '\r'))
1714 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1715 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001716
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001717 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001718 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 EXPECT_LF_HERE(ptr, http_msg_invalid);
1720 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001723 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(HTTP_IS_SPHT(*ptr))) {
1725 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001726 for (; buf->p + msg->sov < ptr; msg->sov++)
1727 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 goto http_msg_hdr_l1_sp;
1729 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001730 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001731 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 goto http_msg_complete_header;
1733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001735 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001736 /* assumes msg->sol points to the first char, and msg->sov
1737 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 */
1739 if (likely(!HTTP_IS_CRLF(*ptr)))
1740 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001742 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 /* Note: we could also copy eol into ->eoh so that we have the
1744 * real header end in case it ends with lots of LWS, but is this
1745 * really needed ?
1746 */
1747 if (likely(*ptr == '\r'))
1748 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1749 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001752 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 EXPECT_LF_HERE(ptr, http_msg_invalid);
1754 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001757 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1759 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001760 for (; buf->p + msg->eol < ptr; msg->eol++)
1761 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 goto http_msg_hdr_val;
1763 }
1764 http_msg_complete_header:
1765 /*
1766 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001767 * Assumes msg->sol points to the first char, msg->sov points
1768 * to the first character of the value and msg->eol to the
1769 * first CR or LF so we know how the line ends. We insert last
1770 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001771 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001772 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 idx, idx->tail) < 0))
1774 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001775
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001776 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 if (likely(!HTTP_IS_CRLF(*ptr))) {
1778 goto http_msg_hdr_name;
1779 }
1780
1781 if (likely(*ptr == '\r'))
1782 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1783 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001785 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001786 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001787 /* Assumes msg->sol points to the first of either CR or LF.
1788 * Sets ->sov and ->next to the total header length, ->eoh to
1789 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1790 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 EXPECT_LF_HERE(ptr, http_msg_invalid);
1792 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001793 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001794 msg->eoh = msg->sol;
1795 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001796 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001797 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001798 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001799
1800 case HTTP_MSG_ERROR:
1801 /* this may only happen if we call http_msg_analyser() twice with an error */
1802 break;
1803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001805#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1807 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001808#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001809 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 }
1811 http_msg_ood:
1812 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001813 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001814 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 http_msg_invalid:
1818 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001819 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001820 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 return;
1822}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001823
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001824/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1825 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1826 * nothing is done and 1 is returned.
1827 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001828static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001829{
1830 int delta;
1831 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001832 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001833
1834 if (msg->sl.rq.v_l != 0)
1835 return 1;
1836
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001837 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1838 if (txn->meth != HTTP_METH_GET)
1839 return 0;
1840
Willy Tarreau9b28e032012-10-12 23:49:43 +02001841 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001842 delta = 0;
1843
1844 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001845 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1846 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001847 }
1848 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001849 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001850 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001851 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001852 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001853 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001854 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001855 NULL, NULL);
1856 if (unlikely(!cur_end))
1857 return 0;
1858
1859 /* we have a full HTTP/1.0 request now and we know that
1860 * we have either a CR or an LF at <ptr>.
1861 */
1862 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1863 return 1;
1864}
1865
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001866/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001867 * and "keep-alive" values. If we already know that some headers may safely
1868 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001869 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1870 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001871 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1873 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1874 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001875 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001876 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001877void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001878{
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001880 const char *hdr_val = "Connection";
1881 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001882
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001883 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001884 return;
1885
Willy Tarreau88d349d2010-01-25 12:15:43 +01001886 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1887 hdr_val = "Proxy-Connection";
1888 hdr_len = 16;
1889 }
1890
Willy Tarreau5b154472009-12-21 20:11:07 +01001891 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001892 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001893 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001894 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1895 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001896 if (to_del & 2)
1897 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 else
1899 txn->flags |= TX_CON_KAL_SET;
1900 }
1901 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1902 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001903 if (to_del & 1)
1904 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001905 else
1906 txn->flags |= TX_CON_CLO_SET;
1907 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001908 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1909 txn->flags |= TX_HDR_CONN_UPG;
1910 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001911 }
1912
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001913 txn->flags |= TX_HDR_CONN_PRS;
1914 return;
1915}
Willy Tarreau5b154472009-12-21 20:11:07 +01001916
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001917/* Apply desired changes on the Connection: header. Values may be removed and/or
1918 * added depending on the <wanted> flags, which are exclusively composed of
1919 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1920 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1921 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001922void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001923{
1924 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001925 const char *hdr_val = "Connection";
1926 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001927
1928 ctx.idx = 0;
1929
Willy Tarreau88d349d2010-01-25 12:15:43 +01001930
1931 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1932 hdr_val = "Proxy-Connection";
1933 hdr_len = 16;
1934 }
1935
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001936 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001937 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001938 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1939 if (wanted & TX_CON_KAL_SET)
1940 txn->flags |= TX_CON_KAL_SET;
1941 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001942 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001943 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001944 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1945 if (wanted & TX_CON_CLO_SET)
1946 txn->flags |= TX_CON_CLO_SET;
1947 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001948 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001949 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001950 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001951
1952 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1953 return;
1954
1955 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1956 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001957 hdr_val = "Connection: close";
1958 hdr_len = 17;
1959 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1960 hdr_val = "Proxy-Connection: close";
1961 hdr_len = 23;
1962 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001963 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001964 }
1965
1966 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1967 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001968 hdr_val = "Connection: keep-alive";
1969 hdr_len = 22;
1970 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1971 hdr_val = "Proxy-Connection: keep-alive";
1972 hdr_len = 28;
1973 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001974 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001975 }
1976 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001977}
1978
Willy Tarreauc24715e2014-04-17 15:21:20 +02001979/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1980 * the first byte of data after the chunk size, so that we know we can forward
1981 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1982 * the chunk size. That way it is always possible to differentiate between the
1983 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001984 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001987static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001988{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001989 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001991 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001992 const char *end = buf->data + buf->size;
1993 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001994 unsigned int chunk = 0;
1995
1996 /* The chunk size is in the following form, though we are only
1997 * interested in the size and CRLF :
1998 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1999 */
2000 while (1) {
2001 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002002 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002003 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002004 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 if (c < 0) /* not a hex digit anymore */
2006 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002007 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002009 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002010 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002011 chunk = (chunk << 4) + c;
2012 }
2013
Willy Tarreaud98cf932009-12-27 22:54:55 +01002014 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002015 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002016 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002017
2018 while (http_is_spht[(unsigned char)*ptr]) {
2019 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002020 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002021 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002022 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 }
2024
Willy Tarreaud98cf932009-12-27 22:54:55 +01002025 /* Up to there, we know that at least one byte is present at *ptr. Check
2026 * for the end of chunk size.
2027 */
2028 while (1) {
2029 if (likely(HTTP_IS_CRLF(*ptr))) {
2030 /* we now have a CR or an LF at ptr */
2031 if (likely(*ptr == '\r')) {
2032 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002033 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002034 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 return 0;
2036 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002037
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002039 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042 /* done */
2043 break;
2044 }
2045 else if (*ptr == ';') {
2046 /* chunk extension, ends at next CRLF */
2047 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002048 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002049 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002050 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002051
2052 while (!HTTP_IS_CRLF(*ptr)) {
2053 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002055 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056 return 0;
2057 }
2058 /* we have a CRLF now, loop above */
2059 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002060 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002062 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002063 }
2064
Willy Tarreaud98cf932009-12-27 22:54:55 +01002065 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002066 * which may or may not be present. We save that into ->next,
2067 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002068 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002069 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002070 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002071 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002072 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002073 msg->chunk_len = chunk;
2074 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002076 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002077 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002078 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002079 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080}
2081
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002082/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002083 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 * the trailers is found, it is automatically scheduled to be forwarded,
2085 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2086 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002087 * except maybe msg->next if it could parse some lines, and returns zero.
2088 * If a parse error is encountered, the function returns < 0 and does not
2089 * change anything except maybe msg->next. Note that the message must
2090 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002091 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002092 * forwarding, and that msg->next exactly matches the length of trailers
2093 * already parsed and not forwarded. It is also important to note that this
2094 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002096static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002098 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002099
Willy Tarreaua458b672012-03-05 11:17:50 +01002100 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002101 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002103 const char *ptr = b_ptr(buf, msg->next);
2104 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002105 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106
2107 /* scan current line and stop at LF or CRLF */
2108 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002109 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 return 0;
2111
2112 if (*ptr == '\n') {
2113 if (!p1)
2114 p1 = ptr;
2115 p2 = ptr;
2116 break;
2117 }
2118
2119 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002120 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002121 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124 p1 = ptr;
2125 }
2126
2127 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002128 if (ptr >= buf->data + buf->size)
2129 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 }
2131
2132 /* after LF; point to beginning of next line */
2133 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002134 if (p2 >= buf->data + buf->size)
2135 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002137 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002138 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002139 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002140
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002142 /* LF/CRLF at beginning of line => end of trailers at p2.
2143 * Everything was scheduled for forwarding, there's nothing
2144 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002145 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002146 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 msg->msg_state = HTTP_MSG_DONE;
2148 return 1;
2149 }
2150 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002151 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002152 }
2153}
2154
Willy Tarreauc24715e2014-04-17 15:21:20 +02002155/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2156 * or a possible LF alone at the end of a chunk. It automatically adjusts
2157 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002158 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2160 * not enough data are available, the function does not change anything and
2161 * returns zero. If a parse error is encountered, the function returns < 0 and
2162 * does not change anything. Note: this function is designed to parse wrapped
2163 * CRLF at the end of the buffer.
2164 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002165static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002166{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002167 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002168 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002169 int bytes;
2170
2171 /* NB: we'll check data availabilty at the end. It's not a
2172 * problem because whatever we match first will be checked
2173 * against the correct length.
2174 */
2175 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002176 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002177 if (*ptr == '\r') {
2178 bytes++;
2179 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002180 if (ptr >= buf->data + buf->size)
2181 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002182 }
2183
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002184 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002185 return 0;
2186
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002187 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002188 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002190 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002191
2192 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002193 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002194 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002195 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002196 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2198 return 1;
2199}
Willy Tarreau5b154472009-12-21 20:11:07 +01002200
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002201/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2202 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2203 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2204 * Unparsable qvalues return 1000 as "q=1.000".
2205 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002206int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002207{
2208 int q = 1000;
2209
Willy Tarreau506c69a2014-07-08 00:59:48 +02002210 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002211 goto out;
2212 q = (*qvalue++ - '0') * 1000;
2213
2214 if (*qvalue++ != '.')
2215 goto out;
2216
Willy Tarreau506c69a2014-07-08 00:59:48 +02002217 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002218 goto out;
2219 q += (*qvalue++ - '0') * 100;
2220
Willy Tarreau506c69a2014-07-08 00:59:48 +02002221 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002222 goto out;
2223 q += (*qvalue++ - '0') * 10;
2224
Willy Tarreau506c69a2014-07-08 00:59:48 +02002225 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 goto out;
2227 q += (*qvalue++ - '0') * 1;
2228 out:
2229 if (q > 1000)
2230 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002231 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002232 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 return q;
2234}
William Lallemand82fe75c2012-10-23 10:25:10 +02002235
2236/*
2237 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002238 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002239int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002240{
2241 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002242 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002243 struct hdr_ctx ctx;
2244 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002245 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002246
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002247 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2248 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002249 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2250 */
2251 ctx.idx = 0;
2252 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2253 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002254 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2255 (ctx.vlen < 31 ||
2256 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2257 ctx.line[ctx.val + 30] < '6' ||
2258 (ctx.line[ctx.val + 30] == '6' &&
2259 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2260 s->comp_algo = NULL;
2261 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002262 }
2263
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002265 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_sess(s)->fe->comp && (comp_algo_back = strm_sess(s)->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 int best_q = 0;
2267
William Lallemand82fe75c2012-10-23 10:25:10 +02002268 ctx.idx = 0;
2269 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002270 const char *qval;
2271 int q;
2272 int toklen;
2273
2274 /* try to isolate the token from the optional q-value */
2275 toklen = 0;
2276 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2277 toklen++;
2278
2279 qval = ctx.line + ctx.val + toklen;
2280 while (1) {
2281 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2282 qval++;
2283
2284 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2285 qval = NULL;
2286 break;
2287 }
2288 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002289
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002290 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2291 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002292
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002293 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2294 qval = NULL;
2295 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002296 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002297 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2298 break;
2299
2300 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2301 qval++;
2302 }
2303
2304 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002305 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002306
2307 if (q <= best_q)
2308 continue;
2309
2310 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2311 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002312 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313 s->comp_algo = comp_algo;
2314 best_q = q;
2315 break;
2316 }
2317 }
2318 }
2319 }
2320
2321 /* remove all occurrences of the header when "compression offload" is set */
2322 if (s->comp_algo) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002323 if ((s->be->comp && s->be->comp->offload) || (strm_sess(s)->fe->comp && strm_sess(s)->fe->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002324 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2325 ctx.idx = 0;
2326 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2327 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002328 }
2329 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002330 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002331 }
2332
2333 /* identity is implicit does not require headers */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002334 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (strm_sess(s)->fe->comp && (comp_algo_back = strm_sess(s)->fe->comp->algos))) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002335 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002336 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002337 s->comp_algo = comp_algo;
2338 return 1;
2339 }
2340 }
2341 }
2342
2343 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 return 0;
2345}
2346
2347/*
2348 * Selects a comression algorithm depending of the server response.
2349 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002350int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002351{
2352 struct http_txn *txn = &s->txn;
2353 struct http_msg *msg = &txn->rsp;
2354 struct hdr_ctx ctx;
2355 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002356
2357 /* no common compression algorithm was found in request header */
2358 if (s->comp_algo == NULL)
2359 goto fail;
2360
2361 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002362 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002363 goto fail;
2364
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002365 /* compress 200,201,202,203 responses only */
2366 if ((txn->status != 200) &&
2367 (txn->status != 201) &&
2368 (txn->status != 202) &&
2369 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002370 goto fail;
2371
William Lallemand82fe75c2012-10-23 10:25:10 +02002372 /* Content-Length is null */
2373 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2374 goto fail;
2375
2376 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002377 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002378 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2379 goto fail;
2380
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002381 /* no compression when Cache-Control: no-transform is present in the message */
2382 ctx.idx = 0;
2383 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2384 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2385 goto fail;
2386 }
2387
William Lallemand82fe75c2012-10-23 10:25:10 +02002388 comp_type = NULL;
2389
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002390 /* we don't want to compress multipart content-types, nor content-types that are
2391 * not listed in the "compression type" directive if any. If no content-type was
2392 * found but configuration requires one, we don't compress either. Backend has
2393 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002394 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002395 ctx.idx = 0;
2396 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2397 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2398 goto fail;
2399
2400 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002401 (strm_sess(s)->fe->comp && (comp_type = strm_sess(s)->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002402 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002403 if (ctx.vlen >= comp_type->name_len &&
2404 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002405 /* this Content-Type should be compressed */
2406 break;
2407 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002408 /* this Content-Type should not be compressed */
2409 if (comp_type == NULL)
2410 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002411 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002412 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002413 else { /* no content-type header */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002414 if ((s->be->comp && s->be->comp->types) || (strm_sess(s)->fe->comp && strm_sess(s)->fe->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002415 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002416 }
2417
William Lallemandd85f9172012-11-09 17:05:39 +01002418 /* limit compression rate */
2419 if (global.comp_rate_lim > 0)
2420 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2421 goto fail;
2422
William Lallemand072a2bf2012-11-20 17:01:01 +01002423 /* limit cpu usage */
2424 if (idle_pct < compress_min_idle)
2425 goto fail;
2426
William Lallemand4c49fae2012-11-07 15:00:23 +01002427 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002428 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002429 goto fail;
2430
Willy Tarreaue7dff022015-04-03 01:14:29 +02002431 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002432
William Lallemand82fe75c2012-10-23 10:25:10 +02002433 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002434 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002435 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2436 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2437
2438 /* add Transfer-Encoding header */
2439 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2440 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2441
2442 /*
2443 * Add Content-Encoding header when it's not identity encoding.
2444 * RFC 2616 : Identity encoding: This content-coding is used only in the
2445 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2446 * header.
2447 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002448 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002449 trash.len = 18;
2450 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002451 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2452 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002453 trash.str[trash.len] = '\0';
2454 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002456 return 1;
2457
2458fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002459 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002460 return 0;
2461}
2462
Willy Tarreau87b09662015-04-03 00:22:06 +02002463void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002464{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002465 struct proxy *fe = strm_sess(s)->fe;
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002466 int tmp = TX_CON_WANT_KAL;
2467
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002468 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2469 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002470 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2471 tmp = TX_CON_WANT_TUN;
2472
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002473 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002474 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2475 tmp = TX_CON_WANT_TUN;
2476 }
2477
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002478 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002479 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2480 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002481 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002482 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2483 tmp = TX_CON_WANT_CLO;
2484 else
2485 tmp = TX_CON_WANT_SCL;
2486 }
2487
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002488 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002489 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2490 tmp = TX_CON_WANT_CLO;
2491
2492 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2493 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2494
2495 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2496 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2497 /* parse the Connection header and possibly clean it */
2498 int to_del = 0;
2499 if ((msg->flags & HTTP_MSGF_VER_11) ||
2500 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002501 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002502 to_del |= 2; /* remove "keep-alive" */
2503 if (!(msg->flags & HTTP_MSGF_VER_11))
2504 to_del |= 1; /* remove "close" */
2505 http_parse_connection_header(txn, msg, to_del);
2506 }
2507
2508 /* check if client or config asks for explicit close in KAL/SCL */
2509 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2510 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2511 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2512 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2513 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002514 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002515 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2516}
William Lallemand82fe75c2012-10-23 10:25:10 +02002517
Willy Tarreaud787e662009-07-07 10:14:51 +02002518/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2519 * processing can continue on next analysers, or zero if it either needs more
2520 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002521 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002522 * when it has nothing left to do, and may remove any analyser when it wants to
2523 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002525int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002526{
Willy Tarreau59234e92008-11-30 23:51:27 +01002527 /*
2528 * We will parse the partial (or complete) lines.
2529 * We will check the request syntax, and also join multi-line
2530 * headers. An index of all the lines will be elaborated while
2531 * parsing.
2532 *
2533 * For the parsing, we use a 28 states FSM.
2534 *
2535 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002536 * req->buf->p = beginning of request
2537 * req->buf->p + msg->eoh = end of processed headers / start of current one
2538 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002539 * msg->eol = end of current header or line (LF or CRLF)
2540 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002541 *
2542 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002543 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002544 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2545 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002547
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002549 int use_close_only;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002550 struct session *sess = s->sess;
Willy Tarreau59234e92008-11-30 23:51:27 +01002551 struct http_txn *txn = &s->txn;
2552 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002553 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002554
Willy Tarreau87b09662015-04-03 00:22:06 +02002555 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 +01002556 now_ms, __FUNCTION__,
2557 s,
2558 req,
2559 req->rex, req->wex,
2560 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002561 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002562 req->analysers);
2563
Willy Tarreau52a0c602009-08-16 22:45:38 +02002564 /* we're speaking HTTP here, so let's speak HTTP to the client */
2565 s->srv_error = http_return_srv_error;
2566
Willy Tarreau83e3af02009-12-28 17:39:57 +01002567 /* There's a protected area at the end of the buffer for rewriting
2568 * purposes. We don't want to start to parse the request if the
2569 * protected area is affected, because we may have to move processed
2570 * data later, which is much more complicated.
2571 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002572 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002573 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002574 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002575 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002576 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002577 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002578 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002579 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002580 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002581 return 0;
2582 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002583 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2584 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2585 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002586 }
2587
Willy Tarreau065e8332010-01-08 00:30:20 +01002588 /* Note that we have the same problem with the response ; we
2589 * may want to send a redirect, error or anything which requires
2590 * some spare space. So we'll ensure that we have at least
2591 * maxrewrite bytes available in the response buffer before
2592 * processing that one. This will only affect pipelined
2593 * keep-alive requests.
2594 */
2595 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002596 unlikely(!channel_is_rewritable(&s->res) ||
2597 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2598 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2599 if (s->res.buf->o) {
2600 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002601 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002602 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002603 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002604 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2605 s->res.flags |= CF_WAKE_WRITE;
2606 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002607 return 0;
2608 }
2609 }
2610
Willy Tarreau9b28e032012-10-12 23:49:43 +02002611 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002612 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002613 }
2614
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 /* 1: we might have to print this header in debug mode */
2616 if (unlikely((global.mode & MODE_DEBUG) &&
2617 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002618 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002619 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002620
Willy Tarreau9b28e032012-10-12 23:49:43 +02002621 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002622 /* this is a bit complex : in case of error on the request line,
2623 * we know that rq.l is still zero, so we display only the part
2624 * up to the end of the line (truncated by debug_hdr).
2625 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002626 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002628
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 sol += hdr_idx_first_pos(&txn->hdr_idx);
2630 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002631
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 while (cur_idx) {
2633 eol = sol + txn->hdr_idx.v[cur_idx].len;
2634 debug_hdr("clihdr", s, sol, eol);
2635 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2636 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002637 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 }
2639
Willy Tarreau58f10d72006-12-04 02:26:12 +01002640
Willy Tarreau59234e92008-11-30 23:51:27 +01002641 /*
2642 * Now we quickly check if we have found a full valid request.
2643 * If not so, we check the FD and buffer states before leaving.
2644 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002645 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002646 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002647 * on a keep-alive stream, if we encounter and error, close, t/o,
2648 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002649 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002650 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002651 * Last, we may increase some tracked counters' http request errors on
2652 * the cases that are deliberately the client's fault. For instance,
2653 * a timeout or connection reset is not counted as an error. However
2654 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002655 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002656
Willy Tarreau655dce92009-11-08 13:10:58 +01002657 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002658 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002660 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002661 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002662 stream_inc_http_req_ctr(s);
2663 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002664 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002665 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002666 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 /* 1: Since we are in header mode, if there's no space
2669 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002670 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002671 * must terminate it now.
2672 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002673 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 /* FIXME: check if URI is set and return Status
2675 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002676 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002677 stream_inc_http_req_ctr(s);
2678 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002679 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002680 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002681 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 goto return_bad_req;
2683 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002684
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002686 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002687 if (!(s->flags & SF_ERR_MASK))
2688 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002689
Willy Tarreaufcffa692010-01-10 14:21:19 +01002690 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002691 goto failed_keep_alive;
2692
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002694 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002695 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002696 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002697 }
2698
Willy Tarreaudc979f22012-12-04 10:39:01 +01002699 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002700 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 msg->msg_state = HTTP_MSG_ERROR;
2702 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002703
Willy Tarreau87b09662015-04-03 00:22:06 +02002704 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002705 proxy_inc_fe_req_ctr(sess->fe);
2706 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002707 if (sess->listener->counters)
2708 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002709
Willy Tarreaue7dff022015-04-03 01:14:29 +02002710 if (!(s->flags & SF_FINST_MASK))
2711 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 return 0;
2713 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002714
Willy Tarreau59234e92008-11-30 23:51:27 +01002715 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002716 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002717 if (!(s->flags & SF_ERR_MASK))
2718 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002719
Willy Tarreaufcffa692010-01-10 14:21:19 +01002720 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002721 goto failed_keep_alive;
2722
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002724 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002725 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002726 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002727 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002728 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002729 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 msg->msg_state = HTTP_MSG_ERROR;
2731 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002732
Willy Tarreau87b09662015-04-03 00:22:06 +02002733 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002734 proxy_inc_fe_req_ctr(sess->fe);
2735 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002736 if (sess->listener->counters)
2737 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002738
Willy Tarreaue7dff022015-04-03 01:14:29 +02002739 if (!(s->flags & SF_FINST_MASK))
2740 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002741 return 0;
2742 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002743
Willy Tarreau59234e92008-11-30 23:51:27 +01002744 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002745 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002746 if (!(s->flags & SF_ERR_MASK))
2747 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002748
Willy Tarreaufcffa692010-01-10 14:21:19 +01002749 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002750 goto failed_keep_alive;
2751
Willy Tarreau4076a152009-04-02 15:18:36 +02002752 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002753 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002754 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002755 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 msg->msg_state = HTTP_MSG_ERROR;
2757 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002758
Willy Tarreau87b09662015-04-03 00:22:06 +02002759 stream_inc_http_err_ctr(s);
2760 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002761 proxy_inc_fe_req_ctr(sess->fe);
2762 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002763 if (sess->listener->counters)
2764 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002765
Willy Tarreaue7dff022015-04-03 01:14:29 +02002766 if (!(s->flags & SF_FINST_MASK))
2767 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002768 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002769 }
2770
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002771 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002772 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002773 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002774#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002775 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2776 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002777 /* We need more data, we have to re-enable quick-ack in case we
2778 * previously disabled it, otherwise we might cause the client
2779 * to delay next data.
2780 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002781 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002782 }
2783#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002784
Willy Tarreaufcffa692010-01-10 14:21:19 +01002785 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2786 /* If the client starts to talk, let's fall back to
2787 * request timeout processing.
2788 */
2789 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002790 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002791 }
2792
Willy Tarreau59234e92008-11-30 23:51:27 +01002793 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002794 if (!tick_isset(req->analyse_exp)) {
2795 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2796 (txn->flags & TX_WAIT_NEXT_RQ) &&
2797 tick_isset(s->be->timeout.httpka))
2798 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2799 else
2800 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2801 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002802
Willy Tarreau59234e92008-11-30 23:51:27 +01002803 /* we're not ready yet */
2804 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002805
2806 failed_keep_alive:
2807 /* Here we process low-level errors for keep-alive requests. In
2808 * short, if the request is not the first one and it experiences
2809 * a timeout, read error or shutdown, we just silently close so
2810 * that the client can try again.
2811 */
2812 txn->status = 0;
2813 msg->msg_state = HTTP_MSG_RQBEFORE;
2814 req->analysers = 0;
2815 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002816 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002817 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002818 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002819 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002821
Willy Tarreaud787e662009-07-07 10:14:51 +02002822 /* OK now we have a complete HTTP request with indexed headers. Let's
2823 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002824 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002825 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002826 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002827 * byte after the last LF. msg->sov points to the first byte of data.
2828 * msg->eol cannot be trusted because it may have been left uninitialized
2829 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002830 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002831
Willy Tarreau87b09662015-04-03 00:22:06 +02002832 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002833 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002834
Willy Tarreaub16a5742010-01-10 14:46:16 +01002835 if (txn->flags & TX_WAIT_NEXT_RQ) {
2836 /* kill the pending keep-alive timeout */
2837 txn->flags &= ~TX_WAIT_NEXT_RQ;
2838 req->analyse_exp = TICK_ETERNITY;
2839 }
2840
2841
Willy Tarreaud787e662009-07-07 10:14:51 +02002842 /* Maybe we found in invalid header name while we were configured not
2843 * to block on that, so we have to capture it now.
2844 */
2845 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002846 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002847
Willy Tarreau59234e92008-11-30 23:51:27 +01002848 /*
2849 * 1: identify the method
2850 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002851 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002852
2853 /* we can make use of server redirect on GET and HEAD */
2854 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002855 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002856
Willy Tarreau59234e92008-11-30 23:51:27 +01002857 /*
2858 * 2: check if the URI matches the monitor_uri.
2859 * We have to do this for every request which gets in, because
2860 * the monitor-uri is defined by the frontend.
2861 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002862 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2863 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002864 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002865 sess->fe->monitor_uri,
2866 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002867 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002869 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002870 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002871
Willy Tarreaue7dff022015-04-03 01:14:29 +02002872 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002873 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002874
Willy Tarreau59234e92008-11-30 23:51:27 +01002875 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002876 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
2877 int ret = acl_exec_cond(cond, sess->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002878
Willy Tarreau59234e92008-11-30 23:51:27 +01002879 ret = acl_pass(ret);
2880 if (cond->pol == ACL_COND_UNLESS)
2881 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002882
Willy Tarreau59234e92008-11-30 23:51:27 +01002883 if (ret) {
2884 /* we fail this request, let's return 503 service unavail */
2885 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002886 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002887 if (!(s->flags & SF_ERR_MASK))
2888 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002889 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002890 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002891 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002892
Willy Tarreau59234e92008-11-30 23:51:27 +01002893 /* nothing to fail, let's reply normaly */
2894 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01002895 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002896 if (!(s->flags & SF_ERR_MASK))
2897 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002898 goto return_prx_cond;
2899 }
2900
2901 /*
2902 * 3: Maybe we have to copy the original REQURI for the logs ?
2903 * Note: we cannot log anymore if the request has been
2904 * classified as invalid.
2905 */
2906 if (unlikely(s->logs.logwait & LW_REQ)) {
2907 /* we have a complete HTTP request that we must log */
2908 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2909 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002910
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 if (urilen >= REQURI_LEN)
2912 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002913 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002914 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002915
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002916 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 s->do_log(s);
2918 } else {
2919 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002920 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002921 }
Willy Tarreau06619262006-12-17 08:37:22 +01002922
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002924 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002925 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002926
Willy Tarreau5b154472009-12-21 20:11:07 +01002927 /* ... and check if the request is HTTP/1.1 or above */
2928 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002929 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2930 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2931 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002932 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002933
2934 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002935 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 +01002936
Willy Tarreau88d349d2010-01-25 12:15:43 +01002937 /* if the frontend has "option http-use-proxy-header", we'll check if
2938 * we have what looks like a proxied connection instead of a connection,
2939 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2940 * Note that this is *not* RFC-compliant, however browsers and proxies
2941 * happen to do that despite being non-standard :-(
2942 * We consider that a request not beginning with either '/' or '*' is
2943 * a proxied connection, which covers both "scheme://location" and
2944 * CONNECT ip:port.
2945 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002946 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002947 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002948 txn->flags |= TX_USE_PX_CONN;
2949
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002950 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002951 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002952
Willy Tarreau59234e92008-11-30 23:51:27 +01002953 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002954 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002955 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002956 txn->req.cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002957
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002958 /* 6: determine the transfer-length.
2959 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2960 * the presence of a message-body in a REQUEST and its transfer length
2961 * must be determined that way (in order of precedence) :
2962 * 1. The presence of a message-body in a request is signaled by the
2963 * inclusion of a Content-Length or Transfer-Encoding header field
2964 * in the request's header fields. When a request message contains
2965 * both a message-body of non-zero length and a method that does
2966 * not define any semantics for that request message-body, then an
2967 * origin server SHOULD either ignore the message-body or respond
2968 * with an appropriate error message (e.g., 413). A proxy or
2969 * gateway, when presented the same request, SHOULD either forward
2970 * the request inbound with the message- body or ignore the
2971 * message-body when determining a response.
2972 *
2973 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2974 * and the "chunked" transfer-coding (Section 6.2) is used, the
2975 * transfer-length is defined by the use of this transfer-coding.
2976 * If a Transfer-Encoding header field is present and the "chunked"
2977 * transfer-coding is not present, the transfer-length is defined
2978 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002979 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002980 * 3. If a Content-Length header field is present, its decimal value in
2981 * OCTETs represents both the entity-length and the transfer-length.
2982 * If a message is received with both a Transfer-Encoding header
2983 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002984 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002985 * 4. By the server closing the connection. (Closing the connection
2986 * cannot be used to indicate the end of a request body, since that
2987 * would leave no possibility for the server to send back a response.)
2988 *
2989 * Whenever a transfer-coding is applied to a message-body, the set of
2990 * transfer-codings MUST include "chunked", unless the message indicates
2991 * it is terminated by closing the connection. When the "chunked"
2992 * transfer-coding is used, it MUST be the last transfer-coding applied
2993 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002994 */
2995
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002996 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002997 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002998 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002999 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003000 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003001 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003002 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3003 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003004 /* bad transfer-encoding (chunked followed by something else) */
3005 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003006 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003007 break;
3008 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003009 }
3010
Willy Tarreau32b47f42009-10-18 20:55:02 +02003011 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003012 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003013 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003014 signed long long cl;
3015
Willy Tarreauad14f752011-09-02 20:33:27 +02003016 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003017 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003018 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003019 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003020
Willy Tarreauad14f752011-09-02 20:33:27 +02003021 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003022 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003023 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003024 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003025
Willy Tarreauad14f752011-09-02 20:33:27 +02003026 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003027 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003028 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003029 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003030
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003031 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003032 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003033 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003034 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003035
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003036 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003037 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003038 }
3039
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003040 /* bodyless requests have a known length */
3041 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003042 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003043
Willy Tarreau179085c2014-04-28 16:48:56 +02003044 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3045 * only change if both the request and the config reference something else.
3046 * Option httpclose by itself sets tunnel mode where headers are mangled.
3047 * However, if another mode is set, it will affect it (eg: server-close/
3048 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3049 * if FE and BE have the same settings (common). The method consists in
3050 * checking if options changed between the two calls (implying that either
3051 * one is non-null, or one of them is non-null and we are there for the first
3052 * time.
3053 */
3054 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003055 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003056 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003057
Willy Tarreaud787e662009-07-07 10:14:51 +02003058 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003059 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003060 req->analyse_exp = TICK_ETERNITY;
3061 return 1;
3062
3063 return_bad_req:
3064 /* We centralize bad requests processing here */
3065 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3066 /* we detected a parsing error. We want to archive this request
3067 * in the dedicated proxy area for later troubleshooting.
3068 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003069 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003070 }
3071
3072 txn->req.msg_state = HTTP_MSG_ERROR;
3073 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003074 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003075
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003076 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003077 if (sess->listener->counters)
3078 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003079
3080 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003081 if (!(s->flags & SF_ERR_MASK))
3082 s->flags |= SF_ERR_PRXCOND;
3083 if (!(s->flags & SF_FINST_MASK))
3084 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003085
3086 req->analysers = 0;
3087 req->analyse_exp = TICK_ETERNITY;
3088 return 0;
3089}
3090
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003091
Willy Tarreau347a35d2013-11-22 17:51:09 +01003092/* This function prepares an applet to handle the stats. It can deal with the
3093 * "100-continue" expectation, check that admin rules are met for POST requests,
3094 * and program a response message if something was unexpected. It cannot fail
3095 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003096 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003097 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003098 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003099 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003100int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003101{
3102 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003103 struct stream_interface *si = &s->si[1];
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003104 struct http_txn *txn = &s->txn;
3105 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003106 struct uri_auth *uri_auth = s->be->uri_auth;
3107 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003108 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003109
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003110 appctx = si_appctx(si);
3111 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3112 appctx->st1 = appctx->st2 = 0;
3113 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3114 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003115 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3116 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003117
3118 uri = msg->chn->buf->p + msg->sl.rq.u;
3119 lookup = uri + uri_auth->uri_len;
3120
3121 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3122 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003123 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003124 break;
3125 }
3126 }
3127
3128 if (uri_auth->refresh) {
3129 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3130 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003131 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003132 break;
3133 }
3134 }
3135 }
3136
3137 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3138 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003139 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003140 break;
3141 }
3142 }
3143
3144 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3145 if (memcmp(h, ";st=", 4) == 0) {
3146 int i;
3147 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003148 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003149 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3150 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003151 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003152 break;
3153 }
3154 }
3155 break;
3156 }
3157 }
3158
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003159 appctx->ctx.stats.scope_str = 0;
3160 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003161 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3162 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3163 int itx = 0;
3164 const char *h2;
3165 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3166 const char *err;
3167
3168 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3169 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003170 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003171 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3172 itx++;
3173 h++;
3174 }
3175
3176 if (itx > STAT_SCOPE_TXT_MAXLEN)
3177 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003178 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003179
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003181 memcpy(scope_txt, h2, itx);
3182 scope_txt[itx] = '\0';
3183 err = invalid_char(scope_txt);
3184 if (err) {
3185 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003186 appctx->ctx.stats.scope_str = 0;
3187 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003188 }
3189 break;
3190 }
3191 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003192
3193 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003194 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003195 int ret = 1;
3196
3197 if (stats_admin_rule->cond) {
3198 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3199 ret = acl_pass(ret);
3200 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3201 ret = !ret;
3202 }
3203
3204 if (ret) {
3205 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003206 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003207 break;
3208 }
3209 }
3210
3211 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003212 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003214 /* we'll need the request body, possibly after sending 100-continue */
3215 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003216 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003217 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003218 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003219 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3220 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003221 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003222 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003223 else {
3224 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003225 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003226 }
3227
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003228 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003229 return 1;
3230}
3231
Lukas Tribus67db8df2013-06-23 17:37:13 +02003232/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3233 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3234 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003235void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003236{
3237#ifdef IP_TOS
3238 if (from.ss_family == AF_INET)
3239 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3240#endif
3241#ifdef IPV6_TCLASS
3242 if (from.ss_family == AF_INET6) {
3243 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3244 /* v4-mapped addresses need IP_TOS */
3245 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3246 else
3247 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3248 }
3249#endif
3250}
3251
Willy Tarreau87b09662015-04-03 00:22:06 +02003252int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003253 const char* name, unsigned int name_len,
3254 const char *str, struct my_regex *re,
3255 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003256{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003257 struct hdr_ctx ctx;
3258 char *buf = msg->chn->buf->p;
3259 struct hdr_idx *idx = &s->txn.hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003260 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3261 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003262 struct chunk *output = get_trash_chunk();
3263
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003264 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003265
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003266 /* Choose the header browsing function. */
3267 switch (action) {
3268 case HTTP_REQ_ACT_REPLACE_VAL:
3269 case HTTP_RES_ACT_REPLACE_VAL:
3270 http_find_hdr_func = http_find_header2;
3271 break;
3272 case HTTP_REQ_ACT_REPLACE_HDR:
3273 case HTTP_RES_ACT_REPLACE_HDR:
3274 http_find_hdr_func = http_find_full_header2;
3275 break;
3276 default: /* impossible */
3277 return -1;
3278 }
3279
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003280 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3281 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003282 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003283 char *val = ctx.line + ctx.val;
3284 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003285
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003286 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3287 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003288
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003289 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003290 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003291 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003292
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003293 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003294
3295 hdr->len += delta;
3296 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003297
3298 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003299 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003300 }
3301
3302 return 0;
3303}
3304
Willy Tarreau87b09662015-04-03 00:22:06 +02003305static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003306 const char* name, unsigned int name_len,
3307 struct list *fmt, struct my_regex *re,
3308 int action)
3309{
3310 struct chunk *replace = get_trash_chunk();
3311
3312 replace->len = build_logline(s, replace->str, replace->size, fmt);
3313 if (replace->len >= replace->size - 1)
3314 return -1;
3315
3316 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3317}
3318
Willy Tarreau87b09662015-04-03 00:22:06 +02003319/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003320 * transaction <txn>. Returns the verdict of the first rule that prevents
3321 * further processing of the request (auth, deny, ...), and defaults to
3322 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3323 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3324 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003325 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003326enum rule_result
Willy Tarreau87b09662015-04-03 00:22:06 +02003327http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003328{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003329 struct session *sess = strm_sess(s);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003330 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003331 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003332 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003333 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003334
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003335 /* If "the current_rule_list" match the executed rule list, we are in
3336 * resume condition. If a resume is needed it is always in the action
3337 * and never in the ACL or converters. In this case, we initialise the
3338 * current rule, and go to the action execution point.
3339 */
3340 if (s->current_rule_list == rules) {
3341 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3342 goto resume_execution;
3343 }
3344 s->current_rule_list = rules;
Willy Tarreauff011f22011-01-06 17:51:27 +01003345 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003346 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003347 continue;
3348
Willy Tarreau96257ec2012-12-27 10:46:37 +01003349 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003350 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003351 int ret;
3352
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003353 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003354 ret = acl_pass(ret);
3355
Willy Tarreauff011f22011-01-06 17:51:27 +01003356 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003357 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003358
3359 if (!ret) /* condition not matched */
3360 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003361 }
3362
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003363resume_execution:
Willy Tarreau20b0de52012-12-24 15:45:22 +01003364
Willy Tarreau96257ec2012-12-27 10:46:37 +01003365 switch (rule->action) {
3366 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003367 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003368
3369 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003370 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003371
Willy Tarreauccbcc372012-12-27 12:37:57 +01003372 case HTTP_REQ_ACT_TARPIT:
3373 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003374 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003375
Willy Tarreau96257ec2012-12-27 10:46:37 +01003376 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003377 /* Auth might be performed on regular http-req rules as well as on stats */
3378 auth_realm = rule->arg.auth.realm;
3379 if (!auth_realm) {
3380 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3381 auth_realm = STATS_DEFAULT_REALM;
3382 else
3383 auth_realm = px->id;
3384 }
3385 /* send 401/407 depending on whether we use a proxy or not. We still
3386 * count one error, because normal browsing won't significantly
3387 * increase the counter but brute force attempts will.
3388 */
3389 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3390 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3391 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003392 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003393 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003394
Willy Tarreau81499eb2012-12-27 12:19:02 +01003395 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003396 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3397 return HTTP_RULE_RES_BADREQ;
3398 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003399
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003400 case HTTP_REQ_ACT_SET_NICE:
3401 s->task->nice = rule->arg.nice;
3402 break;
3403
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003404 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003405 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003406 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003407 break;
3408
Willy Tarreau51347ed2013-06-11 19:34:13 +02003409 case HTTP_REQ_ACT_SET_MARK:
3410#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003411 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003412 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003413#endif
3414 break;
3415
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003416 case HTTP_REQ_ACT_SET_LOGL:
3417 s->logs.level = rule->arg.loglevel;
3418 break;
3419
Sasha Pachev218f0642014-06-16 12:05:59 -06003420 case HTTP_REQ_ACT_REPLACE_HDR:
3421 case HTTP_REQ_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003422 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3423 rule->arg.hdr_add.name_len,
3424 &rule->arg.hdr_add.fmt,
3425 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003426 return HTTP_RULE_RES_BADREQ;
3427 break;
3428
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003429 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003430 ctx.idx = 0;
3431 /* remove all occurrences of the header */
3432 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3433 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3434 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003435 }
Willy Tarreau85603282015-01-21 20:39:27 +01003436 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003437
Willy Tarreau85603282015-01-21 20:39:27 +01003438 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003439 case HTTP_REQ_ACT_ADD_HDR:
3440 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3441 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3442 trash.len = rule->arg.hdr_add.name_len;
3443 trash.str[trash.len++] = ':';
3444 trash.str[trash.len++] = ' ';
3445 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 +01003446
3447 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3448 /* remove all occurrences of the header */
3449 ctx.idx = 0;
3450 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3451 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3452 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3453 }
3454 }
3455
Willy Tarreau96257ec2012-12-27 10:46:37 +01003456 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3457 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003458
3459 case HTTP_REQ_ACT_DEL_ACL:
3460 case HTTP_REQ_ACT_DEL_MAP: {
3461 struct pat_ref *ref;
3462 char *key;
3463 int len;
3464
3465 /* collect reference */
3466 ref = pat_ref_lookup(rule->arg.map.ref);
3467 if (!ref)
3468 continue;
3469
3470 /* collect key */
3471 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3472 key = trash.str;
3473 key[len] = '\0';
3474
3475 /* perform update */
3476 /* returned code: 1=ok, 0=ko */
3477 pat_ref_delete(ref, key);
3478
3479 break;
3480 }
3481
3482 case HTTP_REQ_ACT_ADD_ACL: {
3483 struct pat_ref *ref;
3484 char *key;
3485 struct chunk *trash_key;
3486 int len;
3487
3488 trash_key = get_trash_chunk();
3489
3490 /* collect reference */
3491 ref = pat_ref_lookup(rule->arg.map.ref);
3492 if (!ref)
3493 continue;
3494
3495 /* collect key */
3496 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3497 key = trash_key->str;
3498 key[len] = '\0';
3499
3500 /* perform update */
3501 /* add entry only if it does not already exist */
3502 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003503 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003504
3505 break;
3506 }
3507
3508 case HTTP_REQ_ACT_SET_MAP: {
3509 struct pat_ref *ref;
3510 char *key, *value;
3511 struct chunk *trash_key, *trash_value;
3512 int len;
3513
3514 trash_key = get_trash_chunk();
3515 trash_value = get_trash_chunk();
3516
3517 /* collect reference */
3518 ref = pat_ref_lookup(rule->arg.map.ref);
3519 if (!ref)
3520 continue;
3521
3522 /* collect key */
3523 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3524 key = trash_key->str;
3525 key[len] = '\0';
3526
3527 /* collect value */
3528 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3529 value = trash_value->str;
3530 value[len] = '\0';
3531
3532 /* perform update */
3533 if (pat_ref_find_elt(ref, key) != NULL)
3534 /* update entry if it exists */
3535 pat_ref_set(ref, key, value, NULL);
3536 else
3537 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003538 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003539
3540 break;
3541 }
William Lallemand73025dd2014-04-24 14:38:37 +02003542
3543 case HTTP_REQ_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003544 if (!rule->action_ptr(rule, px, s, txn)) {
3545 s->current_rule = &rule->list;
3546 return HTTP_RULE_RES_YIELD;
3547 }
William Lallemand73025dd2014-04-24 14:38:37 +02003548 break;
3549
3550 case HTTP_REQ_ACT_CUSTOM_STOP:
3551 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003552 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003553
3554 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3555 /* Note: only the first valid tracking parameter of each
3556 * applies.
3557 */
3558
3559 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3560 struct stktable *t;
3561 struct stksess *ts;
3562 struct stktable_key *key;
3563 void *ptr;
3564
3565 t = rule->act_prm.trk_ctr.table.t;
3566 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
3567
3568 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003569 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003570
3571 /* let's count a new HTTP request as it's the first time we do it */
3572 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3573 if (ptr)
3574 stktable_data_cast(ptr, http_req_cnt)++;
3575
3576 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3577 if (ptr)
3578 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3579 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3580
3581 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003582 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003583 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3584 }
3585 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003586 }
3587 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003588
3589 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003590 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003591}
3592
Willy Tarreau71241ab2012-12-27 11:30:54 +01003593
Willy Tarreau87b09662015-04-03 00:22:06 +02003594/* Executes the http-response rules <rules> for stream <s>, proxy <px> and
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003595 * transaction <txn>. Returns 3 states: HTTP_RULE_RES_CONT, HTTP_RULE_RES_YIELD
3596 * or HTTP_RULE_RES_STOP. If *CONT is returned, the process can continue the
3597 * evaluation of next rule list. If *STOP is returned, the process must stop
3598 * the evaluation. It may set the TX_SVDENY on txn->flags if it encounters a deny
3599 * rule. If *YIELD is returned, the czller must call again the function with
3600 * the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003601 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003602static enum rule_result
Willy Tarreau87b09662015-04-03 00:22:06 +02003603http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, struct http_txn *txn)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003604{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003605 struct session *sess = strm_sess(s);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003606 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003607 struct http_res_rule *rule;
3608 struct hdr_ctx ctx;
3609
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003610 /* If "the current_rule_list" match the executed rule list, we are in
3611 * resume condition. If a resume is needed it is always in the action
3612 * and never in the ACL or converters. In this case, we initialise the
3613 * current rule, and go to the action execution point.
3614 */
3615 if (s->current_rule_list == rules) {
3616 rule = LIST_ELEM(s->current_rule, typeof(rule), list);
3617 goto resume_execution;
3618 }
3619 s->current_rule_list = rules;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003620 list_for_each_entry(rule, rules, list) {
3621 if (rule->action >= HTTP_RES_ACT_MAX)
3622 continue;
3623
3624 /* check optional condition */
3625 if (rule->cond) {
3626 int ret;
3627
3628 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3629 ret = acl_pass(ret);
3630
3631 if (rule->cond->pol == ACL_COND_UNLESS)
3632 ret = !ret;
3633
3634 if (!ret) /* condition not matched */
3635 continue;
3636 }
3637
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003638resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003639
3640 switch (rule->action) {
3641 case HTTP_RES_ACT_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003642 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003643
3644 case HTTP_RES_ACT_DENY:
3645 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003646 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003647
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003648 case HTTP_RES_ACT_SET_NICE:
3649 s->task->nice = rule->arg.nice;
3650 break;
3651
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003652 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003653 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003654 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003655 break;
3656
Willy Tarreau51347ed2013-06-11 19:34:13 +02003657 case HTTP_RES_ACT_SET_MARK:
3658#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003659 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003660 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003661#endif
3662 break;
3663
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003664 case HTTP_RES_ACT_SET_LOGL:
3665 s->logs.level = rule->arg.loglevel;
3666 break;
3667
Sasha Pachev218f0642014-06-16 12:05:59 -06003668 case HTTP_RES_ACT_REPLACE_HDR:
3669 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003670 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3671 rule->arg.hdr_add.name_len,
3672 &rule->arg.hdr_add.fmt,
3673 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003674 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003675 break;
3676
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003677 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003678 ctx.idx = 0;
3679 /* remove all occurrences of the header */
3680 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3681 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3682 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3683 }
Willy Tarreau85603282015-01-21 20:39:27 +01003684 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003685
Willy Tarreau85603282015-01-21 20:39:27 +01003686 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003687 case HTTP_RES_ACT_ADD_HDR:
3688 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3689 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3690 trash.len = rule->arg.hdr_add.name_len;
3691 trash.str[trash.len++] = ':';
3692 trash.str[trash.len++] = ' ';
3693 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 +01003694
3695 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3696 /* remove all occurrences of the header */
3697 ctx.idx = 0;
3698 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3699 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3700 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3701 }
3702 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003703 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3704 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003705
3706 case HTTP_RES_ACT_DEL_ACL:
3707 case HTTP_RES_ACT_DEL_MAP: {
3708 struct pat_ref *ref;
3709 char *key;
3710 int len;
3711
3712 /* collect reference */
3713 ref = pat_ref_lookup(rule->arg.map.ref);
3714 if (!ref)
3715 continue;
3716
3717 /* collect key */
3718 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3719 key = trash.str;
3720 key[len] = '\0';
3721
3722 /* perform update */
3723 /* returned code: 1=ok, 0=ko */
3724 pat_ref_delete(ref, key);
3725
3726 break;
3727 }
3728
3729 case HTTP_RES_ACT_ADD_ACL: {
3730 struct pat_ref *ref;
3731 char *key;
3732 struct chunk *trash_key;
3733 int len;
3734
3735 trash_key = get_trash_chunk();
3736
3737 /* collect reference */
3738 ref = pat_ref_lookup(rule->arg.map.ref);
3739 if (!ref)
3740 continue;
3741
3742 /* collect key */
3743 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3744 key = trash_key->str;
3745 key[len] = '\0';
3746
3747 /* perform update */
3748 /* check if the entry already exists */
3749 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003750 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003751
3752 break;
3753 }
3754
3755 case HTTP_RES_ACT_SET_MAP: {
3756 struct pat_ref *ref;
3757 char *key, *value;
3758 struct chunk *trash_key, *trash_value;
3759 int len;
3760
3761 trash_key = get_trash_chunk();
3762 trash_value = get_trash_chunk();
3763
3764 /* collect reference */
3765 ref = pat_ref_lookup(rule->arg.map.ref);
3766 if (!ref)
3767 continue;
3768
3769 /* collect key */
3770 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3771 key = trash_key->str;
3772 key[len] = '\0';
3773
3774 /* collect value */
3775 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3776 value = trash_value->str;
3777 value[len] = '\0';
3778
3779 /* perform update */
3780 if (pat_ref_find_elt(ref, key) != NULL)
3781 /* update entry if it exists */
3782 pat_ref_set(ref, key, value, NULL);
3783 else
3784 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003785 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003786
3787 break;
3788 }
William Lallemand73025dd2014-04-24 14:38:37 +02003789
3790 case HTTP_RES_ACT_CUSTOM_CONT:
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003791 if (!rule->action_ptr(rule, px, s, txn)) {
3792 s->current_rule = &rule->list;
3793 return HTTP_RULE_RES_YIELD;
3794 }
William Lallemand73025dd2014-04-24 14:38:37 +02003795 break;
3796
3797 case HTTP_RES_ACT_CUSTOM_STOP:
3798 rule->action_ptr(rule, px, s, txn);
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003799 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003800 }
3801 }
3802
3803 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003804 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003805}
3806
3807
Willy Tarreau71241ab2012-12-27 11:30:54 +01003808/* Perform an HTTP redirect based on the information in <rule>. The function
3809 * returns non-zero on success, or zero in case of a, irrecoverable error such
3810 * as too large a request to build a valid response.
3811 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003812static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003813{
3814 struct http_msg *msg = &txn->req;
3815 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003816 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003817
3818 /* build redirect message */
3819 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003820 case 308:
3821 msg_fmt = HTTP_308;
3822 break;
3823 case 307:
3824 msg_fmt = HTTP_307;
3825 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003826 case 303:
3827 msg_fmt = HTTP_303;
3828 break;
3829 case 301:
3830 msg_fmt = HTTP_301;
3831 break;
3832 case 302:
3833 default:
3834 msg_fmt = HTTP_302;
3835 break;
3836 }
3837
3838 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3839 return 0;
3840
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003841 location = trash.str + trash.len;
3842
Willy Tarreau71241ab2012-12-27 11:30:54 +01003843 switch(rule->type) {
3844 case REDIRECT_TYPE_SCHEME: {
3845 const char *path;
3846 const char *host;
3847 struct hdr_ctx ctx;
3848 int pathlen;
3849 int hostlen;
3850
3851 host = "";
3852 hostlen = 0;
3853 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003854 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003855 host = ctx.line + ctx.val;
3856 hostlen = ctx.vlen;
3857 }
3858
3859 path = http_get_path(txn);
3860 /* build message using path */
3861 if (path) {
3862 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3863 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3864 int qs = 0;
3865 while (qs < pathlen) {
3866 if (path[qs] == '?') {
3867 pathlen = qs;
3868 break;
3869 }
3870 qs++;
3871 }
3872 }
3873 } else {
3874 path = "/";
3875 pathlen = 1;
3876 }
3877
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003878 if (rule->rdr_str) { /* this is an old "redirect" rule */
3879 /* check if we can add scheme + "://" + host + path */
3880 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3881 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003882
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003883 /* add scheme */
3884 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3885 trash.len += rule->rdr_len;
3886 }
3887 else {
3888 /* add scheme with executing log format */
3889 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003890
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003891 /* check if we can add scheme + "://" + host + path */
3892 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3893 return 0;
3894 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003895 /* add "://" */
3896 memcpy(trash.str + trash.len, "://", 3);
3897 trash.len += 3;
3898
3899 /* add host */
3900 memcpy(trash.str + trash.len, host, hostlen);
3901 trash.len += hostlen;
3902
3903 /* add path */
3904 memcpy(trash.str + trash.len, path, pathlen);
3905 trash.len += pathlen;
3906
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003907 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003908 if (trash.len && trash.str[trash.len - 1] != '/' &&
3909 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3910 if (trash.len > trash.size - 5)
3911 return 0;
3912 trash.str[trash.len] = '/';
3913 trash.len++;
3914 }
3915
3916 break;
3917 }
3918 case REDIRECT_TYPE_PREFIX: {
3919 const char *path;
3920 int pathlen;
3921
3922 path = http_get_path(txn);
3923 /* build message using path */
3924 if (path) {
3925 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3926 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3927 int qs = 0;
3928 while (qs < pathlen) {
3929 if (path[qs] == '?') {
3930 pathlen = qs;
3931 break;
3932 }
3933 qs++;
3934 }
3935 }
3936 } else {
3937 path = "/";
3938 pathlen = 1;
3939 }
3940
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003941 if (rule->rdr_str) { /* this is an old "redirect" rule */
3942 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3943 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003944
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003945 /* add prefix. Note that if prefix == "/", we don't want to
3946 * add anything, otherwise it makes it hard for the user to
3947 * configure a self-redirection.
3948 */
3949 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3950 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3951 trash.len += rule->rdr_len;
3952 }
3953 }
3954 else {
3955 /* add prefix with executing log format */
3956 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3957
3958 /* Check length */
3959 if (trash.len + pathlen > trash.size - 4)
3960 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003961 }
3962
3963 /* add path */
3964 memcpy(trash.str + trash.len, path, pathlen);
3965 trash.len += pathlen;
3966
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003967 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003968 if (trash.len && trash.str[trash.len - 1] != '/' &&
3969 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3970 if (trash.len > trash.size - 5)
3971 return 0;
3972 trash.str[trash.len] = '/';
3973 trash.len++;
3974 }
3975
3976 break;
3977 }
3978 case REDIRECT_TYPE_LOCATION:
3979 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003980 if (rule->rdr_str) { /* this is an old "redirect" rule */
3981 if (trash.len + rule->rdr_len > trash.size - 4)
3982 return 0;
3983
3984 /* add location */
3985 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3986 trash.len += rule->rdr_len;
3987 }
3988 else {
3989 /* add location with executing log format */
3990 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003991
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003992 /* Check left length */
3993 if (trash.len > trash.size - 4)
3994 return 0;
3995 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003996 break;
3997 }
3998
3999 if (rule->cookie_len) {
4000 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4001 trash.len += 14;
4002 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4003 trash.len += rule->cookie_len;
4004 memcpy(trash.str + trash.len, "\r\n", 2);
4005 trash.len += 2;
4006 }
4007
4008 /* add end of headers and the keep-alive/close status.
4009 * We may choose to set keep-alive if the Location begins
4010 * with a slash, because the client will come back to the
4011 * same server.
4012 */
4013 txn->status = rule->code;
4014 /* let's log the request time */
4015 s->logs.tv_request = now;
4016
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004017 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004018 (msg->flags & HTTP_MSGF_XFER_LEN) &&
4019 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
4020 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4021 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4022 /* keep-alive possible */
4023 if (!(msg->flags & HTTP_MSGF_VER_11)) {
4024 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4025 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4026 trash.len += 30;
4027 } else {
4028 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4029 trash.len += 24;
4030 }
4031 }
4032 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4033 trash.len += 4;
4034 bo_inject(txn->rsp.chn, trash.str, trash.len);
4035 /* "eat" the request */
4036 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02004037 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004038 msg->sov = 0;
4039 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004040 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004041 txn->req.msg_state = HTTP_MSG_CLOSED;
4042 txn->rsp.msg_state = HTTP_MSG_DONE;
4043 } else {
4044 /* keep-alive not possible */
4045 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4046 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4047 trash.len += 29;
4048 } else {
4049 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4050 trash.len += 23;
4051 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004052 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004053 txn->req.chn->analysers = 0;
4054 }
4055
Willy Tarreaue7dff022015-04-03 01:14:29 +02004056 if (!(s->flags & SF_ERR_MASK))
4057 s->flags |= SF_ERR_LOCAL;
4058 if (!(s->flags & SF_FINST_MASK))
4059 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004060
4061 return 1;
4062}
4063
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004064/* This stream analyser runs all HTTP request processing which is common to
4065 * frontends and backends, which means blocking ACLs, filters, connection-close,
4066 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004067 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004068 * either needs more data or wants to immediately abort the request (eg: deny,
4069 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004070 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004071int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004072{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004073 struct session *sess = s->sess;
Willy Tarreaud787e662009-07-07 10:14:51 +02004074 struct http_txn *txn = &s->txn;
4075 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004076 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004077 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004078 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004079
Willy Tarreau655dce92009-11-08 13:10:58 +01004080 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004081 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004082 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004083 }
4084
Willy Tarreau87b09662015-04-03 00:22:06 +02004085 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 +02004086 now_ms, __FUNCTION__,
4087 s,
4088 req,
4089 req->rex, req->wex,
4090 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004091 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004092 req->analysers);
4093
Willy Tarreau65410832014-04-28 21:25:43 +02004094 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004095 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004096
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004097 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004098 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4099 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004100
Willy Tarreau0b748332014-04-29 00:13:29 +02004101 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004102 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4103 goto return_prx_yield;
4104
Willy Tarreau0b748332014-04-29 00:13:29 +02004105 case HTTP_RULE_RES_CONT:
4106 case HTTP_RULE_RES_STOP: /* nothing to do */
4107 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004108
Willy Tarreau0b748332014-04-29 00:13:29 +02004109 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4110 if (txn->flags & TX_CLTARPIT)
4111 goto tarpit;
4112 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004113
Willy Tarreau0b748332014-04-29 00:13:29 +02004114 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4115 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004116
Willy Tarreau0b748332014-04-29 00:13:29 +02004117 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004118 goto done;
4119
Willy Tarreau0b748332014-04-29 00:13:29 +02004120 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4121 goto return_bad_req;
4122 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004123 }
4124
Willy Tarreau52542592014-04-28 18:33:26 +02004125 /* OK at this stage, we know that the request was accepted according to
4126 * the http-request rules, we can check for the stats. Note that the
4127 * URI is detected *before* the req* rules in order not to be affected
4128 * by a possible reqrep, while they are processed *after* so that a
4129 * reqdeny can still block them. This clearly needs to change in 1.6!
4130 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004131 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004132 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004133 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004134 txn->status = 500;
4135 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004136 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004137
Willy Tarreaue7dff022015-04-03 01:14:29 +02004138 if (!(s->flags & SF_ERR_MASK))
4139 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004140 goto return_prx_cond;
4141 }
4142
4143 /* parse the whole stats request and extract the relevant information */
4144 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004145 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4146 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004147
Willy Tarreau0b748332014-04-29 00:13:29 +02004148 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4149 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004150
Willy Tarreau0b748332014-04-29 00:13:29 +02004151 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4152 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004153 }
4154
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004155 /* evaluate the req* rules except reqadd */
4156 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004157 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004158 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004159
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004160 if (txn->flags & TX_CLDENY)
4161 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004162
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004163 if (txn->flags & TX_CLTARPIT)
4164 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004165 }
Willy Tarreau06619262006-12-17 08:37:22 +01004166
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004167 /* add request headers from the rule sets in the same order */
4168 list_for_each_entry(wl, &px->req_add, list) {
4169 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004170 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004171 ret = acl_pass(ret);
4172 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4173 ret = !ret;
4174 if (!ret)
4175 continue;
4176 }
4177
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004178 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004179 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004180 }
4181
Willy Tarreau52542592014-04-28 18:33:26 +02004182
4183 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004184 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004185 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004186 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4187 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004188
Willy Tarreaue7dff022015-04-03 01:14:29 +02004189 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4190 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4191 if (!(s->flags & SF_FINST_MASK))
4192 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004193
Willy Tarreau70730dd2014-04-24 18:06:27 +02004194 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004195 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004196 select_compression_request_header(s, req->buf);
4197
4198 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004199 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004200 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004201 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004202
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004203 /* check whether we have some ACLs set to redirect this request */
4204 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004205 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004206 int ret;
4207
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004208 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004209 ret = acl_pass(ret);
4210 if (rule->cond->pol == ACL_COND_UNLESS)
4211 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004212 if (!ret)
4213 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004214 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004215 if (!http_apply_redirect_rule(rule, s, txn))
4216 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004217 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004218 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004219
Willy Tarreau2be39392010-01-03 17:24:51 +01004220 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4221 * If this happens, then the data will not come immediately, so we must
4222 * send all what we have without waiting. Note that due to the small gain
4223 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004224 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004225 * itself once used.
4226 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004227 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004228
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004229 done: /* done with this analyser, continue with next ones that the calling
4230 * points will have set, if any.
4231 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004232 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004233 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4234 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004235 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004236
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004237 tarpit:
4238 /* When a connection is tarpitted, we use the tarpit timeout,
4239 * which may be the same as the connect timeout if unspecified.
4240 * If unset, then set it to zero because we really want it to
4241 * eventually expire. We build the tarpit as an analyser.
4242 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004243 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004244
4245 /* wipe the request out so that we can drop the connection early
4246 * if the client closes first.
4247 */
4248 channel_dont_connect(req);
4249 req->analysers = 0; /* remove switching rules etc... */
4250 req->analysers |= AN_REQ_HTTP_TARPIT;
4251 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4252 if (!req->analyse_exp)
4253 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004254 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004255 sess->fe->fe_counters.denied_req++;
4256 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004257 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004258 if (sess->listener->counters)
4259 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004260 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004261
4262 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004263 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004264 txn->status = 403;
4265 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004266 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_403));
Willy Tarreau87b09662015-04-03 00:22:06 +02004267 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004268 sess->fe->fe_counters.denied_req++;
4269 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004270 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004271 if (sess->listener->counters)
4272 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004273 goto return_prx_cond;
4274
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004275 return_bad_req:
4276 /* We centralize bad requests processing here */
4277 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4278 /* we detected a parsing error. We want to archive this request
4279 * in the dedicated proxy area for later troubleshooting.
4280 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004281 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004282 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004283
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004284 txn->req.msg_state = HTTP_MSG_ERROR;
4285 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004286 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004287
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004288 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004289 if (sess->listener->counters)
4290 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004291
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004292 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004293 if (!(s->flags & SF_ERR_MASK))
4294 s->flags |= SF_ERR_PRXCOND;
4295 if (!(s->flags & SF_FINST_MASK))
4296 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004297
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004298 req->analysers = 0;
4299 req->analyse_exp = TICK_ETERNITY;
4300 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004301
4302 return_prx_yield:
4303 channel_dont_connect(req);
4304 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004305}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004306
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004307/* This function performs all the processing enabled for the current request.
4308 * It returns 1 if the processing can continue on next analysers, or zero if it
4309 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004310 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004311 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004312int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004313{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004314 struct session *sess = s->sess;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004315 struct http_txn *txn = &s->txn;
4316 struct http_msg *msg = &txn->req;
Willy Tarreau350f4872014-11-28 14:42:25 +01004317 struct connection *cli_conn = objt_conn(s->si[1].end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004318
Willy Tarreau655dce92009-11-08 13:10:58 +01004319 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004320 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004321 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004322 return 0;
4323 }
4324
Willy Tarreau87b09662015-04-03 00:22:06 +02004325 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 +02004326 now_ms, __FUNCTION__,
4327 s,
4328 req,
4329 req->rex, req->wex,
4330 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004331 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004332 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004333
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004334 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004335 select_compression_request_header(s, req->buf);
4336
Willy Tarreau59234e92008-11-30 23:51:27 +01004337 /*
4338 * Right now, we know that we have processed the entire headers
4339 * and that unwanted requests have been filtered out. We can do
4340 * whatever we want with the remaining request. Also, now we
4341 * may have separate values for ->fe, ->be.
4342 */
Willy Tarreau06619262006-12-17 08:37:22 +01004343
Willy Tarreau59234e92008-11-30 23:51:27 +01004344 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004345 * If HTTP PROXY is set we simply get remote server address parsing
4346 * incoming request. Note that this requires that a connection is
4347 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004348 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004349 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004350 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004351 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004352
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004353 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau350f4872014-11-28 14:42:25 +01004354 if (unlikely((conn = si_alloc_conn(&s->si[1], 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004355 txn->req.msg_state = HTTP_MSG_ERROR;
4356 txn->status = 500;
4357 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004358 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004359
Willy Tarreaue7dff022015-04-03 01:14:29 +02004360 if (!(s->flags & SF_ERR_MASK))
4361 s->flags |= SF_ERR_RESOURCE;
4362 if (!(s->flags & SF_FINST_MASK))
4363 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004364
4365 return 0;
4366 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004367
4368 path = http_get_path(txn);
4369 url2sa(req->buf->p + msg->sl.rq.u,
4370 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004371 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004372 /* if the path was found, we have to remove everything between
4373 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4374 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4375 * u_l characters by a single "/".
4376 */
4377 if (path) {
4378 char *cur_ptr = req->buf->p;
4379 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4380 int delta;
4381
4382 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4383 http_msg_move_end(&txn->req, delta);
4384 cur_end += delta;
4385 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4386 goto return_bad_req;
4387 }
4388 else {
4389 char *cur_ptr = req->buf->p;
4390 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4391 int delta;
4392
4393 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4394 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4395 http_msg_move_end(&txn->req, delta);
4396 cur_end += delta;
4397 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4398 goto return_bad_req;
4399 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004400 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004401
Willy Tarreau59234e92008-11-30 23:51:27 +01004402 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004403 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004404 * Note that doing so might move headers in the request, but
4405 * the fields will stay coherent and the URI will not move.
4406 * This should only be performed in the backend.
4407 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004408 if ((s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004409 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4410 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004411
Willy Tarreau59234e92008-11-30 23:51:27 +01004412 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004413 * 8: the appsession cookie was looked up very early in 1.2,
4414 * so let's do the same now.
4415 */
4416
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004417 /* It needs to look into the URI unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004418 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004419 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 +01004420 }
4421
William Lallemanda73203e2012-03-12 12:48:57 +01004422 /* add unique-id if "header-unique-id" is specified */
4423
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004424 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004425 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4426 goto return_bad_req;
4427 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004428 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004429 }
William Lallemanda73203e2012-03-12 12:48:57 +01004430
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004431 if (sess->fe->header_unique_id && s->unique_id) {
4432 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004433 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004434 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004435 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004436 goto return_bad_req;
4437 }
4438
Cyril Bontéb21570a2009-11-29 20:04:48 +01004439 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004440 * 9: add X-Forwarded-For if either the frontend or the backend
4441 * asks for it.
4442 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004443 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004444 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004445 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4446 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4447 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004448 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004449 /* The header is set to be added only if none is present
4450 * and we found it, so don't do anything.
4451 */
4452 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004453 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004454 /* Add an X-Forwarded-For header unless the source IP is
4455 * in the 'except' network range.
4456 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004457 if ((!sess->fe->except_mask.s_addr ||
4458 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4459 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004460 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004461 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004462 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004463 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004464 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004465 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004466
4467 /* Note: we rely on the backend to get the header name to be used for
4468 * x-forwarded-for, because the header is really meant for the backends.
4469 * However, if the backend did not specify any option, we have to rely
4470 * on the frontend's header name.
4471 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004472 if (s->be->fwdfor_hdr_len) {
4473 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004474 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004475 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004476 len = sess->fe->fwdfor_hdr_len;
4477 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004478 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004479 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 +01004480
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004481 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004482 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004483 }
4484 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004485 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004486 /* FIXME: for the sake of completeness, we should also support
4487 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004488 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004489 int len;
4490 char pn[INET6_ADDRSTRLEN];
4491 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004492 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004493 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004494
Willy Tarreau59234e92008-11-30 23:51:27 +01004495 /* Note: we rely on the backend to get the header name to be used for
4496 * x-forwarded-for, because the header is really meant for the backends.
4497 * However, if the backend did not specify any option, we have to rely
4498 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004499 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004500 if (s->be->fwdfor_hdr_len) {
4501 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004502 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004503 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004504 len = sess->fe->fwdfor_hdr_len;
4505 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004506 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004507 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004508
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004509 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004510 goto return_bad_req;
4511 }
4512 }
4513
4514 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004515 * 10: add X-Original-To if either the frontend or the backend
4516 * asks for it.
4517 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004518 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004519
4520 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004521 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004522 /* Add an X-Original-To header unless the destination IP is
4523 * in the 'except' network range.
4524 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004525 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004526
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004527 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004528 ((!sess->fe->except_mask_to.s_addr ||
4529 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4530 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004531 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004532 (((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 +02004533 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004534 int len;
4535 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004536 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004537
4538 /* Note: we rely on the backend to get the header name to be used for
4539 * x-original-to, because the header is really meant for the backends.
4540 * However, if the backend did not specify any option, we have to rely
4541 * on the frontend's header name.
4542 */
4543 if (s->be->orgto_hdr_len) {
4544 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004545 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004546 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004547 len = sess->fe->orgto_hdr_len;
4548 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004549 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004550 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 +02004551
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004552 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004553 goto return_bad_req;
4554 }
4555 }
4556 }
4557
Willy Tarreau50fc7772012-11-11 22:19:57 +01004558 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4559 * If an "Upgrade" token is found, the header is left untouched in order not to have
4560 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4561 * "Upgrade" is present in the Connection header.
4562 */
4563 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4564 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004565 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004566 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004567 unsigned int want_flags = 0;
4568
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004569 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004570 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004571 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004572 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004573 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004574 want_flags |= TX_CON_CLO_SET;
4575 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004576 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004577 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004578 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004579 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004580 want_flags |= TX_CON_KAL_SET;
4581 }
4582
4583 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004584 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004585 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004586
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004587
Willy Tarreau522d6c02009-12-06 18:49:18 +01004588 /* If we have no server assigned yet and we're balancing on url_param
4589 * with a POST request, we may be interested in checking the body for
4590 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004591 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004592 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004593 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004594 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004595 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004596 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004597 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004598
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004599 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004600 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004601#ifdef TCP_QUICKACK
4602 /* We expect some data from the client. Unless we know for sure
4603 * we already have a full request, we have to re-enable quick-ack
4604 * in case we previously disabled it, otherwise we might cause
4605 * the client to delay further data.
4606 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004607 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004608 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004609 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004610 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004611 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004612#endif
4613 }
Willy Tarreau03945942009-12-22 16:50:27 +01004614
Willy Tarreau59234e92008-11-30 23:51:27 +01004615 /*************************************************************
4616 * OK, that's finished for the headers. We have done what we *
4617 * could. Let's switch to the DATA state. *
4618 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004619 req->analyse_exp = TICK_ETERNITY;
4620 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004621
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004622 /* if the server closes the connection, we want to immediately react
4623 * and close the socket to save packets and syscalls.
4624 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004625 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004626 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004627
Willy Tarreau59234e92008-11-30 23:51:27 +01004628 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004629 /* OK let's go on with the BODY now */
4630 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004631
Willy Tarreau59234e92008-11-30 23:51:27 +01004632 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004633 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004634 /* we detected a parsing error. We want to archive this request
4635 * in the dedicated proxy area for later troubleshooting.
4636 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004637 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004638 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004639
Willy Tarreau59234e92008-11-30 23:51:27 +01004640 txn->req.msg_state = HTTP_MSG_ERROR;
4641 txn->status = 400;
4642 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004643 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004644
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004645 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004646 if (sess->listener->counters)
4647 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004648
Willy Tarreaue7dff022015-04-03 01:14:29 +02004649 if (!(s->flags & SF_ERR_MASK))
4650 s->flags |= SF_ERR_PRXCOND;
4651 if (!(s->flags & SF_FINST_MASK))
4652 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004653 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004654}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004655
Willy Tarreau60b85b02008-11-30 23:28:40 +01004656/* This function is an analyser which processes the HTTP tarpit. It always
4657 * returns zero, at the beginning because it prevents any other processing
4658 * from occurring, and at the end because it terminates the request.
4659 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004660int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004661{
4662 struct http_txn *txn = &s->txn;
4663
4664 /* This connection is being tarpitted. The CLIENT side has
4665 * already set the connect expiration date to the right
4666 * timeout. We just have to check that the client is still
4667 * there and that the timeout has not expired.
4668 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004669 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004670 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004671 !tick_is_expired(req->analyse_exp, now_ms))
4672 return 0;
4673
4674 /* We will set the queue timer to the time spent, just for
4675 * logging purposes. We fake a 500 server error, so that the
4676 * attacker will not suspect his connection has been tarpitted.
4677 * It will not cause trouble to the logs because we can exclude
4678 * the tarpitted connections by filtering on the 'PT' status flags.
4679 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004680 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4681
4682 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004683 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004684 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004685
4686 req->analysers = 0;
4687 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004688
Willy Tarreaue7dff022015-04-03 01:14:29 +02004689 if (!(s->flags & SF_ERR_MASK))
4690 s->flags |= SF_ERR_PRXCOND;
4691 if (!(s->flags & SF_FINST_MASK))
4692 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004693 return 0;
4694}
4695
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004696/* This function is an analyser which waits for the HTTP request body. It waits
4697 * for either the buffer to be full, or the full advertised contents to have
4698 * reached the buffer. It must only be called after the standard HTTP request
4699 * processing has occurred, because it expects the request to be parsed and will
4700 * look for the Expect header. It may send a 100-Continue interim response. It
4701 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4702 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4703 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004704 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004705int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004706{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004707 struct session *sess = s->sess;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004708 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004709 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004710
4711 /* We have to parse the HTTP request body to find any required data.
4712 * "balance url_param check_post" should have been the only way to get
4713 * into this. We were brought here after HTTP header analysis, so all
4714 * related structures are ready.
4715 */
4716
Willy Tarreau890988f2014-04-10 11:59:33 +02004717 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4718 /* This is the first call */
4719 if (msg->msg_state < HTTP_MSG_BODY)
4720 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004721
Willy Tarreau890988f2014-04-10 11:59:33 +02004722 if (msg->msg_state < HTTP_MSG_100_SENT) {
4723 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4724 * send an HTTP/1.1 100 Continue intermediate response.
4725 */
4726 if (msg->flags & HTTP_MSGF_VER_11) {
4727 struct hdr_ctx ctx;
4728 ctx.idx = 0;
4729 /* Expect is allowed in 1.1, look for it */
4730 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4731 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004732 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004733 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004734 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004735 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004736 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004737
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004738 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004739 * req->buf->p still points to the beginning of the message. We
4740 * must save the body in msg->next because it survives buffer
4741 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004742 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004743 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004744
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004745 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004746 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4747 else
4748 msg->msg_state = HTTP_MSG_DATA;
4749 }
4750
Willy Tarreau890988f2014-04-10 11:59:33 +02004751 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4752 /* We're in content-length mode, we just have to wait for enough data. */
4753 if (req->buf->i - msg->sov < msg->body_len)
4754 goto missing_data;
4755
4756 /* OK we have everything we need now */
4757 goto http_end;
4758 }
4759
4760 /* OK here we're parsing a chunked-encoded message */
4761
Willy Tarreau522d6c02009-12-06 18:49:18 +01004762 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004763 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004764 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004765 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004766 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004767 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004768
Willy Tarreau115acb92009-12-26 13:56:06 +01004769 if (!ret)
4770 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004771 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004772 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004773 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004774 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004775 }
4776
Willy Tarreaud98cf932009-12-27 22:54:55 +01004777 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004778 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004779 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004780 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004781 if (msg->msg_state == HTTP_MSG_TRAILERS)
4782 goto http_end;
4783
Willy Tarreau226071e2014-04-10 11:55:45 +02004784 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004785 goto http_end;
4786
4787 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004788 /* we get here if we need to wait for more data. If the buffer is full,
4789 * we have the maximum we can expect.
4790 */
4791 if (buffer_full(req->buf, global.tune.maxrewrite))
4792 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004793
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004794 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004795 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01004796 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004797
Willy Tarreaue7dff022015-04-03 01:14:29 +02004798 if (!(s->flags & SF_ERR_MASK))
4799 s->flags |= SF_ERR_CLITO;
4800 if (!(s->flags & SF_FINST_MASK))
4801 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004802 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004803 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004804
4805 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004806 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004807 /* Not enough data. We'll re-use the http-request
4808 * timeout here. Ideally, we should set the timeout
4809 * relative to the accept() date. We just set the
4810 * request timeout once at the beginning of the
4811 * request.
4812 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004813 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004814 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004815 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004816 return 0;
4817 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004818
4819 http_end:
4820 /* The situation will not evolve, so let's give up on the analysis. */
4821 s->logs.tv_request = now; /* update the request timer to reflect full request */
4822 req->analysers &= ~an_bit;
4823 req->analyse_exp = TICK_ETERNITY;
4824 return 1;
4825
4826 return_bad_req: /* let's centralize all bad requests */
4827 txn->req.msg_state = HTTP_MSG_ERROR;
4828 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004829 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004830
Willy Tarreaue7dff022015-04-03 01:14:29 +02004831 if (!(s->flags & SF_ERR_MASK))
4832 s->flags |= SF_ERR_PRXCOND;
4833 if (!(s->flags & SF_FINST_MASK))
4834 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004835
Willy Tarreau522d6c02009-12-06 18:49:18 +01004836 return_err_msg:
4837 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004838 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004839 if (sess->listener->counters)
4840 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004841 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004842}
4843
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004844/* send a server's name with an outgoing request over an established connection.
4845 * Note: this function is designed to be called once the request has been scheduled
4846 * for being forwarded. This is the reason why it rewinds the buffer before
4847 * proceeding.
4848 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004849int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004850
4851 struct hdr_ctx ctx;
4852
Mark Lamourinec2247f02012-01-04 13:02:01 -05004853 char *hdr_name = be->server_id_hdr_name;
4854 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004855 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004856 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004857 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004858
William Lallemandd9e90662012-01-30 17:27:17 +01004859 ctx.idx = 0;
4860
Willy Tarreau211cdec2014-04-17 20:18:08 +02004861 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004862 if (old_o) {
4863 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004864 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004865 txn->req.next += old_o;
4866 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004867 }
4868
Willy Tarreau9b28e032012-10-12 23:49:43 +02004869 old_i = chn->buf->i;
4870 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 -05004871 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004872 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004873 }
4874
4875 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004876 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004877 memcpy(hdr_val, hdr_name, hdr_name_len);
4878 hdr_val += hdr_name_len;
4879 *hdr_val++ = ':';
4880 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004881 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4882 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004883
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004884 if (old_o) {
4885 /* If this was a forwarded request, we must readjust the amount of
4886 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004887 * variations. Note that the current state is >= HTTP_MSG_BODY,
4888 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004889 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004890 old_o += chn->buf->i - old_i;
4891 b_adv(chn->buf, old_o);
4892 txn->req.next -= old_o;
4893 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004894 }
4895
Mark Lamourinec2247f02012-01-04 13:02:01 -05004896 return 0;
4897}
4898
Willy Tarreau610ecce2010-01-04 21:15:02 +01004899/* Terminate current transaction and prepare a new one. This is very tricky
4900 * right now but it works.
4901 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004902void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903{
Willy Tarreau068621e2013-12-23 15:11:25 +01004904 int prev_status = s->txn.status;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004905 struct proxy *fe = strm_sess(s)->fe;
Willy Tarreau068621e2013-12-23 15:11:25 +01004906
Willy Tarreau610ecce2010-01-04 21:15:02 +01004907 /* FIXME: We need a more portable way of releasing a backend's and a
4908 * server's connections. We need a safer way to reinitialize buffer
4909 * flags. We also need a more accurate method for computing per-request
4910 * data.
4911 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912
Willy Tarreau4213a112013-12-15 10:25:42 +01004913 /* unless we're doing keep-alive, we want to quickly close the connection
4914 * to the server.
4915 */
4916 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004917 !si_conn_ready(&s->si[1])) {
4918 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4919 si_shutr(&s->si[1]);
4920 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004921 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004922
Willy Tarreaue7dff022015-04-03 01:14:29 +02004923 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004924 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004925 if (unlikely(s->srv_conn))
4926 sess_change_server(s, NULL);
4927 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004928
4929 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004930 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004931
4932 if (s->txn.status) {
4933 int n;
4934
4935 n = s->txn.status / 100;
4936 if (n < 1 || n > 5)
4937 n = 0;
4938
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004939 if (fe->mode == PR_MODE_HTTP) {
4940 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004941 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004942 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004943 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004944 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004945 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004946 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004947 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02004948 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004949 s->be->be_counters.p.http.comp_rsp++;
4950 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004951 }
4952
4953 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004954 s->logs.bytes_in -= s->req.buf->i;
4955 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004956
4957 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004958 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004959 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004960 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004961 s->do_log(s);
4962 }
4963
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004964 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004965 stream_stop_content_counters(s);
4966 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004967
Willy Tarreau610ecce2010-01-04 21:15:02 +01004968 s->logs.accept_date = date; /* user-visible date for logging */
4969 s->logs.tv_accept = now; /* corrected date for internal use */
4970 tv_zero(&s->logs.tv_request);
4971 s->logs.t_queue = -1;
4972 s->logs.t_connect = -1;
4973 s->logs.t_data = -1;
4974 s->logs.t_close = 0;
4975 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4976 s->logs.srv_queue_size = 0; /* we will get this number soon */
4977
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004978 s->logs.bytes_in = s->req.total = s->req.buf->i;
4979 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004980
4981 if (s->pend_pos)
4982 pendconn_free(s->pend_pos);
4983
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004984 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004985 if (s->flags & SF_CURR_SESS) {
4986 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004987 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004988 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004989 if (may_dequeue_tasks(objt_server(s->target), s->be))
4990 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991 }
4992
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004993 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994
Willy Tarreau4213a112013-12-15 10:25:42 +01004995 /* only release our endpoint if we don't intend to reuse the
4996 * connection.
4997 */
4998 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004999 !si_conn_ready(&s->si[1])) {
5000 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005001 }
5002
Willy Tarreau350f4872014-11-28 14:42:25 +01005003 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5004 s->si[1].err_type = SI_ET_NONE;
5005 s->si[1].conn_retries = 0; /* used for logging too */
5006 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005007 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 +01005008 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);
5009 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 +02005010 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5011 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5012 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005013
Willy Tarreau610ecce2010-01-04 21:15:02 +01005014 s->txn.meth = 0;
5015 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01005016 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005017
5018 if (prev_status == 401 || prev_status == 407) {
5019 /* In HTTP keep-alive mode, if we receive a 401, we still have
5020 * a chance of being able to send the visitor again to the same
5021 * server over the same connection. This is required by some
5022 * broken protocols such as NTLM, and anyway whenever there is
5023 * an opportunity for sending the challenge to the proper place,
5024 * it's better to do it (at least it helps with debugging).
5025 */
5026 s->txn.flags |= TX_PREFER_LAST;
5027 }
5028
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005029 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005030 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005031
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005032 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005033 s->req.flags |= CF_NEVER_WAIT;
5034 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005035 }
5036
Willy Tarreau610ecce2010-01-04 21:15:02 +01005037 /* if the request buffer is not empty, it means we're
5038 * about to process another request, so send pending
5039 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005040 * Just don't do this if the buffer is close to be full,
5041 * because the request will wait for it to flush a little
5042 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005043 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005044 if (s->req.buf->i) {
5045 if (s->res.buf->o &&
5046 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5047 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5048 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005049 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005050
5051 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005052 channel_auto_read(&s->req);
5053 channel_auto_close(&s->req);
5054 channel_auto_read(&s->res);
5055 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005056
Willy Tarreau27375622013-12-17 00:00:28 +01005057 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau350f4872014-11-28 14:42:25 +01005058 si_idle_conn(&s->si[1]);
Willy Tarreau27375622013-12-17 00:00:28 +01005059
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005060 s->req.analysers = strm_sess(s)->listener->analysers;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005061 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005062}
5063
5064
5065/* This function updates the request state machine according to the response
5066 * state machine and buffer flags. It returns 1 if it changes anything (flag
5067 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5068 * it is only used to find when a request/response couple is complete. Both
5069 * this function and its equivalent should loop until both return zero. It
5070 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5071 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005072int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005073{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005074 struct channel *chn = &s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005075 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005076 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005077 unsigned int old_state = txn->req.msg_state;
5078
Willy Tarreau610ecce2010-01-04 21:15:02 +01005079 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5080 return 0;
5081
5082 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005083 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005084 * We can shut the read side unless we want to abort_on_close,
5085 * or we have a POST request. The issue with POST requests is
5086 * that some browsers still send a CRLF after the request, and
5087 * this CRLF must be read so that it does not remain in the kernel
5088 * buffers, otherwise a close could cause an RST on some systems
5089 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005090 * Note that if we're using keep-alive on the client side, we'd
5091 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005092 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005093 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005094 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005095 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5096 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5097 !(s->be->options & PR_O_ABRT_CLOSE) &&
5098 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005099 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005100
Willy Tarreau40f151a2012-12-20 12:10:09 +01005101 /* if the server closes the connection, we want to immediately react
5102 * and close the socket to save packets and syscalls.
5103 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005104 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005105
Willy Tarreau610ecce2010-01-04 21:15:02 +01005106 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5107 goto wait_other_side;
5108
5109 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5110 /* The server has not finished to respond, so we
5111 * don't want to move in order not to upset it.
5112 */
5113 goto wait_other_side;
5114 }
5115
5116 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5117 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005118 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005119 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005120 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121 goto wait_other_side;
5122 }
5123
5124 /* When we get here, it means that both the request and the
5125 * response have finished receiving. Depending on the connection
5126 * mode, we'll have to wait for the last bytes to leave in either
5127 * direction, and sometimes for a close to be effective.
5128 */
5129
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005130 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5131 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005132 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5133 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005134 }
5135 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5136 /* Option forceclose is set, or either side wants to close,
5137 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005138 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005139 * once both states are CLOSED.
5140 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005141 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5142 channel_shutr_now(chn);
5143 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005144 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005145 }
5146 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005147 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5148 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005149 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005150 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5151 channel_auto_read(chn);
5152 txn->req.msg_state = HTTP_MSG_TUNNEL;
5153 chn->flags |= CF_NEVER_WAIT;
5154 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005155 }
5156
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005157 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005159 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005160
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005161 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005162 txn->req.msg_state = HTTP_MSG_CLOSING;
5163 goto http_msg_closing;
5164 }
5165 else {
5166 txn->req.msg_state = HTTP_MSG_CLOSED;
5167 goto http_msg_closed;
5168 }
5169 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005170 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005171 }
5172
5173 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5174 http_msg_closing:
5175 /* nothing else to forward, just waiting for the output buffer
5176 * to be empty and for the shutw_now to take effect.
5177 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005178 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005179 txn->req.msg_state = HTTP_MSG_CLOSED;
5180 goto http_msg_closed;
5181 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005182 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005183 txn->req.msg_state = HTTP_MSG_ERROR;
5184 goto wait_other_side;
5185 }
5186 }
5187
5188 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5189 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005190 /* see above in MSG_DONE why we only do this in these states */
5191 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5192 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5193 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005194 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005195 goto wait_other_side;
5196 }
5197
5198 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005199 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200}
5201
5202
5203/* This function updates the response state machine according to the request
5204 * state machine and buffer flags. It returns 1 if it changes anything (flag
5205 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5206 * it is only used to find when a request/response couple is complete. Both
5207 * this function and its equivalent should loop until both return zero. It
5208 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5209 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005210int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005211{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005212 struct channel *chn = &s->res;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005213 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005214 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005215 unsigned int old_state = txn->rsp.msg_state;
5216
Willy Tarreau610ecce2010-01-04 21:15:02 +01005217 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5218 return 0;
5219
5220 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5221 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005222 * still monitor the server connection for a possible close
5223 * while the request is being uploaded, so we don't disable
5224 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005225 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005226 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005227
5228 if (txn->req.msg_state == HTTP_MSG_ERROR)
5229 goto wait_other_side;
5230
5231 if (txn->req.msg_state < HTTP_MSG_DONE) {
5232 /* The client seems to still be sending data, probably
5233 * because we got an error response during an upload.
5234 * We have the choice of either breaking the connection
5235 * or letting it pass through. Let's do the later.
5236 */
5237 goto wait_other_side;
5238 }
5239
5240 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5241 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005242 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005243 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005244 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005245 goto wait_other_side;
5246 }
5247
5248 /* When we get here, it means that both the request and the
5249 * response have finished receiving. Depending on the connection
5250 * mode, we'll have to wait for the last bytes to leave in either
5251 * direction, and sometimes for a close to be effective.
5252 */
5253
5254 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5255 /* Server-close mode : shut read and wait for the request
5256 * side to close its output buffer. The caller will detect
5257 * when we're in DONE and the other is in CLOSED and will
5258 * catch that for the final cleanup.
5259 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005260 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5261 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005262 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005263 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5264 /* Option forceclose is set, or either side wants to close,
5265 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005266 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005267 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005269 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5270 channel_shutr_now(chn);
5271 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005272 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005273 }
5274 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005275 /* The last possible modes are keep-alive and tunnel. Tunnel will
5276 * need to forward remaining data. Keep-alive will need to monitor
5277 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005278 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005279 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005280 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005281 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5282 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005283 }
5284
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005285 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005286 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005287 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005288 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5289 goto http_msg_closing;
5290 }
5291 else {
5292 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5293 goto http_msg_closed;
5294 }
5295 }
5296 goto wait_other_side;
5297 }
5298
5299 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5300 http_msg_closing:
5301 /* nothing else to forward, just waiting for the output buffer
5302 * to be empty and for the shutw_now to take effect.
5303 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005304 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005305 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5306 goto http_msg_closed;
5307 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005308 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005309 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005310 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005311 if (objt_server(s->target))
5312 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005313 goto wait_other_side;
5314 }
5315 }
5316
5317 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5318 http_msg_closed:
5319 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005320 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005321 channel_auto_close(chn);
5322 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005323 goto wait_other_side;
5324 }
5325
5326 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005327 /* We force the response to leave immediately if we're waiting for the
5328 * other side, since there is no pending shutdown to push it out.
5329 */
5330 if (!channel_is_empty(chn))
5331 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005332 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005333}
5334
5335
5336/* Resync the request and response state machines. Return 1 if either state
5337 * changes.
5338 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005339int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005340{
5341 struct http_txn *txn = &s->txn;
5342 int old_req_state = txn->req.msg_state;
5343 int old_res_state = txn->rsp.msg_state;
5344
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 http_sync_req_state(s);
5346 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 if (!http_sync_res_state(s))
5348 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 if (!http_sync_req_state(s))
5350 break;
5351 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005352
Willy Tarreau610ecce2010-01-04 21:15:02 +01005353 /* OK, both state machines agree on a compatible state.
5354 * There are a few cases we're interested in :
5355 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5356 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5357 * directions, so let's simply disable both analysers.
5358 * - HTTP_MSG_CLOSED on the response only means we must abort the
5359 * request.
5360 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5361 * with server-close mode means we've completed one request and we
5362 * must re-initialize the server connection.
5363 */
5364
5365 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5366 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5367 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5368 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005369 s->req.analysers = 0;
5370 channel_auto_close(&s->req);
5371 channel_auto_read(&s->req);
5372 s->res.analysers = 0;
5373 channel_auto_close(&s->res);
5374 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005375 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005376 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005377 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005378 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005379 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005380 s->res.analysers = 0;
5381 channel_auto_close(&s->res);
5382 channel_auto_read(&s->res);
5383 s->req.analysers = 0;
5384 channel_abort(&s->req);
5385 channel_auto_close(&s->req);
5386 channel_auto_read(&s->req);
5387 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005388 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005389 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5390 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005391 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005392 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5393 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5394 /* server-close/keep-alive: terminate this transaction,
5395 * possibly killing the server connection and reinitialize
5396 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005397 */
5398 http_end_txn_clean_session(s);
5399 }
5400
Willy Tarreau610ecce2010-01-04 21:15:02 +01005401 return txn->req.msg_state != old_req_state ||
5402 txn->rsp.msg_state != old_res_state;
5403}
5404
Willy Tarreaud98cf932009-12-27 22:54:55 +01005405/* This function is an analyser which forwards request body (including chunk
5406 * sizes if any). It is called as soon as we must forward, even if we forward
5407 * zero byte. The only situation where it must not be called is when we're in
5408 * tunnel mode and we want to forward till the close. It's used both to forward
5409 * remaining data and to resync after end of body. It expects the msg_state to
5410 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005411 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005412 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005413 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005414 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005415int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005416{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005417 struct session *sess = s->sess;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005418 struct http_txn *txn = &s->txn;
5419 struct http_msg *msg = &s->txn.req;
5420
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005421 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5422 return 0;
5423
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005424 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005425 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005426 /* Output closed while we were sending data. We must abort and
5427 * wake the other side up.
5428 */
5429 msg->msg_state = HTTP_MSG_ERROR;
5430 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005431 return 1;
5432 }
5433
Willy Tarreaud98cf932009-12-27 22:54:55 +01005434 /* Note that we don't have to send 100-continue back because we don't
5435 * need the data to complete our job, and it's up to the server to
5436 * decide whether to return 100, 417 or anything else in return of
5437 * an "Expect: 100-continue" header.
5438 */
5439
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005440 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005441 /* we have msg->sov which points to the first byte of message
5442 * body, and req->buf.p still points to the beginning of the
5443 * message. We forward the headers now, as we don't need them
5444 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005445 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005446 b_adv(req->buf, msg->sov);
5447 msg->next -= msg->sov;
5448 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005449
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005450 /* The previous analysers guarantee that the state is somewhere
5451 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5452 * msg->next are always correct.
5453 */
5454 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5455 if (msg->flags & HTTP_MSGF_TE_CHNK)
5456 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5457 else
5458 msg->msg_state = HTTP_MSG_DATA;
5459 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005460 }
5461
Willy Tarreau7ba23542014-04-17 21:50:00 +02005462 /* Some post-connect processing might want us to refrain from starting to
5463 * forward data. Currently, the only reason for this is "balance url_param"
5464 * whichs need to parse/process the request after we've enabled forwarding.
5465 */
5466 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005467 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005468 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005469 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005470 goto missing_data;
5471 }
5472 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5473 }
5474
Willy Tarreau80a92c02014-03-12 10:41:13 +01005475 /* in most states, we should abort in case of early close */
5476 channel_auto_close(req);
5477
Willy Tarreauefdf0942014-04-24 20:08:57 +02005478 if (req->to_forward) {
5479 /* We can't process the buffer's contents yet */
5480 req->flags |= CF_WAKE_WRITE;
5481 goto missing_data;
5482 }
5483
Willy Tarreaud98cf932009-12-27 22:54:55 +01005484 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005485 if (msg->msg_state == HTTP_MSG_DATA) {
5486 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005487 /* we may have some pending data starting at req->buf->p */
5488 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005489 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005490 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005491 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005492 msg->next += msg->chunk_len;
5493 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005494
5495 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005496 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005497 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005498 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005499 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005500 }
5501 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005502 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005503 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005504 * TRAILERS state.
5505 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005506 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005507
Willy Tarreau54d23df2012-10-25 19:04:45 +02005508 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005509 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005510 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005511 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005512 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005513 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005514 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005515 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005516 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005517 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005518 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005519 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005520 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005521
5522 if (ret == 0)
5523 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005524 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005525 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005526 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005527 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005529 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005530 /* we're in MSG_CHUNK_SIZE now */
5531 }
5532 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005533 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005534
5535 if (ret == 0)
5536 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005537 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005538 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005539 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005540 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005541 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005542 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543 /* we're in HTTP_MSG_DONE now */
5544 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005545 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005546 int old_state = msg->msg_state;
5547
Willy Tarreau610ecce2010-01-04 21:15:02 +01005548 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005549
5550 /* we may have some pending data starting at req->buf->p
5551 * such as last chunk of data or trailers.
5552 */
5553 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005554 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005555 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005556 msg->next = 0;
5557
Willy Tarreau4fe41902010-06-07 22:27:41 +02005558 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005559 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5560 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005561 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005562 if (http_resync_states(s)) {
5563 /* some state changes occurred, maybe the analyser
5564 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005565 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005566 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005567 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005568 /* request errors are most likely due to
5569 * the server aborting the transfer.
5570 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005571 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005572 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005573 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005574 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005575 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005576 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005577 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005578 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005579
5580 /* If "option abortonclose" is set on the backend, we
5581 * want to monitor the client's connection and forward
5582 * any shutdown notification to the server, which will
5583 * decide whether to close or to go on processing the
5584 * request.
5585 */
5586 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005587 channel_auto_read(req);
5588 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005589 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005590 else if (s->txn.meth == HTTP_METH_POST) {
5591 /* POST requests may require to read extra CRLF
5592 * sent by broken browsers and which could cause
5593 * an RST to be sent upon close on some systems
5594 * (eg: Linux).
5595 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005596 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005597 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005598
Willy Tarreau610ecce2010-01-04 21:15:02 +01005599 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005600 }
5601 }
5602
Willy Tarreaud98cf932009-12-27 22:54:55 +01005603 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005604 /* we may have some pending data starting at req->buf->p */
5605 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005606 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005607 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5608
Willy Tarreaubed410e2014-04-22 08:19:34 +02005609 msg->next = 0;
5610 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5611
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005612 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005613 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005614 if (!(s->flags & SF_ERR_MASK))
5615 s->flags |= SF_ERR_CLICL;
5616 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005617 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005618 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005619 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005620 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005621 }
5622
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005623 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005624 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005625 if (objt_server(s->target))
5626 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005627
5628 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005629 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005630
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005631 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005632 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005633 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005634
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005635 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005636 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005637 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005638 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005639 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005640
Willy Tarreau5c620922011-05-11 19:56:11 +02005641 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005642 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005643 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005644 * modes are already handled by the stream sock layer. We must not do
5645 * this in content-length mode because it could present the MSG_MORE
5646 * flag with the last block of forwarded data, which would cause an
5647 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005648 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005649 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005650 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005651
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005652 return 0;
5653
Willy Tarreaud98cf932009-12-27 22:54:55 +01005654 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005655 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005656 if (sess->listener->counters)
5657 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005658
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005659 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005660 /* we may have some pending data starting at req->buf->p */
5661 b_adv(req->buf, msg->next);
5662 msg->next = 0;
5663
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005664 txn->req.msg_state = HTTP_MSG_ERROR;
5665 if (txn->status) {
5666 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005667 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005668 } else {
5669 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005670 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005671 }
5672 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005673 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005674
Willy Tarreaue7dff022015-04-03 01:14:29 +02005675 if (!(s->flags & SF_ERR_MASK))
5676 s->flags |= SF_ERR_PRXCOND;
5677 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005678 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005679 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005680 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005681 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005682 }
5683 return 0;
5684
5685 aborted_xfer:
5686 txn->req.msg_state = HTTP_MSG_ERROR;
5687 if (txn->status) {
5688 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005689 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005690 } else {
5691 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005692 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005693 }
5694 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005695 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005696
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005697 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005698 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005699 if (objt_server(s->target))
5700 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005701
Willy Tarreaue7dff022015-04-03 01:14:29 +02005702 if (!(s->flags & SF_ERR_MASK))
5703 s->flags |= SF_ERR_SRVCL;
5704 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005705 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005706 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005707 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005708 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005709 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005710 return 0;
5711}
5712
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005713/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5714 * processing can continue on next analysers, or zero if it either needs more
5715 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005716 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005717 * when it has nothing left to do, and may remove any analyser when it wants to
5718 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005719 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005720int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005721{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005722 struct session *sess = s->sess;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005723 struct http_txn *txn = &s->txn;
5724 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005725 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005726 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005727 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005728 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005729
Willy Tarreau87b09662015-04-03 00:22:06 +02005730 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 +02005731 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005732 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005733 rep,
5734 rep->rex, rep->wex,
5735 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005736 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005737 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005738
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005739 /*
5740 * Now parse the partial (or complete) lines.
5741 * We will check the response syntax, and also join multi-line
5742 * headers. An index of all the lines will be elaborated while
5743 * parsing.
5744 *
5745 * For the parsing, we use a 28 states FSM.
5746 *
5747 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005748 * rep->buf->p = beginning of response
5749 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5750 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005751 * msg->eol = end of current header or line (LF or CRLF)
5752 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005753 */
5754
Willy Tarreau628c40c2014-04-24 19:11:26 +02005755 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005756 /* There's a protected area at the end of the buffer for rewriting
5757 * purposes. We don't want to start to parse the request if the
5758 * protected area is affected, because we may have to move processed
5759 * data later, which is much more complicated.
5760 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005761 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005762 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005763 /* some data has still not left the buffer, wake us once that's done */
5764 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5765 goto abort_response;
5766 channel_dont_close(rep);
5767 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005768 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005769 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005770 }
5771
Willy Tarreau379357a2013-06-08 12:55:46 +02005772 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5773 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5774 buffer_slow_realign(rep->buf);
5775
Willy Tarreau9b28e032012-10-12 23:49:43 +02005776 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005777 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005778 }
5779
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005780 /* 1: we might have to print this header in debug mode */
5781 if (unlikely((global.mode & MODE_DEBUG) &&
5782 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005783 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005784 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005785
Willy Tarreau9b28e032012-10-12 23:49:43 +02005786 sol = rep->buf->p;
5787 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005788 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005789
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005790 sol += hdr_idx_first_pos(&txn->hdr_idx);
5791 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005792
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005793 while (cur_idx) {
5794 eol = sol + txn->hdr_idx.v[cur_idx].len;
5795 debug_hdr("srvhdr", s, sol, eol);
5796 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5797 cur_idx = txn->hdr_idx.v[cur_idx].next;
5798 }
5799 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005800
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 /*
5802 * Now we quickly check if we have found a full valid response.
5803 * If not so, we check the FD and buffer states before leaving.
5804 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005805 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005806 * responses are checked first.
5807 *
5808 * Depending on whether the client is still there or not, we
5809 * may send an error response back or not. Note that normally
5810 * we should only check for HTTP status there, and check I/O
5811 * errors somewhere else.
5812 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005813
Willy Tarreau655dce92009-11-08 13:10:58 +01005814 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005815 /* Invalid response */
5816 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5817 /* we detected a parsing error. We want to archive this response
5818 * in the dedicated proxy area for later troubleshooting.
5819 */
5820 hdr_response_bad:
5821 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005822 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005823
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005824 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005825 if (objt_server(s->target)) {
5826 objt_server(s->target)->counters.failed_resp++;
5827 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005828 }
Willy Tarreau64648412010-03-05 10:41:54 +01005829 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005830 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005831 rep->analysers = 0;
5832 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005833 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005834 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005835 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005836
Willy Tarreaue7dff022015-04-03 01:14:29 +02005837 if (!(s->flags & SF_ERR_MASK))
5838 s->flags |= SF_ERR_PRXCOND;
5839 if (!(s->flags & SF_FINST_MASK))
5840 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005841
5842 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005843 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005844
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005845 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005846 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005847 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005848 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005849 goto hdr_response_bad;
5850 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005851
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005852 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005853 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005854 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005855 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005856 else if (txn->flags & TX_NOT_FIRST)
5857 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005858
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005859 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005860 if (objt_server(s->target)) {
5861 objt_server(s->target)->counters.failed_resp++;
5862 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005863 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005864
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005865 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005866 rep->analysers = 0;
5867 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005868 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005869 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005870 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005871
Willy Tarreaue7dff022015-04-03 01:14:29 +02005872 if (!(s->flags & SF_ERR_MASK))
5873 s->flags |= SF_ERR_SRVCL;
5874 if (!(s->flags & SF_FINST_MASK))
5875 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005876 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005877 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005878
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005879 /* read timeout : return a 504 to the client. */
5880 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005881 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005882 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005883 else if (txn->flags & TX_NOT_FIRST)
5884 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005885
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005886 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005887 if (objt_server(s->target)) {
5888 objt_server(s->target)->counters.failed_resp++;
5889 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005890 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005891
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005892 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005893 rep->analysers = 0;
5894 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005895 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005896 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005897 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005898
Willy Tarreaue7dff022015-04-03 01:14:29 +02005899 if (!(s->flags & SF_ERR_MASK))
5900 s->flags |= SF_ERR_SRVTO;
5901 if (!(s->flags & SF_FINST_MASK))
5902 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005903 return 0;
5904 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005905
Willy Tarreauf003d372012-11-26 13:35:37 +01005906 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005907 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005908 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005909 s->be->be_counters.cli_aborts++;
5910 if (objt_server(s->target))
5911 objt_server(s->target)->counters.cli_aborts++;
5912
5913 rep->analysers = 0;
5914 channel_auto_close(rep);
5915
5916 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005917 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005918 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01005919
Willy Tarreaue7dff022015-04-03 01:14:29 +02005920 if (!(s->flags & SF_ERR_MASK))
5921 s->flags |= SF_ERR_CLICL;
5922 if (!(s->flags & SF_FINST_MASK))
5923 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005924
Willy Tarreau87b09662015-04-03 00:22:06 +02005925 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005926 return 0;
5927 }
5928
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005929 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005930 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005931 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005932 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005933 else if (txn->flags & TX_NOT_FIRST)
5934 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005935
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005936 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005937 if (objt_server(s->target)) {
5938 objt_server(s->target)->counters.failed_resp++;
5939 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005940 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005941
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005942 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005943 rep->analysers = 0;
5944 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005945 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005946 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01005947 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005948
Willy Tarreaue7dff022015-04-03 01:14:29 +02005949 if (!(s->flags & SF_ERR_MASK))
5950 s->flags |= SF_ERR_SRVCL;
5951 if (!(s->flags & SF_FINST_MASK))
5952 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005953 return 0;
5954 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005955
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005956 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005957 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005958 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005959 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005960 else if (txn->flags & TX_NOT_FIRST)
5961 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005962
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005963 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005964 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005965 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005966
Willy Tarreaue7dff022015-04-03 01:14:29 +02005967 if (!(s->flags & SF_ERR_MASK))
5968 s->flags |= SF_ERR_CLICL;
5969 if (!(s->flags & SF_FINST_MASK))
5970 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005971
Willy Tarreau87b09662015-04-03 00:22:06 +02005972 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005973 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005974 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005975
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005976 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005977 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005978 return 0;
5979 }
5980
5981 /* More interesting part now : we know that we have a complete
5982 * response which at least looks like HTTP. We have an indicator
5983 * of each header's length, so we can parse them quickly.
5984 */
5985
5986 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005987 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005988
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005989 /*
5990 * 1: get the status code
5991 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005992 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005993 if (n < 1 || n > 5)
5994 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005995 /* when the client triggers a 4xx from the server, it's most often due
5996 * to a missing object or permission. These events should be tracked
5997 * because if they happen often, it may indicate a brute force or a
5998 * vulnerability scan.
5999 */
6000 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006001 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006002
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006003 if (objt_server(s->target))
6004 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006005
Willy Tarreau5b154472009-12-21 20:11:07 +01006006 /* check if the response is HTTP/1.1 or above */
6007 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006008 ((rep->buf->p[5] > '1') ||
6009 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006010 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006011
6012 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006013 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 +01006014
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006015 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006016 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006017
Willy Tarreau9b28e032012-10-12 23:49:43 +02006018 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006019
Willy Tarreau39650402010-03-15 19:44:39 +01006020 /* Adjust server's health based on status code. Note: status codes 501
6021 * and 505 are triggered on demand by client request, so we must not
6022 * count them as server failures.
6023 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006024 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006025 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006026 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006027 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006028 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006029 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006030
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006031 /*
6032 * 2: check for cacheability.
6033 */
6034
6035 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006036 case 100:
6037 /*
6038 * We may be facing a 100-continue response, in which case this
6039 * is not the right response, and we're waiting for the next one.
6040 * Let's allow this response to go to the client and wait for the
6041 * next one.
6042 */
6043 hdr_idx_init(&txn->hdr_idx);
6044 msg->next -= channel_forward(rep, msg->next);
6045 msg->msg_state = HTTP_MSG_RPBEFORE;
6046 txn->status = 0;
6047 s->logs.t_data = -1; /* was not a response yet */
6048 goto next_one;
6049
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006050 case 200:
6051 case 203:
6052 case 206:
6053 case 300:
6054 case 301:
6055 case 410:
6056 /* RFC2616 @13.4:
6057 * "A response received with a status code of
6058 * 200, 203, 206, 300, 301 or 410 MAY be stored
6059 * by a cache (...) unless a cache-control
6060 * directive prohibits caching."
6061 *
6062 * RFC2616 @9.5: POST method :
6063 * "Responses to this method are not cacheable,
6064 * unless the response includes appropriate
6065 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006066 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006067 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006068 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006069 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6070 break;
6071 default:
6072 break;
6073 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006074
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006075 /*
6076 * 3: we may need to capture headers
6077 */
6078 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006079 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006080 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006081 txn->rsp.cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006082
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006083 /* 4: determine the transfer-length.
6084 * According to RFC2616 #4.4, amended by the HTTPbis working group,
6085 * the presence of a message-body in a RESPONSE and its transfer length
6086 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006087 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006088 * All responses to the HEAD request method MUST NOT include a
6089 * message-body, even though the presence of entity-header fields
6090 * might lead one to believe they do. All 1xx (informational), 204
6091 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6092 * message-body. All other responses do include a message-body,
6093 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006094 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006095 * 1. Any response which "MUST NOT" include a message-body (such as the
6096 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6097 * always terminated by the first empty line after the header fields,
6098 * regardless of the entity-header fields present in the message.
6099 *
6100 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6101 * the "chunked" transfer-coding (Section 6.2) is used, the
6102 * transfer-length is defined by the use of this transfer-coding.
6103 * If a Transfer-Encoding header field is present and the "chunked"
6104 * transfer-coding is not present, the transfer-length is defined by
6105 * the sender closing the connection.
6106 *
6107 * 3. If a Content-Length header field is present, its decimal value in
6108 * OCTETs represents both the entity-length and the transfer-length.
6109 * If a message is received with both a Transfer-Encoding header
6110 * field and a Content-Length header field, the latter MUST be ignored.
6111 *
6112 * 4. If the message uses the media type "multipart/byteranges", and
6113 * the transfer-length is not otherwise specified, then this self-
6114 * delimiting media type defines the transfer-length. This media
6115 * type MUST NOT be used unless the sender knows that the recipient
6116 * can parse it; the presence in a request of a Range header with
6117 * multiple byte-range specifiers from a 1.1 client implies that the
6118 * client can parse multipart/byteranges responses.
6119 *
6120 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006121 */
6122
6123 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006124 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006125 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006126 * FIXME: should we parse anyway and return an error on chunked encoding ?
6127 */
6128 if (txn->meth == HTTP_METH_HEAD ||
6129 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006130 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006131 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006132 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006133 goto skip_content_length;
6134 }
6135
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006136 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006137 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006138 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006139 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006140 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006141 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6142 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006143 /* bad transfer-encoding (chunked followed by something else) */
6144 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006145 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006146 break;
6147 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006148 }
6149
6150 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6151 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006152 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006153 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006154 signed long long cl;
6155
Willy Tarreauad14f752011-09-02 20:33:27 +02006156 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006157 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006158 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006159 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006160
Willy Tarreauad14f752011-09-02 20:33:27 +02006161 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
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; /* parse failure */
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 (cl < 0) {
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;
Willy Tarreauad14f752011-09-02 20:33:27 +02006169 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006170
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006171 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
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; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006174 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006175
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006176 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006177 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006178 }
6179
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006180 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006181 select_compression_response_header(s, rep->buf);
6182
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006183skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006184 /* Now we have to check if we need to modify the Connection header.
6185 * This is more difficult on the response than it is on the request,
6186 * because we can have two different HTTP versions and we don't know
6187 * how the client will interprete a response. For instance, let's say
6188 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6189 * HTTP/1.1 response without any header. Maybe it will bound itself to
6190 * HTTP/1.0 because it only knows about it, and will consider the lack
6191 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6192 * the lack of header as a keep-alive. Thus we will use two flags
6193 * indicating how a request MAY be understood by the client. In case
6194 * of multiple possibilities, we'll fix the header to be explicit. If
6195 * ambiguous cases such as both close and keepalive are seen, then we
6196 * will fall back to explicit close. Note that we won't take risks with
6197 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006198 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006199 */
6200
Willy Tarreaudc008c52010-02-01 16:20:08 +01006201 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6202 txn->status == 101)) {
6203 /* Either we've established an explicit tunnel, or we're
6204 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006205 * to understand the next protocols. We have to switch to tunnel
6206 * mode, so that we transfer the request and responses then let
6207 * this protocol pass unmodified. When we later implement specific
6208 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006209 * header which contains information about that protocol for
6210 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006211 */
6212 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6213 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006214 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6215 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006216 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006217 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006218 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006219
Willy Tarreau70dffda2014-01-30 03:07:23 +01006220 /* this situation happens when combining pretend-keepalive with httpclose. */
6221 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006222 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006223 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006224 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6225
Willy Tarreau60466522010-01-18 19:08:45 +01006226 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006227 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006228 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6229 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006230
Willy Tarreau60466522010-01-18 19:08:45 +01006231 /* now adjust header transformations depending on current state */
6232 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6233 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6234 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006235 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006236 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006237 }
Willy Tarreau60466522010-01-18 19:08:45 +01006238 else { /* SCL / KAL */
6239 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006240 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006241 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006242 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006243
Willy Tarreau60466522010-01-18 19:08:45 +01006244 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006245 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006246
Willy Tarreau60466522010-01-18 19:08:45 +01006247 /* Some keep-alive responses are converted to Server-close if
6248 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006249 */
Willy Tarreau60466522010-01-18 19:08:45 +01006250 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6251 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006252 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006253 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006254 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006255 }
6256
Willy Tarreau7959a552013-09-23 16:44:27 +02006257 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006258 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006259
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006260 /* end of job, return OK */
6261 rep->analysers &= ~an_bit;
6262 rep->analyse_exp = TICK_ETERNITY;
6263 channel_auto_close(rep);
6264 return 1;
6265
6266 abort_keep_alive:
6267 /* A keep-alive request to the server failed on a network error.
6268 * The client is required to retry. We need to close without returning
6269 * any other information so that the client retries.
6270 */
6271 txn->status = 0;
6272 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006273 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006274 channel_auto_close(rep);
6275 s->logs.logwait = 0;
6276 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006277 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006278 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006279 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006280 return 0;
6281}
6282
6283/* This function performs all the processing enabled for the current response.
6284 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006285 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006286 * other functions. It works like process_request (see indications above).
6287 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006288int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006289{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006290 struct session *sess = s->sess;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006291 struct http_txn *txn = &s->txn;
6292 struct http_msg *msg = &txn->rsp;
6293 struct proxy *cur_proxy;
6294 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006295 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006296
Willy Tarreau87b09662015-04-03 00:22:06 +02006297 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 +02006298 now_ms, __FUNCTION__,
6299 s,
6300 rep,
6301 rep->rex, rep->wex,
6302 rep->flags,
6303 rep->buf->i,
6304 rep->analysers);
6305
6306 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6307 return 0;
6308
Willy Tarreau70730dd2014-04-24 18:06:27 +02006309 /* The stats applet needs to adjust the Connection header but we don't
6310 * apply any filter there.
6311 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006312 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6313 rep->analysers &= ~an_bit;
6314 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006315 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006316 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006317
Willy Tarreau58975672014-04-24 21:13:57 +02006318 /*
6319 * We will have to evaluate the filters.
6320 * As opposed to version 1.2, now they will be evaluated in the
6321 * filters order and not in the header order. This means that
6322 * each filter has to be validated among all headers.
6323 *
6324 * Filters are tried with ->be first, then with ->fe if it is
6325 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006326 *
6327 * Maybe we are in resume condiion. In this case I choose the
6328 * "struct proxy" which contains the rule list matching the resume
6329 * pointer. If none of theses "struct proxy" match, I initialise
6330 * the process with the first one.
6331 *
6332 * In fact, I check only correspondance betwwen the current list
6333 * pointer and the ->fe rule list. If it doesn't match, I initialize
6334 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006335 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006336 if (s->current_rule_list == &sess->fe->http_res_rules)
6337 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006338 else
6339 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006340 while (1) {
6341 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006342
Willy Tarreau58975672014-04-24 21:13:57 +02006343 /* evaluate http-response rules */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006344 if (ret == HTTP_RULE_RES_CONT)
6345 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006346
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006347 /* we need to be called again. */
6348 if (ret == HTTP_RULE_RES_YIELD) {
6349 channel_dont_close(rep);
6350 return 0;
6351 }
6352
Willy Tarreau58975672014-04-24 21:13:57 +02006353 /* try headers filters */
6354 if (rule_set->rsp_exp != NULL) {
6355 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6356 return_bad_resp:
6357 if (objt_server(s->target)) {
6358 objt_server(s->target)->counters.failed_resp++;
6359 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006360 }
Willy Tarreau58975672014-04-24 21:13:57 +02006361 s->be->be_counters.failed_resp++;
6362 return_srv_prx_502:
6363 rep->analysers = 0;
6364 txn->status = 502;
6365 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006366 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006367 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006368 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006369 if (!(s->flags & SF_ERR_MASK))
6370 s->flags |= SF_ERR_PRXCOND;
6371 if (!(s->flags & SF_FINST_MASK))
6372 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006373 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006374 }
Willy Tarreau58975672014-04-24 21:13:57 +02006375 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006376
Willy Tarreau58975672014-04-24 21:13:57 +02006377 /* has the response been denied ? */
6378 if (txn->flags & TX_SVDENY) {
6379 if (objt_server(s->target))
6380 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006381
Willy Tarreau58975672014-04-24 21:13:57 +02006382 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006383 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006384 if (sess->listener->counters)
6385 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006386
Willy Tarreau58975672014-04-24 21:13:57 +02006387 goto return_srv_prx_502;
6388 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006389
Willy Tarreau58975672014-04-24 21:13:57 +02006390 /* add response headers from the rule sets in the same order */
6391 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006392 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006393 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006394 if (wl->cond) {
6395 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6396 ret = acl_pass(ret);
6397 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6398 ret = !ret;
6399 if (!ret)
6400 continue;
6401 }
6402 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6403 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006404 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006405
Willy Tarreau58975672014-04-24 21:13:57 +02006406 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006407 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006408 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006409 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006410 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006411
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006412 /* After this point, this anayzer can't return yield, so we can
6413 * remove the bit corresponding to this analyzer from the list.
6414 *
6415 * Note that the intermediate returns and goto found previously
6416 * reset the analyzers.
6417 */
6418 rep->analysers &= ~an_bit;
6419 rep->analyse_exp = TICK_ETERNITY;
6420
Willy Tarreau58975672014-04-24 21:13:57 +02006421 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006422 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006423 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006424
Willy Tarreau58975672014-04-24 21:13:57 +02006425 /*
6426 * Now check for a server cookie.
6427 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006428 if (s->be->cookie_name || s->be->appsession_name || sess->fe->capture_name ||
Willy Tarreau58975672014-04-24 21:13:57 +02006429 (s->be->options & PR_O_CHK_CACHE))
6430 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006431
Willy Tarreau58975672014-04-24 21:13:57 +02006432 /*
6433 * Check for cache-control or pragma headers if required.
6434 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006435 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 +02006436 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006437
Willy Tarreau58975672014-04-24 21:13:57 +02006438 /*
6439 * Add server cookie in the response if needed
6440 */
6441 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6442 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006443 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006444 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6445 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6446 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6447 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6448 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006449 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006450 /* the server is known, it's not the one the client requested, or the
6451 * cookie's last seen date needs to be refreshed. We have to
6452 * insert a set-cookie here, except if we want to insert only on POST
6453 * requests and this one isn't. Note that servers which don't have cookies
6454 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006455 */
Willy Tarreau58975672014-04-24 21:13:57 +02006456 if (!objt_server(s->target)->cookie) {
6457 chunk_printf(&trash,
6458 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6459 s->be->cookie_name);
6460 }
6461 else {
6462 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006463
Willy Tarreau58975672014-04-24 21:13:57 +02006464 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6465 /* emit last_date, which is mandatory */
6466 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6467 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6468 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006469
Willy Tarreau58975672014-04-24 21:13:57 +02006470 if (s->be->cookie_maxlife) {
6471 /* emit first_date, which is either the original one or
6472 * the current date.
6473 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006474 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006475 s30tob64(txn->cookie_first_date ?
6476 txn->cookie_first_date >> 2 :
6477 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006478 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006479 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006480 }
Willy Tarreau58975672014-04-24 21:13:57 +02006481 chunk_appendf(&trash, "; path=/");
6482 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006483
Willy Tarreau58975672014-04-24 21:13:57 +02006484 if (s->be->cookie_domain)
6485 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006486
Willy Tarreau58975672014-04-24 21:13:57 +02006487 if (s->be->ck_opts & PR_CK_HTTPONLY)
6488 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006489
Willy Tarreau58975672014-04-24 21:13:57 +02006490 if (s->be->ck_opts & PR_CK_SECURE)
6491 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006492
Willy Tarreau58975672014-04-24 21:13:57 +02006493 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6494 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006495
Willy Tarreau58975672014-04-24 21:13:57 +02006496 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006497 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006498 /* the server did not change, only the date was updated */
6499 txn->flags |= TX_SCK_UPDATED;
6500 else
6501 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006502
Willy Tarreau58975672014-04-24 21:13:57 +02006503 /* Here, we will tell an eventual cache on the client side that we don't
6504 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6505 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6506 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006507 */
Willy Tarreau58975672014-04-24 21:13:57 +02006508 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006509
Willy Tarreau58975672014-04-24 21:13:57 +02006510 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006511
Willy Tarreau58975672014-04-24 21:13:57 +02006512 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6513 "Cache-control: private", 22) < 0))
6514 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006515 }
Willy Tarreau58975672014-04-24 21:13:57 +02006516 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006517
Willy Tarreau58975672014-04-24 21:13:57 +02006518 /*
6519 * Check if result will be cacheable with a cookie.
6520 * We'll block the response if security checks have caught
6521 * nasty things such as a cacheable cookie.
6522 */
6523 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6524 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6525 (s->be->options & PR_O_CHK_CACHE)) {
6526 /* we're in presence of a cacheable response containing
6527 * a set-cookie header. We'll block it as requested by
6528 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006529 */
Willy Tarreau58975672014-04-24 21:13:57 +02006530 if (objt_server(s->target))
6531 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006532
Willy Tarreau58975672014-04-24 21:13:57 +02006533 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006534 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006535 if (sess->listener->counters)
6536 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006537
Willy Tarreau58975672014-04-24 21:13:57 +02006538 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6539 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6540 send_log(s->be, LOG_ALERT,
6541 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6542 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6543 goto return_srv_prx_502;
6544 }
Willy Tarreau03945942009-12-22 16:50:27 +01006545
Willy Tarreau70730dd2014-04-24 18:06:27 +02006546 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006547 /*
6548 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6549 * If an "Upgrade" token is found, the header is left untouched in order
6550 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006551 * if anything but "Upgrade" is present in the Connection header. We don't
6552 * want to touch any 101 response either since it's switching to another
6553 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006554 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006555 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006556 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006557 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006558 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6559 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006560
Willy Tarreau58975672014-04-24 21:13:57 +02006561 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6562 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6563 /* we want a keep-alive response here. Keep-alive header
6564 * required if either side is not 1.1.
6565 */
6566 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6567 want_flags |= TX_CON_KAL_SET;
6568 }
6569 else {
6570 /* we want a close response here. Close header required if
6571 * the server is 1.1, regardless of the client.
6572 */
6573 if (msg->flags & HTTP_MSGF_VER_11)
6574 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006575 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576
Willy Tarreau58975672014-04-24 21:13:57 +02006577 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6578 http_change_connection_header(txn, msg, want_flags);
6579 }
6580
6581 skip_header_mangling:
6582 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6583 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6584 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585
Willy Tarreau58975672014-04-24 21:13:57 +02006586 /* if the user wants to log as soon as possible, without counting
6587 * bytes from the server, then this is the right moment. We have
6588 * to temporarily assign bytes_out to log what we currently have.
6589 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006590 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006591 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6592 s->logs.bytes_out = txn->rsp.eoh;
6593 s->do_log(s);
6594 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006595 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006596 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006597}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598
Willy Tarreaud98cf932009-12-27 22:54:55 +01006599/* This function is an analyser which forwards response body (including chunk
6600 * sizes if any). It is called as soon as we must forward, even if we forward
6601 * zero byte. The only situation where it must not be called is when we're in
6602 * tunnel mode and we want to forward till the close. It's used both to forward
6603 * remaining data and to resync after end of body. It expects the msg_state to
6604 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006605 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006606 *
6607 * It is capable of compressing response data both in content-length mode and
6608 * in chunked mode. The state machines follows different flows depending on
6609 * whether content-length and chunked modes are used, since there are no
6610 * trailers in content-length :
6611 *
6612 * chk-mode cl-mode
6613 * ,----- BODY -----.
6614 * / \
6615 * V size > 0 V chk-mode
6616 * .--> SIZE -------------> DATA -------------> CRLF
6617 * | | size == 0 | last byte |
6618 * | v final crlf v inspected |
6619 * | TRAILERS -----------> DONE |
6620 * | |
6621 * `----------------------------------------------'
6622 *
6623 * Compression only happens in the DATA state, and must be flushed in final
6624 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6625 * is performed at once on final states for all bytes parsed, or when leaving
6626 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006627 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006628int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006629{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006630 struct session *sess = s->sess;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006631 struct http_txn *txn = &s->txn;
6632 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006633 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006634 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006635 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006636
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006637 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6638 return 0;
6639
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006640 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006641 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006642 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006643 /* Output closed while we were sending data. We must abort and
6644 * wake the other side up.
6645 */
6646 msg->msg_state = HTTP_MSG_ERROR;
6647 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006648 return 1;
6649 }
6650
Willy Tarreau4fe41902010-06-07 22:27:41 +02006651 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006652 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006653
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006654 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006655 /* we have msg->sov which points to the first byte of message
6656 * body, and res->buf.p still points to the beginning of the
6657 * message. We forward the headers now, as we don't need them
6658 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006659 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006660 b_adv(res->buf, msg->sov);
6661 msg->next -= msg->sov;
6662 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006663
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006664 /* The previous analysers guarantee that the state is somewhere
6665 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6666 * msg->next are always correct.
6667 */
6668 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6669 if (msg->flags & HTTP_MSGF_TE_CHNK)
6670 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6671 else
6672 msg->msg_state = HTTP_MSG_DATA;
6673 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006674 }
6675
Willy Tarreauefdf0942014-04-24 20:08:57 +02006676 if (res->to_forward) {
6677 /* We can't process the buffer's contents yet */
6678 res->flags |= CF_WAKE_WRITE;
6679 goto missing_data;
6680 }
6681
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006682 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6683 /* We need a compression buffer in the DATA state to put the
6684 * output of compressed data, and in CRLF state to let the
6685 * TRAILERS state finish the job of removing the trailing CRLF.
6686 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006687 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006688 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006689 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006690 goto aborted_xfer; /* no memory */
6691 }
6692
6693 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006694 if (ret < 0) {
6695 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006696 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006697 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006698 compressing = 1;
6699 }
6700
Willy Tarreaud98cf932009-12-27 22:54:55 +01006701 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006702 switch (msg->msg_state - HTTP_MSG_DATA) {
6703 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006704 /* we may have some pending data starting at res->buf->p */
6705 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006706 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006707 if (ret < 0)
6708 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006709
Willy Tarreaud5a67832014-04-21 10:54:27 +02006710 if (msg->chunk_len) {
6711 /* input empty or output full */
6712 if (res->buf->i > msg->next)
6713 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006714 goto missing_data;
6715 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006716 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006717 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006718 if (msg->chunk_len > res->buf->i - msg->next) {
6719 /* output full */
6720 res->flags |= CF_WAKE_WRITE;
6721 goto missing_data;
6722 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006723 msg->next += msg->chunk_len;
6724 msg->chunk_len = 0;
6725 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006726
6727 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006728 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006729 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006730 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006731 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006732 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006733 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006734 /* fall through for HTTP_MSG_CHUNK_CRLF */
6735
6736 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6737 /* we want the CRLF after the data */
6738
6739 ret = http_skip_chunk_crlf(msg);
6740 if (ret == 0)
6741 goto missing_data;
6742 else if (ret < 0) {
6743 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006744 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006745 goto return_bad_res;
6746 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006747 /* we're in MSG_CHUNK_SIZE now, fall through */
6748
6749 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006750 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006751 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006752 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006753 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006754
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006755 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006756 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006757 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006758 else if (ret < 0) {
6759 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006760 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006761 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006762 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006763 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006764 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006765
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006766 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006767 if (unlikely(compressing)) {
6768 /* we need to flush output contents before syncing FSMs */
6769 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6770 compressing = 0;
6771 }
6772
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006773 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006774 if (ret == 0)
6775 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006776 else if (ret < 0) {
6777 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006778 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006779 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006780 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006781 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006782
6783 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006784 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006785 if (unlikely(compressing)) {
6786 /* we need to flush output contents before syncing FSMs */
6787 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6788 compressing = 0;
6789 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006790
Willy Tarreauc623c172014-04-18 09:53:50 +02006791 /* we may have some pending data starting at res->buf->p
6792 * such as a last chunk of data or trailers.
6793 */
6794 b_adv(res->buf, msg->next);
6795 msg->next = 0;
6796
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006797 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006798 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006799 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6800 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006801 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006802
Willy Tarreau610ecce2010-01-04 21:15:02 +01006803 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006804 /* some state changes occurred, maybe the analyser
6805 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006806 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006807 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006808 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006809 /* response errors are most likely due to
6810 * the client aborting the transfer.
6811 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006812 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006813 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006814 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006815 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006816 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006817 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006818 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006819 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006820 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006821 }
6822 }
6823
Willy Tarreaud98cf932009-12-27 22:54:55 +01006824 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006825 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006826 if (unlikely(compressing)) {
6827 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006828 compressing = 0;
6829 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006830
Willy Tarreauc623c172014-04-18 09:53:50 +02006831 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6832 b_adv(res->buf, msg->next);
6833 msg->next = 0;
6834 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6835 }
6836
Willy Tarreauf003d372012-11-26 13:35:37 +01006837 if (res->flags & CF_SHUTW)
6838 goto aborted_xfer;
6839
6840 /* stop waiting for data if the input is closed before the end. If the
6841 * client side was already closed, it means that the client has aborted,
6842 * so we don't want to count this as a server abort. Otherwise it's a
6843 * server abort.
6844 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006845 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006846 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006847 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006848 if (!(s->flags & SF_ERR_MASK))
6849 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006850 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006851 if (objt_server(s->target))
6852 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006853 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006854 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006855
Willy Tarreau40dba092010-03-04 18:14:51 +01006856 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006857 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006858 goto return_bad_res;
6859
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006860 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006861 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006862 * Similarly, with keep-alive on the client side, we don't want to forward a
6863 * close.
6864 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006865 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006866 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6867 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006868 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006869
Willy Tarreau5c620922011-05-11 19:56:11 +02006870 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006871 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006872 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006873 * modes are already handled by the stream sock layer. We must not do
6874 * this in content-length mode because it could present the MSG_MORE
6875 * flag with the last block of forwarded data, which would cause an
6876 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006877 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006878 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006879 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006880
Willy Tarreau87b09662015-04-03 00:22:06 +02006881 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006882 return 0;
6883
Willy Tarreau40dba092010-03-04 18:14:51 +01006884 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006885 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006886 if (objt_server(s->target))
6887 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006888
6889 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006890 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006891 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006892 compressing = 0;
6893 }
6894
Willy Tarreauc623c172014-04-18 09:53:50 +02006895 /* we may have some pending data starting at res->buf->p */
6896 if (s->comp_algo == NULL) {
6897 b_adv(res->buf, msg->next);
6898 msg->next = 0;
6899 }
6900
Willy Tarreaud98cf932009-12-27 22:54:55 +01006901 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006902 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006903 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006904 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006905 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006906 if (objt_server(s->target))
6907 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006908
Willy Tarreaue7dff022015-04-03 01:14:29 +02006909 if (!(s->flags & SF_ERR_MASK))
6910 s->flags |= SF_ERR_PRXCOND;
6911 if (!(s->flags & SF_FINST_MASK))
6912 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006913 return 0;
6914
6915 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006916 if (unlikely(compressing)) {
6917 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6918 compressing = 0;
6919 }
6920
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006921 txn->rsp.msg_state = HTTP_MSG_ERROR;
6922 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01006923 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006924 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006925 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006926
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006927 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006928 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006929 if (objt_server(s->target))
6930 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006931
Willy Tarreaue7dff022015-04-03 01:14:29 +02006932 if (!(s->flags & SF_ERR_MASK))
6933 s->flags |= SF_ERR_CLICL;
6934 if (!(s->flags & SF_FINST_MASK))
6935 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006936 return 0;
6937}
6938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006939/* Iterate the same filter through all request headers.
6940 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006941 * Since it can manage the switch to another backend, it updates the per-proxy
6942 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006943 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006944int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006946 char *cur_ptr, *cur_end, *cur_next;
6947 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006948 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006949 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006950 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006951
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006952 last_hdr = 0;
6953
Willy Tarreau9b28e032012-10-12 23:49:43 +02006954 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006955 old_idx = 0;
6956
6957 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006958 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006959 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006960 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006961 (exp->action == ACT_ALLOW ||
6962 exp->action == ACT_DENY ||
6963 exp->action == ACT_TARPIT))
6964 return 0;
6965
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006966 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006967 if (!cur_idx)
6968 break;
6969
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006970 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006971 cur_ptr = cur_next;
6972 cur_end = cur_ptr + cur_hdr->len;
6973 cur_next = cur_end + cur_hdr->cr + 1;
6974
6975 /* Now we have one header between cur_ptr and cur_end,
6976 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006977 */
6978
Willy Tarreau15a53a42015-01-21 13:39:42 +01006979 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006980 switch (exp->action) {
6981 case ACT_SETBE:
6982 /* It is not possible to jump a second time.
6983 * FIXME: should we return an HTTP/500 here so that
6984 * the admin knows there's a problem ?
6985 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006986 if (s->be != strm_sess(s)->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006987 break;
6988
6989 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02006990 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006991 last_hdr = 1;
6992 break;
6993
6994 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006995 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006996 last_hdr = 1;
6997 break;
6998
6999 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007000 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007001 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007002 break;
7003
7004 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007005 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007006 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007007 break;
7008
7009 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007010 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7011 if (trash.len < 0)
7012 return -1;
7013
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007014 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007015 /* FIXME: if the user adds a newline in the replacement, the
7016 * index will not be recalculated for now, and the new line
7017 * will not be counted as a new header.
7018 */
7019
7020 cur_end += delta;
7021 cur_next += delta;
7022 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007023 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007024 break;
7025
7026 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007027 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007028 cur_next += delta;
7029
Willy Tarreaufa355d42009-11-29 18:12:29 +01007030 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007031 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7032 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007033 cur_hdr->len = 0;
7034 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007035 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007036 break;
7037
7038 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039 }
7040
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007041 /* keep the link from this header to next one in case of later
7042 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007043 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044 old_idx = cur_idx;
7045 }
7046 return 0;
7047}
7048
7049
7050/* Apply the filter to the request line.
7051 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7052 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053 * Since it can manage the switch to another backend, it updates the per-proxy
7054 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007055 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007056int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007057{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007058 char *cur_ptr, *cur_end;
7059 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007060 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007061 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007062
Willy Tarreau3d300592007-03-18 18:34:41 +01007063 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007064 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007065 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007066 (exp->action == ACT_ALLOW ||
7067 exp->action == ACT_DENY ||
7068 exp->action == ACT_TARPIT))
7069 return 0;
7070 else if (exp->action == ACT_REMOVE)
7071 return 0;
7072
7073 done = 0;
7074
Willy Tarreau9b28e032012-10-12 23:49:43 +02007075 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007076 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007077
7078 /* Now we have the request line between cur_ptr and cur_end */
7079
Willy Tarreau15a53a42015-01-21 13:39:42 +01007080 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007081 switch (exp->action) {
7082 case ACT_SETBE:
7083 /* It is not possible to jump a second time.
7084 * FIXME: should we return an HTTP/500 here so that
7085 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007086 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007087 if (s->be != strm_sess(s)->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007088 break;
7089
7090 /* Swithing Proxy */
Willy Tarreau87b09662015-04-03 00:22:06 +02007091 stream_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007092 done = 1;
7093 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007095 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007096 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007097 done = 1;
7098 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007099
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007100 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007101 txn->flags |= TX_CLDENY;
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_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007106 txn->flags |= TX_CLTARPIT;
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_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007111 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7112 if (trash.len < 0)
7113 return -1;
7114
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007115 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007116 /* FIXME: if the user adds a newline in the replacement, the
7117 * index will not be recalculated for now, and the new line
7118 * will not be counted as a new header.
7119 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007120
Willy Tarreaufa355d42009-11-29 18:12:29 +01007121 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007122 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007123 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007124 HTTP_MSG_RQMETH,
7125 cur_ptr, cur_end + 1,
7126 NULL, NULL);
7127 if (unlikely(!cur_end))
7128 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007129
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007130 /* we have a full request and we know that we have either a CR
7131 * or an LF at <ptr>.
7132 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007133 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7134 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007135 /* there is no point trying this regex on headers */
7136 return 1;
7137 }
7138 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007139 return done;
7140}
Willy Tarreau97de6242006-12-27 17:18:38 +01007141
Willy Tarreau58f10d72006-12-04 02:26:12 +01007142
Willy Tarreau58f10d72006-12-04 02:26:12 +01007143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007144/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007145 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007146 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007147 * unparsable request. Since it can manage the switch to another backend, it
7148 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007149 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007150int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007151{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007152 struct http_txn *txn = &s->txn;
7153 struct hdr_exp *exp;
7154
7155 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007156 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007157
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007158 /*
7159 * The interleaving of transformations and verdicts
7160 * makes it difficult to decide to continue or stop
7161 * the evaluation.
7162 */
7163
Willy Tarreau6c123b12010-01-28 20:22:06 +01007164 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7165 break;
7166
Willy Tarreau3d300592007-03-18 18:34:41 +01007167 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007168 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007169 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007170 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007171
7172 /* if this filter had a condition, evaluate it now and skip to
7173 * next filter if the condition does not match.
7174 */
7175 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007176 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007177 ret = acl_pass(ret);
7178 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7179 ret = !ret;
7180
7181 if (!ret)
7182 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007183 }
7184
7185 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007186 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007187 if (unlikely(ret < 0))
7188 return -1;
7189
7190 if (likely(ret == 0)) {
7191 /* The filter did not match the request, it can be
7192 * iterated through all headers.
7193 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007194 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7195 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007196 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007197 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007198 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007199}
7200
7201
Willy Tarreaua15645d2007-03-18 16:22:39 +01007202
Willy Tarreau58f10d72006-12-04 02:26:12 +01007203/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007204 * Try to retrieve the server associated to the appsession.
Willy Tarreau87b09662015-04-03 00:22:06 +02007205 * If the server is found, it's assigned to the stream.
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007206 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007207void manage_client_side_appsession(struct stream *s, const char *buf, int len) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007208 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007209 appsess *asession = NULL;
7210 char *sessid_temp = NULL;
7211
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007212 if (len > s->be->appsession_len) {
7213 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007214 }
7215
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007216 if (s->be->options2 & PR_O2_AS_REQL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007217 /* request-learn option is enabled : store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007218 if (txn->sessid != NULL) {
Willy Tarreau87b09662015-04-03 00:22:06 +02007219 /* free previously allocated memory as we don't need the stream id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007220 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007221 }
7222
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007223 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007224 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007225 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007226 return;
7227 }
7228
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007229 memcpy(txn->sessid, buf, len);
7230 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007231 }
7232
7233 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7234 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007235 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007236 return;
7237 }
7238
Cyril Bontéb21570a2009-11-29 20:04:48 +01007239 memcpy(sessid_temp, buf, len);
7240 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007241
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007242 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007243 /* free previously allocated memory */
7244 pool_free2(apools.sessid, sessid_temp);
7245
7246 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007247 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7248 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007249 asession->request_count++;
7250
7251 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007252 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007253
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007254 while (srv) {
7255 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007256 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007257 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007258 (s->flags & SF_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007259 /* we found the server and it's usable */
7260 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007261 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007262 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007263 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007264
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007265 break;
7266 } else {
7267 txn->flags &= ~TX_CK_MASK;
7268 txn->flags |= TX_CK_DOWN;
7269 }
7270 }
7271 srv = srv->next;
7272 }
7273 }
7274 }
7275}
7276
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007277/* Find the end of a cookie value contained between <s> and <e>. It works the
7278 * same way as with headers above except that the semi-colon also ends a token.
7279 * See RFC2965 for more information. Note that it requires a valid header to
7280 * return a valid result.
7281 */
7282char *find_cookie_value_end(char *s, const char *e)
7283{
7284 int quoted, qdpair;
7285
7286 quoted = qdpair = 0;
7287 for (; s < e; s++) {
7288 if (qdpair) qdpair = 0;
7289 else if (quoted) {
7290 if (*s == '\\') qdpair = 1;
7291 else if (*s == '"') quoted = 0;
7292 }
7293 else if (*s == '"') quoted = 1;
7294 else if (*s == ',' || *s == ';') return s;
7295 }
7296 return s;
7297}
7298
7299/* Delete a value in a header between delimiters <from> and <next> in buffer
7300 * <buf>. The number of characters displaced is returned, and the pointer to
7301 * the first delimiter is updated if required. The function tries as much as
7302 * possible to respect the following principles :
7303 * - replace <from> delimiter by the <next> one unless <from> points to a
7304 * colon, in which case <next> is simply removed
7305 * - set exactly one space character after the new first delimiter, unless
7306 * there are not enough characters in the block being moved to do so.
7307 * - remove unneeded spaces before the previous delimiter and after the new
7308 * one.
7309 *
7310 * It is the caller's responsibility to ensure that :
7311 * - <from> points to a valid delimiter or the colon ;
7312 * - <next> points to a valid delimiter or the final CR/LF ;
7313 * - there are non-space chars before <from> ;
7314 * - there is a CR/LF at or after <next>.
7315 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007316int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007317{
7318 char *prev = *from;
7319
7320 if (*prev == ':') {
7321 /* We're removing the first value, preserve the colon and add a
7322 * space if possible.
7323 */
7324 if (!http_is_crlf[(unsigned char)*next])
7325 next++;
7326 prev++;
7327 if (prev < next)
7328 *prev++ = ' ';
7329
7330 while (http_is_spht[(unsigned char)*next])
7331 next++;
7332 } else {
7333 /* Remove useless spaces before the old delimiter. */
7334 while (http_is_spht[(unsigned char)*(prev-1)])
7335 prev--;
7336 *from = prev;
7337
7338 /* copy the delimiter and if possible a space if we're
7339 * not at the end of the line.
7340 */
7341 if (!http_is_crlf[(unsigned char)*next]) {
7342 *prev++ = *next++;
7343 if (prev + 1 < next)
7344 *prev++ = ' ';
7345 while (http_is_spht[(unsigned char)*next])
7346 next++;
7347 }
7348 }
7349 return buffer_replace2(buf, prev, next, NULL, 0);
7350}
7351
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007352/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007353 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007354 * desirable to call it only when needed. This code is quite complex because
7355 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7356 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007357 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007358void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007359{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007360 struct http_txn *txn = &s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007361 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007362 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007363 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007364 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7365 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007366
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007367 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007368 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007369 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007370
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007371 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007372 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007373 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007374
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007375 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007376 hdr_beg = hdr_next;
7377 hdr_end = hdr_beg + cur_hdr->len;
7378 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007379
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007380 /* We have one full header between hdr_beg and hdr_end, and the
7381 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007382 * "Cookie:" headers.
7383 */
7384
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007385 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007386 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007387 old_idx = cur_idx;
7388 continue;
7389 }
7390
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007391 del_from = NULL; /* nothing to be deleted */
7392 preserve_hdr = 0; /* assume we may kill the whole header */
7393
Willy Tarreau58f10d72006-12-04 02:26:12 +01007394 /* Now look for cookies. Conforming to RFC2109, we have to support
7395 * attributes whose name begin with a '$', and associate them with
7396 * the right cookie, if we want to delete this cookie.
7397 * So there are 3 cases for each cookie read :
7398 * 1) it's a special attribute, beginning with a '$' : ignore it.
7399 * 2) it's a server id cookie that we *MAY* want to delete : save
7400 * some pointers on it (last semi-colon, beginning of cookie...)
7401 * 3) it's an application cookie : we *MAY* have to delete a previous
7402 * "special" cookie.
7403 * At the end of loop, if a "special" cookie remains, we may have to
7404 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007405 * *MUST* delete it.
7406 *
7407 * Note: RFC2965 is unclear about the processing of spaces around
7408 * the equal sign in the ATTR=VALUE form. A careful inspection of
7409 * the RFC explicitly allows spaces before it, and not within the
7410 * tokens (attrs or values). An inspection of RFC2109 allows that
7411 * too but section 10.1.3 lets one think that spaces may be allowed
7412 * after the equal sign too, resulting in some (rare) buggy
7413 * implementations trying to do that. So let's do what servers do.
7414 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7415 * allowed quoted strings in values, with any possible character
7416 * after a backslash, including control chars and delimitors, which
7417 * causes parsing to become ambiguous. Browsers also allow spaces
7418 * within values even without quotes.
7419 *
7420 * We have to keep multiple pointers in order to support cookie
7421 * removal at the beginning, middle or end of header without
7422 * corrupting the header. All of these headers are valid :
7423 *
7424 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7425 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7426 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7427 * | | | | | | | | |
7428 * | | | | | | | | hdr_end <--+
7429 * | | | | | | | +--> next
7430 * | | | | | | +----> val_end
7431 * | | | | | +-----------> val_beg
7432 * | | | | +--------------> equal
7433 * | | | +----------------> att_end
7434 * | | +---------------------> att_beg
7435 * | +--------------------------> prev
7436 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007437 */
7438
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007439 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7440 /* Iterate through all cookies on this line */
7441
7442 /* find att_beg */
7443 att_beg = prev + 1;
7444 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7445 att_beg++;
7446
7447 /* find att_end : this is the first character after the last non
7448 * space before the equal. It may be equal to hdr_end.
7449 */
7450 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007451
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007452 while (equal < hdr_end) {
7453 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007454 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007455 if (http_is_spht[(unsigned char)*equal++])
7456 continue;
7457 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007458 }
7459
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007460 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7461 * is between <att_beg> and <equal>, both may be identical.
7462 */
7463
7464 /* look for end of cookie if there is an equal sign */
7465 if (equal < hdr_end && *equal == '=') {
7466 /* look for the beginning of the value */
7467 val_beg = equal + 1;
7468 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7469 val_beg++;
7470
7471 /* find the end of the value, respecting quotes */
7472 next = find_cookie_value_end(val_beg, hdr_end);
7473
7474 /* make val_end point to the first white space or delimitor after the value */
7475 val_end = next;
7476 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7477 val_end--;
7478 } else {
7479 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007480 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007481
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007482 /* We have nothing to do with attributes beginning with '$'. However,
7483 * they will automatically be removed if a header before them is removed,
7484 * since they're supposed to be linked together.
7485 */
7486 if (*att_beg == '$')
7487 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007488
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007489 /* Ignore cookies with no equal sign */
7490 if (equal == next) {
7491 /* This is not our cookie, so we must preserve it. But if we already
7492 * scheduled another cookie for removal, we cannot remove the
7493 * complete header, but we can remove the previous block itself.
7494 */
7495 preserve_hdr = 1;
7496 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007497 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007498 val_end += delta;
7499 next += delta;
7500 hdr_end += delta;
7501 hdr_next += delta;
7502 cur_hdr->len += delta;
7503 http_msg_move_end(&txn->req, delta);
7504 prev = del_from;
7505 del_from = NULL;
7506 }
7507 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007508 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007509
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007510 /* if there are spaces around the equal sign, we need to
7511 * strip them otherwise we'll get trouble for cookie captures,
7512 * or even for rewrites. Since this happens extremely rarely,
7513 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007514 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007515 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7516 int stripped_before = 0;
7517 int stripped_after = 0;
7518
7519 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007520 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007521 equal += stripped_before;
7522 val_beg += stripped_before;
7523 }
7524
7525 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007526 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007527 val_beg += stripped_after;
7528 stripped_before += stripped_after;
7529 }
7530
7531 val_end += stripped_before;
7532 next += stripped_before;
7533 hdr_end += stripped_before;
7534 hdr_next += stripped_before;
7535 cur_hdr->len += stripped_before;
7536 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007537 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007538 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007539
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007540 /* First, let's see if we want to capture this cookie. We check
7541 * that we don't already have a client side cookie, because we
7542 * can only capture one. Also as an optimisation, we ignore
7543 * cookies shorter than the declared name.
7544 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007545 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7546 (val_end - att_beg >= sess->fe->capture_namelen) &&
7547 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007548 int log_len = val_end - att_beg;
7549
7550 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7551 Alert("HTTP logging : out of memory.\n");
7552 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007553 if (log_len > sess->fe->capture_len)
7554 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 memcpy(txn->cli_cookie, att_beg, log_len);
7556 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007557 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007558 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007559
Willy Tarreaubca99692010-10-06 19:25:55 +02007560 /* Persistence cookies in passive, rewrite or insert mode have the
7561 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007563 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007564 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007565 * For cookies in prefix mode, the form is :
7566 *
7567 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007569 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7570 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7571 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007572 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007573
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007574 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7575 * have the server ID between val_beg and delim, and the original cookie between
7576 * delim+1 and val_end. Otherwise, delim==val_end :
7577 *
7578 * Cookie: NAME=SRV; # in all but prefix modes
7579 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7580 * | || || | |+-> next
7581 * | || || | +--> val_end
7582 * | || || +---------> delim
7583 * | || |+------------> val_beg
7584 * | || +-------------> att_end = equal
7585 * | |+-----------------> att_beg
7586 * | +------------------> prev
7587 * +-------------------------> hdr_beg
7588 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007589
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007590 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007591 for (delim = val_beg; delim < val_end; delim++)
7592 if (*delim == COOKIE_DELIM)
7593 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007594 } else {
7595 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007597 /* Now check if the cookie contains a date field, which would
7598 * appear after a vertical bar ('|') just after the server name
7599 * and before the delimiter.
7600 */
7601 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7602 if (vbar1) {
7603 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007604 * right is the last seen date. It is a base64 encoded
7605 * 30-bit value representing the UNIX date since the
7606 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007607 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007608 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007609 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007610 if (val_end - vbar1 >= 5) {
7611 val = b64tos30(vbar1);
7612 if (val > 0)
7613 txn->cookie_last_date = val << 2;
7614 }
7615 /* look for a second vertical bar */
7616 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7617 if (vbar1 && (val_end - vbar1 > 5)) {
7618 val = b64tos30(vbar1 + 1);
7619 if (val > 0)
7620 txn->cookie_first_date = val << 2;
7621 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007622 }
7623 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007624
Willy Tarreauf64d1412010-10-07 20:06:11 +02007625 /* if the cookie has an expiration date and the proxy wants to check
7626 * it, then we do that now. We first check if the cookie is too old,
7627 * then only if it has expired. We detect strict overflow because the
7628 * time resolution here is not great (4 seconds). Cookies with dates
7629 * in the future are ignored if their offset is beyond one day. This
7630 * allows an admin to fix timezone issues without expiring everyone
7631 * and at the same time avoids keeping unwanted side effects for too
7632 * long.
7633 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007634 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7635 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007636 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007637 txn->flags &= ~TX_CK_MASK;
7638 txn->flags |= TX_CK_OLD;
7639 delim = val_beg; // let's pretend we have not found the cookie
7640 txn->cookie_first_date = 0;
7641 txn->cookie_last_date = 0;
7642 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007643 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7644 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007645 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007646 txn->flags &= ~TX_CK_MASK;
7647 txn->flags |= TX_CK_EXPIRED;
7648 delim = val_beg; // let's pretend we have not found the cookie
7649 txn->cookie_first_date = 0;
7650 txn->cookie_last_date = 0;
7651 }
7652
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 /* Here, we'll look for the first running server which supports the cookie.
7654 * This allows to share a same cookie between several servers, for example
7655 * to dedicate backup servers to specific servers only.
7656 * However, to prevent clients from sticking to cookie-less backup server
7657 * when they have incidentely learned an empty cookie, we simply ignore
7658 * empty cookies and mark them as invalid.
7659 * The same behaviour is applied when persistence must be ignored.
7660 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007661 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007662 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007663
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007664 while (srv) {
7665 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7666 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007667 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007668 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007669 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007670 /* we found the server and we can use it */
7671 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007672 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007673 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007674 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007675 break;
7676 } else {
7677 /* we found a server, but it's down,
7678 * mark it as such and go on in case
7679 * another one is available.
7680 */
7681 txn->flags &= ~TX_CK_MASK;
7682 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007683 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007684 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007685 srv = srv->next;
7686 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007687
Willy Tarreauf64d1412010-10-07 20:06:11 +02007688 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007689 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007691 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007692 txn->flags |= TX_CK_UNUSED;
7693 else
7694 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007695 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007696
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 /* depending on the cookie mode, we may have to either :
7698 * - delete the complete cookie if we're in insert+indirect mode, so that
7699 * the server never sees it ;
7700 * - remove the server id from the cookie value, and tag the cookie as an
7701 * application cookie so that it does not get accidentely removed later,
7702 * if we're in cookie prefix mode
7703 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007704 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007705 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007706
Willy Tarreau9b28e032012-10-12 23:49:43 +02007707 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007708 val_end += delta;
7709 next += delta;
7710 hdr_end += delta;
7711 hdr_next += delta;
7712 cur_hdr->len += delta;
7713 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007714
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007715 del_from = NULL;
7716 preserve_hdr = 1; /* we want to keep this cookie */
7717 }
7718 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007719 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 del_from = prev;
7721 }
7722 } else {
7723 /* This is not our cookie, so we must preserve it. But if we already
7724 * scheduled another cookie for removal, we cannot remove the
7725 * complete header, but we can remove the previous block itself.
7726 */
7727 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007730 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007731 if (att_beg >= del_from)
7732 att_beg += delta;
7733 if (att_end >= del_from)
7734 att_end += delta;
7735 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007736 val_end += delta;
7737 next += delta;
7738 hdr_end += delta;
7739 hdr_next += delta;
7740 cur_hdr->len += delta;
7741 http_msg_move_end(&txn->req, delta);
7742 prev = del_from;
7743 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007744 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007745 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007746
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007747 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007748 if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 int cmp_len, value_len;
7750 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007751
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007752 if (s->be->options2 & PR_O2_AS_PFX) {
7753 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7754 value_begin = att_beg + s->be->appsession_name_len;
7755 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007756 } else {
7757 cmp_len = att_end - att_beg;
7758 value_begin = val_beg;
7759 value_len = val_end - val_beg;
7760 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007761
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007762 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007763 if (cmp_len == s->be->appsession_name_len &&
7764 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7765 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007766 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007767 }
7768
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007769 /* continue with next cookie on this header line */
7770 att_beg = next;
7771 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007772
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007773 /* There are no more cookies on this line.
7774 * We may still have one (or several) marked for deletion at the
7775 * end of the line. We must do this now in two ways :
7776 * - if some cookies must be preserved, we only delete from the
7777 * mark to the end of line ;
7778 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007779 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007780 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007781 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007783 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007784 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007785 cur_hdr->len += delta;
7786 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007787 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007788
7789 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007790 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7791 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007792 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007793 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007794 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007795 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007796 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007797 }
7798
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007800 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007801 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007802}
7803
7804
Willy Tarreaua15645d2007-03-18 16:22:39 +01007805/* Iterate the same filter through all response headers contained in <rtr>.
7806 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7807 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007808int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007810 char *cur_ptr, *cur_end, *cur_next;
7811 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007812 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007813 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007814 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007815
7816 last_hdr = 0;
7817
Willy Tarreau9b28e032012-10-12 23:49:43 +02007818 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007819 old_idx = 0;
7820
7821 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007822 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007823 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007824 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825 (exp->action == ACT_ALLOW ||
7826 exp->action == ACT_DENY))
7827 return 0;
7828
7829 cur_idx = txn->hdr_idx.v[old_idx].next;
7830 if (!cur_idx)
7831 break;
7832
7833 cur_hdr = &txn->hdr_idx.v[cur_idx];
7834 cur_ptr = cur_next;
7835 cur_end = cur_ptr + cur_hdr->len;
7836 cur_next = cur_end + cur_hdr->cr + 1;
7837
7838 /* Now we have one header between cur_ptr and cur_end,
7839 * and the next header starts at cur_next.
7840 */
7841
Willy Tarreau15a53a42015-01-21 13:39:42 +01007842 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007843 switch (exp->action) {
7844 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007845 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846 last_hdr = 1;
7847 break;
7848
7849 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007850 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007851 last_hdr = 1;
7852 break;
7853
7854 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007855 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7856 if (trash.len < 0)
7857 return -1;
7858
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007859 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 /* FIXME: if the user adds a newline in the replacement, the
7861 * index will not be recalculated for now, and the new line
7862 * will not be counted as a new header.
7863 */
7864
7865 cur_end += delta;
7866 cur_next += delta;
7867 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007868 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007869 break;
7870
7871 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007872 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007873 cur_next += delta;
7874
Willy Tarreaufa355d42009-11-29 18:12:29 +01007875 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7877 txn->hdr_idx.used--;
7878 cur_hdr->len = 0;
7879 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007880 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007881 break;
7882
7883 }
7884 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885
7886 /* keep the link from this header to next one in case of later
7887 * removal of next header.
7888 */
7889 old_idx = cur_idx;
7890 }
7891 return 0;
7892}
7893
7894
7895/* Apply the filter to the status line in the response buffer <rtr>.
7896 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7897 * or -1 if a replacement resulted in an invalid status line.
7898 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007899int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007901 char *cur_ptr, *cur_end;
7902 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007903 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007904 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007905
7906
Willy Tarreau3d300592007-03-18 18:34:41 +01007907 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007908 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007909 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910 (exp->action == ACT_ALLOW ||
7911 exp->action == ACT_DENY))
7912 return 0;
7913 else if (exp->action == ACT_REMOVE)
7914 return 0;
7915
7916 done = 0;
7917
Willy Tarreau9b28e032012-10-12 23:49:43 +02007918 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007919 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007920
7921 /* Now we have the status line between cur_ptr and cur_end */
7922
Willy Tarreau15a53a42015-01-21 13:39:42 +01007923 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007924 switch (exp->action) {
7925 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007926 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007927 done = 1;
7928 break;
7929
7930 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007931 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007932 done = 1;
7933 break;
7934
7935 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007936 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7937 if (trash.len < 0)
7938 return -1;
7939
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007940 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007941 /* FIXME: if the user adds a newline in the replacement, the
7942 * index will not be recalculated for now, and the new line
7943 * will not be counted as a new header.
7944 */
7945
Willy Tarreaufa355d42009-11-29 18:12:29 +01007946 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007948 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007949 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007950 cur_ptr, cur_end + 1,
7951 NULL, NULL);
7952 if (unlikely(!cur_end))
7953 return -1;
7954
7955 /* we have a full respnse and we know that we have either a CR
7956 * or an LF at <ptr>.
7957 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007958 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007959 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007960 /* there is no point trying this regex on headers */
7961 return 1;
7962 }
7963 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007964 return done;
7965}
7966
7967
7968
7969/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007970 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7972 * unparsable response.
7973 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007974int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007975{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007976 struct http_txn *txn = &s->txn;
7977 struct hdr_exp *exp;
7978
7979 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980 int ret;
7981
7982 /*
7983 * The interleaving of transformations and verdicts
7984 * makes it difficult to decide to continue or stop
7985 * the evaluation.
7986 */
7987
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007988 if (txn->flags & TX_SVDENY)
7989 break;
7990
Willy Tarreau3d300592007-03-18 18:34:41 +01007991 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7993 exp->action == ACT_PASS)) {
7994 exp = exp->next;
7995 continue;
7996 }
7997
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007998 /* if this filter had a condition, evaluate it now and skip to
7999 * next filter if the condition does not match.
8000 */
8001 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008002 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008003 ret = acl_pass(ret);
8004 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8005 ret = !ret;
8006 if (!ret)
8007 continue;
8008 }
8009
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008011 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 if (unlikely(ret < 0))
8013 return -1;
8014
8015 if (likely(ret == 0)) {
8016 /* The filter did not match the response, it can be
8017 * iterated through all headers.
8018 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008019 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8020 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 }
8023 return 0;
8024}
8025
8026
Willy Tarreaua15645d2007-03-18 16:22:39 +01008027/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008028 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008029 * desirable to call it only when needed. This function is also used when we
8030 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008031 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008032void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008034 struct http_txn *txn = &s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008035 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008036 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008037 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008039 char *hdr_beg, *hdr_end, *hdr_next;
8040 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042 /* Iterate through the headers.
8043 * we start with the start line.
8044 */
8045 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008046 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008047
8048 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8049 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008050 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051
8052 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008053 hdr_beg = hdr_next;
8054 hdr_end = hdr_beg + cur_hdr->len;
8055 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008056
Willy Tarreau24581ba2010-08-31 22:39:35 +02008057 /* We have one full header between hdr_beg and hdr_end, and the
8058 * next header starts at hdr_next. We're only interested in
8059 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060 */
8061
Willy Tarreau24581ba2010-08-31 22:39:35 +02008062 is_cookie2 = 0;
8063 prev = hdr_beg + 10;
8064 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008065 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008066 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8067 if (!val) {
8068 old_idx = cur_idx;
8069 continue;
8070 }
8071 is_cookie2 = 1;
8072 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073 }
8074
Willy Tarreau24581ba2010-08-31 22:39:35 +02008075 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8076 * <prev> points to the colon.
8077 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008078 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079
Willy Tarreau24581ba2010-08-31 22:39:35 +02008080 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8081 * check-cache is enabled) and we are not interested in checking
8082 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008083 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008084 if (s->be->cookie_name == NULL &&
8085 s->be->appsession_name == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008086 sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087 return;
8088
Willy Tarreau24581ba2010-08-31 22:39:35 +02008089 /* OK so now we know we have to process this response cookie.
8090 * The format of the Set-Cookie header is slightly different
8091 * from the format of the Cookie header in that it does not
8092 * support the comma as a cookie delimiter (thus the header
8093 * cannot be folded) because the Expires attribute described in
8094 * the original Netscape's spec may contain an unquoted date
8095 * with a comma inside. We have to live with this because
8096 * many browsers don't support Max-Age and some browsers don't
8097 * support quoted strings. However the Set-Cookie2 header is
8098 * clean.
8099 *
8100 * We have to keep multiple pointers in order to support cookie
8101 * removal at the beginning, middle or end of header without
8102 * corrupting the header (in case of set-cookie2). A special
8103 * pointer, <scav> points to the beginning of the set-cookie-av
8104 * fields after the first semi-colon. The <next> pointer points
8105 * either to the end of line (set-cookie) or next unquoted comma
8106 * (set-cookie2). All of these headers are valid :
8107 *
8108 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8109 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8110 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8111 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8112 * | | | | | | | | | |
8113 * | | | | | | | | +-> next hdr_end <--+
8114 * | | | | | | | +------------> scav
8115 * | | | | | | +--------------> val_end
8116 * | | | | | +--------------------> val_beg
8117 * | | | | +----------------------> equal
8118 * | | | +------------------------> att_end
8119 * | | +----------------------------> att_beg
8120 * | +------------------------------> prev
8121 * +-----------------------------------------> hdr_beg
8122 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123
Willy Tarreau24581ba2010-08-31 22:39:35 +02008124 for (; prev < hdr_end; prev = next) {
8125 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008126
Willy Tarreau24581ba2010-08-31 22:39:35 +02008127 /* find att_beg */
8128 att_beg = prev + 1;
8129 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8130 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008131
Willy Tarreau24581ba2010-08-31 22:39:35 +02008132 /* find att_end : this is the first character after the last non
8133 * space before the equal. It may be equal to hdr_end.
8134 */
8135 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008136
Willy Tarreau24581ba2010-08-31 22:39:35 +02008137 while (equal < hdr_end) {
8138 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8139 break;
8140 if (http_is_spht[(unsigned char)*equal++])
8141 continue;
8142 att_end = equal;
8143 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144
Willy Tarreau24581ba2010-08-31 22:39:35 +02008145 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8146 * is between <att_beg> and <equal>, both may be identical.
8147 */
8148
8149 /* look for end of cookie if there is an equal sign */
8150 if (equal < hdr_end && *equal == '=') {
8151 /* look for the beginning of the value */
8152 val_beg = equal + 1;
8153 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8154 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008155
Willy Tarreau24581ba2010-08-31 22:39:35 +02008156 /* find the end of the value, respecting quotes */
8157 next = find_cookie_value_end(val_beg, hdr_end);
8158
8159 /* make val_end point to the first white space or delimitor after the value */
8160 val_end = next;
8161 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8162 val_end--;
8163 } else {
8164 /* <equal> points to next comma, semi-colon or EOL */
8165 val_beg = val_end = next = equal;
8166 }
8167
8168 if (next < hdr_end) {
8169 /* Set-Cookie2 supports multiple cookies, and <next> points to
8170 * a colon or semi-colon before the end. So skip all attr-value
8171 * pairs and look for the next comma. For Set-Cookie, since
8172 * commas are permitted in values, skip to the end.
8173 */
8174 if (is_cookie2)
8175 next = find_hdr_value_end(next, hdr_end);
8176 else
8177 next = hdr_end;
8178 }
8179
8180 /* Now everything is as on the diagram above */
8181
8182 /* Ignore cookies with no equal sign */
8183 if (equal == val_end)
8184 continue;
8185
8186 /* If there are spaces around the equal sign, we need to
8187 * strip them otherwise we'll get trouble for cookie captures,
8188 * or even for rewrites. Since this happens extremely rarely,
8189 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008190 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008191 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8192 int stripped_before = 0;
8193 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008194
Willy Tarreau24581ba2010-08-31 22:39:35 +02008195 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008196 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008197 equal += stripped_before;
8198 val_beg += stripped_before;
8199 }
8200
8201 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008202 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008203 val_beg += stripped_after;
8204 stripped_before += stripped_after;
8205 }
8206
8207 val_end += stripped_before;
8208 next += stripped_before;
8209 hdr_end += stripped_before;
8210 hdr_next += stripped_before;
8211 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008212 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008213 }
8214
8215 /* First, let's see if we want to capture this cookie. We check
8216 * that we don't already have a server side cookie, because we
8217 * can only capture one. Also as an optimisation, we ignore
8218 * cookies shorter than the declared name.
8219 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008220 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008221 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008222 (val_end - att_beg >= sess->fe->capture_namelen) &&
8223 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008225 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226 Alert("HTTP logging : out of memory.\n");
8227 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008228 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008229 if (log_len > sess->fe->capture_len)
8230 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008231 memcpy(txn->srv_cookie, att_beg, log_len);
8232 txn->srv_cookie[log_len] = 0;
8233 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234 }
8235
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008236 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008237 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008238 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008239 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8240 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008241 /* assume passive cookie by default */
8242 txn->flags &= ~TX_SCK_MASK;
8243 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008244
8245 /* If the cookie is in insert mode on a known server, we'll delete
8246 * this occurrence because we'll insert another one later.
8247 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008248 * a direct access.
8249 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008250 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008251 /* The "preserve" flag was set, we don't want to touch the
8252 * server's cookie.
8253 */
8254 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008255 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008256 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008257 /* this cookie must be deleted */
8258 if (*prev == ':' && next == hdr_end) {
8259 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008260 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008261 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8262 txn->hdr_idx.used--;
8263 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008264 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008265 hdr_next += delta;
8266 http_msg_move_end(&txn->rsp, delta);
8267 /* note: while both invalid now, <next> and <hdr_end>
8268 * are still equal, so the for() will stop as expected.
8269 */
8270 } else {
8271 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008272 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008273 next = prev;
8274 hdr_end += delta;
8275 hdr_next += delta;
8276 cur_hdr->len += delta;
8277 http_msg_move_end(&txn->rsp, delta);
8278 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008279 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008280 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008281 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008282 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008283 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008284 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008285 * with this server since we know it.
8286 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008287 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008288 next += delta;
8289 hdr_end += delta;
8290 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008291 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008292 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008293
Willy Tarreauf1348312010-10-07 15:54:11 +02008294 txn->flags &= ~TX_SCK_MASK;
8295 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008296 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008297 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008298 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008299 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008300 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008301 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008302 next += delta;
8303 hdr_end += delta;
8304 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008305 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008306 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008307
Willy Tarreau827aee92011-03-10 16:55:02 +01008308 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008309 txn->flags &= ~TX_SCK_MASK;
8310 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008311 }
8312 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008313 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008314 else if (!(s->flags & SF_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008315 int cmp_len, value_len;
8316 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008317
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008318 if (s->be->options2 & PR_O2_AS_PFX) {
8319 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8320 value_begin = att_beg + s->be->appsession_name_len;
8321 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008322 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 cmp_len = att_end - att_beg;
8324 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008325 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008326 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008327
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008328 if ((cmp_len == s->be->appsession_name_len) &&
8329 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 /* free a possibly previously allocated memory */
8331 pool_free2(apools.sessid, txn->sessid);
8332
Willy Tarreau87b09662015-04-03 00:22:06 +02008333 /* Store the sessid in the stream for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008334 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008335 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008336 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008337 return;
8338 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008339 memcpy(txn->sessid, value_begin, value_len);
8340 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008341 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008342 }
8343 /* that's done for this cookie, check the next one on the same
8344 * line when next != hdr_end (only if is_cookie2).
8345 */
8346 }
8347 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008348 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008349 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008350
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008351 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008352 appsess *asession = NULL;
8353 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008354 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008355 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008356 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008357 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8358 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008359 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008360 return;
8361 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008362 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8363
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008364 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8365 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008366 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8367 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008368 return;
8369 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008370 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8371 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008372
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008373 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008374 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008375 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008376 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8377 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008378 return;
8379 }
8380 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008381 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008382
8383 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008384 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008385 }
8386
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008387 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008388 asession->request_count++;
8389 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008390}
8391
8392
Willy Tarreaua15645d2007-03-18 16:22:39 +01008393/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008394 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008395 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008396void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008397{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008398 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008399 char *p1, *p2;
8400
8401 char *cur_ptr, *cur_end, *cur_next;
8402 int cur_idx;
8403
Willy Tarreau5df51872007-11-25 16:20:08 +01008404 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008405 return;
8406
8407 /* Iterate through the headers.
8408 * we start with the start line.
8409 */
8410 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008411 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008412
8413 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8414 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008415 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008416
8417 cur_hdr = &txn->hdr_idx.v[cur_idx];
8418 cur_ptr = cur_next;
8419 cur_end = cur_ptr + cur_hdr->len;
8420 cur_next = cur_end + cur_hdr->cr + 1;
8421
8422 /* We have one full header between cur_ptr and cur_end, and the
8423 * next header starts at cur_next. We're only interested in
8424 * "Cookie:" headers.
8425 */
8426
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008427 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8428 if (val) {
8429 if ((cur_end - (cur_ptr + val) >= 8) &&
8430 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8431 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8432 return;
8433 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008434 }
8435
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008436 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8437 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008438 continue;
8439
8440 /* OK, right now we know we have a cache-control header at cur_ptr */
8441
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008442 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008443
8444 if (p1 >= cur_end) /* no more info */
8445 continue;
8446
8447 /* p1 is at the beginning of the value */
8448 p2 = p1;
8449
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008450 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008451 p2++;
8452
8453 /* we have a complete value between p1 and p2 */
8454 if (p2 < cur_end && *p2 == '=') {
8455 /* we have something of the form no-cache="set-cookie" */
8456 if ((cur_end - p1 >= 21) &&
8457 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8458 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008459 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008460 continue;
8461 }
8462
8463 /* OK, so we know that either p2 points to the end of string or to a comma */
8464 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008465 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8467 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8468 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008469 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008470 return;
8471 }
8472
8473 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008474 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008475 continue;
8476 }
8477 }
8478}
8479
8480
Willy Tarreau58f10d72006-12-04 02:26:12 +01008481/*
8482 * Try to retrieve a known appsession in the URI, then the associated server.
Willy Tarreau87b09662015-04-03 00:22:06 +02008483 * If the server is found, it's assigned to the stream.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008484 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008485void get_srv_from_appsession(struct stream *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008486{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008487 char *end_params, *first_param, *cur_param, *next_param;
8488 char separator;
8489 int value_len;
8490
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008491 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008492
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008493 if (s->be->appsession_name == NULL ||
8494 (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 +01008495 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008496 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008497
Cyril Bontéb21570a2009-11-29 20:04:48 +01008498 first_param = NULL;
8499 switch (mode) {
8500 case PR_O2_AS_M_PP:
8501 first_param = memchr(begin, ';', len);
8502 break;
8503 case PR_O2_AS_M_QS:
8504 first_param = memchr(begin, '?', len);
8505 break;
8506 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008507
Cyril Bontéb21570a2009-11-29 20:04:48 +01008508 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008509 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008510 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008511
Cyril Bontéb21570a2009-11-29 20:04:48 +01008512 switch (mode) {
8513 case PR_O2_AS_M_PP:
8514 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8515 end_params = (char *) begin + len;
8516 }
8517 separator = ';';
8518 break;
8519 case PR_O2_AS_M_QS:
8520 end_params = (char *) begin + len;
8521 separator = '&';
8522 break;
8523 default:
8524 /* unknown mode, shouldn't happen */
8525 return;
8526 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008527
Cyril Bontéb21570a2009-11-29 20:04:48 +01008528 cur_param = next_param = end_params;
8529 while (cur_param > first_param) {
8530 cur_param--;
8531 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8532 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008533 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8534 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8535 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008536 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008537 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8538 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008539 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008540 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008541 }
8542 break;
8543 }
8544 next_param = cur_param;
8545 }
8546 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008547#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008548 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008549 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008550#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008551}
8552
Willy Tarreaub2513902006-12-17 14:52:38 +01008553/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008554 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008555 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008556 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008557 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008558 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008559 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008560 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008561 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008562int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008563{
8564 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008565 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008566 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008567
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008568 if (!uri_auth)
8569 return 0;
8570
Cyril Bonté70be45d2010-10-12 00:14:35 +02008571 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008572 return 0;
8573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008574 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008575 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008576 return 0;
8577
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008578 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008579 return 0;
8580
Willy Tarreaub2513902006-12-17 14:52:38 +01008581 return 1;
8582}
8583
Willy Tarreau4076a152009-04-02 15:18:36 +02008584/*
8585 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008586 * By default it tries to report the error position as msg->err_pos. However if
8587 * this one is not set, it will then report msg->next, which is the last known
8588 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008589 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008590 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008591void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008592 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008593 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008594{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008595 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008596 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008597 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008598
Willy Tarreau9b28e032012-10-12 23:49:43 +02008599 es->len = MIN(chn->buf->i, sizeof(es->buf));
8600 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008601 len1 = MIN(len1, es->len);
8602 len2 = es->len - len1; /* remaining data if buffer wraps */
8603
Willy Tarreau9b28e032012-10-12 23:49:43 +02008604 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008605 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008606 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008607
Willy Tarreau4076a152009-04-02 15:18:36 +02008608 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008609 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008610 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008611 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008612
Willy Tarreau4076a152009-04-02 15:18:36 +02008613 es->when = date; // user-visible date
8614 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008615 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008616 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008617 if (objt_conn(sess->origin))
8618 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008619 else
8620 memset(&es->src, 0, sizeof(es->src));
8621
Willy Tarreau078272e2010-12-12 12:46:33 +01008622 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008623 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008624 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008625 es->s_flags = s->flags;
8626 es->t_flags = s->txn.flags;
8627 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008628 es->b_out = chn->buf->o;
8629 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008630 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008631 es->m_clen = msg->chunk_len;
8632 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008633}
Willy Tarreaub2513902006-12-17 14:52:38 +01008634
Willy Tarreau294c4732011-12-16 21:35:50 +01008635/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8636 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8637 * performed over the whole headers. Otherwise it must contain a valid header
8638 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8639 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8640 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8641 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008642 * -1. The value fetch stops at commas, so this function is suited for use with
8643 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008644 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008645 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008646unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008647 struct hdr_idx *idx, int occ,
8648 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008649{
Willy Tarreau294c4732011-12-16 21:35:50 +01008650 struct hdr_ctx local_ctx;
8651 char *ptr_hist[MAX_HDR_HISTORY];
8652 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008653 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008654 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008655
Willy Tarreau294c4732011-12-16 21:35:50 +01008656 if (!ctx) {
8657 local_ctx.idx = 0;
8658 ctx = &local_ctx;
8659 }
8660
Willy Tarreaubce70882009-09-07 11:51:47 +02008661 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008662 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008663 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008664 occ--;
8665 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008666 *vptr = ctx->line + ctx->val;
8667 *vlen = ctx->vlen;
8668 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008669 }
8670 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008671 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008672 }
8673
8674 /* negative occurrence, we scan all the list then walk back */
8675 if (-occ > MAX_HDR_HISTORY)
8676 return 0;
8677
Willy Tarreau294c4732011-12-16 21:35:50 +01008678 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008679 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008680 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8681 len_hist[hist_ptr] = ctx->vlen;
8682 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008683 hist_ptr = 0;
8684 found++;
8685 }
8686 if (-occ > found)
8687 return 0;
8688 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008689 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8690 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8691 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008692 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008693 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008694 if (hist_ptr >= MAX_HDR_HISTORY)
8695 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008696 *vptr = ptr_hist[hist_ptr];
8697 *vlen = len_hist[hist_ptr];
8698 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008699}
8700
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008701/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8702 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8703 * performed over the whole headers. Otherwise it must contain a valid header
8704 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8705 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8706 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8707 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8708 * -1. This function differs from http_get_hdr() in that it only returns full
8709 * line header values and does not stop at commas.
8710 * The return value is 0 if nothing was found, or non-zero otherwise.
8711 */
8712unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8713 struct hdr_idx *idx, int occ,
8714 struct hdr_ctx *ctx, char **vptr, int *vlen)
8715{
8716 struct hdr_ctx local_ctx;
8717 char *ptr_hist[MAX_HDR_HISTORY];
8718 int len_hist[MAX_HDR_HISTORY];
8719 unsigned int hist_ptr;
8720 int found;
8721
8722 if (!ctx) {
8723 local_ctx.idx = 0;
8724 ctx = &local_ctx;
8725 }
8726
8727 if (occ >= 0) {
8728 /* search from the beginning */
8729 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8730 occ--;
8731 if (occ <= 0) {
8732 *vptr = ctx->line + ctx->val;
8733 *vlen = ctx->vlen;
8734 return 1;
8735 }
8736 }
8737 return 0;
8738 }
8739
8740 /* negative occurrence, we scan all the list then walk back */
8741 if (-occ > MAX_HDR_HISTORY)
8742 return 0;
8743
8744 found = hist_ptr = 0;
8745 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8746 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8747 len_hist[hist_ptr] = ctx->vlen;
8748 if (++hist_ptr >= MAX_HDR_HISTORY)
8749 hist_ptr = 0;
8750 found++;
8751 }
8752 if (-occ > found)
8753 return 0;
8754 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8755 * find occurrence -occ, so we have to check [hist_ptr+occ].
8756 */
8757 hist_ptr += occ;
8758 if (hist_ptr >= MAX_HDR_HISTORY)
8759 hist_ptr -= MAX_HDR_HISTORY;
8760 *vptr = ptr_hist[hist_ptr];
8761 *vlen = len_hist[hist_ptr];
8762 return 1;
8763}
8764
Willy Tarreaubaaee002006-06-26 02:48:02 +02008765/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008766 * Print a debug line with a header. Always stop at the first CR or LF char,
8767 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8768 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008769 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008770void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008771{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008772 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008773 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008774
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008775 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008776 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008777 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008778 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 +02008779
8780 for (max = 0; start + max < end; max++)
8781 if (start[max] == '\r' || start[max] == '\n')
8782 break;
8783
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008784 UBOUND(max, trash.size - trash.len - 3);
8785 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8786 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008787 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008788}
8789
Willy Tarreau0937bc42009-12-22 15:03:09 +01008790/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008791 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008792 * the required fields are properly allocated and that we only need to (re)init
8793 * them. This should be used before processing any new request.
8794 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008795void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008796{
8797 struct http_txn *txn = &s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008798 struct proxy *fe = strm_sess(s)->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008799
8800 txn->flags = 0;
8801 txn->status = -1;
8802
Willy Tarreauf64d1412010-10-07 20:06:11 +02008803 txn->cookie_first_date = 0;
8804 txn->cookie_last_date = 0;
8805
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008806 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008807 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008808 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008809 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008810 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008811 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008812 txn->req.chunk_len = 0LL;
8813 txn->req.body_len = 0LL;
8814 txn->rsp.chunk_len = 0LL;
8815 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008816 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8817 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008818 txn->req.chn = &s->req;
8819 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008820
8821 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008822
8823 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8824 if (fe->options2 & PR_O2_REQBUG_OK)
8825 txn->req.err_pos = -1; /* let buggy requests pass */
8826
Willy Tarreau46023632010-01-07 22:51:47 +01008827 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008828 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8829
Willy Tarreau46023632010-01-07 22:51:47 +01008830 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008831 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8832
8833 if (txn->hdr_idx.v)
8834 hdr_idx_init(&txn->hdr_idx);
8835}
8836
8837/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008838void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008839{
8840 struct http_txn *txn = &s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008841 struct proxy *fe = strm_sess(s)->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008842
Willy Tarreau75195602014-03-11 15:48:55 +01008843 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008844 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008845 s->comp_algo->end(&s->comp_ctx);
8846 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008847 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008848
Willy Tarreau0937bc42009-12-22 15:03:09 +01008849 /* these ones will have been dynamically allocated */
8850 pool_free2(pool2_requri, txn->uri);
8851 pool_free2(pool2_capture, txn->cli_cookie);
8852 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008853 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008854 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008855
William Lallemanda73203e2012-03-12 12:48:57 +01008856 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008857 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008858 txn->uri = NULL;
8859 txn->srv_cookie = NULL;
8860 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008861
8862 if (txn->req.cap) {
8863 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008864 for (h = fe->req_cap; h; h = h->next)
Willy Tarreau46023632010-01-07 22:51:47 +01008865 pool_free2(h->pool, txn->req.cap[h->index]);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008866 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008867 }
8868
8869 if (txn->rsp.cap) {
8870 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008871 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreau46023632010-01-07 22:51:47 +01008872 pool_free2(h->pool, txn->rsp.cap[h->index]);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008873 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008874 }
8875
Willy Tarreau0937bc42009-12-22 15:03:09 +01008876}
8877
8878/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008879void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008880{
8881 http_end_txn(s);
8882 http_init_txn(s);
8883
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008884 /* reinitialise the current rule list pointer to NULL. We are sure that
8885 * any rulelist match the NULL pointer.
8886 */
8887 s->current_rule_list = NULL;
8888
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008889 s->be = strm_sess(s)->fe;
8890 s->logs.logwait = strm_sess(s)->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008891 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008892 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008893 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008894 /* re-init store persistence */
8895 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008896 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008897
Willy Tarreau0937bc42009-12-22 15:03:09 +01008898 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008899
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008900 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008901
Willy Tarreau739cfba2010-01-25 23:11:14 +01008902 /* We must trim any excess data from the response buffer, because we
8903 * may have blocked an invalid response from a server that we don't
8904 * want to accidentely forward once we disable the analysers, nor do
8905 * we want those data to come along with next response. A typical
8906 * example of such data would be from a buggy server responding to
8907 * a HEAD with some data, or sending more than the advertised
8908 * content-length.
8909 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008910 if (unlikely(s->res.buf->i))
8911 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008912
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008913 s->req.rto = strm_sess(s)->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008914 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008916 s->res.rto = TICK_ETERNITY;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008917 s->res.wto = strm_sess(s)->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008918
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008919 s->req.rex = TICK_ETERNITY;
8920 s->req.wex = TICK_ETERNITY;
8921 s->req.analyse_exp = TICK_ETERNITY;
8922 s->res.rex = TICK_ETERNITY;
8923 s->res.wex = TICK_ETERNITY;
8924 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008925}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008926
Sasha Pachev218f0642014-06-16 12:05:59 -06008927void free_http_res_rules(struct list *r)
8928{
8929 struct http_res_rule *tr, *pr;
8930
8931 list_for_each_entry_safe(pr, tr, r, list) {
8932 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008933 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008934 free(pr);
8935 }
8936}
8937
8938void free_http_req_rules(struct list *r)
8939{
Willy Tarreauff011f22011-01-06 17:51:27 +01008940 struct http_req_rule *tr, *pr;
8941
8942 list_for_each_entry_safe(pr, tr, r, list) {
8943 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008944 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008945 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008946
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008947 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008948 free(pr);
8949 }
8950}
8951
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008952/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008953struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8954{
8955 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008956 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008957 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008958 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008959
8960 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8961 if (!rule) {
8962 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008963 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008964 }
8965
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008966 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008967 rule->action = HTTP_REQ_ACT_ALLOW;
8968 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008969 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008970 rule->action = HTTP_REQ_ACT_DENY;
8971 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008972 } else if (!strcmp(args[0], "tarpit")) {
8973 rule->action = HTTP_REQ_ACT_TARPIT;
8974 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008975 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008976 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008977 cur_arg = 1;
8978
8979 while(*args[cur_arg]) {
8980 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008981 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008982 cur_arg+=2;
8983 continue;
8984 } else
8985 break;
8986 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008987 } else if (!strcmp(args[0], "set-nice")) {
8988 rule->action = HTTP_REQ_ACT_SET_NICE;
8989 cur_arg = 1;
8990
8991 if (!*args[cur_arg] ||
8992 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8993 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8994 file, linenum, args[0]);
8995 goto out_err;
8996 }
8997 rule->arg.nice = atoi(args[cur_arg]);
8998 if (rule->arg.nice < -1024)
8999 rule->arg.nice = -1024;
9000 else if (rule->arg.nice > 1024)
9001 rule->arg.nice = 1024;
9002 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009003 } else if (!strcmp(args[0], "set-tos")) {
9004#ifdef IP_TOS
9005 char *err;
9006 rule->action = HTTP_REQ_ACT_SET_TOS;
9007 cur_arg = 1;
9008
9009 if (!*args[cur_arg] ||
9010 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9011 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9012 file, linenum, args[0]);
9013 goto out_err;
9014 }
9015
9016 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9017 if (err && *err != '\0') {
9018 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9019 file, linenum, err, args[0]);
9020 goto out_err;
9021 }
9022 cur_arg++;
9023#else
9024 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9025 goto out_err;
9026#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009027 } else if (!strcmp(args[0], "set-mark")) {
9028#ifdef SO_MARK
9029 char *err;
9030 rule->action = HTTP_REQ_ACT_SET_MARK;
9031 cur_arg = 1;
9032
9033 if (!*args[cur_arg] ||
9034 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9035 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9036 file, linenum, args[0]);
9037 goto out_err;
9038 }
9039
9040 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9041 if (err && *err != '\0') {
9042 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9043 file, linenum, err, args[0]);
9044 goto out_err;
9045 }
9046 cur_arg++;
9047 global.last_checks |= LSTCHK_NETADM;
9048#else
9049 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9050 goto out_err;
9051#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009052 } else if (!strcmp(args[0], "set-log-level")) {
9053 rule->action = HTTP_REQ_ACT_SET_LOGL;
9054 cur_arg = 1;
9055
9056 if (!*args[cur_arg] ||
9057 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9058 bad_log_level:
9059 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9060 file, linenum, args[0]);
9061 goto out_err;
9062 }
9063 if (strcmp(args[cur_arg], "silent") == 0)
9064 rule->arg.loglevel = -1;
9065 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9066 goto bad_log_level;
9067 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009068 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9069 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9070 cur_arg = 1;
9071
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009072 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9073 (*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 +01009074 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9075 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009076 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009077 }
9078
9079 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9080 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9081 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009082
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009083 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009084 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009085 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9086 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009087 free(proxy->conf.lfs_file);
9088 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9089 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009090 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009091 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9092 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009093 cur_arg = 1;
9094
9095 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009096 (*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 -06009097 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9098 file, linenum, args[0]);
9099 goto out_err;
9100 }
9101
9102 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9103 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9104 LIST_INIT(&rule->arg.hdr_add.fmt);
9105
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009106 error = NULL;
9107 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9108 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9109 args[cur_arg + 1], error);
9110 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009111 goto out_err;
9112 }
9113
9114 proxy->conf.args.ctx = ARGC_HRQ;
9115 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9116 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9117 file, linenum);
9118
9119 free(proxy->conf.lfs_file);
9120 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9121 proxy->conf.lfs_line = proxy->conf.args.line;
9122 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009123 } else if (strcmp(args[0], "del-header") == 0) {
9124 rule->action = HTTP_REQ_ACT_DEL_HDR;
9125 cur_arg = 1;
9126
9127 if (!*args[cur_arg] ||
9128 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9129 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9130 file, linenum, args[0]);
9131 goto out_err;
9132 }
9133
9134 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9135 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9136
9137 proxy->conf.args.ctx = ARGC_HRQ;
9138 free(proxy->conf.lfs_file);
9139 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9140 proxy->conf.lfs_line = proxy->conf.args.line;
9141 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009142 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9143 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009144 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009145 struct sample_expr *expr;
9146 unsigned int where;
9147 char *err = NULL;
9148
9149 cur_arg = 1;
9150 proxy->conf.args.ctx = ARGC_TRK;
9151
9152 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9153 if (!expr) {
9154 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9155 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9156 free(err);
9157 goto out_err;
9158 }
9159
9160 where = 0;
9161 if (proxy->cap & PR_CAP_FE)
9162 where |= SMP_VAL_FE_HRQ_HDR;
9163 if (proxy->cap & PR_CAP_BE)
9164 where |= SMP_VAL_BE_HRQ_HDR;
9165
9166 if (!(expr->fetch->val & where)) {
9167 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9168 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9169 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9170 args[cur_arg-1], sample_src_names(expr->fetch->use));
9171 free(expr);
9172 goto out_err;
9173 }
9174
9175 if (strcmp(args[cur_arg], "table") == 0) {
9176 cur_arg++;
9177 if (!args[cur_arg]) {
9178 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9179 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9180 free(expr);
9181 goto out_err;
9182 }
9183 /* we copy the table name for now, it will be resolved later */
9184 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9185 cur_arg++;
9186 }
9187 rule->act_prm.trk_ctr.expr = expr;
9188 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009189 } else if (strcmp(args[0], "redirect") == 0) {
9190 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009191 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009192
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009193 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009194 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9195 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9196 goto out_err;
9197 }
9198
9199 /* this redirect rule might already contain a parsed condition which
9200 * we'll pass to the http-request rule.
9201 */
9202 rule->action = HTTP_REQ_ACT_REDIR;
9203 rule->arg.redir = redir;
9204 rule->cond = redir->cond;
9205 redir->cond = NULL;
9206 cur_arg = 2;
9207 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009208 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9209 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9210 rule->action = HTTP_REQ_ACT_ADD_ACL;
9211 /*
9212 * '+ 8' for 'add-acl('
9213 * '- 9' for 'add-acl(' + trailing ')'
9214 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009215 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009216
9217 cur_arg = 1;
9218
9219 if (!*args[cur_arg] ||
9220 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9221 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9222 file, linenum, args[0]);
9223 goto out_err;
9224 }
9225
9226 LIST_INIT(&rule->arg.map.key);
9227 proxy->conf.args.ctx = ARGC_HRQ;
9228 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9229 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9230 file, linenum);
9231 free(proxy->conf.lfs_file);
9232 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9233 proxy->conf.lfs_line = proxy->conf.args.line;
9234 cur_arg += 1;
9235 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9236 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9237 rule->action = HTTP_REQ_ACT_DEL_ACL;
9238 /*
9239 * '+ 8' for 'del-acl('
9240 * '- 9' for 'del-acl(' + trailing ')'
9241 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009242 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009243
9244 cur_arg = 1;
9245
9246 if (!*args[cur_arg] ||
9247 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9248 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9249 file, linenum, args[0]);
9250 goto out_err;
9251 }
9252
9253 LIST_INIT(&rule->arg.map.key);
9254 proxy->conf.args.ctx = ARGC_HRQ;
9255 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9256 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9257 file, linenum);
9258 free(proxy->conf.lfs_file);
9259 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9260 proxy->conf.lfs_line = proxy->conf.args.line;
9261 cur_arg += 1;
9262 } else if (strncmp(args[0], "del-map", 7) == 0) {
9263 /* http-request del-map(<reference (map name)>) <key pattern> */
9264 rule->action = HTTP_REQ_ACT_DEL_MAP;
9265 /*
9266 * '+ 8' for 'del-map('
9267 * '- 9' for 'del-map(' + trailing ')'
9268 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009269 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009270
9271 cur_arg = 1;
9272
9273 if (!*args[cur_arg] ||
9274 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9275 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9276 file, linenum, args[0]);
9277 goto out_err;
9278 }
9279
9280 LIST_INIT(&rule->arg.map.key);
9281 proxy->conf.args.ctx = ARGC_HRQ;
9282 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9283 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9284 file, linenum);
9285 free(proxy->conf.lfs_file);
9286 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9287 proxy->conf.lfs_line = proxy->conf.args.line;
9288 cur_arg += 1;
9289 } else if (strncmp(args[0], "set-map", 7) == 0) {
9290 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9291 rule->action = HTTP_REQ_ACT_SET_MAP;
9292 /*
9293 * '+ 8' for 'set-map('
9294 * '- 9' for 'set-map(' + trailing ')'
9295 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009296 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009297
9298 cur_arg = 1;
9299
9300 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9301 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9302 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9303 file, linenum, args[0]);
9304 goto out_err;
9305 }
9306
9307 LIST_INIT(&rule->arg.map.key);
9308 LIST_INIT(&rule->arg.map.value);
9309 proxy->conf.args.ctx = ARGC_HRQ;
9310
9311 /* key pattern */
9312 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9313 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9314 file, linenum);
9315
9316 /* value pattern */
9317 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9318 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9319 file, linenum);
9320 free(proxy->conf.lfs_file);
9321 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9322 proxy->conf.lfs_line = proxy->conf.args.line;
9323
9324 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009325 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9326 char *errmsg = NULL;
9327 cur_arg = 1;
9328 /* try in the module list */
9329 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9330 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9331 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9332 free(errmsg);
9333 goto out_err;
9334 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009335 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009336 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 +01009337 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009338 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009339 }
9340
9341 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9342 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009343 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009344
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009345 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9346 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9347 file, linenum, args[0], errmsg);
9348 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009349 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009350 }
9351 rule->cond = cond;
9352 }
9353 else if (*args[cur_arg]) {
9354 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9355 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9356 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009357 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009358 }
9359
9360 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009361 out_err:
9362 free(rule);
9363 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009364}
9365
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009366/* parse an "http-respose" rule */
9367struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9368{
9369 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009370 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009371 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009372 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009373
9374 rule = calloc(1, sizeof(*rule));
9375 if (!rule) {
9376 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9377 goto out_err;
9378 }
9379
9380 if (!strcmp(args[0], "allow")) {
9381 rule->action = HTTP_RES_ACT_ALLOW;
9382 cur_arg = 1;
9383 } else if (!strcmp(args[0], "deny")) {
9384 rule->action = HTTP_RES_ACT_DENY;
9385 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009386 } else if (!strcmp(args[0], "set-nice")) {
9387 rule->action = HTTP_RES_ACT_SET_NICE;
9388 cur_arg = 1;
9389
9390 if (!*args[cur_arg] ||
9391 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9392 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9393 file, linenum, args[0]);
9394 goto out_err;
9395 }
9396 rule->arg.nice = atoi(args[cur_arg]);
9397 if (rule->arg.nice < -1024)
9398 rule->arg.nice = -1024;
9399 else if (rule->arg.nice > 1024)
9400 rule->arg.nice = 1024;
9401 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009402 } else if (!strcmp(args[0], "set-tos")) {
9403#ifdef IP_TOS
9404 char *err;
9405 rule->action = HTTP_RES_ACT_SET_TOS;
9406 cur_arg = 1;
9407
9408 if (!*args[cur_arg] ||
9409 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9410 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9411 file, linenum, args[0]);
9412 goto out_err;
9413 }
9414
9415 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9416 if (err && *err != '\0') {
9417 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9418 file, linenum, err, args[0]);
9419 goto out_err;
9420 }
9421 cur_arg++;
9422#else
9423 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9424 goto out_err;
9425#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009426 } else if (!strcmp(args[0], "set-mark")) {
9427#ifdef SO_MARK
9428 char *err;
9429 rule->action = HTTP_RES_ACT_SET_MARK;
9430 cur_arg = 1;
9431
9432 if (!*args[cur_arg] ||
9433 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9434 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9435 file, linenum, args[0]);
9436 goto out_err;
9437 }
9438
9439 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9440 if (err && *err != '\0') {
9441 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9442 file, linenum, err, args[0]);
9443 goto out_err;
9444 }
9445 cur_arg++;
9446 global.last_checks |= LSTCHK_NETADM;
9447#else
9448 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9449 goto out_err;
9450#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009451 } else if (!strcmp(args[0], "set-log-level")) {
9452 rule->action = HTTP_RES_ACT_SET_LOGL;
9453 cur_arg = 1;
9454
9455 if (!*args[cur_arg] ||
9456 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9457 bad_log_level:
9458 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9459 file, linenum, args[0]);
9460 goto out_err;
9461 }
9462 if (strcmp(args[cur_arg], "silent") == 0)
9463 rule->arg.loglevel = -1;
9464 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9465 goto bad_log_level;
9466 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009467 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9468 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9469 cur_arg = 1;
9470
9471 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9472 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9473 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9474 file, linenum, args[0]);
9475 goto out_err;
9476 }
9477
9478 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9479 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9480 LIST_INIT(&rule->arg.hdr_add.fmt);
9481
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009482 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009483 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009484 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9485 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009486 free(proxy->conf.lfs_file);
9487 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9488 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009489 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009490 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009491 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009492 cur_arg = 1;
9493
9494 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009495 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9496 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009497 file, linenum, args[0]);
9498 goto out_err;
9499 }
9500
9501 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9502 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9503 LIST_INIT(&rule->arg.hdr_add.fmt);
9504
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009505 error = NULL;
9506 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9507 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9508 args[cur_arg + 1], error);
9509 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009510 goto out_err;
9511 }
9512
9513 proxy->conf.args.ctx = ARGC_HRQ;
9514 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9515 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9516 file, linenum);
9517
9518 free(proxy->conf.lfs_file);
9519 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9520 proxy->conf.lfs_line = proxy->conf.args.line;
9521 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009522 } else if (strcmp(args[0], "del-header") == 0) {
9523 rule->action = HTTP_RES_ACT_DEL_HDR;
9524 cur_arg = 1;
9525
9526 if (!*args[cur_arg] ||
9527 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9528 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9529 file, linenum, args[0]);
9530 goto out_err;
9531 }
9532
9533 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9534 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9535
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009536 proxy->conf.args.ctx = ARGC_HRS;
9537 free(proxy->conf.lfs_file);
9538 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9539 proxy->conf.lfs_line = proxy->conf.args.line;
9540 cur_arg += 1;
9541 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9542 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9543 rule->action = HTTP_RES_ACT_ADD_ACL;
9544 /*
9545 * '+ 8' for 'add-acl('
9546 * '- 9' for 'add-acl(' + trailing ')'
9547 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009548 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009549
9550 cur_arg = 1;
9551
9552 if (!*args[cur_arg] ||
9553 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9554 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9555 file, linenum, args[0]);
9556 goto out_err;
9557 }
9558
9559 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009560 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009561 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9562 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9563 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009564 free(proxy->conf.lfs_file);
9565 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9566 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009567
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009568 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009569 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9570 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9571 rule->action = HTTP_RES_ACT_DEL_ACL;
9572 /*
9573 * '+ 8' for 'del-acl('
9574 * '- 9' for 'del-acl(' + trailing ')'
9575 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009576 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009577
9578 cur_arg = 1;
9579
9580 if (!*args[cur_arg] ||
9581 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9582 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9583 file, linenum, args[0]);
9584 goto out_err;
9585 }
9586
9587 LIST_INIT(&rule->arg.map.key);
9588 proxy->conf.args.ctx = ARGC_HRS;
9589 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9590 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9591 file, linenum);
9592 free(proxy->conf.lfs_file);
9593 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9594 proxy->conf.lfs_line = proxy->conf.args.line;
9595 cur_arg += 1;
9596 } else if (strncmp(args[0], "del-map", 7) == 0) {
9597 /* http-response del-map(<reference (map name)>) <key pattern> */
9598 rule->action = HTTP_RES_ACT_DEL_MAP;
9599 /*
9600 * '+ 8' for 'del-map('
9601 * '- 9' for 'del-map(' + trailing ')'
9602 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009603 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009604
9605 cur_arg = 1;
9606
9607 if (!*args[cur_arg] ||
9608 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9609 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9610 file, linenum, args[0]);
9611 goto out_err;
9612 }
9613
9614 LIST_INIT(&rule->arg.map.key);
9615 proxy->conf.args.ctx = ARGC_HRS;
9616 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9617 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9618 file, linenum);
9619 free(proxy->conf.lfs_file);
9620 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9621 proxy->conf.lfs_line = proxy->conf.args.line;
9622 cur_arg += 1;
9623 } else if (strncmp(args[0], "set-map", 7) == 0) {
9624 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9625 rule->action = HTTP_RES_ACT_SET_MAP;
9626 /*
9627 * '+ 8' for 'set-map('
9628 * '- 9' for 'set-map(' + trailing ')'
9629 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009630 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009631
9632 cur_arg = 1;
9633
9634 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9635 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9636 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9637 file, linenum, args[0]);
9638 goto out_err;
9639 }
9640
9641 LIST_INIT(&rule->arg.map.key);
9642 LIST_INIT(&rule->arg.map.value);
9643
9644 proxy->conf.args.ctx = ARGC_HRS;
9645
9646 /* key pattern */
9647 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9648 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9649 file, linenum);
9650
9651 /* value pattern */
9652 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9653 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9654 file, linenum);
9655
9656 free(proxy->conf.lfs_file);
9657 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9658 proxy->conf.lfs_line = proxy->conf.args.line;
9659
9660 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009661 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9662 char *errmsg = NULL;
9663 cur_arg = 1;
9664 /* try in the module list */
9665 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9666 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9667 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9668 free(errmsg);
9669 goto out_err;
9670 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009671 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009672 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 +02009673 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9674 goto out_err;
9675 }
9676
9677 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9678 struct acl_cond *cond;
9679 char *errmsg = NULL;
9680
9681 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9682 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9683 file, linenum, args[0], errmsg);
9684 free(errmsg);
9685 goto out_err;
9686 }
9687 rule->cond = cond;
9688 }
9689 else if (*args[cur_arg]) {
9690 Alert("parsing [%s:%d]: 'http-response %s' expects"
9691 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9692 file, linenum, args[0], args[cur_arg]);
9693 goto out_err;
9694 }
9695
9696 return rule;
9697 out_err:
9698 free(rule);
9699 return NULL;
9700}
9701
Willy Tarreau4baae242012-12-27 12:00:31 +01009702/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009703 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9704 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009705 */
9706struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009707 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009708{
9709 struct redirect_rule *rule;
9710 int cur_arg;
9711 int type = REDIRECT_TYPE_NONE;
9712 int code = 302;
9713 const char *destination = NULL;
9714 const char *cookie = NULL;
9715 int cookie_set = 0;
9716 unsigned int flags = REDIRECT_FLAG_NONE;
9717 struct acl_cond *cond = NULL;
9718
9719 cur_arg = 0;
9720 while (*(args[cur_arg])) {
9721 if (strcmp(args[cur_arg], "location") == 0) {
9722 if (!*args[cur_arg + 1])
9723 goto missing_arg;
9724
9725 type = REDIRECT_TYPE_LOCATION;
9726 cur_arg++;
9727 destination = args[cur_arg];
9728 }
9729 else if (strcmp(args[cur_arg], "prefix") == 0) {
9730 if (!*args[cur_arg + 1])
9731 goto missing_arg;
9732
9733 type = REDIRECT_TYPE_PREFIX;
9734 cur_arg++;
9735 destination = args[cur_arg];
9736 }
9737 else if (strcmp(args[cur_arg], "scheme") == 0) {
9738 if (!*args[cur_arg + 1])
9739 goto missing_arg;
9740
9741 type = REDIRECT_TYPE_SCHEME;
9742 cur_arg++;
9743 destination = args[cur_arg];
9744 }
9745 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9746 if (!*args[cur_arg + 1])
9747 goto missing_arg;
9748
9749 cur_arg++;
9750 cookie = args[cur_arg];
9751 cookie_set = 1;
9752 }
9753 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9754 if (!*args[cur_arg + 1])
9755 goto missing_arg;
9756
9757 cur_arg++;
9758 cookie = args[cur_arg];
9759 cookie_set = 0;
9760 }
9761 else if (strcmp(args[cur_arg], "code") == 0) {
9762 if (!*args[cur_arg + 1])
9763 goto missing_arg;
9764
9765 cur_arg++;
9766 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009767 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009768 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009769 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009770 args[cur_arg - 1], args[cur_arg]);
9771 return NULL;
9772 }
9773 }
9774 else if (!strcmp(args[cur_arg],"drop-query")) {
9775 flags |= REDIRECT_FLAG_DROP_QS;
9776 }
9777 else if (!strcmp(args[cur_arg],"append-slash")) {
9778 flags |= REDIRECT_FLAG_APPEND_SLASH;
9779 }
9780 else if (strcmp(args[cur_arg], "if") == 0 ||
9781 strcmp(args[cur_arg], "unless") == 0) {
9782 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9783 if (!cond) {
9784 memprintf(errmsg, "error in condition: %s", *errmsg);
9785 return NULL;
9786 }
9787 break;
9788 }
9789 else {
9790 memprintf(errmsg,
9791 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9792 args[cur_arg]);
9793 return NULL;
9794 }
9795 cur_arg++;
9796 }
9797
9798 if (type == REDIRECT_TYPE_NONE) {
9799 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9800 return NULL;
9801 }
9802
9803 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9804 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009805 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009806
9807 if (!use_fmt) {
9808 /* old-style static redirect rule */
9809 rule->rdr_str = strdup(destination);
9810 rule->rdr_len = strlen(destination);
9811 }
9812 else {
9813 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009814
9815 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9816 * if prefix == "/", we don't want to add anything, otherwise it
9817 * makes it hard for the user to configure a self-redirection.
9818 */
Godbachd9722032014-12-18 15:44:58 +08009819 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009820 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009821 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009822 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9823 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009824 free(curproxy->conf.lfs_file);
9825 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9826 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009827 }
9828 }
9829
Willy Tarreau4baae242012-12-27 12:00:31 +01009830 if (cookie) {
9831 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9832 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9833 */
9834 rule->cookie_len = strlen(cookie);
9835 if (cookie_set) {
9836 rule->cookie_str = malloc(rule->cookie_len + 10);
9837 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9838 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9839 rule->cookie_len += 9;
9840 } else {
9841 rule->cookie_str = malloc(rule->cookie_len + 21);
9842 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9843 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9844 rule->cookie_len += 20;
9845 }
9846 }
9847 rule->type = type;
9848 rule->code = code;
9849 rule->flags = flags;
9850 LIST_INIT(&rule->list);
9851 return rule;
9852
9853 missing_arg:
9854 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9855 return NULL;
9856}
9857
Willy Tarreau8797c062007-05-07 00:55:35 +02009858/************************************************************************/
9859/* The code below is dedicated to ACL parsing and matching */
9860/************************************************************************/
9861
9862
Willy Tarreau14174bc2012-04-16 14:34:04 +02009863/* This function ensures that the prerequisites for an L7 fetch are ready,
9864 * which means that a request or response is ready. If some data is missing,
9865 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009866 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9867 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009868 *
9869 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009870 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9871 * decide whether or not an HTTP message is present ;
9872 * 0 if the requested data cannot be fetched or if it is certain that
9873 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009874 * 1 if an HTTP message is ready
9875 */
9876static int
Willy Tarreau87b09662015-04-03 00:22:06 +02009877smp_prefetch_http(struct proxy *px, struct stream *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009878 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009879{
9880 struct http_txn *txn = l7;
9881 struct http_msg *msg = &txn->req;
9882
9883 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9884 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9885 */
9886
9887 if (unlikely(!s || !txn))
9888 return 0;
9889
9890 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009891 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009892
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009893 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009894 /* If the buffer does not leave enough free space at the end,
9895 * we must first realign it.
9896 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009897 if (s->req.buf->p > s->req.buf->data &&
9898 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9899 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009900
Willy Tarreau14174bc2012-04-16 14:34:04 +02009901 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009902 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009903 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009904
9905 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009906 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009907 http_msg_analyzer(msg, &txn->hdr_idx);
9908
9909 /* Still no valid request ? */
9910 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009911 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009912 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009913 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009914 }
9915 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009916 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009917 return 0;
9918 }
9919
9920 /* OK we just got a valid HTTP request. We have some minor
9921 * preparation to perform so that further checks can rely
9922 * on HTTP tests.
9923 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009924
9925 /* If the request was parsed but was too large, we must absolutely
9926 * return an error so that it is not processed. At the moment this
9927 * cannot happen, but if the parsers are to change in the future,
9928 * we want this check to be maintained.
9929 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009930 if (unlikely(s->req.buf->i + s->req.buf->p >
9931 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009932 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009933 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009934 return 1;
9935 }
9936
Willy Tarreau9b28e032012-10-12 23:49:43 +02009937 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009938 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009939 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009940
Willy Tarreau506d0502013-07-06 13:29:24 +02009941 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9942 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009943 }
9944
Willy Tarreau506d0502013-07-06 13:29:24 +02009945 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009946 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009947 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009948
9949 /* otherwise everything's ready for the request */
9950 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009951 else {
9952 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009953 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9954 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009955 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009956 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009957 }
9958
9959 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009960 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009961 return 1;
9962}
Willy Tarreau8797c062007-05-07 00:55:35 +02009963
Willy Tarreau6c616e02014-06-25 16:56:41 +02009964/* Note: these functinos *do* modify the sample. Even in case of success, at
9965 * least the type and uint value are modified.
9966 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009967#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009968 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02009969
Willy Tarreau24e32d82012-04-23 23:55:44 +02009970#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009971 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02009972
Willy Tarreau8797c062007-05-07 00:55:35 +02009973
9974/* 1. Check on METHOD
9975 * We use the pre-parsed method if it is known, and store its number as an
9976 * integer. If it is unknown, we use the pointer and the length.
9977 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009978static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009979{
9980 int len, meth;
9981
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009982 len = strlen(text);
9983 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009984
9985 pattern->val.i = meth;
9986 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009987 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009988 pattern->len = len;
9989 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009990 else {
9991 pattern->ptr.str = NULL;
9992 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009993 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009994 return 1;
9995}
9996
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009997/* This function fetches the method of current HTTP request and stores
9998 * it in the global pattern struct as a chunk. There are two possibilities :
9999 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10000 * in <len> and <ptr> is NULL ;
10001 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10002 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010003 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010004 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010005static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010006smp_fetch_meth(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010007 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010008{
10009 int meth;
10010 struct http_txn *txn = l7;
10011
Willy Tarreau24e32d82012-04-23 23:55:44 +020010012 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010013
Willy Tarreau8797c062007-05-07 00:55:35 +020010014 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010015 smp->type = SMP_T_METH;
10016 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010017 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010018 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10019 /* ensure the indexes are not affected */
10020 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010021 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010022 smp->data.meth.str.len = txn->req.sl.rq.m_l;
10023 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010024 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010025 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010026 return 1;
10027}
10028
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010029/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010030static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010031{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010032 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010033 struct pattern_list *lst;
10034 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010035
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010036 list_for_each_entry(lst, &expr->patterns, list) {
10037 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010038
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010039 /* well-known method */
10040 if (pattern->val.i != HTTP_METH_OTHER) {
10041 if (smp->data.meth.meth == pattern->val.i)
10042 return pattern;
10043 else
10044 continue;
10045 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010046
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010047 /* Other method, we must compare the strings */
10048 if (pattern->len != smp->data.meth.str.len)
10049 continue;
10050
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010051 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +020010052 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
10053 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010054 return pattern;
10055 }
10056 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010057}
10058
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010059static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010060smp_fetch_rqver(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010061 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010062{
10063 struct http_txn *txn = l7;
10064 char *ptr;
10065 int len;
10066
Willy Tarreauc0239e02012-04-16 14:42:55 +020010067 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010068
Willy Tarreau8797c062007-05-07 00:55:35 +020010069 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010070 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010071
10072 while ((len-- > 0) && (*ptr++ != '/'));
10073 if (len <= 0)
10074 return 0;
10075
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010076 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010077 smp->data.str.str = ptr;
10078 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010079
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010080 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010081 return 1;
10082}
10083
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010084static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010085smp_fetch_stver(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010086 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010087{
10088 struct http_txn *txn = l7;
10089 char *ptr;
10090 int len;
10091
Willy Tarreauc0239e02012-04-16 14:42:55 +020010092 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010093
Willy Tarreauf26b2522012-12-14 08:33:14 +010010094 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10095 return 0;
10096
Willy Tarreau8797c062007-05-07 00:55:35 +020010097 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010098 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010099
10100 while ((len-- > 0) && (*ptr++ != '/'));
10101 if (len <= 0)
10102 return 0;
10103
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010104 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010105 smp->data.str.str = ptr;
10106 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010107
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010108 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010109 return 1;
10110}
10111
10112/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010113static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010114smp_fetch_stcode(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010115 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010116{
10117 struct http_txn *txn = l7;
10118 char *ptr;
10119 int len;
10120
Willy Tarreauc0239e02012-04-16 14:42:55 +020010121 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010122
Willy Tarreauf26b2522012-12-14 08:33:14 +010010123 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10124 return 0;
10125
Willy Tarreau8797c062007-05-07 00:55:35 +020010126 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010127 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010128
Willy Tarreauf853c462012-04-23 18:53:56 +020010129 smp->type = SMP_T_UINT;
10130 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010131 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010132 return 1;
10133}
10134
10135/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010136static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010137smp_fetch_url(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010138 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010139{
10140 struct http_txn *txn = l7;
10141
Willy Tarreauc0239e02012-04-16 14:42:55 +020010142 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010143
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010144 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010145 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010146 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010147 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010148 return 1;
10149}
10150
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010151static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010152smp_fetch_url_ip(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010153 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010154{
10155 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010156 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010157
Willy Tarreauc0239e02012-04-16 14:42:55 +020010158 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010159
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010160 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 +020010161 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010162 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010163
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010164 smp->type = SMP_T_IPV4;
10165 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010166 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010167 return 1;
10168}
10169
10170static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010171smp_fetch_url_port(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010172 const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010173{
10174 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010175 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010176
Willy Tarreauc0239e02012-04-16 14:42:55 +020010177 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010178
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010179 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 +020010180 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10181 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010182
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010183 smp->type = SMP_T_UINT;
10184 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010185 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010186 return 1;
10187}
10188
Willy Tarreau185b5c42012-04-26 15:11:51 +020010189/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10190 * Accepts an optional argument of type string containing the header field name,
10191 * and an optional argument of type signed or unsigned integer to request an
10192 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010193 * headers are considered from the first one. It does not stop on commas and
10194 * returns full lines instead (useful for User-Agent or Date for example).
10195 */
10196static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010197smp_fetch_fhdr(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010198 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010199{
10200 struct http_txn *txn = l7;
10201 struct hdr_idx *idx = &txn->hdr_idx;
10202 struct hdr_ctx *ctx = smp->ctx.a[0];
10203 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10204 int occ = 0;
10205 const char *name_str = NULL;
10206 int name_len = 0;
10207
10208 if (!ctx) {
10209 /* first call */
10210 ctx = &static_hdr_ctx;
10211 ctx->idx = 0;
10212 smp->ctx.a[0] = ctx;
10213 }
10214
10215 if (args) {
10216 if (args[0].type != ARGT_STR)
10217 return 0;
10218 name_str = args[0].data.str.str;
10219 name_len = args[0].data.str.len;
10220
10221 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10222 occ = args[1].data.uint;
10223 }
10224
10225 CHECK_HTTP_MESSAGE_FIRST();
10226
10227 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10228 /* search for header from the beginning */
10229 ctx->idx = 0;
10230
10231 if (!occ && !(opt & SMP_OPT_ITERATE))
10232 /* no explicit occurrence and single fetch => last header by default */
10233 occ = -1;
10234
10235 if (!occ)
10236 /* prepare to report multiple occurrences for ACL fetches */
10237 smp->flags |= SMP_F_NOT_LAST;
10238
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010239 smp->type = SMP_T_STR;
10240 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010241 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10242 return 1;
10243
10244 smp->flags &= ~SMP_F_NOT_LAST;
10245 return 0;
10246}
10247
10248/* 6. Check on HTTP header count. The number of occurrences is returned.
10249 * Accepts exactly 1 argument of type string. It does not stop on commas and
10250 * returns full lines instead (useful for User-Agent or Date for example).
10251 */
10252static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010253smp_fetch_fhdr_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010254 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010255{
10256 struct http_txn *txn = l7;
10257 struct hdr_idx *idx = &txn->hdr_idx;
10258 struct hdr_ctx ctx;
10259 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10260 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010261 const char *name = NULL;
10262 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010263
Willy Tarreau601a4d12015-04-01 19:16:09 +020010264 if (args && args->type == ARGT_STR) {
10265 name = args->data.str.str;
10266 len = args->data.str.len;
10267 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010268
10269 CHECK_HTTP_MESSAGE_FIRST();
10270
10271 ctx.idx = 0;
10272 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010273 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010274 cnt++;
10275
10276 smp->type = SMP_T_UINT;
10277 smp->data.uint = cnt;
10278 smp->flags = SMP_F_VOL_HDR;
10279 return 1;
10280}
10281
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010282static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010283smp_fetch_hdr_names(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010284 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010285{
10286 struct http_txn *txn = l7;
10287 struct hdr_idx *idx = &txn->hdr_idx;
10288 struct hdr_ctx ctx;
10289 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10290 struct chunk *temp;
10291 char del = ',';
10292
10293 if (args && args->type == ARGT_STR)
10294 del = *args[0].data.str.str;
10295
10296 CHECK_HTTP_MESSAGE_FIRST();
10297
10298 temp = get_trash_chunk();
10299
10300 ctx.idx = 0;
10301 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10302 if (temp->len)
10303 temp->str[temp->len++] = del;
10304 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10305 temp->len += ctx.del;
10306 }
10307
10308 smp->type = SMP_T_STR;
10309 smp->data.str.str = temp->str;
10310 smp->data.str.len = temp->len;
10311 smp->flags = SMP_F_VOL_HDR;
10312 return 1;
10313}
10314
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010315/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10316 * Accepts an optional argument of type string containing the header field name,
10317 * and an optional argument of type signed or unsigned integer to request an
10318 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010319 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010320 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010321static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010322smp_fetch_hdr(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010323 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010324{
10325 struct http_txn *txn = l7;
10326 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010327 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010328 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010329 int occ = 0;
10330 const char *name_str = NULL;
10331 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010332
Willy Tarreaua890d072013-04-02 12:01:06 +020010333 if (!ctx) {
10334 /* first call */
10335 ctx = &static_hdr_ctx;
10336 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010337 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010338 }
10339
Willy Tarreau185b5c42012-04-26 15:11:51 +020010340 if (args) {
10341 if (args[0].type != ARGT_STR)
10342 return 0;
10343 name_str = args[0].data.str.str;
10344 name_len = args[0].data.str.len;
10345
10346 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10347 occ = args[1].data.uint;
10348 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010349
Willy Tarreaue333ec92012-04-16 16:26:40 +020010350 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010351
Willy Tarreau185b5c42012-04-26 15:11:51 +020010352 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010353 /* search for header from the beginning */
10354 ctx->idx = 0;
10355
Willy Tarreau185b5c42012-04-26 15:11:51 +020010356 if (!occ && !(opt & SMP_OPT_ITERATE))
10357 /* no explicit occurrence and single fetch => last header by default */
10358 occ = -1;
10359
10360 if (!occ)
10361 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010362 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010363
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010364 smp->type = SMP_T_STR;
10365 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010366 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 +020010367 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010368
Willy Tarreau37406352012-04-23 16:16:37 +020010369 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010370 return 0;
10371}
10372
Willy Tarreauc11416f2007-06-17 16:58:38 +020010373/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010374 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010375 */
10376static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010377smp_fetch_hdr_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010378 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010379{
10380 struct http_txn *txn = l7;
10381 struct hdr_idx *idx = &txn->hdr_idx;
10382 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010383 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010384 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010385 const char *name = NULL;
10386 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010387
Willy Tarreau601a4d12015-04-01 19:16:09 +020010388 if (args && args->type == ARGT_STR) {
10389 name = args->data.str.str;
10390 len = args->data.str.len;
10391 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010392
Willy Tarreaue333ec92012-04-16 16:26:40 +020010393 CHECK_HTTP_MESSAGE_FIRST();
10394
Willy Tarreau33a7e692007-06-10 19:45:56 +020010395 ctx.idx = 0;
10396 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010397 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010398 cnt++;
10399
Willy Tarreauf853c462012-04-23 18:53:56 +020010400 smp->type = SMP_T_UINT;
10401 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010402 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010403 return 1;
10404}
10405
Willy Tarreau185b5c42012-04-26 15:11:51 +020010406/* Fetch an HTTP header's integer value. The integer value is returned. It
10407 * takes a mandatory argument of type string and an optional one of type int
10408 * to designate a specific occurrence. It returns an unsigned integer, which
10409 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010410 */
10411static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010412smp_fetch_hdr_val(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010413 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010414{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010415 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010416
Willy Tarreauf853c462012-04-23 18:53:56 +020010417 if (ret > 0) {
10418 smp->type = SMP_T_UINT;
10419 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10420 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010421
Willy Tarreaud53e2422012-04-16 17:21:11 +020010422 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010423}
10424
Cyril Bonté69fa9922012-10-25 00:01:06 +020010425/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10426 * and an optional one of type int to designate a specific occurrence.
10427 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010428 */
10429static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010430smp_fetch_hdr_ip(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010431 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010432{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010433 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010434
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010435 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw, private)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010436 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10437 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010438 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010439 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010440 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010441 if (smp->data.str.len < temp->size - 1) {
10442 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10443 temp->str[smp->data.str.len] = '\0';
10444 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10445 smp->type = SMP_T_IPV6;
10446 break;
10447 }
10448 }
10449 }
10450
Willy Tarreaud53e2422012-04-16 17:21:11 +020010451 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010452 if (!(smp->flags & SMP_F_NOT_LAST))
10453 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010454 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010455 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010456}
10457
Willy Tarreau737b0c12007-06-10 21:28:46 +020010458/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10459 * the first '/' after the possible hostname, and ends before the possible '?'.
10460 */
10461static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010462smp_fetch_path(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010463 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010464{
10465 struct http_txn *txn = l7;
10466 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010467
Willy Tarreauc0239e02012-04-16 14:42:55 +020010468 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010469
Willy Tarreau9b28e032012-10-12 23:49:43 +020010470 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010471 ptr = http_get_path(txn);
10472 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010473 return 0;
10474
10475 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010476 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010477 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010478
10479 while (ptr < end && *ptr != '?')
10480 ptr++;
10481
Willy Tarreauf853c462012-04-23 18:53:56 +020010482 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010483 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010484 return 1;
10485}
10486
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010487/* This produces a concatenation of the first occurrence of the Host header
10488 * followed by the path component if it begins with a slash ('/'). This means
10489 * that '*' will not be added, resulting in exactly the first Host entry.
10490 * If no Host header is found, then the path is returned as-is. The returned
10491 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010492 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010493 */
10494static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010495smp_fetch_base(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010496 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010497{
10498 struct http_txn *txn = l7;
10499 char *ptr, *end, *beg;
10500 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010501 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010502
10503 CHECK_HTTP_MESSAGE_FIRST();
10504
10505 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010506 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010507 return smp_fetch_path(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010508
10509 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010510 temp = get_trash_chunk();
10511 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010512 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010513 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010514 smp->data.str.len = ctx.vlen;
10515
10516 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010517 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010518 beg = http_get_path(txn);
10519 if (!beg)
10520 beg = end;
10521
10522 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10523
10524 if (beg < ptr && *beg == '/') {
10525 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10526 smp->data.str.len += ptr - beg;
10527 }
10528
10529 smp->flags = SMP_F_VOL_1ST;
10530 return 1;
10531}
10532
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010533/* This produces a 32-bit hash of the concatenation of the first occurrence of
10534 * the Host header followed by the path component if it begins with a slash ('/').
10535 * This means that '*' will not be added, resulting in exactly the first Host
10536 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010537 * is hashed using the path hash followed by a full avalanche hash and provides a
10538 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010539 * high-traffic sites without having to store whole paths.
10540 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010541int
Willy Tarreau87b09662015-04-03 00:22:06 +020010542smp_fetch_base32(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010543 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010544{
10545 struct http_txn *txn = l7;
10546 struct hdr_ctx ctx;
10547 unsigned int hash = 0;
10548 char *ptr, *beg, *end;
10549 int len;
10550
10551 CHECK_HTTP_MESSAGE_FIRST();
10552
10553 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010554 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010555 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10556 ptr = ctx.line + ctx.val;
10557 len = ctx.vlen;
10558 while (len--)
10559 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10560 }
10561
10562 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010563 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010564 beg = http_get_path(txn);
10565 if (!beg)
10566 beg = end;
10567
10568 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10569
10570 if (beg < ptr && *beg == '/') {
10571 while (beg < ptr)
10572 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10573 }
10574 hash = full_hash(hash);
10575
10576 smp->type = SMP_T_UINT;
10577 smp->data.uint = hash;
10578 smp->flags = SMP_F_VOL_1ST;
10579 return 1;
10580}
10581
Willy Tarreau4a550602012-12-09 14:53:32 +010010582/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010583 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10584 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10585 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010586 * that in environments where IPv6 is insignificant, truncating the output to
10587 * 8 bytes would still work.
10588 */
10589static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010590smp_fetch_base32_src(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010591 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010592{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010593 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020010594 struct session *sess = strm_sess(l4);
10595 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010596
10597 if (!cli_conn)
10598 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010599
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010600 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010601 return 0;
10602
Willy Tarreau47ca5452012-12-23 20:22:19 +010010603 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010604 *(unsigned int *)temp->str = htonl(smp->data.uint);
10605 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010606
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010607 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010608 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010609 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010610 temp->len += 4;
10611 break;
10612 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010613 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010614 temp->len += 16;
10615 break;
10616 default:
10617 return 0;
10618 }
10619
10620 smp->data.str = *temp;
10621 smp->type = SMP_T_BIN;
10622 return 1;
10623}
10624
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010625/* Extracts the query string, which comes after the question mark '?'. If no
10626 * question mark is found, nothing is returned. Otherwise it returns a sample
10627 * of type string carrying the whole query string.
10628 */
10629static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010630smp_fetch_query(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010631 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010632{
10633 struct http_txn *txn = l7;
10634 char *ptr, *end;
10635
10636 CHECK_HTTP_MESSAGE_FIRST();
10637
10638 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10639 end = ptr + txn->req.sl.rq.u_l;
10640
10641 /* look up the '?' */
10642 do {
10643 if (ptr == end)
10644 return 0;
10645 } while (*ptr++ != '?');
10646
10647 smp->type = SMP_T_STR;
10648 smp->data.str.str = ptr;
10649 smp->data.str.len = end - ptr;
10650 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10651 return 1;
10652}
10653
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010654static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010655smp_fetch_proto_http(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010656 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010657{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010658 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10659 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10660 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010661
Willy Tarreau24e32d82012-04-23 23:55:44 +020010662 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010663
Willy Tarreauf853c462012-04-23 18:53:56 +020010664 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010665 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010666 return 1;
10667}
10668
Willy Tarreau7f18e522010-10-22 20:04:13 +020010669/* return a valid test if the current request is the first one on the connection */
10670static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010671smp_fetch_http_first_req(struct proxy *px, struct stream *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010672 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010673{
Willy Tarreauf853c462012-04-23 18:53:56 +020010674 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010675 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010676 return 1;
10677}
10678
Willy Tarreau34db1082012-04-19 17:16:54 +020010679/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010680static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010681smp_fetch_http_auth(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010682 const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010683{
10684
Willy Tarreau24e32d82012-04-23 23:55:44 +020010685 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010686 return 0;
10687
Willy Tarreauc0239e02012-04-16 14:42:55 +020010688 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010689
Willy Tarreauc0239e02012-04-16 14:42:55 +020010690 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010691 return 0;
10692
Willy Tarreauf853c462012-04-23 18:53:56 +020010693 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010694 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010695 return 1;
10696}
Willy Tarreau8797c062007-05-07 00:55:35 +020010697
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010698/* Accepts exactly 1 argument of type userlist */
10699static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010700smp_fetch_http_auth_grp(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010701 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010702{
10703
10704 if (!args || args->type != ARGT_USR)
10705 return 0;
10706
10707 CHECK_HTTP_MESSAGE_FIRST();
10708
10709 if (!get_http_auth(l4))
10710 return 0;
10711
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010712 /* if the user does not belong to the userlist or has a wrong password,
10713 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010714 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010715 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010716 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10717 return 0;
10718
10719 /* pat_match_auth() will need the user list */
10720 smp->ctx.a[0] = args->data.usr;
10721
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010722 smp->type = SMP_T_STR;
10723 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010724 smp->data.str.str = l4->txn.auth.user;
10725 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010726
10727 return 1;
10728}
10729
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010730/* Try to find the next occurrence of a cookie name in a cookie header value.
10731 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10732 * the cookie value is returned into *value and *value_l, and the function
10733 * returns a pointer to the next pointer to search from if the value was found.
10734 * Otherwise if the cookie was not found, NULL is returned and neither value
10735 * nor value_l are touched. The input <hdr> string should first point to the
10736 * header's value, and the <hdr_end> pointer must point to the first character
10737 * not part of the value. <list> must be non-zero if value may represent a list
10738 * of values (cookie headers). This makes it faster to abort parsing when no
10739 * list is expected.
10740 */
10741static char *
10742extract_cookie_value(char *hdr, const char *hdr_end,
10743 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010744 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010745{
10746 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10747 char *next;
10748
10749 /* we search at least a cookie name followed by an equal, and more
10750 * generally something like this :
10751 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10752 */
10753 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10754 /* Iterate through all cookies on this line */
10755
10756 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10757 att_beg++;
10758
10759 /* find att_end : this is the first character after the last non
10760 * space before the equal. It may be equal to hdr_end.
10761 */
10762 equal = att_end = att_beg;
10763
10764 while (equal < hdr_end) {
10765 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10766 break;
10767 if (http_is_spht[(unsigned char)*equal++])
10768 continue;
10769 att_end = equal;
10770 }
10771
10772 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10773 * is between <att_beg> and <equal>, both may be identical.
10774 */
10775
10776 /* look for end of cookie if there is an equal sign */
10777 if (equal < hdr_end && *equal == '=') {
10778 /* look for the beginning of the value */
10779 val_beg = equal + 1;
10780 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10781 val_beg++;
10782
10783 /* find the end of the value, respecting quotes */
10784 next = find_cookie_value_end(val_beg, hdr_end);
10785
10786 /* make val_end point to the first white space or delimitor after the value */
10787 val_end = next;
10788 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10789 val_end--;
10790 } else {
10791 val_beg = val_end = next = equal;
10792 }
10793
10794 /* We have nothing to do with attributes beginning with '$'. However,
10795 * they will automatically be removed if a header before them is removed,
10796 * since they're supposed to be linked together.
10797 */
10798 if (*att_beg == '$')
10799 continue;
10800
10801 /* Ignore cookies with no equal sign */
10802 if (equal == next)
10803 continue;
10804
10805 /* Now we have the cookie name between att_beg and att_end, and
10806 * its value between val_beg and val_end.
10807 */
10808
10809 if (att_end - att_beg == cookie_name_l &&
10810 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10811 /* let's return this value and indicate where to go on from */
10812 *value = val_beg;
10813 *value_l = val_end - val_beg;
10814 return next + 1;
10815 }
10816
10817 /* Set-Cookie headers only have the name in the first attr=value part */
10818 if (!list)
10819 break;
10820 }
10821
10822 return NULL;
10823}
10824
William Lallemanda43ba4e2014-01-28 18:14:25 +010010825/* Fetch a captured HTTP request header. The index is the position of
10826 * the "capture" option in the configuration file
10827 */
10828static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010829smp_fetch_capture_header_req(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010830 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010831{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020010832 struct proxy *fe = strm_sess(l4)->fe;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010833 struct http_txn *txn = l7;
10834 int idx;
10835
10836 if (!args || args->type != ARGT_UINT)
10837 return 0;
10838
10839 idx = args->data.uint;
10840
10841 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10842 return 0;
10843
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010844 smp->type = SMP_T_STR;
10845 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010846 smp->data.str.str = txn->req.cap[idx];
10847 smp->data.str.len = strlen(txn->req.cap[idx]);
10848
10849 return 1;
10850}
10851
10852/* Fetch a captured HTTP response header. The index is the position of
10853 * the "capture" option in the configuration file
10854 */
10855static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010856smp_fetch_capture_header_res(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010857 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010858{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020010859 struct proxy *fe = strm_sess(l4)->fe;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010860 struct http_txn *txn = l7;
10861 int idx;
10862
10863 if (!args || args->type != ARGT_UINT)
10864 return 0;
10865
10866 idx = args->data.uint;
10867
10868 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10869 return 0;
10870
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010871 smp->type = SMP_T_STR;
10872 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010873 smp->data.str.str = txn->rsp.cap[idx];
10874 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10875
10876 return 1;
10877}
10878
William Lallemand65ad6e12014-01-31 15:08:02 +010010879/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10880static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010881smp_fetch_capture_req_method(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010882 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010883{
10884 struct chunk *temp;
10885 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010886 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010887
10888 if (!txn->uri)
10889 return 0;
10890
William Lallemand96a77852014-02-05 00:30:02 +010010891 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010892
William Lallemand96a77852014-02-05 00:30:02 +010010893 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10894 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010895
William Lallemand96a77852014-02-05 00:30:02 +010010896 temp = get_trash_chunk();
10897 temp->str = txn->uri;
10898 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010899 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010900 smp->type = SMP_T_STR;
10901 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010902
10903 return 1;
10904
10905}
10906
10907/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10908static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010909smp_fetch_capture_req_uri(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010910 const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010911{
10912 struct chunk *temp;
10913 struct http_txn *txn = l7;
10914 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010915
10916 if (!txn->uri)
10917 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010918
William Lallemand65ad6e12014-01-31 15:08:02 +010010919 ptr = txn->uri;
10920
10921 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10922 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010923
William Lallemand65ad6e12014-01-31 15:08:02 +010010924 if (!*ptr)
10925 return 0;
10926
10927 ptr++; /* skip the space */
10928
10929 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010930 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010931 if (!ptr)
10932 return 0;
10933 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10934 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010935
10936 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010937 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010938 smp->type = SMP_T_STR;
10939 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010940
10941 return 1;
10942}
10943
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010944/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10945 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10946 */
10947static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010948smp_fetch_capture_req_ver(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010949 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010950{
10951 struct http_txn *txn = l7;
10952
10953 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10954 return 0;
10955
10956 if (txn->req.flags & HTTP_MSGF_VER_11)
10957 smp->data.str.str = "HTTP/1.1";
10958 else
10959 smp->data.str.str = "HTTP/1.0";
10960
10961 smp->data.str.len = 8;
10962 smp->type = SMP_T_STR;
10963 smp->flags = SMP_F_CONST;
10964 return 1;
10965
10966}
10967
10968/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10969 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10970 */
10971static int
Willy Tarreau87b09662015-04-03 00:22:06 +020010972smp_fetch_capture_res_ver(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010010973 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010974{
10975 struct http_txn *txn = l7;
10976
10977 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10978 return 0;
10979
10980 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10981 smp->data.str.str = "HTTP/1.1";
10982 else
10983 smp->data.str.str = "HTTP/1.0";
10984
10985 smp->data.str.len = 8;
10986 smp->type = SMP_T_STR;
10987 smp->flags = SMP_F_CONST;
10988 return 1;
10989
10990}
10991
William Lallemand65ad6e12014-01-31 15:08:02 +010010992
Willy Tarreaue333ec92012-04-16 16:26:40 +020010993/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010994 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010995 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010996 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010997 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010998 * Accepts exactly 1 argument of type string. If the input options indicate
10999 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011000 * The returned sample is of type CSTR. Can be used to parse cookies in other
11001 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011002 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011003int smp_fetch_cookie(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011004 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011005{
11006 struct http_txn *txn = l7;
11007 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011008 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011009 const struct http_msg *msg;
11010 const char *hdr_name;
11011 int hdr_name_len;
11012 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011013 int occ = 0;
11014 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011015
Willy Tarreau24e32d82012-04-23 23:55:44 +020011016 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011017 return 0;
11018
Willy Tarreaua890d072013-04-02 12:01:06 +020011019 if (!ctx) {
11020 /* first call */
11021 ctx = &static_hdr_ctx;
11022 ctx->idx = 0;
11023 smp->ctx.a[2] = ctx;
11024 }
11025
Willy Tarreaue333ec92012-04-16 16:26:40 +020011026 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011027
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011028 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011029 msg = &txn->req;
11030 hdr_name = "Cookie";
11031 hdr_name_len = 6;
11032 } else {
11033 msg = &txn->rsp;
11034 hdr_name = "Set-Cookie";
11035 hdr_name_len = 10;
11036 }
11037
Willy Tarreau28376d62012-04-26 21:26:10 +020011038 if (!occ && !(opt & SMP_OPT_ITERATE))
11039 /* no explicit occurrence and single fetch => last cookie by default */
11040 occ = -1;
11041
11042 /* OK so basically here, either we want only one value and it's the
11043 * last one, or we want to iterate over all of them and we fetch the
11044 * next one.
11045 */
11046
Willy Tarreau9b28e032012-10-12 23:49:43 +020011047 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011048 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011049 /* search for the header from the beginning, we must first initialize
11050 * the search parameters.
11051 */
Willy Tarreau37406352012-04-23 16:16:37 +020011052 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011053 ctx->idx = 0;
11054 }
11055
Willy Tarreau28376d62012-04-26 21:26:10 +020011056 smp->flags |= SMP_F_VOL_HDR;
11057
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011058 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011059 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11060 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011061 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11062 goto out;
11063
Willy Tarreau24e32d82012-04-23 23:55:44 +020011064 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011065 continue;
11066
Willy Tarreau37406352012-04-23 16:16:37 +020011067 smp->ctx.a[0] = ctx->line + ctx->val;
11068 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011069 }
11070
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011071 smp->type = SMP_T_STR;
11072 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011073 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011074 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011075 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011076 &smp->data.str.str,
11077 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011078 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011079 found = 1;
11080 if (occ >= 0) {
11081 /* one value was returned into smp->data.str.{str,len} */
11082 smp->flags |= SMP_F_NOT_LAST;
11083 return 1;
11084 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011085 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011086 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011087 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011088 /* all cookie headers and values were scanned. If we're looking for the
11089 * last occurrence, we may return it now.
11090 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011091 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011092 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011093 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011094}
11095
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011096/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011097 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011098 * multiple cookies may be parsed on the same line. The returned sample is of
11099 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011100 */
11101static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011102smp_fetch_cookie_cnt(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011103 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011104{
11105 struct http_txn *txn = l7;
11106 struct hdr_idx *idx = &txn->hdr_idx;
11107 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011108 const struct http_msg *msg;
11109 const char *hdr_name;
11110 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011111 int cnt;
11112 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011113 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011114
Willy Tarreau24e32d82012-04-23 23:55:44 +020011115 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011116 return 0;
11117
Willy Tarreaue333ec92012-04-16 16:26:40 +020011118 CHECK_HTTP_MESSAGE_FIRST();
11119
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011120 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011121 msg = &txn->req;
11122 hdr_name = "Cookie";
11123 hdr_name_len = 6;
11124 } else {
11125 msg = &txn->rsp;
11126 hdr_name = "Set-Cookie";
11127 hdr_name_len = 10;
11128 }
11129
Willy Tarreau9b28e032012-10-12 23:49:43 +020011130 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011131 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011132 ctx.idx = 0;
11133 cnt = 0;
11134
11135 while (1) {
11136 /* Note: val_beg == NULL every time we need to fetch a new header */
11137 if (!val_beg) {
11138 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11139 break;
11140
Willy Tarreau24e32d82012-04-23 23:55:44 +020011141 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011142 continue;
11143
11144 val_beg = ctx.line + ctx.val;
11145 val_end = val_beg + ctx.vlen;
11146 }
11147
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011148 smp->type = SMP_T_STR;
11149 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011150 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011151 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020011152 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020011153 &smp->data.str.str,
11154 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011155 cnt++;
11156 }
11157 }
11158
Willy Tarreaub169eba2013-12-16 15:14:43 +010011159 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011160 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011161 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011162 return 1;
11163}
11164
Willy Tarreau51539362012-05-08 12:46:28 +020011165/* Fetch an cookie's integer value. The integer value is returned. It
11166 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11167 */
11168static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011169smp_fetch_cookie_val(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011170 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011171{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011172 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011173
11174 if (ret > 0) {
11175 smp->type = SMP_T_UINT;
11176 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11177 }
11178
11179 return ret;
11180}
11181
Willy Tarreau8797c062007-05-07 00:55:35 +020011182/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011183/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011184/************************************************************************/
11185
David Cournapeau16023ee2010-12-23 20:55:41 +090011186/*
11187 * Given a path string and its length, find the position of beginning of the
11188 * query string. Returns NULL if no query string is found in the path.
11189 *
11190 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11191 *
11192 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11193 */
bedis4c75cca2012-10-05 08:38:24 +020011194static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011195{
11196 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011197
bedis4c75cca2012-10-05 08:38:24 +020011198 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011199 return p ? p + 1 : NULL;
11200}
11201
bedis4c75cca2012-10-05 08:38:24 +020011202static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011203{
bedis4c75cca2012-10-05 08:38:24 +020011204 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011205}
11206
11207/*
11208 * Given a url parameter, find the starting position of the first occurence,
11209 * or NULL if the parameter is not found.
11210 *
11211 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11212 * the function will return query_string+8.
11213 */
11214static char*
11215find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011216 char* url_param_name, size_t url_param_name_l,
11217 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011218{
11219 char *pos, *last;
11220
11221 pos = query_string;
11222 last = query_string + query_string_l - url_param_name_l - 1;
11223
11224 while (pos <= last) {
11225 if (pos[url_param_name_l] == '=') {
11226 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11227 return pos;
11228 pos += url_param_name_l + 1;
11229 }
bedis4c75cca2012-10-05 08:38:24 +020011230 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011231 pos++;
11232 pos++;
11233 }
11234 return NULL;
11235}
11236
11237/*
11238 * Given a url parameter name, returns its value and size into *value and
11239 * *value_l respectively, and returns non-zero. If the parameter is not found,
11240 * zero is returned and value/value_l are not touched.
11241 */
11242static int
11243find_url_param_value(char* path, size_t path_l,
11244 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011245 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011246{
11247 char *query_string, *qs_end;
11248 char *arg_start;
11249 char *value_start, *value_end;
11250
bedis4c75cca2012-10-05 08:38:24 +020011251 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011252 if (!query_string)
11253 return 0;
11254
11255 qs_end = path + path_l;
11256 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011257 url_param_name, url_param_name_l,
11258 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011259 if (!arg_start)
11260 return 0;
11261
11262 value_start = arg_start + url_param_name_l + 1;
11263 value_end = value_start;
11264
bedis4c75cca2012-10-05 08:38:24 +020011265 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011266 value_end++;
11267
11268 *value = value_start;
11269 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011270 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011271}
11272
11273static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011274smp_fetch_url_param(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011275 const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011276{
bedis4c75cca2012-10-05 08:38:24 +020011277 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011278 struct http_txn *txn = l7;
11279 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011280
bedis4c75cca2012-10-05 08:38:24 +020011281 if (!args || args[0].type != ARGT_STR ||
11282 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011283 return 0;
11284
11285 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011286
bedis4c75cca2012-10-05 08:38:24 +020011287 if (args[1].type)
11288 delim = *args[1].data.str.str;
11289
Willy Tarreau9b28e032012-10-12 23:49:43 +020011290 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011291 args->data.str.str, args->data.str.len,
11292 &smp->data.str.str, &smp->data.str.len,
11293 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011294 return 0;
11295
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011296 smp->type = SMP_T_STR;
11297 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011298 return 1;
11299}
11300
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011301/* Return the signed integer value for the specified url parameter (see url_param
11302 * above).
11303 */
11304static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011305smp_fetch_url_param_val(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011306 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011307{
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011308 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011309
11310 if (ret > 0) {
11311 smp->type = SMP_T_UINT;
11312 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11313 }
11314
11315 return ret;
11316}
11317
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011318/* This produces a 32-bit hash of the concatenation of the first occurrence of
11319 * the Host header followed by the path component if it begins with a slash ('/').
11320 * This means that '*' will not be added, resulting in exactly the first Host
11321 * entry. If no Host header is found, then the path is used. The resulting value
11322 * is hashed using the url hash followed by a full avalanche hash and provides a
11323 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11324 * high-traffic sites without having to store whole paths.
11325 * this differs from the base32 functions in that it includes the url parameters
11326 * as well as the path
11327 */
11328static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011329smp_fetch_url32(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011330 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011331{
11332 struct http_txn *txn = l7;
11333 struct hdr_ctx ctx;
11334 unsigned int hash = 0;
11335 char *ptr, *beg, *end;
11336 int len;
11337
11338 CHECK_HTTP_MESSAGE_FIRST();
11339
11340 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011341 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011342 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11343 ptr = ctx.line + ctx.val;
11344 len = ctx.vlen;
11345 while (len--)
11346 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11347 }
11348
11349 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011350 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 +000011351 beg = http_get_path(txn);
11352 if (!beg)
11353 beg = end;
11354
11355 for (ptr = beg; ptr < end ; ptr++);
11356
11357 if (beg < ptr && *beg == '/') {
11358 while (beg < ptr)
11359 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11360 }
11361 hash = full_hash(hash);
11362
11363 smp->type = SMP_T_UINT;
11364 smp->data.uint = hash;
11365 smp->flags = SMP_F_VOL_1ST;
11366 return 1;
11367}
11368
11369/* This concatenates the source address with the 32-bit hash of the Host and
11370 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11371 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11372 * on the source address length. The URL hash is stored before the address so
11373 * that in environments where IPv6 is insignificant, truncating the output to
11374 * 8 bytes would still work.
11375 */
11376static int
Willy Tarreau87b09662015-04-03 00:22:06 +020011377smp_fetch_url32_src(struct proxy *px, struct stream *l4, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011378 const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011379{
11380 struct chunk *temp;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020011381 struct session *sess = strm_sess(l4);
11382 struct connection *cli_conn = objt_conn(sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011383
Thierry FOURNIERf41a8092014-12-07 18:37:57 +010011384 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011385 return 0;
11386
11387 temp = get_trash_chunk();
11388 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11389 temp->len += sizeof(smp->data.uint);
11390
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011391 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011392 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011393 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011394 temp->len += 4;
11395 break;
11396 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011397 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011398 temp->len += 16;
11399 break;
11400 default:
11401 return 0;
11402 }
11403
11404 smp->data.str = *temp;
11405 smp->type = SMP_T_BIN;
11406 return 1;
11407}
11408
Willy Tarreau185b5c42012-04-26 15:11:51 +020011409/* This function is used to validate the arguments passed to any "hdr" fetch
11410 * keyword. These keywords support an optional positive or negative occurrence
11411 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11412 * is assumed that the types are already the correct ones. Returns 0 on error,
11413 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11414 * error message in case of error, that the caller is responsible for freeing.
11415 * The initial location must either be freeable or NULL.
11416 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011417int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011418{
11419 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011420 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011421 return 0;
11422 }
11423 return 1;
11424}
11425
Willy Tarreau276fae92013-07-25 14:36:01 +020011426/* takes an UINT value on input supposed to represent the time since EPOCH,
11427 * adds an optional offset found in args[0] and emits a string representing
11428 * the date in RFC-1123/5322 format.
11429 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011430static int sample_conv_http_date(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011431 struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011432{
11433 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11434 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11435 struct chunk *temp;
11436 struct tm *tm;
11437 time_t curr_date = smp->data.uint;
11438
11439 /* add offset */
11440 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11441 curr_date += args[0].data.sint;
11442
11443 tm = gmtime(&curr_date);
11444
11445 temp = get_trash_chunk();
11446 temp->len = snprintf(temp->str, temp->size - temp->len,
11447 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11448 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11449 tm->tm_hour, tm->tm_min, tm->tm_sec);
11450
11451 smp->data.str = *temp;
11452 smp->type = SMP_T_STR;
11453 return 1;
11454}
11455
Thierry FOURNIERad903512014-04-11 17:51:01 +020011456/* Match language range with language tag. RFC2616 14.4:
11457 *
11458 * A language-range matches a language-tag if it exactly equals
11459 * the tag, or if it exactly equals a prefix of the tag such
11460 * that the first tag character following the prefix is "-".
11461 *
11462 * Return 1 if the strings match, else return 0.
11463 */
11464static inline int language_range_match(const char *range, int range_len,
11465 const char *tag, int tag_len)
11466{
11467 const char *end = range + range_len;
11468 const char *tend = tag + tag_len;
11469 while (range < end) {
11470 if (*range == '-' && tag == tend)
11471 return 1;
11472 if (*range != *tag || tag == tend)
11473 return 0;
11474 range++;
11475 tag++;
11476 }
11477 /* Return true only if the last char of the tag is matched. */
11478 return tag == tend;
11479}
11480
11481/* Arguments: The list of expected value, the number of parts returned and the separator */
Willy Tarreau87b09662015-04-03 00:22:06 +020011482static int sample_conv_q_prefered(struct stream *stream, const struct arg *args,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +010011483 struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011484{
11485 const char *al = smp->data.str.str;
11486 const char *end = al + smp->data.str.len;
11487 const char *token;
11488 int toklen;
11489 int qvalue;
11490 const char *str;
11491 const char *w;
11492 int best_q = 0;
11493
11494 /* Set the constant to the sample, because the output of the
11495 * function will be peek in the constant configuration string.
11496 */
11497 smp->flags |= SMP_F_CONST;
11498 smp->data.str.size = 0;
11499 smp->data.str.str = "";
11500 smp->data.str.len = 0;
11501
11502 /* Parse the accept language */
11503 while (1) {
11504
11505 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011506 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011507 al++;
11508 if (al >= end)
11509 break;
11510
11511 /* Start of the fisrt word. */
11512 token = al;
11513
11514 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011515 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011516 al++;
11517 if (al == token)
11518 goto expect_comma;
11519
11520 /* Length of the token. */
11521 toklen = al - token;
11522 qvalue = 1000;
11523
11524 /* Check if the token exists in the list. If the token not exists,
11525 * jump to the next token.
11526 */
11527 str = args[0].data.str.str;
11528 w = str;
11529 while (1) {
11530 if (*str == ';' || *str == '\0') {
11531 if (language_range_match(token, toklen, w, str-w))
11532 goto look_for_q;
11533 if (*str == '\0')
11534 goto expect_comma;
11535 w = str + 1;
11536 }
11537 str++;
11538 }
11539 goto expect_comma;
11540
11541look_for_q:
11542
11543 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011544 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011545 al++;
11546 if (al >= end)
11547 goto process_value;
11548
11549 /* If ',' is found, process the result */
11550 if (*al == ',')
11551 goto process_value;
11552
11553 /* If the character is different from ';', look
11554 * for the end of the header part in best effort.
11555 */
11556 if (*al != ';')
11557 goto expect_comma;
11558
11559 /* Assumes that the char is ';', now expect "q=". */
11560 al++;
11561
11562 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011563 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011564 al++;
11565 if (al >= end)
11566 goto process_value;
11567
11568 /* Expect 'q'. If no 'q', continue in best effort */
11569 if (*al != 'q')
11570 goto process_value;
11571 al++;
11572
11573 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011574 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011575 al++;
11576 if (al >= end)
11577 goto process_value;
11578
11579 /* Expect '='. If no '=', continue in best effort */
11580 if (*al != '=')
11581 goto process_value;
11582 al++;
11583
11584 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011585 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011586 al++;
11587 if (al >= end)
11588 goto process_value;
11589
11590 /* Parse the q value. */
11591 qvalue = parse_qvalue(al, &al);
11592
11593process_value:
11594
11595 /* If the new q value is the best q value, then store the associated
11596 * language in the response. If qvalue is the biggest value (1000),
11597 * break the process.
11598 */
11599 if (qvalue > best_q) {
11600 smp->data.str.str = (char *)w;
11601 smp->data.str.len = str - w;
11602 if (qvalue >= 1000)
11603 break;
11604 best_q = qvalue;
11605 }
11606
11607expect_comma:
11608
11609 /* Expect comma or end. If the end is detected, quit the loop. */
11610 while (al < end && *al != ',')
11611 al++;
11612 if (al >= end)
11613 break;
11614
11615 /* Comma is found, jump it and restart the analyzer. */
11616 al++;
11617 }
11618
11619 /* Set default value if required. */
11620 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11621 smp->data.str.str = args[1].data.str.str;
11622 smp->data.str.len = args[1].data.str.len;
11623 }
11624
11625 /* Return true only if a matching language was found. */
11626 return smp->data.str.len != 0;
11627}
11628
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011629/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011630 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011631 * the relevant part of the request line accordingly. Then it updates various
11632 * pointers to the next elements which were moved, and the total buffer length.
11633 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011634 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11635 * error, though this can be revisited when this code is finally exploited.
11636 *
11637 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11638 * query string and 3 to replace uri.
11639 *
11640 * In query string case, the mark question '?' must be set at the start of the
11641 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011642 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011643int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau87b09662015-04-03 00:22:06 +020011644 struct proxy *px, struct stream *s, struct http_txn *txn)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011645{
11646 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011647 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011648 int delta;
11649
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011650 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011651 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011652 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011653 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11654
11655 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011656 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011657 txn->req.sl.rq.m_l += delta;
11658 txn->req.sl.rq.u += delta;
11659 txn->req.sl.rq.v += delta;
11660 break;
11661
11662 case 1: // path
11663 cur_ptr = http_get_path(txn);
11664 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011665 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011666
11667 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011668 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 +010011669 cur_end++;
11670
11671 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011672 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011673 txn->req.sl.rq.u_l += delta;
11674 txn->req.sl.rq.v += delta;
11675 break;
11676
11677 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011678 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011679 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011680 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11681 while (cur_ptr < cur_end && *cur_ptr != '?')
11682 cur_ptr++;
11683
11684 /* skip the question mark or indicate that we must insert it
11685 * (but only if the format string is not empty then).
11686 */
11687 if (cur_ptr < cur_end)
11688 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011689 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011690 offset = 0;
11691
11692 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011693 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011694 txn->req.sl.rq.u_l += delta;
11695 txn->req.sl.rq.v += delta;
11696 break;
11697
11698 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011699 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011700 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11701
11702 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011703 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011704 txn->req.sl.rq.u_l += delta;
11705 txn->req.sl.rq.v += delta;
11706 break;
11707
11708 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011709 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011710 }
11711
11712 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011713 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011714 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011715 return 0;
11716}
11717
11718/* This function executes one of the set-{method,path,query,uri} actions. It
11719 * builds a string in the trash from the specified format string. It finds
11720 * the action to be performed in p[2], previously filled by function
11721 * parse_set_req_line(). The replacement action is excuted by the function
11722 * http_action_set_req_line_exec(). It always returns 1. If an error occurs
11723 * the action is canceled, but the rule processing continue.
11724 */
Willy Tarreau87b09662015-04-03 00:22:06 +020011725int http_action_set_req_line(struct http_req_rule *rule, struct proxy *px, struct stream *s, struct http_txn *txn)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011726{
11727 chunk_reset(&trash);
11728
11729 /* If we have to create a query string, prepare a '?'. */
11730 if (*(int *)&rule->arg.act.p[2] == 2)
11731 trash.str[trash.len++] = '?';
11732 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, (struct list *)&rule->arg.act.p[0]);
11733
11734 http_replace_req_line(*(int *)&rule->arg.act.p[2], trash.str, trash.len, px, s, txn);
Thierry FOURNIER01c30122015-03-14 14:14:47 +010011735 return 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011736}
11737
11738/* parse an http-request action among :
11739 * set-method
11740 * set-path
11741 * set-query
11742 * set-uri
11743 *
11744 * All of them accept a single argument of type string representing a log-format.
11745 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11746 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
11747 * It returns 0 on success, < 0 on error.
11748 */
11749int parse_set_req_line(const char **args, int *orig_arg, struct proxy *px, struct http_req_rule *rule, char **err)
11750{
11751 int cur_arg = *orig_arg;
11752
11753 rule->action = HTTP_REQ_ACT_CUSTOM_CONT;
11754
11755 switch (args[0][4]) {
11756 case 'm' :
11757 *(int *)&rule->arg.act.p[2] = 0;
11758 rule->action_ptr = http_action_set_req_line;
11759 break;
11760 case 'p' :
11761 *(int *)&rule->arg.act.p[2] = 1;
11762 rule->action_ptr = http_action_set_req_line;
11763 break;
11764 case 'q' :
11765 *(int *)&rule->arg.act.p[2] = 2;
11766 rule->action_ptr = http_action_set_req_line;
11767 break;
11768 case 'u' :
11769 *(int *)&rule->arg.act.p[2] = 3;
11770 rule->action_ptr = http_action_set_req_line;
11771 break;
11772 default:
11773 memprintf(err, "internal error: unhandled action '%s'", args[0]);
11774 return -1;
11775 }
11776
11777 if (!*args[cur_arg] ||
11778 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11779 memprintf(err, "expects exactly 1 argument <format>");
11780 return -1;
11781 }
11782
11783 LIST_INIT((struct list *)&rule->arg.act.p[0]);
11784 proxy->conf.args.ctx = ARGC_HRQ;
11785 parse_logformat_string(args[cur_arg], proxy, (struct list *)&rule->arg.act.p[0], LOG_OPT_HTTP,
11786 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
11787 proxy->conf.args.file, proxy->conf.args.line);
11788
11789 (*orig_arg)++;
11790 return 0;
11791}
11792
William Lallemand73025dd2014-04-24 14:38:37 +020011793/*
11794 * Return the struct http_req_action_kw associated to a keyword.
11795 */
11796struct http_req_action_kw *action_http_req_custom(const char *kw)
11797{
11798 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11799 struct http_req_action_kw_list *kw_list;
11800 int i;
11801
11802 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11803 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11804 if (!strcmp(kw, kw_list->kw[i].kw))
11805 return &kw_list->kw[i];
11806 }
11807 }
11808 }
11809 return NULL;
11810}
11811
11812/*
11813 * Return the struct http_res_action_kw associated to a keyword.
11814 */
11815struct http_res_action_kw *action_http_res_custom(const char *kw)
11816{
11817 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11818 struct http_res_action_kw_list *kw_list;
11819 int i;
11820
11821 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11822 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11823 if (!strcmp(kw, kw_list->kw[i].kw))
11824 return &kw_list->kw[i];
11825 }
11826 }
11827 }
11828 return NULL;
11829}
11830
Willy Tarreau4a568972010-05-12 08:08:50 +020011831/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011832/* All supported ACL keywords must be declared here. */
11833/************************************************************************/
11834
11835/* Note: must not be declared <const> as its list will be overwritten.
11836 * Please take care of keeping this list alphabetically sorted.
11837 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011838static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011839 { "base", "base", PAT_MATCH_STR },
11840 { "base_beg", "base", PAT_MATCH_BEG },
11841 { "base_dir", "base", PAT_MATCH_DIR },
11842 { "base_dom", "base", PAT_MATCH_DOM },
11843 { "base_end", "base", PAT_MATCH_END },
11844 { "base_len", "base", PAT_MATCH_LEN },
11845 { "base_reg", "base", PAT_MATCH_REG },
11846 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011847
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011848 { "cook", "req.cook", PAT_MATCH_STR },
11849 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11850 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11851 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11852 { "cook_end", "req.cook", PAT_MATCH_END },
11853 { "cook_len", "req.cook", PAT_MATCH_LEN },
11854 { "cook_reg", "req.cook", PAT_MATCH_REG },
11855 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011856
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011857 { "hdr", "req.hdr", PAT_MATCH_STR },
11858 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11859 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11860 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11861 { "hdr_end", "req.hdr", PAT_MATCH_END },
11862 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11863 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11864 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011865
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011866 /* these two declarations uses strings with list storage (in place
11867 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11868 * and delete functions are relative to the list management. The parse
11869 * and match method are related to the corresponding fetch methods. This
11870 * is very particular ACL declaration mode.
11871 */
11872 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11873 { "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 +020011874
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011875 { "path", "path", PAT_MATCH_STR },
11876 { "path_beg", "path", PAT_MATCH_BEG },
11877 { "path_dir", "path", PAT_MATCH_DIR },
11878 { "path_dom", "path", PAT_MATCH_DOM },
11879 { "path_end", "path", PAT_MATCH_END },
11880 { "path_len", "path", PAT_MATCH_LEN },
11881 { "path_reg", "path", PAT_MATCH_REG },
11882 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011883
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011884 { "req_ver", "req.ver", PAT_MATCH_STR },
11885 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011886
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011887 { "scook", "res.cook", PAT_MATCH_STR },
11888 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11889 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11890 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11891 { "scook_end", "res.cook", PAT_MATCH_END },
11892 { "scook_len", "res.cook", PAT_MATCH_LEN },
11893 { "scook_reg", "res.cook", PAT_MATCH_REG },
11894 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011895
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011896 { "shdr", "res.hdr", PAT_MATCH_STR },
11897 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11898 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11899 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11900 { "shdr_end", "res.hdr", PAT_MATCH_END },
11901 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11902 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11903 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011904
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011905 { "url", "url", PAT_MATCH_STR },
11906 { "url_beg", "url", PAT_MATCH_BEG },
11907 { "url_dir", "url", PAT_MATCH_DIR },
11908 { "url_dom", "url", PAT_MATCH_DOM },
11909 { "url_end", "url", PAT_MATCH_END },
11910 { "url_len", "url", PAT_MATCH_LEN },
11911 { "url_reg", "url", PAT_MATCH_REG },
11912 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011913
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011914 { "urlp", "urlp", PAT_MATCH_STR },
11915 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11916 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11917 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11918 { "urlp_end", "urlp", PAT_MATCH_END },
11919 { "urlp_len", "urlp", PAT_MATCH_LEN },
11920 { "urlp_reg", "urlp", PAT_MATCH_REG },
11921 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011922
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011923 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011924}};
11925
11926/************************************************************************/
11927/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011928/************************************************************************/
11929/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011930static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011931 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011932 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11933 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11934
Willy Tarreau87b09662015-04-03 00:22:06 +020011935 /* capture are allocated and are permanent in the stream */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011936 { "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 +020011937
11938 /* retrieve these captures from the HTTP logs */
11939 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11940 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11941 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11942
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011943 { "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 +020011944 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011945
Willy Tarreau409bcde2013-01-08 00:31:00 +010011946 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11947 * are only here to match the ACL's name, are request-only and are used
11948 * for ACL compatibility only.
11949 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011950 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11951 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011952 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11953 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11954
11955 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11956 * only here to match the ACL's name, are request-only and are used for
11957 * ACL compatibility only.
11958 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011959 { "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 +010011960 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11961 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11962 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11963
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011964 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011965 { "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 +010011966 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011967 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011968 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011969 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011970
11971 /* HTTP protocol on the request path */
11972 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011973 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011974
11975 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011976 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11977 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011978
11979 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011980 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11981 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011982
Willy Tarreau18ed2562013-01-14 15:56:36 +010011983 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011984 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011985 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11986 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11987
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011988 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011989 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011990 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011991 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11992 { "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 +010011993 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011994 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11995
11996 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011997 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011998 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11999 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12000
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012001 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020012002 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012003 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012004 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12005 { "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 +010012006 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012007 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12008
Willy Tarreau409bcde2013-01-08 00:31:00 +010012009 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012010 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012011 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12012 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012013 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012014
12015 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012016 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012017 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
12018 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
12019 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
12020
12021 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012022 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012023 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
12024 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012025 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
12026 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012027 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12028 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012029 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
12030 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012031}};
12032
Willy Tarreau8797c062007-05-07 00:55:35 +020012033
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012034/************************************************************************/
12035/* All supported converter keywords must be declared here. */
12036/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012037/* Note: must not be declared <const> as its list will be overwritten */
12038static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020012039 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
12040 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012041 { NULL, NULL, 0, 0, 0 },
12042}};
12043
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012044/************************************************************************/
12045/* All supported http-request action keywords must be declared here. */
12046/************************************************************************/
12047struct http_req_action_kw_list http_req_actions = {
12048 .scope = "http",
12049 .kw = {
12050 { "set-method", parse_set_req_line },
12051 { "set-path", parse_set_req_line },
12052 { "set-query", parse_set_req_line },
12053 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012054 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012055 }
12056};
12057
Willy Tarreau8797c062007-05-07 00:55:35 +020012058__attribute__((constructor))
12059static void __http_protocol_init(void)
12060{
12061 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012062 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012063 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012064 http_req_keywords_register(&http_req_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012065}
12066
12067
Willy Tarreau58f10d72006-12-04 02:26:12 +010012068/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012069 * Local variables:
12070 * c-indent-level: 8
12071 * c-basic-offset: 8
12072 * End:
12073 */