blob: 1953caa10580ac3218844ce18b7c7e56b0af483e [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 Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.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"
127 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
128
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 Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
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,
452 ['H'] = 1, ['P'] = 1, ['T'] = 1,
453};
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 Tarreau68085d82010-01-18 14:54:04 +0100590/* Find the end of the header value contained between <s> and <e>. See RFC2616,
591 * par 2.2 for more information. Note that it requires a valid header to return
592 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200593 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100594char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200595{
596 int quoted, qdpair;
597
598 quoted = qdpair = 0;
599 for (; s < e; s++) {
600 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200601 else if (quoted) {
602 if (*s == '\\') qdpair = 1;
603 else if (*s == '"') quoted = 0;
604 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200605 else if (*s == '"') quoted = 1;
606 else if (*s == ',') return s;
607 }
608 return s;
609}
610
611/* Find the first or next occurrence of header <name> in message buffer <sol>
612 * using headers index <idx>, and return it in the <ctx> structure. This
613 * structure holds everything necessary to use the header and find next
614 * occurrence. If its <idx> member is 0, the header is searched from the
615 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100616 * 1 when it finds a value, and 0 when there is no more. It is designed to work
617 * with headers defined as comma-separated lists. As a special case, if ctx->val
618 * is NULL when searching for a new values of a header, the current header is
619 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200620 */
621int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200623 struct hdr_ctx *ctx)
624{
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 char *eol, *sov;
626 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200627
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 cur_idx = ctx->idx;
629 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200630 /* We have previously returned a value, let's search
631 * another one on the same line.
632 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200633 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200634 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100635 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200636 eol = sol + idx->v[cur_idx].len;
637
638 if (sov >= eol)
639 /* no more values in this header */
640 goto next_hdr;
641
Willy Tarreau68085d82010-01-18 14:54:04 +0100642 /* values remaining for this header, skip the comma but save it
643 * for later use (eg: for header deletion).
644 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200645 sov++;
646 while (sov < eol && http_is_lws[(unsigned char)*sov])
647 sov++;
648
649 goto return_hdr;
650 }
651
652 /* first request for this header */
653 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100654 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200655 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200656 while (cur_idx) {
657 eol = sol + idx->v[cur_idx].len;
658
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200659 if (len == 0) {
660 /* No argument was passed, we want any header.
661 * To achieve this, we simply build a fake request. */
662 while (sol + len < eol && sol[len] != ':')
663 len++;
664 name = sol;
665 }
666
Willy Tarreau33a7e692007-06-10 19:45:56 +0200667 if ((len < eol - sol) &&
668 (sol[len] == ':') &&
669 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100670 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200671 sov = sol + len + 1;
672 while (sov < eol && http_is_lws[(unsigned char)*sov])
673 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100674
Willy Tarreau33a7e692007-06-10 19:45:56 +0200675 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100676 ctx->prev = old_idx;
677 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678 ctx->idx = cur_idx;
679 ctx->val = sov - sol;
680
681 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200682 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200683 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200684 eol--;
685 ctx->tws++;
686 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 ctx->vlen = eol - sov;
688 return 1;
689 }
690 next_hdr:
691 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100692 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200693 cur_idx = idx->v[cur_idx].next;
694 }
695 return 0;
696}
697
698int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200700 struct hdr_ctx *ctx)
701{
702 return http_find_header2(name, strlen(name), sol, idx, ctx);
703}
704
Willy Tarreau68085d82010-01-18 14:54:04 +0100705/* Remove one value of a header. This only works on a <ctx> returned by one of
706 * the http_find_header functions. The value is removed, as well as surrounding
707 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100708 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100709 * message <msg>. The new index is returned. If it is zero, it means there is
710 * no more header, so any processing may stop. The ctx is always left in a form
711 * that can be handled by http_find_header2() to find next occurrence.
712 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100713int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100714{
715 int cur_idx = ctx->idx;
716 char *sol = ctx->line;
717 struct hdr_idx_elem *hdr;
718 int delta, skip_comma;
719
720 if (!cur_idx)
721 return 0;
722
723 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200724 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200726 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100727 http_msg_move_end(msg, delta);
728 idx->used--;
729 hdr->len = 0; /* unused entry */
730 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100731 if (idx->tail == ctx->idx)
732 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100733 ctx->idx = ctx->prev; /* walk back to the end of previous header */
734 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
735 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200736 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 return ctx->idx;
738 }
739
740 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200741 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
742 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100743 */
744
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200745 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200746 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100748 NULL, 0);
749 hdr->len += delta;
750 http_msg_move_end(msg, delta);
751 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200752 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100753 return ctx->idx;
754}
755
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100756/* This function handles a server error at the stream interface level. The
757 * stream interface is assumed to be already in a closed state. An optional
758 * message is copied into the input buffer, and an HTTP status code stored.
759 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100760 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200762static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100763 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200765 channel_auto_read(si->ob);
766 channel_abort(si->ob);
767 channel_auto_close(si->ob);
768 channel_erase(si->ob);
769 channel_auto_close(si->ib);
770 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100771 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200772 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200773 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200775 if (!(s->flags & SN_ERR_MASK))
776 s->flags |= err;
777 if (!(s->flags & SN_FINST_MASK))
778 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779}
780
Willy Tarreau80587432006-12-24 17:47:20 +0100781/* This function returns the appropriate error location for the given session
782 * and message.
783 */
784
Willy Tarreau783f2582012-09-04 12:19:04 +0200785struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100786{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200787 if (s->be->errmsg[msgnum].str)
788 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100789 else if (s->fe->errmsg[msgnum].str)
790 return &s->fe->errmsg[msgnum];
791 else
792 return &http_err_chunks[msgnum];
793}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794
Willy Tarreau53b6c742006-12-17 13:37:46 +0100795/*
796 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
797 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
798 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100799enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100800{
801 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100802 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100803
804 m = ((unsigned)*str - 'A');
805
806 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100807 for (h = http_methods[m]; h->len > 0; h++) {
808 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100809 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100810 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100812 };
813 return HTTP_METH_OTHER;
814 }
815 return HTTP_METH_NONE;
816
817}
818
Willy Tarreau21d2af32008-02-14 20:25:24 +0100819/* Parse the URI from the given transaction (which is assumed to be in request
820 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
821 * It is returned otherwise.
822 */
823static char *
824http_get_path(struct http_txn *txn)
825{
826 char *ptr, *end;
827
Willy Tarreau9b28e032012-10-12 23:49:43 +0200828 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100829 end = ptr + txn->req.sl.rq.u_l;
830
831 if (ptr >= end)
832 return NULL;
833
834 /* RFC2616, par. 5.1.2 :
835 * Request-URI = "*" | absuri | abspath | authority
836 */
837
838 if (*ptr == '*')
839 return NULL;
840
841 if (isalpha((unsigned char)*ptr)) {
842 /* this is a scheme as described by RFC3986, par. 3.1 */
843 ptr++;
844 while (ptr < end &&
845 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
846 ptr++;
847 /* skip '://' */
848 if (ptr == end || *ptr++ != ':')
849 return NULL;
850 if (ptr == end || *ptr++ != '/')
851 return NULL;
852 if (ptr == end || *ptr++ != '/')
853 return NULL;
854 }
855 /* skip [user[:passwd]@]host[:[port]] */
856
857 while (ptr < end && *ptr != '/')
858 ptr++;
859
860 if (ptr == end)
861 return NULL;
862
863 /* OK, we got the '/' ! */
864 return ptr;
865}
866
William Lallemand65ad6e12014-01-31 15:08:02 +0100867/* Parse the URI from the given string and look for the "/" beginning the PATH.
868 * If not found, return NULL. It is returned otherwise.
869 */
870static char *
871http_get_path_from_string(char *str)
872{
873 char *ptr = str;
874
875 /* RFC2616, par. 5.1.2 :
876 * Request-URI = "*" | absuri | abspath | authority
877 */
878
879 if (*ptr == '*')
880 return NULL;
881
882 if (isalpha((unsigned char)*ptr)) {
883 /* this is a scheme as described by RFC3986, par. 3.1 */
884 ptr++;
885 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
886 ptr++;
887 /* skip '://' */
888 if (*ptr == '\0' || *ptr++ != ':')
889 return NULL;
890 if (*ptr == '\0' || *ptr++ != '/')
891 return NULL;
892 if (*ptr == '\0' || *ptr++ != '/')
893 return NULL;
894 }
895 /* skip [user[:passwd]@]host[:[port]] */
896
897 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
898 ptr++;
899
900 if (*ptr == '\0' || *ptr == ' ')
901 return NULL;
902
903 /* OK, we got the '/' ! */
904 return ptr;
905}
906
Willy Tarreau71241ab2012-12-27 11:30:54 +0100907/* Returns a 302 for a redirectable request that reaches a server working in
908 * in redirect mode. This may only be called just after the stream interface
909 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
910 * follow normal proxy processing. NOTE: this function is designed to support
911 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100912 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100913void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100914{
915 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100916 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100917 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200918 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100919
920 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100921 trash.len = strlen(HTTP_302);
922 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100924 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100925
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100927 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 return;
929
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100930 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
933 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100934 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100935
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200936 /* 3: add the request URI. Since it was already forwarded, we need
937 * to temporarily rewind the buffer.
938 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200940 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200941
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200943 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 +0200944
Willy Tarreau9b28e032012-10-12 23:49:43 +0200945 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200946
Willy Tarreauefb453c2008-10-26 20:49:47 +0100947 if (!path)
948 return;
949
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100950 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100951 return;
952
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100953 memcpy(trash.str + trash.len, path, len);
954 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100955
956 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100957 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
958 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100959 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100960 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
961 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100962 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100963
964 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200965 si_shutr(si);
966 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100967 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100968 si->state = SI_ST_CLO;
969
970 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200971 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100972
973 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100974 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500975 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976}
977
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100978/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 * that the server side is closed. Note that err_type is actually a
980 * bitmask, where almost only aborts may be cumulated with other
981 * values. We consider that aborted operations are more important
982 * than timeouts or errors due to the fact that nobody else in the
983 * logs might explain incomplete retries. All others should avoid
984 * being cumulated. It should normally not be possible to have multiple
985 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100986 * Note that connection errors appearing on the second request of a keep-alive
987 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100991 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992
993 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100994 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200995 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100996 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100997 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100998 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
999 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001001 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001002 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001004 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001005 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001007 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001008 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1009 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001010 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001011 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001012 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001013 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001014 else if (err_type & SI_ET_CONN_RES)
1015 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001016 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1017 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001019 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001020 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021}
1022
Willy Tarreau42250582007-04-01 01:30:43 +02001023extern const char sess_term_cond[8];
1024extern const char sess_fin_state[8];
1025extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001026struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001027struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001028struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029
Willy Tarreau117f59e2007-03-04 18:17:17 +01001030/*
1031 * Capture headers from message starting at <som> according to header list
1032 * <cap_hdr>, and fill the <idx> structure appropriately.
1033 */
1034void capture_headers(char *som, struct hdr_idx *idx,
1035 char **cap, struct cap_hdr *cap_hdr)
1036{
1037 char *eol, *sol, *col, *sov;
1038 int cur_idx;
1039 struct cap_hdr *h;
1040 int len;
1041
1042 sol = som + hdr_idx_first_pos(idx);
1043 cur_idx = hdr_idx_first_idx(idx);
1044
1045 while (cur_idx) {
1046 eol = sol + idx->v[cur_idx].len;
1047
1048 col = sol;
1049 while (col < eol && *col != ':')
1050 col++;
1051
1052 sov = col + 1;
1053 while (sov < eol && http_is_lws[(unsigned char)*sov])
1054 sov++;
1055
1056 for (h = cap_hdr; h; h = h->next) {
1057 if ((h->namelen == col - sol) &&
1058 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1059 if (cap[h->index] == NULL)
1060 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001061 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001062
1063 if (cap[h->index] == NULL) {
1064 Alert("HTTP capture : out of memory.\n");
1065 continue;
1066 }
1067
1068 len = eol - sov;
1069 if (len > h->len)
1070 len = h->len;
1071
1072 memcpy(cap[h->index], sov, len);
1073 cap[h->index][len]=0;
1074 }
1075 }
1076 sol = eol + idx->v[cur_idx].cr + 1;
1077 cur_idx = idx->v[cur_idx].next;
1078 }
1079}
1080
1081
Willy Tarreau42250582007-04-01 01:30:43 +02001082/* either we find an LF at <ptr> or we jump to <bad>.
1083 */
1084#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1085
1086/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1087 * otherwise to <http_msg_ood> with <state> set to <st>.
1088 */
1089#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1090 ptr++; \
1091 if (likely(ptr < end)) \
1092 goto good; \
1093 else { \
1094 state = (st); \
1095 goto http_msg_ood; \
1096 } \
1097 } while (0)
1098
1099
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001101 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1103 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1104 * will give undefined results.
1105 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1106 * and that msg->sol points to the beginning of the response.
1107 * If a complete line is found (which implies that at least one CR or LF is
1108 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1109 * returned indicating an incomplete line (which does not mean that parts have
1110 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1111 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1112 * upon next call.
1113 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001114 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001115 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1116 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001117 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001119const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001120 enum ht_state state, const char *ptr, const char *end,
1121 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001122{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001123 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001124
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001127 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001128 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1130
1131 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001132 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1134 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001135 state = HTTP_MSG_ERROR;
1136 break;
1137
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001139 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001141 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001142 goto http_msg_rpcode;
1143 }
1144 if (likely(HTTP_IS_SPHT(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1146 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001147 state = HTTP_MSG_ERROR;
1148 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001149
Willy Tarreau8973c702007-01-21 23:58:29 +01001150 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001151 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 if (likely(!HTTP_IS_LWS(*ptr)))
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1154
1155 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001156 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001157 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1158 }
1159
1160 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001161 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001162 http_msg_rsp_reason:
1163 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001165 msg->sl.st.r_l = 0;
1166 goto http_msg_rpline_eol;
1167
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001169 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 goto http_msg_rpreason;
1173 }
1174 if (likely(HTTP_IS_SPHT(*ptr)))
1175 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1176 /* so it's a CR/LF, so there is no reason phrase */
1177 goto http_msg_rsp_reason;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_CRLF(*ptr)))
1182 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001183 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001184 http_msg_rpline_eol:
1185 /* We have seen the end of line. Note that we do not
1186 * necessarily have the \n yet, but at least we know that we
1187 * have EITHER \r OR \n, otherwise the response would not be
1188 * complete. We can then record the response length and return
1189 * to the caller which will be able to register it.
1190 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001191 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 return ptr;
1193
Willy Tarreau8973c702007-01-21 23:58:29 +01001194 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001195#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1197 exit(1);
1198#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001199 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 }
1201
1202 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001203 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (ret_state)
1205 *ret_state = state;
1206 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001207 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001208 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209}
1210
Willy Tarreau8973c702007-01-21 23:58:29 +01001211/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 * This function parses a request line between <ptr> and <end>, starting with
1213 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1214 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1215 * will give undefined results.
1216 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1217 * and that msg->sol points to the beginning of the request.
1218 * If a complete line is found (which implies that at least one CR or LF is
1219 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1220 * returned indicating an incomplete line (which does not mean that parts have
1221 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1222 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1223 * upon next call.
1224 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001225 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1227 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001228 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001230const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001231 enum ht_state state, const char *ptr, const char *end,
1232 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001234 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001238 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 if (likely(HTTP_IS_TOKEN(*ptr)))
1240 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001243 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 if (likely(HTTP_IS_CRLF(*ptr))) {
1248 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001251 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001253 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001254 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001256 msg->sl.rq.v_l = 0;
1257 goto http_msg_rqline_eol;
1258 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001259 state = HTTP_MSG_ERROR;
1260 break;
1261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001263 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001265 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 goto http_msg_rquri;
1267 }
1268 if (likely(HTTP_IS_SPHT(*ptr)))
1269 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1270 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1271 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001275 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001277
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001279 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001283 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001284 /* non-ASCII chars are forbidden unless option
1285 * accept-invalid-http-request is enabled in the frontend.
1286 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001287 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001288 if (msg->err_pos < -1)
1289 goto invalid_char;
1290 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001291 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001292 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1293 }
1294
1295 if (likely(HTTP_IS_CRLF(*ptr))) {
1296 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1297 goto http_msg_req09_uri_e;
1298 }
1299
1300 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001301 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 state = HTTP_MSG_ERROR;
1304 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001307 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001309 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 goto http_msg_rqver;
1311 }
1312 if (likely(HTTP_IS_SPHT(*ptr)))
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1314 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1315 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001321
1322 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001323 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001324 http_msg_rqline_eol:
1325 /* We have seen the end of line. Note that we do not
1326 * necessarily have the \n yet, but at least we know that we
1327 * have EITHER \r OR \n, otherwise the request would not be
1328 * complete. We can then record the request length and return
1329 * to the caller which will be able to register it.
1330 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001331 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332 return ptr;
1333 }
1334
1335 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001336 state = HTTP_MSG_ERROR;
1337 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001340#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1342 exit(1);
1343#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001344 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001348 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 if (ret_state)
1350 *ret_state = state;
1351 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001352 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001356/*
1357 * Returns the data from Authorization header. Function may be called more
1358 * than once so data is stored in txn->auth_data. When no header is found
1359 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001360 * searching again for something we are unable to find anyway. However, if
1361 * the result if valid, the cache is not reused because we would risk to
1362 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001363 */
1364
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001365/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1366 * set according to global.tune.bufsize.
1367 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001368char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001369
1370int
1371get_http_auth(struct session *s)
1372{
1373
1374 struct http_txn *txn = &s->txn;
1375 struct chunk auth_method;
1376 struct hdr_ctx ctx;
1377 char *h, *p;
1378 int len;
1379
1380#ifdef DEBUG_AUTH
1381 printf("Auth for session %p: %d\n", s, txn->auth.method);
1382#endif
1383
1384 if (txn->auth.method == HTTP_AUTH_WRONG)
1385 return 0;
1386
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001387 txn->auth.method = HTTP_AUTH_WRONG;
1388
1389 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001390
1391 if (txn->flags & TX_USE_PX_CONN) {
1392 h = "Proxy-Authorization";
1393 len = strlen(h);
1394 } else {
1395 h = "Authorization";
1396 len = strlen(h);
1397 }
1398
Willy Tarreau9b28e032012-10-12 23:49:43 +02001399 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001400 return 0;
1401
1402 h = ctx.line + ctx.val;
1403
1404 p = memchr(h, ' ', ctx.vlen);
1405 if (!p || p == h)
1406 return 0;
1407
1408 chunk_initlen(&auth_method, h, 0, p-h);
1409 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1410
1411 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1412
1413 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001414 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001415
1416 if (len < 0)
1417 return 0;
1418
1419
1420 get_http_auth_buff[len] = '\0';
1421
1422 p = strchr(get_http_auth_buff, ':');
1423
1424 if (!p)
1425 return 0;
1426
1427 txn->auth.user = get_http_auth_buff;
1428 *p = '\0';
1429 txn->auth.pass = p+1;
1430
1431 txn->auth.method = HTTP_AUTH_BASIC;
1432 return 1;
1433 }
1434
1435 return 0;
1436}
1437
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8973c702007-01-21 23:58:29 +01001439/*
1440 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001441 * depending on the initial msg->msg_state. The caller is responsible for
1442 * ensuring that the message does not wrap. The function can be preempted
1443 * everywhere when data are missing and recalled at the exact same location
1444 * with no information loss. The message may even be realigned between two
1445 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001446 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001447 * fields. Note that msg->sol will be initialized after completing the first
1448 * state, so that none of the msg pointers has to be initialized prior to the
1449 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001451void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452{
Willy Tarreau3770f232013-12-07 00:01:53 +01001453 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001455 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001457 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001458 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001459 ptr = buf->p + msg->next;
1460 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (unlikely(ptr >= end))
1463 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 /*
1467 * First, states that are specific to the response only.
1468 * We check them first so that request and headers are
1469 * closer to each other (accessed more often).
1470 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001472 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001474 /* we have a start of message, but we have to check
1475 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001476 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001477 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001478 if (unlikely(ptr != buf->p)) {
1479 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001480 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001481 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001482 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001483 }
Willy Tarreau26927362012-05-18 23:22:52 +02001484 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001485 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 hdr_idx_init(idx);
1487 state = HTTP_MSG_RPVER;
1488 goto http_msg_rpver;
1489 }
1490
1491 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1492 goto http_msg_invalid;
1493
1494 if (unlikely(*ptr == '\n'))
1495 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1497 /* stop here */
1498
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001500 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 EXPECT_LF_HERE(ptr, http_msg_invalid);
1502 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1503 /* stop here */
1504
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001506 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 case HTTP_MSG_RPVER_SP:
1508 case HTTP_MSG_RPCODE:
1509 case HTTP_MSG_RPCODE_SP:
1510 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001511 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001512 state, ptr, end,
1513 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 if (unlikely(!ptr))
1515 return;
1516
1517 /* we have a full response and we know that we have either a CR
1518 * or an LF at <ptr>.
1519 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1521
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001522 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 if (likely(*ptr == '\r'))
1524 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1525 goto http_msg_rpline_end;
1526
Willy Tarreau8973c702007-01-21 23:58:29 +01001527 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001528 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 /* msg->sol must point to the first of CR or LF. */
1530 EXPECT_LF_HERE(ptr, http_msg_invalid);
1531 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1532 /* stop here */
1533
1534 /*
1535 * Second, states that are specific to the request only
1536 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001538 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001540 /* we have a start of message, but we have to check
1541 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001542 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001543 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 if (likely(ptr != buf->p)) {
1545 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001546 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001547 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 }
Willy Tarreau26927362012-05-18 23:22:52 +02001550 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001551 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001552 state = HTTP_MSG_RQMETH;
1553 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1557 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 if (unlikely(*ptr == '\n'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001562 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001565 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 EXPECT_LF_HERE(ptr, http_msg_invalid);
1567 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001568 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 case HTTP_MSG_RQMETH_SP:
1573 case HTTP_MSG_RQURI:
1574 case HTTP_MSG_RQURI_SP:
1575 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001576 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001577 state, ptr, end,
1578 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 if (unlikely(!ptr))
1580 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 /* we have a full request and we know that we have either a CR
1583 * or an LF at <ptr>.
1584 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001586
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001587 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 if (likely(*ptr == '\r'))
1589 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 /* check for HTTP/0.9 request : no version information available.
1595 * msg->sol must point to the first of CR or LF.
1596 */
1597 if (unlikely(msg->sl.rq.v_l == 0))
1598 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 EXPECT_LF_HERE(ptr, http_msg_invalid);
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 /*
1605 * Common states below
1606 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001608 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001609 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 if (likely(!HTTP_IS_CRLF(*ptr))) {
1611 goto http_msg_hdr_name;
1612 }
1613
1614 if (likely(*ptr == '\r'))
1615 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1616 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 /* assumes msg->sol points to the first char */
1621 if (likely(HTTP_IS_TOKEN(*ptr)))
1622 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001624 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001627 if (likely(msg->err_pos < -1) || *ptr == '\n')
1628 goto http_msg_invalid;
1629
1630 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001631 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001632
1633 /* and we still accept this non-token character */
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001637 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001638 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 if (likely(HTTP_IS_SPHT(*ptr)))
1640 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 if (likely(!HTTP_IS_CRLF(*ptr))) {
1646 goto http_msg_hdr_val;
1647 }
1648
1649 if (likely(*ptr == '\r'))
1650 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1651 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001654 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 EXPECT_LF_HERE(ptr, http_msg_invalid);
1656 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001659 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 if (likely(HTTP_IS_SPHT(*ptr))) {
1661 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 for (; buf->p + msg->sov < ptr; msg->sov++)
1663 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 goto http_msg_hdr_l1_sp;
1665 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001666 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001667 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 goto http_msg_complete_header;
1669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001671 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001672 /* assumes msg->sol points to the first char, and msg->sov
1673 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 */
1675 if (likely(!HTTP_IS_CRLF(*ptr)))
1676 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001677
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001678 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* Note: we could also copy eol into ->eoh so that we have the
1680 * real header end in case it ends with lots of LWS, but is this
1681 * really needed ?
1682 */
1683 if (likely(*ptr == '\r'))
1684 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1685 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001688 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EXPECT_LF_HERE(ptr, http_msg_invalid);
1690 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001693 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1695 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001696 for (; buf->p + msg->eol < ptr; msg->eol++)
1697 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 goto http_msg_hdr_val;
1699 }
1700 http_msg_complete_header:
1701 /*
1702 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001703 * Assumes msg->sol points to the first char, msg->sov points
1704 * to the first character of the value and msg->eol to the
1705 * first CR or LF so we know how the line ends. We insert last
1706 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001708 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 idx, idx->tail) < 0))
1710 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001711
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001712 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 if (likely(!HTTP_IS_CRLF(*ptr))) {
1714 goto http_msg_hdr_name;
1715 }
1716
1717 if (likely(*ptr == '\r'))
1718 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1719 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001723 /* Assumes msg->sol points to the first of either CR or LF.
1724 * Sets ->sov and ->next to the total header length, ->eoh to
1725 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1726 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 EXPECT_LF_HERE(ptr, http_msg_invalid);
1728 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001729 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001730 msg->eoh = msg->sol;
1731 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001732 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001733 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001735
1736 case HTTP_MSG_ERROR:
1737 /* this may only happen if we call http_msg_analyser() twice with an error */
1738 break;
1739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001741#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1743 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001744#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001745 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 }
1747 http_msg_ood:
1748 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001749 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001750 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 http_msg_invalid:
1754 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001755 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001756 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 return;
1758}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001759
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001760/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1761 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1762 * nothing is done and 1 is returned.
1763 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001764static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001765{
1766 int delta;
1767 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001768 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001769
1770 if (msg->sl.rq.v_l != 0)
1771 return 1;
1772
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001773 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1774 if (txn->meth != HTTP_METH_GET)
1775 return 0;
1776
Willy Tarreau9b28e032012-10-12 23:49:43 +02001777 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001778 delta = 0;
1779
1780 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001781 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1782 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001783 }
1784 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001785 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001786 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001787 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001788 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001790 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001791 NULL, NULL);
1792 if (unlikely(!cur_end))
1793 return 0;
1794
1795 /* we have a full HTTP/1.0 request now and we know that
1796 * we have either a CR or an LF at <ptr>.
1797 */
1798 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1799 return 1;
1800}
1801
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001802/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001803 * and "keep-alive" values. If we already know that some headers may safely
1804 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001805 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1806 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001807 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001808 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1809 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1810 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001811 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001812 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001813void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001814{
Willy Tarreau5b154472009-12-21 20:11:07 +01001815 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001816 const char *hdr_val = "Connection";
1817 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001818
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001819 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 return;
1821
Willy Tarreau88d349d2010-01-25 12:15:43 +01001822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1823 hdr_val = "Proxy-Connection";
1824 hdr_len = 16;
1825 }
1826
Willy Tarreau5b154472009-12-21 20:11:07 +01001827 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001829 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001830 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1831 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001832 if (to_del & 2)
1833 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001834 else
1835 txn->flags |= TX_CON_KAL_SET;
1836 }
1837 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1838 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001839 if (to_del & 1)
1840 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 else
1842 txn->flags |= TX_CON_CLO_SET;
1843 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001844 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1845 txn->flags |= TX_HDR_CONN_UPG;
1846 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001847 }
1848
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849 txn->flags |= TX_HDR_CONN_PRS;
1850 return;
1851}
Willy Tarreau5b154472009-12-21 20:11:07 +01001852
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001853/* Apply desired changes on the Connection: header. Values may be removed and/or
1854 * added depending on the <wanted> flags, which are exclusively composed of
1855 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1856 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1857 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001858void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859{
1860 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001861 const char *hdr_val = "Connection";
1862 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001863
1864 ctx.idx = 0;
1865
Willy Tarreau88d349d2010-01-25 12:15:43 +01001866
1867 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1868 hdr_val = "Proxy-Connection";
1869 hdr_len = 16;
1870 }
1871
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001873 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1875 if (wanted & TX_CON_KAL_SET)
1876 txn->flags |= TX_CON_KAL_SET;
1877 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001878 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001880 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1881 if (wanted & TX_CON_CLO_SET)
1882 txn->flags |= TX_CON_CLO_SET;
1883 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001884 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001885 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887
1888 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1889 return;
1890
1891 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1892 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001893 hdr_val = "Connection: close";
1894 hdr_len = 17;
1895 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1896 hdr_val = "Proxy-Connection: close";
1897 hdr_len = 23;
1898 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001899 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001900 }
1901
1902 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1903 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001904 hdr_val = "Connection: keep-alive";
1905 hdr_len = 22;
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection: keep-alive";
1908 hdr_len = 28;
1909 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001910 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 }
1912 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001913}
1914
Willy Tarreauc24715e2014-04-17 15:21:20 +02001915/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1916 * the first byte of data after the chunk size, so that we know we can forward
1917 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1918 * the chunk size. That way it is always possible to differentiate between the
1919 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001922 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001923static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001924{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001925 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 const char *end = buf->data + buf->size;
1929 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001930 unsigned int chunk = 0;
1931
1932 /* The chunk size is in the following form, though we are only
1933 * interested in the size and CRLF :
1934 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1935 */
1936 while (1) {
1937 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001938 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001941 if (c < 0) /* not a hex digit anymore */
1942 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001943 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001944 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001945 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001946 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001947 chunk = (chunk << 4) + c;
1948 }
1949
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001951 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953
1954 while (http_is_spht[(unsigned char)*ptr]) {
1955 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001957 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001958 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 }
1960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 /* Up to there, we know that at least one byte is present at *ptr. Check
1962 * for the end of chunk size.
1963 */
1964 while (1) {
1965 if (likely(HTTP_IS_CRLF(*ptr))) {
1966 /* we now have a CR or an LF at ptr */
1967 if (likely(*ptr == '\r')) {
1968 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001969 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001970 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001971 return 0;
1972 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001973
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001975 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001977 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001978 /* done */
1979 break;
1980 }
1981 else if (*ptr == ';') {
1982 /* chunk extension, ends at next CRLF */
1983 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001985 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987
1988 while (!HTTP_IS_CRLF(*ptr)) {
1989 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001991 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 return 0;
1993 }
1994 /* we have a CRLF now, loop above */
1995 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001996 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001998 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 }
2000
Willy Tarreaud98cf932009-12-27 22:54:55 +01002001 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002002 * which may or may not be present. We save that into ->next,
2003 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002005 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002006 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002007 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002009 msg->chunk_len = chunk;
2010 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002012 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002013 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002014 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002015 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002016}
2017
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002018/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002019 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 * the trailers is found, it is automatically scheduled to be forwarded,
2021 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2022 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002023 * except maybe msg->next if it could parse some lines, and returns zero.
2024 * If a parse error is encountered, the function returns < 0 and does not
2025 * change anything except maybe msg->next. Note that the message must
2026 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002027 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002028 * forwarding, and that msg->next exactly matches the length of trailers
2029 * already parsed and not forwarded. It is also important to note that this
2030 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002032static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002034 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002035
Willy Tarreaua458b672012-03-05 11:17:50 +01002036 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002038 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 const char *ptr = b_ptr(buf, msg->next);
2040 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002041 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042
2043 /* scan current line and stop at LF or CRLF */
2044 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002045 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046 return 0;
2047
2048 if (*ptr == '\n') {
2049 if (!p1)
2050 p1 = ptr;
2051 p2 = ptr;
2052 break;
2053 }
2054
2055 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002056 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002057 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 p1 = ptr;
2061 }
2062
2063 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 if (ptr >= buf->data + buf->size)
2065 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 }
2067
2068 /* after LF; point to beginning of next line */
2069 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002070 if (p2 >= buf->data + buf->size)
2071 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002073 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002074 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002076
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002077 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002078 /* LF/CRLF at beginning of line => end of trailers at p2.
2079 * Everything was scheduled for forwarding, there's nothing
2080 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002081 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002082 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002083 msg->msg_state = HTTP_MSG_DONE;
2084 return 1;
2085 }
2086 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002087 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002088 }
2089}
2090
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2092 * or a possible LF alone at the end of a chunk. It automatically adjusts
2093 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002094 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2096 * not enough data are available, the function does not change anything and
2097 * returns zero. If a parse error is encountered, the function returns < 0 and
2098 * does not change anything. Note: this function is designed to parse wrapped
2099 * CRLF at the end of the buffer.
2100 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002101static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002103 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002104 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 int bytes;
2106
2107 /* NB: we'll check data availabilty at the end. It's not a
2108 * problem because whatever we match first will be checked
2109 * against the correct length.
2110 */
2111 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002112 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002113 if (*ptr == '\r') {
2114 bytes++;
2115 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002116 if (ptr >= buf->data + buf->size)
2117 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 }
2119
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002120 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 return 0;
2122
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002124 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002126 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127
2128 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002129 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002130 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002131 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002132 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2134 return 1;
2135}
Willy Tarreau5b154472009-12-21 20:11:07 +01002136
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002137/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2138 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2139 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2140 * Unparsable qvalues return 1000 as "q=1.000".
2141 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002142int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002143{
2144 int q = 1000;
2145
2146 if (!isdigit(*qvalue))
2147 goto out;
2148 q = (*qvalue++ - '0') * 1000;
2149
2150 if (*qvalue++ != '.')
2151 goto out;
2152
2153 if (!isdigit(*qvalue))
2154 goto out;
2155 q += (*qvalue++ - '0') * 100;
2156
2157 if (!isdigit(*qvalue))
2158 goto out;
2159 q += (*qvalue++ - '0') * 10;
2160
2161 if (!isdigit(*qvalue))
2162 goto out;
2163 q += (*qvalue++ - '0') * 1;
2164 out:
2165 if (q > 1000)
2166 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002167 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002168 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002169 return q;
2170}
William Lallemand82fe75c2012-10-23 10:25:10 +02002171
2172/*
2173 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002174 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002175int select_compression_request_header(struct session *s, struct buffer *req)
2176{
2177 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002178 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002179 struct hdr_ctx ctx;
2180 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002181 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002182
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002183 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2184 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002185 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2186 */
2187 ctx.idx = 0;
2188 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2189 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002190 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2191 (ctx.vlen < 31 ||
2192 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2193 ctx.line[ctx.val + 30] < '6' ||
2194 (ctx.line[ctx.val + 30] == '6' &&
2195 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2196 s->comp_algo = NULL;
2197 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002198 }
2199
William Lallemand82fe75c2012-10-23 10:25:10 +02002200 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002201 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002202 int best_q = 0;
2203
William Lallemand82fe75c2012-10-23 10:25:10 +02002204 ctx.idx = 0;
2205 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002206 const char *qval;
2207 int q;
2208 int toklen;
2209
2210 /* try to isolate the token from the optional q-value */
2211 toklen = 0;
2212 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2213 toklen++;
2214
2215 qval = ctx.line + ctx.val + toklen;
2216 while (1) {
2217 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2218 qval++;
2219
2220 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2221 qval = NULL;
2222 break;
2223 }
2224 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002225
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2227 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002228
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002229 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2230 qval = NULL;
2231 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002232 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2234 break;
2235
2236 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2237 qval++;
2238 }
2239
2240 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002241 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242
2243 if (q <= best_q)
2244 continue;
2245
2246 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2247 if (*(ctx.line + ctx.val) == '*' ||
2248 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2249 s->comp_algo = comp_algo;
2250 best_q = q;
2251 break;
2252 }
2253 }
2254 }
2255 }
2256
2257 /* remove all occurrences of the header when "compression offload" is set */
2258 if (s->comp_algo) {
2259 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2260 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2261 ctx.idx = 0;
2262 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2263 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 }
2265 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002267 }
2268
2269 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002270 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2271 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002272 if (comp_algo->add_data == identity_add_data) {
2273 s->comp_algo = comp_algo;
2274 return 1;
2275 }
2276 }
2277 }
2278
2279 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002280 return 0;
2281}
2282
2283/*
2284 * Selects a comression algorithm depending of the server response.
2285 */
2286int select_compression_response_header(struct session *s, struct buffer *res)
2287{
2288 struct http_txn *txn = &s->txn;
2289 struct http_msg *msg = &txn->rsp;
2290 struct hdr_ctx ctx;
2291 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002292
2293 /* no common compression algorithm was found in request header */
2294 if (s->comp_algo == NULL)
2295 goto fail;
2296
2297 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002298 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002299 goto fail;
2300
William Lallemandd3002612012-11-26 14:34:47 +01002301 /* 200 only */
2302 if (txn->status != 200)
2303 goto fail;
2304
William Lallemand82fe75c2012-10-23 10:25:10 +02002305 /* Content-Length is null */
2306 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2307 goto fail;
2308
2309 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002310 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002311 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2312 goto fail;
2313
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002314 /* no compression when Cache-Control: no-transform is present in the message */
2315 ctx.idx = 0;
2316 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2317 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2318 goto fail;
2319 }
2320
William Lallemand82fe75c2012-10-23 10:25:10 +02002321 comp_type = NULL;
2322
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002323 /* we don't want to compress multipart content-types, nor content-types that are
2324 * not listed in the "compression type" directive if any. If no content-type was
2325 * found but configuration requires one, we don't compress either. Backend has
2326 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002327 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002328 ctx.idx = 0;
2329 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2330 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2331 goto fail;
2332
2333 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2334 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002335 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002336 if (ctx.vlen >= comp_type->name_len &&
2337 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002338 /* this Content-Type should be compressed */
2339 break;
2340 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002341 /* this Content-Type should not be compressed */
2342 if (comp_type == NULL)
2343 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002346 else { /* no content-type header */
2347 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2348 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002349 }
2350
William Lallemandd85f9172012-11-09 17:05:39 +01002351 /* limit compression rate */
2352 if (global.comp_rate_lim > 0)
2353 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2354 goto fail;
2355
William Lallemand072a2bf2012-11-20 17:01:01 +01002356 /* limit cpu usage */
2357 if (idle_pct < compress_min_idle)
2358 goto fail;
2359
William Lallemand4c49fae2012-11-07 15:00:23 +01002360 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002361 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002362 goto fail;
2363
William Lallemandec3e3892012-11-12 17:02:18 +01002364 s->flags |= SN_COMP_READY;
2365
William Lallemand82fe75c2012-10-23 10:25:10 +02002366 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002367 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002368 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2369 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2370
2371 /* add Transfer-Encoding header */
2372 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2373 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2374
2375 /*
2376 * Add Content-Encoding header when it's not identity encoding.
2377 * RFC 2616 : Identity encoding: This content-coding is used only in the
2378 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2379 * header.
2380 */
2381 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002382 trash.len = 18;
2383 memcpy(trash.str, "Content-Encoding: ", trash.len);
2384 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2385 trash.len += s->comp_algo->name_len;
2386 trash.str[trash.len] = '\0';
2387 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002388 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002389 return 1;
2390
2391fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002392 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002393 return 0;
2394}
2395
2396
Willy Tarreaud787e662009-07-07 10:14:51 +02002397/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2398 * processing can continue on next analysers, or zero if it either needs more
2399 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2400 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2401 * when it has nothing left to do, and may remove any analyser when it wants to
2402 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002403 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002404int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002405{
Willy Tarreau59234e92008-11-30 23:51:27 +01002406 /*
2407 * We will parse the partial (or complete) lines.
2408 * We will check the request syntax, and also join multi-line
2409 * headers. An index of all the lines will be elaborated while
2410 * parsing.
2411 *
2412 * For the parsing, we use a 28 states FSM.
2413 *
2414 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002415 * req->buf->p = beginning of request
2416 * req->buf->p + msg->eoh = end of processed headers / start of current one
2417 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002418 * msg->eol = end of current header or line (LF or CRLF)
2419 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002420 *
2421 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002422 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002423 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2424 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002426
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002428 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002429 struct http_txn *txn = &s->txn;
2430 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002431 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002432
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002433 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002434 now_ms, __FUNCTION__,
2435 s,
2436 req,
2437 req->rex, req->wex,
2438 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002439 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002440 req->analysers);
2441
Willy Tarreau52a0c602009-08-16 22:45:38 +02002442 /* we're speaking HTTP here, so let's speak HTTP to the client */
2443 s->srv_error = http_return_srv_error;
2444
Willy Tarreau83e3af02009-12-28 17:39:57 +01002445 /* There's a protected area at the end of the buffer for rewriting
2446 * purposes. We don't want to start to parse the request if the
2447 * protected area is affected, because we may have to move processed
2448 * data later, which is much more complicated.
2449 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002450 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002451 if (txn->flags & TX_NOT_FIRST) {
2452 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002453 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002454 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002455 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002456 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002457 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002458 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002459 return 0;
2460 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002461 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2462 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2463 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002464 }
2465
Willy Tarreau065e8332010-01-08 00:30:20 +01002466 /* Note that we have the same problem with the response ; we
2467 * may want to send a redirect, error or anything which requires
2468 * some spare space. So we'll ensure that we have at least
2469 * maxrewrite bytes available in the response buffer before
2470 * processing that one. This will only affect pipelined
2471 * keep-alive requests.
2472 */
2473 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002474 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002475 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2476 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2477 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002478 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002479 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002480 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002481 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002482 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002483 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002484 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002485 return 0;
2486 }
2487 }
2488
Willy Tarreau9b28e032012-10-12 23:49:43 +02002489 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002490 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002491 }
2492
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 /* 1: we might have to print this header in debug mode */
2494 if (unlikely((global.mode & MODE_DEBUG) &&
2495 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002496 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002498
Willy Tarreau9b28e032012-10-12 23:49:43 +02002499 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002500 /* this is a bit complex : in case of error on the request line,
2501 * we know that rq.l is still zero, so we display only the part
2502 * up to the end of the line (truncated by debug_hdr).
2503 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002504 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002506
Willy Tarreau59234e92008-11-30 23:51:27 +01002507 sol += hdr_idx_first_pos(&txn->hdr_idx);
2508 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002509
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 while (cur_idx) {
2511 eol = sol + txn->hdr_idx.v[cur_idx].len;
2512 debug_hdr("clihdr", s, sol, eol);
2513 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2514 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002515 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 }
2517
Willy Tarreau58f10d72006-12-04 02:26:12 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /*
2520 * Now we quickly check if we have found a full valid request.
2521 * If not so, we check the FD and buffer states before leaving.
2522 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002523 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002524 * requests are checked first. When waiting for a second request
2525 * on a keep-alive session, if we encounter and error, close, t/o,
2526 * we note the error in the session flags but don't set any state.
2527 * Since the error will be noted there, it will not be counted by
2528 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002529 * Last, we may increase some tracked counters' http request errors on
2530 * the cases that are deliberately the client's fault. For instance,
2531 * a timeout or connection reset is not counted as an error. However
2532 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002533 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002534
Willy Tarreau655dce92009-11-08 13:10:58 +01002535 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002536 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002538 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002539 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002540 session_inc_http_req_ctr(s);
2541 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002542 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002543 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002544 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002545
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 /* 1: Since we are in header mode, if there's no space
2547 * left for headers, we won't be able to free more
2548 * later, so the session will never terminate. We
2549 * must terminate it now.
2550 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002551 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 /* FIXME: check if URI is set and return Status
2553 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002554 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002555 session_inc_http_req_ctr(s);
2556 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002557 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002558 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002559 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002560 goto return_bad_req;
2561 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002564 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002565 if (!(s->flags & SN_ERR_MASK))
2566 s->flags |= SN_ERR_CLICL;
2567
Willy Tarreaufcffa692010-01-10 14:21:19 +01002568 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002569 goto failed_keep_alive;
2570
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002572 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002573 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002574 session_inc_http_err_ctr(s);
2575 }
2576
Willy Tarreaudc979f22012-12-04 10:39:01 +01002577 txn->status = 400;
2578 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 msg->msg_state = HTTP_MSG_ERROR;
2580 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002581
Willy Tarreauda7ff642010-06-23 11:44:09 +02002582 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002583 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002584 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002585 if (s->listener->counters)
2586 s->listener->counters->failed_req++;
2587
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 if (!(s->flags & SN_FINST_MASK))
2589 s->flags |= SN_FINST_R;
2590 return 0;
2591 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002594 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002595 if (!(s->flags & SN_ERR_MASK))
2596 s->flags |= SN_ERR_CLITO;
2597
Willy Tarreaufcffa692010-01-10 14:21:19 +01002598 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002599 goto failed_keep_alive;
2600
Willy Tarreau59234e92008-11-30 23:51:27 +01002601 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002602 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002603 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002604 session_inc_http_err_ctr(s);
2605 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002607 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 msg->msg_state = HTTP_MSG_ERROR;
2609 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002610
Willy Tarreauda7ff642010-06-23 11:44:09 +02002611 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002612 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002613 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002614 if (s->listener->counters)
2615 s->listener->counters->failed_req++;
2616
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 if (!(s->flags & SN_FINST_MASK))
2618 s->flags |= SN_FINST_R;
2619 return 0;
2620 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002621
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002623 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002624 if (!(s->flags & SN_ERR_MASK))
2625 s->flags |= SN_ERR_CLICL;
2626
Willy Tarreaufcffa692010-01-10 14:21:19 +01002627 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002628 goto failed_keep_alive;
2629
Willy Tarreau4076a152009-04-02 15:18:36 +02002630 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002631 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002633 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 msg->msg_state = HTTP_MSG_ERROR;
2635 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002636
Willy Tarreauda7ff642010-06-23 11:44:09 +02002637 session_inc_http_err_ctr(s);
2638 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002639 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002640 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002641 if (s->listener->counters)
2642 s->listener->counters->failed_req++;
2643
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 if (!(s->flags & SN_FINST_MASK))
2645 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002646 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002647 }
2648
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002649 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002650 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2651 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002652#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002653 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002654 /* We need more data, we have to re-enable quick-ack in case we
2655 * previously disabled it, otherwise we might cause the client
2656 * to delay next data.
2657 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002658 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002659 }
2660#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002661
Willy Tarreaufcffa692010-01-10 14:21:19 +01002662 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2663 /* If the client starts to talk, let's fall back to
2664 * request timeout processing.
2665 */
2666 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002667 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002668 }
2669
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002671 if (!tick_isset(req->analyse_exp)) {
2672 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2673 (txn->flags & TX_WAIT_NEXT_RQ) &&
2674 tick_isset(s->be->timeout.httpka))
2675 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2676 else
2677 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* we're not ready yet */
2681 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002682
2683 failed_keep_alive:
2684 /* Here we process low-level errors for keep-alive requests. In
2685 * short, if the request is not the first one and it experiences
2686 * a timeout, read error or shutdown, we just silently close so
2687 * that the client can try again.
2688 */
2689 txn->status = 0;
2690 msg->msg_state = HTTP_MSG_RQBEFORE;
2691 req->analysers = 0;
2692 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002693 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002694 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002695 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002696 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002698
Willy Tarreaud787e662009-07-07 10:14:51 +02002699 /* OK now we have a complete HTTP request with indexed headers. Let's
2700 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002701 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002702 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002703 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002704 * byte after the last LF. msg->sov points to the first byte of data.
2705 * msg->eol cannot be trusted because it may have been left uninitialized
2706 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002707 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002708
Willy Tarreauda7ff642010-06-23 11:44:09 +02002709 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002710 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2711
Willy Tarreaub16a5742010-01-10 14:46:16 +01002712 if (txn->flags & TX_WAIT_NEXT_RQ) {
2713 /* kill the pending keep-alive timeout */
2714 txn->flags &= ~TX_WAIT_NEXT_RQ;
2715 req->analyse_exp = TICK_ETERNITY;
2716 }
2717
2718
Willy Tarreaud787e662009-07-07 10:14:51 +02002719 /* Maybe we found in invalid header name while we were configured not
2720 * to block on that, so we have to capture it now.
2721 */
2722 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002723 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002724
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 /*
2726 * 1: identify the method
2727 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002728 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002729
2730 /* we can make use of server redirect on GET and HEAD */
2731 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2732 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 /*
2735 * 2: check if the URI matches the monitor_uri.
2736 * We have to do this for every request which gets in, because
2737 * the monitor-uri is defined by the frontend.
2738 */
2739 if (unlikely((s->fe->monitor_uri_len != 0) &&
2740 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002741 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 s->fe->monitor_uri,
2743 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002744 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002746 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002748
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002750 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002751
Willy Tarreau59234e92008-11-30 23:51:27 +01002752 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002753 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002754 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002755
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 ret = acl_pass(ret);
2757 if (cond->pol == ACL_COND_UNLESS)
2758 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002759
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 if (ret) {
2761 /* we fail this request, let's return 503 service unavail */
2762 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002763 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002764 if (!(s->flags & SN_ERR_MASK))
2765 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002766 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002767 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002768 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 /* nothing to fail, let's reply normaly */
2771 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002772 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002773 if (!(s->flags & SN_ERR_MASK))
2774 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 goto return_prx_cond;
2776 }
2777
2778 /*
2779 * 3: Maybe we have to copy the original REQURI for the logs ?
2780 * Note: we cannot log anymore if the request has been
2781 * classified as invalid.
2782 */
2783 if (unlikely(s->logs.logwait & LW_REQ)) {
2784 /* we have a complete HTTP request that we must log */
2785 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2786 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002787
Willy Tarreau59234e92008-11-30 23:51:27 +01002788 if (urilen >= REQURI_LEN)
2789 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002790 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002791 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002792
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002793 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 s->do_log(s);
2795 } else {
2796 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002797 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 }
Willy Tarreau06619262006-12-17 08:37:22 +01002799
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002801 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002802 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002803
Willy Tarreau5b154472009-12-21 20:11:07 +01002804 /* ... and check if the request is HTTP/1.1 or above */
2805 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002806 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2807 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2808 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002809 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002810
2811 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002812 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 +01002813
Willy Tarreau88d349d2010-01-25 12:15:43 +01002814 /* if the frontend has "option http-use-proxy-header", we'll check if
2815 * we have what looks like a proxied connection instead of a connection,
2816 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2817 * Note that this is *not* RFC-compliant, however browsers and proxies
2818 * happen to do that despite being non-standard :-(
2819 * We consider that a request not beginning with either '/' or '*' is
2820 * a proxied connection, which covers both "scheme://location" and
2821 * CONNECT ip:port.
2822 */
2823 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002824 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002825 txn->flags |= TX_USE_PX_CONN;
2826
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002827 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002828 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002829
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002831 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002832 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002833 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002834
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002835 /* 6: determine the transfer-length.
2836 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2837 * the presence of a message-body in a REQUEST and its transfer length
2838 * must be determined that way (in order of precedence) :
2839 * 1. The presence of a message-body in a request is signaled by the
2840 * inclusion of a Content-Length or Transfer-Encoding header field
2841 * in the request's header fields. When a request message contains
2842 * both a message-body of non-zero length and a method that does
2843 * not define any semantics for that request message-body, then an
2844 * origin server SHOULD either ignore the message-body or respond
2845 * with an appropriate error message (e.g., 413). A proxy or
2846 * gateway, when presented the same request, SHOULD either forward
2847 * the request inbound with the message- body or ignore the
2848 * message-body when determining a response.
2849 *
2850 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2851 * and the "chunked" transfer-coding (Section 6.2) is used, the
2852 * transfer-length is defined by the use of this transfer-coding.
2853 * If a Transfer-Encoding header field is present and the "chunked"
2854 * transfer-coding is not present, the transfer-length is defined
2855 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002856 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002857 * 3. If a Content-Length header field is present, its decimal value in
2858 * OCTETs represents both the entity-length and the transfer-length.
2859 * If a message is received with both a Transfer-Encoding header
2860 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002861 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002862 * 4. By the server closing the connection. (Closing the connection
2863 * cannot be used to indicate the end of a request body, since that
2864 * would leave no possibility for the server to send back a response.)
2865 *
2866 * Whenever a transfer-coding is applied to a message-body, the set of
2867 * transfer-codings MUST include "chunked", unless the message indicates
2868 * it is terminated by closing the connection. When the "chunked"
2869 * transfer-coding is used, it MUST be the last transfer-coding applied
2870 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002871 */
2872
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002873 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002874 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002875 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002876 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002877 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002878 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002879 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2880 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002881 /* bad transfer-encoding (chunked followed by something else) */
2882 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002883 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002884 break;
2885 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002886 }
2887
Willy Tarreau32b47f42009-10-18 20:55:02 +02002888 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002889 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002890 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002891 signed long long cl;
2892
Willy Tarreauad14f752011-09-02 20:33:27 +02002893 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002894 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002895 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002896 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002897
Willy Tarreauad14f752011-09-02 20:33:27 +02002898 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002899 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002900 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002901 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002902
Willy Tarreauad14f752011-09-02 20:33:27 +02002903 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002904 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002905 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002906 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002907
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002908 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002909 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002910 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002911 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002912
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002913 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002914 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002915 }
2916
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002917 /* bodyless requests have a known length */
2918 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002919 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002920
Willy Tarreau179085c2014-04-28 16:48:56 +02002921 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2922 * only change if both the request and the config reference something else.
2923 * Option httpclose by itself sets tunnel mode where headers are mangled.
2924 * However, if another mode is set, it will affect it (eg: server-close/
2925 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2926 * if FE and BE have the same settings (common). The method consists in
2927 * checking if options changed between the two calls (implying that either
2928 * one is non-null, or one of them is non-null and we are there for the first
2929 * time.
2930 */
2931 if (!(txn->flags & TX_HDR_CONN_PRS) ||
2932 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
2933 int tmp = TX_CON_WANT_KAL;
2934
2935 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2936 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2937 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2938 tmp = TX_CON_WANT_TUN;
2939
2940 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2941 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2942 tmp = TX_CON_WANT_TUN;
2943 }
2944
2945 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2946 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2947 /* option httpclose + server_close => forceclose */
2948 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2949 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2950 tmp = TX_CON_WANT_CLO;
2951 else
2952 tmp = TX_CON_WANT_SCL;
2953 }
2954
2955 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2956 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2957 tmp = TX_CON_WANT_CLO;
2958
2959 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2960 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2961
2962 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2963 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2964 /* parse the Connection header and possibly clean it */
2965 int to_del = 0;
2966 if ((msg->flags & HTTP_MSGF_VER_11) ||
2967 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2968 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2969 to_del |= 2; /* remove "keep-alive" */
2970 if (!(msg->flags & HTTP_MSGF_VER_11))
2971 to_del |= 1; /* remove "close" */
2972 http_parse_connection_header(txn, msg, to_del);
2973 }
2974
2975 /* check if client or config asks for explicit close in KAL/SCL */
2976 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2977 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2978 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2979 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2980 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2981 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2982 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2983 }
2984
Willy Tarreaud787e662009-07-07 10:14:51 +02002985 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002986 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002987 req->analyse_exp = TICK_ETERNITY;
2988 return 1;
2989
2990 return_bad_req:
2991 /* We centralize bad requests processing here */
2992 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2993 /* we detected a parsing error. We want to archive this request
2994 * in the dedicated proxy area for later troubleshooting.
2995 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002996 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002997 }
2998
2999 txn->req.msg_state = HTTP_MSG_ERROR;
3000 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003001 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003002
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003003 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003004 if (s->listener->counters)
3005 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003006
3007 return_prx_cond:
3008 if (!(s->flags & SN_ERR_MASK))
3009 s->flags |= SN_ERR_PRXCOND;
3010 if (!(s->flags & SN_FINST_MASK))
3011 s->flags |= SN_FINST_R;
3012
3013 req->analysers = 0;
3014 req->analyse_exp = TICK_ETERNITY;
3015 return 0;
3016}
3017
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003018
Willy Tarreau347a35d2013-11-22 17:51:09 +01003019/* This function prepares an applet to handle the stats. It can deal with the
3020 * "100-continue" expectation, check that admin rules are met for POST requests,
3021 * and program a response message if something was unexpected. It cannot fail
3022 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003023 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003024 * s->target which is supposed to already point to the stats applet. The caller
3025 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003026 */
3027int http_handle_stats(struct session *s, struct channel *req)
3028{
3029 struct stats_admin_rule *stats_admin_rule;
3030 struct stream_interface *si = s->rep->prod;
3031 struct http_txn *txn = &s->txn;
3032 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003033 struct uri_auth *uri_auth = s->be->uri_auth;
3034 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003035 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003036
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003037 appctx = si_appctx(si);
3038 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3039 appctx->st1 = appctx->st2 = 0;
3040 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3041 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003042 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3043 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003044
3045 uri = msg->chn->buf->p + msg->sl.rq.u;
3046 lookup = uri + uri_auth->uri_len;
3047
3048 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3049 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003050 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003051 break;
3052 }
3053 }
3054
3055 if (uri_auth->refresh) {
3056 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3057 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003058 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003059 break;
3060 }
3061 }
3062 }
3063
3064 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3065 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003066 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003067 break;
3068 }
3069 }
3070
3071 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3072 if (memcmp(h, ";st=", 4) == 0) {
3073 int i;
3074 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003075 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003076 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3077 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003078 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003079 break;
3080 }
3081 }
3082 break;
3083 }
3084 }
3085
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003086 appctx->ctx.stats.scope_str = 0;
3087 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003088 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3089 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3090 int itx = 0;
3091 const char *h2;
3092 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3093 const char *err;
3094
3095 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3096 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003097 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003098 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3099 itx++;
3100 h++;
3101 }
3102
3103 if (itx > STAT_SCOPE_TXT_MAXLEN)
3104 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003107 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003108 memcpy(scope_txt, h2, itx);
3109 scope_txt[itx] = '\0';
3110 err = invalid_char(scope_txt);
3111 if (err) {
3112 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003113 appctx->ctx.stats.scope_str = 0;
3114 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003115 }
3116 break;
3117 }
3118 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003119
3120 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003121 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003122 int ret = 1;
3123
3124 if (stats_admin_rule->cond) {
3125 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3126 ret = acl_pass(ret);
3127 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3128 ret = !ret;
3129 }
3130
3131 if (ret) {
3132 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003133 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003134 break;
3135 }
3136 }
3137
3138 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003139 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003140 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003141 /* we'll need the request body, possibly after sending 100-continue */
3142 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003143 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003144 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003145 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003146 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3147 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003148 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003149 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003150 else {
3151 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003152 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003153 }
3154
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003155 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003156 return 1;
3157}
3158
Lukas Tribus67db8df2013-06-23 17:37:13 +02003159/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3160 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3161 */
3162static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3163{
3164#ifdef IP_TOS
3165 if (from.ss_family == AF_INET)
3166 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3167#endif
3168#ifdef IPV6_TCLASS
3169 if (from.ss_family == AF_INET6) {
3170 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3171 /* v4-mapped addresses need IP_TOS */
3172 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3173 else
3174 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3175 }
3176#endif
3177}
3178
Willy Tarreau20b0de52012-12-24 15:45:22 +01003179/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003180 * transaction <txn>. Returns the verdict of the first rule that prevents
3181 * further processing of the request (auth, deny, ...), and defaults to
3182 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3183 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3184 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003185 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003186enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003187http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003188{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003189 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003190 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003191 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003192 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003193
Willy Tarreauff011f22011-01-06 17:51:27 +01003194 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003195 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003196 continue;
3197
Willy Tarreau96257ec2012-12-27 10:46:37 +01003198 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003199 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003200 int ret;
3201
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003202 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003203 ret = acl_pass(ret);
3204
Willy Tarreauff011f22011-01-06 17:51:27 +01003205 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003206 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003207
3208 if (!ret) /* condition not matched */
3209 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003210 }
3211
Willy Tarreau20b0de52012-12-24 15:45:22 +01003212
Willy Tarreau96257ec2012-12-27 10:46:37 +01003213 switch (rule->action) {
3214 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003215 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003216
3217 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003218 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003219
Willy Tarreauccbcc372012-12-27 12:37:57 +01003220 case HTTP_REQ_ACT_TARPIT:
3221 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003222 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003223
Willy Tarreau96257ec2012-12-27 10:46:37 +01003224 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003225 /* Auth might be performed on regular http-req rules as well as on stats */
3226 auth_realm = rule->arg.auth.realm;
3227 if (!auth_realm) {
3228 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3229 auth_realm = STATS_DEFAULT_REALM;
3230 else
3231 auth_realm = px->id;
3232 }
3233 /* send 401/407 depending on whether we use a proxy or not. We still
3234 * count one error, because normal browsing won't significantly
3235 * increase the counter but brute force attempts will.
3236 */
3237 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3238 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3239 stream_int_retnclose(&s->si[0], &trash);
3240 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003241 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003242
Willy Tarreau81499eb2012-12-27 12:19:02 +01003243 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003244 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3245 return HTTP_RULE_RES_BADREQ;
3246 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003247
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003248 case HTTP_REQ_ACT_SET_NICE:
3249 s->task->nice = rule->arg.nice;
3250 break;
3251
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003252 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003253 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003254 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003255 break;
3256
Willy Tarreau51347ed2013-06-11 19:34:13 +02003257 case HTTP_REQ_ACT_SET_MARK:
3258#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003259 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003260 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003261#endif
3262 break;
3263
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003264 case HTTP_REQ_ACT_SET_LOGL:
3265 s->logs.level = rule->arg.loglevel;
3266 break;
3267
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003268 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003269 case HTTP_REQ_ACT_SET_HDR:
3270 ctx.idx = 0;
3271 /* remove all occurrences of the header */
3272 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3273 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3274 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003275 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003276 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3277 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003278 /* now fall through to header addition */
3279
3280 case HTTP_REQ_ACT_ADD_HDR:
3281 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3282 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3283 trash.len = rule->arg.hdr_add.name_len;
3284 trash.str[trash.len++] = ':';
3285 trash.str[trash.len++] = ' ';
3286 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3287 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3288 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003289
3290 case HTTP_REQ_ACT_DEL_ACL:
3291 case HTTP_REQ_ACT_DEL_MAP: {
3292 struct pat_ref *ref;
3293 char *key;
3294 int len;
3295
3296 /* collect reference */
3297 ref = pat_ref_lookup(rule->arg.map.ref);
3298 if (!ref)
3299 continue;
3300
3301 /* collect key */
3302 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3303 key = trash.str;
3304 key[len] = '\0';
3305
3306 /* perform update */
3307 /* returned code: 1=ok, 0=ko */
3308 pat_ref_delete(ref, key);
3309
3310 break;
3311 }
3312
3313 case HTTP_REQ_ACT_ADD_ACL: {
3314 struct pat_ref *ref;
3315 char *key;
3316 struct chunk *trash_key;
3317 int len;
3318
3319 trash_key = get_trash_chunk();
3320
3321 /* collect reference */
3322 ref = pat_ref_lookup(rule->arg.map.ref);
3323 if (!ref)
3324 continue;
3325
3326 /* collect key */
3327 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3328 key = trash_key->str;
3329 key[len] = '\0';
3330
3331 /* perform update */
3332 /* add entry only if it does not already exist */
3333 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003334 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003335
3336 break;
3337 }
3338
3339 case HTTP_REQ_ACT_SET_MAP: {
3340 struct pat_ref *ref;
3341 char *key, *value;
3342 struct chunk *trash_key, *trash_value;
3343 int len;
3344
3345 trash_key = get_trash_chunk();
3346 trash_value = get_trash_chunk();
3347
3348 /* collect reference */
3349 ref = pat_ref_lookup(rule->arg.map.ref);
3350 if (!ref)
3351 continue;
3352
3353 /* collect key */
3354 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3355 key = trash_key->str;
3356 key[len] = '\0';
3357
3358 /* collect value */
3359 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3360 value = trash_value->str;
3361 value[len] = '\0';
3362
3363 /* perform update */
3364 if (pat_ref_find_elt(ref, key) != NULL)
3365 /* update entry if it exists */
3366 pat_ref_set(ref, key, value, NULL);
3367 else
3368 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003369 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003370
3371 break;
3372 }
William Lallemand73025dd2014-04-24 14:38:37 +02003373
3374 case HTTP_REQ_ACT_CUSTOM_CONT:
3375 rule->action_ptr(rule, px, s, txn);
3376 break;
3377
3378 case HTTP_REQ_ACT_CUSTOM_STOP:
3379 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003380 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003381 }
3382 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003383
3384 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003385 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003386}
3387
Willy Tarreau71241ab2012-12-27 11:30:54 +01003388
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003389/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3390 * transaction <txn>. Returns the first rule that prevents further processing
3391 * of the response (deny, ...) or NULL if it executed all rules or stopped
3392 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3393 * rule.
3394 */
3395static struct http_res_rule *
3396http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3397{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003398 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003399 struct http_res_rule *rule;
3400 struct hdr_ctx ctx;
3401
3402 list_for_each_entry(rule, rules, list) {
3403 if (rule->action >= HTTP_RES_ACT_MAX)
3404 continue;
3405
3406 /* check optional condition */
3407 if (rule->cond) {
3408 int ret;
3409
3410 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3411 ret = acl_pass(ret);
3412
3413 if (rule->cond->pol == ACL_COND_UNLESS)
3414 ret = !ret;
3415
3416 if (!ret) /* condition not matched */
3417 continue;
3418 }
3419
3420
3421 switch (rule->action) {
3422 case HTTP_RES_ACT_ALLOW:
3423 return NULL; /* "allow" rules are OK */
3424
3425 case HTTP_RES_ACT_DENY:
3426 txn->flags |= TX_SVDENY;
3427 return rule;
3428
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003429 case HTTP_RES_ACT_SET_NICE:
3430 s->task->nice = rule->arg.nice;
3431 break;
3432
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003433 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003434 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003435 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003436 break;
3437
Willy Tarreau51347ed2013-06-11 19:34:13 +02003438 case HTTP_RES_ACT_SET_MARK:
3439#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003440 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003441 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003442#endif
3443 break;
3444
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003445 case HTTP_RES_ACT_SET_LOGL:
3446 s->logs.level = rule->arg.loglevel;
3447 break;
3448
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003449 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003450 case HTTP_RES_ACT_SET_HDR:
3451 ctx.idx = 0;
3452 /* remove all occurrences of the header */
3453 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3454 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3455 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3456 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003457 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3458 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003459 /* now fall through to header addition */
3460
3461 case HTTP_RES_ACT_ADD_HDR:
3462 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3463 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3464 trash.len = rule->arg.hdr_add.name_len;
3465 trash.str[trash.len++] = ':';
3466 trash.str[trash.len++] = ' ';
3467 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3468 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3469 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003470
3471 case HTTP_RES_ACT_DEL_ACL:
3472 case HTTP_RES_ACT_DEL_MAP: {
3473 struct pat_ref *ref;
3474 char *key;
3475 int len;
3476
3477 /* collect reference */
3478 ref = pat_ref_lookup(rule->arg.map.ref);
3479 if (!ref)
3480 continue;
3481
3482 /* collect key */
3483 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3484 key = trash.str;
3485 key[len] = '\0';
3486
3487 /* perform update */
3488 /* returned code: 1=ok, 0=ko */
3489 pat_ref_delete(ref, key);
3490
3491 break;
3492 }
3493
3494 case HTTP_RES_ACT_ADD_ACL: {
3495 struct pat_ref *ref;
3496 char *key;
3497 struct chunk *trash_key;
3498 int len;
3499
3500 trash_key = get_trash_chunk();
3501
3502 /* collect reference */
3503 ref = pat_ref_lookup(rule->arg.map.ref);
3504 if (!ref)
3505 continue;
3506
3507 /* collect key */
3508 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3509 key = trash_key->str;
3510 key[len] = '\0';
3511
3512 /* perform update */
3513 /* check if the entry already exists */
3514 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003515 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003516
3517 break;
3518 }
3519
3520 case HTTP_RES_ACT_SET_MAP: {
3521 struct pat_ref *ref;
3522 char *key, *value;
3523 struct chunk *trash_key, *trash_value;
3524 int len;
3525
3526 trash_key = get_trash_chunk();
3527 trash_value = get_trash_chunk();
3528
3529 /* collect reference */
3530 ref = pat_ref_lookup(rule->arg.map.ref);
3531 if (!ref)
3532 continue;
3533
3534 /* collect key */
3535 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3536 key = trash_key->str;
3537 key[len] = '\0';
3538
3539 /* collect value */
3540 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3541 value = trash_value->str;
3542 value[len] = '\0';
3543
3544 /* perform update */
3545 if (pat_ref_find_elt(ref, key) != NULL)
3546 /* update entry if it exists */
3547 pat_ref_set(ref, key, value, NULL);
3548 else
3549 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003550 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003551
3552 break;
3553 }
William Lallemand73025dd2014-04-24 14:38:37 +02003554
3555 case HTTP_RES_ACT_CUSTOM_CONT:
3556 rule->action_ptr(rule, px, s, txn);
3557 break;
3558
3559 case HTTP_RES_ACT_CUSTOM_STOP:
3560 rule->action_ptr(rule, px, s, txn);
3561 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003562 }
3563 }
3564
3565 /* we reached the end of the rules, nothing to report */
3566 return NULL;
3567}
3568
3569
Willy Tarreau71241ab2012-12-27 11:30:54 +01003570/* Perform an HTTP redirect based on the information in <rule>. The function
3571 * returns non-zero on success, or zero in case of a, irrecoverable error such
3572 * as too large a request to build a valid response.
3573 */
3574static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3575{
3576 struct http_msg *msg = &txn->req;
3577 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003578 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003579
3580 /* build redirect message */
3581 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003582 case 308:
3583 msg_fmt = HTTP_308;
3584 break;
3585 case 307:
3586 msg_fmt = HTTP_307;
3587 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003588 case 303:
3589 msg_fmt = HTTP_303;
3590 break;
3591 case 301:
3592 msg_fmt = HTTP_301;
3593 break;
3594 case 302:
3595 default:
3596 msg_fmt = HTTP_302;
3597 break;
3598 }
3599
3600 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3601 return 0;
3602
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003603 location = trash.str + trash.len;
3604
Willy Tarreau71241ab2012-12-27 11:30:54 +01003605 switch(rule->type) {
3606 case REDIRECT_TYPE_SCHEME: {
3607 const char *path;
3608 const char *host;
3609 struct hdr_ctx ctx;
3610 int pathlen;
3611 int hostlen;
3612
3613 host = "";
3614 hostlen = 0;
3615 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003616 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003617 host = ctx.line + ctx.val;
3618 hostlen = ctx.vlen;
3619 }
3620
3621 path = http_get_path(txn);
3622 /* build message using path */
3623 if (path) {
3624 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3625 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3626 int qs = 0;
3627 while (qs < pathlen) {
3628 if (path[qs] == '?') {
3629 pathlen = qs;
3630 break;
3631 }
3632 qs++;
3633 }
3634 }
3635 } else {
3636 path = "/";
3637 pathlen = 1;
3638 }
3639
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003640 if (rule->rdr_str) { /* this is an old "redirect" rule */
3641 /* check if we can add scheme + "://" + host + path */
3642 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3643 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003644
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003645 /* add scheme */
3646 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3647 trash.len += rule->rdr_len;
3648 }
3649 else {
3650 /* add scheme with executing log format */
3651 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003652
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003653 /* check if we can add scheme + "://" + host + path */
3654 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3655 return 0;
3656 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003657 /* add "://" */
3658 memcpy(trash.str + trash.len, "://", 3);
3659 trash.len += 3;
3660
3661 /* add host */
3662 memcpy(trash.str + trash.len, host, hostlen);
3663 trash.len += hostlen;
3664
3665 /* add path */
3666 memcpy(trash.str + trash.len, path, pathlen);
3667 trash.len += pathlen;
3668
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003669 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003670 if (trash.len && trash.str[trash.len - 1] != '/' &&
3671 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3672 if (trash.len > trash.size - 5)
3673 return 0;
3674 trash.str[trash.len] = '/';
3675 trash.len++;
3676 }
3677
3678 break;
3679 }
3680 case REDIRECT_TYPE_PREFIX: {
3681 const char *path;
3682 int pathlen;
3683
3684 path = http_get_path(txn);
3685 /* build message using path */
3686 if (path) {
3687 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3688 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3689 int qs = 0;
3690 while (qs < pathlen) {
3691 if (path[qs] == '?') {
3692 pathlen = qs;
3693 break;
3694 }
3695 qs++;
3696 }
3697 }
3698 } else {
3699 path = "/";
3700 pathlen = 1;
3701 }
3702
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003703 if (rule->rdr_str) { /* this is an old "redirect" rule */
3704 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3705 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003706
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003707 /* add prefix. Note that if prefix == "/", we don't want to
3708 * add anything, otherwise it makes it hard for the user to
3709 * configure a self-redirection.
3710 */
3711 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3712 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3713 trash.len += rule->rdr_len;
3714 }
3715 }
3716 else {
3717 /* add prefix with executing log format */
3718 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3719
3720 /* Check length */
3721 if (trash.len + pathlen > trash.size - 4)
3722 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003723 }
3724
3725 /* add path */
3726 memcpy(trash.str + trash.len, path, pathlen);
3727 trash.len += pathlen;
3728
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003729 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003730 if (trash.len && trash.str[trash.len - 1] != '/' &&
3731 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3732 if (trash.len > trash.size - 5)
3733 return 0;
3734 trash.str[trash.len] = '/';
3735 trash.len++;
3736 }
3737
3738 break;
3739 }
3740 case REDIRECT_TYPE_LOCATION:
3741 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003742 if (rule->rdr_str) { /* this is an old "redirect" rule */
3743 if (trash.len + rule->rdr_len > trash.size - 4)
3744 return 0;
3745
3746 /* add location */
3747 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3748 trash.len += rule->rdr_len;
3749 }
3750 else {
3751 /* add location with executing log format */
3752 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003753
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003754 /* Check left length */
3755 if (trash.len > trash.size - 4)
3756 return 0;
3757 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003758 break;
3759 }
3760
3761 if (rule->cookie_len) {
3762 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3763 trash.len += 14;
3764 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3765 trash.len += rule->cookie_len;
3766 memcpy(trash.str + trash.len, "\r\n", 2);
3767 trash.len += 2;
3768 }
3769
3770 /* add end of headers and the keep-alive/close status.
3771 * We may choose to set keep-alive if the Location begins
3772 * with a slash, because the client will come back to the
3773 * same server.
3774 */
3775 txn->status = rule->code;
3776 /* let's log the request time */
3777 s->logs.tv_request = now;
3778
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003779 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003780 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3781 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3782 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3783 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3784 /* keep-alive possible */
3785 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3786 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3787 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3788 trash.len += 30;
3789 } else {
3790 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3791 trash.len += 24;
3792 }
3793 }
3794 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3795 trash.len += 4;
3796 bo_inject(txn->rsp.chn, trash.str, trash.len);
3797 /* "eat" the request */
3798 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003799 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003800 msg->sov = 0;
3801 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3802 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3803 txn->req.msg_state = HTTP_MSG_CLOSED;
3804 txn->rsp.msg_state = HTTP_MSG_DONE;
3805 } else {
3806 /* keep-alive not possible */
3807 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3808 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3809 trash.len += 29;
3810 } else {
3811 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3812 trash.len += 23;
3813 }
3814 stream_int_retnclose(txn->req.chn->prod, &trash);
3815 txn->req.chn->analysers = 0;
3816 }
3817
3818 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003819 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003820 if (!(s->flags & SN_FINST_MASK))
3821 s->flags |= SN_FINST_R;
3822
3823 return 1;
3824}
3825
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003826/* This stream analyser runs all HTTP request processing which is common to
3827 * frontends and backends, which means blocking ACLs, filters, connection-close,
3828 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003829 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003830 * either needs more data or wants to immediately abort the request (eg: deny,
3831 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003832 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003833int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003834{
Willy Tarreaud787e662009-07-07 10:14:51 +02003835 struct http_txn *txn = &s->txn;
3836 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003837 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003838 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003839 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003840
Willy Tarreau655dce92009-11-08 13:10:58 +01003841 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003842 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003843 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003844 return 0;
3845 }
3846
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003847 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003848 now_ms, __FUNCTION__,
3849 s,
3850 req,
3851 req->rex, req->wex,
3852 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003853 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003854 req->analysers);
3855
Willy Tarreau65410832014-04-28 21:25:43 +02003856 /* just in case we have some per-backend tracking */
3857 session_inc_be_http_req_ctr(s);
3858
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003859 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003860 if (!LIST_ISEMPTY(&px->http_req_rules)) {
3861 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003862
Willy Tarreau0b748332014-04-29 00:13:29 +02003863 switch (verdict) {
3864 case HTTP_RULE_RES_CONT:
3865 case HTTP_RULE_RES_STOP: /* nothing to do */
3866 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003867
Willy Tarreau0b748332014-04-29 00:13:29 +02003868 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3869 if (txn->flags & TX_CLTARPIT)
3870 goto tarpit;
3871 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003872
Willy Tarreau0b748332014-04-29 00:13:29 +02003873 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3874 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003875
Willy Tarreau0b748332014-04-29 00:13:29 +02003876 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003877 goto done;
3878
Willy Tarreau0b748332014-04-29 00:13:29 +02003879 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3880 goto return_bad_req;
3881 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003882 }
3883
Willy Tarreau52542592014-04-28 18:33:26 +02003884 /* OK at this stage, we know that the request was accepted according to
3885 * the http-request rules, we can check for the stats. Note that the
3886 * URI is detected *before* the req* rules in order not to be affected
3887 * by a possible reqrep, while they are processed *after* so that a
3888 * reqdeny can still block them. This clearly needs to change in 1.6!
3889 */
3890 if (stats_check_uri(s->rep->prod, txn, px)) {
3891 s->target = &http_stats_applet.obj_type;
3892 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3893 txn->status = 500;
3894 s->logs.tv_request = now;
3895 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003896
Willy Tarreau52542592014-04-28 18:33:26 +02003897 if (!(s->flags & SN_ERR_MASK))
3898 s->flags |= SN_ERR_RESOURCE;
3899 goto return_prx_cond;
3900 }
3901
3902 /* parse the whole stats request and extract the relevant information */
3903 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02003904 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
3905 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003906
Willy Tarreau0b748332014-04-29 00:13:29 +02003907 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3908 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003909
Willy Tarreau0b748332014-04-29 00:13:29 +02003910 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3911 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003912 }
3913
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003914 /* evaluate the req* rules except reqadd */
3915 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003916 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003917 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003918
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003919 if (txn->flags & TX_CLDENY)
3920 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003921
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003922 if (txn->flags & TX_CLTARPIT)
3923 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003924 }
Willy Tarreau06619262006-12-17 08:37:22 +01003925
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003926 /* add request headers from the rule sets in the same order */
3927 list_for_each_entry(wl, &px->req_add, list) {
3928 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003929 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003930 ret = acl_pass(ret);
3931 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3932 ret = !ret;
3933 if (!ret)
3934 continue;
3935 }
3936
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003937 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003938 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003939 }
3940
Willy Tarreau52542592014-04-28 18:33:26 +02003941
3942 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003943 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003944 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003945 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3946 s->fe->fe_counters.intercepted_req++;
3947
3948 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3949 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3950 if (!(s->flags & SN_FINST_MASK))
3951 s->flags |= SN_FINST_R;
3952
Willy Tarreau70730dd2014-04-24 18:06:27 +02003953 /* we may want to compress the stats page */
3954 if (s->fe->comp || s->be->comp)
3955 select_compression_request_header(s, req->buf);
3956
3957 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003958 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) |
3959 AN_REQ_HTTP_XFER_BODY | AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE | AN_RES_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003960 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003961 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003962
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003963 /* check whether we have some ACLs set to redirect this request */
3964 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003965 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003966 int ret;
3967
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003968 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003969 ret = acl_pass(ret);
3970 if (rule->cond->pol == ACL_COND_UNLESS)
3971 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003972 if (!ret)
3973 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003974 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003975 if (!http_apply_redirect_rule(rule, s, txn))
3976 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003977 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003978 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003979
Willy Tarreau2be39392010-01-03 17:24:51 +01003980 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3981 * If this happens, then the data will not come immediately, so we must
3982 * send all what we have without waiting. Note that due to the small gain
3983 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003984 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003985 * itself once used.
3986 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003987 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003988
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003989 done: /* done with this analyser, continue with next ones that the calling
3990 * points will have set, if any.
3991 */
3992 req->analysers &= ~an_bit;
3993 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003994 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003995
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003996 tarpit:
3997 /* When a connection is tarpitted, we use the tarpit timeout,
3998 * which may be the same as the connect timeout if unspecified.
3999 * If unset, then set it to zero because we really want it to
4000 * eventually expire. We build the tarpit as an analyser.
4001 */
4002 channel_erase(s->req);
4003
4004 /* wipe the request out so that we can drop the connection early
4005 * if the client closes first.
4006 */
4007 channel_dont_connect(req);
4008 req->analysers = 0; /* remove switching rules etc... */
4009 req->analysers |= AN_REQ_HTTP_TARPIT;
4010 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4011 if (!req->analyse_exp)
4012 req->analyse_exp = tick_add(now_ms, 0);
4013 session_inc_http_err_ctr(s);
4014 s->fe->fe_counters.denied_req++;
4015 if (s->fe != s->be)
4016 s->be->be_counters.denied_req++;
4017 if (s->listener->counters)
4018 s->listener->counters->denied_req++;
4019 goto done;
4020
4021 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004022 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004023 txn->status = 403;
4024 s->logs.tv_request = now;
4025 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4026 session_inc_http_err_ctr(s);
4027 s->fe->fe_counters.denied_req++;
4028 if (s->fe != s->be)
4029 s->be->be_counters.denied_req++;
4030 if (s->listener->counters)
4031 s->listener->counters->denied_req++;
4032 goto return_prx_cond;
4033
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004034 return_bad_req:
4035 /* We centralize bad requests processing here */
4036 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4037 /* we detected a parsing error. We want to archive this request
4038 * in the dedicated proxy area for later troubleshooting.
4039 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004040 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004041 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004042
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004043 txn->req.msg_state = HTTP_MSG_ERROR;
4044 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004045 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004046
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004047 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004048 if (s->listener->counters)
4049 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004050
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004051 return_prx_cond:
4052 if (!(s->flags & SN_ERR_MASK))
4053 s->flags |= SN_ERR_PRXCOND;
4054 if (!(s->flags & SN_FINST_MASK))
4055 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004056
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004057 req->analysers = 0;
4058 req->analyse_exp = TICK_ETERNITY;
4059 return 0;
4060}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004061
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004062/* This function performs all the processing enabled for the current request.
4063 * It returns 1 if the processing can continue on next analysers, or zero if it
4064 * needs more data, encounters an error, or wants to immediately abort the
4065 * request. It relies on buffers flags, and updates s->req->analysers.
4066 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004067int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004068{
4069 struct http_txn *txn = &s->txn;
4070 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004071 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004072
Willy Tarreau655dce92009-11-08 13:10:58 +01004073 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004074 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004075 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004076 return 0;
4077 }
4078
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004079 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004080 now_ms, __FUNCTION__,
4081 s,
4082 req,
4083 req->rex, req->wex,
4084 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004085 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004086 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004087
William Lallemand82fe75c2012-10-23 10:25:10 +02004088 if (s->fe->comp || s->be->comp)
4089 select_compression_request_header(s, req->buf);
4090
Willy Tarreau59234e92008-11-30 23:51:27 +01004091 /*
4092 * Right now, we know that we have processed the entire headers
4093 * and that unwanted requests have been filtered out. We can do
4094 * whatever we want with the remaining request. Also, now we
4095 * may have separate values for ->fe, ->be.
4096 */
Willy Tarreau06619262006-12-17 08:37:22 +01004097
Willy Tarreau59234e92008-11-30 23:51:27 +01004098 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004099 * If HTTP PROXY is set we simply get remote server address parsing
4100 * incoming request. Note that this requires that a connection is
4101 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004102 */
4103 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004104 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004105 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004106
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004107 /* Note that for now we don't reuse existing proxy connections */
4108 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004109 txn->req.msg_state = HTTP_MSG_ERROR;
4110 txn->status = 500;
4111 req->analysers = 0;
4112 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4113
4114 if (!(s->flags & SN_ERR_MASK))
4115 s->flags |= SN_ERR_RESOURCE;
4116 if (!(s->flags & SN_FINST_MASK))
4117 s->flags |= SN_FINST_R;
4118
4119 return 0;
4120 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004121
4122 path = http_get_path(txn);
4123 url2sa(req->buf->p + msg->sl.rq.u,
4124 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004125 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004126 /* if the path was found, we have to remove everything between
4127 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4128 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4129 * u_l characters by a single "/".
4130 */
4131 if (path) {
4132 char *cur_ptr = req->buf->p;
4133 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4134 int delta;
4135
4136 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4137 http_msg_move_end(&txn->req, delta);
4138 cur_end += delta;
4139 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4140 goto return_bad_req;
4141 }
4142 else {
4143 char *cur_ptr = req->buf->p;
4144 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4145 int delta;
4146
4147 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4148 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4149 http_msg_move_end(&txn->req, delta);
4150 cur_end += delta;
4151 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4152 goto return_bad_req;
4153 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004154 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004155
Willy Tarreau59234e92008-11-30 23:51:27 +01004156 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004157 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004158 * Note that doing so might move headers in the request, but
4159 * the fields will stay coherent and the URI will not move.
4160 * This should only be performed in the backend.
4161 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004162 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004163 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4164 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004165
Willy Tarreau59234e92008-11-30 23:51:27 +01004166 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004167 * 8: the appsession cookie was looked up very early in 1.2,
4168 * so let's do the same now.
4169 */
4170
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004171 /* It needs to look into the URI unless persistence must be ignored */
4172 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004173 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 +01004174 }
4175
William Lallemanda73203e2012-03-12 12:48:57 +01004176 /* add unique-id if "header-unique-id" is specified */
4177
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004178 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4179 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4180 goto return_bad_req;
4181 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004182 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004183 }
William Lallemanda73203e2012-03-12 12:48:57 +01004184
4185 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004186 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4187 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004188 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004189 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004190 goto return_bad_req;
4191 }
4192
Cyril Bontéb21570a2009-11-29 20:04:48 +01004193 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004194 * 9: add X-Forwarded-For if either the frontend or the backend
4195 * asks for it.
4196 */
4197 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004198 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004199 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004200 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4201 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004202 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004203 /* The header is set to be added only if none is present
4204 * and we found it, so don't do anything.
4205 */
4206 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004207 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004208 /* Add an X-Forwarded-For header unless the source IP is
4209 * in the 'except' network range.
4210 */
4211 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004212 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004213 != s->fe->except_net.s_addr) &&
4214 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004215 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004216 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004217 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004218 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004219 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004220
4221 /* Note: we rely on the backend to get the header name to be used for
4222 * x-forwarded-for, because the header is really meant for the backends.
4223 * However, if the backend did not specify any option, we have to rely
4224 * on the frontend's header name.
4225 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004226 if (s->be->fwdfor_hdr_len) {
4227 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004228 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004229 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004230 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004231 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004232 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004233 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 +01004234
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004235 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004236 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004237 }
4238 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004239 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004240 /* FIXME: for the sake of completeness, we should also support
4241 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004242 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004243 int len;
4244 char pn[INET6_ADDRSTRLEN];
4245 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004246 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004247 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004248
Willy Tarreau59234e92008-11-30 23:51:27 +01004249 /* Note: we rely on the backend to get the header name to be used for
4250 * x-forwarded-for, because the header is really meant for the backends.
4251 * However, if the backend did not specify any option, we have to rely
4252 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004253 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004254 if (s->be->fwdfor_hdr_len) {
4255 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004256 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004257 } else {
4258 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004259 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004260 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004261 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004262
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004263 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004264 goto return_bad_req;
4265 }
4266 }
4267
4268 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004269 * 10: add X-Original-To if either the frontend or the backend
4270 * asks for it.
4271 */
4272 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4273
4274 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004275 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004276 /* Add an X-Original-To header unless the destination IP is
4277 * in the 'except' network range.
4278 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004279 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004280
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004281 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004282 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004283 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004284 != s->fe->except_to.s_addr) &&
4285 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004286 (((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 +02004287 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004288 int len;
4289 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004290 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004291
4292 /* Note: we rely on the backend to get the header name to be used for
4293 * x-original-to, because the header is really meant for the backends.
4294 * However, if the backend did not specify any option, we have to rely
4295 * on the frontend's header name.
4296 */
4297 if (s->be->orgto_hdr_len) {
4298 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004299 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004300 } else {
4301 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004302 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004303 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004304 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 +02004305
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004306 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004307 goto return_bad_req;
4308 }
4309 }
4310 }
4311
Willy Tarreau50fc7772012-11-11 22:19:57 +01004312 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4313 * If an "Upgrade" token is found, the header is left untouched in order not to have
4314 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4315 * "Upgrade" is present in the Connection header.
4316 */
4317 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4318 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004319 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4320 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004321 unsigned int want_flags = 0;
4322
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004323 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004324 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004325 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4326 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004327 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004328 want_flags |= TX_CON_CLO_SET;
4329 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004330 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004331 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4332 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004333 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004334 want_flags |= TX_CON_KAL_SET;
4335 }
4336
4337 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004338 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004339 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004340
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004341
Willy Tarreau522d6c02009-12-06 18:49:18 +01004342 /* If we have no server assigned yet and we're balancing on url_param
4343 * with a POST request, we may be interested in checking the body for
4344 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004345 */
4346 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4347 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004348 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004349 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004350 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004351 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004352
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004353 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004354 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004355#ifdef TCP_QUICKACK
4356 /* We expect some data from the client. Unless we know for sure
4357 * we already have a full request, we have to re-enable quick-ack
4358 * in case we previously disabled it, otherwise we might cause
4359 * the client to delay further data.
4360 */
4361 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004362 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004363 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004364 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004365 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004366#endif
4367 }
Willy Tarreau03945942009-12-22 16:50:27 +01004368
Willy Tarreau59234e92008-11-30 23:51:27 +01004369 /*************************************************************
4370 * OK, that's finished for the headers. We have done what we *
4371 * could. Let's switch to the DATA state. *
4372 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004373 req->analyse_exp = TICK_ETERNITY;
4374 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004375
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004376 /* if the server closes the connection, we want to immediately react
4377 * and close the socket to save packets and syscalls.
4378 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004379 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4380 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004381
Willy Tarreau59234e92008-11-30 23:51:27 +01004382 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004383 /* OK let's go on with the BODY now */
4384 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004385
Willy Tarreau59234e92008-11-30 23:51:27 +01004386 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004387 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004388 /* we detected a parsing error. We want to archive this request
4389 * in the dedicated proxy area for later troubleshooting.
4390 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004391 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004392 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004393
Willy Tarreau59234e92008-11-30 23:51:27 +01004394 txn->req.msg_state = HTTP_MSG_ERROR;
4395 txn->status = 400;
4396 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004397 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004398
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004399 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004400 if (s->listener->counters)
4401 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004402
Willy Tarreau59234e92008-11-30 23:51:27 +01004403 if (!(s->flags & SN_ERR_MASK))
4404 s->flags |= SN_ERR_PRXCOND;
4405 if (!(s->flags & SN_FINST_MASK))
4406 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004407 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004408}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004409
Willy Tarreau60b85b02008-11-30 23:28:40 +01004410/* This function is an analyser which processes the HTTP tarpit. It always
4411 * returns zero, at the beginning because it prevents any other processing
4412 * from occurring, and at the end because it terminates the request.
4413 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004414int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004415{
4416 struct http_txn *txn = &s->txn;
4417
4418 /* This connection is being tarpitted. The CLIENT side has
4419 * already set the connect expiration date to the right
4420 * timeout. We just have to check that the client is still
4421 * there and that the timeout has not expired.
4422 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004423 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004424 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004425 !tick_is_expired(req->analyse_exp, now_ms))
4426 return 0;
4427
4428 /* We will set the queue timer to the time spent, just for
4429 * logging purposes. We fake a 500 server error, so that the
4430 * attacker will not suspect his connection has been tarpitted.
4431 * It will not cause trouble to the logs because we can exclude
4432 * the tarpitted connections by filtering on the 'PT' status flags.
4433 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004434 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4435
4436 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004437 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004438 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004439
4440 req->analysers = 0;
4441 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004442
Willy Tarreau60b85b02008-11-30 23:28:40 +01004443 if (!(s->flags & SN_ERR_MASK))
4444 s->flags |= SN_ERR_PRXCOND;
4445 if (!(s->flags & SN_FINST_MASK))
4446 s->flags |= SN_FINST_T;
4447 return 0;
4448}
4449
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004450/* This function is an analyser which waits for the HTTP request body. It waits
4451 * for either the buffer to be full, or the full advertised contents to have
4452 * reached the buffer. It must only be called after the standard HTTP request
4453 * processing has occurred, because it expects the request to be parsed and will
4454 * look for the Expect header. It may send a 100-Continue interim response. It
4455 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4456 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4457 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004458 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004459int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004460{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004461 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004462 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004463
4464 /* We have to parse the HTTP request body to find any required data.
4465 * "balance url_param check_post" should have been the only way to get
4466 * into this. We were brought here after HTTP header analysis, so all
4467 * related structures are ready.
4468 */
4469
Willy Tarreau890988f2014-04-10 11:59:33 +02004470 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4471 /* This is the first call */
4472 if (msg->msg_state < HTTP_MSG_BODY)
4473 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004474
Willy Tarreau890988f2014-04-10 11:59:33 +02004475 if (msg->msg_state < HTTP_MSG_100_SENT) {
4476 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4477 * send an HTTP/1.1 100 Continue intermediate response.
4478 */
4479 if (msg->flags & HTTP_MSGF_VER_11) {
4480 struct hdr_ctx ctx;
4481 ctx.idx = 0;
4482 /* Expect is allowed in 1.1, look for it */
4483 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4484 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4485 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4486 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004487 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004488 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004489 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004490
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004491 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004492 * req->buf->p still points to the beginning of the message. We
4493 * must save the body in msg->next because it survives buffer
4494 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004495 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004496 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004497
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004498 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004499 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4500 else
4501 msg->msg_state = HTTP_MSG_DATA;
4502 }
4503
Willy Tarreau890988f2014-04-10 11:59:33 +02004504 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4505 /* We're in content-length mode, we just have to wait for enough data. */
4506 if (req->buf->i - msg->sov < msg->body_len)
4507 goto missing_data;
4508
4509 /* OK we have everything we need now */
4510 goto http_end;
4511 }
4512
4513 /* OK here we're parsing a chunked-encoded message */
4514
Willy Tarreau522d6c02009-12-06 18:49:18 +01004515 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004516 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004517 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004518 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004519 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004520 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004521
Willy Tarreau115acb92009-12-26 13:56:06 +01004522 if (!ret)
4523 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004524 else if (ret < 0) {
4525 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004526 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004527 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004528 }
4529
Willy Tarreaud98cf932009-12-27 22:54:55 +01004530 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004531 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004532 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004533 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004534 if (msg->msg_state == HTTP_MSG_TRAILERS)
4535 goto http_end;
4536
Willy Tarreau226071e2014-04-10 11:55:45 +02004537 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004538 goto http_end;
4539
4540 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004541 /* we get here if we need to wait for more data. If the buffer is full,
4542 * we have the maximum we can expect.
4543 */
4544 if (buffer_full(req->buf, global.tune.maxrewrite))
4545 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004546
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004547 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004548 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004549 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004550
4551 if (!(s->flags & SN_ERR_MASK))
4552 s->flags |= SN_ERR_CLITO;
4553 if (!(s->flags & SN_FINST_MASK))
4554 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004555 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004556 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004557
4558 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004559 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004560 /* Not enough data. We'll re-use the http-request
4561 * timeout here. Ideally, we should set the timeout
4562 * relative to the accept() date. We just set the
4563 * request timeout once at the beginning of the
4564 * request.
4565 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004566 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004567 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004568 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004569 return 0;
4570 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004571
4572 http_end:
4573 /* The situation will not evolve, so let's give up on the analysis. */
4574 s->logs.tv_request = now; /* update the request timer to reflect full request */
4575 req->analysers &= ~an_bit;
4576 req->analyse_exp = TICK_ETERNITY;
4577 return 1;
4578
4579 return_bad_req: /* let's centralize all bad requests */
4580 txn->req.msg_state = HTTP_MSG_ERROR;
4581 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004582 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004583
Willy Tarreau79ebac62010-06-07 13:47:49 +02004584 if (!(s->flags & SN_ERR_MASK))
4585 s->flags |= SN_ERR_PRXCOND;
4586 if (!(s->flags & SN_FINST_MASK))
4587 s->flags |= SN_FINST_R;
4588
Willy Tarreau522d6c02009-12-06 18:49:18 +01004589 return_err_msg:
4590 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004591 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004592 if (s->listener->counters)
4593 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004594 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004595}
4596
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004597/* send a server's name with an outgoing request over an established connection.
4598 * Note: this function is designed to be called once the request has been scheduled
4599 * for being forwarded. This is the reason why it rewinds the buffer before
4600 * proceeding.
4601 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004602int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004603
4604 struct hdr_ctx ctx;
4605
Mark Lamourinec2247f02012-01-04 13:02:01 -05004606 char *hdr_name = be->server_id_hdr_name;
4607 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004608 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004609 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004610 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004611
William Lallemandd9e90662012-01-30 17:27:17 +01004612 ctx.idx = 0;
4613
Willy Tarreau211cdec2014-04-17 20:18:08 +02004614 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004615 if (old_o) {
4616 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004617 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004618 txn->req.next += old_o;
4619 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004620 }
4621
Willy Tarreau9b28e032012-10-12 23:49:43 +02004622 old_i = chn->buf->i;
4623 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 -05004624 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004625 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004626 }
4627
4628 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004629 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004630 memcpy(hdr_val, hdr_name, hdr_name_len);
4631 hdr_val += hdr_name_len;
4632 *hdr_val++ = ':';
4633 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004634 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4635 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004636
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004637 if (old_o) {
4638 /* If this was a forwarded request, we must readjust the amount of
4639 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004640 * variations. Note that the current state is >= HTTP_MSG_BODY,
4641 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004642 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004643 old_o += chn->buf->i - old_i;
4644 b_adv(chn->buf, old_o);
4645 txn->req.next -= old_o;
4646 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004647 }
4648
Mark Lamourinec2247f02012-01-04 13:02:01 -05004649 return 0;
4650}
4651
Willy Tarreau610ecce2010-01-04 21:15:02 +01004652/* Terminate current transaction and prepare a new one. This is very tricky
4653 * right now but it works.
4654 */
4655void http_end_txn_clean_session(struct session *s)
4656{
Willy Tarreau068621e2013-12-23 15:11:25 +01004657 int prev_status = s->txn.status;
4658
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659 /* FIXME: We need a more portable way of releasing a backend's and a
4660 * server's connections. We need a safer way to reinitialize buffer
4661 * flags. We also need a more accurate method for computing per-request
4662 * data.
4663 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004664
Willy Tarreau4213a112013-12-15 10:25:42 +01004665 /* unless we're doing keep-alive, we want to quickly close the connection
4666 * to the server.
4667 */
4668 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4669 !si_conn_ready(s->req->cons)) {
4670 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4671 si_shutr(s->req->cons);
4672 si_shutw(s->req->cons);
4673 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004674
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004675 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004677 if (unlikely(s->srv_conn))
4678 sess_change_server(s, NULL);
4679 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004680
4681 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4682 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004683 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684
4685 if (s->txn.status) {
4686 int n;
4687
4688 n = s->txn.status / 100;
4689 if (n < 1 || n > 5)
4690 n = 0;
4691
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004692 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004693 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004694 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004695 s->fe->fe_counters.p.http.comp_rsp++;
4696 }
Willy Tarreau24657792010-02-26 10:30:28 +01004697 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004698 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004699 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004700 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004701 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004702 s->be->be_counters.p.http.comp_rsp++;
4703 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004704 }
4705
4706 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004707 s->logs.bytes_in -= s->req->buf->i;
4708 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004709
4710 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004711 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004712 !(s->flags & SN_MONITOR) &&
4713 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4714 s->do_log(s);
4715 }
4716
4717 s->logs.accept_date = date; /* user-visible date for logging */
4718 s->logs.tv_accept = now; /* corrected date for internal use */
4719 tv_zero(&s->logs.tv_request);
4720 s->logs.t_queue = -1;
4721 s->logs.t_connect = -1;
4722 s->logs.t_data = -1;
4723 s->logs.t_close = 0;
4724 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4725 s->logs.srv_queue_size = 0; /* we will get this number soon */
4726
Willy Tarreau9b28e032012-10-12 23:49:43 +02004727 s->logs.bytes_in = s->req->total = s->req->buf->i;
4728 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729
4730 if (s->pend_pos)
4731 pendconn_free(s->pend_pos);
4732
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004733 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004734 if (s->flags & SN_CURR_SESS) {
4735 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004736 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004737 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004738 if (may_dequeue_tasks(objt_server(s->target), s->be))
4739 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004740 }
4741
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004742 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004743
Willy Tarreau4213a112013-12-15 10:25:42 +01004744 /* only release our endpoint if we don't intend to reuse the
4745 * connection.
4746 */
4747 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4748 !si_conn_ready(s->req->cons)) {
4749 si_release_endpoint(s->req->cons);
4750 }
4751
Willy Tarreau610ecce2010-01-04 21:15:02 +01004752 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004753 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004754 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004755 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004756 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004757 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4758 s->rep->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);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004759 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau36346242014-02-24 18:26:30 +01004760 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004761
Willy Tarreau610ecce2010-01-04 21:15:02 +01004762 s->txn.meth = 0;
4763 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004764 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004765
4766 if (prev_status == 401 || prev_status == 407) {
4767 /* In HTTP keep-alive mode, if we receive a 401, we still have
4768 * a chance of being able to send the visitor again to the same
4769 * server over the same connection. This is required by some
4770 * broken protocols such as NTLM, and anyway whenever there is
4771 * an opportunity for sending the challenge to the proper place,
4772 * it's better to do it (at least it helps with debugging).
4773 */
4774 s->txn.flags |= TX_PREFER_LAST;
4775 }
4776
Willy Tarreauee55dc02010-06-01 10:56:34 +02004777 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004778 s->req->cons->flags |= SI_FL_INDEP_STR;
4779
Willy Tarreau96e31212011-05-30 18:10:30 +02004780 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004781 s->req->flags |= CF_NEVER_WAIT;
4782 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004783 }
4784
Willy Tarreau610ecce2010-01-04 21:15:02 +01004785 /* if the request buffer is not empty, it means we're
4786 * about to process another request, so send pending
4787 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004788 * Just don't do this if the buffer is close to be full,
4789 * because the request will wait for it to flush a little
4790 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004791 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004792 if (s->req->buf->i) {
4793 if (s->rep->buf->o &&
4794 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4795 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004796 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004797 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004798
4799 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004800 channel_auto_read(s->req);
4801 channel_auto_close(s->req);
4802 channel_auto_read(s->rep);
4803 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004804
Willy Tarreau27375622013-12-17 00:00:28 +01004805 /* we're in keep-alive with an idle connection, monitor it */
4806 si_idle_conn(s->req->cons);
4807
Willy Tarreau342b11c2010-11-24 16:22:09 +01004808 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004809 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004810}
4811
4812
4813/* This function updates the request state machine according to the response
4814 * state machine and buffer flags. It returns 1 if it changes anything (flag
4815 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4816 * it is only used to find when a request/response couple is complete. Both
4817 * this function and its equivalent should loop until both return zero. It
4818 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4819 */
4820int http_sync_req_state(struct session *s)
4821{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004822 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004824 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004825 unsigned int old_state = txn->req.msg_state;
4826
Willy Tarreau610ecce2010-01-04 21:15:02 +01004827 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4828 return 0;
4829
4830 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004831 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004832 * We can shut the read side unless we want to abort_on_close,
4833 * or we have a POST request. The issue with POST requests is
4834 * that some browsers still send a CRLF after the request, and
4835 * this CRLF must be read so that it does not remain in the kernel
4836 * buffers, otherwise a close could cause an RST on some systems
4837 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004838 * Note that if we're using keep-alive on the client side, we'd
4839 * rather poll now and keep the polling enabled for the whole
4840 * session's life than enabling/disabling it between each
4841 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004842 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004843 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4844 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4845 !(s->be->options & PR_O_ABRT_CLOSE) &&
4846 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004847 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004848
Willy Tarreau40f151a2012-12-20 12:10:09 +01004849 /* if the server closes the connection, we want to immediately react
4850 * and close the socket to save packets and syscalls.
4851 */
4852 chn->cons->flags |= SI_FL_NOHALF;
4853
Willy Tarreau610ecce2010-01-04 21:15:02 +01004854 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4855 goto wait_other_side;
4856
4857 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4858 /* The server has not finished to respond, so we
4859 * don't want to move in order not to upset it.
4860 */
4861 goto wait_other_side;
4862 }
4863
4864 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4865 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004866 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004867 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004868 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869 goto wait_other_side;
4870 }
4871
4872 /* When we get here, it means that both the request and the
4873 * response have finished receiving. Depending on the connection
4874 * mode, we'll have to wait for the last bytes to leave in either
4875 * direction, and sometimes for a close to be effective.
4876 */
4877
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004878 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4879 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004880 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4881 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004882 }
4883 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4884 /* Option forceclose is set, or either side wants to close,
4885 * let's enforce it now that we're not expecting any new
4886 * data to come. The caller knows the session is complete
4887 * once both states are CLOSED.
4888 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004889 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4890 channel_shutr_now(chn);
4891 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004892 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004893 }
4894 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004895 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4896 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004897 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004898 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4899 channel_auto_read(chn);
4900 txn->req.msg_state = HTTP_MSG_TUNNEL;
4901 chn->flags |= CF_NEVER_WAIT;
4902 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903 }
4904
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004905 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004906 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004907 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004908
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004909 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004910 txn->req.msg_state = HTTP_MSG_CLOSING;
4911 goto http_msg_closing;
4912 }
4913 else {
4914 txn->req.msg_state = HTTP_MSG_CLOSED;
4915 goto http_msg_closed;
4916 }
4917 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004918 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004919 }
4920
4921 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4922 http_msg_closing:
4923 /* nothing else to forward, just waiting for the output buffer
4924 * to be empty and for the shutw_now to take effect.
4925 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004926 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004927 txn->req.msg_state = HTTP_MSG_CLOSED;
4928 goto http_msg_closed;
4929 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004930 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004931 txn->req.msg_state = HTTP_MSG_ERROR;
4932 goto wait_other_side;
4933 }
4934 }
4935
4936 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4937 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004938 /* see above in MSG_DONE why we only do this in these states */
4939 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4940 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4941 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004942 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004943 goto wait_other_side;
4944 }
4945
4946 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004947 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004948}
4949
4950
4951/* This function updates the response state machine according to the request
4952 * state machine and buffer flags. It returns 1 if it changes anything (flag
4953 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4954 * it is only used to find when a request/response couple is complete. Both
4955 * this function and its equivalent should loop until both return zero. It
4956 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4957 */
4958int http_sync_res_state(struct session *s)
4959{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004960 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004961 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004962 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963 unsigned int old_state = txn->rsp.msg_state;
4964
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4966 return 0;
4967
4968 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4969 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004970 * still monitor the server connection for a possible close
4971 * while the request is being uploaded, so we don't disable
4972 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004973 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004974 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004975
4976 if (txn->req.msg_state == HTTP_MSG_ERROR)
4977 goto wait_other_side;
4978
4979 if (txn->req.msg_state < HTTP_MSG_DONE) {
4980 /* The client seems to still be sending data, probably
4981 * because we got an error response during an upload.
4982 * We have the choice of either breaking the connection
4983 * or letting it pass through. Let's do the later.
4984 */
4985 goto wait_other_side;
4986 }
4987
4988 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4989 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004990 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004992 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993 goto wait_other_side;
4994 }
4995
4996 /* When we get here, it means that both the request and the
4997 * response have finished receiving. Depending on the connection
4998 * mode, we'll have to wait for the last bytes to leave in either
4999 * direction, and sometimes for a close to be effective.
5000 */
5001
5002 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5003 /* Server-close mode : shut read and wait for the request
5004 * side to close its output buffer. The caller will detect
5005 * when we're in DONE and the other is in CLOSED and will
5006 * catch that for the final cleanup.
5007 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005008 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5009 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005010 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005011 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5012 /* Option forceclose is set, or either side wants to close,
5013 * let's enforce it now that we're not expecting any new
5014 * data to come. The caller knows the session is complete
5015 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005016 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005017 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5018 channel_shutr_now(chn);
5019 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005020 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005021 }
5022 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005023 /* The last possible modes are keep-alive and tunnel. Tunnel will
5024 * need to forward remaining data. Keep-alive will need to monitor
5025 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005026 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005027 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005028 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005029 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5030 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005031 }
5032
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005033 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005034 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005035 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005036 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5037 goto http_msg_closing;
5038 }
5039 else {
5040 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5041 goto http_msg_closed;
5042 }
5043 }
5044 goto wait_other_side;
5045 }
5046
5047 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5048 http_msg_closing:
5049 /* nothing else to forward, just waiting for the output buffer
5050 * to be empty and for the shutw_now to take effect.
5051 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005052 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005053 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5054 goto http_msg_closed;
5055 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005056 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005057 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005058 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005059 if (objt_server(s->target))
5060 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005061 goto wait_other_side;
5062 }
5063 }
5064
5065 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5066 http_msg_closed:
5067 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005068 bi_erase(chn);
5069 channel_auto_close(chn);
5070 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005071 goto wait_other_side;
5072 }
5073
5074 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005075 /* We force the response to leave immediately if we're waiting for the
5076 * other side, since there is no pending shutdown to push it out.
5077 */
5078 if (!channel_is_empty(chn))
5079 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005080 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005081}
5082
5083
5084/* Resync the request and response state machines. Return 1 if either state
5085 * changes.
5086 */
5087int http_resync_states(struct session *s)
5088{
5089 struct http_txn *txn = &s->txn;
5090 int old_req_state = txn->req.msg_state;
5091 int old_res_state = txn->rsp.msg_state;
5092
Willy Tarreau610ecce2010-01-04 21:15:02 +01005093 http_sync_req_state(s);
5094 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 if (!http_sync_res_state(s))
5096 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097 if (!http_sync_req_state(s))
5098 break;
5099 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005100
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101 /* OK, both state machines agree on a compatible state.
5102 * There are a few cases we're interested in :
5103 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5104 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5105 * directions, so let's simply disable both analysers.
5106 * - HTTP_MSG_CLOSED on the response only means we must abort the
5107 * request.
5108 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5109 * with server-close mode means we've completed one request and we
5110 * must re-initialize the server connection.
5111 */
5112
5113 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5114 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5115 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5116 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5117 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005118 channel_auto_close(s->req);
5119 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005121 channel_auto_close(s->rep);
5122 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005123 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005124 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5125 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005126 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005127 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005128 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005129 channel_auto_close(s->rep);
5130 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005131 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005132 channel_abort(s->req);
5133 channel_auto_close(s->req);
5134 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005135 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005136 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005137 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5138 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005140 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5141 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5142 /* server-close/keep-alive: terminate this transaction,
5143 * possibly killing the server connection and reinitialize
5144 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005145 */
5146 http_end_txn_clean_session(s);
5147 }
5148
Willy Tarreau610ecce2010-01-04 21:15:02 +01005149 return txn->req.msg_state != old_req_state ||
5150 txn->rsp.msg_state != old_res_state;
5151}
5152
Willy Tarreaud98cf932009-12-27 22:54:55 +01005153/* This function is an analyser which forwards request body (including chunk
5154 * sizes if any). It is called as soon as we must forward, even if we forward
5155 * zero byte. The only situation where it must not be called is when we're in
5156 * tunnel mode and we want to forward till the close. It's used both to forward
5157 * remaining data and to resync after end of body. It expects the msg_state to
5158 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5159 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005160 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005161 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005162 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005163int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005164{
5165 struct http_txn *txn = &s->txn;
5166 struct http_msg *msg = &s->txn.req;
5167
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005168 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5169 return 0;
5170
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005171 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005172 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005173 /* Output closed while we were sending data. We must abort and
5174 * wake the other side up.
5175 */
5176 msg->msg_state = HTTP_MSG_ERROR;
5177 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005178 return 1;
5179 }
5180
Willy Tarreaud98cf932009-12-27 22:54:55 +01005181 /* Note that we don't have to send 100-continue back because we don't
5182 * need the data to complete our job, and it's up to the server to
5183 * decide whether to return 100, 417 or anything else in return of
5184 * an "Expect: 100-continue" header.
5185 */
5186
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005187 if (msg->sov) {
5188 /* we have msg->sov which points to the first byte of message
5189 * body, and req->buf.p still points to the beginning of the
5190 * message. We forward the headers now, as we don't need them
5191 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005192 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005193 b_adv(req->buf, msg->sov);
5194 msg->next -= msg->sov;
5195 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005196
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005197 /* The previous analysers guarantee that the state is somewhere
5198 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5199 * msg->next are always correct.
5200 */
5201 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5202 if (msg->flags & HTTP_MSGF_TE_CHNK)
5203 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5204 else
5205 msg->msg_state = HTTP_MSG_DATA;
5206 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005207 }
5208
Willy Tarreau7ba23542014-04-17 21:50:00 +02005209 /* Some post-connect processing might want us to refrain from starting to
5210 * forward data. Currently, the only reason for this is "balance url_param"
5211 * whichs need to parse/process the request after we've enabled forwarding.
5212 */
5213 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5214 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5215 channel_auto_connect(req);
5216 goto missing_data;
5217 }
5218 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5219 }
5220
Willy Tarreau80a92c02014-03-12 10:41:13 +01005221 /* in most states, we should abort in case of early close */
5222 channel_auto_close(req);
5223
Willy Tarreauefdf0942014-04-24 20:08:57 +02005224 if (req->to_forward) {
5225 /* We can't process the buffer's contents yet */
5226 req->flags |= CF_WAKE_WRITE;
5227 goto missing_data;
5228 }
5229
Willy Tarreaud98cf932009-12-27 22:54:55 +01005230 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005231 if (msg->msg_state == HTTP_MSG_DATA) {
5232 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005233 /* we may have some pending data starting at req->buf->p */
5234 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005235 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005236 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005237 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005238 msg->next += msg->chunk_len;
5239 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005240
5241 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005242 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005243 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005245 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005246 }
5247 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005248 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005249 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005250 * TRAILERS state.
5251 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005252 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005253
Willy Tarreau54d23df2012-10-25 19:04:45 +02005254 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005255 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005256 else if (ret < 0) {
5257 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005258 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005259 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005260 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005261 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005262 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005263 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005264 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005265 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005266 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005267
5268 if (ret == 0)
5269 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005270 else if (ret < 0) {
5271 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005272 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005273 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005274 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005275 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005276 /* we're in MSG_CHUNK_SIZE now */
5277 }
5278 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005279 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005280
5281 if (ret == 0)
5282 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005283 else if (ret < 0) {
5284 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005285 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005286 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005287 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005288 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005289 /* we're in HTTP_MSG_DONE now */
5290 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005291 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005292 int old_state = msg->msg_state;
5293
Willy Tarreau610ecce2010-01-04 21:15:02 +01005294 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005295
5296 /* we may have some pending data starting at req->buf->p
5297 * such as last chunk of data or trailers.
5298 */
5299 b_adv(req->buf, msg->next);
5300 msg->next = 0;
5301
Willy Tarreau4fe41902010-06-07 22:27:41 +02005302 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005303 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5304 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005305 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005306 if (http_resync_states(s)) {
5307 /* some state changes occurred, maybe the analyser
5308 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005309 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005310 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005311 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005312 /* request errors are most likely due to
5313 * the server aborting the transfer.
5314 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005315 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005316 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005317 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005318 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005320 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005321 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005322 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005323
5324 /* If "option abortonclose" is set on the backend, we
5325 * want to monitor the client's connection and forward
5326 * any shutdown notification to the server, which will
5327 * decide whether to close or to go on processing the
5328 * request.
5329 */
5330 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005331 channel_auto_read(req);
5332 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005333 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005334 else if (s->txn.meth == HTTP_METH_POST) {
5335 /* POST requests may require to read extra CRLF
5336 * sent by broken browsers and which could cause
5337 * an RST to be sent upon close on some systems
5338 * (eg: Linux).
5339 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005340 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005341 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005342
Willy Tarreau610ecce2010-01-04 21:15:02 +01005343 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005344 }
5345 }
5346
Willy Tarreaud98cf932009-12-27 22:54:55 +01005347 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005348 /* we may have some pending data starting at req->buf->p */
5349 b_adv(req->buf, msg->next);
5350 msg->next = 0;
5351 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5352
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005353 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005354 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005355 if (!(s->flags & SN_ERR_MASK))
5356 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005357 if (!(s->flags & SN_FINST_MASK)) {
5358 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5359 s->flags |= SN_FINST_H;
5360 else
5361 s->flags |= SN_FINST_D;
5362 }
5363
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005364 s->fe->fe_counters.cli_aborts++;
5365 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005366 if (objt_server(s->target))
5367 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005368
5369 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005370 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005371
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005372 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005373 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005374 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005375
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005376 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005377 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005378 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005379 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005380 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005381
Willy Tarreau5c620922011-05-11 19:56:11 +02005382 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005383 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005384 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005385 * modes are already handled by the stream sock layer. We must not do
5386 * this in content-length mode because it could present the MSG_MORE
5387 * flag with the last block of forwarded data, which would cause an
5388 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005389 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005390 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005391 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005392
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005393 return 0;
5394
Willy Tarreaud98cf932009-12-27 22:54:55 +01005395 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005396 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005397 if (s->listener->counters)
5398 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005399
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005400 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005401 /* we may have some pending data starting at req->buf->p */
5402 b_adv(req->buf, msg->next);
5403 msg->next = 0;
5404
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005405 txn->req.msg_state = HTTP_MSG_ERROR;
5406 if (txn->status) {
5407 /* Note: we don't send any error if some data were already sent */
5408 stream_int_retnclose(req->prod, NULL);
5409 } else {
5410 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005411 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005412 }
5413 req->analysers = 0;
5414 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415
5416 if (!(s->flags & SN_ERR_MASK))
5417 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005418 if (!(s->flags & SN_FINST_MASK)) {
5419 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5420 s->flags |= SN_FINST_H;
5421 else
5422 s->flags |= SN_FINST_D;
5423 }
5424 return 0;
5425
5426 aborted_xfer:
5427 txn->req.msg_state = HTTP_MSG_ERROR;
5428 if (txn->status) {
5429 /* Note: we don't send any error if some data were already sent */
5430 stream_int_retnclose(req->prod, NULL);
5431 } else {
5432 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005433 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005434 }
5435 req->analysers = 0;
5436 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5437
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005438 s->fe->fe_counters.srv_aborts++;
5439 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005440 if (objt_server(s->target))
5441 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005442
5443 if (!(s->flags & SN_ERR_MASK))
5444 s->flags |= SN_ERR_SRVCL;
5445 if (!(s->flags & SN_FINST_MASK)) {
5446 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5447 s->flags |= SN_FINST_H;
5448 else
5449 s->flags |= SN_FINST_D;
5450 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005451 return 0;
5452}
5453
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005454/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5455 * processing can continue on next analysers, or zero if it either needs more
5456 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5457 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5458 * when it has nothing left to do, and may remove any analyser when it wants to
5459 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005460 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005461int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005462{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005463 struct http_txn *txn = &s->txn;
5464 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005465 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005466 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005467 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005468 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005469
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005470 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005471 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005472 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005473 rep,
5474 rep->rex, rep->wex,
5475 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005476 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005477 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005478
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005479 /*
5480 * Now parse the partial (or complete) lines.
5481 * We will check the response syntax, and also join multi-line
5482 * headers. An index of all the lines will be elaborated while
5483 * parsing.
5484 *
5485 * For the parsing, we use a 28 states FSM.
5486 *
5487 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005488 * rep->buf->p = beginning of response
5489 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5490 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005491 * msg->eol = end of current header or line (LF or CRLF)
5492 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005493 */
5494
Willy Tarreau628c40c2014-04-24 19:11:26 +02005495 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005496 /* There's a protected area at the end of the buffer for rewriting
5497 * purposes. We don't want to start to parse the request if the
5498 * protected area is affected, because we may have to move processed
5499 * data later, which is much more complicated.
5500 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005501 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005502 if (unlikely(!channel_reserved(rep))) {
5503 /* some data has still not left the buffer, wake us once that's done */
5504 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5505 goto abort_response;
5506 channel_dont_close(rep);
5507 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005508 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005509 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005510 }
5511
Willy Tarreau379357a2013-06-08 12:55:46 +02005512 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5513 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5514 buffer_slow_realign(rep->buf);
5515
Willy Tarreau9b28e032012-10-12 23:49:43 +02005516 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005517 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005518 }
5519
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005520 /* 1: we might have to print this header in debug mode */
5521 if (unlikely((global.mode & MODE_DEBUG) &&
5522 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005523 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005524 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005525
Willy Tarreau9b28e032012-10-12 23:49:43 +02005526 sol = rep->buf->p;
5527 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005528 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005529
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005530 sol += hdr_idx_first_pos(&txn->hdr_idx);
5531 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005532
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005533 while (cur_idx) {
5534 eol = sol + txn->hdr_idx.v[cur_idx].len;
5535 debug_hdr("srvhdr", s, sol, eol);
5536 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5537 cur_idx = txn->hdr_idx.v[cur_idx].next;
5538 }
5539 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005540
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005541 /*
5542 * Now we quickly check if we have found a full valid response.
5543 * If not so, we check the FD and buffer states before leaving.
5544 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005545 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005546 * responses are checked first.
5547 *
5548 * Depending on whether the client is still there or not, we
5549 * may send an error response back or not. Note that normally
5550 * we should only check for HTTP status there, and check I/O
5551 * errors somewhere else.
5552 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005553
Willy Tarreau655dce92009-11-08 13:10:58 +01005554 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005555 /* Invalid response */
5556 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5557 /* we detected a parsing error. We want to archive this response
5558 * in the dedicated proxy area for later troubleshooting.
5559 */
5560 hdr_response_bad:
5561 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005562 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005563
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005564 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005565 if (objt_server(s->target)) {
5566 objt_server(s->target)->counters.failed_resp++;
5567 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005568 }
Willy Tarreau64648412010-03-05 10:41:54 +01005569 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005570 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005571 rep->analysers = 0;
5572 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005573 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005574 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005575 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005576
5577 if (!(s->flags & SN_ERR_MASK))
5578 s->flags |= SN_ERR_PRXCOND;
5579 if (!(s->flags & SN_FINST_MASK))
5580 s->flags |= SN_FINST_H;
5581
5582 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005583 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005584
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005585 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005586 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005587 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005588 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005589 goto hdr_response_bad;
5590 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005591
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005592 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005593 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005594 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005595 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005596 else if (txn->flags & TX_NOT_FIRST)
5597 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005598
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005599 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005600 if (objt_server(s->target)) {
5601 objt_server(s->target)->counters.failed_resp++;
5602 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005603 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005604
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005605 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005606 rep->analysers = 0;
5607 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005608 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005609 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005610 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005611
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005612 if (!(s->flags & SN_ERR_MASK))
5613 s->flags |= SN_ERR_SRVCL;
5614 if (!(s->flags & SN_FINST_MASK))
5615 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005616 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005617 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005618
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005619 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005620 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005621 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005622 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005623 else if (txn->flags & TX_NOT_FIRST)
5624 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005625
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005626 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005627 if (objt_server(s->target)) {
5628 objt_server(s->target)->counters.failed_resp++;
5629 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005630 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005631
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005632 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005633 rep->analysers = 0;
5634 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005635 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005636 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005637 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005638
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005639 if (!(s->flags & SN_ERR_MASK))
5640 s->flags |= SN_ERR_SRVTO;
5641 if (!(s->flags & SN_FINST_MASK))
5642 s->flags |= SN_FINST_H;
5643 return 0;
5644 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005645
Willy Tarreauf003d372012-11-26 13:35:37 +01005646 /* client abort with an abortonclose */
5647 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5648 s->fe->fe_counters.cli_aborts++;
5649 s->be->be_counters.cli_aborts++;
5650 if (objt_server(s->target))
5651 objt_server(s->target)->counters.cli_aborts++;
5652
5653 rep->analysers = 0;
5654 channel_auto_close(rep);
5655
5656 txn->status = 400;
5657 bi_erase(rep);
5658 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5659
5660 if (!(s->flags & SN_ERR_MASK))
5661 s->flags |= SN_ERR_CLICL;
5662 if (!(s->flags & SN_FINST_MASK))
5663 s->flags |= SN_FINST_H;
5664
5665 /* process_session() will take care of the error */
5666 return 0;
5667 }
5668
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005669 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005670 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005671 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005672 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005673 else if (txn->flags & TX_NOT_FIRST)
5674 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005675
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005676 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005677 if (objt_server(s->target)) {
5678 objt_server(s->target)->counters.failed_resp++;
5679 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005680 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005681
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005682 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005683 rep->analysers = 0;
5684 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005685 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005686 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005687 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005688
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005689 if (!(s->flags & SN_ERR_MASK))
5690 s->flags |= SN_ERR_SRVCL;
5691 if (!(s->flags & SN_FINST_MASK))
5692 s->flags |= SN_FINST_H;
5693 return 0;
5694 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005695
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005696 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005697 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005698 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005699 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005700 else if (txn->flags & TX_NOT_FIRST)
5701 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005702
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005703 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005704 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005705 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005706
5707 if (!(s->flags & SN_ERR_MASK))
5708 s->flags |= SN_ERR_CLICL;
5709 if (!(s->flags & SN_FINST_MASK))
5710 s->flags |= SN_FINST_H;
5711
5712 /* process_session() will take care of the error */
5713 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005714 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005715
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005716 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005717 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005718 return 0;
5719 }
5720
5721 /* More interesting part now : we know that we have a complete
5722 * response which at least looks like HTTP. We have an indicator
5723 * of each header's length, so we can parse them quickly.
5724 */
5725
5726 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005727 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005728
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005729 /*
5730 * 1: get the status code
5731 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005732 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005733 if (n < 1 || n > 5)
5734 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005735 /* when the client triggers a 4xx from the server, it's most often due
5736 * to a missing object or permission. These events should be tracked
5737 * because if they happen often, it may indicate a brute force or a
5738 * vulnerability scan.
5739 */
5740 if (n == 4)
5741 session_inc_http_err_ctr(s);
5742
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005743 if (objt_server(s->target))
5744 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005745
Willy Tarreau5b154472009-12-21 20:11:07 +01005746 /* check if the response is HTTP/1.1 or above */
5747 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005748 ((rep->buf->p[5] > '1') ||
5749 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005750 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005751
5752 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005753 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 +01005754
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005755 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005756 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005757
Willy Tarreau9b28e032012-10-12 23:49:43 +02005758 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005759
Willy Tarreau39650402010-03-15 19:44:39 +01005760 /* Adjust server's health based on status code. Note: status codes 501
5761 * and 505 are triggered on demand by client request, so we must not
5762 * count them as server failures.
5763 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005764 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005765 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005766 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005767 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005768 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005769 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005770
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005771 /*
5772 * 2: check for cacheability.
5773 */
5774
5775 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005776 case 100:
5777 /*
5778 * We may be facing a 100-continue response, in which case this
5779 * is not the right response, and we're waiting for the next one.
5780 * Let's allow this response to go to the client and wait for the
5781 * next one.
5782 */
5783 hdr_idx_init(&txn->hdr_idx);
5784 msg->next -= channel_forward(rep, msg->next);
5785 msg->msg_state = HTTP_MSG_RPBEFORE;
5786 txn->status = 0;
5787 s->logs.t_data = -1; /* was not a response yet */
5788 goto next_one;
5789
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005790 case 200:
5791 case 203:
5792 case 206:
5793 case 300:
5794 case 301:
5795 case 410:
5796 /* RFC2616 @13.4:
5797 * "A response received with a status code of
5798 * 200, 203, 206, 300, 301 or 410 MAY be stored
5799 * by a cache (...) unless a cache-control
5800 * directive prohibits caching."
5801 *
5802 * RFC2616 @9.5: POST method :
5803 * "Responses to this method are not cacheable,
5804 * unless the response includes appropriate
5805 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005806 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005807 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005808 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005809 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5810 break;
5811 default:
5812 break;
5813 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005814
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005815 /*
5816 * 3: we may need to capture headers
5817 */
5818 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005819 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005820 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005821 txn->rsp.cap, s->fe->rsp_cap);
5822
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005823 /* 4: determine the transfer-length.
5824 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5825 * the presence of a message-body in a RESPONSE and its transfer length
5826 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005827 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005828 * All responses to the HEAD request method MUST NOT include a
5829 * message-body, even though the presence of entity-header fields
5830 * might lead one to believe they do. All 1xx (informational), 204
5831 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5832 * message-body. All other responses do include a message-body,
5833 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005834 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005835 * 1. Any response which "MUST NOT" include a message-body (such as the
5836 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5837 * always terminated by the first empty line after the header fields,
5838 * regardless of the entity-header fields present in the message.
5839 *
5840 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5841 * the "chunked" transfer-coding (Section 6.2) is used, the
5842 * transfer-length is defined by the use of this transfer-coding.
5843 * If a Transfer-Encoding header field is present and the "chunked"
5844 * transfer-coding is not present, the transfer-length is defined by
5845 * the sender closing the connection.
5846 *
5847 * 3. If a Content-Length header field is present, its decimal value in
5848 * OCTETs represents both the entity-length and the transfer-length.
5849 * If a message is received with both a Transfer-Encoding header
5850 * field and a Content-Length header field, the latter MUST be ignored.
5851 *
5852 * 4. If the message uses the media type "multipart/byteranges", and
5853 * the transfer-length is not otherwise specified, then this self-
5854 * delimiting media type defines the transfer-length. This media
5855 * type MUST NOT be used unless the sender knows that the recipient
5856 * can parse it; the presence in a request of a Range header with
5857 * multiple byte-range specifiers from a 1.1 client implies that the
5858 * client can parse multipart/byteranges responses.
5859 *
5860 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005861 */
5862
5863 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005864 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005865 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005866 * FIXME: should we parse anyway and return an error on chunked encoding ?
5867 */
5868 if (txn->meth == HTTP_METH_HEAD ||
5869 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005870 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005871 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005872 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005873 goto skip_content_length;
5874 }
5875
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005876 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005877 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005878 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005879 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005880 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005881 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5882 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005883 /* bad transfer-encoding (chunked followed by something else) */
5884 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005885 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005886 break;
5887 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005888 }
5889
5890 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5891 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005892 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005893 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005894 signed long long cl;
5895
Willy Tarreauad14f752011-09-02 20:33:27 +02005896 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005897 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005898 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005899 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005900
Willy Tarreauad14f752011-09-02 20:33:27 +02005901 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005902 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005903 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005904 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005905
Willy Tarreauad14f752011-09-02 20:33:27 +02005906 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005907 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005908 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005909 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005910
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005911 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005912 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005913 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005914 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005915
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005916 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005917 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005918 }
5919
William Lallemand82fe75c2012-10-23 10:25:10 +02005920 if (s->fe->comp || s->be->comp)
5921 select_compression_response_header(s, rep->buf);
5922
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005923skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005924 /* Now we have to check if we need to modify the Connection header.
5925 * This is more difficult on the response than it is on the request,
5926 * because we can have two different HTTP versions and we don't know
5927 * how the client will interprete a response. For instance, let's say
5928 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5929 * HTTP/1.1 response without any header. Maybe it will bound itself to
5930 * HTTP/1.0 because it only knows about it, and will consider the lack
5931 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5932 * the lack of header as a keep-alive. Thus we will use two flags
5933 * indicating how a request MAY be understood by the client. In case
5934 * of multiple possibilities, we'll fix the header to be explicit. If
5935 * ambiguous cases such as both close and keepalive are seen, then we
5936 * will fall back to explicit close. Note that we won't take risks with
5937 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005938 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005939 */
5940
Willy Tarreaudc008c52010-02-01 16:20:08 +01005941 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5942 txn->status == 101)) {
5943 /* Either we've established an explicit tunnel, or we're
5944 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005945 * to understand the next protocols. We have to switch to tunnel
5946 * mode, so that we transfer the request and responses then let
5947 * this protocol pass unmodified. When we later implement specific
5948 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005949 * header which contains information about that protocol for
5950 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005951 */
5952 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5953 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005954 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5955 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005956 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5957 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005958 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005959
Willy Tarreau70dffda2014-01-30 03:07:23 +01005960 /* this situation happens when combining pretend-keepalive with httpclose. */
5961 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005962 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5963 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005964 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5965
Willy Tarreau60466522010-01-18 19:08:45 +01005966 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005967 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005968 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5969 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005970
Willy Tarreau60466522010-01-18 19:08:45 +01005971 /* now adjust header transformations depending on current state */
5972 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5973 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5974 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005975 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005976 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005977 }
Willy Tarreau60466522010-01-18 19:08:45 +01005978 else { /* SCL / KAL */
5979 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005980 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005981 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005982 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005983
Willy Tarreau60466522010-01-18 19:08:45 +01005984 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005985 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005986
Willy Tarreau60466522010-01-18 19:08:45 +01005987 /* Some keep-alive responses are converted to Server-close if
5988 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005989 */
Willy Tarreau60466522010-01-18 19:08:45 +01005990 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5991 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005992 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005993 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005994 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005995 }
5996
Willy Tarreau7959a552013-09-23 16:44:27 +02005997 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005998 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005999
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006000 /* end of job, return OK */
6001 rep->analysers &= ~an_bit;
6002 rep->analyse_exp = TICK_ETERNITY;
6003 channel_auto_close(rep);
6004 return 1;
6005
6006 abort_keep_alive:
6007 /* A keep-alive request to the server failed on a network error.
6008 * The client is required to retry. We need to close without returning
6009 * any other information so that the client retries.
6010 */
6011 txn->status = 0;
6012 rep->analysers = 0;
6013 s->req->analysers = 0;
6014 channel_auto_close(rep);
6015 s->logs.logwait = 0;
6016 s->logs.level = 0;
6017 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6018 bi_erase(rep);
6019 stream_int_retnclose(rep->cons, NULL);
6020 return 0;
6021}
6022
6023/* This function performs all the processing enabled for the current response.
6024 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6025 * and updates s->rep->analysers. It might make sense to explode it into several
6026 * other functions. It works like process_request (see indications above).
6027 */
6028int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6029{
6030 struct http_txn *txn = &s->txn;
6031 struct http_msg *msg = &txn->rsp;
6032 struct proxy *cur_proxy;
6033 struct cond_wordlist *wl;
6034 struct http_res_rule *http_res_last_rule = NULL;
6035
6036 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6037 now_ms, __FUNCTION__,
6038 s,
6039 rep,
6040 rep->rex, rep->wex,
6041 rep->flags,
6042 rep->buf->i,
6043 rep->analysers);
6044
6045 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6046 return 0;
6047
6048 rep->analysers &= ~an_bit;
6049 rep->analyse_exp = TICK_ETERNITY;
6050
Willy Tarreau70730dd2014-04-24 18:06:27 +02006051 /* The stats applet needs to adjust the Connection header but we don't
6052 * apply any filter there.
6053 */
6054 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6055 goto skip_filters;
6056
Willy Tarreau58975672014-04-24 21:13:57 +02006057 /*
6058 * We will have to evaluate the filters.
6059 * As opposed to version 1.2, now they will be evaluated in the
6060 * filters order and not in the header order. This means that
6061 * each filter has to be validated among all headers.
6062 *
6063 * Filters are tried with ->be first, then with ->fe if it is
6064 * different from ->be.
6065 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006066
Willy Tarreau58975672014-04-24 21:13:57 +02006067 cur_proxy = s->be;
6068 while (1) {
6069 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006070
Willy Tarreau58975672014-04-24 21:13:57 +02006071 /* evaluate http-response rules */
6072 if (!http_res_last_rule)
6073 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s, txn);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006074
Willy Tarreau58975672014-04-24 21:13:57 +02006075 /* try headers filters */
6076 if (rule_set->rsp_exp != NULL) {
6077 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6078 return_bad_resp:
6079 if (objt_server(s->target)) {
6080 objt_server(s->target)->counters.failed_resp++;
6081 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006082 }
Willy Tarreau58975672014-04-24 21:13:57 +02006083 s->be->be_counters.failed_resp++;
6084 return_srv_prx_502:
6085 rep->analysers = 0;
6086 txn->status = 502;
6087 s->logs.t_data = -1; /* was not a valid response */
6088 rep->prod->flags |= SI_FL_NOLINGER;
6089 bi_erase(rep);
6090 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6091 if (!(s->flags & SN_ERR_MASK))
6092 s->flags |= SN_ERR_PRXCOND;
6093 if (!(s->flags & SN_FINST_MASK))
6094 s->flags |= SN_FINST_H;
6095 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006096 }
Willy Tarreau58975672014-04-24 21:13:57 +02006097 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006098
Willy Tarreau58975672014-04-24 21:13:57 +02006099 /* has the response been denied ? */
6100 if (txn->flags & TX_SVDENY) {
6101 if (objt_server(s->target))
6102 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006103
Willy Tarreau58975672014-04-24 21:13:57 +02006104 s->be->be_counters.denied_resp++;
6105 s->fe->fe_counters.denied_resp++;
6106 if (s->listener->counters)
6107 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006108
Willy Tarreau58975672014-04-24 21:13:57 +02006109 goto return_srv_prx_502;
6110 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006111
Willy Tarreau58975672014-04-24 21:13:57 +02006112 /* add response headers from the rule sets in the same order */
6113 list_for_each_entry(wl, &rule_set->rsp_add, list) {
6114 if (txn->status < 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006115 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006116 if (wl->cond) {
6117 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6118 ret = acl_pass(ret);
6119 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6120 ret = !ret;
6121 if (!ret)
6122 continue;
6123 }
6124 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6125 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006126 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006127
Willy Tarreau58975672014-04-24 21:13:57 +02006128 /* check whether we're already working on the frontend */
6129 if (cur_proxy == s->fe)
6130 break;
6131 cur_proxy = s->fe;
6132 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006133
Willy Tarreau58975672014-04-24 21:13:57 +02006134 /* OK that's all we can do for 1xx responses */
6135 if (unlikely(txn->status < 200))
6136 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006137
Willy Tarreau58975672014-04-24 21:13:57 +02006138 /*
6139 * Now check for a server cookie.
6140 */
6141 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6142 (s->be->options & PR_O_CHK_CACHE))
6143 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006144
Willy Tarreau58975672014-04-24 21:13:57 +02006145 /*
6146 * Check for cache-control or pragma headers if required.
6147 */
6148 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
6149 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006150
Willy Tarreau58975672014-04-24 21:13:57 +02006151 /*
6152 * Add server cookie in the response if needed
6153 */
6154 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6155 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6156 (!(s->flags & SN_DIRECT) ||
6157 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6158 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6159 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6160 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6161 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6162 !(s->flags & SN_IGNORE_PRST)) {
6163 /* the server is known, it's not the one the client requested, or the
6164 * cookie's last seen date needs to be refreshed. We have to
6165 * insert a set-cookie here, except if we want to insert only on POST
6166 * requests and this one isn't. Note that servers which don't have cookies
6167 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006168 */
Willy Tarreau58975672014-04-24 21:13:57 +02006169 if (!objt_server(s->target)->cookie) {
6170 chunk_printf(&trash,
6171 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6172 s->be->cookie_name);
6173 }
6174 else {
6175 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006176
Willy Tarreau58975672014-04-24 21:13:57 +02006177 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6178 /* emit last_date, which is mandatory */
6179 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6180 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6181 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006182
Willy Tarreau58975672014-04-24 21:13:57 +02006183 if (s->be->cookie_maxlife) {
6184 /* emit first_date, which is either the original one or
6185 * the current date.
6186 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006187 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006188 s30tob64(txn->cookie_first_date ?
6189 txn->cookie_first_date >> 2 :
6190 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006191 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006192 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006193 }
Willy Tarreau58975672014-04-24 21:13:57 +02006194 chunk_appendf(&trash, "; path=/");
6195 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006196
Willy Tarreau58975672014-04-24 21:13:57 +02006197 if (s->be->cookie_domain)
6198 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006199
Willy Tarreau58975672014-04-24 21:13:57 +02006200 if (s->be->ck_opts & PR_CK_HTTPONLY)
6201 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006202
Willy Tarreau58975672014-04-24 21:13:57 +02006203 if (s->be->ck_opts & PR_CK_SECURE)
6204 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006205
Willy Tarreau58975672014-04-24 21:13:57 +02006206 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6207 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006208
Willy Tarreau58975672014-04-24 21:13:57 +02006209 txn->flags &= ~TX_SCK_MASK;
6210 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6211 /* the server did not change, only the date was updated */
6212 txn->flags |= TX_SCK_UPDATED;
6213 else
6214 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006215
Willy Tarreau58975672014-04-24 21:13:57 +02006216 /* Here, we will tell an eventual cache on the client side that we don't
6217 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6218 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6219 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006220 */
Willy Tarreau58975672014-04-24 21:13:57 +02006221 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006222
Willy Tarreau58975672014-04-24 21:13:57 +02006223 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006224
Willy Tarreau58975672014-04-24 21:13:57 +02006225 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6226 "Cache-control: private", 22) < 0))
6227 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006228 }
Willy Tarreau58975672014-04-24 21:13:57 +02006229 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006230
Willy Tarreau58975672014-04-24 21:13:57 +02006231 /*
6232 * Check if result will be cacheable with a cookie.
6233 * We'll block the response if security checks have caught
6234 * nasty things such as a cacheable cookie.
6235 */
6236 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6237 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6238 (s->be->options & PR_O_CHK_CACHE)) {
6239 /* we're in presence of a cacheable response containing
6240 * a set-cookie header. We'll block it as requested by
6241 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006242 */
Willy Tarreau58975672014-04-24 21:13:57 +02006243 if (objt_server(s->target))
6244 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006245
Willy Tarreau58975672014-04-24 21:13:57 +02006246 s->be->be_counters.denied_resp++;
6247 s->fe->fe_counters.denied_resp++;
6248 if (s->listener->counters)
6249 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006250
Willy Tarreau58975672014-04-24 21:13:57 +02006251 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6252 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6253 send_log(s->be, LOG_ALERT,
6254 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6255 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6256 goto return_srv_prx_502;
6257 }
Willy Tarreau03945942009-12-22 16:50:27 +01006258
Willy Tarreau70730dd2014-04-24 18:06:27 +02006259 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006260 /*
6261 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6262 * If an "Upgrade" token is found, the header is left untouched in order
6263 * not to have to deal with some client bugs : some of them fail an upgrade
6264 * if anything but "Upgrade" is present in the Connection header.
6265 */
6266 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6267 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6268 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6269 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6270 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006271
Willy Tarreau58975672014-04-24 21:13:57 +02006272 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6273 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6274 /* we want a keep-alive response here. Keep-alive header
6275 * required if either side is not 1.1.
6276 */
6277 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6278 want_flags |= TX_CON_KAL_SET;
6279 }
6280 else {
6281 /* we want a close response here. Close header required if
6282 * the server is 1.1, regardless of the client.
6283 */
6284 if (msg->flags & HTTP_MSGF_VER_11)
6285 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006286 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006287
Willy Tarreau58975672014-04-24 21:13:57 +02006288 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6289 http_change_connection_header(txn, msg, want_flags);
6290 }
6291
6292 skip_header_mangling:
6293 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6294 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6295 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006296
Willy Tarreau58975672014-04-24 21:13:57 +02006297 /* if the user wants to log as soon as possible, without counting
6298 * bytes from the server, then this is the right moment. We have
6299 * to temporarily assign bytes_out to log what we currently have.
6300 */
6301 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6302 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6303 s->logs.bytes_out = txn->rsp.eoh;
6304 s->do_log(s);
6305 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006306 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006307 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006308}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006309
Willy Tarreaud98cf932009-12-27 22:54:55 +01006310/* This function is an analyser which forwards response body (including chunk
6311 * sizes if any). It is called as soon as we must forward, even if we forward
6312 * zero byte. The only situation where it must not be called is when we're in
6313 * tunnel mode and we want to forward till the close. It's used both to forward
6314 * remaining data and to resync after end of body. It expects the msg_state to
6315 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6316 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006317 *
6318 * It is capable of compressing response data both in content-length mode and
6319 * in chunked mode. The state machines follows different flows depending on
6320 * whether content-length and chunked modes are used, since there are no
6321 * trailers in content-length :
6322 *
6323 * chk-mode cl-mode
6324 * ,----- BODY -----.
6325 * / \
6326 * V size > 0 V chk-mode
6327 * .--> SIZE -------------> DATA -------------> CRLF
6328 * | | size == 0 | last byte |
6329 * | v final crlf v inspected |
6330 * | TRAILERS -----------> DONE |
6331 * | |
6332 * `----------------------------------------------'
6333 *
6334 * Compression only happens in the DATA state, and must be flushed in final
6335 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6336 * is performed at once on final states for all bytes parsed, or when leaving
6337 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006338 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006339int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006340{
6341 struct http_txn *txn = &s->txn;
6342 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006343 static struct buffer *tmpbuf = NULL;
6344 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006345 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006346
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006347 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6348 return 0;
6349
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006350 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006351 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006352 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006353 /* Output closed while we were sending data. We must abort and
6354 * wake the other side up.
6355 */
6356 msg->msg_state = HTTP_MSG_ERROR;
6357 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006358 return 1;
6359 }
6360
Willy Tarreau4fe41902010-06-07 22:27:41 +02006361 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006362 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006363
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006364 if (msg->sov) {
6365 /* we have msg->sov which points to the first byte of message
6366 * body, and res->buf.p still points to the beginning of the
6367 * message. We forward the headers now, as we don't need them
6368 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006369 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006370 b_adv(res->buf, msg->sov);
6371 msg->next -= msg->sov;
6372 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006373
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006374 /* The previous analysers guarantee that the state is somewhere
6375 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6376 * msg->next are always correct.
6377 */
6378 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6379 if (msg->flags & HTTP_MSGF_TE_CHNK)
6380 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6381 else
6382 msg->msg_state = HTTP_MSG_DATA;
6383 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006384 }
6385
Willy Tarreauefdf0942014-04-24 20:08:57 +02006386 if (res->to_forward) {
6387 /* We can't process the buffer's contents yet */
6388 res->flags |= CF_WAKE_WRITE;
6389 goto missing_data;
6390 }
6391
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006392 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6393 /* We need a compression buffer in the DATA state to put the
6394 * output of compressed data, and in CRLF state to let the
6395 * TRAILERS state finish the job of removing the trailing CRLF.
6396 */
6397 if (unlikely(tmpbuf == NULL)) {
6398 /* this is the first time we need the compression buffer */
6399 tmpbuf = pool_alloc2(pool2_buffer);
6400 if (tmpbuf == NULL)
6401 goto aborted_xfer; /* no memory */
6402 }
6403
6404 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006405 if (ret < 0) {
6406 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006407 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006408 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006409 compressing = 1;
6410 }
6411
Willy Tarreaud98cf932009-12-27 22:54:55 +01006412 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006413 switch (msg->msg_state - HTTP_MSG_DATA) {
6414 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006415 /* we may have some pending data starting at res->buf->p */
6416 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006417 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006418 if (ret < 0)
6419 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006420
Willy Tarreaud5a67832014-04-21 10:54:27 +02006421 if (msg->chunk_len) {
6422 /* input empty or output full */
6423 if (res->buf->i > msg->next)
6424 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006425 goto missing_data;
6426 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006427 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006428 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006429 if (msg->chunk_len > res->buf->i - msg->next) {
6430 /* output full */
6431 res->flags |= CF_WAKE_WRITE;
6432 goto missing_data;
6433 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006434 msg->next += msg->chunk_len;
6435 msg->chunk_len = 0;
6436 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006437
6438 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006439 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006440 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006441 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006442 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006443 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006444 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006445 /* fall through for HTTP_MSG_CHUNK_CRLF */
6446
6447 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6448 /* we want the CRLF after the data */
6449
6450 ret = http_skip_chunk_crlf(msg);
6451 if (ret == 0)
6452 goto missing_data;
6453 else if (ret < 0) {
6454 if (msg->err_pos >= 0)
6455 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6456 goto return_bad_res;
6457 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006458 /* we're in MSG_CHUNK_SIZE now, fall through */
6459
6460 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006461 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006462 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006463 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006464 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006465
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006466 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006467 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006468 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006469 else if (ret < 0) {
6470 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006471 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006472 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006473 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006474 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006475 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006476
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006477 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006478 if (unlikely(compressing)) {
6479 /* we need to flush output contents before syncing FSMs */
6480 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6481 compressing = 0;
6482 }
6483
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006484 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006485 if (ret == 0)
6486 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006487 else if (ret < 0) {
6488 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006489 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006490 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006491 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006492 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006493
6494 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006495 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006496 if (unlikely(compressing)) {
6497 /* we need to flush output contents before syncing FSMs */
6498 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6499 compressing = 0;
6500 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006501
Willy Tarreauc623c172014-04-18 09:53:50 +02006502 /* we may have some pending data starting at res->buf->p
6503 * such as a last chunk of data or trailers.
6504 */
6505 b_adv(res->buf, msg->next);
6506 msg->next = 0;
6507
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006508 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006509 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006510 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6511 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006512 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006513
Willy Tarreau610ecce2010-01-04 21:15:02 +01006514 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006515 /* some state changes occurred, maybe the analyser
6516 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006517 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006518 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006519 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006520 /* response errors are most likely due to
6521 * the client aborting the transfer.
6522 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006523 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006524 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006525 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006526 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006527 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006528 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006529 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006530 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006531 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006532 }
6533 }
6534
Willy Tarreaud98cf932009-12-27 22:54:55 +01006535 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006536 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006537 if (unlikely(compressing)) {
6538 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006539 compressing = 0;
6540 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006541
Willy Tarreauc623c172014-04-18 09:53:50 +02006542 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6543 b_adv(res->buf, msg->next);
6544 msg->next = 0;
6545 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6546 }
6547
Willy Tarreauf003d372012-11-26 13:35:37 +01006548 if (res->flags & CF_SHUTW)
6549 goto aborted_xfer;
6550
6551 /* stop waiting for data if the input is closed before the end. If the
6552 * client side was already closed, it means that the client has aborted,
6553 * so we don't want to count this as a server abort. Otherwise it's a
6554 * server abort.
6555 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006556 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006557 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006558 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006559 if (!(s->flags & SN_ERR_MASK))
6560 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006561 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006562 if (objt_server(s->target))
6563 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006564 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006565 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006566
Willy Tarreau40dba092010-03-04 18:14:51 +01006567 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006568 if (!s->req->analysers)
6569 goto return_bad_res;
6570
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006571 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006572 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006573 * Similarly, with keep-alive on the client side, we don't want to forward a
6574 * close.
6575 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006576 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006577 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6578 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006579 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006580
Willy Tarreau5c620922011-05-11 19:56:11 +02006581 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006582 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006583 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006584 * modes are already handled by the stream sock layer. We must not do
6585 * this in content-length mode because it could present the MSG_MORE
6586 * flag with the last block of forwarded data, which would cause an
6587 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006588 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006589 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006590 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006591
Willy Tarreaud98cf932009-12-27 22:54:55 +01006592 /* the session handler will take care of timeouts and errors */
6593 return 0;
6594
Willy Tarreau40dba092010-03-04 18:14:51 +01006595 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006596 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006597 if (objt_server(s->target))
6598 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006599
6600 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006601 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006602 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006603 compressing = 0;
6604 }
6605
Willy Tarreauc623c172014-04-18 09:53:50 +02006606 /* we may have some pending data starting at res->buf->p */
6607 if (s->comp_algo == NULL) {
6608 b_adv(res->buf, msg->next);
6609 msg->next = 0;
6610 }
6611
Willy Tarreaud98cf932009-12-27 22:54:55 +01006612 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006613 /* don't send any error message as we're in the body */
6614 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006615 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006616 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006617 if (objt_server(s->target))
6618 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006619
6620 if (!(s->flags & SN_ERR_MASK))
6621 s->flags |= SN_ERR_PRXCOND;
6622 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006623 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006624 return 0;
6625
6626 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006627 if (unlikely(compressing)) {
6628 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6629 compressing = 0;
6630 }
6631
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006632 txn->rsp.msg_state = HTTP_MSG_ERROR;
6633 /* don't send any error message as we're in the body */
6634 stream_int_retnclose(res->cons, NULL);
6635 res->analysers = 0;
6636 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6637
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006638 s->fe->fe_counters.cli_aborts++;
6639 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006640 if (objt_server(s->target))
6641 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006642
6643 if (!(s->flags & SN_ERR_MASK))
6644 s->flags |= SN_ERR_CLICL;
6645 if (!(s->flags & SN_FINST_MASK))
6646 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006647 return 0;
6648}
6649
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006650/* Iterate the same filter through all request headers.
6651 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652 * Since it can manage the switch to another backend, it updates the per-proxy
6653 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006655int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006656{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006657 char term;
6658 char *cur_ptr, *cur_end, *cur_next;
6659 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006660 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006661 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006662 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006664 last_hdr = 0;
6665
Willy Tarreau9b28e032012-10-12 23:49:43 +02006666 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006667 old_idx = 0;
6668
6669 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006670 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006671 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006672 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006673 (exp->action == ACT_ALLOW ||
6674 exp->action == ACT_DENY ||
6675 exp->action == ACT_TARPIT))
6676 return 0;
6677
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006678 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006679 if (!cur_idx)
6680 break;
6681
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006682 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006683 cur_ptr = cur_next;
6684 cur_end = cur_ptr + cur_hdr->len;
6685 cur_next = cur_end + cur_hdr->cr + 1;
6686
6687 /* Now we have one header between cur_ptr and cur_end,
6688 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006689 */
6690
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006691 /* The annoying part is that pattern matching needs
6692 * that we modify the contents to null-terminate all
6693 * strings before testing them.
6694 */
6695
6696 term = *cur_end;
6697 *cur_end = '\0';
6698
6699 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6700 switch (exp->action) {
6701 case ACT_SETBE:
6702 /* It is not possible to jump a second time.
6703 * FIXME: should we return an HTTP/500 here so that
6704 * the admin knows there's a problem ?
6705 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006706 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006707 break;
6708
6709 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006710 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006711 last_hdr = 1;
6712 break;
6713
6714 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006715 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006716 last_hdr = 1;
6717 break;
6718
6719 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006720 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006721 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006722 break;
6723
6724 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006725 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006726 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006727 break;
6728
6729 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006730 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6731 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006732 /* FIXME: if the user adds a newline in the replacement, the
6733 * index will not be recalculated for now, and the new line
6734 * will not be counted as a new header.
6735 */
6736
6737 cur_end += delta;
6738 cur_next += delta;
6739 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006740 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006741 break;
6742
6743 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006744 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006745 cur_next += delta;
6746
Willy Tarreaufa355d42009-11-29 18:12:29 +01006747 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006748 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6749 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006750 cur_hdr->len = 0;
6751 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006752 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006753 break;
6754
6755 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006757 if (cur_end)
6758 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006759
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006760 /* keep the link from this header to next one in case of later
6761 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006762 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006763 old_idx = cur_idx;
6764 }
6765 return 0;
6766}
6767
6768
6769/* Apply the filter to the request line.
6770 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6771 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772 * Since it can manage the switch to another backend, it updates the per-proxy
6773 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006774 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006775int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006776{
6777 char term;
6778 char *cur_ptr, *cur_end;
6779 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006780 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006781 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006782
Willy Tarreau3d300592007-03-18 18:34:41 +01006783 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006784 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006785 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006786 (exp->action == ACT_ALLOW ||
6787 exp->action == ACT_DENY ||
6788 exp->action == ACT_TARPIT))
6789 return 0;
6790 else if (exp->action == ACT_REMOVE)
6791 return 0;
6792
6793 done = 0;
6794
Willy Tarreau9b28e032012-10-12 23:49:43 +02006795 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006796 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006797
6798 /* Now we have the request line between cur_ptr and cur_end */
6799
6800 /* The annoying part is that pattern matching needs
6801 * that we modify the contents to null-terminate all
6802 * strings before testing them.
6803 */
6804
6805 term = *cur_end;
6806 *cur_end = '\0';
6807
6808 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6809 switch (exp->action) {
6810 case ACT_SETBE:
6811 /* It is not possible to jump a second time.
6812 * FIXME: should we return an HTTP/500 here so that
6813 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006814 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006815 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006816 break;
6817
6818 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006819 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006820 done = 1;
6821 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006822
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006823 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006824 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006825 done = 1;
6826 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006828 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006829 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006830 done = 1;
6831 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006832
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006833 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006834 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006835 done = 1;
6836 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006838 case ACT_REPLACE:
6839 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006840 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6841 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006842 /* FIXME: if the user adds a newline in the replacement, the
6843 * index will not be recalculated for now, and the new line
6844 * will not be counted as a new header.
6845 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006846
Willy Tarreaufa355d42009-11-29 18:12:29 +01006847 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006848 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006849 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006850 HTTP_MSG_RQMETH,
6851 cur_ptr, cur_end + 1,
6852 NULL, NULL);
6853 if (unlikely(!cur_end))
6854 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006855
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006856 /* we have a full request and we know that we have either a CR
6857 * or an LF at <ptr>.
6858 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006859 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6860 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006861 /* there is no point trying this regex on headers */
6862 return 1;
6863 }
6864 }
6865 *cur_end = term; /* restore the string terminator */
6866 return done;
6867}
Willy Tarreau97de6242006-12-27 17:18:38 +01006868
Willy Tarreau58f10d72006-12-04 02:26:12 +01006869
Willy Tarreau58f10d72006-12-04 02:26:12 +01006870
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006871/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006872 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006873 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006874 * unparsable request. Since it can manage the switch to another backend, it
6875 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006876 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006877int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006878{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006879 struct http_txn *txn = &s->txn;
6880 struct hdr_exp *exp;
6881
6882 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006883 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006884
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006885 /*
6886 * The interleaving of transformations and verdicts
6887 * makes it difficult to decide to continue or stop
6888 * the evaluation.
6889 */
6890
Willy Tarreau6c123b12010-01-28 20:22:06 +01006891 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6892 break;
6893
Willy Tarreau3d300592007-03-18 18:34:41 +01006894 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006895 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006896 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006897 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006898
6899 /* if this filter had a condition, evaluate it now and skip to
6900 * next filter if the condition does not match.
6901 */
6902 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006903 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006904 ret = acl_pass(ret);
6905 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6906 ret = !ret;
6907
6908 if (!ret)
6909 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006910 }
6911
6912 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006913 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006914 if (unlikely(ret < 0))
6915 return -1;
6916
6917 if (likely(ret == 0)) {
6918 /* The filter did not match the request, it can be
6919 * iterated through all headers.
6920 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006921 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006922 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006924 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925}
6926
6927
Willy Tarreaua15645d2007-03-18 16:22:39 +01006928
Willy Tarreau58f10d72006-12-04 02:26:12 +01006929/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006930 * Try to retrieve the server associated to the appsession.
6931 * If the server is found, it's assigned to the session.
6932 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006933void manage_client_side_appsession(struct session *s, const char *buf, int len) {
6934 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006935 appsess *asession = NULL;
6936 char *sessid_temp = NULL;
6937
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006938 if (len > s->be->appsession_len) {
6939 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006940 }
6941
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006942 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006943 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006944 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006945 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006946 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006947 }
6948
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006949 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006950 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006951 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006952 return;
6953 }
6954
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006955 memcpy(txn->sessid, buf, len);
6956 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006957 }
6958
6959 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6960 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006961 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006962 return;
6963 }
6964
Cyril Bontéb21570a2009-11-29 20:04:48 +01006965 memcpy(sessid_temp, buf, len);
6966 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006967
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006968 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006969 /* free previously allocated memory */
6970 pool_free2(apools.sessid, sessid_temp);
6971
6972 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006973 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
6974 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006975 asession->request_count++;
6976
6977 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006978 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006979
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006980 while (srv) {
6981 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006982 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006983 (s->be->options & PR_O_PERSIST) ||
6984 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006985 /* we found the server and it's usable */
6986 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006987 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006988 s->flags |= SN_DIRECT | SN_ASSIGNED;
6989 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006990
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006991 break;
6992 } else {
6993 txn->flags &= ~TX_CK_MASK;
6994 txn->flags |= TX_CK_DOWN;
6995 }
6996 }
6997 srv = srv->next;
6998 }
6999 }
7000 }
7001}
7002
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007003/* Find the end of a cookie value contained between <s> and <e>. It works the
7004 * same way as with headers above except that the semi-colon also ends a token.
7005 * See RFC2965 for more information. Note that it requires a valid header to
7006 * return a valid result.
7007 */
7008char *find_cookie_value_end(char *s, const char *e)
7009{
7010 int quoted, qdpair;
7011
7012 quoted = qdpair = 0;
7013 for (; s < e; s++) {
7014 if (qdpair) qdpair = 0;
7015 else if (quoted) {
7016 if (*s == '\\') qdpair = 1;
7017 else if (*s == '"') quoted = 0;
7018 }
7019 else if (*s == '"') quoted = 1;
7020 else if (*s == ',' || *s == ';') return s;
7021 }
7022 return s;
7023}
7024
7025/* Delete a value in a header between delimiters <from> and <next> in buffer
7026 * <buf>. The number of characters displaced is returned, and the pointer to
7027 * the first delimiter is updated if required. The function tries as much as
7028 * possible to respect the following principles :
7029 * - replace <from> delimiter by the <next> one unless <from> points to a
7030 * colon, in which case <next> is simply removed
7031 * - set exactly one space character after the new first delimiter, unless
7032 * there are not enough characters in the block being moved to do so.
7033 * - remove unneeded spaces before the previous delimiter and after the new
7034 * one.
7035 *
7036 * It is the caller's responsibility to ensure that :
7037 * - <from> points to a valid delimiter or the colon ;
7038 * - <next> points to a valid delimiter or the final CR/LF ;
7039 * - there are non-space chars before <from> ;
7040 * - there is a CR/LF at or after <next>.
7041 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007042int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007043{
7044 char *prev = *from;
7045
7046 if (*prev == ':') {
7047 /* We're removing the first value, preserve the colon and add a
7048 * space if possible.
7049 */
7050 if (!http_is_crlf[(unsigned char)*next])
7051 next++;
7052 prev++;
7053 if (prev < next)
7054 *prev++ = ' ';
7055
7056 while (http_is_spht[(unsigned char)*next])
7057 next++;
7058 } else {
7059 /* Remove useless spaces before the old delimiter. */
7060 while (http_is_spht[(unsigned char)*(prev-1)])
7061 prev--;
7062 *from = prev;
7063
7064 /* copy the delimiter and if possible a space if we're
7065 * not at the end of the line.
7066 */
7067 if (!http_is_crlf[(unsigned char)*next]) {
7068 *prev++ = *next++;
7069 if (prev + 1 < next)
7070 *prev++ = ' ';
7071 while (http_is_spht[(unsigned char)*next])
7072 next++;
7073 }
7074 }
7075 return buffer_replace2(buf, prev, next, NULL, 0);
7076}
7077
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007078/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007079 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007080 * desirable to call it only when needed. This code is quite complex because
7081 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7082 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007084void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007085{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007086 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007087 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007088 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007089 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7090 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007092 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007093 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007094 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007096 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007097 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007098 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007100 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 hdr_beg = hdr_next;
7102 hdr_end = hdr_beg + cur_hdr->len;
7103 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007105 /* We have one full header between hdr_beg and hdr_end, and the
7106 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107 * "Cookie:" headers.
7108 */
7109
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007110 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007111 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007112 old_idx = cur_idx;
7113 continue;
7114 }
7115
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007116 del_from = NULL; /* nothing to be deleted */
7117 preserve_hdr = 0; /* assume we may kill the whole header */
7118
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119 /* Now look for cookies. Conforming to RFC2109, we have to support
7120 * attributes whose name begin with a '$', and associate them with
7121 * the right cookie, if we want to delete this cookie.
7122 * So there are 3 cases for each cookie read :
7123 * 1) it's a special attribute, beginning with a '$' : ignore it.
7124 * 2) it's a server id cookie that we *MAY* want to delete : save
7125 * some pointers on it (last semi-colon, beginning of cookie...)
7126 * 3) it's an application cookie : we *MAY* have to delete a previous
7127 * "special" cookie.
7128 * At the end of loop, if a "special" cookie remains, we may have to
7129 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007130 * *MUST* delete it.
7131 *
7132 * Note: RFC2965 is unclear about the processing of spaces around
7133 * the equal sign in the ATTR=VALUE form. A careful inspection of
7134 * the RFC explicitly allows spaces before it, and not within the
7135 * tokens (attrs or values). An inspection of RFC2109 allows that
7136 * too but section 10.1.3 lets one think that spaces may be allowed
7137 * after the equal sign too, resulting in some (rare) buggy
7138 * implementations trying to do that. So let's do what servers do.
7139 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7140 * allowed quoted strings in values, with any possible character
7141 * after a backslash, including control chars and delimitors, which
7142 * causes parsing to become ambiguous. Browsers also allow spaces
7143 * within values even without quotes.
7144 *
7145 * We have to keep multiple pointers in order to support cookie
7146 * removal at the beginning, middle or end of header without
7147 * corrupting the header. All of these headers are valid :
7148 *
7149 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7150 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7151 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7152 * | | | | | | | | |
7153 * | | | | | | | | hdr_end <--+
7154 * | | | | | | | +--> next
7155 * | | | | | | +----> val_end
7156 * | | | | | +-----------> val_beg
7157 * | | | | +--------------> equal
7158 * | | | +----------------> att_end
7159 * | | +---------------------> att_beg
7160 * | +--------------------------> prev
7161 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007162 */
7163
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007164 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7165 /* Iterate through all cookies on this line */
7166
7167 /* find att_beg */
7168 att_beg = prev + 1;
7169 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7170 att_beg++;
7171
7172 /* find att_end : this is the first character after the last non
7173 * space before the equal. It may be equal to hdr_end.
7174 */
7175 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007177 while (equal < hdr_end) {
7178 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007179 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007180 if (http_is_spht[(unsigned char)*equal++])
7181 continue;
7182 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007183 }
7184
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007185 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7186 * is between <att_beg> and <equal>, both may be identical.
7187 */
7188
7189 /* look for end of cookie if there is an equal sign */
7190 if (equal < hdr_end && *equal == '=') {
7191 /* look for the beginning of the value */
7192 val_beg = equal + 1;
7193 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7194 val_beg++;
7195
7196 /* find the end of the value, respecting quotes */
7197 next = find_cookie_value_end(val_beg, hdr_end);
7198
7199 /* make val_end point to the first white space or delimitor after the value */
7200 val_end = next;
7201 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7202 val_end--;
7203 } else {
7204 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007205 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007206
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007207 /* We have nothing to do with attributes beginning with '$'. However,
7208 * they will automatically be removed if a header before them is removed,
7209 * since they're supposed to be linked together.
7210 */
7211 if (*att_beg == '$')
7212 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007213
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007214 /* Ignore cookies with no equal sign */
7215 if (equal == next) {
7216 /* This is not our cookie, so we must preserve it. But if we already
7217 * scheduled another cookie for removal, we cannot remove the
7218 * complete header, but we can remove the previous block itself.
7219 */
7220 preserve_hdr = 1;
7221 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007222 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007223 val_end += delta;
7224 next += delta;
7225 hdr_end += delta;
7226 hdr_next += delta;
7227 cur_hdr->len += delta;
7228 http_msg_move_end(&txn->req, delta);
7229 prev = del_from;
7230 del_from = NULL;
7231 }
7232 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007233 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007234
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007235 /* if there are spaces around the equal sign, we need to
7236 * strip them otherwise we'll get trouble for cookie captures,
7237 * or even for rewrites. Since this happens extremely rarely,
7238 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007239 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007240 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7241 int stripped_before = 0;
7242 int stripped_after = 0;
7243
7244 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007245 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007246 equal += stripped_before;
7247 val_beg += stripped_before;
7248 }
7249
7250 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007251 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007252 val_beg += stripped_after;
7253 stripped_before += stripped_after;
7254 }
7255
7256 val_end += stripped_before;
7257 next += stripped_before;
7258 hdr_end += stripped_before;
7259 hdr_next += stripped_before;
7260 cur_hdr->len += stripped_before;
7261 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007263 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007265 /* First, let's see if we want to capture this cookie. We check
7266 * that we don't already have a client side cookie, because we
7267 * can only capture one. Also as an optimisation, we ignore
7268 * cookies shorter than the declared name.
7269 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007270 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7271 (val_end - att_beg >= s->fe->capture_namelen) &&
7272 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007273 int log_len = val_end - att_beg;
7274
7275 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7276 Alert("HTTP logging : out of memory.\n");
7277 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007278 if (log_len > s->fe->capture_len)
7279 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007280 memcpy(txn->cli_cookie, att_beg, log_len);
7281 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007282 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007283 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007284
Willy Tarreaubca99692010-10-06 19:25:55 +02007285 /* Persistence cookies in passive, rewrite or insert mode have the
7286 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007287 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007288 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007289 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007290 * For cookies in prefix mode, the form is :
7291 *
7292 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007293 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007294 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7295 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7296 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007297 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007298
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007299 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7300 * have the server ID between val_beg and delim, and the original cookie between
7301 * delim+1 and val_end. Otherwise, delim==val_end :
7302 *
7303 * Cookie: NAME=SRV; # in all but prefix modes
7304 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7305 * | || || | |+-> next
7306 * | || || | +--> val_end
7307 * | || || +---------> delim
7308 * | || |+------------> val_beg
7309 * | || +-------------> att_end = equal
7310 * | |+-----------------> att_beg
7311 * | +------------------> prev
7312 * +-------------------------> hdr_beg
7313 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007314
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007315 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007316 for (delim = val_beg; delim < val_end; delim++)
7317 if (*delim == COOKIE_DELIM)
7318 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007319 } else {
7320 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007321 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007322 /* Now check if the cookie contains a date field, which would
7323 * appear after a vertical bar ('|') just after the server name
7324 * and before the delimiter.
7325 */
7326 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7327 if (vbar1) {
7328 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007329 * right is the last seen date. It is a base64 encoded
7330 * 30-bit value representing the UNIX date since the
7331 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007332 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007333 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007334 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007335 if (val_end - vbar1 >= 5) {
7336 val = b64tos30(vbar1);
7337 if (val > 0)
7338 txn->cookie_last_date = val << 2;
7339 }
7340 /* look for a second vertical bar */
7341 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7342 if (vbar1 && (val_end - vbar1 > 5)) {
7343 val = b64tos30(vbar1 + 1);
7344 if (val > 0)
7345 txn->cookie_first_date = val << 2;
7346 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007347 }
7348 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007349
Willy Tarreauf64d1412010-10-07 20:06:11 +02007350 /* if the cookie has an expiration date and the proxy wants to check
7351 * it, then we do that now. We first check if the cookie is too old,
7352 * then only if it has expired. We detect strict overflow because the
7353 * time resolution here is not great (4 seconds). Cookies with dates
7354 * in the future are ignored if their offset is beyond one day. This
7355 * allows an admin to fix timezone issues without expiring everyone
7356 * and at the same time avoids keeping unwanted side effects for too
7357 * long.
7358 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007359 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7360 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007361 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007362 txn->flags &= ~TX_CK_MASK;
7363 txn->flags |= TX_CK_OLD;
7364 delim = val_beg; // let's pretend we have not found the cookie
7365 txn->cookie_first_date = 0;
7366 txn->cookie_last_date = 0;
7367 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007368 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7369 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007370 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007371 txn->flags &= ~TX_CK_MASK;
7372 txn->flags |= TX_CK_EXPIRED;
7373 delim = val_beg; // let's pretend we have not found the cookie
7374 txn->cookie_first_date = 0;
7375 txn->cookie_last_date = 0;
7376 }
7377
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007378 /* Here, we'll look for the first running server which supports the cookie.
7379 * This allows to share a same cookie between several servers, for example
7380 * to dedicate backup servers to specific servers only.
7381 * However, to prevent clients from sticking to cookie-less backup server
7382 * when they have incidentely learned an empty cookie, we simply ignore
7383 * empty cookies and mark them as invalid.
7384 * The same behaviour is applied when persistence must be ignored.
7385 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007386 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007387 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007388
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007389 while (srv) {
7390 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7391 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7392 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007393 (s->be->options & PR_O_PERSIST) ||
7394 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007395 /* we found the server and we can use it */
7396 txn->flags &= ~TX_CK_MASK;
7397 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007398 s->flags |= SN_DIRECT | SN_ASSIGNED;
7399 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007400 break;
7401 } else {
7402 /* we found a server, but it's down,
7403 * mark it as such and go on in case
7404 * another one is available.
7405 */
7406 txn->flags &= ~TX_CK_MASK;
7407 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007409 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007410 srv = srv->next;
7411 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007412
Willy Tarreauf64d1412010-10-07 20:06:11 +02007413 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007414 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007415 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007416 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007417 txn->flags |= TX_CK_UNUSED;
7418 else
7419 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007420 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007421
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007422 /* depending on the cookie mode, we may have to either :
7423 * - delete the complete cookie if we're in insert+indirect mode, so that
7424 * the server never sees it ;
7425 * - remove the server id from the cookie value, and tag the cookie as an
7426 * application cookie so that it does not get accidentely removed later,
7427 * if we're in cookie prefix mode
7428 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007429 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007430 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007431
Willy Tarreau9b28e032012-10-12 23:49:43 +02007432 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007433 val_end += delta;
7434 next += delta;
7435 hdr_end += delta;
7436 hdr_next += delta;
7437 cur_hdr->len += delta;
7438 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007439
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007440 del_from = NULL;
7441 preserve_hdr = 1; /* we want to keep this cookie */
7442 }
7443 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007444 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007445 del_from = prev;
7446 }
7447 } else {
7448 /* This is not our cookie, so we must preserve it. But if we already
7449 * scheduled another cookie for removal, we cannot remove the
7450 * complete header, but we can remove the previous block itself.
7451 */
7452 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007453
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007454 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007455 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007456 if (att_beg >= del_from)
7457 att_beg += delta;
7458 if (att_end >= del_from)
7459 att_end += delta;
7460 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007461 val_end += delta;
7462 next += delta;
7463 hdr_end += delta;
7464 hdr_next += delta;
7465 cur_hdr->len += delta;
7466 http_msg_move_end(&txn->req, delta);
7467 prev = del_from;
7468 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007469 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007470 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007471
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007473 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007474 int cmp_len, value_len;
7475 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007476
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007477 if (s->be->options2 & PR_O2_AS_PFX) {
7478 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7479 value_begin = att_beg + s->be->appsession_name_len;
7480 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007481 } else {
7482 cmp_len = att_end - att_beg;
7483 value_begin = val_beg;
7484 value_len = val_end - val_beg;
7485 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007486
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007488 if (cmp_len == s->be->appsession_name_len &&
7489 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7490 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007491 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007492 }
7493
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007494 /* continue with next cookie on this header line */
7495 att_beg = next;
7496 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007498 /* There are no more cookies on this line.
7499 * We may still have one (or several) marked for deletion at the
7500 * end of the line. We must do this now in two ways :
7501 * - if some cookies must be preserved, we only delete from the
7502 * mark to the end of line ;
7503 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007504 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007505 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007506 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007507 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007508 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007509 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510 cur_hdr->len += delta;
7511 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007512 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007513
7514 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007515 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7516 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007517 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007518 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007519 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007520 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007521 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007522 }
7523
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007525 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007527}
7528
7529
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530/* Iterate the same filter through all response headers contained in <rtr>.
7531 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7532 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007533int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007534{
7535 char term;
7536 char *cur_ptr, *cur_end, *cur_next;
7537 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007538 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007539 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007540 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007541
7542 last_hdr = 0;
7543
Willy Tarreau9b28e032012-10-12 23:49:43 +02007544 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007545 old_idx = 0;
7546
7547 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007548 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007549 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007550 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007551 (exp->action == ACT_ALLOW ||
7552 exp->action == ACT_DENY))
7553 return 0;
7554
7555 cur_idx = txn->hdr_idx.v[old_idx].next;
7556 if (!cur_idx)
7557 break;
7558
7559 cur_hdr = &txn->hdr_idx.v[cur_idx];
7560 cur_ptr = cur_next;
7561 cur_end = cur_ptr + cur_hdr->len;
7562 cur_next = cur_end + cur_hdr->cr + 1;
7563
7564 /* Now we have one header between cur_ptr and cur_end,
7565 * and the next header starts at cur_next.
7566 */
7567
7568 /* The annoying part is that pattern matching needs
7569 * that we modify the contents to null-terminate all
7570 * strings before testing them.
7571 */
7572
7573 term = *cur_end;
7574 *cur_end = '\0';
7575
7576 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7577 switch (exp->action) {
7578 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007579 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007580 last_hdr = 1;
7581 break;
7582
7583 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007584 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007585 last_hdr = 1;
7586 break;
7587
7588 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007589 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7590 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007591 /* FIXME: if the user adds a newline in the replacement, the
7592 * index will not be recalculated for now, and the new line
7593 * will not be counted as a new header.
7594 */
7595
7596 cur_end += delta;
7597 cur_next += delta;
7598 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007599 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600 break;
7601
7602 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007603 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007604 cur_next += delta;
7605
Willy Tarreaufa355d42009-11-29 18:12:29 +01007606 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007607 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7608 txn->hdr_idx.used--;
7609 cur_hdr->len = 0;
7610 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007611 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007612 break;
7613
7614 }
7615 }
7616 if (cur_end)
7617 *cur_end = term; /* restore the string terminator */
7618
7619 /* keep the link from this header to next one in case of later
7620 * removal of next header.
7621 */
7622 old_idx = cur_idx;
7623 }
7624 return 0;
7625}
7626
7627
7628/* Apply the filter to the status line in the response buffer <rtr>.
7629 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7630 * or -1 if a replacement resulted in an invalid status line.
7631 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007632int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007633{
7634 char term;
7635 char *cur_ptr, *cur_end;
7636 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007637 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007638 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007639
7640
Willy Tarreau3d300592007-03-18 18:34:41 +01007641 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007642 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007643 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007644 (exp->action == ACT_ALLOW ||
7645 exp->action == ACT_DENY))
7646 return 0;
7647 else if (exp->action == ACT_REMOVE)
7648 return 0;
7649
7650 done = 0;
7651
Willy Tarreau9b28e032012-10-12 23:49:43 +02007652 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007653 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654
7655 /* Now we have the status line between cur_ptr and cur_end */
7656
7657 /* The annoying part is that pattern matching needs
7658 * that we modify the contents to null-terminate all
7659 * strings before testing them.
7660 */
7661
7662 term = *cur_end;
7663 *cur_end = '\0';
7664
7665 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7666 switch (exp->action) {
7667 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007668 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007669 done = 1;
7670 break;
7671
7672 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007673 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007674 done = 1;
7675 break;
7676
7677 case ACT_REPLACE:
7678 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007679 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7680 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007681 /* FIXME: if the user adds a newline in the replacement, the
7682 * index will not be recalculated for now, and the new line
7683 * will not be counted as a new header.
7684 */
7685
Willy Tarreaufa355d42009-11-29 18:12:29 +01007686 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007687 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007688 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007689 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007690 cur_ptr, cur_end + 1,
7691 NULL, NULL);
7692 if (unlikely(!cur_end))
7693 return -1;
7694
7695 /* we have a full respnse and we know that we have either a CR
7696 * or an LF at <ptr>.
7697 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007698 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007699 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007700 /* there is no point trying this regex on headers */
7701 return 1;
7702 }
7703 }
7704 *cur_end = term; /* restore the string terminator */
7705 return done;
7706}
7707
7708
7709
7710/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007711 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007712 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7713 * unparsable response.
7714 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007715int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007716{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007717 struct http_txn *txn = &s->txn;
7718 struct hdr_exp *exp;
7719
7720 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007721 int ret;
7722
7723 /*
7724 * The interleaving of transformations and verdicts
7725 * makes it difficult to decide to continue or stop
7726 * the evaluation.
7727 */
7728
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007729 if (txn->flags & TX_SVDENY)
7730 break;
7731
Willy Tarreau3d300592007-03-18 18:34:41 +01007732 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007733 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7734 exp->action == ACT_PASS)) {
7735 exp = exp->next;
7736 continue;
7737 }
7738
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007739 /* if this filter had a condition, evaluate it now and skip to
7740 * next filter if the condition does not match.
7741 */
7742 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007743 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007744 ret = acl_pass(ret);
7745 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7746 ret = !ret;
7747 if (!ret)
7748 continue;
7749 }
7750
Willy Tarreaua15645d2007-03-18 16:22:39 +01007751 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007752 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007753 if (unlikely(ret < 0))
7754 return -1;
7755
7756 if (likely(ret == 0)) {
7757 /* The filter did not match the response, it can be
7758 * iterated through all headers.
7759 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007760 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007761 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007762 }
7763 return 0;
7764}
7765
7766
Willy Tarreaua15645d2007-03-18 16:22:39 +01007767/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007768 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007769 * desirable to call it only when needed. This function is also used when we
7770 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007771 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007772void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007773{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007774 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007775 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007776 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007778 char *hdr_beg, *hdr_end, *hdr_next;
7779 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007780
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781 /* Iterate through the headers.
7782 * we start with the start line.
7783 */
7784 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007785 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007786
7787 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7788 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007789 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007790
7791 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007792 hdr_beg = hdr_next;
7793 hdr_end = hdr_beg + cur_hdr->len;
7794 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007795
Willy Tarreau24581ba2010-08-31 22:39:35 +02007796 /* We have one full header between hdr_beg and hdr_end, and the
7797 * next header starts at hdr_next. We're only interested in
7798 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 */
7800
Willy Tarreau24581ba2010-08-31 22:39:35 +02007801 is_cookie2 = 0;
7802 prev = hdr_beg + 10;
7803 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007804 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007805 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7806 if (!val) {
7807 old_idx = cur_idx;
7808 continue;
7809 }
7810 is_cookie2 = 1;
7811 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007812 }
7813
Willy Tarreau24581ba2010-08-31 22:39:35 +02007814 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7815 * <prev> points to the colon.
7816 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007817 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007818
Willy Tarreau24581ba2010-08-31 22:39:35 +02007819 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7820 * check-cache is enabled) and we are not interested in checking
7821 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007822 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007823 if (s->be->cookie_name == NULL &&
7824 s->be->appsession_name == NULL &&
7825 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007826 return;
7827
Willy Tarreau24581ba2010-08-31 22:39:35 +02007828 /* OK so now we know we have to process this response cookie.
7829 * The format of the Set-Cookie header is slightly different
7830 * from the format of the Cookie header in that it does not
7831 * support the comma as a cookie delimiter (thus the header
7832 * cannot be folded) because the Expires attribute described in
7833 * the original Netscape's spec may contain an unquoted date
7834 * with a comma inside. We have to live with this because
7835 * many browsers don't support Max-Age and some browsers don't
7836 * support quoted strings. However the Set-Cookie2 header is
7837 * clean.
7838 *
7839 * We have to keep multiple pointers in order to support cookie
7840 * removal at the beginning, middle or end of header without
7841 * corrupting the header (in case of set-cookie2). A special
7842 * pointer, <scav> points to the beginning of the set-cookie-av
7843 * fields after the first semi-colon. The <next> pointer points
7844 * either to the end of line (set-cookie) or next unquoted comma
7845 * (set-cookie2). All of these headers are valid :
7846 *
7847 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7848 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7849 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7850 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7851 * | | | | | | | | | |
7852 * | | | | | | | | +-> next hdr_end <--+
7853 * | | | | | | | +------------> scav
7854 * | | | | | | +--------------> val_end
7855 * | | | | | +--------------------> val_beg
7856 * | | | | +----------------------> equal
7857 * | | | +------------------------> att_end
7858 * | | +----------------------------> att_beg
7859 * | +------------------------------> prev
7860 * +-----------------------------------------> hdr_beg
7861 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862
Willy Tarreau24581ba2010-08-31 22:39:35 +02007863 for (; prev < hdr_end; prev = next) {
7864 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865
Willy Tarreau24581ba2010-08-31 22:39:35 +02007866 /* find att_beg */
7867 att_beg = prev + 1;
7868 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7869 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007870
Willy Tarreau24581ba2010-08-31 22:39:35 +02007871 /* find att_end : this is the first character after the last non
7872 * space before the equal. It may be equal to hdr_end.
7873 */
7874 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007875
Willy Tarreau24581ba2010-08-31 22:39:35 +02007876 while (equal < hdr_end) {
7877 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7878 break;
7879 if (http_is_spht[(unsigned char)*equal++])
7880 continue;
7881 att_end = equal;
7882 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883
Willy Tarreau24581ba2010-08-31 22:39:35 +02007884 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7885 * is between <att_beg> and <equal>, both may be identical.
7886 */
7887
7888 /* look for end of cookie if there is an equal sign */
7889 if (equal < hdr_end && *equal == '=') {
7890 /* look for the beginning of the value */
7891 val_beg = equal + 1;
7892 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7893 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007894
Willy Tarreau24581ba2010-08-31 22:39:35 +02007895 /* find the end of the value, respecting quotes */
7896 next = find_cookie_value_end(val_beg, hdr_end);
7897
7898 /* make val_end point to the first white space or delimitor after the value */
7899 val_end = next;
7900 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7901 val_end--;
7902 } else {
7903 /* <equal> points to next comma, semi-colon or EOL */
7904 val_beg = val_end = next = equal;
7905 }
7906
7907 if (next < hdr_end) {
7908 /* Set-Cookie2 supports multiple cookies, and <next> points to
7909 * a colon or semi-colon before the end. So skip all attr-value
7910 * pairs and look for the next comma. For Set-Cookie, since
7911 * commas are permitted in values, skip to the end.
7912 */
7913 if (is_cookie2)
7914 next = find_hdr_value_end(next, hdr_end);
7915 else
7916 next = hdr_end;
7917 }
7918
7919 /* Now everything is as on the diagram above */
7920
7921 /* Ignore cookies with no equal sign */
7922 if (equal == val_end)
7923 continue;
7924
7925 /* If there are spaces around the equal sign, we need to
7926 * strip them otherwise we'll get trouble for cookie captures,
7927 * or even for rewrites. Since this happens extremely rarely,
7928 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007929 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007930 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7931 int stripped_before = 0;
7932 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007933
Willy Tarreau24581ba2010-08-31 22:39:35 +02007934 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007935 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007936 equal += stripped_before;
7937 val_beg += stripped_before;
7938 }
7939
7940 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007941 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007942 val_beg += stripped_after;
7943 stripped_before += stripped_after;
7944 }
7945
7946 val_end += stripped_before;
7947 next += stripped_before;
7948 hdr_end += stripped_before;
7949 hdr_next += stripped_before;
7950 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007951 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007952 }
7953
7954 /* First, let's see if we want to capture this cookie. We check
7955 * that we don't already have a server side cookie, because we
7956 * can only capture one. Also as an optimisation, we ignore
7957 * cookies shorter than the declared name.
7958 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007959 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007960 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007961 (val_end - att_beg >= s->fe->capture_namelen) &&
7962 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007963 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007964 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965 Alert("HTTP logging : out of memory.\n");
7966 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007967 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007968 if (log_len > s->fe->capture_len)
7969 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007970 memcpy(txn->srv_cookie, att_beg, log_len);
7971 txn->srv_cookie[log_len] = 0;
7972 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007973 }
7974
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007975 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007977 if (!(s->flags & SN_IGNORE_PRST) &&
7978 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7979 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007980 /* assume passive cookie by default */
7981 txn->flags &= ~TX_SCK_MASK;
7982 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983
7984 /* If the cookie is in insert mode on a known server, we'll delete
7985 * this occurrence because we'll insert another one later.
7986 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007987 * a direct access.
7988 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007989 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007990 /* The "preserve" flag was set, we don't want to touch the
7991 * server's cookie.
7992 */
7993 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007994 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
7995 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007996 /* this cookie must be deleted */
7997 if (*prev == ':' && next == hdr_end) {
7998 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007999 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008000 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8001 txn->hdr_idx.used--;
8002 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008003 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008004 hdr_next += delta;
8005 http_msg_move_end(&txn->rsp, delta);
8006 /* note: while both invalid now, <next> and <hdr_end>
8007 * are still equal, so the for() will stop as expected.
8008 */
8009 } else {
8010 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008011 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008012 next = prev;
8013 hdr_end += delta;
8014 hdr_next += delta;
8015 cur_hdr->len += delta;
8016 http_msg_move_end(&txn->rsp, delta);
8017 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008018 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008019 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008020 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008022 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008023 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008024 * with this server since we know it.
8025 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008026 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008027 next += delta;
8028 hdr_end += delta;
8029 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008030 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008031 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032
Willy Tarreauf1348312010-10-07 15:54:11 +02008033 txn->flags &= ~TX_SCK_MASK;
8034 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008036 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008037 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008038 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008040 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008041 next += delta;
8042 hdr_end += delta;
8043 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008044 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008045 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046
Willy Tarreau827aee92011-03-10 16:55:02 +01008047 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008048 txn->flags &= ~TX_SCK_MASK;
8049 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 }
8051 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008052 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008053 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008054 int cmp_len, value_len;
8055 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008056
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008057 if (s->be->options2 & PR_O2_AS_PFX) {
8058 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8059 value_begin = att_beg + s->be->appsession_name_len;
8060 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008061 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008062 cmp_len = att_end - att_beg;
8063 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008064 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008065 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008066
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008067 if ((cmp_len == s->be->appsession_name_len) &&
8068 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008069 /* free a possibly previously allocated memory */
8070 pool_free2(apools.sessid, txn->sessid);
8071
Cyril Bontéb21570a2009-11-29 20:04:48 +01008072 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008073 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008074 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008075 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008076 return;
8077 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008078 memcpy(txn->sessid, value_begin, value_len);
8079 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008080 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008081 }
8082 /* that's done for this cookie, check the next one on the same
8083 * line when next != hdr_end (only if is_cookie2).
8084 */
8085 }
8086 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008088 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008089
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008090 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008091 appsess *asession = NULL;
8092 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008093 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008094 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008095 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008096 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8097 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008098 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008099 return;
8100 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008101 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8102
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008103 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8104 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008105 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8106 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008107 return;
8108 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008109 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8110 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008111
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008112 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008113 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008114 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008115 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8116 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008117 return;
8118 }
8119 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008120 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008121
8122 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008123 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008124 }
8125
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008126 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008127 asession->request_count++;
8128 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129}
8130
8131
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008133 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008134 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008135void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008136{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008137 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008138 char *p1, *p2;
8139
8140 char *cur_ptr, *cur_end, *cur_next;
8141 int cur_idx;
8142
Willy Tarreau5df51872007-11-25 16:20:08 +01008143 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 return;
8145
8146 /* Iterate through the headers.
8147 * we start with the start line.
8148 */
8149 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008150 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151
8152 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8153 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008154 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008155
8156 cur_hdr = &txn->hdr_idx.v[cur_idx];
8157 cur_ptr = cur_next;
8158 cur_end = cur_ptr + cur_hdr->len;
8159 cur_next = cur_end + cur_hdr->cr + 1;
8160
8161 /* We have one full header between cur_ptr and cur_end, and the
8162 * next header starts at cur_next. We're only interested in
8163 * "Cookie:" headers.
8164 */
8165
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008166 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8167 if (val) {
8168 if ((cur_end - (cur_ptr + val) >= 8) &&
8169 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8170 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8171 return;
8172 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008173 }
8174
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008175 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8176 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008177 continue;
8178
8179 /* OK, right now we know we have a cache-control header at cur_ptr */
8180
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008181 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008182
8183 if (p1 >= cur_end) /* no more info */
8184 continue;
8185
8186 /* p1 is at the beginning of the value */
8187 p2 = p1;
8188
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008189 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008190 p2++;
8191
8192 /* we have a complete value between p1 and p2 */
8193 if (p2 < cur_end && *p2 == '=') {
8194 /* we have something of the form no-cache="set-cookie" */
8195 if ((cur_end - p1 >= 21) &&
8196 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8197 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008198 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008199 continue;
8200 }
8201
8202 /* OK, so we know that either p2 points to the end of string or to a comma */
8203 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008204 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8206 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8207 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008208 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008209 return;
8210 }
8211
8212 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008213 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214 continue;
8215 }
8216 }
8217}
8218
8219
Willy Tarreau58f10d72006-12-04 02:26:12 +01008220/*
8221 * Try to retrieve a known appsession in the URI, then the associated server.
8222 * If the server is found, it's assigned to the session.
8223 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008224void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008225{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008226 char *end_params, *first_param, *cur_param, *next_param;
8227 char separator;
8228 int value_len;
8229
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008230 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008231
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008232 if (s->be->appsession_name == NULL ||
8233 (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 +01008234 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008235 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008236
Cyril Bontéb21570a2009-11-29 20:04:48 +01008237 first_param = NULL;
8238 switch (mode) {
8239 case PR_O2_AS_M_PP:
8240 first_param = memchr(begin, ';', len);
8241 break;
8242 case PR_O2_AS_M_QS:
8243 first_param = memchr(begin, '?', len);
8244 break;
8245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008246
Cyril Bontéb21570a2009-11-29 20:04:48 +01008247 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008248 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008249 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008250
Cyril Bontéb21570a2009-11-29 20:04:48 +01008251 switch (mode) {
8252 case PR_O2_AS_M_PP:
8253 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8254 end_params = (char *) begin + len;
8255 }
8256 separator = ';';
8257 break;
8258 case PR_O2_AS_M_QS:
8259 end_params = (char *) begin + len;
8260 separator = '&';
8261 break;
8262 default:
8263 /* unknown mode, shouldn't happen */
8264 return;
8265 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008266
Cyril Bontéb21570a2009-11-29 20:04:48 +01008267 cur_param = next_param = end_params;
8268 while (cur_param > first_param) {
8269 cur_param--;
8270 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8271 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008272 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8273 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8274 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008275 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008276 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8277 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008278 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008279 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008280 }
8281 break;
8282 }
8283 next_param = cur_param;
8284 }
8285 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008286#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008287 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008288 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008289#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008290}
8291
Willy Tarreaub2513902006-12-17 14:52:38 +01008292/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008293 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008294 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008295 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008296 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008297 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008298 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008299 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008300 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008301int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008302{
8303 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008304 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008305 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008306
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008307 if (!uri_auth)
8308 return 0;
8309
Cyril Bonté70be45d2010-10-12 00:14:35 +02008310 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008311 return 0;
8312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008313 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008314 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008315 return 0;
8316
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008317 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008318 return 0;
8319
Willy Tarreaub2513902006-12-17 14:52:38 +01008320 return 1;
8321}
8322
Willy Tarreau4076a152009-04-02 15:18:36 +02008323/*
8324 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008325 * By default it tries to report the error position as msg->err_pos. However if
8326 * this one is not set, it will then report msg->next, which is the last known
8327 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008328 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008329 */
8330void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008331 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008332 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008333{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008334 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008335 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008336
Willy Tarreau9b28e032012-10-12 23:49:43 +02008337 es->len = MIN(chn->buf->i, sizeof(es->buf));
8338 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008339 len1 = MIN(len1, es->len);
8340 len2 = es->len - len1; /* remaining data if buffer wraps */
8341
Willy Tarreau9b28e032012-10-12 23:49:43 +02008342 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008343 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008344 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008345
Willy Tarreau4076a152009-04-02 15:18:36 +02008346 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008347 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008348 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008349 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008350
Willy Tarreau4076a152009-04-02 15:18:36 +02008351 es->when = date; // user-visible date
8352 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008353 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008354 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008355 if (objt_conn(s->req->prod->end))
8356 es->src = __objt_conn(s->req->prod->end)->addr.from;
8357 else
8358 memset(&es->src, 0, sizeof(es->src));
8359
Willy Tarreau078272e2010-12-12 12:46:33 +01008360 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008361 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008362 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008363 es->s_flags = s->flags;
8364 es->t_flags = s->txn.flags;
8365 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008366 es->b_out = chn->buf->o;
8367 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008368 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008369 es->m_clen = msg->chunk_len;
8370 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008371}
Willy Tarreaub2513902006-12-17 14:52:38 +01008372
Willy Tarreau294c4732011-12-16 21:35:50 +01008373/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8374 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8375 * performed over the whole headers. Otherwise it must contain a valid header
8376 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8377 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8378 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8379 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008380 * -1. The value fetch stops at commas, so this function is suited for use with
8381 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008382 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008383 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008384unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008385 struct hdr_idx *idx, int occ,
8386 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008387{
Willy Tarreau294c4732011-12-16 21:35:50 +01008388 struct hdr_ctx local_ctx;
8389 char *ptr_hist[MAX_HDR_HISTORY];
8390 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008391 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008392 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008393
Willy Tarreau294c4732011-12-16 21:35:50 +01008394 if (!ctx) {
8395 local_ctx.idx = 0;
8396 ctx = &local_ctx;
8397 }
8398
Willy Tarreaubce70882009-09-07 11:51:47 +02008399 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008400 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008401 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008402 occ--;
8403 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008404 *vptr = ctx->line + ctx->val;
8405 *vlen = ctx->vlen;
8406 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008407 }
8408 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008409 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008410 }
8411
8412 /* negative occurrence, we scan all the list then walk back */
8413 if (-occ > MAX_HDR_HISTORY)
8414 return 0;
8415
Willy Tarreau294c4732011-12-16 21:35:50 +01008416 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008417 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008418 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8419 len_hist[hist_ptr] = ctx->vlen;
8420 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008421 hist_ptr = 0;
8422 found++;
8423 }
8424 if (-occ > found)
8425 return 0;
8426 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008427 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8428 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8429 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008430 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008431 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008432 if (hist_ptr >= MAX_HDR_HISTORY)
8433 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008434 *vptr = ptr_hist[hist_ptr];
8435 *vlen = len_hist[hist_ptr];
8436 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008437}
8438
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008439/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8440 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8441 * performed over the whole headers. Otherwise it must contain a valid header
8442 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8443 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8444 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8445 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8446 * -1. This function differs from http_get_hdr() in that it only returns full
8447 * line header values and does not stop at commas.
8448 * The return value is 0 if nothing was found, or non-zero otherwise.
8449 */
8450unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8451 struct hdr_idx *idx, int occ,
8452 struct hdr_ctx *ctx, char **vptr, int *vlen)
8453{
8454 struct hdr_ctx local_ctx;
8455 char *ptr_hist[MAX_HDR_HISTORY];
8456 int len_hist[MAX_HDR_HISTORY];
8457 unsigned int hist_ptr;
8458 int found;
8459
8460 if (!ctx) {
8461 local_ctx.idx = 0;
8462 ctx = &local_ctx;
8463 }
8464
8465 if (occ >= 0) {
8466 /* search from the beginning */
8467 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8468 occ--;
8469 if (occ <= 0) {
8470 *vptr = ctx->line + ctx->val;
8471 *vlen = ctx->vlen;
8472 return 1;
8473 }
8474 }
8475 return 0;
8476 }
8477
8478 /* negative occurrence, we scan all the list then walk back */
8479 if (-occ > MAX_HDR_HISTORY)
8480 return 0;
8481
8482 found = hist_ptr = 0;
8483 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8484 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8485 len_hist[hist_ptr] = ctx->vlen;
8486 if (++hist_ptr >= MAX_HDR_HISTORY)
8487 hist_ptr = 0;
8488 found++;
8489 }
8490 if (-occ > found)
8491 return 0;
8492 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8493 * find occurrence -occ, so we have to check [hist_ptr+occ].
8494 */
8495 hist_ptr += occ;
8496 if (hist_ptr >= MAX_HDR_HISTORY)
8497 hist_ptr -= MAX_HDR_HISTORY;
8498 *vptr = ptr_hist[hist_ptr];
8499 *vlen = len_hist[hist_ptr];
8500 return 1;
8501}
8502
Willy Tarreaubaaee002006-06-26 02:48:02 +02008503/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008504 * Print a debug line with a header. Always stop at the first CR or LF char,
8505 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8506 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008507 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008508void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008509{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008510 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008511 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008512 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008513 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8514 objt_conn(s->req->cons->end) ? (unsigned short)objt_conn(s->req->cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008515
8516 for (max = 0; start + max < end; max++)
8517 if (start[max] == '\r' || start[max] == '\n')
8518 break;
8519
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008520 UBOUND(max, trash.size - trash.len - 3);
8521 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8522 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008523 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008524}
8525
Willy Tarreau0937bc42009-12-22 15:03:09 +01008526/*
8527 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8528 * the required fields are properly allocated and that we only need to (re)init
8529 * them. This should be used before processing any new request.
8530 */
8531void http_init_txn(struct session *s)
8532{
8533 struct http_txn *txn = &s->txn;
8534 struct proxy *fe = s->fe;
8535
8536 txn->flags = 0;
8537 txn->status = -1;
8538
Willy Tarreauf64d1412010-10-07 20:06:11 +02008539 txn->cookie_first_date = 0;
8540 txn->cookie_last_date = 0;
8541
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008542 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008543 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008544 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008545 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008546 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008547 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008548 txn->req.chunk_len = 0LL;
8549 txn->req.body_len = 0LL;
8550 txn->rsp.chunk_len = 0LL;
8551 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008552 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8553 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008554 txn->req.chn = s->req;
8555 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008556
8557 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008558
8559 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8560 if (fe->options2 & PR_O2_REQBUG_OK)
8561 txn->req.err_pos = -1; /* let buggy requests pass */
8562
Willy Tarreau46023632010-01-07 22:51:47 +01008563 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008564 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8565
Willy Tarreau46023632010-01-07 22:51:47 +01008566 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008567 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8568
8569 if (txn->hdr_idx.v)
8570 hdr_idx_init(&txn->hdr_idx);
8571}
8572
8573/* to be used at the end of a transaction */
8574void http_end_txn(struct session *s)
8575{
8576 struct http_txn *txn = &s->txn;
8577
Willy Tarreau75195602014-03-11 15:48:55 +01008578 /* release any possible compression context */
8579 if (s->flags & SN_COMP_READY)
8580 s->comp_algo->end(&s->comp_ctx);
8581 s->comp_algo = NULL;
8582 s->flags &= ~SN_COMP_READY;
8583
Willy Tarreau0937bc42009-12-22 15:03:09 +01008584 /* these ones will have been dynamically allocated */
8585 pool_free2(pool2_requri, txn->uri);
8586 pool_free2(pool2_capture, txn->cli_cookie);
8587 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008588 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008589 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008590
William Lallemanda73203e2012-03-12 12:48:57 +01008591 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008592 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008593 txn->uri = NULL;
8594 txn->srv_cookie = NULL;
8595 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008596
8597 if (txn->req.cap) {
8598 struct cap_hdr *h;
8599 for (h = s->fe->req_cap; h; h = h->next)
8600 pool_free2(h->pool, txn->req.cap[h->index]);
8601 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8602 }
8603
8604 if (txn->rsp.cap) {
8605 struct cap_hdr *h;
8606 for (h = s->fe->rsp_cap; h; h = h->next)
8607 pool_free2(h->pool, txn->rsp.cap[h->index]);
8608 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8609 }
8610
Willy Tarreau0937bc42009-12-22 15:03:09 +01008611}
8612
8613/* to be used at the end of a transaction to prepare a new one */
8614void http_reset_txn(struct session *s)
8615{
8616 http_end_txn(s);
8617 http_init_txn(s);
8618
8619 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008620 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008621 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008622 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008623 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008624 /* re-init store persistence */
8625 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008626 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008627
Willy Tarreau0937bc42009-12-22 15:03:09 +01008628 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008629
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008630 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008631
Willy Tarreau739cfba2010-01-25 23:11:14 +01008632 /* We must trim any excess data from the response buffer, because we
8633 * may have blocked an invalid response from a server that we don't
8634 * want to accidentely forward once we disable the analysers, nor do
8635 * we want those data to come along with next response. A typical
8636 * example of such data would be from a buggy server responding to
8637 * a HEAD with some data, or sending more than the advertised
8638 * content-length.
8639 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008640 if (unlikely(s->rep->buf->i))
8641 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008642
Willy Tarreau0937bc42009-12-22 15:03:09 +01008643 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008644 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008645
Willy Tarreaud04e8582010-05-31 12:31:35 +02008646 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008647 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008648
8649 s->req->rex = TICK_ETERNITY;
8650 s->req->wex = TICK_ETERNITY;
8651 s->req->analyse_exp = TICK_ETERNITY;
8652 s->rep->rex = TICK_ETERNITY;
8653 s->rep->wex = TICK_ETERNITY;
8654 s->rep->analyse_exp = TICK_ETERNITY;
8655}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008656
Willy Tarreauff011f22011-01-06 17:51:27 +01008657void free_http_req_rules(struct list *r) {
8658 struct http_req_rule *tr, *pr;
8659
8660 list_for_each_entry_safe(pr, tr, r, list) {
8661 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008662 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008663 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008664
8665 free(pr);
8666 }
8667}
8668
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008669/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008670struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8671{
8672 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008673 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008674 int cur_arg;
8675
8676 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8677 if (!rule) {
8678 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008679 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008680 }
8681
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008682 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008683 rule->action = HTTP_REQ_ACT_ALLOW;
8684 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008685 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008686 rule->action = HTTP_REQ_ACT_DENY;
8687 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008688 } else if (!strcmp(args[0], "tarpit")) {
8689 rule->action = HTTP_REQ_ACT_TARPIT;
8690 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008691 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008692 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008693 cur_arg = 1;
8694
8695 while(*args[cur_arg]) {
8696 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008697 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008698 cur_arg+=2;
8699 continue;
8700 } else
8701 break;
8702 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008703 } else if (!strcmp(args[0], "set-nice")) {
8704 rule->action = HTTP_REQ_ACT_SET_NICE;
8705 cur_arg = 1;
8706
8707 if (!*args[cur_arg] ||
8708 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8709 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8710 file, linenum, args[0]);
8711 goto out_err;
8712 }
8713 rule->arg.nice = atoi(args[cur_arg]);
8714 if (rule->arg.nice < -1024)
8715 rule->arg.nice = -1024;
8716 else if (rule->arg.nice > 1024)
8717 rule->arg.nice = 1024;
8718 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008719 } else if (!strcmp(args[0], "set-tos")) {
8720#ifdef IP_TOS
8721 char *err;
8722 rule->action = HTTP_REQ_ACT_SET_TOS;
8723 cur_arg = 1;
8724
8725 if (!*args[cur_arg] ||
8726 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8727 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8728 file, linenum, args[0]);
8729 goto out_err;
8730 }
8731
8732 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8733 if (err && *err != '\0') {
8734 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8735 file, linenum, err, args[0]);
8736 goto out_err;
8737 }
8738 cur_arg++;
8739#else
8740 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8741 goto out_err;
8742#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008743 } else if (!strcmp(args[0], "set-mark")) {
8744#ifdef SO_MARK
8745 char *err;
8746 rule->action = HTTP_REQ_ACT_SET_MARK;
8747 cur_arg = 1;
8748
8749 if (!*args[cur_arg] ||
8750 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8751 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8752 file, linenum, args[0]);
8753 goto out_err;
8754 }
8755
8756 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8757 if (err && *err != '\0') {
8758 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8759 file, linenum, err, args[0]);
8760 goto out_err;
8761 }
8762 cur_arg++;
8763 global.last_checks |= LSTCHK_NETADM;
8764#else
8765 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8766 goto out_err;
8767#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008768 } else if (!strcmp(args[0], "set-log-level")) {
8769 rule->action = HTTP_REQ_ACT_SET_LOGL;
8770 cur_arg = 1;
8771
8772 if (!*args[cur_arg] ||
8773 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8774 bad_log_level:
8775 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8776 file, linenum, args[0]);
8777 goto out_err;
8778 }
8779 if (strcmp(args[cur_arg], "silent") == 0)
8780 rule->arg.loglevel = -1;
8781 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8782 goto bad_log_level;
8783 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008784 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8785 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8786 cur_arg = 1;
8787
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008788 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8789 (*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 +01008790 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8791 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008792 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008793 }
8794
8795 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8796 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8797 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008798
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008799 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008800 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008801 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8802 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008803 free(proxy->conf.lfs_file);
8804 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8805 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008806 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008807 } else if (strcmp(args[0], "del-header") == 0) {
8808 rule->action = HTTP_REQ_ACT_DEL_HDR;
8809 cur_arg = 1;
8810
8811 if (!*args[cur_arg] ||
8812 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8813 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8814 file, linenum, args[0]);
8815 goto out_err;
8816 }
8817
8818 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8819 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8820
8821 proxy->conf.args.ctx = ARGC_HRQ;
8822 free(proxy->conf.lfs_file);
8823 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8824 proxy->conf.lfs_line = proxy->conf.args.line;
8825 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008826 } else if (strcmp(args[0], "redirect") == 0) {
8827 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008828 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008829
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008830 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008831 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8832 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8833 goto out_err;
8834 }
8835
8836 /* this redirect rule might already contain a parsed condition which
8837 * we'll pass to the http-request rule.
8838 */
8839 rule->action = HTTP_REQ_ACT_REDIR;
8840 rule->arg.redir = redir;
8841 rule->cond = redir->cond;
8842 redir->cond = NULL;
8843 cur_arg = 2;
8844 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008845 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8846 /* http-request add-acl(<reference (acl name)>) <key pattern> */
8847 rule->action = HTTP_REQ_ACT_ADD_ACL;
8848 /*
8849 * '+ 8' for 'add-acl('
8850 * '- 9' for 'add-acl(' + trailing ')'
8851 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008852 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008853
8854 cur_arg = 1;
8855
8856 if (!*args[cur_arg] ||
8857 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8858 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8859 file, linenum, args[0]);
8860 goto out_err;
8861 }
8862
8863 LIST_INIT(&rule->arg.map.key);
8864 proxy->conf.args.ctx = ARGC_HRQ;
8865 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8866 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8867 file, linenum);
8868 free(proxy->conf.lfs_file);
8869 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8870 proxy->conf.lfs_line = proxy->conf.args.line;
8871 cur_arg += 1;
8872 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8873 /* http-request del-acl(<reference (acl name)>) <key pattern> */
8874 rule->action = HTTP_REQ_ACT_DEL_ACL;
8875 /*
8876 * '+ 8' for 'del-acl('
8877 * '- 9' for 'del-acl(' + trailing ')'
8878 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008879 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008880
8881 cur_arg = 1;
8882
8883 if (!*args[cur_arg] ||
8884 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8885 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8886 file, linenum, args[0]);
8887 goto out_err;
8888 }
8889
8890 LIST_INIT(&rule->arg.map.key);
8891 proxy->conf.args.ctx = ARGC_HRQ;
8892 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8893 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8894 file, linenum);
8895 free(proxy->conf.lfs_file);
8896 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8897 proxy->conf.lfs_line = proxy->conf.args.line;
8898 cur_arg += 1;
8899 } else if (strncmp(args[0], "del-map", 7) == 0) {
8900 /* http-request del-map(<reference (map name)>) <key pattern> */
8901 rule->action = HTTP_REQ_ACT_DEL_MAP;
8902 /*
8903 * '+ 8' for 'del-map('
8904 * '- 9' for 'del-map(' + trailing ')'
8905 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008906 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008907
8908 cur_arg = 1;
8909
8910 if (!*args[cur_arg] ||
8911 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8912 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8913 file, linenum, args[0]);
8914 goto out_err;
8915 }
8916
8917 LIST_INIT(&rule->arg.map.key);
8918 proxy->conf.args.ctx = ARGC_HRQ;
8919 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8920 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8921 file, linenum);
8922 free(proxy->conf.lfs_file);
8923 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8924 proxy->conf.lfs_line = proxy->conf.args.line;
8925 cur_arg += 1;
8926 } else if (strncmp(args[0], "set-map", 7) == 0) {
8927 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
8928 rule->action = HTTP_REQ_ACT_SET_MAP;
8929 /*
8930 * '+ 8' for 'set-map('
8931 * '- 9' for 'set-map(' + trailing ')'
8932 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008933 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008934
8935 cur_arg = 1;
8936
8937 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8938 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8939 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8940 file, linenum, args[0]);
8941 goto out_err;
8942 }
8943
8944 LIST_INIT(&rule->arg.map.key);
8945 LIST_INIT(&rule->arg.map.value);
8946 proxy->conf.args.ctx = ARGC_HRQ;
8947
8948 /* key pattern */
8949 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8950 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8951 file, linenum);
8952
8953 /* value pattern */
8954 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
8955 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8956 file, linenum);
8957 free(proxy->conf.lfs_file);
8958 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8959 proxy->conf.lfs_line = proxy->conf.args.line;
8960
8961 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008962 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8963 char *errmsg = NULL;
8964 cur_arg = 1;
8965 /* try in the module list */
8966 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
8967 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8968 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8969 free(errmsg);
8970 goto out_err;
8971 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008972 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008973 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', '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 +01008974 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008975 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008976 }
8977
8978 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8979 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008980 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008981
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008982 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8983 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8984 file, linenum, args[0], errmsg);
8985 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008986 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008987 }
8988 rule->cond = cond;
8989 }
8990 else if (*args[cur_arg]) {
8991 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8992 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8993 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008994 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008995 }
8996
8997 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008998 out_err:
8999 free(rule);
9000 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009001}
9002
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009003/* parse an "http-respose" rule */
9004struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9005{
9006 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009007 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009008 int cur_arg;
9009
9010 rule = calloc(1, sizeof(*rule));
9011 if (!rule) {
9012 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9013 goto out_err;
9014 }
9015
9016 if (!strcmp(args[0], "allow")) {
9017 rule->action = HTTP_RES_ACT_ALLOW;
9018 cur_arg = 1;
9019 } else if (!strcmp(args[0], "deny")) {
9020 rule->action = HTTP_RES_ACT_DENY;
9021 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009022 } else if (!strcmp(args[0], "set-nice")) {
9023 rule->action = HTTP_RES_ACT_SET_NICE;
9024 cur_arg = 1;
9025
9026 if (!*args[cur_arg] ||
9027 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9028 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9029 file, linenum, args[0]);
9030 goto out_err;
9031 }
9032 rule->arg.nice = atoi(args[cur_arg]);
9033 if (rule->arg.nice < -1024)
9034 rule->arg.nice = -1024;
9035 else if (rule->arg.nice > 1024)
9036 rule->arg.nice = 1024;
9037 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009038 } else if (!strcmp(args[0], "set-tos")) {
9039#ifdef IP_TOS
9040 char *err;
9041 rule->action = HTTP_RES_ACT_SET_TOS;
9042 cur_arg = 1;
9043
9044 if (!*args[cur_arg] ||
9045 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9046 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9047 file, linenum, args[0]);
9048 goto out_err;
9049 }
9050
9051 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9052 if (err && *err != '\0') {
9053 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9054 file, linenum, err, args[0]);
9055 goto out_err;
9056 }
9057 cur_arg++;
9058#else
9059 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9060 goto out_err;
9061#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009062 } else if (!strcmp(args[0], "set-mark")) {
9063#ifdef SO_MARK
9064 char *err;
9065 rule->action = HTTP_RES_ACT_SET_MARK;
9066 cur_arg = 1;
9067
9068 if (!*args[cur_arg] ||
9069 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9070 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9071 file, linenum, args[0]);
9072 goto out_err;
9073 }
9074
9075 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9076 if (err && *err != '\0') {
9077 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9078 file, linenum, err, args[0]);
9079 goto out_err;
9080 }
9081 cur_arg++;
9082 global.last_checks |= LSTCHK_NETADM;
9083#else
9084 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9085 goto out_err;
9086#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009087 } else if (!strcmp(args[0], "set-log-level")) {
9088 rule->action = HTTP_RES_ACT_SET_LOGL;
9089 cur_arg = 1;
9090
9091 if (!*args[cur_arg] ||
9092 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9093 bad_log_level:
9094 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9095 file, linenum, args[0]);
9096 goto out_err;
9097 }
9098 if (strcmp(args[cur_arg], "silent") == 0)
9099 rule->arg.loglevel = -1;
9100 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9101 goto bad_log_level;
9102 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009103 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9104 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9105 cur_arg = 1;
9106
9107 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9108 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9109 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9110 file, linenum, args[0]);
9111 goto out_err;
9112 }
9113
9114 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9115 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9116 LIST_INIT(&rule->arg.hdr_add.fmt);
9117
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009118 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009119 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009120 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9121 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009122 free(proxy->conf.lfs_file);
9123 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9124 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009125 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009126 } else if (strcmp(args[0], "del-header") == 0) {
9127 rule->action = HTTP_RES_ACT_DEL_HDR;
9128 cur_arg = 1;
9129
9130 if (!*args[cur_arg] ||
9131 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9132 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9133 file, linenum, args[0]);
9134 goto out_err;
9135 }
9136
9137 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9138 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9139
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009140 proxy->conf.args.ctx = ARGC_HRS;
9141 free(proxy->conf.lfs_file);
9142 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9143 proxy->conf.lfs_line = proxy->conf.args.line;
9144 cur_arg += 1;
9145 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9146 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9147 rule->action = HTTP_RES_ACT_ADD_ACL;
9148 /*
9149 * '+ 8' for 'add-acl('
9150 * '- 9' for 'add-acl(' + trailing ')'
9151 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009152 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009153
9154 cur_arg = 1;
9155
9156 if (!*args[cur_arg] ||
9157 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9158 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9159 file, linenum, args[0]);
9160 goto out_err;
9161 }
9162
9163 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009164 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009165 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9166 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9167 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009168 free(proxy->conf.lfs_file);
9169 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9170 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009171
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009172 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009173 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9174 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9175 rule->action = HTTP_RES_ACT_DEL_ACL;
9176 /*
9177 * '+ 8' for 'del-acl('
9178 * '- 9' for 'del-acl(' + trailing ')'
9179 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009180 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009181
9182 cur_arg = 1;
9183
9184 if (!*args[cur_arg] ||
9185 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9186 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9187 file, linenum, args[0]);
9188 goto out_err;
9189 }
9190
9191 LIST_INIT(&rule->arg.map.key);
9192 proxy->conf.args.ctx = ARGC_HRS;
9193 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9194 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9195 file, linenum);
9196 free(proxy->conf.lfs_file);
9197 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9198 proxy->conf.lfs_line = proxy->conf.args.line;
9199 cur_arg += 1;
9200 } else if (strncmp(args[0], "del-map", 7) == 0) {
9201 /* http-response del-map(<reference (map name)>) <key pattern> */
9202 rule->action = HTTP_RES_ACT_DEL_MAP;
9203 /*
9204 * '+ 8' for 'del-map('
9205 * '- 9' for 'del-map(' + trailing ')'
9206 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009207 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009208
9209 cur_arg = 1;
9210
9211 if (!*args[cur_arg] ||
9212 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9213 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9214 file, linenum, args[0]);
9215 goto out_err;
9216 }
9217
9218 LIST_INIT(&rule->arg.map.key);
9219 proxy->conf.args.ctx = ARGC_HRS;
9220 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9221 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9222 file, linenum);
9223 free(proxy->conf.lfs_file);
9224 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9225 proxy->conf.lfs_line = proxy->conf.args.line;
9226 cur_arg += 1;
9227 } else if (strncmp(args[0], "set-map", 7) == 0) {
9228 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9229 rule->action = HTTP_RES_ACT_SET_MAP;
9230 /*
9231 * '+ 8' for 'set-map('
9232 * '- 9' for 'set-map(' + trailing ')'
9233 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009234 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009235
9236 cur_arg = 1;
9237
9238 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9239 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9240 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9241 file, linenum, args[0]);
9242 goto out_err;
9243 }
9244
9245 LIST_INIT(&rule->arg.map.key);
9246 LIST_INIT(&rule->arg.map.value);
9247
9248 proxy->conf.args.ctx = ARGC_HRS;
9249
9250 /* key pattern */
9251 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9252 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9253 file, linenum);
9254
9255 /* value pattern */
9256 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9257 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9258 file, linenum);
9259
9260 free(proxy->conf.lfs_file);
9261 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9262 proxy->conf.lfs_line = proxy->conf.args.line;
9263
9264 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009265 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9266 char *errmsg = NULL;
9267 cur_arg = 1;
9268 /* try in the module list */
9269 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9270 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9271 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9272 free(errmsg);
9273 goto out_err;
9274 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009275 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009276 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', '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 +02009277 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9278 goto out_err;
9279 }
9280
9281 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9282 struct acl_cond *cond;
9283 char *errmsg = NULL;
9284
9285 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9286 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9287 file, linenum, args[0], errmsg);
9288 free(errmsg);
9289 goto out_err;
9290 }
9291 rule->cond = cond;
9292 }
9293 else if (*args[cur_arg]) {
9294 Alert("parsing [%s:%d]: 'http-response %s' expects"
9295 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9296 file, linenum, args[0], args[cur_arg]);
9297 goto out_err;
9298 }
9299
9300 return rule;
9301 out_err:
9302 free(rule);
9303 return NULL;
9304}
9305
Willy Tarreau4baae242012-12-27 12:00:31 +01009306/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009307 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9308 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009309 */
9310struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009311 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009312{
9313 struct redirect_rule *rule;
9314 int cur_arg;
9315 int type = REDIRECT_TYPE_NONE;
9316 int code = 302;
9317 const char *destination = NULL;
9318 const char *cookie = NULL;
9319 int cookie_set = 0;
9320 unsigned int flags = REDIRECT_FLAG_NONE;
9321 struct acl_cond *cond = NULL;
9322
9323 cur_arg = 0;
9324 while (*(args[cur_arg])) {
9325 if (strcmp(args[cur_arg], "location") == 0) {
9326 if (!*args[cur_arg + 1])
9327 goto missing_arg;
9328
9329 type = REDIRECT_TYPE_LOCATION;
9330 cur_arg++;
9331 destination = args[cur_arg];
9332 }
9333 else if (strcmp(args[cur_arg], "prefix") == 0) {
9334 if (!*args[cur_arg + 1])
9335 goto missing_arg;
9336
9337 type = REDIRECT_TYPE_PREFIX;
9338 cur_arg++;
9339 destination = args[cur_arg];
9340 }
9341 else if (strcmp(args[cur_arg], "scheme") == 0) {
9342 if (!*args[cur_arg + 1])
9343 goto missing_arg;
9344
9345 type = REDIRECT_TYPE_SCHEME;
9346 cur_arg++;
9347 destination = args[cur_arg];
9348 }
9349 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9350 if (!*args[cur_arg + 1])
9351 goto missing_arg;
9352
9353 cur_arg++;
9354 cookie = args[cur_arg];
9355 cookie_set = 1;
9356 }
9357 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9358 if (!*args[cur_arg + 1])
9359 goto missing_arg;
9360
9361 cur_arg++;
9362 cookie = args[cur_arg];
9363 cookie_set = 0;
9364 }
9365 else if (strcmp(args[cur_arg], "code") == 0) {
9366 if (!*args[cur_arg + 1])
9367 goto missing_arg;
9368
9369 cur_arg++;
9370 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009371 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009372 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009373 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009374 args[cur_arg - 1], args[cur_arg]);
9375 return NULL;
9376 }
9377 }
9378 else if (!strcmp(args[cur_arg],"drop-query")) {
9379 flags |= REDIRECT_FLAG_DROP_QS;
9380 }
9381 else if (!strcmp(args[cur_arg],"append-slash")) {
9382 flags |= REDIRECT_FLAG_APPEND_SLASH;
9383 }
9384 else if (strcmp(args[cur_arg], "if") == 0 ||
9385 strcmp(args[cur_arg], "unless") == 0) {
9386 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9387 if (!cond) {
9388 memprintf(errmsg, "error in condition: %s", *errmsg);
9389 return NULL;
9390 }
9391 break;
9392 }
9393 else {
9394 memprintf(errmsg,
9395 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9396 args[cur_arg]);
9397 return NULL;
9398 }
9399 cur_arg++;
9400 }
9401
9402 if (type == REDIRECT_TYPE_NONE) {
9403 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9404 return NULL;
9405 }
9406
9407 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9408 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009409 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009410
9411 if (!use_fmt) {
9412 /* old-style static redirect rule */
9413 rule->rdr_str = strdup(destination);
9414 rule->rdr_len = strlen(destination);
9415 }
9416 else {
9417 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009418
9419 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9420 * if prefix == "/", we don't want to add anything, otherwise it
9421 * makes it hard for the user to configure a self-redirection.
9422 */
9423 proxy->conf.args.ctx = ARGC_RDR;
9424 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009425 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009426 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9427 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009428 free(curproxy->conf.lfs_file);
9429 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9430 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009431 }
9432 }
9433
Willy Tarreau4baae242012-12-27 12:00:31 +01009434 if (cookie) {
9435 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9436 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9437 */
9438 rule->cookie_len = strlen(cookie);
9439 if (cookie_set) {
9440 rule->cookie_str = malloc(rule->cookie_len + 10);
9441 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9442 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9443 rule->cookie_len += 9;
9444 } else {
9445 rule->cookie_str = malloc(rule->cookie_len + 21);
9446 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9447 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9448 rule->cookie_len += 20;
9449 }
9450 }
9451 rule->type = type;
9452 rule->code = code;
9453 rule->flags = flags;
9454 LIST_INIT(&rule->list);
9455 return rule;
9456
9457 missing_arg:
9458 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9459 return NULL;
9460}
9461
Willy Tarreau8797c062007-05-07 00:55:35 +02009462/************************************************************************/
9463/* The code below is dedicated to ACL parsing and matching */
9464/************************************************************************/
9465
9466
Willy Tarreau14174bc2012-04-16 14:34:04 +02009467/* This function ensures that the prerequisites for an L7 fetch are ready,
9468 * which means that a request or response is ready. If some data is missing,
9469 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009470 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9471 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009472 *
9473 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009474 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9475 * decide whether or not an HTTP message is present ;
9476 * 0 if the requested data cannot be fetched or if it is certain that
9477 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009478 * 1 if an HTTP message is ready
9479 */
9480static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009481smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009482 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009483{
9484 struct http_txn *txn = l7;
9485 struct http_msg *msg = &txn->req;
9486
9487 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9488 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9489 */
9490
9491 if (unlikely(!s || !txn))
9492 return 0;
9493
9494 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009495 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009496
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009497 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009498 if (unlikely(!s->req))
9499 return 0;
9500
Willy Tarreauaae75e32013-03-29 12:31:49 +01009501 /* If the buffer does not leave enough free space at the end,
9502 * we must first realign it.
9503 */
9504 if (s->req->buf->p > s->req->buf->data &&
9505 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9506 buffer_slow_realign(s->req->buf);
9507
Willy Tarreau14174bc2012-04-16 14:34:04 +02009508 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009509 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009510 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009511
9512 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009513 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009514 http_msg_analyzer(msg, &txn->hdr_idx);
9515
9516 /* Still no valid request ? */
9517 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009518 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009519 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009520 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009521 }
9522 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009523 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009524 return 0;
9525 }
9526
9527 /* OK we just got a valid HTTP request. We have some minor
9528 * preparation to perform so that further checks can rely
9529 * on HTTP tests.
9530 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009531
9532 /* If the request was parsed but was too large, we must absolutely
9533 * return an error so that it is not processed. At the moment this
9534 * cannot happen, but if the parsers are to change in the future,
9535 * we want this check to be maintained.
9536 */
9537 if (unlikely(s->req->buf->i + s->req->buf->p >
9538 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9539 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009540 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009541 return 1;
9542 }
9543
Willy Tarreau9b28e032012-10-12 23:49:43 +02009544 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009545 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9546 s->flags |= SN_REDIRECTABLE;
9547
Willy Tarreau506d0502013-07-06 13:29:24 +02009548 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9549 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009550 }
9551
Willy Tarreau506d0502013-07-06 13:29:24 +02009552 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009553 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009554 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009555
9556 /* otherwise everything's ready for the request */
9557 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009558 else {
9559 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009560 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9561 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009562 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009563 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009564 }
9565
9566 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009567 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009568 return 1;
9569}
Willy Tarreau8797c062007-05-07 00:55:35 +02009570
Willy Tarreauc0239e02012-04-16 14:42:55 +02009571#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009572 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 +02009573
Willy Tarreau24e32d82012-04-23 23:55:44 +02009574#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009575 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 +02009576
Willy Tarreau8797c062007-05-07 00:55:35 +02009577
9578/* 1. Check on METHOD
9579 * We use the pre-parsed method if it is known, and store its number as an
9580 * integer. If it is unknown, we use the pointer and the length.
9581 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009582static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009583{
9584 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009585 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009586
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009587 len = strlen(text);
9588 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009589
9590 pattern->val.i = meth;
9591 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009592 trash = get_trash_chunk();
9593 if (trash->size < len) {
9594 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9595 len, trash->size);
9596 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009597 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009598 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009599 pattern->len = len;
9600 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009601 else {
9602 pattern->ptr.str = NULL;
9603 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009604 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009605 return 1;
9606}
9607
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009608/* This function fetches the method of current HTTP request and stores
9609 * it in the global pattern struct as a chunk. There are two possibilities :
9610 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9611 * in <len> and <ptr> is NULL ;
9612 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9613 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009614 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009615 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009616static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009617smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009618 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009619{
9620 int meth;
9621 struct http_txn *txn = l7;
9622
Willy Tarreau24e32d82012-04-23 23:55:44 +02009623 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009624
Willy Tarreau8797c062007-05-07 00:55:35 +02009625 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009626 smp->type = SMP_T_METH;
9627 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009628 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009629 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9630 /* ensure the indexes are not affected */
9631 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009632 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009633 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9634 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009635 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009636 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009637 return 1;
9638}
9639
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009640/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009641static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009642{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009643 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009644 struct pattern_list *lst;
9645 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009646
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009647 list_for_each_entry(lst, &expr->patterns, list) {
9648 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009649
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009650 /* well-known method */
9651 if (pattern->val.i != HTTP_METH_OTHER) {
9652 if (smp->data.meth.meth == pattern->val.i)
9653 return pattern;
9654 else
9655 continue;
9656 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009657
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009658 /* Other method, we must compare the strings */
9659 if (pattern->len != smp->data.meth.str.len)
9660 continue;
9661
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009662 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009663 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9664 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9665 return pattern;
9666 }
9667 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009668}
9669
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009670static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009671smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009672 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009673{
9674 struct http_txn *txn = l7;
9675 char *ptr;
9676 int len;
9677
Willy Tarreauc0239e02012-04-16 14:42:55 +02009678 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009679
Willy Tarreau8797c062007-05-07 00:55:35 +02009680 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009681 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009682
9683 while ((len-- > 0) && (*ptr++ != '/'));
9684 if (len <= 0)
9685 return 0;
9686
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009687 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009688 smp->data.str.str = ptr;
9689 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009690
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009691 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009692 return 1;
9693}
9694
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009695static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009696smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009697 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009698{
9699 struct http_txn *txn = l7;
9700 char *ptr;
9701 int len;
9702
Willy Tarreauc0239e02012-04-16 14:42:55 +02009703 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009704
Willy Tarreauf26b2522012-12-14 08:33:14 +01009705 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9706 return 0;
9707
Willy Tarreau8797c062007-05-07 00:55:35 +02009708 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009709 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009710
9711 while ((len-- > 0) && (*ptr++ != '/'));
9712 if (len <= 0)
9713 return 0;
9714
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009715 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009716 smp->data.str.str = ptr;
9717 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009718
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009719 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009720 return 1;
9721}
9722
9723/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009724static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009725smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009726 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009727{
9728 struct http_txn *txn = l7;
9729 char *ptr;
9730 int len;
9731
Willy Tarreauc0239e02012-04-16 14:42:55 +02009732 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009733
Willy Tarreauf26b2522012-12-14 08:33:14 +01009734 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9735 return 0;
9736
Willy Tarreau8797c062007-05-07 00:55:35 +02009737 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009738 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009739
Willy Tarreauf853c462012-04-23 18:53:56 +02009740 smp->type = SMP_T_UINT;
9741 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009742 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009743 return 1;
9744}
9745
9746/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009747static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009748smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009749 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009750{
9751 struct http_txn *txn = l7;
9752
Willy Tarreauc0239e02012-04-16 14:42:55 +02009753 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009754
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009755 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009756 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009757 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009758 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009759 return 1;
9760}
9761
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009762static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009763smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009764 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009765{
9766 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009767 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009768
Willy Tarreauc0239e02012-04-16 14:42:55 +02009769 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009770
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009771 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 +02009772 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009773 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009774
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009775 smp->type = SMP_T_IPV4;
9776 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009777 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009778 return 1;
9779}
9780
9781static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009782smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009783 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009784{
9785 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009786 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009787
Willy Tarreauc0239e02012-04-16 14:42:55 +02009788 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009789
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009790 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 +02009791 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9792 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009793
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009794 smp->type = SMP_T_UINT;
9795 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009796 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009797 return 1;
9798}
9799
Willy Tarreau185b5c42012-04-26 15:11:51 +02009800/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9801 * Accepts an optional argument of type string containing the header field name,
9802 * and an optional argument of type signed or unsigned integer to request an
9803 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009804 * headers are considered from the first one. It does not stop on commas and
9805 * returns full lines instead (useful for User-Agent or Date for example).
9806 */
9807static int
9808smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009809 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009810{
9811 struct http_txn *txn = l7;
9812 struct hdr_idx *idx = &txn->hdr_idx;
9813 struct hdr_ctx *ctx = smp->ctx.a[0];
9814 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9815 int occ = 0;
9816 const char *name_str = NULL;
9817 int name_len = 0;
9818
9819 if (!ctx) {
9820 /* first call */
9821 ctx = &static_hdr_ctx;
9822 ctx->idx = 0;
9823 smp->ctx.a[0] = ctx;
9824 }
9825
9826 if (args) {
9827 if (args[0].type != ARGT_STR)
9828 return 0;
9829 name_str = args[0].data.str.str;
9830 name_len = args[0].data.str.len;
9831
9832 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9833 occ = args[1].data.uint;
9834 }
9835
9836 CHECK_HTTP_MESSAGE_FIRST();
9837
9838 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9839 /* search for header from the beginning */
9840 ctx->idx = 0;
9841
9842 if (!occ && !(opt & SMP_OPT_ITERATE))
9843 /* no explicit occurrence and single fetch => last header by default */
9844 occ = -1;
9845
9846 if (!occ)
9847 /* prepare to report multiple occurrences for ACL fetches */
9848 smp->flags |= SMP_F_NOT_LAST;
9849
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009850 smp->type = SMP_T_STR;
9851 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009852 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9853 return 1;
9854
9855 smp->flags &= ~SMP_F_NOT_LAST;
9856 return 0;
9857}
9858
9859/* 6. Check on HTTP header count. The number of occurrences is returned.
9860 * Accepts exactly 1 argument of type string. It does not stop on commas and
9861 * returns full lines instead (useful for User-Agent or Date for example).
9862 */
9863static int
9864smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009865 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009866{
9867 struct http_txn *txn = l7;
9868 struct hdr_idx *idx = &txn->hdr_idx;
9869 struct hdr_ctx ctx;
9870 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9871 int cnt;
9872
9873 if (!args || args->type != ARGT_STR)
9874 return 0;
9875
9876 CHECK_HTTP_MESSAGE_FIRST();
9877
9878 ctx.idx = 0;
9879 cnt = 0;
9880 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9881 cnt++;
9882
9883 smp->type = SMP_T_UINT;
9884 smp->data.uint = cnt;
9885 smp->flags = SMP_F_VOL_HDR;
9886 return 1;
9887}
9888
9889/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9890 * Accepts an optional argument of type string containing the header field name,
9891 * and an optional argument of type signed or unsigned integer to request an
9892 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009893 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009894 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009895static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009896smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009897 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009898{
9899 struct http_txn *txn = l7;
9900 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009901 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009902 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009903 int occ = 0;
9904 const char *name_str = NULL;
9905 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009906
Willy Tarreaua890d072013-04-02 12:01:06 +02009907 if (!ctx) {
9908 /* first call */
9909 ctx = &static_hdr_ctx;
9910 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009911 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009912 }
9913
Willy Tarreau185b5c42012-04-26 15:11:51 +02009914 if (args) {
9915 if (args[0].type != ARGT_STR)
9916 return 0;
9917 name_str = args[0].data.str.str;
9918 name_len = args[0].data.str.len;
9919
9920 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9921 occ = args[1].data.uint;
9922 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009923
Willy Tarreaue333ec92012-04-16 16:26:40 +02009924 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009925
Willy Tarreau185b5c42012-04-26 15:11:51 +02009926 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009927 /* search for header from the beginning */
9928 ctx->idx = 0;
9929
Willy Tarreau185b5c42012-04-26 15:11:51 +02009930 if (!occ && !(opt & SMP_OPT_ITERATE))
9931 /* no explicit occurrence and single fetch => last header by default */
9932 occ = -1;
9933
9934 if (!occ)
9935 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009936 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009937
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009938 smp->type = SMP_T_STR;
9939 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009940 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 +02009941 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009942
Willy Tarreau37406352012-04-23 16:16:37 +02009943 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009944 return 0;
9945}
9946
Willy Tarreauc11416f2007-06-17 16:58:38 +02009947/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009948 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009949 */
9950static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009951smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009952 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009953{
9954 struct http_txn *txn = l7;
9955 struct hdr_idx *idx = &txn->hdr_idx;
9956 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009957 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009958 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009959
Willy Tarreau24e32d82012-04-23 23:55:44 +02009960 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009961 return 0;
9962
Willy Tarreaue333ec92012-04-16 16:26:40 +02009963 CHECK_HTTP_MESSAGE_FIRST();
9964
Willy Tarreau33a7e692007-06-10 19:45:56 +02009965 ctx.idx = 0;
9966 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009967 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009968 cnt++;
9969
Willy Tarreauf853c462012-04-23 18:53:56 +02009970 smp->type = SMP_T_UINT;
9971 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009972 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009973 return 1;
9974}
9975
Willy Tarreau185b5c42012-04-26 15:11:51 +02009976/* Fetch an HTTP header's integer value. The integer value is returned. It
9977 * takes a mandatory argument of type string and an optional one of type int
9978 * to designate a specific occurrence. It returns an unsigned integer, which
9979 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009980 */
9981static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009982smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009983 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009984{
Willy Tarreauef38c392013-07-22 16:29:32 +02009985 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009986
Willy Tarreauf853c462012-04-23 18:53:56 +02009987 if (ret > 0) {
9988 smp->type = SMP_T_UINT;
9989 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9990 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009991
Willy Tarreaud53e2422012-04-16 17:21:11 +02009992 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009993}
9994
Cyril Bonté69fa9922012-10-25 00:01:06 +02009995/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9996 * and an optional one of type int to designate a specific occurrence.
9997 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009998 */
9999static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010000smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010001 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010002{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010003 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010004
Willy Tarreauef38c392013-07-22 16:29:32 +020010005 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010006 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10007 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010008 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010009 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010010 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010011 if (smp->data.str.len < temp->size - 1) {
10012 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10013 temp->str[smp->data.str.len] = '\0';
10014 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10015 smp->type = SMP_T_IPV6;
10016 break;
10017 }
10018 }
10019 }
10020
Willy Tarreaud53e2422012-04-16 17:21:11 +020010021 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010022 if (!(smp->flags & SMP_F_NOT_LAST))
10023 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010024 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010025 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010026}
10027
Willy Tarreau737b0c12007-06-10 21:28:46 +020010028/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10029 * the first '/' after the possible hostname, and ends before the possible '?'.
10030 */
10031static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010032smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010033 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010034{
10035 struct http_txn *txn = l7;
10036 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010037
Willy Tarreauc0239e02012-04-16 14:42:55 +020010038 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010039
Willy Tarreau9b28e032012-10-12 23:49:43 +020010040 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010041 ptr = http_get_path(txn);
10042 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010043 return 0;
10044
10045 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010046 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010047 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010048
10049 while (ptr < end && *ptr != '?')
10050 ptr++;
10051
Willy Tarreauf853c462012-04-23 18:53:56 +020010052 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010053 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010054 return 1;
10055}
10056
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010057/* This produces a concatenation of the first occurrence of the Host header
10058 * followed by the path component if it begins with a slash ('/'). This means
10059 * that '*' will not be added, resulting in exactly the first Host entry.
10060 * If no Host header is found, then the path is returned as-is. The returned
10061 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010062 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010063 */
10064static int
10065smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010066 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010067{
10068 struct http_txn *txn = l7;
10069 char *ptr, *end, *beg;
10070 struct hdr_ctx ctx;
10071
10072 CHECK_HTTP_MESSAGE_FIRST();
10073
10074 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010075 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010076 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010077
10078 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010079 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010080 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010081 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010082 smp->data.str.len = ctx.vlen;
10083
10084 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010085 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010086 beg = http_get_path(txn);
10087 if (!beg)
10088 beg = end;
10089
10090 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10091
10092 if (beg < ptr && *beg == '/') {
10093 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10094 smp->data.str.len += ptr - beg;
10095 }
10096
10097 smp->flags = SMP_F_VOL_1ST;
10098 return 1;
10099}
10100
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010101/* This produces a 32-bit hash of the concatenation of the first occurrence of
10102 * the Host header followed by the path component if it begins with a slash ('/').
10103 * This means that '*' will not be added, resulting in exactly the first Host
10104 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010105 * is hashed using the path hash followed by a full avalanche hash and provides a
10106 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010107 * high-traffic sites without having to store whole paths.
10108 */
10109static int
10110smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010111 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010112{
10113 struct http_txn *txn = l7;
10114 struct hdr_ctx ctx;
10115 unsigned int hash = 0;
10116 char *ptr, *beg, *end;
10117 int len;
10118
10119 CHECK_HTTP_MESSAGE_FIRST();
10120
10121 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010122 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010123 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10124 ptr = ctx.line + ctx.val;
10125 len = ctx.vlen;
10126 while (len--)
10127 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10128 }
10129
10130 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010131 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010132 beg = http_get_path(txn);
10133 if (!beg)
10134 beg = end;
10135
10136 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10137
10138 if (beg < ptr && *beg == '/') {
10139 while (beg < ptr)
10140 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10141 }
10142 hash = full_hash(hash);
10143
10144 smp->type = SMP_T_UINT;
10145 smp->data.uint = hash;
10146 smp->flags = SMP_F_VOL_1ST;
10147 return 1;
10148}
10149
Willy Tarreau4a550602012-12-09 14:53:32 +010010150/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010151 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10152 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10153 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010154 * that in environments where IPv6 is insignificant, truncating the output to
10155 * 8 bytes would still work.
10156 */
10157static int
10158smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010159 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010160{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010161 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010162 struct connection *cli_conn = objt_conn(l4->si[0].end);
10163
10164 if (!cli_conn)
10165 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010166
Willy Tarreauef38c392013-07-22 16:29:32 +020010167 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010168 return 0;
10169
Willy Tarreau47ca5452012-12-23 20:22:19 +010010170 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +010010171 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10172 temp->len += sizeof(smp->data.uint);
10173
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010174 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010175 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010176 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010177 temp->len += 4;
10178 break;
10179 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010180 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010181 temp->len += 16;
10182 break;
10183 default:
10184 return 0;
10185 }
10186
10187 smp->data.str = *temp;
10188 smp->type = SMP_T_BIN;
10189 return 1;
10190}
10191
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010192static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010193smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010194 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010195{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010196 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10197 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10198 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010199
Willy Tarreau24e32d82012-04-23 23:55:44 +020010200 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010201
Willy Tarreauf853c462012-04-23 18:53:56 +020010202 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010203 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010204 return 1;
10205}
10206
Willy Tarreau7f18e522010-10-22 20:04:13 +020010207/* return a valid test if the current request is the first one on the connection */
10208static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010209smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010210 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010211{
10212 if (!s)
10213 return 0;
10214
Willy Tarreauf853c462012-04-23 18:53:56 +020010215 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010216 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010217 return 1;
10218}
10219
Willy Tarreau34db1082012-04-19 17:16:54 +020010220/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010221static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010222smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010223 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010224{
10225
Willy Tarreau24e32d82012-04-23 23:55:44 +020010226 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010227 return 0;
10228
Willy Tarreauc0239e02012-04-16 14:42:55 +020010229 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010230
Willy Tarreauc0239e02012-04-16 14:42:55 +020010231 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010232 return 0;
10233
Willy Tarreauf853c462012-04-23 18:53:56 +020010234 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010235 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010236 return 1;
10237}
Willy Tarreau8797c062007-05-07 00:55:35 +020010238
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010239/* Accepts exactly 1 argument of type userlist */
10240static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010241smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010242 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010243{
10244
10245 if (!args || args->type != ARGT_USR)
10246 return 0;
10247
10248 CHECK_HTTP_MESSAGE_FIRST();
10249
10250 if (!get_http_auth(l4))
10251 return 0;
10252
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010253 /* if the user does not belong to the userlist or has a wrong password,
10254 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010255 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010256 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010257 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10258 return 0;
10259
10260 /* pat_match_auth() will need the user list */
10261 smp->ctx.a[0] = args->data.usr;
10262
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010263 smp->type = SMP_T_STR;
10264 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010265 smp->data.str.str = l4->txn.auth.user;
10266 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010267
10268 return 1;
10269}
10270
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010271/* Try to find the next occurrence of a cookie name in a cookie header value.
10272 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10273 * the cookie value is returned into *value and *value_l, and the function
10274 * returns a pointer to the next pointer to search from if the value was found.
10275 * Otherwise if the cookie was not found, NULL is returned and neither value
10276 * nor value_l are touched. The input <hdr> string should first point to the
10277 * header's value, and the <hdr_end> pointer must point to the first character
10278 * not part of the value. <list> must be non-zero if value may represent a list
10279 * of values (cookie headers). This makes it faster to abort parsing when no
10280 * list is expected.
10281 */
10282static char *
10283extract_cookie_value(char *hdr, const char *hdr_end,
10284 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010285 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010286{
10287 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10288 char *next;
10289
10290 /* we search at least a cookie name followed by an equal, and more
10291 * generally something like this :
10292 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10293 */
10294 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10295 /* Iterate through all cookies on this line */
10296
10297 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10298 att_beg++;
10299
10300 /* find att_end : this is the first character after the last non
10301 * space before the equal. It may be equal to hdr_end.
10302 */
10303 equal = att_end = att_beg;
10304
10305 while (equal < hdr_end) {
10306 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10307 break;
10308 if (http_is_spht[(unsigned char)*equal++])
10309 continue;
10310 att_end = equal;
10311 }
10312
10313 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10314 * is between <att_beg> and <equal>, both may be identical.
10315 */
10316
10317 /* look for end of cookie if there is an equal sign */
10318 if (equal < hdr_end && *equal == '=') {
10319 /* look for the beginning of the value */
10320 val_beg = equal + 1;
10321 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10322 val_beg++;
10323
10324 /* find the end of the value, respecting quotes */
10325 next = find_cookie_value_end(val_beg, hdr_end);
10326
10327 /* make val_end point to the first white space or delimitor after the value */
10328 val_end = next;
10329 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10330 val_end--;
10331 } else {
10332 val_beg = val_end = next = equal;
10333 }
10334
10335 /* We have nothing to do with attributes beginning with '$'. However,
10336 * they will automatically be removed if a header before them is removed,
10337 * since they're supposed to be linked together.
10338 */
10339 if (*att_beg == '$')
10340 continue;
10341
10342 /* Ignore cookies with no equal sign */
10343 if (equal == next)
10344 continue;
10345
10346 /* Now we have the cookie name between att_beg and att_end, and
10347 * its value between val_beg and val_end.
10348 */
10349
10350 if (att_end - att_beg == cookie_name_l &&
10351 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10352 /* let's return this value and indicate where to go on from */
10353 *value = val_beg;
10354 *value_l = val_end - val_beg;
10355 return next + 1;
10356 }
10357
10358 /* Set-Cookie headers only have the name in the first attr=value part */
10359 if (!list)
10360 break;
10361 }
10362
10363 return NULL;
10364}
10365
William Lallemanda43ba4e2014-01-28 18:14:25 +010010366/* Fetch a captured HTTP request header. The index is the position of
10367 * the "capture" option in the configuration file
10368 */
10369static int
10370smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10371 const struct arg *args, struct sample *smp, const char *kw)
10372{
10373 struct proxy *fe = l4->fe;
10374 struct http_txn *txn = l7;
10375 int idx;
10376
10377 if (!args || args->type != ARGT_UINT)
10378 return 0;
10379
10380 idx = args->data.uint;
10381
10382 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10383 return 0;
10384
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010385 smp->type = SMP_T_STR;
10386 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010387 smp->data.str.str = txn->req.cap[idx];
10388 smp->data.str.len = strlen(txn->req.cap[idx]);
10389
10390 return 1;
10391}
10392
10393/* Fetch a captured HTTP response header. The index is the position of
10394 * the "capture" option in the configuration file
10395 */
10396static int
10397smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10398 const struct arg *args, struct sample *smp, const char *kw)
10399{
10400 struct proxy *fe = l4->fe;
10401 struct http_txn *txn = l7;
10402 int idx;
10403
10404 if (!args || args->type != ARGT_UINT)
10405 return 0;
10406
10407 idx = args->data.uint;
10408
10409 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10410 return 0;
10411
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010412 smp->type = SMP_T_STR;
10413 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010414 smp->data.str.str = txn->rsp.cap[idx];
10415 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10416
10417 return 1;
10418}
10419
William Lallemand65ad6e12014-01-31 15:08:02 +010010420/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10421static int
10422smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10423 const struct arg *args, struct sample *smp, const char *kw)
10424{
10425 struct chunk *temp;
10426 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010427 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010428
10429 if (!txn->uri)
10430 return 0;
10431
William Lallemand96a77852014-02-05 00:30:02 +010010432 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010433
William Lallemand96a77852014-02-05 00:30:02 +010010434 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10435 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010436
William Lallemand96a77852014-02-05 00:30:02 +010010437 temp = get_trash_chunk();
10438 temp->str = txn->uri;
10439 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010440 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010441 smp->type = SMP_T_STR;
10442 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010443
10444 return 1;
10445
10446}
10447
10448/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10449static int
10450smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10451 const struct arg *args, struct sample *smp, const char *kw)
10452{
10453 struct chunk *temp;
10454 struct http_txn *txn = l7;
10455 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010456
10457 if (!txn->uri)
10458 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010459
William Lallemand65ad6e12014-01-31 15:08:02 +010010460 ptr = txn->uri;
10461
10462 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10463 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010464
William Lallemand65ad6e12014-01-31 15:08:02 +010010465 if (!*ptr)
10466 return 0;
10467
10468 ptr++; /* skip the space */
10469
10470 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010471 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010472 if (!ptr)
10473 return 0;
10474 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10475 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010476
10477 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010478 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010479 smp->type = SMP_T_STR;
10480 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010481
10482 return 1;
10483}
10484
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010485/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10486 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10487 */
10488static int
10489smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10490 const struct arg *args, struct sample *smp, const char *kw)
10491{
10492 struct http_txn *txn = l7;
10493
10494 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10495 return 0;
10496
10497 if (txn->req.flags & HTTP_MSGF_VER_11)
10498 smp->data.str.str = "HTTP/1.1";
10499 else
10500 smp->data.str.str = "HTTP/1.0";
10501
10502 smp->data.str.len = 8;
10503 smp->type = SMP_T_STR;
10504 smp->flags = SMP_F_CONST;
10505 return 1;
10506
10507}
10508
10509/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10510 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10511 */
10512static int
10513smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10514 const struct arg *args, struct sample *smp, const char *kw)
10515{
10516 struct http_txn *txn = l7;
10517
10518 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10519 return 0;
10520
10521 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10522 smp->data.str.str = "HTTP/1.1";
10523 else
10524 smp->data.str.str = "HTTP/1.0";
10525
10526 smp->data.str.len = 8;
10527 smp->type = SMP_T_STR;
10528 smp->flags = SMP_F_CONST;
10529 return 1;
10530
10531}
10532
William Lallemand65ad6e12014-01-31 15:08:02 +010010533
Willy Tarreaue333ec92012-04-16 16:26:40 +020010534/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010535 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010536 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010537 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010538 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010539 * Accepts exactly 1 argument of type string. If the input options indicate
10540 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010541 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010542 */
10543static int
Willy Tarreau51539362012-05-08 12:46:28 +020010544smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010545 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010546{
10547 struct http_txn *txn = l7;
10548 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010549 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010550 const struct http_msg *msg;
10551 const char *hdr_name;
10552 int hdr_name_len;
10553 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010554 int occ = 0;
10555 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010556
Willy Tarreau24e32d82012-04-23 23:55:44 +020010557 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010558 return 0;
10559
Willy Tarreaua890d072013-04-02 12:01:06 +020010560 if (!ctx) {
10561 /* first call */
10562 ctx = &static_hdr_ctx;
10563 ctx->idx = 0;
10564 smp->ctx.a[2] = ctx;
10565 }
10566
Willy Tarreaue333ec92012-04-16 16:26:40 +020010567 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010568
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010569 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010570 msg = &txn->req;
10571 hdr_name = "Cookie";
10572 hdr_name_len = 6;
10573 } else {
10574 msg = &txn->rsp;
10575 hdr_name = "Set-Cookie";
10576 hdr_name_len = 10;
10577 }
10578
Willy Tarreau28376d62012-04-26 21:26:10 +020010579 if (!occ && !(opt & SMP_OPT_ITERATE))
10580 /* no explicit occurrence and single fetch => last cookie by default */
10581 occ = -1;
10582
10583 /* OK so basically here, either we want only one value and it's the
10584 * last one, or we want to iterate over all of them and we fetch the
10585 * next one.
10586 */
10587
Willy Tarreau9b28e032012-10-12 23:49:43 +020010588 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010589 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010590 /* search for the header from the beginning, we must first initialize
10591 * the search parameters.
10592 */
Willy Tarreau37406352012-04-23 16:16:37 +020010593 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010594 ctx->idx = 0;
10595 }
10596
Willy Tarreau28376d62012-04-26 21:26:10 +020010597 smp->flags |= SMP_F_VOL_HDR;
10598
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010599 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010600 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10601 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010602 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10603 goto out;
10604
Willy Tarreau24e32d82012-04-23 23:55:44 +020010605 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010606 continue;
10607
Willy Tarreau37406352012-04-23 16:16:37 +020010608 smp->ctx.a[0] = ctx->line + ctx->val;
10609 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010610 }
10611
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010612 smp->type = SMP_T_STR;
10613 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010614 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010615 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010616 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010617 &smp->data.str.str,
10618 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010619 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010620 found = 1;
10621 if (occ >= 0) {
10622 /* one value was returned into smp->data.str.{str,len} */
10623 smp->flags |= SMP_F_NOT_LAST;
10624 return 1;
10625 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010626 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010627 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010628 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010629 /* all cookie headers and values were scanned. If we're looking for the
10630 * last occurrence, we may return it now.
10631 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010632 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010633 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010634 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010635}
10636
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010637/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010638 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010639 * multiple cookies may be parsed on the same line. The returned sample is of
10640 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010641 */
10642static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010643smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010644 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010645{
10646 struct http_txn *txn = l7;
10647 struct hdr_idx *idx = &txn->hdr_idx;
10648 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010649 const struct http_msg *msg;
10650 const char *hdr_name;
10651 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010652 int cnt;
10653 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010654 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010655
Willy Tarreau24e32d82012-04-23 23:55:44 +020010656 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010657 return 0;
10658
Willy Tarreaue333ec92012-04-16 16:26:40 +020010659 CHECK_HTTP_MESSAGE_FIRST();
10660
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010661 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010662 msg = &txn->req;
10663 hdr_name = "Cookie";
10664 hdr_name_len = 6;
10665 } else {
10666 msg = &txn->rsp;
10667 hdr_name = "Set-Cookie";
10668 hdr_name_len = 10;
10669 }
10670
Willy Tarreau9b28e032012-10-12 23:49:43 +020010671 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010672 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010673 ctx.idx = 0;
10674 cnt = 0;
10675
10676 while (1) {
10677 /* Note: val_beg == NULL every time we need to fetch a new header */
10678 if (!val_beg) {
10679 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10680 break;
10681
Willy Tarreau24e32d82012-04-23 23:55:44 +020010682 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010683 continue;
10684
10685 val_beg = ctx.line + ctx.val;
10686 val_end = val_beg + ctx.vlen;
10687 }
10688
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010689 smp->type = SMP_T_STR;
10690 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010691 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010692 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010693 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010694 &smp->data.str.str,
10695 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010696 cnt++;
10697 }
10698 }
10699
Willy Tarreaub169eba2013-12-16 15:14:43 +010010700 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010701 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010702 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010703 return 1;
10704}
10705
Willy Tarreau51539362012-05-08 12:46:28 +020010706/* Fetch an cookie's integer value. The integer value is returned. It
10707 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10708 */
10709static int
10710smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010711 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010712{
Willy Tarreauef38c392013-07-22 16:29:32 +020010713 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010714
10715 if (ret > 0) {
10716 smp->type = SMP_T_UINT;
10717 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10718 }
10719
10720 return ret;
10721}
10722
Willy Tarreau8797c062007-05-07 00:55:35 +020010723/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010724/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010725/************************************************************************/
10726
David Cournapeau16023ee2010-12-23 20:55:41 +090010727/*
10728 * Given a path string and its length, find the position of beginning of the
10729 * query string. Returns NULL if no query string is found in the path.
10730 *
10731 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10732 *
10733 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10734 */
bedis4c75cca2012-10-05 08:38:24 +020010735static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010736{
10737 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010738
bedis4c75cca2012-10-05 08:38:24 +020010739 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010740 return p ? p + 1 : NULL;
10741}
10742
bedis4c75cca2012-10-05 08:38:24 +020010743static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010744{
bedis4c75cca2012-10-05 08:38:24 +020010745 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010746}
10747
10748/*
10749 * Given a url parameter, find the starting position of the first occurence,
10750 * or NULL if the parameter is not found.
10751 *
10752 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10753 * the function will return query_string+8.
10754 */
10755static char*
10756find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010757 char* url_param_name, size_t url_param_name_l,
10758 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010759{
10760 char *pos, *last;
10761
10762 pos = query_string;
10763 last = query_string + query_string_l - url_param_name_l - 1;
10764
10765 while (pos <= last) {
10766 if (pos[url_param_name_l] == '=') {
10767 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10768 return pos;
10769 pos += url_param_name_l + 1;
10770 }
bedis4c75cca2012-10-05 08:38:24 +020010771 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010772 pos++;
10773 pos++;
10774 }
10775 return NULL;
10776}
10777
10778/*
10779 * Given a url parameter name, returns its value and size into *value and
10780 * *value_l respectively, and returns non-zero. If the parameter is not found,
10781 * zero is returned and value/value_l are not touched.
10782 */
10783static int
10784find_url_param_value(char* path, size_t path_l,
10785 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010786 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010787{
10788 char *query_string, *qs_end;
10789 char *arg_start;
10790 char *value_start, *value_end;
10791
bedis4c75cca2012-10-05 08:38:24 +020010792 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010793 if (!query_string)
10794 return 0;
10795
10796 qs_end = path + path_l;
10797 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010798 url_param_name, url_param_name_l,
10799 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010800 if (!arg_start)
10801 return 0;
10802
10803 value_start = arg_start + url_param_name_l + 1;
10804 value_end = value_start;
10805
bedis4c75cca2012-10-05 08:38:24 +020010806 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010807 value_end++;
10808
10809 *value = value_start;
10810 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010811 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010812}
10813
10814static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010815smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010816 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010817{
bedis4c75cca2012-10-05 08:38:24 +020010818 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010819 struct http_txn *txn = l7;
10820 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010821
bedis4c75cca2012-10-05 08:38:24 +020010822 if (!args || args[0].type != ARGT_STR ||
10823 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010824 return 0;
10825
10826 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010827
bedis4c75cca2012-10-05 08:38:24 +020010828 if (args[1].type)
10829 delim = *args[1].data.str.str;
10830
Willy Tarreau9b28e032012-10-12 23:49:43 +020010831 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010832 args->data.str.str, args->data.str.len,
10833 &smp->data.str.str, &smp->data.str.len,
10834 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010835 return 0;
10836
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010837 smp->type = SMP_T_STR;
10838 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010839 return 1;
10840}
10841
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010842/* Return the signed integer value for the specified url parameter (see url_param
10843 * above).
10844 */
10845static int
10846smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010847 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010848{
Willy Tarreauef38c392013-07-22 16:29:32 +020010849 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010850
10851 if (ret > 0) {
10852 smp->type = SMP_T_UINT;
10853 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10854 }
10855
10856 return ret;
10857}
10858
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010859/* This produces a 32-bit hash of the concatenation of the first occurrence of
10860 * the Host header followed by the path component if it begins with a slash ('/').
10861 * This means that '*' will not be added, resulting in exactly the first Host
10862 * entry. If no Host header is found, then the path is used. The resulting value
10863 * is hashed using the url hash followed by a full avalanche hash and provides a
10864 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10865 * high-traffic sites without having to store whole paths.
10866 * this differs from the base32 functions in that it includes the url parameters
10867 * as well as the path
10868 */
10869static int
10870smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010871 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010872{
10873 struct http_txn *txn = l7;
10874 struct hdr_ctx ctx;
10875 unsigned int hash = 0;
10876 char *ptr, *beg, *end;
10877 int len;
10878
10879 CHECK_HTTP_MESSAGE_FIRST();
10880
10881 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010882 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010883 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10884 ptr = ctx.line + ctx.val;
10885 len = ctx.vlen;
10886 while (len--)
10887 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10888 }
10889
10890 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010891 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 +000010892 beg = http_get_path(txn);
10893 if (!beg)
10894 beg = end;
10895
10896 for (ptr = beg; ptr < end ; ptr++);
10897
10898 if (beg < ptr && *beg == '/') {
10899 while (beg < ptr)
10900 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10901 }
10902 hash = full_hash(hash);
10903
10904 smp->type = SMP_T_UINT;
10905 smp->data.uint = hash;
10906 smp->flags = SMP_F_VOL_1ST;
10907 return 1;
10908}
10909
10910/* This concatenates the source address with the 32-bit hash of the Host and
10911 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10912 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10913 * on the source address length. The URL hash is stored before the address so
10914 * that in environments where IPv6 is insignificant, truncating the output to
10915 * 8 bytes would still work.
10916 */
10917static int
10918smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010919 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010920{
10921 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010922 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010923
Willy Tarreaue155ec22013-11-18 18:33:22 +010010924 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010925 return 0;
10926
10927 temp = get_trash_chunk();
10928 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10929 temp->len += sizeof(smp->data.uint);
10930
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010931 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010932 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010933 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010934 temp->len += 4;
10935 break;
10936 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010937 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010938 temp->len += 16;
10939 break;
10940 default:
10941 return 0;
10942 }
10943
10944 smp->data.str = *temp;
10945 smp->type = SMP_T_BIN;
10946 return 1;
10947}
10948
Willy Tarreau185b5c42012-04-26 15:11:51 +020010949/* This function is used to validate the arguments passed to any "hdr" fetch
10950 * keyword. These keywords support an optional positive or negative occurrence
10951 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10952 * is assumed that the types are already the correct ones. Returns 0 on error,
10953 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10954 * error message in case of error, that the caller is responsible for freeing.
10955 * The initial location must either be freeable or NULL.
10956 */
10957static int val_hdr(struct arg *arg, char **err_msg)
10958{
10959 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010960 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010961 return 0;
10962 }
10963 return 1;
10964}
10965
Willy Tarreau276fae92013-07-25 14:36:01 +020010966/* takes an UINT value on input supposed to represent the time since EPOCH,
10967 * adds an optional offset found in args[0] and emits a string representing
10968 * the date in RFC-1123/5322 format.
10969 */
10970static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10971{
10972 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10973 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10974 struct chunk *temp;
10975 struct tm *tm;
10976 time_t curr_date = smp->data.uint;
10977
10978 /* add offset */
10979 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10980 curr_date += args[0].data.sint;
10981
10982 tm = gmtime(&curr_date);
10983
10984 temp = get_trash_chunk();
10985 temp->len = snprintf(temp->str, temp->size - temp->len,
10986 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10987 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10988 tm->tm_hour, tm->tm_min, tm->tm_sec);
10989
10990 smp->data.str = *temp;
10991 smp->type = SMP_T_STR;
10992 return 1;
10993}
10994
Thierry FOURNIERad903512014-04-11 17:51:01 +020010995/* Match language range with language tag. RFC2616 14.4:
10996 *
10997 * A language-range matches a language-tag if it exactly equals
10998 * the tag, or if it exactly equals a prefix of the tag such
10999 * that the first tag character following the prefix is "-".
11000 *
11001 * Return 1 if the strings match, else return 0.
11002 */
11003static inline int language_range_match(const char *range, int range_len,
11004 const char *tag, int tag_len)
11005{
11006 const char *end = range + range_len;
11007 const char *tend = tag + tag_len;
11008 while (range < end) {
11009 if (*range == '-' && tag == tend)
11010 return 1;
11011 if (*range != *tag || tag == tend)
11012 return 0;
11013 range++;
11014 tag++;
11015 }
11016 /* Return true only if the last char of the tag is matched. */
11017 return tag == tend;
11018}
11019
11020/* Arguments: The list of expected value, the number of parts returned and the separator */
11021static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11022{
11023 const char *al = smp->data.str.str;
11024 const char *end = al + smp->data.str.len;
11025 const char *token;
11026 int toklen;
11027 int qvalue;
11028 const char *str;
11029 const char *w;
11030 int best_q = 0;
11031
11032 /* Set the constant to the sample, because the output of the
11033 * function will be peek in the constant configuration string.
11034 */
11035 smp->flags |= SMP_F_CONST;
11036 smp->data.str.size = 0;
11037 smp->data.str.str = "";
11038 smp->data.str.len = 0;
11039
11040 /* Parse the accept language */
11041 while (1) {
11042
11043 /* Jump spaces, quit if the end is detected. */
11044 while (al < end && isspace(*al))
11045 al++;
11046 if (al >= end)
11047 break;
11048
11049 /* Start of the fisrt word. */
11050 token = al;
11051
11052 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
11053 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
11054 al++;
11055 if (al == token)
11056 goto expect_comma;
11057
11058 /* Length of the token. */
11059 toklen = al - token;
11060 qvalue = 1000;
11061
11062 /* Check if the token exists in the list. If the token not exists,
11063 * jump to the next token.
11064 */
11065 str = args[0].data.str.str;
11066 w = str;
11067 while (1) {
11068 if (*str == ';' || *str == '\0') {
11069 if (language_range_match(token, toklen, w, str-w))
11070 goto look_for_q;
11071 if (*str == '\0')
11072 goto expect_comma;
11073 w = str + 1;
11074 }
11075 str++;
11076 }
11077 goto expect_comma;
11078
11079look_for_q:
11080
11081 /* Jump spaces, quit if the end is detected. */
11082 while (al < end && isspace(*al))
11083 al++;
11084 if (al >= end)
11085 goto process_value;
11086
11087 /* If ',' is found, process the result */
11088 if (*al == ',')
11089 goto process_value;
11090
11091 /* If the character is different from ';', look
11092 * for the end of the header part in best effort.
11093 */
11094 if (*al != ';')
11095 goto expect_comma;
11096
11097 /* Assumes that the char is ';', now expect "q=". */
11098 al++;
11099
11100 /* Jump spaces, process value if the end is detected. */
11101 while (al < end && isspace(*al))
11102 al++;
11103 if (al >= end)
11104 goto process_value;
11105
11106 /* Expect 'q'. If no 'q', continue in best effort */
11107 if (*al != 'q')
11108 goto process_value;
11109 al++;
11110
11111 /* Jump spaces, process value if the end is detected. */
11112 while (al < end && isspace(*al))
11113 al++;
11114 if (al >= end)
11115 goto process_value;
11116
11117 /* Expect '='. If no '=', continue in best effort */
11118 if (*al != '=')
11119 goto process_value;
11120 al++;
11121
11122 /* Jump spaces, process value if the end is detected. */
11123 while (al < end && isspace(*al))
11124 al++;
11125 if (al >= end)
11126 goto process_value;
11127
11128 /* Parse the q value. */
11129 qvalue = parse_qvalue(al, &al);
11130
11131process_value:
11132
11133 /* If the new q value is the best q value, then store the associated
11134 * language in the response. If qvalue is the biggest value (1000),
11135 * break the process.
11136 */
11137 if (qvalue > best_q) {
11138 smp->data.str.str = (char *)w;
11139 smp->data.str.len = str - w;
11140 if (qvalue >= 1000)
11141 break;
11142 best_q = qvalue;
11143 }
11144
11145expect_comma:
11146
11147 /* Expect comma or end. If the end is detected, quit the loop. */
11148 while (al < end && *al != ',')
11149 al++;
11150 if (al >= end)
11151 break;
11152
11153 /* Comma is found, jump it and restart the analyzer. */
11154 al++;
11155 }
11156
11157 /* Set default value if required. */
11158 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11159 smp->data.str.str = args[1].data.str.str;
11160 smp->data.str.len = args[1].data.str.len;
11161 }
11162
11163 /* Return true only if a matching language was found. */
11164 return smp->data.str.len != 0;
11165}
11166
William Lallemand73025dd2014-04-24 14:38:37 +020011167/*
11168 * Return the struct http_req_action_kw associated to a keyword.
11169 */
11170struct http_req_action_kw *action_http_req_custom(const char *kw)
11171{
11172 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11173 struct http_req_action_kw_list *kw_list;
11174 int i;
11175
11176 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11177 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11178 if (!strcmp(kw, kw_list->kw[i].kw))
11179 return &kw_list->kw[i];
11180 }
11181 }
11182 }
11183 return NULL;
11184}
11185
11186/*
11187 * Return the struct http_res_action_kw associated to a keyword.
11188 */
11189struct http_res_action_kw *action_http_res_custom(const char *kw)
11190{
11191 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11192 struct http_res_action_kw_list *kw_list;
11193 int i;
11194
11195 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11196 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11197 if (!strcmp(kw, kw_list->kw[i].kw))
11198 return &kw_list->kw[i];
11199 }
11200 }
11201 }
11202 return NULL;
11203}
11204
Willy Tarreau4a568972010-05-12 08:08:50 +020011205/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011206/* All supported ACL keywords must be declared here. */
11207/************************************************************************/
11208
11209/* Note: must not be declared <const> as its list will be overwritten.
11210 * Please take care of keeping this list alphabetically sorted.
11211 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011212static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011213 { "base", "base", PAT_MATCH_STR },
11214 { "base_beg", "base", PAT_MATCH_BEG },
11215 { "base_dir", "base", PAT_MATCH_DIR },
11216 { "base_dom", "base", PAT_MATCH_DOM },
11217 { "base_end", "base", PAT_MATCH_END },
11218 { "base_len", "base", PAT_MATCH_LEN },
11219 { "base_reg", "base", PAT_MATCH_REG },
11220 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011221
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011222 { "cook", "req.cook", PAT_MATCH_STR },
11223 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11224 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11225 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11226 { "cook_end", "req.cook", PAT_MATCH_END },
11227 { "cook_len", "req.cook", PAT_MATCH_LEN },
11228 { "cook_reg", "req.cook", PAT_MATCH_REG },
11229 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011230
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011231 { "hdr", "req.hdr", PAT_MATCH_STR },
11232 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11233 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11234 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11235 { "hdr_end", "req.hdr", PAT_MATCH_END },
11236 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11237 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11238 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011239
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011240 /* these two declarations uses strings with list storage (in place
11241 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11242 * and delete functions are relative to the list management. The parse
11243 * and match method are related to the corresponding fetch methods. This
11244 * is very particular ACL declaration mode.
11245 */
11246 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11247 { "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 +020011248
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011249 { "path", "path", PAT_MATCH_STR },
11250 { "path_beg", "path", PAT_MATCH_BEG },
11251 { "path_dir", "path", PAT_MATCH_DIR },
11252 { "path_dom", "path", PAT_MATCH_DOM },
11253 { "path_end", "path", PAT_MATCH_END },
11254 { "path_len", "path", PAT_MATCH_LEN },
11255 { "path_reg", "path", PAT_MATCH_REG },
11256 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011257
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011258 { "req_ver", "req.ver", PAT_MATCH_STR },
11259 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011260
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011261 { "scook", "res.cook", PAT_MATCH_STR },
11262 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11263 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11264 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11265 { "scook_end", "res.cook", PAT_MATCH_END },
11266 { "scook_len", "res.cook", PAT_MATCH_LEN },
11267 { "scook_reg", "res.cook", PAT_MATCH_REG },
11268 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011269
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011270 { "shdr", "res.hdr", PAT_MATCH_STR },
11271 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11272 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11273 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11274 { "shdr_end", "res.hdr", PAT_MATCH_END },
11275 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11276 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11277 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011278
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011279 { "url", "url", PAT_MATCH_STR },
11280 { "url_beg", "url", PAT_MATCH_BEG },
11281 { "url_dir", "url", PAT_MATCH_DIR },
11282 { "url_dom", "url", PAT_MATCH_DOM },
11283 { "url_end", "url", PAT_MATCH_END },
11284 { "url_len", "url", PAT_MATCH_LEN },
11285 { "url_reg", "url", PAT_MATCH_REG },
11286 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011287
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011288 { "urlp", "urlp", PAT_MATCH_STR },
11289 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11290 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11291 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11292 { "urlp_end", "urlp", PAT_MATCH_END },
11293 { "urlp_len", "urlp", PAT_MATCH_LEN },
11294 { "urlp_reg", "urlp", PAT_MATCH_REG },
11295 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011296
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011297 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011298}};
11299
11300/************************************************************************/
11301/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011302/************************************************************************/
11303/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011304static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011305 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011306 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11307 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11308
William Lallemanda43ba4e2014-01-28 18:14:25 +010011309 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011310 { "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 +020011311
11312 /* retrieve these captures from the HTTP logs */
11313 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11314 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11315 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11316
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011317 { "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 +020011318 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011319
Willy Tarreau409bcde2013-01-08 00:31:00 +010011320 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11321 * are only here to match the ACL's name, are request-only and are used
11322 * for ACL compatibility only.
11323 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011324 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11325 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011326 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11327 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11328
11329 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11330 * only here to match the ACL's name, are request-only and are used for
11331 * ACL compatibility only.
11332 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011333 { "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 +010011334 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11335 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11336 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11337
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011338 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011339 { "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 +010011340 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011341 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011342 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011343
11344 /* HTTP protocol on the request path */
11345 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011346 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011347
11348 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011349 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11350 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011351
11352 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011353 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11354 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011355
Willy Tarreau18ed2562013-01-14 15:56:36 +010011356 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011357 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011358 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11359 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11360
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011361 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011362 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011363 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011364 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11365 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11366 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11367
11368 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011369 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011370 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11371 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11372
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011373 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011374 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011375 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011376 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11377 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11378 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11379
Willy Tarreau409bcde2013-01-08 00:31:00 +010011380 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011381 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011382 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11383 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011384 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011385
11386 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011387 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011388 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11389 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11390 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11391
11392 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011393 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011394 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11395 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011396 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11397 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011398 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11399 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011400 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11401 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011402}};
11403
Willy Tarreau8797c062007-05-07 00:55:35 +020011404
Willy Tarreau276fae92013-07-25 14:36:01 +020011405/* Note: must not be declared <const> as its list will be overwritten */
11406static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011407 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11408 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011409 { NULL, NULL, 0, 0, 0 },
11410}};
11411
Willy Tarreau8797c062007-05-07 00:55:35 +020011412__attribute__((constructor))
11413static void __http_protocol_init(void)
11414{
11415 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011416 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011417 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011418}
11419
11420
Willy Tarreau58f10d72006-12-04 02:26:12 +010011421/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011422 * Local variables:
11423 * c-indent-level: 8
11424 * c-basic-offset: 8
11425 * End:
11426 */