blob: e797e002c11810c62c55e921504d53b9f0910e83 [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 Tarreau80587432006-12-24 17:47:20 +0100255void init_proto_http()
256{
Willy Tarreau42250582007-04-01 01:30:43 +0200257 int i;
258 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100259 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200260
Willy Tarreau80587432006-12-24 17:47:20 +0100261 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
262 if (!http_err_msgs[msg]) {
263 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
264 abort();
265 }
266
267 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
268 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
269 }
Willy Tarreau42250582007-04-01 01:30:43 +0200270
271 /* initialize the log header encoding map : '{|}"#' should be encoded with
272 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
273 * URL encoding only requires '"', '#' to be encoded as well as non-
274 * printable characters above.
275 */
276 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
277 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100278 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200279 for (i = 0; i < 32; i++) {
280 FD_SET(i, hdr_encode_map);
281 FD_SET(i, url_encode_map);
282 }
283 for (i = 127; i < 256; i++) {
284 FD_SET(i, hdr_encode_map);
285 FD_SET(i, url_encode_map);
286 }
287
288 tmp = "\"#{|}";
289 while (*tmp) {
290 FD_SET(*tmp, hdr_encode_map);
291 tmp++;
292 }
293
294 tmp = "\"#";
295 while (*tmp) {
296 FD_SET(*tmp, url_encode_map);
297 tmp++;
298 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200299
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100300 /* initialize the http header encoding map. The draft httpbis define the
301 * header content as:
302 *
303 * HTTP-message = start-line
304 * *( header-field CRLF )
305 * CRLF
306 * [ message-body ]
307 * header-field = field-name ":" OWS field-value OWS
308 * field-value = *( field-content / obs-fold )
309 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
310 * obs-fold = CRLF 1*( SP / HTAB )
311 * field-vchar = VCHAR / obs-text
312 * VCHAR = %x21-7E
313 * obs-text = %x80-FF
314 *
315 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
316 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
317 * "obs-fold" is volontary forgotten because haproxy remove this.
318 */
319 memset(http_encode_map, 0, sizeof(http_encode_map));
320 for (i = 0x00; i <= 0x08; i++)
321 FD_SET(i, http_encode_map);
322 for (i = 0x0a; i <= 0x1f; i++)
323 FD_SET(i, http_encode_map);
324 FD_SET(0x7f, http_encode_map);
325
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200326 /* memory allocations */
327 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100328 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100329}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330
Willy Tarreau53b6c742006-12-17 13:37:46 +0100331/*
332 * We have 26 list of methods (1 per first letter), each of which can have
333 * up to 3 entries (2 valid, 1 null).
334 */
335struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100336 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100337 int len;
338 const char text[8];
339};
340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100341const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100342 ['C' - 'A'] = {
343 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
344 },
345 ['D' - 'A'] = {
346 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
347 },
348 ['G' - 'A'] = {
349 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
350 },
351 ['H' - 'A'] = {
352 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
353 },
354 ['P' - 'A'] = {
355 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
356 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
357 },
358 ['T' - 'A'] = {
359 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
360 },
361 /* rest is empty like this :
362 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
363 */
364};
365
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100366const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
367 [HTTP_METH_NONE] = { "", 0 },
368 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
369 [HTTP_METH_GET] = { "GET", 3 },
370 [HTTP_METH_HEAD] = { "HEAD", 4 },
371 [HTTP_METH_POST] = { "POST", 4 },
372 [HTTP_METH_PUT] = { "PUT", 3 },
373 [HTTP_METH_DELETE] = { "DELETE", 6 },
374 [HTTP_METH_TRACE] = { "TRACE", 5 },
375 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
376};
377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100378/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200379 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380 * RFC2616 for those chars.
381 */
382
383const char http_is_spht[256] = {
384 [' '] = 1, ['\t'] = 1,
385};
386
387const char http_is_crlf[256] = {
388 ['\r'] = 1, ['\n'] = 1,
389};
390
391const char http_is_lws[256] = {
392 [' '] = 1, ['\t'] = 1,
393 ['\r'] = 1, ['\n'] = 1,
394};
395
396const char http_is_sep[256] = {
397 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
398 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
399 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
400 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
401 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
402};
403
404const char http_is_ctl[256] = {
405 [0 ... 31] = 1,
406 [127] = 1,
407};
408
409/*
410 * A token is any ASCII char that is neither a separator nor a CTL char.
411 * Do not overwrite values in assignment since gcc-2.95 will not handle
412 * them correctly. Instead, define every non-CTL char's status.
413 */
414const char http_is_token[256] = {
415 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
416 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
417 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
418 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
419 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
420 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
421 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
422 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
423 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
424 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
425 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
426 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
427 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
428 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
429 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
430 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
431 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
432 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
433 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
434 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
435 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
436 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
437 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
438 ['|'] = 1, ['}'] = 0, ['~'] = 1,
439};
440
441
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100442/*
443 * An http ver_token is any ASCII which can be found in an HTTP version,
444 * which includes 'H', 'T', 'P', '/', '.' and any digit.
445 */
446const char http_is_ver_token[256] = {
447 ['.'] = 1, ['/'] = 1,
448 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
449 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
450 ['H'] = 1, ['P'] = 1, ['T'] = 1,
451};
452
453
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100454/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100455 * Adds a header and its CRLF at the tail of the message's buffer, just before
456 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100457 * The header is also automatically added to the index <hdr_idx>, and the end
458 * of headers is automatically adjusted. The number of bytes added is returned
459 * on success, otherwise <0 is returned indicating an error.
460 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100461int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100462{
463 int bytes, len;
464
465 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200466 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100467 if (!bytes)
468 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100469 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100470 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
471}
472
473/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100474 * Adds a header and its CRLF at the tail of the message's buffer, just before
475 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100476 * the buffer is only opened and the space reserved, but nothing is copied.
477 * The header is also automatically added to the index <hdr_idx>, and the end
478 * of headers is automatically adjusted. The number of bytes added is returned
479 * on success, otherwise <0 is returned indicating an error.
480 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100481int http_header_add_tail2(struct http_msg *msg,
482 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483{
484 int bytes;
485
Willy Tarreau9b28e032012-10-12 23:49:43 +0200486 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100487 if (!bytes)
488 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100489 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100490 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
491}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492
493/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100494 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
495 * If so, returns the position of the first non-space character relative to
496 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
497 * to return a pointer to the place after the first space. Returns 0 if the
498 * header name does not match. Checks are case-insensitive.
499 */
500int http_header_match2(const char *hdr, const char *end,
501 const char *name, int len)
502{
503 const char *val;
504
505 if (hdr + len >= end)
506 return 0;
507 if (hdr[len] != ':')
508 return 0;
509 if (strncasecmp(hdr, name, len) != 0)
510 return 0;
511 val = hdr + len + 1;
512 while (val < end && HTTP_IS_SPHT(*val))
513 val++;
514 if ((val >= end) && (len + 2 <= end - hdr))
515 return len + 2; /* we may replace starting from second space */
516 return val - hdr;
517}
518
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200519/* Find the first or next occurrence of header <name> in message buffer <sol>
520 * using headers index <idx>, and return it in the <ctx> structure. This
521 * structure holds everything necessary to use the header and find next
522 * occurrence. If its <idx> member is 0, the header is searched from the
523 * beginning. Otherwise, the next occurrence is returned. The function returns
524 * 1 when it finds a value, and 0 when there is no more. It is very similar to
525 * http_find_header2() except that it is designed to work with full-line headers
526 * whose comma is not a delimiter but is part of the syntax. As a special case,
527 * if ctx->val is NULL when searching for a new values of a header, the current
528 * header is rescanned. This allows rescanning after a header deletion.
529 */
530int http_find_full_header2(const char *name, int len,
531 char *sol, struct hdr_idx *idx,
532 struct hdr_ctx *ctx)
533{
534 char *eol, *sov;
535 int cur_idx, old_idx;
536
537 cur_idx = ctx->idx;
538 if (cur_idx) {
539 /* We have previously returned a header, let's search another one */
540 sol = ctx->line;
541 eol = sol + idx->v[cur_idx].len;
542 goto next_hdr;
543 }
544
545 /* first request for this header */
546 sol += hdr_idx_first_pos(idx);
547 old_idx = 0;
548 cur_idx = hdr_idx_first_idx(idx);
549 while (cur_idx) {
550 eol = sol + idx->v[cur_idx].len;
551
552 if (len == 0) {
553 /* No argument was passed, we want any header.
554 * To achieve this, we simply build a fake request. */
555 while (sol + len < eol && sol[len] != ':')
556 len++;
557 name = sol;
558 }
559
560 if ((len < eol - sol) &&
561 (sol[len] == ':') &&
562 (strncasecmp(sol, name, len) == 0)) {
563 ctx->del = len;
564 sov = sol + len + 1;
565 while (sov < eol && http_is_lws[(unsigned char)*sov])
566 sov++;
567
568 ctx->line = sol;
569 ctx->prev = old_idx;
570 ctx->idx = cur_idx;
571 ctx->val = sov - sol;
572 ctx->tws = 0;
573 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
574 eol--;
575 ctx->tws++;
576 }
577 ctx->vlen = eol - sov;
578 return 1;
579 }
580 next_hdr:
581 sol = eol + idx->v[cur_idx].cr + 1;
582 old_idx = cur_idx;
583 cur_idx = idx->v[cur_idx].next;
584 }
585 return 0;
586}
587
Willy Tarreau68085d82010-01-18 14:54:04 +0100588/* Find the end of the header value contained between <s> and <e>. See RFC2616,
589 * par 2.2 for more information. Note that it requires a valid header to return
590 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200591 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100592char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200593{
594 int quoted, qdpair;
595
596 quoted = qdpair = 0;
597 for (; s < e; s++) {
598 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200599 else if (quoted) {
600 if (*s == '\\') qdpair = 1;
601 else if (*s == '"') quoted = 0;
602 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200603 else if (*s == '"') quoted = 1;
604 else if (*s == ',') return s;
605 }
606 return s;
607}
608
609/* Find the first or next occurrence of header <name> in message buffer <sol>
610 * using headers index <idx>, and return it in the <ctx> structure. This
611 * structure holds everything necessary to use the header and find next
612 * occurrence. If its <idx> member is 0, the header is searched from the
613 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100614 * 1 when it finds a value, and 0 when there is no more. It is designed to work
615 * with headers defined as comma-separated lists. As a special case, if ctx->val
616 * is NULL when searching for a new values of a header, the current header is
617 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200618 */
619int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100620 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200621 struct hdr_ctx *ctx)
622{
Willy Tarreau68085d82010-01-18 14:54:04 +0100623 char *eol, *sov;
624 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200625
Willy Tarreau68085d82010-01-18 14:54:04 +0100626 cur_idx = ctx->idx;
627 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200628 /* We have previously returned a value, let's search
629 * another one on the same line.
630 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200631 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200634 eol = sol + idx->v[cur_idx].len;
635
636 if (sov >= eol)
637 /* no more values in this header */
638 goto next_hdr;
639
Willy Tarreau68085d82010-01-18 14:54:04 +0100640 /* values remaining for this header, skip the comma but save it
641 * for later use (eg: for header deletion).
642 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200643 sov++;
644 while (sov < eol && http_is_lws[(unsigned char)*sov])
645 sov++;
646
647 goto return_hdr;
648 }
649
650 /* first request for this header */
651 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100652 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200653 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200654 while (cur_idx) {
655 eol = sol + idx->v[cur_idx].len;
656
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200657 if (len == 0) {
658 /* No argument was passed, we want any header.
659 * To achieve this, we simply build a fake request. */
660 while (sol + len < eol && sol[len] != ':')
661 len++;
662 name = sol;
663 }
664
Willy Tarreau33a7e692007-06-10 19:45:56 +0200665 if ((len < eol - sol) &&
666 (sol[len] == ':') &&
667 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100668 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200669 sov = sol + len + 1;
670 while (sov < eol && http_is_lws[(unsigned char)*sov])
671 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100672
Willy Tarreau33a7e692007-06-10 19:45:56 +0200673 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100674 ctx->prev = old_idx;
675 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200676 ctx->idx = cur_idx;
677 ctx->val = sov - sol;
678
679 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200680 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200681 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200682 eol--;
683 ctx->tws++;
684 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200685 ctx->vlen = eol - sov;
686 return 1;
687 }
688 next_hdr:
689 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100690 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200691 cur_idx = idx->v[cur_idx].next;
692 }
693 return 0;
694}
695
696int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100697 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200698 struct hdr_ctx *ctx)
699{
700 return http_find_header2(name, strlen(name), sol, idx, ctx);
701}
702
Willy Tarreau68085d82010-01-18 14:54:04 +0100703/* Remove one value of a header. This only works on a <ctx> returned by one of
704 * the http_find_header functions. The value is removed, as well as surrounding
705 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100706 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100707 * message <msg>. The new index is returned. If it is zero, it means there is
708 * no more header, so any processing may stop. The ctx is always left in a form
709 * that can be handled by http_find_header2() to find next occurrence.
710 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100711int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100712{
713 int cur_idx = ctx->idx;
714 char *sol = ctx->line;
715 struct hdr_idx_elem *hdr;
716 int delta, skip_comma;
717
718 if (!cur_idx)
719 return 0;
720
721 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200722 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100723 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200724 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 http_msg_move_end(msg, delta);
726 idx->used--;
727 hdr->len = 0; /* unused entry */
728 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100729 if (idx->tail == ctx->idx)
730 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100731 ctx->idx = ctx->prev; /* walk back to the end of previous header */
732 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
733 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200734 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100735 return ctx->idx;
736 }
737
738 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200739 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
740 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100741 */
742
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200743 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200744 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200745 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100746 NULL, 0);
747 hdr->len += delta;
748 http_msg_move_end(msg, delta);
749 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200750 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100751 return ctx->idx;
752}
753
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100754/* This function handles a server error at the stream interface level. The
755 * stream interface is assumed to be already in a closed state. An optional
756 * message is copied into the input buffer, and an HTTP status code stored.
757 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100758 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200760static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100761 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200763 channel_auto_read(si->ob);
764 channel_abort(si->ob);
765 channel_auto_close(si->ob);
766 channel_erase(si->ob);
767 channel_auto_close(si->ib);
768 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100769 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200770 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200771 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200773 if (!(s->flags & SN_ERR_MASK))
774 s->flags |= err;
775 if (!(s->flags & SN_FINST_MASK))
776 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777}
778
Willy Tarreau80587432006-12-24 17:47:20 +0100779/* This function returns the appropriate error location for the given session
780 * and message.
781 */
782
Willy Tarreau783f2582012-09-04 12:19:04 +0200783struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100784{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200785 if (s->be->errmsg[msgnum].str)
786 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100787 else if (s->fe->errmsg[msgnum].str)
788 return &s->fe->errmsg[msgnum];
789 else
790 return &http_err_chunks[msgnum];
791}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792
Willy Tarreau53b6c742006-12-17 13:37:46 +0100793/*
794 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
795 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
796 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100797enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100798{
799 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100800 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100801
802 m = ((unsigned)*str - 'A');
803
804 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100805 for (h = http_methods[m]; h->len > 0; h++) {
806 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100807 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100808 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100809 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100810 };
811 return HTTP_METH_OTHER;
812 }
813 return HTTP_METH_NONE;
814
815}
816
Willy Tarreau21d2af32008-02-14 20:25:24 +0100817/* Parse the URI from the given transaction (which is assumed to be in request
818 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
819 * It is returned otherwise.
820 */
821static char *
822http_get_path(struct http_txn *txn)
823{
824 char *ptr, *end;
825
Willy Tarreau9b28e032012-10-12 23:49:43 +0200826 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100827 end = ptr + txn->req.sl.rq.u_l;
828
829 if (ptr >= end)
830 return NULL;
831
832 /* RFC2616, par. 5.1.2 :
833 * Request-URI = "*" | absuri | abspath | authority
834 */
835
836 if (*ptr == '*')
837 return NULL;
838
839 if (isalpha((unsigned char)*ptr)) {
840 /* this is a scheme as described by RFC3986, par. 3.1 */
841 ptr++;
842 while (ptr < end &&
843 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
844 ptr++;
845 /* skip '://' */
846 if (ptr == end || *ptr++ != ':')
847 return NULL;
848 if (ptr == end || *ptr++ != '/')
849 return NULL;
850 if (ptr == end || *ptr++ != '/')
851 return NULL;
852 }
853 /* skip [user[:passwd]@]host[:[port]] */
854
855 while (ptr < end && *ptr != '/')
856 ptr++;
857
858 if (ptr == end)
859 return NULL;
860
861 /* OK, we got the '/' ! */
862 return ptr;
863}
864
William Lallemand65ad6e12014-01-31 15:08:02 +0100865/* Parse the URI from the given string and look for the "/" beginning the PATH.
866 * If not found, return NULL. It is returned otherwise.
867 */
868static char *
869http_get_path_from_string(char *str)
870{
871 char *ptr = str;
872
873 /* RFC2616, par. 5.1.2 :
874 * Request-URI = "*" | absuri | abspath | authority
875 */
876
877 if (*ptr == '*')
878 return NULL;
879
880 if (isalpha((unsigned char)*ptr)) {
881 /* this is a scheme as described by RFC3986, par. 3.1 */
882 ptr++;
883 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
884 ptr++;
885 /* skip '://' */
886 if (*ptr == '\0' || *ptr++ != ':')
887 return NULL;
888 if (*ptr == '\0' || *ptr++ != '/')
889 return NULL;
890 if (*ptr == '\0' || *ptr++ != '/')
891 return NULL;
892 }
893 /* skip [user[:passwd]@]host[:[port]] */
894
895 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
896 ptr++;
897
898 if (*ptr == '\0' || *ptr == ' ')
899 return NULL;
900
901 /* OK, we got the '/' ! */
902 return ptr;
903}
904
Willy Tarreau71241ab2012-12-27 11:30:54 +0100905/* Returns a 302 for a redirectable request that reaches a server working in
906 * in redirect mode. This may only be called just after the stream interface
907 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
908 * follow normal proxy processing. NOTE: this function is designed to support
909 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100910 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100911void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100912{
913 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100914 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100915 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200916 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100917
918 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100919 trash.len = strlen(HTTP_302);
920 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100921
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100922 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100923
Willy Tarreauefb453c2008-10-26 20:49:47 +0100924 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100925 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 return;
927
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100928 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100929 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100930 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
931 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100932 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100933
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200934 /* 3: add the request URI. Since it was already forwarded, we need
935 * to temporarily rewind the buffer.
936 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100937 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200938 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200939
Willy Tarreauefb453c2008-10-26 20:49:47 +0100940 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200941 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 +0200942
Willy Tarreau9b28e032012-10-12 23:49:43 +0200943 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200944
Willy Tarreauefb453c2008-10-26 20:49:47 +0100945 if (!path)
946 return;
947
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100948 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100949 return;
950
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100951 memcpy(trash.str + trash.len, path, len);
952 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100953
954 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100955 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
956 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100957 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100958 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
959 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100960 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100961
962 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200963 si_shutr(si);
964 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100965 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100966 si->state = SI_ST_CLO;
967
968 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200969 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100970
971 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100972 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500973 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100974}
975
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100976/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100977 * that the server side is closed. Note that err_type is actually a
978 * bitmask, where almost only aborts may be cumulated with other
979 * values. We consider that aborted operations are more important
980 * than timeouts or errors due to the fact that nobody else in the
981 * logs might explain incomplete retries. All others should avoid
982 * being cumulated. It should normally not be possible to have multiple
983 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100984 * Note that connection errors appearing on the second request of a keep-alive
985 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100986 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100987void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990
991 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100992 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200993 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100994 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100995 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100996 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
997 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100998 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100999 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001000 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001001 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001002 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001003 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001004 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001005 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001006 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1007 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001008 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001009 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001010 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001011 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001012 else if (err_type & SI_ET_CONN_RES)
1013 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001014 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1015 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001016 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001017 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001018 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001019}
1020
Willy Tarreau42250582007-04-01 01:30:43 +02001021extern const char sess_term_cond[8];
1022extern const char sess_fin_state[8];
1023extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001024struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001025struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001026struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001027
Willy Tarreau117f59e2007-03-04 18:17:17 +01001028/*
1029 * Capture headers from message starting at <som> according to header list
1030 * <cap_hdr>, and fill the <idx> structure appropriately.
1031 */
1032void capture_headers(char *som, struct hdr_idx *idx,
1033 char **cap, struct cap_hdr *cap_hdr)
1034{
1035 char *eol, *sol, *col, *sov;
1036 int cur_idx;
1037 struct cap_hdr *h;
1038 int len;
1039
1040 sol = som + hdr_idx_first_pos(idx);
1041 cur_idx = hdr_idx_first_idx(idx);
1042
1043 while (cur_idx) {
1044 eol = sol + idx->v[cur_idx].len;
1045
1046 col = sol;
1047 while (col < eol && *col != ':')
1048 col++;
1049
1050 sov = col + 1;
1051 while (sov < eol && http_is_lws[(unsigned char)*sov])
1052 sov++;
1053
1054 for (h = cap_hdr; h; h = h->next) {
1055 if ((h->namelen == col - sol) &&
1056 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1057 if (cap[h->index] == NULL)
1058 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001059 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001060
1061 if (cap[h->index] == NULL) {
1062 Alert("HTTP capture : out of memory.\n");
1063 continue;
1064 }
1065
1066 len = eol - sov;
1067 if (len > h->len)
1068 len = h->len;
1069
1070 memcpy(cap[h->index], sov, len);
1071 cap[h->index][len]=0;
1072 }
1073 }
1074 sol = eol + idx->v[cur_idx].cr + 1;
1075 cur_idx = idx->v[cur_idx].next;
1076 }
1077}
1078
1079
Willy Tarreau42250582007-04-01 01:30:43 +02001080/* either we find an LF at <ptr> or we jump to <bad>.
1081 */
1082#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1083
1084/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1085 * otherwise to <http_msg_ood> with <state> set to <st>.
1086 */
1087#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1088 ptr++; \
1089 if (likely(ptr < end)) \
1090 goto good; \
1091 else { \
1092 state = (st); \
1093 goto http_msg_ood; \
1094 } \
1095 } while (0)
1096
1097
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001099 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001100 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1101 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1102 * will give undefined results.
1103 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1104 * and that msg->sol points to the beginning of the response.
1105 * If a complete line is found (which implies that at least one CR or LF is
1106 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1107 * returned indicating an incomplete line (which does not mean that parts have
1108 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1109 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1110 * upon next call.
1111 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001112 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1114 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001115 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001116 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001117const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001118 enum ht_state state, const char *ptr, const char *end,
1119 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001120{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001121 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001122
Willy Tarreau8973c702007-01-21 23:58:29 +01001123 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001125 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001126 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001127 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1128
1129 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001130 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001131 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1132 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001133 state = HTTP_MSG_ERROR;
1134 break;
1135
Willy Tarreau8973c702007-01-21 23:58:29 +01001136 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001137 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001139 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 goto http_msg_rpcode;
1141 }
1142 if (likely(HTTP_IS_SPHT(*ptr)))
1143 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1144 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001145 state = HTTP_MSG_ERROR;
1146 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001147
Willy Tarreau8973c702007-01-21 23:58:29 +01001148 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001149 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001150 if (likely(!HTTP_IS_LWS(*ptr)))
1151 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1152
1153 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001154 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001155 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1156 }
1157
1158 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001159 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001160 http_msg_rsp_reason:
1161 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001162 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001163 msg->sl.st.r_l = 0;
1164 goto http_msg_rpline_eol;
1165
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001167 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001169 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 goto http_msg_rpreason;
1171 }
1172 if (likely(HTTP_IS_SPHT(*ptr)))
1173 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1174 /* so it's a CR/LF, so there is no reason phrase */
1175 goto http_msg_rsp_reason;
1176
Willy Tarreau8973c702007-01-21 23:58:29 +01001177 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001178 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 if (likely(!HTTP_IS_CRLF(*ptr)))
1180 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001181 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 http_msg_rpline_eol:
1183 /* We have seen the end of line. Note that we do not
1184 * necessarily have the \n yet, but at least we know that we
1185 * have EITHER \r OR \n, otherwise the response would not be
1186 * complete. We can then record the response length and return
1187 * to the caller which will be able to register it.
1188 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001189 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 return ptr;
1191
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001193#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001194 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1195 exit(1);
1196#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001197 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001198 }
1199
1200 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001201 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 if (ret_state)
1203 *ret_state = state;
1204 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001205 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001207}
1208
Willy Tarreau8973c702007-01-21 23:58:29 +01001209/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 * This function parses a request line between <ptr> and <end>, starting with
1211 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1212 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1213 * will give undefined results.
1214 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1215 * and that msg->sol points to the beginning of the request.
1216 * If a complete line is found (which implies that at least one CR or LF is
1217 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1218 * returned indicating an incomplete line (which does not mean that parts have
1219 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1220 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1221 * upon next call.
1222 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001223 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001224 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1225 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001226 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001228const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001229 enum ht_state state, const char *ptr, const char *end,
1230 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001231{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001232 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001236 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 if (likely(HTTP_IS_TOKEN(*ptr)))
1238 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001241 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1243 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001244
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001245 if (likely(HTTP_IS_CRLF(*ptr))) {
1246 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001251 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001253 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001254 msg->sl.rq.v_l = 0;
1255 goto http_msg_rqline_eol;
1256 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001257 state = HTTP_MSG_ERROR;
1258 break;
1259
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001260 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001261 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001263 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 goto http_msg_rquri;
1265 }
1266 if (likely(HTTP_IS_SPHT(*ptr)))
1267 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1268 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1269 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001272 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001273 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001277 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1279 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001280
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001281 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001282 /* non-ASCII chars are forbidden unless option
1283 * accept-invalid-http-request is enabled in the frontend.
1284 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001285 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001286 if (msg->err_pos < -1)
1287 goto invalid_char;
1288 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001289 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001290 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1291 }
1292
1293 if (likely(HTTP_IS_CRLF(*ptr))) {
1294 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1295 goto http_msg_req09_uri_e;
1296 }
1297
1298 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001299 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001300 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001301 state = HTTP_MSG_ERROR;
1302 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 goto http_msg_rqver;
1309 }
1310 if (likely(HTTP_IS_SPHT(*ptr)))
1311 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1312 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1313 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001314
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001316 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001317 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319
1320 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001321 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001322 http_msg_rqline_eol:
1323 /* We have seen the end of line. Note that we do not
1324 * necessarily have the \n yet, but at least we know that we
1325 * have EITHER \r OR \n, otherwise the request would not be
1326 * complete. We can then record the request length and return
1327 * to the caller which will be able to register it.
1328 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001329 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001330 return ptr;
1331 }
1332
1333 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001334 state = HTTP_MSG_ERROR;
1335 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001338#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1340 exit(1);
1341#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001342 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001343 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001346 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 if (ret_state)
1348 *ret_state = state;
1349 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001350 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001353
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001354/*
1355 * Returns the data from Authorization header. Function may be called more
1356 * than once so data is stored in txn->auth_data. When no header is found
1357 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001358 * searching again for something we are unable to find anyway. However, if
1359 * the result if valid, the cache is not reused because we would risk to
1360 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001361 */
1362
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001363/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1364 * set according to global.tune.bufsize.
1365 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001366char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001367
1368int
1369get_http_auth(struct session *s)
1370{
1371
1372 struct http_txn *txn = &s->txn;
1373 struct chunk auth_method;
1374 struct hdr_ctx ctx;
1375 char *h, *p;
1376 int len;
1377
1378#ifdef DEBUG_AUTH
1379 printf("Auth for session %p: %d\n", s, txn->auth.method);
1380#endif
1381
1382 if (txn->auth.method == HTTP_AUTH_WRONG)
1383 return 0;
1384
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001385 txn->auth.method = HTTP_AUTH_WRONG;
1386
1387 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001388
1389 if (txn->flags & TX_USE_PX_CONN) {
1390 h = "Proxy-Authorization";
1391 len = strlen(h);
1392 } else {
1393 h = "Authorization";
1394 len = strlen(h);
1395 }
1396
Willy Tarreau9b28e032012-10-12 23:49:43 +02001397 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001398 return 0;
1399
1400 h = ctx.line + ctx.val;
1401
1402 p = memchr(h, ' ', ctx.vlen);
1403 if (!p || p == h)
1404 return 0;
1405
1406 chunk_initlen(&auth_method, h, 0, p-h);
1407 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1408
1409 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1410
1411 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001412 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001413
1414 if (len < 0)
1415 return 0;
1416
1417
1418 get_http_auth_buff[len] = '\0';
1419
1420 p = strchr(get_http_auth_buff, ':');
1421
1422 if (!p)
1423 return 0;
1424
1425 txn->auth.user = get_http_auth_buff;
1426 *p = '\0';
1427 txn->auth.pass = p+1;
1428
1429 txn->auth.method = HTTP_AUTH_BASIC;
1430 return 1;
1431 }
1432
1433 return 0;
1434}
1435
Willy Tarreau58f10d72006-12-04 02:26:12 +01001436
Willy Tarreau8973c702007-01-21 23:58:29 +01001437/*
1438 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001439 * depending on the initial msg->msg_state. The caller is responsible for
1440 * ensuring that the message does not wrap. The function can be preempted
1441 * everywhere when data are missing and recalled at the exact same location
1442 * with no information loss. The message may even be realigned between two
1443 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001444 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001445 * fields. Note that msg->sol will be initialized after completing the first
1446 * state, so that none of the msg pointers has to be initialized prior to the
1447 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001449void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450{
Willy Tarreau3770f232013-12-07 00:01:53 +01001451 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001453 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001454
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001455 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001456 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001457 ptr = buf->p + msg->next;
1458 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001459
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 if (unlikely(ptr >= end))
1461 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001464 /*
1465 * First, states that are specific to the response only.
1466 * We check them first so that request and headers are
1467 * closer to each other (accessed more often).
1468 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001470 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001472 /* we have a start of message, but we have to check
1473 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001474 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001475 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001476 if (unlikely(ptr != buf->p)) {
1477 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001478 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001479 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001480 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 }
Willy Tarreau26927362012-05-18 23:22:52 +02001482 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001483 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 hdr_idx_init(idx);
1485 state = HTTP_MSG_RPVER;
1486 goto http_msg_rpver;
1487 }
1488
1489 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1490 goto http_msg_invalid;
1491
1492 if (unlikely(*ptr == '\n'))
1493 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1494 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1495 /* stop here */
1496
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001498 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 EXPECT_LF_HERE(ptr, http_msg_invalid);
1500 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1501 /* stop here */
1502
Willy Tarreau8973c702007-01-21 23:58:29 +01001503 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001504 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 case HTTP_MSG_RPVER_SP:
1506 case HTTP_MSG_RPCODE:
1507 case HTTP_MSG_RPCODE_SP:
1508 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001509 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001510 state, ptr, end,
1511 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001512 if (unlikely(!ptr))
1513 return;
1514
1515 /* we have a full response and we know that we have either a CR
1516 * or an LF at <ptr>.
1517 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001518 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1519
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001520 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001521 if (likely(*ptr == '\r'))
1522 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1523 goto http_msg_rpline_end;
1524
Willy Tarreau8973c702007-01-21 23:58:29 +01001525 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001527 /* msg->sol must point to the first of CR or LF. */
1528 EXPECT_LF_HERE(ptr, http_msg_invalid);
1529 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1530 /* stop here */
1531
1532 /*
1533 * Second, states that are specific to the request only
1534 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001538 /* we have a start of message, but we have to check
1539 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001540 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001542 if (likely(ptr != buf->p)) {
1543 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001544 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001545 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 }
Willy Tarreau26927362012-05-18 23:22:52 +02001548 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001549 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 state = HTTP_MSG_RQMETH;
1551 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1555 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 if (unlikely(*ptr == '\n'))
1558 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1559 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001560 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001563 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 EXPECT_LF_HERE(ptr, http_msg_invalid);
1565 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001566 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001569 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQMETH_SP:
1571 case HTTP_MSG_RQURI:
1572 case HTTP_MSG_RQURI_SP:
1573 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001574 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001575 state, ptr, end,
1576 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 if (unlikely(!ptr))
1578 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 /* we have a full request and we know that we have either a CR
1581 * or an LF at <ptr>.
1582 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001584
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001585 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 if (likely(*ptr == '\r'))
1587 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001589
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001591 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 /* check for HTTP/0.9 request : no version information available.
1593 * msg->sol must point to the first of CR or LF.
1594 */
1595 if (unlikely(msg->sl.rq.v_l == 0))
1596 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001597
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 EXPECT_LF_HERE(ptr, http_msg_invalid);
1599 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001600 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 /*
1603 * Common states below
1604 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001606 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001607 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 if (likely(!HTTP_IS_CRLF(*ptr))) {
1609 goto http_msg_hdr_name;
1610 }
1611
1612 if (likely(*ptr == '\r'))
1613 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1614 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001615
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001617 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 /* assumes msg->sol points to the first char */
1619 if (likely(HTTP_IS_TOKEN(*ptr)))
1620 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001621
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001622 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001625 if (likely(msg->err_pos < -1) || *ptr == '\n')
1626 goto http_msg_invalid;
1627
1628 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001629 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001630
1631 /* and we still accept this non-token character */
1632 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001635 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001636 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 if (likely(HTTP_IS_SPHT(*ptr)))
1638 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001641 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (likely(!HTTP_IS_CRLF(*ptr))) {
1644 goto http_msg_hdr_val;
1645 }
1646
1647 if (likely(*ptr == '\r'))
1648 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1649 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001652 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 EXPECT_LF_HERE(ptr, http_msg_invalid);
1654 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001657 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 if (likely(HTTP_IS_SPHT(*ptr))) {
1659 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001660 for (; buf->p + msg->sov < ptr; msg->sov++)
1661 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 goto http_msg_hdr_l1_sp;
1663 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001664 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001665 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 goto http_msg_complete_header;
1667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001669 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001670 /* assumes msg->sol points to the first char, and msg->sov
1671 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 */
1673 if (likely(!HTTP_IS_CRLF(*ptr)))
1674 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001675
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001676 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 /* Note: we could also copy eol into ->eoh so that we have the
1678 * real header end in case it ends with lots of LWS, but is this
1679 * really needed ?
1680 */
1681 if (likely(*ptr == '\r'))
1682 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1683 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001686 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 EXPECT_LF_HERE(ptr, http_msg_invalid);
1688 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001691 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1693 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001694 for (; buf->p + msg->eol < ptr; msg->eol++)
1695 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 goto http_msg_hdr_val;
1697 }
1698 http_msg_complete_header:
1699 /*
1700 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001701 * Assumes msg->sol points to the first char, msg->sov points
1702 * to the first character of the value and msg->eol to the
1703 * first CR or LF so we know how the line ends. We insert last
1704 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001706 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 idx, idx->tail) < 0))
1708 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001709
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001710 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 if (likely(!HTTP_IS_CRLF(*ptr))) {
1712 goto http_msg_hdr_name;
1713 }
1714
1715 if (likely(*ptr == '\r'))
1716 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1717 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001720 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001721 /* Assumes msg->sol points to the first of either CR or LF.
1722 * Sets ->sov and ->next to the total header length, ->eoh to
1723 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1724 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 EXPECT_LF_HERE(ptr, http_msg_invalid);
1726 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001727 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001728 msg->eoh = msg->sol;
1729 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001730 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001731 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001733
1734 case HTTP_MSG_ERROR:
1735 /* this may only happen if we call http_msg_analyser() twice with an error */
1736 break;
1737
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001739#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1741 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001742#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001743 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 }
1745 http_msg_ood:
1746 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001747 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001748 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 http_msg_invalid:
1752 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001753 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001754 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 return;
1756}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001757
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001758/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1759 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1760 * nothing is done and 1 is returned.
1761 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001762static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001763{
1764 int delta;
1765 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001766 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001767
1768 if (msg->sl.rq.v_l != 0)
1769 return 1;
1770
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001771 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1772 if (txn->meth != HTTP_METH_GET)
1773 return 0;
1774
Willy Tarreau9b28e032012-10-12 23:49:43 +02001775 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776 delta = 0;
1777
1778 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001779 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1780 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001781 }
1782 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001783 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001784 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001785 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001786 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001787 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001788 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 NULL, NULL);
1790 if (unlikely(!cur_end))
1791 return 0;
1792
1793 /* we have a full HTTP/1.0 request now and we know that
1794 * we have either a CR or an LF at <ptr>.
1795 */
1796 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1797 return 1;
1798}
1799
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001800/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001801 * and "keep-alive" values. If we already know that some headers may safely
1802 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001803 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1804 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001805 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001806 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1807 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1808 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001809 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001810 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001811void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001812{
Willy Tarreau5b154472009-12-21 20:11:07 +01001813 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001814 const char *hdr_val = "Connection";
1815 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001816
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001817 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001818 return;
1819
Willy Tarreau88d349d2010-01-25 12:15:43 +01001820 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1821 hdr_val = "Proxy-Connection";
1822 hdr_len = 16;
1823 }
1824
Willy Tarreau5b154472009-12-21 20:11:07 +01001825 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001826 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001827 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1829 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001830 if (to_del & 2)
1831 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001832 else
1833 txn->flags |= TX_CON_KAL_SET;
1834 }
1835 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1836 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001837 if (to_del & 1)
1838 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001839 else
1840 txn->flags |= TX_CON_CLO_SET;
1841 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001842 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1843 txn->flags |= TX_HDR_CONN_UPG;
1844 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001845 }
1846
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001847 txn->flags |= TX_HDR_CONN_PRS;
1848 return;
1849}
Willy Tarreau5b154472009-12-21 20:11:07 +01001850
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001851/* Apply desired changes on the Connection: header. Values may be removed and/or
1852 * added depending on the <wanted> flags, which are exclusively composed of
1853 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1854 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1855 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001856void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001857{
1858 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001859 const char *hdr_val = "Connection";
1860 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001861
1862 ctx.idx = 0;
1863
Willy Tarreau88d349d2010-01-25 12:15:43 +01001864
1865 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1866 hdr_val = "Proxy-Connection";
1867 hdr_len = 16;
1868 }
1869
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001870 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001871 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1873 if (wanted & TX_CON_KAL_SET)
1874 txn->flags |= TX_CON_KAL_SET;
1875 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001876 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001877 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001878 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1879 if (wanted & TX_CON_CLO_SET)
1880 txn->flags |= TX_CON_CLO_SET;
1881 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001882 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001883 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001884 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001885
1886 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1887 return;
1888
1889 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1890 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001891 hdr_val = "Connection: close";
1892 hdr_len = 17;
1893 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1894 hdr_val = "Proxy-Connection: close";
1895 hdr_len = 23;
1896 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001897 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 }
1899
1900 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1901 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001902 hdr_val = "Connection: keep-alive";
1903 hdr_len = 22;
1904 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1905 hdr_val = "Proxy-Connection: keep-alive";
1906 hdr_len = 28;
1907 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001908 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001909 }
1910 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001911}
1912
Willy Tarreauc24715e2014-04-17 15:21:20 +02001913/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1914 * the first byte of data after the chunk size, so that we know we can forward
1915 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1916 * the chunk size. That way it is always possible to differentiate between the
1917 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001921static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001922{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001923 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001924 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001925 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 const char *end = buf->data + buf->size;
1927 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001928 unsigned int chunk = 0;
1929
1930 /* The chunk size is in the following form, though we are only
1931 * interested in the size and CRLF :
1932 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1933 */
1934 while (1) {
1935 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001936 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001937 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001938 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 if (c < 0) /* not a hex digit anymore */
1940 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001941 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001942 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001943 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001944 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001945 chunk = (chunk << 4) + c;
1946 }
1947
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001949 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001950 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951
1952 while (http_is_spht[(unsigned char)*ptr]) {
1953 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001954 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001955 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001956 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001957 }
1958
Willy Tarreaud98cf932009-12-27 22:54:55 +01001959 /* Up to there, we know that at least one byte is present at *ptr. Check
1960 * for the end of chunk size.
1961 */
1962 while (1) {
1963 if (likely(HTTP_IS_CRLF(*ptr))) {
1964 /* we now have a CR or an LF at ptr */
1965 if (likely(*ptr == '\r')) {
1966 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001967 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001968 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001969 return 0;
1970 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001971
Willy Tarreaud98cf932009-12-27 22:54:55 +01001972 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001973 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001975 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 /* done */
1977 break;
1978 }
1979 else if (*ptr == ';') {
1980 /* chunk extension, ends at next CRLF */
1981 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001982 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001983 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001984 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985
1986 while (!HTTP_IS_CRLF(*ptr)) {
1987 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001988 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001989 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001990 return 0;
1991 }
1992 /* we have a CRLF now, loop above */
1993 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001994 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001995 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001996 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001997 }
1998
Willy Tarreaud98cf932009-12-27 22:54:55 +01001999 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002000 * which may or may not be present. We save that into ->next,
2001 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002002 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002003 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002004 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002005 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002006 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002007 msg->chunk_len = chunk;
2008 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002009 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002010 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002011 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002012 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002013 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002014}
2015
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002016/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002017 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002018 * the trailers is found, it is automatically scheduled to be forwarded,
2019 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2020 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002021 * except maybe msg->next if it could parse some lines, and returns zero.
2022 * If a parse error is encountered, the function returns < 0 and does not
2023 * change anything except maybe msg->next. Note that the message must
2024 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002025 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002026 * forwarding, and that msg->next exactly matches the length of trailers
2027 * already parsed and not forwarded. It is also important to note that this
2028 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002029 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002030static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002032 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002033
Willy Tarreaua458b672012-03-05 11:17:50 +01002034 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002036 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002037 const char *ptr = b_ptr(buf, msg->next);
2038 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002039 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040
2041 /* scan current line and stop at LF or CRLF */
2042 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002043 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002044 return 0;
2045
2046 if (*ptr == '\n') {
2047 if (!p1)
2048 p1 = ptr;
2049 p2 = ptr;
2050 break;
2051 }
2052
2053 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002054 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002055 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002057 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 p1 = ptr;
2059 }
2060
2061 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 if (ptr >= buf->data + buf->size)
2063 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002064 }
2065
2066 /* after LF; point to beginning of next line */
2067 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 if (p2 >= buf->data + buf->size)
2069 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002071 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002072 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002073 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002074
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002076 /* LF/CRLF at beginning of line => end of trailers at p2.
2077 * Everything was scheduled for forwarding, there's nothing
2078 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002079 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002080 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 msg->msg_state = HTTP_MSG_DONE;
2082 return 1;
2083 }
2084 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002085 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 }
2087}
2088
Willy Tarreauc24715e2014-04-17 15:21:20 +02002089/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2090 * or a possible LF alone at the end of a chunk. It automatically adjusts
2091 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002092 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002093 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2094 * not enough data are available, the function does not change anything and
2095 * returns zero. If a parse error is encountered, the function returns < 0 and
2096 * does not change anything. Note: this function is designed to parse wrapped
2097 * CRLF at the end of the buffer.
2098 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002099static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002100{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002101 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103 int bytes;
2104
2105 /* NB: we'll check data availabilty at the end. It's not a
2106 * problem because whatever we match first will be checked
2107 * against the correct length.
2108 */
2109 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002110 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002111 if (*ptr == '\r') {
2112 bytes++;
2113 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002114 if (ptr >= buf->data + buf->size)
2115 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 }
2117
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002118 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119 return 0;
2120
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002121 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002122 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002123 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002124 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125
2126 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002127 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002128 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002129 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002130 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002131 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2132 return 1;
2133}
Willy Tarreau5b154472009-12-21 20:11:07 +01002134
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002135/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2136 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2137 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2138 * Unparsable qvalues return 1000 as "q=1.000".
2139 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002140int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002141{
2142 int q = 1000;
2143
2144 if (!isdigit(*qvalue))
2145 goto out;
2146 q = (*qvalue++ - '0') * 1000;
2147
2148 if (*qvalue++ != '.')
2149 goto out;
2150
2151 if (!isdigit(*qvalue))
2152 goto out;
2153 q += (*qvalue++ - '0') * 100;
2154
2155 if (!isdigit(*qvalue))
2156 goto out;
2157 q += (*qvalue++ - '0') * 10;
2158
2159 if (!isdigit(*qvalue))
2160 goto out;
2161 q += (*qvalue++ - '0') * 1;
2162 out:
2163 if (q > 1000)
2164 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002165 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002166 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002167 return q;
2168}
William Lallemand82fe75c2012-10-23 10:25:10 +02002169
2170/*
2171 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002172 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002173int select_compression_request_header(struct session *s, struct buffer *req)
2174{
2175 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002176 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002177 struct hdr_ctx ctx;
2178 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002179 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002180
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002181 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2182 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002183 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2184 */
2185 ctx.idx = 0;
2186 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2187 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002188 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2189 (ctx.vlen < 31 ||
2190 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2191 ctx.line[ctx.val + 30] < '6' ||
2192 (ctx.line[ctx.val + 30] == '6' &&
2193 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2194 s->comp_algo = NULL;
2195 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002196 }
2197
William Lallemand82fe75c2012-10-23 10:25:10 +02002198 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002199 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 +01002200 int best_q = 0;
2201
William Lallemand82fe75c2012-10-23 10:25:10 +02002202 ctx.idx = 0;
2203 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002204 const char *qval;
2205 int q;
2206 int toklen;
2207
2208 /* try to isolate the token from the optional q-value */
2209 toklen = 0;
2210 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2211 toklen++;
2212
2213 qval = ctx.line + ctx.val + toklen;
2214 while (1) {
2215 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2216 qval++;
2217
2218 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2219 qval = NULL;
2220 break;
2221 }
2222 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002223
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002224 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2225 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002226
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002227 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2228 qval = NULL;
2229 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002230 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002231 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2232 break;
2233
2234 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2235 qval++;
2236 }
2237
2238 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002239 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002240
2241 if (q <= best_q)
2242 continue;
2243
2244 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2245 if (*(ctx.line + ctx.val) == '*' ||
2246 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2247 s->comp_algo = comp_algo;
2248 best_q = q;
2249 break;
2250 }
2251 }
2252 }
2253 }
2254
2255 /* remove all occurrences of the header when "compression offload" is set */
2256 if (s->comp_algo) {
2257 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2258 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2259 ctx.idx = 0;
2260 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2261 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002262 }
2263 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002264 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002265 }
2266
2267 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002268 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2269 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002270 if (comp_algo->add_data == identity_add_data) {
2271 s->comp_algo = comp_algo;
2272 return 1;
2273 }
2274 }
2275 }
2276
2277 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002278 return 0;
2279}
2280
2281/*
2282 * Selects a comression algorithm depending of the server response.
2283 */
2284int select_compression_response_header(struct session *s, struct buffer *res)
2285{
2286 struct http_txn *txn = &s->txn;
2287 struct http_msg *msg = &txn->rsp;
2288 struct hdr_ctx ctx;
2289 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002290
2291 /* no common compression algorithm was found in request header */
2292 if (s->comp_algo == NULL)
2293 goto fail;
2294
2295 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002296 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002297 goto fail;
2298
William Lallemandd3002612012-11-26 14:34:47 +01002299 /* 200 only */
2300 if (txn->status != 200)
2301 goto fail;
2302
William Lallemand82fe75c2012-10-23 10:25:10 +02002303 /* Content-Length is null */
2304 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2305 goto fail;
2306
2307 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002308 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002309 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2310 goto fail;
2311
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002312 /* no compression when Cache-Control: no-transform is present in the message */
2313 ctx.idx = 0;
2314 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2315 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2316 goto fail;
2317 }
2318
William Lallemand82fe75c2012-10-23 10:25:10 +02002319 comp_type = NULL;
2320
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002321 /* we don't want to compress multipart content-types, nor content-types that are
2322 * not listed in the "compression type" directive if any. If no content-type was
2323 * found but configuration requires one, we don't compress either. Backend has
2324 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002325 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002326 ctx.idx = 0;
2327 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2328 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2329 goto fail;
2330
2331 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2332 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002333 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002334 if (ctx.vlen >= comp_type->name_len &&
2335 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002336 /* this Content-Type should be compressed */
2337 break;
2338 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002339 /* this Content-Type should not be compressed */
2340 if (comp_type == NULL)
2341 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002342 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002343 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002344 else { /* no content-type header */
2345 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2346 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002347 }
2348
William Lallemandd85f9172012-11-09 17:05:39 +01002349 /* limit compression rate */
2350 if (global.comp_rate_lim > 0)
2351 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2352 goto fail;
2353
William Lallemand072a2bf2012-11-20 17:01:01 +01002354 /* limit cpu usage */
2355 if (idle_pct < compress_min_idle)
2356 goto fail;
2357
William Lallemand4c49fae2012-11-07 15:00:23 +01002358 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002359 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002360 goto fail;
2361
William Lallemandec3e3892012-11-12 17:02:18 +01002362 s->flags |= SN_COMP_READY;
2363
William Lallemand82fe75c2012-10-23 10:25:10 +02002364 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002365 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002366 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2367 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2368
2369 /* add Transfer-Encoding header */
2370 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2371 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2372
2373 /*
2374 * Add Content-Encoding header when it's not identity encoding.
2375 * RFC 2616 : Identity encoding: This content-coding is used only in the
2376 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2377 * header.
2378 */
2379 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002380 trash.len = 18;
2381 memcpy(trash.str, "Content-Encoding: ", trash.len);
2382 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2383 trash.len += s->comp_algo->name_len;
2384 trash.str[trash.len] = '\0';
2385 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002386 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002387 return 1;
2388
2389fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002390 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002391 return 0;
2392}
2393
2394
Willy Tarreaud787e662009-07-07 10:14:51 +02002395/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2396 * processing can continue on next analysers, or zero if it either needs more
2397 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2398 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2399 * when it has nothing left to do, and may remove any analyser when it wants to
2400 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002401 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002402int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002403{
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 /*
2405 * We will parse the partial (or complete) lines.
2406 * We will check the request syntax, and also join multi-line
2407 * headers. An index of all the lines will be elaborated while
2408 * parsing.
2409 *
2410 * For the parsing, we use a 28 states FSM.
2411 *
2412 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002413 * req->buf->p = beginning of request
2414 * req->buf->p + msg->eoh = end of processed headers / start of current one
2415 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002416 * msg->eol = end of current header or line (LF or CRLF)
2417 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002418 *
2419 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002420 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002421 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2422 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002424
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002426 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 struct http_txn *txn = &s->txn;
2428 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002429 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002430
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002431 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 +01002432 now_ms, __FUNCTION__,
2433 s,
2434 req,
2435 req->rex, req->wex,
2436 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002437 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002438 req->analysers);
2439
Willy Tarreau52a0c602009-08-16 22:45:38 +02002440 /* we're speaking HTTP here, so let's speak HTTP to the client */
2441 s->srv_error = http_return_srv_error;
2442
Willy Tarreau83e3af02009-12-28 17:39:57 +01002443 /* There's a protected area at the end of the buffer for rewriting
2444 * purposes. We don't want to start to parse the request if the
2445 * protected area is affected, because we may have to move processed
2446 * data later, which is much more complicated.
2447 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002448 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002449 if (txn->flags & TX_NOT_FIRST) {
2450 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002451 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002452 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002453 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002454 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002455 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002456 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002457 return 0;
2458 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002459 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2460 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2461 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002462 }
2463
Willy Tarreau065e8332010-01-08 00:30:20 +01002464 /* Note that we have the same problem with the response ; we
2465 * may want to send a redirect, error or anything which requires
2466 * some spare space. So we'll ensure that we have at least
2467 * maxrewrite bytes available in the response buffer before
2468 * processing that one. This will only affect pipelined
2469 * keep-alive requests.
2470 */
2471 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002472 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002473 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2474 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2475 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002476 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002477 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002478 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002479 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002480 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002481 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002482 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002483 return 0;
2484 }
2485 }
2486
Willy Tarreau9b28e032012-10-12 23:49:43 +02002487 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002488 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002489 }
2490
Willy Tarreau59234e92008-11-30 23:51:27 +01002491 /* 1: we might have to print this header in debug mode */
2492 if (unlikely((global.mode & MODE_DEBUG) &&
2493 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002494 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002495 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002496
Willy Tarreau9b28e032012-10-12 23:49:43 +02002497 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002498 /* this is a bit complex : in case of error on the request line,
2499 * we know that rq.l is still zero, so we display only the part
2500 * up to the end of the line (truncated by debug_hdr).
2501 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002502 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002503 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002504
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 sol += hdr_idx_first_pos(&txn->hdr_idx);
2506 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 while (cur_idx) {
2509 eol = sol + txn->hdr_idx.v[cur_idx].len;
2510 debug_hdr("clihdr", s, sol, eol);
2511 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2512 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002513 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 }
2515
Willy Tarreau58f10d72006-12-04 02:26:12 +01002516
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 /*
2518 * Now we quickly check if we have found a full valid request.
2519 * If not so, we check the FD and buffer states before leaving.
2520 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002521 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002522 * requests are checked first. When waiting for a second request
2523 * on a keep-alive session, if we encounter and error, close, t/o,
2524 * we note the error in the session flags but don't set any state.
2525 * Since the error will be noted there, it will not be counted by
2526 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002527 * Last, we may increase some tracked counters' http request errors on
2528 * the cases that are deliberately the client's fault. For instance,
2529 * a timeout or connection reset is not counted as an error. However
2530 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002532
Willy Tarreau655dce92009-11-08 13:10:58 +01002533 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002534 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002536 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002537 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002538 session_inc_http_req_ctr(s);
2539 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002540 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002542 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002543
Willy Tarreau59234e92008-11-30 23:51:27 +01002544 /* 1: Since we are in header mode, if there's no space
2545 * left for headers, we won't be able to free more
2546 * later, so the session will never terminate. We
2547 * must terminate it now.
2548 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002549 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 /* FIXME: check if URI is set and return Status
2551 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002552 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002553 session_inc_http_req_ctr(s);
2554 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002555 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002556 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002557 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 goto return_bad_req;
2559 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002560
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002562 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002563 if (!(s->flags & SN_ERR_MASK))
2564 s->flags |= SN_ERR_CLICL;
2565
Willy Tarreaufcffa692010-01-10 14:21:19 +01002566 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002567 goto failed_keep_alive;
2568
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002570 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002571 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002572 session_inc_http_err_ctr(s);
2573 }
2574
Willy Tarreaudc979f22012-12-04 10:39:01 +01002575 txn->status = 400;
2576 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002577 msg->msg_state = HTTP_MSG_ERROR;
2578 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002579
Willy Tarreauda7ff642010-06-23 11:44:09 +02002580 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002581 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002582 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002583 if (s->listener->counters)
2584 s->listener->counters->failed_req++;
2585
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 if (!(s->flags & SN_FINST_MASK))
2587 s->flags |= SN_FINST_R;
2588 return 0;
2589 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002590
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002592 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002593 if (!(s->flags & SN_ERR_MASK))
2594 s->flags |= SN_ERR_CLITO;
2595
Willy Tarreaufcffa692010-01-10 14:21:19 +01002596 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002597 goto failed_keep_alive;
2598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002600 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002601 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002602 session_inc_http_err_ctr(s);
2603 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002605 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 msg->msg_state = HTTP_MSG_ERROR;
2607 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002608
Willy Tarreauda7ff642010-06-23 11:44:09 +02002609 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002610 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002611 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002612 if (s->listener->counters)
2613 s->listener->counters->failed_req++;
2614
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 if (!(s->flags & SN_FINST_MASK))
2616 s->flags |= SN_FINST_R;
2617 return 0;
2618 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002619
Willy Tarreau59234e92008-11-30 23:51:27 +01002620 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002621 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002622 if (!(s->flags & SN_ERR_MASK))
2623 s->flags |= SN_ERR_CLICL;
2624
Willy Tarreaufcffa692010-01-10 14:21:19 +01002625 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002626 goto failed_keep_alive;
2627
Willy Tarreau4076a152009-04-02 15:18:36 +02002628 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002629 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002630 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002631 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 msg->msg_state = HTTP_MSG_ERROR;
2633 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002634
Willy Tarreauda7ff642010-06-23 11:44:09 +02002635 session_inc_http_err_ctr(s);
2636 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002637 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002638 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002639 if (s->listener->counters)
2640 s->listener->counters->failed_req++;
2641
Willy Tarreau59234e92008-11-30 23:51:27 +01002642 if (!(s->flags & SN_FINST_MASK))
2643 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002644 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002645 }
2646
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002647 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002648 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2649 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002650#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002651 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 +01002652 /* We need more data, we have to re-enable quick-ack in case we
2653 * previously disabled it, otherwise we might cause the client
2654 * to delay next data.
2655 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002656 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002657 }
2658#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002659
Willy Tarreaufcffa692010-01-10 14:21:19 +01002660 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2661 /* If the client starts to talk, let's fall back to
2662 * request timeout processing.
2663 */
2664 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002665 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002666 }
2667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002669 if (!tick_isset(req->analyse_exp)) {
2670 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2671 (txn->flags & TX_WAIT_NEXT_RQ) &&
2672 tick_isset(s->be->timeout.httpka))
2673 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2674 else
2675 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2676 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002677
Willy Tarreau59234e92008-11-30 23:51:27 +01002678 /* we're not ready yet */
2679 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002680
2681 failed_keep_alive:
2682 /* Here we process low-level errors for keep-alive requests. In
2683 * short, if the request is not the first one and it experiences
2684 * a timeout, read error or shutdown, we just silently close so
2685 * that the client can try again.
2686 */
2687 txn->status = 0;
2688 msg->msg_state = HTTP_MSG_RQBEFORE;
2689 req->analysers = 0;
2690 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002691 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002692 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002693 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002694 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002696
Willy Tarreaud787e662009-07-07 10:14:51 +02002697 /* OK now we have a complete HTTP request with indexed headers. Let's
2698 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002699 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002700 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002701 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002702 * byte after the last LF. msg->sov points to the first byte of data.
2703 * msg->eol cannot be trusted because it may have been left uninitialized
2704 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002705 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002706
Willy Tarreauda7ff642010-06-23 11:44:09 +02002707 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002708 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2709
Willy Tarreaub16a5742010-01-10 14:46:16 +01002710 if (txn->flags & TX_WAIT_NEXT_RQ) {
2711 /* kill the pending keep-alive timeout */
2712 txn->flags &= ~TX_WAIT_NEXT_RQ;
2713 req->analyse_exp = TICK_ETERNITY;
2714 }
2715
2716
Willy Tarreaud787e662009-07-07 10:14:51 +02002717 /* Maybe we found in invalid header name while we were configured not
2718 * to block on that, so we have to capture it now.
2719 */
2720 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002721 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002722
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 /*
2724 * 1: identify the method
2725 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002726 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002727
2728 /* we can make use of server redirect on GET and HEAD */
2729 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2730 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002731
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 /*
2733 * 2: check if the URI matches the monitor_uri.
2734 * We have to do this for every request which gets in, because
2735 * the monitor-uri is defined by the frontend.
2736 */
2737 if (unlikely((s->fe->monitor_uri_len != 0) &&
2738 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002739 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002740 s->fe->monitor_uri,
2741 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002742 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002744 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002746
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002748 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002749
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002751 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002752 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002753
Willy Tarreau59234e92008-11-30 23:51:27 +01002754 ret = acl_pass(ret);
2755 if (cond->pol == ACL_COND_UNLESS)
2756 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002757
Willy Tarreau59234e92008-11-30 23:51:27 +01002758 if (ret) {
2759 /* we fail this request, let's return 503 service unavail */
2760 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002761 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002762 if (!(s->flags & SN_ERR_MASK))
2763 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002765 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002766 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002767
Willy Tarreau59234e92008-11-30 23:51:27 +01002768 /* nothing to fail, let's reply normaly */
2769 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002770 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002771 if (!(s->flags & SN_ERR_MASK))
2772 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002773 goto return_prx_cond;
2774 }
2775
2776 /*
2777 * 3: Maybe we have to copy the original REQURI for the logs ?
2778 * Note: we cannot log anymore if the request has been
2779 * classified as invalid.
2780 */
2781 if (unlikely(s->logs.logwait & LW_REQ)) {
2782 /* we have a complete HTTP request that we must log */
2783 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2784 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002785
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 if (urilen >= REQURI_LEN)
2787 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002788 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002789 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002790
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002791 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002792 s->do_log(s);
2793 } else {
2794 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002795 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002796 }
Willy Tarreau06619262006-12-17 08:37:22 +01002797
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002799 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002800 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002801
Willy Tarreau5b154472009-12-21 20:11:07 +01002802 /* ... and check if the request is HTTP/1.1 or above */
2803 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002804 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2805 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2806 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002807 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002808
2809 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002810 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 +01002811
Willy Tarreau88d349d2010-01-25 12:15:43 +01002812 /* if the frontend has "option http-use-proxy-header", we'll check if
2813 * we have what looks like a proxied connection instead of a connection,
2814 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2815 * Note that this is *not* RFC-compliant, however browsers and proxies
2816 * happen to do that despite being non-standard :-(
2817 * We consider that a request not beginning with either '/' or '*' is
2818 * a proxied connection, which covers both "scheme://location" and
2819 * CONNECT ip:port.
2820 */
2821 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002822 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002823 txn->flags |= TX_USE_PX_CONN;
2824
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002825 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002826 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002827
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002829 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002830 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002832
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002833 /* 6: determine the transfer-length.
2834 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2835 * the presence of a message-body in a REQUEST and its transfer length
2836 * must be determined that way (in order of precedence) :
2837 * 1. The presence of a message-body in a request is signaled by the
2838 * inclusion of a Content-Length or Transfer-Encoding header field
2839 * in the request's header fields. When a request message contains
2840 * both a message-body of non-zero length and a method that does
2841 * not define any semantics for that request message-body, then an
2842 * origin server SHOULD either ignore the message-body or respond
2843 * with an appropriate error message (e.g., 413). A proxy or
2844 * gateway, when presented the same request, SHOULD either forward
2845 * the request inbound with the message- body or ignore the
2846 * message-body when determining a response.
2847 *
2848 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2849 * and the "chunked" transfer-coding (Section 6.2) is used, the
2850 * transfer-length is defined by the use of this transfer-coding.
2851 * If a Transfer-Encoding header field is present and the "chunked"
2852 * transfer-coding is not present, the transfer-length is defined
2853 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002854 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002855 * 3. If a Content-Length header field is present, its decimal value in
2856 * OCTETs represents both the entity-length and the transfer-length.
2857 * If a message is received with both a Transfer-Encoding header
2858 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002859 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002860 * 4. By the server closing the connection. (Closing the connection
2861 * cannot be used to indicate the end of a request body, since that
2862 * would leave no possibility for the server to send back a response.)
2863 *
2864 * Whenever a transfer-coding is applied to a message-body, the set of
2865 * transfer-codings MUST include "chunked", unless the message indicates
2866 * it is terminated by closing the connection. When the "chunked"
2867 * transfer-coding is used, it MUST be the last transfer-coding applied
2868 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002869 */
2870
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002871 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002872 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002873 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002874 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002875 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002876 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002877 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2878 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002879 /* bad transfer-encoding (chunked followed by something else) */
2880 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002881 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002882 break;
2883 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002884 }
2885
Willy Tarreau32b47f42009-10-18 20:55:02 +02002886 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002887 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002888 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002889 signed long long cl;
2890
Willy Tarreauad14f752011-09-02 20:33:27 +02002891 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002892 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002893 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002894 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002895
Willy Tarreauad14f752011-09-02 20:33:27 +02002896 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002897 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002898 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002899 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002900
Willy Tarreauad14f752011-09-02 20:33:27 +02002901 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002902 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002903 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002904 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002905
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002906 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002907 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002908 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002909 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002910
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002911 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002912 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002913 }
2914
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002915 /* bodyless requests have a known length */
2916 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002917 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002918
Willy Tarreau179085c2014-04-28 16:48:56 +02002919 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2920 * only change if both the request and the config reference something else.
2921 * Option httpclose by itself sets tunnel mode where headers are mangled.
2922 * However, if another mode is set, it will affect it (eg: server-close/
2923 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2924 * if FE and BE have the same settings (common). The method consists in
2925 * checking if options changed between the two calls (implying that either
2926 * one is non-null, or one of them is non-null and we are there for the first
2927 * time.
2928 */
2929 if (!(txn->flags & TX_HDR_CONN_PRS) ||
2930 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
2931 int tmp = TX_CON_WANT_KAL;
2932
2933 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2934 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2935 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2936 tmp = TX_CON_WANT_TUN;
2937
2938 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2939 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2940 tmp = TX_CON_WANT_TUN;
2941 }
2942
2943 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2944 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2945 /* option httpclose + server_close => forceclose */
2946 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2947 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2948 tmp = TX_CON_WANT_CLO;
2949 else
2950 tmp = TX_CON_WANT_SCL;
2951 }
2952
2953 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2954 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2955 tmp = TX_CON_WANT_CLO;
2956
2957 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2958 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2959
2960 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2961 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2962 /* parse the Connection header and possibly clean it */
2963 int to_del = 0;
2964 if ((msg->flags & HTTP_MSGF_VER_11) ||
2965 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2966 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2967 to_del |= 2; /* remove "keep-alive" */
2968 if (!(msg->flags & HTTP_MSGF_VER_11))
2969 to_del |= 1; /* remove "close" */
2970 http_parse_connection_header(txn, msg, to_del);
2971 }
2972
2973 /* check if client or config asks for explicit close in KAL/SCL */
2974 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2975 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2976 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2977 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2978 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2979 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2980 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2981 }
2982
Willy Tarreaud787e662009-07-07 10:14:51 +02002983 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002984 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002985 req->analyse_exp = TICK_ETERNITY;
2986 return 1;
2987
2988 return_bad_req:
2989 /* We centralize bad requests processing here */
2990 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2991 /* we detected a parsing error. We want to archive this request
2992 * in the dedicated proxy area for later troubleshooting.
2993 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002994 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002995 }
2996
2997 txn->req.msg_state = HTTP_MSG_ERROR;
2998 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002999 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003000
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003001 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003002 if (s->listener->counters)
3003 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003004
3005 return_prx_cond:
3006 if (!(s->flags & SN_ERR_MASK))
3007 s->flags |= SN_ERR_PRXCOND;
3008 if (!(s->flags & SN_FINST_MASK))
3009 s->flags |= SN_FINST_R;
3010
3011 req->analysers = 0;
3012 req->analyse_exp = TICK_ETERNITY;
3013 return 0;
3014}
3015
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003016
Willy Tarreau347a35d2013-11-22 17:51:09 +01003017/* This function prepares an applet to handle the stats. It can deal with the
3018 * "100-continue" expectation, check that admin rules are met for POST requests,
3019 * and program a response message if something was unexpected. It cannot fail
3020 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003021 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003022 * s->target which is supposed to already point to the stats applet. The caller
3023 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003024 */
3025int http_handle_stats(struct session *s, struct channel *req)
3026{
3027 struct stats_admin_rule *stats_admin_rule;
3028 struct stream_interface *si = s->rep->prod;
3029 struct http_txn *txn = &s->txn;
3030 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003031 struct uri_auth *uri_auth = s->be->uri_auth;
3032 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003033 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003034
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003035 appctx = si_appctx(si);
3036 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3037 appctx->st1 = appctx->st2 = 0;
3038 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3039 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003040 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3041 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003042
3043 uri = msg->chn->buf->p + msg->sl.rq.u;
3044 lookup = uri + uri_auth->uri_len;
3045
3046 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3047 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003048 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003049 break;
3050 }
3051 }
3052
3053 if (uri_auth->refresh) {
3054 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3055 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003056 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003057 break;
3058 }
3059 }
3060 }
3061
3062 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3063 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003064 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003065 break;
3066 }
3067 }
3068
3069 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3070 if (memcmp(h, ";st=", 4) == 0) {
3071 int i;
3072 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003073 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003074 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3075 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003076 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003077 break;
3078 }
3079 }
3080 break;
3081 }
3082 }
3083
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003084 appctx->ctx.stats.scope_str = 0;
3085 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003086 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3087 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3088 int itx = 0;
3089 const char *h2;
3090 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3091 const char *err;
3092
3093 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3094 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003095 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003096 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3097 itx++;
3098 h++;
3099 }
3100
3101 if (itx > STAT_SCOPE_TXT_MAXLEN)
3102 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003103 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003104
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003106 memcpy(scope_txt, h2, itx);
3107 scope_txt[itx] = '\0';
3108 err = invalid_char(scope_txt);
3109 if (err) {
3110 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003111 appctx->ctx.stats.scope_str = 0;
3112 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003113 }
3114 break;
3115 }
3116 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003117
3118 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003119 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003120 int ret = 1;
3121
3122 if (stats_admin_rule->cond) {
3123 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3124 ret = acl_pass(ret);
3125 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3126 ret = !ret;
3127 }
3128
3129 if (ret) {
3130 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003131 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003132 break;
3133 }
3134 }
3135
3136 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003137 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003138 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003139 /* we'll need the request body, possibly after sending 100-continue */
3140 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003141 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003142 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003143 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003144 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3145 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003146 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003147 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003148 else {
3149 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003150 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003151 }
3152
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003153 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003154 return 1;
3155}
3156
Lukas Tribus67db8df2013-06-23 17:37:13 +02003157/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3158 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3159 */
3160static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3161{
3162#ifdef IP_TOS
3163 if (from.ss_family == AF_INET)
3164 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3165#endif
3166#ifdef IPV6_TCLASS
3167 if (from.ss_family == AF_INET6) {
3168 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3169 /* v4-mapped addresses need IP_TOS */
3170 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3171 else
3172 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3173 }
3174#endif
3175}
3176
Willy Tarreau20b0de52012-12-24 15:45:22 +01003177/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003178 * transaction <txn>. Returns the first rule that prevents further processing
3179 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3180 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3181 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003182 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003183static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003184http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003185{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003186 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003187 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003188 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003189
Willy Tarreauff011f22011-01-06 17:51:27 +01003190 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003191 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003192 continue;
3193
Willy Tarreau96257ec2012-12-27 10:46:37 +01003194 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003195 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003196 int ret;
3197
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003198 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003199 ret = acl_pass(ret);
3200
Willy Tarreauff011f22011-01-06 17:51:27 +01003201 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003202 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003203
3204 if (!ret) /* condition not matched */
3205 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003206 }
3207
Willy Tarreau20b0de52012-12-24 15:45:22 +01003208
Willy Tarreau96257ec2012-12-27 10:46:37 +01003209 switch (rule->action) {
3210 case HTTP_REQ_ACT_ALLOW:
3211 return NULL; /* "allow" rules are OK */
3212
3213 case HTTP_REQ_ACT_DENY:
3214 txn->flags |= TX_CLDENY;
3215 return rule;
3216
Willy Tarreauccbcc372012-12-27 12:37:57 +01003217 case HTTP_REQ_ACT_TARPIT:
3218 txn->flags |= TX_CLTARPIT;
3219 return rule;
3220
Willy Tarreau96257ec2012-12-27 10:46:37 +01003221 case HTTP_REQ_ACT_AUTH:
3222 return rule;
3223
Willy Tarreau81499eb2012-12-27 12:19:02 +01003224 case HTTP_REQ_ACT_REDIR:
3225 return rule;
3226
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003227 case HTTP_REQ_ACT_SET_NICE:
3228 s->task->nice = rule->arg.nice;
3229 break;
3230
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003231 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003232 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003233 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003234 break;
3235
Willy Tarreau51347ed2013-06-11 19:34:13 +02003236 case HTTP_REQ_ACT_SET_MARK:
3237#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003238 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003239 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003240#endif
3241 break;
3242
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003243 case HTTP_REQ_ACT_SET_LOGL:
3244 s->logs.level = rule->arg.loglevel;
3245 break;
3246
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003247 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003248 case HTTP_REQ_ACT_SET_HDR:
3249 ctx.idx = 0;
3250 /* remove all occurrences of the header */
3251 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3252 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3253 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003254 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003255 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3256 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003257 /* now fall through to header addition */
3258
3259 case HTTP_REQ_ACT_ADD_HDR:
3260 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3261 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3262 trash.len = rule->arg.hdr_add.name_len;
3263 trash.str[trash.len++] = ':';
3264 trash.str[trash.len++] = ' ';
3265 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3266 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3267 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003268
3269 case HTTP_REQ_ACT_DEL_ACL:
3270 case HTTP_REQ_ACT_DEL_MAP: {
3271 struct pat_ref *ref;
3272 char *key;
3273 int len;
3274
3275 /* collect reference */
3276 ref = pat_ref_lookup(rule->arg.map.ref);
3277 if (!ref)
3278 continue;
3279
3280 /* collect key */
3281 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3282 key = trash.str;
3283 key[len] = '\0';
3284
3285 /* perform update */
3286 /* returned code: 1=ok, 0=ko */
3287 pat_ref_delete(ref, key);
3288
3289 break;
3290 }
3291
3292 case HTTP_REQ_ACT_ADD_ACL: {
3293 struct pat_ref *ref;
3294 char *key;
3295 struct chunk *trash_key;
3296 int len;
3297
3298 trash_key = get_trash_chunk();
3299
3300 /* collect reference */
3301 ref = pat_ref_lookup(rule->arg.map.ref);
3302 if (!ref)
3303 continue;
3304
3305 /* collect key */
3306 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3307 key = trash_key->str;
3308 key[len] = '\0';
3309
3310 /* perform update */
3311 /* add entry only if it does not already exist */
3312 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003313 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003314
3315 break;
3316 }
3317
3318 case HTTP_REQ_ACT_SET_MAP: {
3319 struct pat_ref *ref;
3320 char *key, *value;
3321 struct chunk *trash_key, *trash_value;
3322 int len;
3323
3324 trash_key = get_trash_chunk();
3325 trash_value = get_trash_chunk();
3326
3327 /* collect reference */
3328 ref = pat_ref_lookup(rule->arg.map.ref);
3329 if (!ref)
3330 continue;
3331
3332 /* collect key */
3333 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3334 key = trash_key->str;
3335 key[len] = '\0';
3336
3337 /* collect value */
3338 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3339 value = trash_value->str;
3340 value[len] = '\0';
3341
3342 /* perform update */
3343 if (pat_ref_find_elt(ref, key) != NULL)
3344 /* update entry if it exists */
3345 pat_ref_set(ref, key, value, NULL);
3346 else
3347 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003348 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003349
3350 break;
3351 }
William Lallemand73025dd2014-04-24 14:38:37 +02003352
3353 case HTTP_REQ_ACT_CUSTOM_CONT:
3354 rule->action_ptr(rule, px, s, txn);
3355 break;
3356
3357 case HTTP_REQ_ACT_CUSTOM_STOP:
3358 rule->action_ptr(rule, px, s, txn);
3359 return rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003360 }
3361 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003362
3363 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003364 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003365}
3366
Willy Tarreau71241ab2012-12-27 11:30:54 +01003367
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003368/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3369 * transaction <txn>. Returns the first rule that prevents further processing
3370 * of the response (deny, ...) or NULL if it executed all rules or stopped
3371 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3372 * rule.
3373 */
3374static struct http_res_rule *
3375http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3376{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003377 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003378 struct http_res_rule *rule;
3379 struct hdr_ctx ctx;
3380
3381 list_for_each_entry(rule, rules, list) {
3382 if (rule->action >= HTTP_RES_ACT_MAX)
3383 continue;
3384
3385 /* check optional condition */
3386 if (rule->cond) {
3387 int ret;
3388
3389 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3390 ret = acl_pass(ret);
3391
3392 if (rule->cond->pol == ACL_COND_UNLESS)
3393 ret = !ret;
3394
3395 if (!ret) /* condition not matched */
3396 continue;
3397 }
3398
3399
3400 switch (rule->action) {
3401 case HTTP_RES_ACT_ALLOW:
3402 return NULL; /* "allow" rules are OK */
3403
3404 case HTTP_RES_ACT_DENY:
3405 txn->flags |= TX_SVDENY;
3406 return rule;
3407
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003408 case HTTP_RES_ACT_SET_NICE:
3409 s->task->nice = rule->arg.nice;
3410 break;
3411
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003412 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003413 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003414 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003415 break;
3416
Willy Tarreau51347ed2013-06-11 19:34:13 +02003417 case HTTP_RES_ACT_SET_MARK:
3418#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003419 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003420 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003421#endif
3422 break;
3423
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003424 case HTTP_RES_ACT_SET_LOGL:
3425 s->logs.level = rule->arg.loglevel;
3426 break;
3427
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003428 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003429 case HTTP_RES_ACT_SET_HDR:
3430 ctx.idx = 0;
3431 /* remove all occurrences of the header */
3432 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3433 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3434 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3435 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003436 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3437 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003438 /* now fall through to header addition */
3439
3440 case HTTP_RES_ACT_ADD_HDR:
3441 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3442 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3443 trash.len = rule->arg.hdr_add.name_len;
3444 trash.str[trash.len++] = ':';
3445 trash.str[trash.len++] = ' ';
3446 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3447 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3448 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003449
3450 case HTTP_RES_ACT_DEL_ACL:
3451 case HTTP_RES_ACT_DEL_MAP: {
3452 struct pat_ref *ref;
3453 char *key;
3454 int len;
3455
3456 /* collect reference */
3457 ref = pat_ref_lookup(rule->arg.map.ref);
3458 if (!ref)
3459 continue;
3460
3461 /* collect key */
3462 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3463 key = trash.str;
3464 key[len] = '\0';
3465
3466 /* perform update */
3467 /* returned code: 1=ok, 0=ko */
3468 pat_ref_delete(ref, key);
3469
3470 break;
3471 }
3472
3473 case HTTP_RES_ACT_ADD_ACL: {
3474 struct pat_ref *ref;
3475 char *key;
3476 struct chunk *trash_key;
3477 int len;
3478
3479 trash_key = get_trash_chunk();
3480
3481 /* collect reference */
3482 ref = pat_ref_lookup(rule->arg.map.ref);
3483 if (!ref)
3484 continue;
3485
3486 /* collect key */
3487 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3488 key = trash_key->str;
3489 key[len] = '\0';
3490
3491 /* perform update */
3492 /* check if the entry already exists */
3493 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003494 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003495
3496 break;
3497 }
3498
3499 case HTTP_RES_ACT_SET_MAP: {
3500 struct pat_ref *ref;
3501 char *key, *value;
3502 struct chunk *trash_key, *trash_value;
3503 int len;
3504
3505 trash_key = get_trash_chunk();
3506 trash_value = get_trash_chunk();
3507
3508 /* collect reference */
3509 ref = pat_ref_lookup(rule->arg.map.ref);
3510 if (!ref)
3511 continue;
3512
3513 /* collect key */
3514 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3515 key = trash_key->str;
3516 key[len] = '\0';
3517
3518 /* collect value */
3519 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3520 value = trash_value->str;
3521 value[len] = '\0';
3522
3523 /* perform update */
3524 if (pat_ref_find_elt(ref, key) != NULL)
3525 /* update entry if it exists */
3526 pat_ref_set(ref, key, value, NULL);
3527 else
3528 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003529 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003530
3531 break;
3532 }
William Lallemand73025dd2014-04-24 14:38:37 +02003533
3534 case HTTP_RES_ACT_CUSTOM_CONT:
3535 rule->action_ptr(rule, px, s, txn);
3536 break;
3537
3538 case HTTP_RES_ACT_CUSTOM_STOP:
3539 rule->action_ptr(rule, px, s, txn);
3540 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003541 }
3542 }
3543
3544 /* we reached the end of the rules, nothing to report */
3545 return NULL;
3546}
3547
3548
Willy Tarreau71241ab2012-12-27 11:30:54 +01003549/* Perform an HTTP redirect based on the information in <rule>. The function
3550 * returns non-zero on success, or zero in case of a, irrecoverable error such
3551 * as too large a request to build a valid response.
3552 */
3553static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3554{
3555 struct http_msg *msg = &txn->req;
3556 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003557 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003558
3559 /* build redirect message */
3560 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003561 case 308:
3562 msg_fmt = HTTP_308;
3563 break;
3564 case 307:
3565 msg_fmt = HTTP_307;
3566 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003567 case 303:
3568 msg_fmt = HTTP_303;
3569 break;
3570 case 301:
3571 msg_fmt = HTTP_301;
3572 break;
3573 case 302:
3574 default:
3575 msg_fmt = HTTP_302;
3576 break;
3577 }
3578
3579 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3580 return 0;
3581
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003582 location = trash.str + trash.len;
3583
Willy Tarreau71241ab2012-12-27 11:30:54 +01003584 switch(rule->type) {
3585 case REDIRECT_TYPE_SCHEME: {
3586 const char *path;
3587 const char *host;
3588 struct hdr_ctx ctx;
3589 int pathlen;
3590 int hostlen;
3591
3592 host = "";
3593 hostlen = 0;
3594 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003595 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003596 host = ctx.line + ctx.val;
3597 hostlen = ctx.vlen;
3598 }
3599
3600 path = http_get_path(txn);
3601 /* build message using path */
3602 if (path) {
3603 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3604 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3605 int qs = 0;
3606 while (qs < pathlen) {
3607 if (path[qs] == '?') {
3608 pathlen = qs;
3609 break;
3610 }
3611 qs++;
3612 }
3613 }
3614 } else {
3615 path = "/";
3616 pathlen = 1;
3617 }
3618
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003619 if (rule->rdr_str) { /* this is an old "redirect" rule */
3620 /* check if we can add scheme + "://" + host + path */
3621 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3622 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003623
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003624 /* add scheme */
3625 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3626 trash.len += rule->rdr_len;
3627 }
3628 else {
3629 /* add scheme with executing log format */
3630 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003631
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003632 /* check if we can add scheme + "://" + host + path */
3633 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3634 return 0;
3635 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003636 /* add "://" */
3637 memcpy(trash.str + trash.len, "://", 3);
3638 trash.len += 3;
3639
3640 /* add host */
3641 memcpy(trash.str + trash.len, host, hostlen);
3642 trash.len += hostlen;
3643
3644 /* add path */
3645 memcpy(trash.str + trash.len, path, pathlen);
3646 trash.len += pathlen;
3647
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003648 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003649 if (trash.len && trash.str[trash.len - 1] != '/' &&
3650 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3651 if (trash.len > trash.size - 5)
3652 return 0;
3653 trash.str[trash.len] = '/';
3654 trash.len++;
3655 }
3656
3657 break;
3658 }
3659 case REDIRECT_TYPE_PREFIX: {
3660 const char *path;
3661 int pathlen;
3662
3663 path = http_get_path(txn);
3664 /* build message using path */
3665 if (path) {
3666 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3667 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3668 int qs = 0;
3669 while (qs < pathlen) {
3670 if (path[qs] == '?') {
3671 pathlen = qs;
3672 break;
3673 }
3674 qs++;
3675 }
3676 }
3677 } else {
3678 path = "/";
3679 pathlen = 1;
3680 }
3681
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003682 if (rule->rdr_str) { /* this is an old "redirect" rule */
3683 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3684 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003685
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003686 /* add prefix. Note that if prefix == "/", we don't want to
3687 * add anything, otherwise it makes it hard for the user to
3688 * configure a self-redirection.
3689 */
3690 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3691 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3692 trash.len += rule->rdr_len;
3693 }
3694 }
3695 else {
3696 /* add prefix with executing log format */
3697 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3698
3699 /* Check length */
3700 if (trash.len + pathlen > trash.size - 4)
3701 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003702 }
3703
3704 /* add path */
3705 memcpy(trash.str + trash.len, path, pathlen);
3706 trash.len += pathlen;
3707
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003708 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003709 if (trash.len && trash.str[trash.len - 1] != '/' &&
3710 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3711 if (trash.len > trash.size - 5)
3712 return 0;
3713 trash.str[trash.len] = '/';
3714 trash.len++;
3715 }
3716
3717 break;
3718 }
3719 case REDIRECT_TYPE_LOCATION:
3720 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003721 if (rule->rdr_str) { /* this is an old "redirect" rule */
3722 if (trash.len + rule->rdr_len > trash.size - 4)
3723 return 0;
3724
3725 /* add location */
3726 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3727 trash.len += rule->rdr_len;
3728 }
3729 else {
3730 /* add location with executing log format */
3731 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003732
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003733 /* Check left length */
3734 if (trash.len > trash.size - 4)
3735 return 0;
3736 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003737 break;
3738 }
3739
3740 if (rule->cookie_len) {
3741 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3742 trash.len += 14;
3743 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3744 trash.len += rule->cookie_len;
3745 memcpy(trash.str + trash.len, "\r\n", 2);
3746 trash.len += 2;
3747 }
3748
3749 /* add end of headers and the keep-alive/close status.
3750 * We may choose to set keep-alive if the Location begins
3751 * with a slash, because the client will come back to the
3752 * same server.
3753 */
3754 txn->status = rule->code;
3755 /* let's log the request time */
3756 s->logs.tv_request = now;
3757
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003758 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003759 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3760 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3761 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3762 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3763 /* keep-alive possible */
3764 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3765 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3766 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3767 trash.len += 30;
3768 } else {
3769 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3770 trash.len += 24;
3771 }
3772 }
3773 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3774 trash.len += 4;
3775 bo_inject(txn->rsp.chn, trash.str, trash.len);
3776 /* "eat" the request */
3777 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003778 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003779 msg->sov = 0;
3780 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3781 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3782 txn->req.msg_state = HTTP_MSG_CLOSED;
3783 txn->rsp.msg_state = HTTP_MSG_DONE;
3784 } else {
3785 /* keep-alive not possible */
3786 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3787 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3788 trash.len += 29;
3789 } else {
3790 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3791 trash.len += 23;
3792 }
3793 stream_int_retnclose(txn->req.chn->prod, &trash);
3794 txn->req.chn->analysers = 0;
3795 }
3796
3797 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003798 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003799 if (!(s->flags & SN_FINST_MASK))
3800 s->flags |= SN_FINST_R;
3801
3802 return 1;
3803}
3804
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003805/* This stream analyser runs all HTTP request processing which is common to
3806 * frontends and backends, which means blocking ACLs, filters, connection-close,
3807 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003808 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003809 * either needs more data or wants to immediately abort the request (eg: deny,
3810 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003811 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003812int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003813{
Willy Tarreaud787e662009-07-07 10:14:51 +02003814 struct http_txn *txn = &s->txn;
3815 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003816 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003817 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003818 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003819 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003820
Willy Tarreau655dce92009-11-08 13:10:58 +01003821 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003822 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003823 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003824 return 0;
3825 }
3826
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003827 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 +02003828 now_ms, __FUNCTION__,
3829 s,
3830 req,
3831 req->rex, req->wex,
3832 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003833 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003834 req->analysers);
3835
Willy Tarreau65410832014-04-28 21:25:43 +02003836 /* just in case we have some per-backend tracking */
3837 session_inc_be_http_req_ctr(s);
3838
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003839 /* first check whether we have some ACLs set to block this request */
3840 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003841 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003842
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003843 ret = acl_pass(ret);
3844 if (cond->pol == ACL_COND_UNLESS)
3845 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003846
Willy Tarreau5fa70822014-04-28 18:27:12 +02003847 if (ret)
3848 goto deny;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003849 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003850
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003851 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003852 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003853
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003854 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
3855 if (txn->flags & TX_CLDENY)
3856 goto deny;
3857 else
3858 goto tarpit;
3859 }
3860
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003861 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003862 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003863 if (stats_check_uri(s->rep->prod, txn, px)) {
3864 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003865 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3866 txn->status = 500;
3867 s->logs.tv_request = now;
3868 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003869
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003870 if (!(s->flags & SN_ERR_MASK))
3871 s->flags |= SN_ERR_RESOURCE;
3872 goto return_prx_cond;
3873 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003874 /* parse the whole stats request and extract the relevant information */
3875 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003876 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003877
3878 /* we can still get a deny on the stats page */
3879 if (txn->flags & TX_CLDENY)
3880 goto deny;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003881 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003882 }
3883
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003884 /* evaluate the req* rules except reqadd */
3885 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003886 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003887 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003888
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003889 if (txn->flags & TX_CLDENY)
3890 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003891
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003892 if (txn->flags & TX_CLTARPIT)
3893 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003894 }
Willy Tarreau06619262006-12-17 08:37:22 +01003895
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003896 /* we can be blocked here because the request needs to be authenticated,
3897 * either to pass or to access stats.
3898 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003899 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003900 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003901
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003902 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003903 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003904
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003905 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Willy Tarreaue6d24162014-04-28 16:59:15 +02003906 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003907 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003908 /* on 401 we still count one error, because normal browsing
3909 * won't significantly increase the counter but brute force
3910 * attempts will.
3911 */
3912 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003913 goto return_prx_cond;
3914 }
3915
Willy Tarreau5e9edce2014-04-28 11:13:33 +02003916 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3917 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3918 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003919 goto done;
Willy Tarreau5e9edce2014-04-28 11:13:33 +02003920 }
3921
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003922 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_CUSTOM_STOP)
3923 goto done;
Willy Tarreau5e9edce2014-04-28 11:13:33 +02003924
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003925 /* add request headers from the rule sets in the same order */
3926 list_for_each_entry(wl, &px->req_add, list) {
3927 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003928 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003929 ret = acl_pass(ret);
3930 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3931 ret = !ret;
3932 if (!ret)
3933 continue;
3934 }
3935
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003936 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003937 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003938 }
3939
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003940 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003941 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003942 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3943 s->fe->fe_counters.intercepted_req++;
3944
3945 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3946 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3947 if (!(s->flags & SN_FINST_MASK))
3948 s->flags |= SN_FINST_R;
3949
Willy Tarreau70730dd2014-04-24 18:06:27 +02003950 /* we may want to compress the stats page */
3951 if (s->fe->comp || s->be->comp)
3952 select_compression_request_header(s, req->buf);
3953
3954 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003955 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) |
3956 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 +02003957 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003958 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003959
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003960 /* check whether we have some ACLs set to redirect this request */
3961 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003962 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003963 int ret;
3964
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003965 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003966 ret = acl_pass(ret);
3967 if (rule->cond->pol == ACL_COND_UNLESS)
3968 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003969 if (!ret)
3970 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003971 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003972 if (!http_apply_redirect_rule(rule, s, txn))
3973 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003974 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003975 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003976
Willy Tarreau2be39392010-01-03 17:24:51 +01003977 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3978 * If this happens, then the data will not come immediately, so we must
3979 * send all what we have without waiting. Note that due to the small gain
3980 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003981 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003982 * itself once used.
3983 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003984 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003985
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003986 done: /* done with this analyser, continue with next ones that the calling
3987 * points will have set, if any.
3988 */
3989 req->analysers &= ~an_bit;
3990 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003991 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003992
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003993 tarpit:
3994 /* When a connection is tarpitted, we use the tarpit timeout,
3995 * which may be the same as the connect timeout if unspecified.
3996 * If unset, then set it to zero because we really want it to
3997 * eventually expire. We build the tarpit as an analyser.
3998 */
3999 channel_erase(s->req);
4000
4001 /* wipe the request out so that we can drop the connection early
4002 * if the client closes first.
4003 */
4004 channel_dont_connect(req);
4005 req->analysers = 0; /* remove switching rules etc... */
4006 req->analysers |= AN_REQ_HTTP_TARPIT;
4007 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4008 if (!req->analyse_exp)
4009 req->analyse_exp = tick_add(now_ms, 0);
4010 session_inc_http_err_ctr(s);
4011 s->fe->fe_counters.denied_req++;
4012 if (s->fe != s->be)
4013 s->be->be_counters.denied_req++;
4014 if (s->listener->counters)
4015 s->listener->counters->denied_req++;
4016 goto done;
4017
4018 deny: /* this request was blocked (denied) */
4019 txn->status = 403;
4020 s->logs.tv_request = now;
4021 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4022 session_inc_http_err_ctr(s);
4023 s->fe->fe_counters.denied_req++;
4024 if (s->fe != s->be)
4025 s->be->be_counters.denied_req++;
4026 if (s->listener->counters)
4027 s->listener->counters->denied_req++;
4028 goto return_prx_cond;
4029
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004030 return_bad_req:
4031 /* We centralize bad requests processing here */
4032 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4033 /* we detected a parsing error. We want to archive this request
4034 * in the dedicated proxy area for later troubleshooting.
4035 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004036 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004037 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004038
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004039 txn->req.msg_state = HTTP_MSG_ERROR;
4040 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004041 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004042
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004043 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004044 if (s->listener->counters)
4045 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004046
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004047 return_prx_cond:
4048 if (!(s->flags & SN_ERR_MASK))
4049 s->flags |= SN_ERR_PRXCOND;
4050 if (!(s->flags & SN_FINST_MASK))
4051 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004053 req->analysers = 0;
4054 req->analyse_exp = TICK_ETERNITY;
4055 return 0;
4056}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004057
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004058/* This function performs all the processing enabled for the current request.
4059 * It returns 1 if the processing can continue on next analysers, or zero if it
4060 * needs more data, encounters an error, or wants to immediately abort the
4061 * request. It relies on buffers flags, and updates s->req->analysers.
4062 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004063int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004064{
4065 struct http_txn *txn = &s->txn;
4066 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004067 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004068
Willy Tarreau655dce92009-11-08 13:10:58 +01004069 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004070 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004071 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004072 return 0;
4073 }
4074
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004075 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 +02004076 now_ms, __FUNCTION__,
4077 s,
4078 req,
4079 req->rex, req->wex,
4080 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004081 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004082 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004083
William Lallemand82fe75c2012-10-23 10:25:10 +02004084 if (s->fe->comp || s->be->comp)
4085 select_compression_request_header(s, req->buf);
4086
Willy Tarreau59234e92008-11-30 23:51:27 +01004087 /*
4088 * Right now, we know that we have processed the entire headers
4089 * and that unwanted requests have been filtered out. We can do
4090 * whatever we want with the remaining request. Also, now we
4091 * may have separate values for ->fe, ->be.
4092 */
Willy Tarreau06619262006-12-17 08:37:22 +01004093
Willy Tarreau59234e92008-11-30 23:51:27 +01004094 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004095 * If HTTP PROXY is set we simply get remote server address parsing
4096 * incoming request. Note that this requires that a connection is
4097 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004098 */
4099 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004100 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004101 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004102
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004103 /* Note that for now we don't reuse existing proxy connections */
4104 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004105 txn->req.msg_state = HTTP_MSG_ERROR;
4106 txn->status = 500;
4107 req->analysers = 0;
4108 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4109
4110 if (!(s->flags & SN_ERR_MASK))
4111 s->flags |= SN_ERR_RESOURCE;
4112 if (!(s->flags & SN_FINST_MASK))
4113 s->flags |= SN_FINST_R;
4114
4115 return 0;
4116 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004117
4118 path = http_get_path(txn);
4119 url2sa(req->buf->p + msg->sl.rq.u,
4120 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004121 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004122 /* if the path was found, we have to remove everything between
4123 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4124 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4125 * u_l characters by a single "/".
4126 */
4127 if (path) {
4128 char *cur_ptr = req->buf->p;
4129 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4130 int delta;
4131
4132 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4133 http_msg_move_end(&txn->req, delta);
4134 cur_end += delta;
4135 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4136 goto return_bad_req;
4137 }
4138 else {
4139 char *cur_ptr = req->buf->p;
4140 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4141 int delta;
4142
4143 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4144 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4145 http_msg_move_end(&txn->req, delta);
4146 cur_end += delta;
4147 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4148 goto return_bad_req;
4149 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004150 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004151
Willy Tarreau59234e92008-11-30 23:51:27 +01004152 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004153 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004154 * Note that doing so might move headers in the request, but
4155 * the fields will stay coherent and the URI will not move.
4156 * This should only be performed in the backend.
4157 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004158 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004159 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4160 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004161
Willy Tarreau59234e92008-11-30 23:51:27 +01004162 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004163 * 8: the appsession cookie was looked up very early in 1.2,
4164 * so let's do the same now.
4165 */
4166
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004167 /* It needs to look into the URI unless persistence must be ignored */
4168 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004169 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 +01004170 }
4171
William Lallemanda73203e2012-03-12 12:48:57 +01004172 /* add unique-id if "header-unique-id" is specified */
4173
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004174 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4175 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4176 goto return_bad_req;
4177 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004178 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004179 }
William Lallemanda73203e2012-03-12 12:48:57 +01004180
4181 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004182 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4183 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004184 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004185 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004186 goto return_bad_req;
4187 }
4188
Cyril Bontéb21570a2009-11-29 20:04:48 +01004189 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004190 * 9: add X-Forwarded-For if either the frontend or the backend
4191 * asks for it.
4192 */
4193 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004194 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004195 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004196 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4197 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004198 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004199 /* The header is set to be added only if none is present
4200 * and we found it, so don't do anything.
4201 */
4202 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004203 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004204 /* Add an X-Forwarded-For header unless the source IP is
4205 * in the 'except' network range.
4206 */
4207 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004208 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004209 != s->fe->except_net.s_addr) &&
4210 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004211 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004212 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004213 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004214 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004215 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004216
4217 /* Note: we rely on the backend to get the header name to be used for
4218 * x-forwarded-for, because the header is really meant for the backends.
4219 * However, if the backend did not specify any option, we have to rely
4220 * on the frontend's header name.
4221 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004222 if (s->be->fwdfor_hdr_len) {
4223 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004224 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004225 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004226 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004227 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004228 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004229 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 +01004230
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004231 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004232 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004233 }
4234 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004235 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004236 /* FIXME: for the sake of completeness, we should also support
4237 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004238 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004239 int len;
4240 char pn[INET6_ADDRSTRLEN];
4241 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004242 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004243 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004244
Willy Tarreau59234e92008-11-30 23:51:27 +01004245 /* Note: we rely on the backend to get the header name to be used for
4246 * x-forwarded-for, because the header is really meant for the backends.
4247 * However, if the backend did not specify any option, we have to rely
4248 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004249 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004250 if (s->be->fwdfor_hdr_len) {
4251 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004252 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004253 } else {
4254 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004255 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004256 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004257 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004258
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004259 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004260 goto return_bad_req;
4261 }
4262 }
4263
4264 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004265 * 10: add X-Original-To if either the frontend or the backend
4266 * asks for it.
4267 */
4268 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4269
4270 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004271 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004272 /* Add an X-Original-To header unless the destination IP is
4273 * in the 'except' network range.
4274 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004275 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004276
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004277 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004278 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004279 (((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 +02004280 != s->fe->except_to.s_addr) &&
4281 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004282 (((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 +02004283 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004284 int len;
4285 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004286 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004287
4288 /* Note: we rely on the backend to get the header name to be used for
4289 * x-original-to, because the header is really meant for the backends.
4290 * However, if the backend did not specify any option, we have to rely
4291 * on the frontend's header name.
4292 */
4293 if (s->be->orgto_hdr_len) {
4294 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004295 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004296 } else {
4297 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004298 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004299 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004300 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 +02004301
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004302 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004303 goto return_bad_req;
4304 }
4305 }
4306 }
4307
Willy Tarreau50fc7772012-11-11 22:19:57 +01004308 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4309 * If an "Upgrade" token is found, the header is left untouched in order not to have
4310 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4311 * "Upgrade" is present in the Connection header.
4312 */
4313 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4314 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004315 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4316 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004317 unsigned int want_flags = 0;
4318
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004319 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004320 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004321 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4322 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004323 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004324 want_flags |= TX_CON_CLO_SET;
4325 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004326 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004327 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4328 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004329 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004330 want_flags |= TX_CON_KAL_SET;
4331 }
4332
4333 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004334 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004335 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004336
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004337
Willy Tarreau522d6c02009-12-06 18:49:18 +01004338 /* If we have no server assigned yet and we're balancing on url_param
4339 * with a POST request, we may be interested in checking the body for
4340 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004341 */
4342 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4343 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004344 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004345 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004346 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004347 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004348
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004349 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004350 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004351#ifdef TCP_QUICKACK
4352 /* We expect some data from the client. Unless we know for sure
4353 * we already have a full request, we have to re-enable quick-ack
4354 * in case we previously disabled it, otherwise we might cause
4355 * the client to delay further data.
4356 */
4357 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004358 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004359 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004360 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004361 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004362#endif
4363 }
Willy Tarreau03945942009-12-22 16:50:27 +01004364
Willy Tarreau59234e92008-11-30 23:51:27 +01004365 /*************************************************************
4366 * OK, that's finished for the headers. We have done what we *
4367 * could. Let's switch to the DATA state. *
4368 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004369 req->analyse_exp = TICK_ETERNITY;
4370 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004371
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004372 /* if the server closes the connection, we want to immediately react
4373 * and close the socket to save packets and syscalls.
4374 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004375 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4376 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004377
Willy Tarreau59234e92008-11-30 23:51:27 +01004378 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004379 /* OK let's go on with the BODY now */
4380 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004381
Willy Tarreau59234e92008-11-30 23:51:27 +01004382 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004383 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004384 /* we detected a parsing error. We want to archive this request
4385 * in the dedicated proxy area for later troubleshooting.
4386 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004387 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004388 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004389
Willy Tarreau59234e92008-11-30 23:51:27 +01004390 txn->req.msg_state = HTTP_MSG_ERROR;
4391 txn->status = 400;
4392 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004393 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004394
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004395 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004396 if (s->listener->counters)
4397 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004398
Willy Tarreau59234e92008-11-30 23:51:27 +01004399 if (!(s->flags & SN_ERR_MASK))
4400 s->flags |= SN_ERR_PRXCOND;
4401 if (!(s->flags & SN_FINST_MASK))
4402 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004403 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004404}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004405
Willy Tarreau60b85b02008-11-30 23:28:40 +01004406/* This function is an analyser which processes the HTTP tarpit. It always
4407 * returns zero, at the beginning because it prevents any other processing
4408 * from occurring, and at the end because it terminates the request.
4409 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004410int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004411{
4412 struct http_txn *txn = &s->txn;
4413
4414 /* This connection is being tarpitted. The CLIENT side has
4415 * already set the connect expiration date to the right
4416 * timeout. We just have to check that the client is still
4417 * there and that the timeout has not expired.
4418 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004419 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004420 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004421 !tick_is_expired(req->analyse_exp, now_ms))
4422 return 0;
4423
4424 /* We will set the queue timer to the time spent, just for
4425 * logging purposes. We fake a 500 server error, so that the
4426 * attacker will not suspect his connection has been tarpitted.
4427 * It will not cause trouble to the logs because we can exclude
4428 * the tarpitted connections by filtering on the 'PT' status flags.
4429 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004430 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4431
4432 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004433 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004434 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004435
4436 req->analysers = 0;
4437 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004438
Willy Tarreau60b85b02008-11-30 23:28:40 +01004439 if (!(s->flags & SN_ERR_MASK))
4440 s->flags |= SN_ERR_PRXCOND;
4441 if (!(s->flags & SN_FINST_MASK))
4442 s->flags |= SN_FINST_T;
4443 return 0;
4444}
4445
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004446/* This function is an analyser which waits for the HTTP request body. It waits
4447 * for either the buffer to be full, or the full advertised contents to have
4448 * reached the buffer. It must only be called after the standard HTTP request
4449 * processing has occurred, because it expects the request to be parsed and will
4450 * look for the Expect header. It may send a 100-Continue interim response. It
4451 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4452 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4453 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004454 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004455int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004456{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004457 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004458 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004459
4460 /* We have to parse the HTTP request body to find any required data.
4461 * "balance url_param check_post" should have been the only way to get
4462 * into this. We were brought here after HTTP header analysis, so all
4463 * related structures are ready.
4464 */
4465
Willy Tarreau890988f2014-04-10 11:59:33 +02004466 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4467 /* This is the first call */
4468 if (msg->msg_state < HTTP_MSG_BODY)
4469 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004470
Willy Tarreau890988f2014-04-10 11:59:33 +02004471 if (msg->msg_state < HTTP_MSG_100_SENT) {
4472 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4473 * send an HTTP/1.1 100 Continue intermediate response.
4474 */
4475 if (msg->flags & HTTP_MSGF_VER_11) {
4476 struct hdr_ctx ctx;
4477 ctx.idx = 0;
4478 /* Expect is allowed in 1.1, look for it */
4479 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4480 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4481 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4482 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004483 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004484 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004485 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004486
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004487 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004488 * req->buf->p still points to the beginning of the message. We
4489 * must save the body in msg->next because it survives buffer
4490 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004491 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004492 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004493
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004494 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004495 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4496 else
4497 msg->msg_state = HTTP_MSG_DATA;
4498 }
4499
Willy Tarreau890988f2014-04-10 11:59:33 +02004500 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4501 /* We're in content-length mode, we just have to wait for enough data. */
4502 if (req->buf->i - msg->sov < msg->body_len)
4503 goto missing_data;
4504
4505 /* OK we have everything we need now */
4506 goto http_end;
4507 }
4508
4509 /* OK here we're parsing a chunked-encoded message */
4510
Willy Tarreau522d6c02009-12-06 18:49:18 +01004511 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004512 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004513 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004514 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004515 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004516 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004517
Willy Tarreau115acb92009-12-26 13:56:06 +01004518 if (!ret)
4519 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004520 else if (ret < 0) {
4521 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004522 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004523 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004524 }
4525
Willy Tarreaud98cf932009-12-27 22:54:55 +01004526 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004527 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004528 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004529 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004530 if (msg->msg_state == HTTP_MSG_TRAILERS)
4531 goto http_end;
4532
Willy Tarreau226071e2014-04-10 11:55:45 +02004533 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004534 goto http_end;
4535
4536 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004537 /* we get here if we need to wait for more data. If the buffer is full,
4538 * we have the maximum we can expect.
4539 */
4540 if (buffer_full(req->buf, global.tune.maxrewrite))
4541 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004542
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004543 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004544 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004545 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004546
4547 if (!(s->flags & SN_ERR_MASK))
4548 s->flags |= SN_ERR_CLITO;
4549 if (!(s->flags & SN_FINST_MASK))
4550 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004551 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004552 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004553
4554 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004555 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004556 /* Not enough data. We'll re-use the http-request
4557 * timeout here. Ideally, we should set the timeout
4558 * relative to the accept() date. We just set the
4559 * request timeout once at the beginning of the
4560 * request.
4561 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004562 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004563 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004564 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004565 return 0;
4566 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004567
4568 http_end:
4569 /* The situation will not evolve, so let's give up on the analysis. */
4570 s->logs.tv_request = now; /* update the request timer to reflect full request */
4571 req->analysers &= ~an_bit;
4572 req->analyse_exp = TICK_ETERNITY;
4573 return 1;
4574
4575 return_bad_req: /* let's centralize all bad requests */
4576 txn->req.msg_state = HTTP_MSG_ERROR;
4577 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004578 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004579
Willy Tarreau79ebac62010-06-07 13:47:49 +02004580 if (!(s->flags & SN_ERR_MASK))
4581 s->flags |= SN_ERR_PRXCOND;
4582 if (!(s->flags & SN_FINST_MASK))
4583 s->flags |= SN_FINST_R;
4584
Willy Tarreau522d6c02009-12-06 18:49:18 +01004585 return_err_msg:
4586 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004587 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004588 if (s->listener->counters)
4589 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004590 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004591}
4592
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004593/* send a server's name with an outgoing request over an established connection.
4594 * Note: this function is designed to be called once the request has been scheduled
4595 * for being forwarded. This is the reason why it rewinds the buffer before
4596 * proceeding.
4597 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004598int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004599
4600 struct hdr_ctx ctx;
4601
Mark Lamourinec2247f02012-01-04 13:02:01 -05004602 char *hdr_name = be->server_id_hdr_name;
4603 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004604 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004605 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004606 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004607
William Lallemandd9e90662012-01-30 17:27:17 +01004608 ctx.idx = 0;
4609
Willy Tarreau211cdec2014-04-17 20:18:08 +02004610 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004611 if (old_o) {
4612 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004613 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004614 txn->req.next += old_o;
4615 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004616 }
4617
Willy Tarreau9b28e032012-10-12 23:49:43 +02004618 old_i = chn->buf->i;
4619 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 -05004620 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004621 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004622 }
4623
4624 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004625 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004626 memcpy(hdr_val, hdr_name, hdr_name_len);
4627 hdr_val += hdr_name_len;
4628 *hdr_val++ = ':';
4629 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004630 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4631 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004632
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004633 if (old_o) {
4634 /* If this was a forwarded request, we must readjust the amount of
4635 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004636 * variations. Note that the current state is >= HTTP_MSG_BODY,
4637 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004638 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004639 old_o += chn->buf->i - old_i;
4640 b_adv(chn->buf, old_o);
4641 txn->req.next -= old_o;
4642 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004643 }
4644
Mark Lamourinec2247f02012-01-04 13:02:01 -05004645 return 0;
4646}
4647
Willy Tarreau610ecce2010-01-04 21:15:02 +01004648/* Terminate current transaction and prepare a new one. This is very tricky
4649 * right now but it works.
4650 */
4651void http_end_txn_clean_session(struct session *s)
4652{
Willy Tarreau068621e2013-12-23 15:11:25 +01004653 int prev_status = s->txn.status;
4654
Willy Tarreau610ecce2010-01-04 21:15:02 +01004655 /* FIXME: We need a more portable way of releasing a backend's and a
4656 * server's connections. We need a safer way to reinitialize buffer
4657 * flags. We also need a more accurate method for computing per-request
4658 * data.
4659 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660
Willy Tarreau4213a112013-12-15 10:25:42 +01004661 /* unless we're doing keep-alive, we want to quickly close the connection
4662 * to the server.
4663 */
4664 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4665 !si_conn_ready(s->req->cons)) {
4666 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4667 si_shutr(s->req->cons);
4668 si_shutw(s->req->cons);
4669 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004670
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004671 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004672 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004673 if (unlikely(s->srv_conn))
4674 sess_change_server(s, NULL);
4675 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676
4677 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4678 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004679 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004680
4681 if (s->txn.status) {
4682 int n;
4683
4684 n = s->txn.status / 100;
4685 if (n < 1 || n > 5)
4686 n = 0;
4687
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004688 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004689 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004690 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004691 s->fe->fe_counters.p.http.comp_rsp++;
4692 }
Willy Tarreau24657792010-02-26 10:30:28 +01004693 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004694 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004695 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004696 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004697 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004698 s->be->be_counters.p.http.comp_rsp++;
4699 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004700 }
4701
4702 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004703 s->logs.bytes_in -= s->req->buf->i;
4704 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004705
4706 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004707 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004708 !(s->flags & SN_MONITOR) &&
4709 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4710 s->do_log(s);
4711 }
4712
4713 s->logs.accept_date = date; /* user-visible date for logging */
4714 s->logs.tv_accept = now; /* corrected date for internal use */
4715 tv_zero(&s->logs.tv_request);
4716 s->logs.t_queue = -1;
4717 s->logs.t_connect = -1;
4718 s->logs.t_data = -1;
4719 s->logs.t_close = 0;
4720 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4721 s->logs.srv_queue_size = 0; /* we will get this number soon */
4722
Willy Tarreau9b28e032012-10-12 23:49:43 +02004723 s->logs.bytes_in = s->req->total = s->req->buf->i;
4724 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004725
4726 if (s->pend_pos)
4727 pendconn_free(s->pend_pos);
4728
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004729 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 if (s->flags & SN_CURR_SESS) {
4731 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004732 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004733 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004734 if (may_dequeue_tasks(objt_server(s->target), s->be))
4735 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004736 }
4737
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004738 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004739
Willy Tarreau4213a112013-12-15 10:25:42 +01004740 /* only release our endpoint if we don't intend to reuse the
4741 * connection.
4742 */
4743 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4744 !si_conn_ready(s->req->cons)) {
4745 si_release_endpoint(s->req->cons);
4746 }
4747
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004749 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004750 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004751 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004752 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004753 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4754 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 +02004755 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 +01004756 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004757
Willy Tarreau610ecce2010-01-04 21:15:02 +01004758 s->txn.meth = 0;
4759 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004760 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004761
4762 if (prev_status == 401 || prev_status == 407) {
4763 /* In HTTP keep-alive mode, if we receive a 401, we still have
4764 * a chance of being able to send the visitor again to the same
4765 * server over the same connection. This is required by some
4766 * broken protocols such as NTLM, and anyway whenever there is
4767 * an opportunity for sending the challenge to the proper place,
4768 * it's better to do it (at least it helps with debugging).
4769 */
4770 s->txn.flags |= TX_PREFER_LAST;
4771 }
4772
Willy Tarreauee55dc02010-06-01 10:56:34 +02004773 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004774 s->req->cons->flags |= SI_FL_INDEP_STR;
4775
Willy Tarreau96e31212011-05-30 18:10:30 +02004776 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004777 s->req->flags |= CF_NEVER_WAIT;
4778 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004779 }
4780
Willy Tarreau610ecce2010-01-04 21:15:02 +01004781 /* if the request buffer is not empty, it means we're
4782 * about to process another request, so send pending
4783 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004784 * Just don't do this if the buffer is close to be full,
4785 * because the request will wait for it to flush a little
4786 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004787 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004788 if (s->req->buf->i) {
4789 if (s->rep->buf->o &&
4790 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4791 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004792 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004793 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004794
4795 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004796 channel_auto_read(s->req);
4797 channel_auto_close(s->req);
4798 channel_auto_read(s->rep);
4799 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004800
Willy Tarreau27375622013-12-17 00:00:28 +01004801 /* we're in keep-alive with an idle connection, monitor it */
4802 si_idle_conn(s->req->cons);
4803
Willy Tarreau342b11c2010-11-24 16:22:09 +01004804 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004805 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004806}
4807
4808
4809/* This function updates the request state machine according to the response
4810 * state machine and buffer flags. It returns 1 if it changes anything (flag
4811 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4812 * it is only used to find when a request/response couple is complete. Both
4813 * this function and its equivalent should loop until both return zero. It
4814 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4815 */
4816int http_sync_req_state(struct session *s)
4817{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004818 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004819 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004820 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821 unsigned int old_state = txn->req.msg_state;
4822
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4824 return 0;
4825
4826 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004827 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004828 * We can shut the read side unless we want to abort_on_close,
4829 * or we have a POST request. The issue with POST requests is
4830 * that some browsers still send a CRLF after the request, and
4831 * this CRLF must be read so that it does not remain in the kernel
4832 * buffers, otherwise a close could cause an RST on some systems
4833 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004834 * Note that if we're using keep-alive on the client side, we'd
4835 * rather poll now and keep the polling enabled for the whole
4836 * session's life than enabling/disabling it between each
4837 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004838 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004839 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4840 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4841 !(s->be->options & PR_O_ABRT_CLOSE) &&
4842 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004843 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004844
Willy Tarreau40f151a2012-12-20 12:10:09 +01004845 /* if the server closes the connection, we want to immediately react
4846 * and close the socket to save packets and syscalls.
4847 */
4848 chn->cons->flags |= SI_FL_NOHALF;
4849
Willy Tarreau610ecce2010-01-04 21:15:02 +01004850 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4851 goto wait_other_side;
4852
4853 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4854 /* The server has not finished to respond, so we
4855 * don't want to move in order not to upset it.
4856 */
4857 goto wait_other_side;
4858 }
4859
4860 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4861 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004862 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004863 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004864 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004865 goto wait_other_side;
4866 }
4867
4868 /* When we get here, it means that both the request and the
4869 * response have finished receiving. Depending on the connection
4870 * mode, we'll have to wait for the last bytes to leave in either
4871 * direction, and sometimes for a close to be effective.
4872 */
4873
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004874 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4875 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004876 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4877 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004878 }
4879 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4880 /* Option forceclose is set, or either side wants to close,
4881 * let's enforce it now that we're not expecting any new
4882 * data to come. The caller knows the session is complete
4883 * once both states are CLOSED.
4884 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004885 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4886 channel_shutr_now(chn);
4887 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004888 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004889 }
4890 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004891 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4892 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004893 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004894 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4895 channel_auto_read(chn);
4896 txn->req.msg_state = HTTP_MSG_TUNNEL;
4897 chn->flags |= CF_NEVER_WAIT;
4898 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004899 }
4900
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004901 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004902 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004903 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004904
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004905 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004906 txn->req.msg_state = HTTP_MSG_CLOSING;
4907 goto http_msg_closing;
4908 }
4909 else {
4910 txn->req.msg_state = HTTP_MSG_CLOSED;
4911 goto http_msg_closed;
4912 }
4913 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004914 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004915 }
4916
4917 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4918 http_msg_closing:
4919 /* nothing else to forward, just waiting for the output buffer
4920 * to be empty and for the shutw_now to take effect.
4921 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004922 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004923 txn->req.msg_state = HTTP_MSG_CLOSED;
4924 goto http_msg_closed;
4925 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004926 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004927 txn->req.msg_state = HTTP_MSG_ERROR;
4928 goto wait_other_side;
4929 }
4930 }
4931
4932 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4933 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004934 /* see above in MSG_DONE why we only do this in these states */
4935 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4936 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4937 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004938 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939 goto wait_other_side;
4940 }
4941
4942 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004943 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004944}
4945
4946
4947/* This function updates the response state machine according to the request
4948 * state machine and buffer flags. It returns 1 if it changes anything (flag
4949 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4950 * it is only used to find when a request/response couple is complete. Both
4951 * this function and its equivalent should loop until both return zero. It
4952 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4953 */
4954int http_sync_res_state(struct session *s)
4955{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004956 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004957 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004958 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959 unsigned int old_state = txn->rsp.msg_state;
4960
Willy Tarreau610ecce2010-01-04 21:15:02 +01004961 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4962 return 0;
4963
4964 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4965 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004966 * still monitor the server connection for a possible close
4967 * while the request is being uploaded, so we don't disable
4968 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004969 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004970 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004971
4972 if (txn->req.msg_state == HTTP_MSG_ERROR)
4973 goto wait_other_side;
4974
4975 if (txn->req.msg_state < HTTP_MSG_DONE) {
4976 /* The client seems to still be sending data, probably
4977 * because we got an error response during an upload.
4978 * We have the choice of either breaking the connection
4979 * or letting it pass through. Let's do the later.
4980 */
4981 goto wait_other_side;
4982 }
4983
4984 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4985 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004986 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004987 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004988 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989 goto wait_other_side;
4990 }
4991
4992 /* When we get here, it means that both the request and the
4993 * response have finished receiving. Depending on the connection
4994 * mode, we'll have to wait for the last bytes to leave in either
4995 * direction, and sometimes for a close to be effective.
4996 */
4997
4998 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4999 /* Server-close mode : shut read and wait for the request
5000 * side to close its output buffer. The caller will detect
5001 * when we're in DONE and the other is in CLOSED and will
5002 * catch that for the final cleanup.
5003 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005004 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5005 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005006 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005007 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5008 /* Option forceclose is set, or either side wants to close,
5009 * let's enforce it now that we're not expecting any new
5010 * data to come. The caller knows the session is complete
5011 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005012 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005013 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5014 channel_shutr_now(chn);
5015 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005016 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005017 }
5018 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005019 /* The last possible modes are keep-alive and tunnel. Tunnel will
5020 * need to forward remaining data. Keep-alive will need to monitor
5021 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005023 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005024 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005025 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5026 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005027 }
5028
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005029 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005030 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005031 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005032 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5033 goto http_msg_closing;
5034 }
5035 else {
5036 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5037 goto http_msg_closed;
5038 }
5039 }
5040 goto wait_other_side;
5041 }
5042
5043 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5044 http_msg_closing:
5045 /* nothing else to forward, just waiting for the output buffer
5046 * to be empty and for the shutw_now to take effect.
5047 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005048 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005049 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5050 goto http_msg_closed;
5051 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005052 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005053 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005054 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005055 if (objt_server(s->target))
5056 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005057 goto wait_other_side;
5058 }
5059 }
5060
5061 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5062 http_msg_closed:
5063 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005064 bi_erase(chn);
5065 channel_auto_close(chn);
5066 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005067 goto wait_other_side;
5068 }
5069
5070 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005071 /* We force the response to leave immediately if we're waiting for the
5072 * other side, since there is no pending shutdown to push it out.
5073 */
5074 if (!channel_is_empty(chn))
5075 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005076 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005077}
5078
5079
5080/* Resync the request and response state machines. Return 1 if either state
5081 * changes.
5082 */
5083int http_resync_states(struct session *s)
5084{
5085 struct http_txn *txn = &s->txn;
5086 int old_req_state = txn->req.msg_state;
5087 int old_res_state = txn->rsp.msg_state;
5088
Willy Tarreau610ecce2010-01-04 21:15:02 +01005089 http_sync_req_state(s);
5090 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005091 if (!http_sync_res_state(s))
5092 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005093 if (!http_sync_req_state(s))
5094 break;
5095 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005096
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097 /* OK, both state machines agree on a compatible state.
5098 * There are a few cases we're interested in :
5099 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5100 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5101 * directions, so let's simply disable both analysers.
5102 * - HTTP_MSG_CLOSED on the response only means we must abort the
5103 * request.
5104 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5105 * with server-close mode means we've completed one request and we
5106 * must re-initialize the server connection.
5107 */
5108
5109 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5110 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5111 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5112 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5113 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005114 channel_auto_close(s->req);
5115 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005117 channel_auto_close(s->rep);
5118 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005119 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005120 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5121 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005122 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005123 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005124 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005125 channel_auto_close(s->rep);
5126 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005127 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005128 channel_abort(s->req);
5129 channel_auto_close(s->req);
5130 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005131 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005132 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005133 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5134 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005135 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005136 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5137 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5138 /* server-close/keep-alive: terminate this transaction,
5139 * possibly killing the server connection and reinitialize
5140 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 */
5142 http_end_txn_clean_session(s);
5143 }
5144
Willy Tarreau610ecce2010-01-04 21:15:02 +01005145 return txn->req.msg_state != old_req_state ||
5146 txn->rsp.msg_state != old_res_state;
5147}
5148
Willy Tarreaud98cf932009-12-27 22:54:55 +01005149/* This function is an analyser which forwards request body (including chunk
5150 * sizes if any). It is called as soon as we must forward, even if we forward
5151 * zero byte. The only situation where it must not be called is when we're in
5152 * tunnel mode and we want to forward till the close. It's used both to forward
5153 * remaining data and to resync after end of body. It expects the msg_state to
5154 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5155 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005156 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005157 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005158 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005159int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005160{
5161 struct http_txn *txn = &s->txn;
5162 struct http_msg *msg = &s->txn.req;
5163
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005164 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5165 return 0;
5166
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005167 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005168 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005169 /* Output closed while we were sending data. We must abort and
5170 * wake the other side up.
5171 */
5172 msg->msg_state = HTTP_MSG_ERROR;
5173 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005174 return 1;
5175 }
5176
Willy Tarreaud98cf932009-12-27 22:54:55 +01005177 /* Note that we don't have to send 100-continue back because we don't
5178 * need the data to complete our job, and it's up to the server to
5179 * decide whether to return 100, 417 or anything else in return of
5180 * an "Expect: 100-continue" header.
5181 */
5182
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005183 if (msg->sov) {
5184 /* we have msg->sov which points to the first byte of message
5185 * body, and req->buf.p still points to the beginning of the
5186 * message. We forward the headers now, as we don't need them
5187 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005188 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005189 b_adv(req->buf, msg->sov);
5190 msg->next -= msg->sov;
5191 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005192
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005193 /* The previous analysers guarantee that the state is somewhere
5194 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5195 * msg->next are always correct.
5196 */
5197 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5198 if (msg->flags & HTTP_MSGF_TE_CHNK)
5199 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5200 else
5201 msg->msg_state = HTTP_MSG_DATA;
5202 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005203 }
5204
Willy Tarreau7ba23542014-04-17 21:50:00 +02005205 /* Some post-connect processing might want us to refrain from starting to
5206 * forward data. Currently, the only reason for this is "balance url_param"
5207 * whichs need to parse/process the request after we've enabled forwarding.
5208 */
5209 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5210 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5211 channel_auto_connect(req);
5212 goto missing_data;
5213 }
5214 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5215 }
5216
Willy Tarreau80a92c02014-03-12 10:41:13 +01005217 /* in most states, we should abort in case of early close */
5218 channel_auto_close(req);
5219
Willy Tarreauefdf0942014-04-24 20:08:57 +02005220 if (req->to_forward) {
5221 /* We can't process the buffer's contents yet */
5222 req->flags |= CF_WAKE_WRITE;
5223 goto missing_data;
5224 }
5225
Willy Tarreaud98cf932009-12-27 22:54:55 +01005226 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005227 if (msg->msg_state == HTTP_MSG_DATA) {
5228 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005229 /* we may have some pending data starting at req->buf->p */
5230 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005231 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005232 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005233 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005234 msg->next += msg->chunk_len;
5235 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005236
5237 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005238 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005239 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005240 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005241 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005242 }
5243 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005244 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005245 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005246 * TRAILERS state.
5247 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005248 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005249
Willy Tarreau54d23df2012-10-25 19:04:45 +02005250 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005251 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005252 else if (ret < 0) {
5253 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005254 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005255 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005256 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005257 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005258 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005259 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005260 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005261 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005262 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005263
5264 if (ret == 0)
5265 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005266 else if (ret < 0) {
5267 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005268 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005269 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005270 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005271 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005272 /* we're in MSG_CHUNK_SIZE now */
5273 }
5274 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005275 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005276
5277 if (ret == 0)
5278 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005279 else if (ret < 0) {
5280 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005281 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005282 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005283 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005284 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005285 /* we're in HTTP_MSG_DONE now */
5286 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005287 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005288 int old_state = msg->msg_state;
5289
Willy Tarreau610ecce2010-01-04 21:15:02 +01005290 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005291
5292 /* we may have some pending data starting at req->buf->p
5293 * such as last chunk of data or trailers.
5294 */
5295 b_adv(req->buf, msg->next);
5296 msg->next = 0;
5297
Willy Tarreau4fe41902010-06-07 22:27:41 +02005298 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005299 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5300 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005301 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005302 if (http_resync_states(s)) {
5303 /* some state changes occurred, maybe the analyser
5304 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005305 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005306 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005307 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005308 /* request errors are most likely due to
5309 * the server aborting the transfer.
5310 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005311 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005312 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005313 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005314 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005316 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005318 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005319
5320 /* If "option abortonclose" is set on the backend, we
5321 * want to monitor the client's connection and forward
5322 * any shutdown notification to the server, which will
5323 * decide whether to close or to go on processing the
5324 * request.
5325 */
5326 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005327 channel_auto_read(req);
5328 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005329 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005330 else if (s->txn.meth == HTTP_METH_POST) {
5331 /* POST requests may require to read extra CRLF
5332 * sent by broken browsers and which could cause
5333 * an RST to be sent upon close on some systems
5334 * (eg: Linux).
5335 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005336 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005337 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005338
Willy Tarreau610ecce2010-01-04 21:15:02 +01005339 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005340 }
5341 }
5342
Willy Tarreaud98cf932009-12-27 22:54:55 +01005343 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005344 /* we may have some pending data starting at req->buf->p */
5345 b_adv(req->buf, msg->next);
5346 msg->next = 0;
5347 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5348
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005349 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005350 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005351 if (!(s->flags & SN_ERR_MASK))
5352 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005353 if (!(s->flags & SN_FINST_MASK)) {
5354 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5355 s->flags |= SN_FINST_H;
5356 else
5357 s->flags |= SN_FINST_D;
5358 }
5359
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005360 s->fe->fe_counters.cli_aborts++;
5361 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005362 if (objt_server(s->target))
5363 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005364
5365 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005366 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005367
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005368 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005369 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005370 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005371
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005372 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005373 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005374 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005375 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005376 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005377
Willy Tarreau5c620922011-05-11 19:56:11 +02005378 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005379 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005380 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005381 * modes are already handled by the stream sock layer. We must not do
5382 * this in content-length mode because it could present the MSG_MORE
5383 * flag with the last block of forwarded data, which would cause an
5384 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005385 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005386 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005387 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005388
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005389 return 0;
5390
Willy Tarreaud98cf932009-12-27 22:54:55 +01005391 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005392 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005393 if (s->listener->counters)
5394 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005395
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005396 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005397 /* we may have some pending data starting at req->buf->p */
5398 b_adv(req->buf, msg->next);
5399 msg->next = 0;
5400
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005401 txn->req.msg_state = HTTP_MSG_ERROR;
5402 if (txn->status) {
5403 /* Note: we don't send any error if some data were already sent */
5404 stream_int_retnclose(req->prod, NULL);
5405 } else {
5406 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005407 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005408 }
5409 req->analysers = 0;
5410 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411
5412 if (!(s->flags & SN_ERR_MASK))
5413 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005414 if (!(s->flags & SN_FINST_MASK)) {
5415 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5416 s->flags |= SN_FINST_H;
5417 else
5418 s->flags |= SN_FINST_D;
5419 }
5420 return 0;
5421
5422 aborted_xfer:
5423 txn->req.msg_state = HTTP_MSG_ERROR;
5424 if (txn->status) {
5425 /* Note: we don't send any error if some data were already sent */
5426 stream_int_retnclose(req->prod, NULL);
5427 } else {
5428 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005429 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005430 }
5431 req->analysers = 0;
5432 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5433
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005434 s->fe->fe_counters.srv_aborts++;
5435 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005436 if (objt_server(s->target))
5437 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005438
5439 if (!(s->flags & SN_ERR_MASK))
5440 s->flags |= SN_ERR_SRVCL;
5441 if (!(s->flags & SN_FINST_MASK)) {
5442 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5443 s->flags |= SN_FINST_H;
5444 else
5445 s->flags |= SN_FINST_D;
5446 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005447 return 0;
5448}
5449
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005450/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5451 * processing can continue on next analysers, or zero if it either needs more
5452 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5453 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5454 * when it has nothing left to do, and may remove any analyser when it wants to
5455 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005456 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005457int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005458{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005459 struct http_txn *txn = &s->txn;
5460 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005461 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005462 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005463 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005464 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005465
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005466 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 +02005467 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005468 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005469 rep,
5470 rep->rex, rep->wex,
5471 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005472 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005473 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005474
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005475 /*
5476 * Now parse the partial (or complete) lines.
5477 * We will check the response syntax, and also join multi-line
5478 * headers. An index of all the lines will be elaborated while
5479 * parsing.
5480 *
5481 * For the parsing, we use a 28 states FSM.
5482 *
5483 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005484 * rep->buf->p = beginning of response
5485 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5486 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005487 * msg->eol = end of current header or line (LF or CRLF)
5488 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005489 */
5490
Willy Tarreau628c40c2014-04-24 19:11:26 +02005491 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005492 /* There's a protected area at the end of the buffer for rewriting
5493 * purposes. We don't want to start to parse the request if the
5494 * protected area is affected, because we may have to move processed
5495 * data later, which is much more complicated.
5496 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005497 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005498 if (unlikely(!channel_reserved(rep))) {
5499 /* some data has still not left the buffer, wake us once that's done */
5500 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5501 goto abort_response;
5502 channel_dont_close(rep);
5503 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005504 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005505 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005506 }
5507
Willy Tarreau379357a2013-06-08 12:55:46 +02005508 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5509 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5510 buffer_slow_realign(rep->buf);
5511
Willy Tarreau9b28e032012-10-12 23:49:43 +02005512 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005513 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005514 }
5515
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005516 /* 1: we might have to print this header in debug mode */
5517 if (unlikely((global.mode & MODE_DEBUG) &&
5518 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005519 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005520 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005521
Willy Tarreau9b28e032012-10-12 23:49:43 +02005522 sol = rep->buf->p;
5523 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005524 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005525
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005526 sol += hdr_idx_first_pos(&txn->hdr_idx);
5527 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005528
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005529 while (cur_idx) {
5530 eol = sol + txn->hdr_idx.v[cur_idx].len;
5531 debug_hdr("srvhdr", s, sol, eol);
5532 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5533 cur_idx = txn->hdr_idx.v[cur_idx].next;
5534 }
5535 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005536
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005537 /*
5538 * Now we quickly check if we have found a full valid response.
5539 * If not so, we check the FD and buffer states before leaving.
5540 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005541 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005542 * responses are checked first.
5543 *
5544 * Depending on whether the client is still there or not, we
5545 * may send an error response back or not. Note that normally
5546 * we should only check for HTTP status there, and check I/O
5547 * errors somewhere else.
5548 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005549
Willy Tarreau655dce92009-11-08 13:10:58 +01005550 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005551 /* Invalid response */
5552 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5553 /* we detected a parsing error. We want to archive this response
5554 * in the dedicated proxy area for later troubleshooting.
5555 */
5556 hdr_response_bad:
5557 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005558 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005559
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005560 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005561 if (objt_server(s->target)) {
5562 objt_server(s->target)->counters.failed_resp++;
5563 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005564 }
Willy Tarreau64648412010-03-05 10:41:54 +01005565 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005566 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005567 rep->analysers = 0;
5568 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005569 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005570 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005571 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005572
5573 if (!(s->flags & SN_ERR_MASK))
5574 s->flags |= SN_ERR_PRXCOND;
5575 if (!(s->flags & SN_FINST_MASK))
5576 s->flags |= SN_FINST_H;
5577
5578 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005579 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005580
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005581 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005582 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005583 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005584 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005585 goto hdr_response_bad;
5586 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005587
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005588 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005589 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005590 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005591 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005592 else if (txn->flags & TX_NOT_FIRST)
5593 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005594
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005595 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005596 if (objt_server(s->target)) {
5597 objt_server(s->target)->counters.failed_resp++;
5598 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005599 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005600
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005601 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005602 rep->analysers = 0;
5603 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005604 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005605 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005606 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005607
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005608 if (!(s->flags & SN_ERR_MASK))
5609 s->flags |= SN_ERR_SRVCL;
5610 if (!(s->flags & SN_FINST_MASK))
5611 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005612 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005613 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005614
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005615 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005616 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005617 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005618 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005619 else if (txn->flags & TX_NOT_FIRST)
5620 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005621
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005622 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005623 if (objt_server(s->target)) {
5624 objt_server(s->target)->counters.failed_resp++;
5625 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005626 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005627
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005628 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005629 rep->analysers = 0;
5630 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005631 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005632 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005633 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005634
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005635 if (!(s->flags & SN_ERR_MASK))
5636 s->flags |= SN_ERR_SRVTO;
5637 if (!(s->flags & SN_FINST_MASK))
5638 s->flags |= SN_FINST_H;
5639 return 0;
5640 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005641
Willy Tarreauf003d372012-11-26 13:35:37 +01005642 /* client abort with an abortonclose */
5643 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5644 s->fe->fe_counters.cli_aborts++;
5645 s->be->be_counters.cli_aborts++;
5646 if (objt_server(s->target))
5647 objt_server(s->target)->counters.cli_aborts++;
5648
5649 rep->analysers = 0;
5650 channel_auto_close(rep);
5651
5652 txn->status = 400;
5653 bi_erase(rep);
5654 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5655
5656 if (!(s->flags & SN_ERR_MASK))
5657 s->flags |= SN_ERR_CLICL;
5658 if (!(s->flags & SN_FINST_MASK))
5659 s->flags |= SN_FINST_H;
5660
5661 /* process_session() will take care of the error */
5662 return 0;
5663 }
5664
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005665 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005666 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005667 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005668 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005669 else if (txn->flags & TX_NOT_FIRST)
5670 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005671
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005672 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005673 if (objt_server(s->target)) {
5674 objt_server(s->target)->counters.failed_resp++;
5675 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005676 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005677
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005678 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005679 rep->analysers = 0;
5680 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005681 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005682 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005683 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005684
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005685 if (!(s->flags & SN_ERR_MASK))
5686 s->flags |= SN_ERR_SRVCL;
5687 if (!(s->flags & SN_FINST_MASK))
5688 s->flags |= SN_FINST_H;
5689 return 0;
5690 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005691
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005692 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005693 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005694 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005695 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005696 else if (txn->flags & TX_NOT_FIRST)
5697 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005698
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005699 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005700 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005701 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005702
5703 if (!(s->flags & SN_ERR_MASK))
5704 s->flags |= SN_ERR_CLICL;
5705 if (!(s->flags & SN_FINST_MASK))
5706 s->flags |= SN_FINST_H;
5707
5708 /* process_session() will take care of the error */
5709 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005710 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005711
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005712 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005713 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005714 return 0;
5715 }
5716
5717 /* More interesting part now : we know that we have a complete
5718 * response which at least looks like HTTP. We have an indicator
5719 * of each header's length, so we can parse them quickly.
5720 */
5721
5722 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005723 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005724
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005725 /*
5726 * 1: get the status code
5727 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005728 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005729 if (n < 1 || n > 5)
5730 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005731 /* when the client triggers a 4xx from the server, it's most often due
5732 * to a missing object or permission. These events should be tracked
5733 * because if they happen often, it may indicate a brute force or a
5734 * vulnerability scan.
5735 */
5736 if (n == 4)
5737 session_inc_http_err_ctr(s);
5738
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005739 if (objt_server(s->target))
5740 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005741
Willy Tarreau5b154472009-12-21 20:11:07 +01005742 /* check if the response is HTTP/1.1 or above */
5743 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005744 ((rep->buf->p[5] > '1') ||
5745 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005746 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005747
5748 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005749 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 +01005750
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005751 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005752 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005753
Willy Tarreau9b28e032012-10-12 23:49:43 +02005754 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005755
Willy Tarreau39650402010-03-15 19:44:39 +01005756 /* Adjust server's health based on status code. Note: status codes 501
5757 * and 505 are triggered on demand by client request, so we must not
5758 * count them as server failures.
5759 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005760 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005761 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005762 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005763 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005764 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005765 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005766
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005767 /*
5768 * 2: check for cacheability.
5769 */
5770
5771 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005772 case 100:
5773 /*
5774 * We may be facing a 100-continue response, in which case this
5775 * is not the right response, and we're waiting for the next one.
5776 * Let's allow this response to go to the client and wait for the
5777 * next one.
5778 */
5779 hdr_idx_init(&txn->hdr_idx);
5780 msg->next -= channel_forward(rep, msg->next);
5781 msg->msg_state = HTTP_MSG_RPBEFORE;
5782 txn->status = 0;
5783 s->logs.t_data = -1; /* was not a response yet */
5784 goto next_one;
5785
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005786 case 200:
5787 case 203:
5788 case 206:
5789 case 300:
5790 case 301:
5791 case 410:
5792 /* RFC2616 @13.4:
5793 * "A response received with a status code of
5794 * 200, 203, 206, 300, 301 or 410 MAY be stored
5795 * by a cache (...) unless a cache-control
5796 * directive prohibits caching."
5797 *
5798 * RFC2616 @9.5: POST method :
5799 * "Responses to this method are not cacheable,
5800 * unless the response includes appropriate
5801 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005802 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005803 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005804 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005805 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5806 break;
5807 default:
5808 break;
5809 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005810
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005811 /*
5812 * 3: we may need to capture headers
5813 */
5814 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005815 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005816 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005817 txn->rsp.cap, s->fe->rsp_cap);
5818
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005819 /* 4: determine the transfer-length.
5820 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5821 * the presence of a message-body in a RESPONSE and its transfer length
5822 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005823 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005824 * All responses to the HEAD request method MUST NOT include a
5825 * message-body, even though the presence of entity-header fields
5826 * might lead one to believe they do. All 1xx (informational), 204
5827 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5828 * message-body. All other responses do include a message-body,
5829 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005830 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005831 * 1. Any response which "MUST NOT" include a message-body (such as the
5832 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5833 * always terminated by the first empty line after the header fields,
5834 * regardless of the entity-header fields present in the message.
5835 *
5836 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5837 * the "chunked" transfer-coding (Section 6.2) is used, the
5838 * transfer-length is defined by the use of this transfer-coding.
5839 * If a Transfer-Encoding header field is present and the "chunked"
5840 * transfer-coding is not present, the transfer-length is defined by
5841 * the sender closing the connection.
5842 *
5843 * 3. If a Content-Length header field is present, its decimal value in
5844 * OCTETs represents both the entity-length and the transfer-length.
5845 * If a message is received with both a Transfer-Encoding header
5846 * field and a Content-Length header field, the latter MUST be ignored.
5847 *
5848 * 4. If the message uses the media type "multipart/byteranges", and
5849 * the transfer-length is not otherwise specified, then this self-
5850 * delimiting media type defines the transfer-length. This media
5851 * type MUST NOT be used unless the sender knows that the recipient
5852 * can parse it; the presence in a request of a Range header with
5853 * multiple byte-range specifiers from a 1.1 client implies that the
5854 * client can parse multipart/byteranges responses.
5855 *
5856 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005857 */
5858
5859 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005860 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005861 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005862 * FIXME: should we parse anyway and return an error on chunked encoding ?
5863 */
5864 if (txn->meth == HTTP_METH_HEAD ||
5865 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005866 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005867 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005868 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005869 goto skip_content_length;
5870 }
5871
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005872 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005873 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005874 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005875 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005876 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005877 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5878 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005879 /* bad transfer-encoding (chunked followed by something else) */
5880 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005881 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005882 break;
5883 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005884 }
5885
5886 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5887 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005888 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005889 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005890 signed long long cl;
5891
Willy Tarreauad14f752011-09-02 20:33:27 +02005892 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005893 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005894 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005895 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005896
Willy Tarreauad14f752011-09-02 20:33:27 +02005897 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005898 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005899 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005900 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005901
Willy Tarreauad14f752011-09-02 20:33:27 +02005902 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005903 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005904 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005905 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005906
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005907 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005908 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005909 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005910 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005911
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005912 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005913 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005914 }
5915
William Lallemand82fe75c2012-10-23 10:25:10 +02005916 if (s->fe->comp || s->be->comp)
5917 select_compression_response_header(s, rep->buf);
5918
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005919skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005920 /* Now we have to check if we need to modify the Connection header.
5921 * This is more difficult on the response than it is on the request,
5922 * because we can have two different HTTP versions and we don't know
5923 * how the client will interprete a response. For instance, let's say
5924 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5925 * HTTP/1.1 response without any header. Maybe it will bound itself to
5926 * HTTP/1.0 because it only knows about it, and will consider the lack
5927 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5928 * the lack of header as a keep-alive. Thus we will use two flags
5929 * indicating how a request MAY be understood by the client. In case
5930 * of multiple possibilities, we'll fix the header to be explicit. If
5931 * ambiguous cases such as both close and keepalive are seen, then we
5932 * will fall back to explicit close. Note that we won't take risks with
5933 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005934 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005935 */
5936
Willy Tarreaudc008c52010-02-01 16:20:08 +01005937 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5938 txn->status == 101)) {
5939 /* Either we've established an explicit tunnel, or we're
5940 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005941 * to understand the next protocols. We have to switch to tunnel
5942 * mode, so that we transfer the request and responses then let
5943 * this protocol pass unmodified. When we later implement specific
5944 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005945 * header which contains information about that protocol for
5946 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005947 */
5948 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5949 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005950 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5951 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005952 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5953 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005954 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005955
Willy Tarreau70dffda2014-01-30 03:07:23 +01005956 /* this situation happens when combining pretend-keepalive with httpclose. */
5957 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005958 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5959 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005960 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5961
Willy Tarreau60466522010-01-18 19:08:45 +01005962 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005963 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005964 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5965 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005966
Willy Tarreau60466522010-01-18 19:08:45 +01005967 /* now adjust header transformations depending on current state */
5968 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5969 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5970 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005971 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005972 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005973 }
Willy Tarreau60466522010-01-18 19:08:45 +01005974 else { /* SCL / KAL */
5975 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005976 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005977 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005978 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005979
Willy Tarreau60466522010-01-18 19:08:45 +01005980 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005981 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005982
Willy Tarreau60466522010-01-18 19:08:45 +01005983 /* Some keep-alive responses are converted to Server-close if
5984 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005985 */
Willy Tarreau60466522010-01-18 19:08:45 +01005986 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5987 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005988 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005989 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005990 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005991 }
5992
Willy Tarreau7959a552013-09-23 16:44:27 +02005993 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005994 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005995
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005996 /* end of job, return OK */
5997 rep->analysers &= ~an_bit;
5998 rep->analyse_exp = TICK_ETERNITY;
5999 channel_auto_close(rep);
6000 return 1;
6001
6002 abort_keep_alive:
6003 /* A keep-alive request to the server failed on a network error.
6004 * The client is required to retry. We need to close without returning
6005 * any other information so that the client retries.
6006 */
6007 txn->status = 0;
6008 rep->analysers = 0;
6009 s->req->analysers = 0;
6010 channel_auto_close(rep);
6011 s->logs.logwait = 0;
6012 s->logs.level = 0;
6013 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6014 bi_erase(rep);
6015 stream_int_retnclose(rep->cons, NULL);
6016 return 0;
6017}
6018
6019/* This function performs all the processing enabled for the current response.
6020 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6021 * and updates s->rep->analysers. It might make sense to explode it into several
6022 * other functions. It works like process_request (see indications above).
6023 */
6024int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6025{
6026 struct http_txn *txn = &s->txn;
6027 struct http_msg *msg = &txn->rsp;
6028 struct proxy *cur_proxy;
6029 struct cond_wordlist *wl;
6030 struct http_res_rule *http_res_last_rule = NULL;
6031
6032 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6033 now_ms, __FUNCTION__,
6034 s,
6035 rep,
6036 rep->rex, rep->wex,
6037 rep->flags,
6038 rep->buf->i,
6039 rep->analysers);
6040
6041 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6042 return 0;
6043
6044 rep->analysers &= ~an_bit;
6045 rep->analyse_exp = TICK_ETERNITY;
6046
Willy Tarreau70730dd2014-04-24 18:06:27 +02006047 /* The stats applet needs to adjust the Connection header but we don't
6048 * apply any filter there.
6049 */
6050 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6051 goto skip_filters;
6052
Willy Tarreau58975672014-04-24 21:13:57 +02006053 /*
6054 * We will have to evaluate the filters.
6055 * As opposed to version 1.2, now they will be evaluated in the
6056 * filters order and not in the header order. This means that
6057 * each filter has to be validated among all headers.
6058 *
6059 * Filters are tried with ->be first, then with ->fe if it is
6060 * different from ->be.
6061 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006062
Willy Tarreau58975672014-04-24 21:13:57 +02006063 cur_proxy = s->be;
6064 while (1) {
6065 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006066
Willy Tarreau58975672014-04-24 21:13:57 +02006067 /* evaluate http-response rules */
6068 if (!http_res_last_rule)
6069 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 +02006070
Willy Tarreau58975672014-04-24 21:13:57 +02006071 /* try headers filters */
6072 if (rule_set->rsp_exp != NULL) {
6073 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6074 return_bad_resp:
6075 if (objt_server(s->target)) {
6076 objt_server(s->target)->counters.failed_resp++;
6077 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006078 }
Willy Tarreau58975672014-04-24 21:13:57 +02006079 s->be->be_counters.failed_resp++;
6080 return_srv_prx_502:
6081 rep->analysers = 0;
6082 txn->status = 502;
6083 s->logs.t_data = -1; /* was not a valid response */
6084 rep->prod->flags |= SI_FL_NOLINGER;
6085 bi_erase(rep);
6086 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6087 if (!(s->flags & SN_ERR_MASK))
6088 s->flags |= SN_ERR_PRXCOND;
6089 if (!(s->flags & SN_FINST_MASK))
6090 s->flags |= SN_FINST_H;
6091 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006092 }
Willy Tarreau58975672014-04-24 21:13:57 +02006093 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006094
Willy Tarreau58975672014-04-24 21:13:57 +02006095 /* has the response been denied ? */
6096 if (txn->flags & TX_SVDENY) {
6097 if (objt_server(s->target))
6098 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006099
Willy Tarreau58975672014-04-24 21:13:57 +02006100 s->be->be_counters.denied_resp++;
6101 s->fe->fe_counters.denied_resp++;
6102 if (s->listener->counters)
6103 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006104
Willy Tarreau58975672014-04-24 21:13:57 +02006105 goto return_srv_prx_502;
6106 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006107
Willy Tarreau58975672014-04-24 21:13:57 +02006108 /* add response headers from the rule sets in the same order */
6109 list_for_each_entry(wl, &rule_set->rsp_add, list) {
6110 if (txn->status < 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006111 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006112 if (wl->cond) {
6113 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6114 ret = acl_pass(ret);
6115 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6116 ret = !ret;
6117 if (!ret)
6118 continue;
6119 }
6120 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6121 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006122 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006123
Willy Tarreau58975672014-04-24 21:13:57 +02006124 /* check whether we're already working on the frontend */
6125 if (cur_proxy == s->fe)
6126 break;
6127 cur_proxy = s->fe;
6128 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006129
Willy Tarreau58975672014-04-24 21:13:57 +02006130 /* OK that's all we can do for 1xx responses */
6131 if (unlikely(txn->status < 200))
6132 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006133
Willy Tarreau58975672014-04-24 21:13:57 +02006134 /*
6135 * Now check for a server cookie.
6136 */
6137 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6138 (s->be->options & PR_O_CHK_CACHE))
6139 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006140
Willy Tarreau58975672014-04-24 21:13:57 +02006141 /*
6142 * Check for cache-control or pragma headers if required.
6143 */
6144 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
6145 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006146
Willy Tarreau58975672014-04-24 21:13:57 +02006147 /*
6148 * Add server cookie in the response if needed
6149 */
6150 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6151 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6152 (!(s->flags & SN_DIRECT) ||
6153 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6154 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6155 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6156 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6157 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6158 !(s->flags & SN_IGNORE_PRST)) {
6159 /* the server is known, it's not the one the client requested, or the
6160 * cookie's last seen date needs to be refreshed. We have to
6161 * insert a set-cookie here, except if we want to insert only on POST
6162 * requests and this one isn't. Note that servers which don't have cookies
6163 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006164 */
Willy Tarreau58975672014-04-24 21:13:57 +02006165 if (!objt_server(s->target)->cookie) {
6166 chunk_printf(&trash,
6167 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6168 s->be->cookie_name);
6169 }
6170 else {
6171 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006172
Willy Tarreau58975672014-04-24 21:13:57 +02006173 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6174 /* emit last_date, which is mandatory */
6175 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6176 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6177 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006178
Willy Tarreau58975672014-04-24 21:13:57 +02006179 if (s->be->cookie_maxlife) {
6180 /* emit first_date, which is either the original one or
6181 * the current date.
6182 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006183 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006184 s30tob64(txn->cookie_first_date ?
6185 txn->cookie_first_date >> 2 :
6186 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006187 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006188 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006189 }
Willy Tarreau58975672014-04-24 21:13:57 +02006190 chunk_appendf(&trash, "; path=/");
6191 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006192
Willy Tarreau58975672014-04-24 21:13:57 +02006193 if (s->be->cookie_domain)
6194 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006195
Willy Tarreau58975672014-04-24 21:13:57 +02006196 if (s->be->ck_opts & PR_CK_HTTPONLY)
6197 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006198
Willy Tarreau58975672014-04-24 21:13:57 +02006199 if (s->be->ck_opts & PR_CK_SECURE)
6200 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006201
Willy Tarreau58975672014-04-24 21:13:57 +02006202 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6203 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006204
Willy Tarreau58975672014-04-24 21:13:57 +02006205 txn->flags &= ~TX_SCK_MASK;
6206 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6207 /* the server did not change, only the date was updated */
6208 txn->flags |= TX_SCK_UPDATED;
6209 else
6210 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006211
Willy Tarreau58975672014-04-24 21:13:57 +02006212 /* Here, we will tell an eventual cache on the client side that we don't
6213 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6214 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6215 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006216 */
Willy Tarreau58975672014-04-24 21:13:57 +02006217 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006218
Willy Tarreau58975672014-04-24 21:13:57 +02006219 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006220
Willy Tarreau58975672014-04-24 21:13:57 +02006221 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6222 "Cache-control: private", 22) < 0))
6223 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006224 }
Willy Tarreau58975672014-04-24 21:13:57 +02006225 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006226
Willy Tarreau58975672014-04-24 21:13:57 +02006227 /*
6228 * Check if result will be cacheable with a cookie.
6229 * We'll block the response if security checks have caught
6230 * nasty things such as a cacheable cookie.
6231 */
6232 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6233 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6234 (s->be->options & PR_O_CHK_CACHE)) {
6235 /* we're in presence of a cacheable response containing
6236 * a set-cookie header. We'll block it as requested by
6237 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006238 */
Willy Tarreau58975672014-04-24 21:13:57 +02006239 if (objt_server(s->target))
6240 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006241
Willy Tarreau58975672014-04-24 21:13:57 +02006242 s->be->be_counters.denied_resp++;
6243 s->fe->fe_counters.denied_resp++;
6244 if (s->listener->counters)
6245 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006246
Willy Tarreau58975672014-04-24 21:13:57 +02006247 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6248 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6249 send_log(s->be, LOG_ALERT,
6250 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6251 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6252 goto return_srv_prx_502;
6253 }
Willy Tarreau03945942009-12-22 16:50:27 +01006254
Willy Tarreau70730dd2014-04-24 18:06:27 +02006255 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006256 /*
6257 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6258 * If an "Upgrade" token is found, the header is left untouched in order
6259 * not to have to deal with some client bugs : some of them fail an upgrade
6260 * if anything but "Upgrade" is present in the Connection header.
6261 */
6262 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6263 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6264 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6265 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6266 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006267
Willy Tarreau58975672014-04-24 21:13:57 +02006268 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6269 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6270 /* we want a keep-alive response here. Keep-alive header
6271 * required if either side is not 1.1.
6272 */
6273 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6274 want_flags |= TX_CON_KAL_SET;
6275 }
6276 else {
6277 /* we want a close response here. Close header required if
6278 * the server is 1.1, regardless of the client.
6279 */
6280 if (msg->flags & HTTP_MSGF_VER_11)
6281 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006282 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006283
Willy Tarreau58975672014-04-24 21:13:57 +02006284 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6285 http_change_connection_header(txn, msg, want_flags);
6286 }
6287
6288 skip_header_mangling:
6289 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6290 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6291 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006292
Willy Tarreau58975672014-04-24 21:13:57 +02006293 /* if the user wants to log as soon as possible, without counting
6294 * bytes from the server, then this is the right moment. We have
6295 * to temporarily assign bytes_out to log what we currently have.
6296 */
6297 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6298 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6299 s->logs.bytes_out = txn->rsp.eoh;
6300 s->do_log(s);
6301 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006302 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006303 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006304}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006305
Willy Tarreaud98cf932009-12-27 22:54:55 +01006306/* This function is an analyser which forwards response body (including chunk
6307 * sizes if any). It is called as soon as we must forward, even if we forward
6308 * zero byte. The only situation where it must not be called is when we're in
6309 * tunnel mode and we want to forward till the close. It's used both to forward
6310 * remaining data and to resync after end of body. It expects the msg_state to
6311 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6312 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006313 *
6314 * It is capable of compressing response data both in content-length mode and
6315 * in chunked mode. The state machines follows different flows depending on
6316 * whether content-length and chunked modes are used, since there are no
6317 * trailers in content-length :
6318 *
6319 * chk-mode cl-mode
6320 * ,----- BODY -----.
6321 * / \
6322 * V size > 0 V chk-mode
6323 * .--> SIZE -------------> DATA -------------> CRLF
6324 * | | size == 0 | last byte |
6325 * | v final crlf v inspected |
6326 * | TRAILERS -----------> DONE |
6327 * | |
6328 * `----------------------------------------------'
6329 *
6330 * Compression only happens in the DATA state, and must be flushed in final
6331 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6332 * is performed at once on final states for all bytes parsed, or when leaving
6333 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006334 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006335int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006336{
6337 struct http_txn *txn = &s->txn;
6338 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006339 static struct buffer *tmpbuf = NULL;
6340 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006341 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006342
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006343 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6344 return 0;
6345
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006346 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006347 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006348 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006349 /* Output closed while we were sending data. We must abort and
6350 * wake the other side up.
6351 */
6352 msg->msg_state = HTTP_MSG_ERROR;
6353 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006354 return 1;
6355 }
6356
Willy Tarreau4fe41902010-06-07 22:27:41 +02006357 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006358 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006359
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006360 if (msg->sov) {
6361 /* we have msg->sov which points to the first byte of message
6362 * body, and res->buf.p still points to the beginning of the
6363 * message. We forward the headers now, as we don't need them
6364 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006365 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006366 b_adv(res->buf, msg->sov);
6367 msg->next -= msg->sov;
6368 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006369
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006370 /* The previous analysers guarantee that the state is somewhere
6371 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6372 * msg->next are always correct.
6373 */
6374 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6375 if (msg->flags & HTTP_MSGF_TE_CHNK)
6376 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6377 else
6378 msg->msg_state = HTTP_MSG_DATA;
6379 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006380 }
6381
Willy Tarreauefdf0942014-04-24 20:08:57 +02006382 if (res->to_forward) {
6383 /* We can't process the buffer's contents yet */
6384 res->flags |= CF_WAKE_WRITE;
6385 goto missing_data;
6386 }
6387
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006388 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6389 /* We need a compression buffer in the DATA state to put the
6390 * output of compressed data, and in CRLF state to let the
6391 * TRAILERS state finish the job of removing the trailing CRLF.
6392 */
6393 if (unlikely(tmpbuf == NULL)) {
6394 /* this is the first time we need the compression buffer */
6395 tmpbuf = pool_alloc2(pool2_buffer);
6396 if (tmpbuf == NULL)
6397 goto aborted_xfer; /* no memory */
6398 }
6399
6400 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006401 if (ret < 0) {
6402 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006403 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006404 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006405 compressing = 1;
6406 }
6407
Willy Tarreaud98cf932009-12-27 22:54:55 +01006408 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006409 switch (msg->msg_state - HTTP_MSG_DATA) {
6410 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006411 /* we may have some pending data starting at res->buf->p */
6412 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006413 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006414 if (ret < 0)
6415 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006416
Willy Tarreaud5a67832014-04-21 10:54:27 +02006417 if (msg->chunk_len) {
6418 /* input empty or output full */
6419 if (res->buf->i > msg->next)
6420 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006421 goto missing_data;
6422 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006423 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006424 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006425 if (msg->chunk_len > res->buf->i - msg->next) {
6426 /* output full */
6427 res->flags |= CF_WAKE_WRITE;
6428 goto missing_data;
6429 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006430 msg->next += msg->chunk_len;
6431 msg->chunk_len = 0;
6432 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006433
6434 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006435 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006436 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006437 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006438 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006439 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006440 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006441 /* fall through for HTTP_MSG_CHUNK_CRLF */
6442
6443 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6444 /* we want the CRLF after the data */
6445
6446 ret = http_skip_chunk_crlf(msg);
6447 if (ret == 0)
6448 goto missing_data;
6449 else if (ret < 0) {
6450 if (msg->err_pos >= 0)
6451 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6452 goto return_bad_res;
6453 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006454 /* we're in MSG_CHUNK_SIZE now, fall through */
6455
6456 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006457 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006458 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006459 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006460 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006461
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006462 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006463 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006464 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006465 else if (ret < 0) {
6466 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006467 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006468 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006469 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006470 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006471 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006472
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006473 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006474 if (unlikely(compressing)) {
6475 /* we need to flush output contents before syncing FSMs */
6476 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6477 compressing = 0;
6478 }
6479
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006480 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006481 if (ret == 0)
6482 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006483 else if (ret < 0) {
6484 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006485 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006486 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006487 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006488 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006489
6490 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006491 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006492 if (unlikely(compressing)) {
6493 /* we need to flush output contents before syncing FSMs */
6494 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6495 compressing = 0;
6496 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006497
Willy Tarreauc623c172014-04-18 09:53:50 +02006498 /* we may have some pending data starting at res->buf->p
6499 * such as a last chunk of data or trailers.
6500 */
6501 b_adv(res->buf, msg->next);
6502 msg->next = 0;
6503
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006504 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006505 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006506 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6507 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006508 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006509
Willy Tarreau610ecce2010-01-04 21:15:02 +01006510 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006511 /* some state changes occurred, maybe the analyser
6512 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006513 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006514 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006515 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006516 /* response errors are most likely due to
6517 * the client aborting the transfer.
6518 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006519 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006520 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006521 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006522 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006523 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006524 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006525 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006526 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006527 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006528 }
6529 }
6530
Willy Tarreaud98cf932009-12-27 22:54:55 +01006531 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006532 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006533 if (unlikely(compressing)) {
6534 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006535 compressing = 0;
6536 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006537
Willy Tarreauc623c172014-04-18 09:53:50 +02006538 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6539 b_adv(res->buf, msg->next);
6540 msg->next = 0;
6541 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6542 }
6543
Willy Tarreauf003d372012-11-26 13:35:37 +01006544 if (res->flags & CF_SHUTW)
6545 goto aborted_xfer;
6546
6547 /* stop waiting for data if the input is closed before the end. If the
6548 * client side was already closed, it means that the client has aborted,
6549 * so we don't want to count this as a server abort. Otherwise it's a
6550 * server abort.
6551 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006552 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006553 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006554 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006555 if (!(s->flags & SN_ERR_MASK))
6556 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006557 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006558 if (objt_server(s->target))
6559 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006560 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006561 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006562
Willy Tarreau40dba092010-03-04 18:14:51 +01006563 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006564 if (!s->req->analysers)
6565 goto return_bad_res;
6566
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006567 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006568 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006569 * Similarly, with keep-alive on the client side, we don't want to forward a
6570 * close.
6571 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006572 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006573 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6574 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006575 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006576
Willy Tarreau5c620922011-05-11 19:56:11 +02006577 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006578 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006579 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006580 * modes are already handled by the stream sock layer. We must not do
6581 * this in content-length mode because it could present the MSG_MORE
6582 * flag with the last block of forwarded data, which would cause an
6583 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006584 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006585 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006586 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006587
Willy Tarreaud98cf932009-12-27 22:54:55 +01006588 /* the session handler will take care of timeouts and errors */
6589 return 0;
6590
Willy Tarreau40dba092010-03-04 18:14:51 +01006591 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006592 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006593 if (objt_server(s->target))
6594 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006595
6596 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006597 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006598 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006599 compressing = 0;
6600 }
6601
Willy Tarreauc623c172014-04-18 09:53:50 +02006602 /* we may have some pending data starting at res->buf->p */
6603 if (s->comp_algo == NULL) {
6604 b_adv(res->buf, msg->next);
6605 msg->next = 0;
6606 }
6607
Willy Tarreaud98cf932009-12-27 22:54:55 +01006608 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006609 /* don't send any error message as we're in the body */
6610 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006611 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006612 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006613 if (objt_server(s->target))
6614 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006615
6616 if (!(s->flags & SN_ERR_MASK))
6617 s->flags |= SN_ERR_PRXCOND;
6618 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006619 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006620 return 0;
6621
6622 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006623 if (unlikely(compressing)) {
6624 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6625 compressing = 0;
6626 }
6627
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006628 txn->rsp.msg_state = HTTP_MSG_ERROR;
6629 /* don't send any error message as we're in the body */
6630 stream_int_retnclose(res->cons, NULL);
6631 res->analysers = 0;
6632 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6633
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006634 s->fe->fe_counters.cli_aborts++;
6635 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006636 if (objt_server(s->target))
6637 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006638
6639 if (!(s->flags & SN_ERR_MASK))
6640 s->flags |= SN_ERR_CLICL;
6641 if (!(s->flags & SN_FINST_MASK))
6642 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006643 return 0;
6644}
6645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646/* Iterate the same filter through all request headers.
6647 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648 * Since it can manage the switch to another backend, it updates the per-proxy
6649 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006650 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006651int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006652{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006653 char term;
6654 char *cur_ptr, *cur_end, *cur_next;
6655 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006656 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006657 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006658 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006660 last_hdr = 0;
6661
Willy Tarreau9b28e032012-10-12 23:49:43 +02006662 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006663 old_idx = 0;
6664
6665 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006666 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006667 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006668 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006669 (exp->action == ACT_ALLOW ||
6670 exp->action == ACT_DENY ||
6671 exp->action == ACT_TARPIT))
6672 return 0;
6673
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006674 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006675 if (!cur_idx)
6676 break;
6677
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006678 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006679 cur_ptr = cur_next;
6680 cur_end = cur_ptr + cur_hdr->len;
6681 cur_next = cur_end + cur_hdr->cr + 1;
6682
6683 /* Now we have one header between cur_ptr and cur_end,
6684 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006685 */
6686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006687 /* The annoying part is that pattern matching needs
6688 * that we modify the contents to null-terminate all
6689 * strings before testing them.
6690 */
6691
6692 term = *cur_end;
6693 *cur_end = '\0';
6694
6695 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6696 switch (exp->action) {
6697 case ACT_SETBE:
6698 /* It is not possible to jump a second time.
6699 * FIXME: should we return an HTTP/500 here so that
6700 * the admin knows there's a problem ?
6701 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006702 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006703 break;
6704
6705 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006706 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006707 last_hdr = 1;
6708 break;
6709
6710 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006711 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006712 last_hdr = 1;
6713 break;
6714
6715 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006716 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006717 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006718 break;
6719
6720 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006721 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006722 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006723 break;
6724
6725 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006726 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6727 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006728 /* FIXME: if the user adds a newline in the replacement, the
6729 * index will not be recalculated for now, and the new line
6730 * will not be counted as a new header.
6731 */
6732
6733 cur_end += delta;
6734 cur_next += delta;
6735 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006736 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006737 break;
6738
6739 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006740 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006741 cur_next += delta;
6742
Willy Tarreaufa355d42009-11-29 18:12:29 +01006743 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006744 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6745 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006746 cur_hdr->len = 0;
6747 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006748 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006749 break;
6750
6751 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006753 if (cur_end)
6754 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006756 /* keep the link from this header to next one in case of later
6757 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006759 old_idx = cur_idx;
6760 }
6761 return 0;
6762}
6763
6764
6765/* Apply the filter to the request line.
6766 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6767 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006768 * Since it can manage the switch to another backend, it updates the per-proxy
6769 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006770 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006771int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006772{
6773 char term;
6774 char *cur_ptr, *cur_end;
6775 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006776 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006777 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006778
Willy Tarreau3d300592007-03-18 18:34:41 +01006779 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006780 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006781 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006782 (exp->action == ACT_ALLOW ||
6783 exp->action == ACT_DENY ||
6784 exp->action == ACT_TARPIT))
6785 return 0;
6786 else if (exp->action == ACT_REMOVE)
6787 return 0;
6788
6789 done = 0;
6790
Willy Tarreau9b28e032012-10-12 23:49:43 +02006791 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006792 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006793
6794 /* Now we have the request line between cur_ptr and cur_end */
6795
6796 /* The annoying part is that pattern matching needs
6797 * that we modify the contents to null-terminate all
6798 * strings before testing them.
6799 */
6800
6801 term = *cur_end;
6802 *cur_end = '\0';
6803
6804 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6805 switch (exp->action) {
6806 case ACT_SETBE:
6807 /* It is not possible to jump a second time.
6808 * FIXME: should we return an HTTP/500 here so that
6809 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006810 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006811 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006812 break;
6813
6814 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006815 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006816 done = 1;
6817 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006819 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006820 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006821 done = 1;
6822 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006824 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006825 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006826 done = 1;
6827 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006828
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006829 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006830 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006831 done = 1;
6832 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006833
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006834 case ACT_REPLACE:
6835 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006836 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6837 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006838 /* FIXME: if the user adds a newline in the replacement, the
6839 * index will not be recalculated for now, and the new line
6840 * will not be counted as a new header.
6841 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006842
Willy Tarreaufa355d42009-11-29 18:12:29 +01006843 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006844 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006845 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006846 HTTP_MSG_RQMETH,
6847 cur_ptr, cur_end + 1,
6848 NULL, NULL);
6849 if (unlikely(!cur_end))
6850 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006851
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006852 /* we have a full request and we know that we have either a CR
6853 * or an LF at <ptr>.
6854 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006855 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6856 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006857 /* there is no point trying this regex on headers */
6858 return 1;
6859 }
6860 }
6861 *cur_end = term; /* restore the string terminator */
6862 return done;
6863}
Willy Tarreau97de6242006-12-27 17:18:38 +01006864
Willy Tarreau58f10d72006-12-04 02:26:12 +01006865
Willy Tarreau58f10d72006-12-04 02:26:12 +01006866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006867/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006868 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006869 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006870 * unparsable request. Since it can manage the switch to another backend, it
6871 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006872 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006873int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006874{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006875 struct http_txn *txn = &s->txn;
6876 struct hdr_exp *exp;
6877
6878 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006879 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006880
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006881 /*
6882 * The interleaving of transformations and verdicts
6883 * makes it difficult to decide to continue or stop
6884 * the evaluation.
6885 */
6886
Willy Tarreau6c123b12010-01-28 20:22:06 +01006887 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6888 break;
6889
Willy Tarreau3d300592007-03-18 18:34:41 +01006890 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006891 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006892 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006893 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006894
6895 /* if this filter had a condition, evaluate it now and skip to
6896 * next filter if the condition does not match.
6897 */
6898 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006899 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006900 ret = acl_pass(ret);
6901 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6902 ret = !ret;
6903
6904 if (!ret)
6905 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006906 }
6907
6908 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006909 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006910 if (unlikely(ret < 0))
6911 return -1;
6912
6913 if (likely(ret == 0)) {
6914 /* The filter did not match the request, it can be
6915 * iterated through all headers.
6916 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006917 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006918 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006919 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006920 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921}
6922
6923
Willy Tarreaua15645d2007-03-18 16:22:39 +01006924
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006926 * Try to retrieve the server associated to the appsession.
6927 * If the server is found, it's assigned to the session.
6928 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006929void manage_client_side_appsession(struct session *s, const char *buf, int len) {
6930 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006931 appsess *asession = NULL;
6932 char *sessid_temp = NULL;
6933
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006934 if (len > s->be->appsession_len) {
6935 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006936 }
6937
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006938 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006939 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006940 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006941 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006942 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006943 }
6944
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006945 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006946 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006947 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006948 return;
6949 }
6950
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006951 memcpy(txn->sessid, buf, len);
6952 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006953 }
6954
6955 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6956 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006957 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006958 return;
6959 }
6960
Cyril Bontéb21570a2009-11-29 20:04:48 +01006961 memcpy(sessid_temp, buf, len);
6962 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006963
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006964 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006965 /* free previously allocated memory */
6966 pool_free2(apools.sessid, sessid_temp);
6967
6968 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006969 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
6970 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006971 asession->request_count++;
6972
6973 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006974 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006975
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006976 while (srv) {
6977 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006978 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006979 (s->be->options & PR_O_PERSIST) ||
6980 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006981 /* we found the server and it's usable */
6982 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006983 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006984 s->flags |= SN_DIRECT | SN_ASSIGNED;
6985 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006986
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006987 break;
6988 } else {
6989 txn->flags &= ~TX_CK_MASK;
6990 txn->flags |= TX_CK_DOWN;
6991 }
6992 }
6993 srv = srv->next;
6994 }
6995 }
6996 }
6997}
6998
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999/* Find the end of a cookie value contained between <s> and <e>. It works the
7000 * same way as with headers above except that the semi-colon also ends a token.
7001 * See RFC2965 for more information. Note that it requires a valid header to
7002 * return a valid result.
7003 */
7004char *find_cookie_value_end(char *s, const char *e)
7005{
7006 int quoted, qdpair;
7007
7008 quoted = qdpair = 0;
7009 for (; s < e; s++) {
7010 if (qdpair) qdpair = 0;
7011 else if (quoted) {
7012 if (*s == '\\') qdpair = 1;
7013 else if (*s == '"') quoted = 0;
7014 }
7015 else if (*s == '"') quoted = 1;
7016 else if (*s == ',' || *s == ';') return s;
7017 }
7018 return s;
7019}
7020
7021/* Delete a value in a header between delimiters <from> and <next> in buffer
7022 * <buf>. The number of characters displaced is returned, and the pointer to
7023 * the first delimiter is updated if required. The function tries as much as
7024 * possible to respect the following principles :
7025 * - replace <from> delimiter by the <next> one unless <from> points to a
7026 * colon, in which case <next> is simply removed
7027 * - set exactly one space character after the new first delimiter, unless
7028 * there are not enough characters in the block being moved to do so.
7029 * - remove unneeded spaces before the previous delimiter and after the new
7030 * one.
7031 *
7032 * It is the caller's responsibility to ensure that :
7033 * - <from> points to a valid delimiter or the colon ;
7034 * - <next> points to a valid delimiter or the final CR/LF ;
7035 * - there are non-space chars before <from> ;
7036 * - there is a CR/LF at or after <next>.
7037 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007038int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007039{
7040 char *prev = *from;
7041
7042 if (*prev == ':') {
7043 /* We're removing the first value, preserve the colon and add a
7044 * space if possible.
7045 */
7046 if (!http_is_crlf[(unsigned char)*next])
7047 next++;
7048 prev++;
7049 if (prev < next)
7050 *prev++ = ' ';
7051
7052 while (http_is_spht[(unsigned char)*next])
7053 next++;
7054 } else {
7055 /* Remove useless spaces before the old delimiter. */
7056 while (http_is_spht[(unsigned char)*(prev-1)])
7057 prev--;
7058 *from = prev;
7059
7060 /* copy the delimiter and if possible a space if we're
7061 * not at the end of the line.
7062 */
7063 if (!http_is_crlf[(unsigned char)*next]) {
7064 *prev++ = *next++;
7065 if (prev + 1 < next)
7066 *prev++ = ' ';
7067 while (http_is_spht[(unsigned char)*next])
7068 next++;
7069 }
7070 }
7071 return buffer_replace2(buf, prev, next, NULL, 0);
7072}
7073
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007074/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007075 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007076 * desirable to call it only when needed. This code is quite complex because
7077 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7078 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007079 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007080void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007082 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007083 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007084 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007085 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7086 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007087
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007089 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007090 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007092 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007094 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007096 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007097 hdr_beg = hdr_next;
7098 hdr_end = hdr_beg + cur_hdr->len;
7099 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007100
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007101 /* We have one full header between hdr_beg and hdr_end, and the
7102 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103 * "Cookie:" headers.
7104 */
7105
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007106 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007107 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007108 old_idx = cur_idx;
7109 continue;
7110 }
7111
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007112 del_from = NULL; /* nothing to be deleted */
7113 preserve_hdr = 0; /* assume we may kill the whole header */
7114
Willy Tarreau58f10d72006-12-04 02:26:12 +01007115 /* Now look for cookies. Conforming to RFC2109, we have to support
7116 * attributes whose name begin with a '$', and associate them with
7117 * the right cookie, if we want to delete this cookie.
7118 * So there are 3 cases for each cookie read :
7119 * 1) it's a special attribute, beginning with a '$' : ignore it.
7120 * 2) it's a server id cookie that we *MAY* want to delete : save
7121 * some pointers on it (last semi-colon, beginning of cookie...)
7122 * 3) it's an application cookie : we *MAY* have to delete a previous
7123 * "special" cookie.
7124 * At the end of loop, if a "special" cookie remains, we may have to
7125 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007126 * *MUST* delete it.
7127 *
7128 * Note: RFC2965 is unclear about the processing of spaces around
7129 * the equal sign in the ATTR=VALUE form. A careful inspection of
7130 * the RFC explicitly allows spaces before it, and not within the
7131 * tokens (attrs or values). An inspection of RFC2109 allows that
7132 * too but section 10.1.3 lets one think that spaces may be allowed
7133 * after the equal sign too, resulting in some (rare) buggy
7134 * implementations trying to do that. So let's do what servers do.
7135 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7136 * allowed quoted strings in values, with any possible character
7137 * after a backslash, including control chars and delimitors, which
7138 * causes parsing to become ambiguous. Browsers also allow spaces
7139 * within values even without quotes.
7140 *
7141 * We have to keep multiple pointers in order to support cookie
7142 * removal at the beginning, middle or end of header without
7143 * corrupting the header. All of these headers are valid :
7144 *
7145 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7146 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7147 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7148 * | | | | | | | | |
7149 * | | | | | | | | hdr_end <--+
7150 * | | | | | | | +--> next
7151 * | | | | | | +----> val_end
7152 * | | | | | +-----------> val_beg
7153 * | | | | +--------------> equal
7154 * | | | +----------------> att_end
7155 * | | +---------------------> att_beg
7156 * | +--------------------------> prev
7157 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007158 */
7159
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007160 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7161 /* Iterate through all cookies on this line */
7162
7163 /* find att_beg */
7164 att_beg = prev + 1;
7165 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7166 att_beg++;
7167
7168 /* find att_end : this is the first character after the last non
7169 * space before the equal. It may be equal to hdr_end.
7170 */
7171 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007173 while (equal < hdr_end) {
7174 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007175 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007176 if (http_is_spht[(unsigned char)*equal++])
7177 continue;
7178 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007179 }
7180
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007181 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7182 * is between <att_beg> and <equal>, both may be identical.
7183 */
7184
7185 /* look for end of cookie if there is an equal sign */
7186 if (equal < hdr_end && *equal == '=') {
7187 /* look for the beginning of the value */
7188 val_beg = equal + 1;
7189 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7190 val_beg++;
7191
7192 /* find the end of the value, respecting quotes */
7193 next = find_cookie_value_end(val_beg, hdr_end);
7194
7195 /* make val_end point to the first white space or delimitor after the value */
7196 val_end = next;
7197 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7198 val_end--;
7199 } else {
7200 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007201 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007202
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007203 /* We have nothing to do with attributes beginning with '$'. However,
7204 * they will automatically be removed if a header before them is removed,
7205 * since they're supposed to be linked together.
7206 */
7207 if (*att_beg == '$')
7208 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007209
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007210 /* Ignore cookies with no equal sign */
7211 if (equal == next) {
7212 /* This is not our cookie, so we must preserve it. But if we already
7213 * scheduled another cookie for removal, we cannot remove the
7214 * complete header, but we can remove the previous block itself.
7215 */
7216 preserve_hdr = 1;
7217 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007218 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007219 val_end += delta;
7220 next += delta;
7221 hdr_end += delta;
7222 hdr_next += delta;
7223 cur_hdr->len += delta;
7224 http_msg_move_end(&txn->req, delta);
7225 prev = del_from;
7226 del_from = NULL;
7227 }
7228 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007229 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007231 /* if there are spaces around the equal sign, we need to
7232 * strip them otherwise we'll get trouble for cookie captures,
7233 * or even for rewrites. Since this happens extremely rarely,
7234 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007235 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007236 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7237 int stripped_before = 0;
7238 int stripped_after = 0;
7239
7240 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007241 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007242 equal += stripped_before;
7243 val_beg += stripped_before;
7244 }
7245
7246 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007247 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007248 val_beg += stripped_after;
7249 stripped_before += stripped_after;
7250 }
7251
7252 val_end += stripped_before;
7253 next += stripped_before;
7254 hdr_end += stripped_before;
7255 hdr_next += stripped_before;
7256 cur_hdr->len += stripped_before;
7257 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007258 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007259 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007260
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007261 /* First, let's see if we want to capture this cookie. We check
7262 * that we don't already have a client side cookie, because we
7263 * can only capture one. Also as an optimisation, we ignore
7264 * cookies shorter than the declared name.
7265 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007266 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7267 (val_end - att_beg >= s->fe->capture_namelen) &&
7268 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007269 int log_len = val_end - att_beg;
7270
7271 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7272 Alert("HTTP logging : out of memory.\n");
7273 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007274 if (log_len > s->fe->capture_len)
7275 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007276 memcpy(txn->cli_cookie, att_beg, log_len);
7277 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007278 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007279 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007280
Willy Tarreaubca99692010-10-06 19:25:55 +02007281 /* Persistence cookies in passive, rewrite or insert mode have the
7282 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007283 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007284 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007285 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007286 * For cookies in prefix mode, the form is :
7287 *
7288 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007289 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007290 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7291 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7292 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007293 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007294
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007295 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7296 * have the server ID between val_beg and delim, and the original cookie between
7297 * delim+1 and val_end. Otherwise, delim==val_end :
7298 *
7299 * Cookie: NAME=SRV; # in all but prefix modes
7300 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7301 * | || || | |+-> next
7302 * | || || | +--> val_end
7303 * | || || +---------> delim
7304 * | || |+------------> val_beg
7305 * | || +-------------> att_end = equal
7306 * | |+-----------------> att_beg
7307 * | +------------------> prev
7308 * +-------------------------> hdr_beg
7309 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007311 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007312 for (delim = val_beg; delim < val_end; delim++)
7313 if (*delim == COOKIE_DELIM)
7314 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007315 } else {
7316 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007317 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007318 /* Now check if the cookie contains a date field, which would
7319 * appear after a vertical bar ('|') just after the server name
7320 * and before the delimiter.
7321 */
7322 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7323 if (vbar1) {
7324 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007325 * right is the last seen date. It is a base64 encoded
7326 * 30-bit value representing the UNIX date since the
7327 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007328 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007329 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007330 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007331 if (val_end - vbar1 >= 5) {
7332 val = b64tos30(vbar1);
7333 if (val > 0)
7334 txn->cookie_last_date = val << 2;
7335 }
7336 /* look for a second vertical bar */
7337 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7338 if (vbar1 && (val_end - vbar1 > 5)) {
7339 val = b64tos30(vbar1 + 1);
7340 if (val > 0)
7341 txn->cookie_first_date = val << 2;
7342 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007343 }
7344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007345
Willy Tarreauf64d1412010-10-07 20:06:11 +02007346 /* if the cookie has an expiration date and the proxy wants to check
7347 * it, then we do that now. We first check if the cookie is too old,
7348 * then only if it has expired. We detect strict overflow because the
7349 * time resolution here is not great (4 seconds). Cookies with dates
7350 * in the future are ignored if their offset is beyond one day. This
7351 * allows an admin to fix timezone issues without expiring everyone
7352 * and at the same time avoids keeping unwanted side effects for too
7353 * long.
7354 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007355 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7356 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007357 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007358 txn->flags &= ~TX_CK_MASK;
7359 txn->flags |= TX_CK_OLD;
7360 delim = val_beg; // let's pretend we have not found the cookie
7361 txn->cookie_first_date = 0;
7362 txn->cookie_last_date = 0;
7363 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007364 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7365 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007366 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007367 txn->flags &= ~TX_CK_MASK;
7368 txn->flags |= TX_CK_EXPIRED;
7369 delim = val_beg; // let's pretend we have not found the cookie
7370 txn->cookie_first_date = 0;
7371 txn->cookie_last_date = 0;
7372 }
7373
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007374 /* Here, we'll look for the first running server which supports the cookie.
7375 * This allows to share a same cookie between several servers, for example
7376 * to dedicate backup servers to specific servers only.
7377 * However, to prevent clients from sticking to cookie-less backup server
7378 * when they have incidentely learned an empty cookie, we simply ignore
7379 * empty cookies and mark them as invalid.
7380 * The same behaviour is applied when persistence must be ignored.
7381 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007382 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007383 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007384
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007385 while (srv) {
7386 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7387 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7388 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007389 (s->be->options & PR_O_PERSIST) ||
7390 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007391 /* we found the server and we can use it */
7392 txn->flags &= ~TX_CK_MASK;
7393 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007394 s->flags |= SN_DIRECT | SN_ASSIGNED;
7395 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007396 break;
7397 } else {
7398 /* we found a server, but it's down,
7399 * mark it as such and go on in case
7400 * another one is available.
7401 */
7402 txn->flags &= ~TX_CK_MASK;
7403 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007404 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007405 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007406 srv = srv->next;
7407 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408
Willy Tarreauf64d1412010-10-07 20:06:11 +02007409 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007410 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007411 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007412 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007413 txn->flags |= TX_CK_UNUSED;
7414 else
7415 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007416 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007417
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007418 /* depending on the cookie mode, we may have to either :
7419 * - delete the complete cookie if we're in insert+indirect mode, so that
7420 * the server never sees it ;
7421 * - remove the server id from the cookie value, and tag the cookie as an
7422 * application cookie so that it does not get accidentely removed later,
7423 * if we're in cookie prefix mode
7424 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007425 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007426 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007427
Willy Tarreau9b28e032012-10-12 23:49:43 +02007428 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007429 val_end += delta;
7430 next += delta;
7431 hdr_end += delta;
7432 hdr_next += delta;
7433 cur_hdr->len += delta;
7434 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007435
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007436 del_from = NULL;
7437 preserve_hdr = 1; /* we want to keep this cookie */
7438 }
7439 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007440 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007441 del_from = prev;
7442 }
7443 } else {
7444 /* This is not our cookie, so we must preserve it. But if we already
7445 * scheduled another cookie for removal, we cannot remove the
7446 * complete header, but we can remove the previous block itself.
7447 */
7448 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007449
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007450 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007451 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007452 if (att_beg >= del_from)
7453 att_beg += delta;
7454 if (att_end >= del_from)
7455 att_end += delta;
7456 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007457 val_end += delta;
7458 next += delta;
7459 hdr_end += delta;
7460 hdr_next += delta;
7461 cur_hdr->len += delta;
7462 http_msg_move_end(&txn->req, delta);
7463 prev = del_from;
7464 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007469 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007470 int cmp_len, value_len;
7471 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007472
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007473 if (s->be->options2 & PR_O2_AS_PFX) {
7474 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7475 value_begin = att_beg + s->be->appsession_name_len;
7476 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007477 } else {
7478 cmp_len = att_end - att_beg;
7479 value_begin = val_beg;
7480 value_len = val_end - val_beg;
7481 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007482
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007483 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007484 if (cmp_len == s->be->appsession_name_len &&
7485 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7486 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007488 }
7489
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007490 /* continue with next cookie on this header line */
7491 att_beg = next;
7492 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007493
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007494 /* There are no more cookies on this line.
7495 * We may still have one (or several) marked for deletion at the
7496 * end of the line. We must do this now in two ways :
7497 * - if some cookies must be preserved, we only delete from the
7498 * mark to the end of line ;
7499 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007500 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007501 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007502 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007503 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007504 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007505 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007506 cur_hdr->len += delta;
7507 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007508 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007509
7510 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007511 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7512 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007513 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007514 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007516 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007517 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007518 }
7519
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007520 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007521 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007523}
7524
7525
Willy Tarreaua15645d2007-03-18 16:22:39 +01007526/* Iterate the same filter through all response headers contained in <rtr>.
7527 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7528 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007529int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530{
7531 char term;
7532 char *cur_ptr, *cur_end, *cur_next;
7533 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007534 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007535 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007536 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007537
7538 last_hdr = 0;
7539
Willy Tarreau9b28e032012-10-12 23:49:43 +02007540 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007541 old_idx = 0;
7542
7543 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007544 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007545 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007546 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007547 (exp->action == ACT_ALLOW ||
7548 exp->action == ACT_DENY))
7549 return 0;
7550
7551 cur_idx = txn->hdr_idx.v[old_idx].next;
7552 if (!cur_idx)
7553 break;
7554
7555 cur_hdr = &txn->hdr_idx.v[cur_idx];
7556 cur_ptr = cur_next;
7557 cur_end = cur_ptr + cur_hdr->len;
7558 cur_next = cur_end + cur_hdr->cr + 1;
7559
7560 /* Now we have one header between cur_ptr and cur_end,
7561 * and the next header starts at cur_next.
7562 */
7563
7564 /* The annoying part is that pattern matching needs
7565 * that we modify the contents to null-terminate all
7566 * strings before testing them.
7567 */
7568
7569 term = *cur_end;
7570 *cur_end = '\0';
7571
7572 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7573 switch (exp->action) {
7574 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007575 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007576 last_hdr = 1;
7577 break;
7578
7579 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007580 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007581 last_hdr = 1;
7582 break;
7583
7584 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007585 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7586 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007587 /* FIXME: if the user adds a newline in the replacement, the
7588 * index will not be recalculated for now, and the new line
7589 * will not be counted as a new header.
7590 */
7591
7592 cur_end += delta;
7593 cur_next += delta;
7594 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007595 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007596 break;
7597
7598 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007599 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600 cur_next += delta;
7601
Willy Tarreaufa355d42009-11-29 18:12:29 +01007602 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7604 txn->hdr_idx.used--;
7605 cur_hdr->len = 0;
7606 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007607 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007608 break;
7609
7610 }
7611 }
7612 if (cur_end)
7613 *cur_end = term; /* restore the string terminator */
7614
7615 /* keep the link from this header to next one in case of later
7616 * removal of next header.
7617 */
7618 old_idx = cur_idx;
7619 }
7620 return 0;
7621}
7622
7623
7624/* Apply the filter to the status line in the response buffer <rtr>.
7625 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7626 * or -1 if a replacement resulted in an invalid status line.
7627 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007628int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629{
7630 char term;
7631 char *cur_ptr, *cur_end;
7632 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007633 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007634 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007635
7636
Willy Tarreau3d300592007-03-18 18:34:41 +01007637 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007638 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007639 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640 (exp->action == ACT_ALLOW ||
7641 exp->action == ACT_DENY))
7642 return 0;
7643 else if (exp->action == ACT_REMOVE)
7644 return 0;
7645
7646 done = 0;
7647
Willy Tarreau9b28e032012-10-12 23:49:43 +02007648 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007649 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007650
7651 /* Now we have the status line between cur_ptr and cur_end */
7652
7653 /* The annoying part is that pattern matching needs
7654 * that we modify the contents to null-terminate all
7655 * strings before testing them.
7656 */
7657
7658 term = *cur_end;
7659 *cur_end = '\0';
7660
7661 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7662 switch (exp->action) {
7663 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007664 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 done = 1;
7666 break;
7667
7668 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007669 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007670 done = 1;
7671 break;
7672
7673 case ACT_REPLACE:
7674 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007675 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7676 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677 /* FIXME: if the user adds a newline in the replacement, the
7678 * index will not be recalculated for now, and the new line
7679 * will not be counted as a new header.
7680 */
7681
Willy Tarreaufa355d42009-11-29 18:12:29 +01007682 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007683 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007684 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007685 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686 cur_ptr, cur_end + 1,
7687 NULL, NULL);
7688 if (unlikely(!cur_end))
7689 return -1;
7690
7691 /* we have a full respnse and we know that we have either a CR
7692 * or an LF at <ptr>.
7693 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007694 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007695 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007696 /* there is no point trying this regex on headers */
7697 return 1;
7698 }
7699 }
7700 *cur_end = term; /* restore the string terminator */
7701 return done;
7702}
7703
7704
7705
7706/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007707 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007708 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7709 * unparsable response.
7710 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007711int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007712{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007713 struct http_txn *txn = &s->txn;
7714 struct hdr_exp *exp;
7715
7716 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007717 int ret;
7718
7719 /*
7720 * The interleaving of transformations and verdicts
7721 * makes it difficult to decide to continue or stop
7722 * the evaluation.
7723 */
7724
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007725 if (txn->flags & TX_SVDENY)
7726 break;
7727
Willy Tarreau3d300592007-03-18 18:34:41 +01007728 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007729 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7730 exp->action == ACT_PASS)) {
7731 exp = exp->next;
7732 continue;
7733 }
7734
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007735 /* if this filter had a condition, evaluate it now and skip to
7736 * next filter if the condition does not match.
7737 */
7738 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007739 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007740 ret = acl_pass(ret);
7741 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7742 ret = !ret;
7743 if (!ret)
7744 continue;
7745 }
7746
Willy Tarreaua15645d2007-03-18 16:22:39 +01007747 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007748 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007749 if (unlikely(ret < 0))
7750 return -1;
7751
7752 if (likely(ret == 0)) {
7753 /* The filter did not match the response, it can be
7754 * iterated through all headers.
7755 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007756 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007757 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007758 }
7759 return 0;
7760}
7761
7762
Willy Tarreaua15645d2007-03-18 16:22:39 +01007763/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007764 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007765 * desirable to call it only when needed. This function is also used when we
7766 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007767 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007768void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007769{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007770 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007771 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007772 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007773 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007774 char *hdr_beg, *hdr_end, *hdr_next;
7775 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007776
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777 /* Iterate through the headers.
7778 * we start with the start line.
7779 */
7780 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007781 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007782
7783 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7784 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007785 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007786
7787 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007788 hdr_beg = hdr_next;
7789 hdr_end = hdr_beg + cur_hdr->len;
7790 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007791
Willy Tarreau24581ba2010-08-31 22:39:35 +02007792 /* We have one full header between hdr_beg and hdr_end, and the
7793 * next header starts at hdr_next. We're only interested in
7794 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007795 */
7796
Willy Tarreau24581ba2010-08-31 22:39:35 +02007797 is_cookie2 = 0;
7798 prev = hdr_beg + 10;
7799 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007800 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007801 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7802 if (!val) {
7803 old_idx = cur_idx;
7804 continue;
7805 }
7806 is_cookie2 = 1;
7807 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007808 }
7809
Willy Tarreau24581ba2010-08-31 22:39:35 +02007810 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7811 * <prev> points to the colon.
7812 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007813 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007814
Willy Tarreau24581ba2010-08-31 22:39:35 +02007815 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7816 * check-cache is enabled) and we are not interested in checking
7817 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007818 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007819 if (s->be->cookie_name == NULL &&
7820 s->be->appsession_name == NULL &&
7821 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007822 return;
7823
Willy Tarreau24581ba2010-08-31 22:39:35 +02007824 /* OK so now we know we have to process this response cookie.
7825 * The format of the Set-Cookie header is slightly different
7826 * from the format of the Cookie header in that it does not
7827 * support the comma as a cookie delimiter (thus the header
7828 * cannot be folded) because the Expires attribute described in
7829 * the original Netscape's spec may contain an unquoted date
7830 * with a comma inside. We have to live with this because
7831 * many browsers don't support Max-Age and some browsers don't
7832 * support quoted strings. However the Set-Cookie2 header is
7833 * clean.
7834 *
7835 * We have to keep multiple pointers in order to support cookie
7836 * removal at the beginning, middle or end of header without
7837 * corrupting the header (in case of set-cookie2). A special
7838 * pointer, <scav> points to the beginning of the set-cookie-av
7839 * fields after the first semi-colon. The <next> pointer points
7840 * either to the end of line (set-cookie) or next unquoted comma
7841 * (set-cookie2). All of these headers are valid :
7842 *
7843 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7844 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7845 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7846 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7847 * | | | | | | | | | |
7848 * | | | | | | | | +-> next hdr_end <--+
7849 * | | | | | | | +------------> scav
7850 * | | | | | | +--------------> val_end
7851 * | | | | | +--------------------> val_beg
7852 * | | | | +----------------------> equal
7853 * | | | +------------------------> att_end
7854 * | | +----------------------------> att_beg
7855 * | +------------------------------> prev
7856 * +-----------------------------------------> hdr_beg
7857 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858
Willy Tarreau24581ba2010-08-31 22:39:35 +02007859 for (; prev < hdr_end; prev = next) {
7860 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007861
Willy Tarreau24581ba2010-08-31 22:39:35 +02007862 /* find att_beg */
7863 att_beg = prev + 1;
7864 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7865 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007866
Willy Tarreau24581ba2010-08-31 22:39:35 +02007867 /* find att_end : this is the first character after the last non
7868 * space before the equal. It may be equal to hdr_end.
7869 */
7870 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007871
Willy Tarreau24581ba2010-08-31 22:39:35 +02007872 while (equal < hdr_end) {
7873 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7874 break;
7875 if (http_is_spht[(unsigned char)*equal++])
7876 continue;
7877 att_end = equal;
7878 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007879
Willy Tarreau24581ba2010-08-31 22:39:35 +02007880 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7881 * is between <att_beg> and <equal>, both may be identical.
7882 */
7883
7884 /* look for end of cookie if there is an equal sign */
7885 if (equal < hdr_end && *equal == '=') {
7886 /* look for the beginning of the value */
7887 val_beg = equal + 1;
7888 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7889 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890
Willy Tarreau24581ba2010-08-31 22:39:35 +02007891 /* find the end of the value, respecting quotes */
7892 next = find_cookie_value_end(val_beg, hdr_end);
7893
7894 /* make val_end point to the first white space or delimitor after the value */
7895 val_end = next;
7896 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7897 val_end--;
7898 } else {
7899 /* <equal> points to next comma, semi-colon or EOL */
7900 val_beg = val_end = next = equal;
7901 }
7902
7903 if (next < hdr_end) {
7904 /* Set-Cookie2 supports multiple cookies, and <next> points to
7905 * a colon or semi-colon before the end. So skip all attr-value
7906 * pairs and look for the next comma. For Set-Cookie, since
7907 * commas are permitted in values, skip to the end.
7908 */
7909 if (is_cookie2)
7910 next = find_hdr_value_end(next, hdr_end);
7911 else
7912 next = hdr_end;
7913 }
7914
7915 /* Now everything is as on the diagram above */
7916
7917 /* Ignore cookies with no equal sign */
7918 if (equal == val_end)
7919 continue;
7920
7921 /* If there are spaces around the equal sign, we need to
7922 * strip them otherwise we'll get trouble for cookie captures,
7923 * or even for rewrites. Since this happens extremely rarely,
7924 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007925 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007926 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7927 int stripped_before = 0;
7928 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007929
Willy Tarreau24581ba2010-08-31 22:39:35 +02007930 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007931 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007932 equal += stripped_before;
7933 val_beg += stripped_before;
7934 }
7935
7936 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007937 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007938 val_beg += stripped_after;
7939 stripped_before += stripped_after;
7940 }
7941
7942 val_end += stripped_before;
7943 next += stripped_before;
7944 hdr_end += stripped_before;
7945 hdr_next += stripped_before;
7946 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007947 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007948 }
7949
7950 /* First, let's see if we want to capture this cookie. We check
7951 * that we don't already have a server side cookie, because we
7952 * can only capture one. Also as an optimisation, we ignore
7953 * cookies shorter than the declared name.
7954 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007955 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007956 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007957 (val_end - att_beg >= s->fe->capture_namelen) &&
7958 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007959 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007960 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961 Alert("HTTP logging : out of memory.\n");
7962 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007963 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007964 if (log_len > s->fe->capture_len)
7965 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007966 memcpy(txn->srv_cookie, att_beg, log_len);
7967 txn->srv_cookie[log_len] = 0;
7968 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007969 }
7970
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007971 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007972 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007973 if (!(s->flags & SN_IGNORE_PRST) &&
7974 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7975 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007976 /* assume passive cookie by default */
7977 txn->flags &= ~TX_SCK_MASK;
7978 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979
7980 /* If the cookie is in insert mode on a known server, we'll delete
7981 * this occurrence because we'll insert another one later.
7982 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007983 * a direct access.
7984 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007985 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007986 /* The "preserve" flag was set, we don't want to touch the
7987 * server's cookie.
7988 */
7989 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007990 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
7991 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007992 /* this cookie must be deleted */
7993 if (*prev == ':' && next == hdr_end) {
7994 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007995 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007996 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7997 txn->hdr_idx.used--;
7998 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007999 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008000 hdr_next += delta;
8001 http_msg_move_end(&txn->rsp, delta);
8002 /* note: while both invalid now, <next> and <hdr_end>
8003 * are still equal, so the for() will stop as expected.
8004 */
8005 } else {
8006 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008007 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008008 next = prev;
8009 hdr_end += delta;
8010 hdr_next += delta;
8011 cur_hdr->len += delta;
8012 http_msg_move_end(&txn->rsp, delta);
8013 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008014 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008015 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008016 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008017 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008018 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008019 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020 * with this server since we know it.
8021 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008022 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008023 next += delta;
8024 hdr_end += delta;
8025 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008026 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008027 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028
Willy Tarreauf1348312010-10-07 15:54:11 +02008029 txn->flags &= ~TX_SCK_MASK;
8030 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008031 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008032 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008034 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008036 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008037 next += delta;
8038 hdr_end += delta;
8039 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008040 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008041 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042
Willy Tarreau827aee92011-03-10 16:55:02 +01008043 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008044 txn->flags &= ~TX_SCK_MASK;
8045 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 }
8047 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008048 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008049 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008050 int cmp_len, value_len;
8051 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008053 if (s->be->options2 & PR_O2_AS_PFX) {
8054 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8055 value_begin = att_beg + s->be->appsession_name_len;
8056 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008057 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008058 cmp_len = att_end - att_beg;
8059 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008060 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008061 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008062
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008063 if ((cmp_len == s->be->appsession_name_len) &&
8064 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008065 /* free a possibly previously allocated memory */
8066 pool_free2(apools.sessid, txn->sessid);
8067
Cyril Bontéb21570a2009-11-29 20:04:48 +01008068 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008069 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008070 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008071 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008072 return;
8073 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008074 memcpy(txn->sessid, value_begin, value_len);
8075 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008077 }
8078 /* that's done for this cookie, check the next one on the same
8079 * line when next != hdr_end (only if is_cookie2).
8080 */
8081 }
8082 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008083 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008084 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008085
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008086 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008087 appsess *asession = NULL;
8088 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008089 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008090 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008091 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008092 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8093 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008094 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008095 return;
8096 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008097 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8098
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008099 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8100 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008101 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8102 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008103 return;
8104 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008105 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8106 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008107
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008108 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008109 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008110 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008111 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8112 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008113 return;
8114 }
8115 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008116 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008117
8118 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008119 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008120 }
8121
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008122 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008123 asession->request_count++;
8124 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008125}
8126
8127
Willy Tarreaua15645d2007-03-18 16:22:39 +01008128/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008129 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008131void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008133 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008134 char *p1, *p2;
8135
8136 char *cur_ptr, *cur_end, *cur_next;
8137 int cur_idx;
8138
Willy Tarreau5df51872007-11-25 16:20:08 +01008139 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008140 return;
8141
8142 /* Iterate through the headers.
8143 * we start with the start line.
8144 */
8145 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008146 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008147
8148 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8149 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008150 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151
8152 cur_hdr = &txn->hdr_idx.v[cur_idx];
8153 cur_ptr = cur_next;
8154 cur_end = cur_ptr + cur_hdr->len;
8155 cur_next = cur_end + cur_hdr->cr + 1;
8156
8157 /* We have one full header between cur_ptr and cur_end, and the
8158 * next header starts at cur_next. We're only interested in
8159 * "Cookie:" headers.
8160 */
8161
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008162 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8163 if (val) {
8164 if ((cur_end - (cur_ptr + val) >= 8) &&
8165 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8166 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8167 return;
8168 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008169 }
8170
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008171 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8172 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008173 continue;
8174
8175 /* OK, right now we know we have a cache-control header at cur_ptr */
8176
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008177 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008178
8179 if (p1 >= cur_end) /* no more info */
8180 continue;
8181
8182 /* p1 is at the beginning of the value */
8183 p2 = p1;
8184
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008185 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008186 p2++;
8187
8188 /* we have a complete value between p1 and p2 */
8189 if (p2 < cur_end && *p2 == '=') {
8190 /* we have something of the form no-cache="set-cookie" */
8191 if ((cur_end - p1 >= 21) &&
8192 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8193 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008194 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008195 continue;
8196 }
8197
8198 /* OK, so we know that either p2 points to the end of string or to a comma */
8199 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008200 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8202 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8203 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008204 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205 return;
8206 }
8207
8208 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008209 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008210 continue;
8211 }
8212 }
8213}
8214
8215
Willy Tarreau58f10d72006-12-04 02:26:12 +01008216/*
8217 * Try to retrieve a known appsession in the URI, then the associated server.
8218 * If the server is found, it's assigned to the session.
8219 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008220void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008221{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008222 char *end_params, *first_param, *cur_param, *next_param;
8223 char separator;
8224 int value_len;
8225
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008226 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008227
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008228 if (s->be->appsession_name == NULL ||
8229 (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 +01008230 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008231 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008232
Cyril Bontéb21570a2009-11-29 20:04:48 +01008233 first_param = NULL;
8234 switch (mode) {
8235 case PR_O2_AS_M_PP:
8236 first_param = memchr(begin, ';', len);
8237 break;
8238 case PR_O2_AS_M_QS:
8239 first_param = memchr(begin, '?', len);
8240 break;
8241 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008242
Cyril Bontéb21570a2009-11-29 20:04:48 +01008243 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008244 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008246
Cyril Bontéb21570a2009-11-29 20:04:48 +01008247 switch (mode) {
8248 case PR_O2_AS_M_PP:
8249 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8250 end_params = (char *) begin + len;
8251 }
8252 separator = ';';
8253 break;
8254 case PR_O2_AS_M_QS:
8255 end_params = (char *) begin + len;
8256 separator = '&';
8257 break;
8258 default:
8259 /* unknown mode, shouldn't happen */
8260 return;
8261 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008262
Cyril Bontéb21570a2009-11-29 20:04:48 +01008263 cur_param = next_param = end_params;
8264 while (cur_param > first_param) {
8265 cur_param--;
8266 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8267 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008268 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8269 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8270 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008271 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008272 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8273 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008274 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008275 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008276 }
8277 break;
8278 }
8279 next_param = cur_param;
8280 }
8281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008282#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008283 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008284 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008285#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008286}
8287
Willy Tarreaub2513902006-12-17 14:52:38 +01008288/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008289 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008290 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008291 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008292 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008293 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008294 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008295 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008296 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008297int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008298{
8299 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008300 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008301 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008302
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008303 if (!uri_auth)
8304 return 0;
8305
Cyril Bonté70be45d2010-10-12 00:14:35 +02008306 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008307 return 0;
8308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008309 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008310 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008311 return 0;
8312
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008313 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008314 return 0;
8315
Willy Tarreaub2513902006-12-17 14:52:38 +01008316 return 1;
8317}
8318
Willy Tarreau4076a152009-04-02 15:18:36 +02008319/*
8320 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008321 * By default it tries to report the error position as msg->err_pos. However if
8322 * this one is not set, it will then report msg->next, which is the last known
8323 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008324 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008325 */
8326void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008327 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008328 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008329{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008330 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008331 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008332
Willy Tarreau9b28e032012-10-12 23:49:43 +02008333 es->len = MIN(chn->buf->i, sizeof(es->buf));
8334 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008335 len1 = MIN(len1, es->len);
8336 len2 = es->len - len1; /* remaining data if buffer wraps */
8337
Willy Tarreau9b28e032012-10-12 23:49:43 +02008338 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008339 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008340 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008341
Willy Tarreau4076a152009-04-02 15:18:36 +02008342 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008343 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008344 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008345 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008346
Willy Tarreau4076a152009-04-02 15:18:36 +02008347 es->when = date; // user-visible date
8348 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008349 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008350 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008351 if (objt_conn(s->req->prod->end))
8352 es->src = __objt_conn(s->req->prod->end)->addr.from;
8353 else
8354 memset(&es->src, 0, sizeof(es->src));
8355
Willy Tarreau078272e2010-12-12 12:46:33 +01008356 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008357 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008358 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008359 es->s_flags = s->flags;
8360 es->t_flags = s->txn.flags;
8361 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008362 es->b_out = chn->buf->o;
8363 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008364 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008365 es->m_clen = msg->chunk_len;
8366 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008367}
Willy Tarreaub2513902006-12-17 14:52:38 +01008368
Willy Tarreau294c4732011-12-16 21:35:50 +01008369/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8370 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8371 * performed over the whole headers. Otherwise it must contain a valid header
8372 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8373 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8374 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8375 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008376 * -1. The value fetch stops at commas, so this function is suited for use with
8377 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008378 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008379 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008380unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008381 struct hdr_idx *idx, int occ,
8382 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008383{
Willy Tarreau294c4732011-12-16 21:35:50 +01008384 struct hdr_ctx local_ctx;
8385 char *ptr_hist[MAX_HDR_HISTORY];
8386 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008387 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008388 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008389
Willy Tarreau294c4732011-12-16 21:35:50 +01008390 if (!ctx) {
8391 local_ctx.idx = 0;
8392 ctx = &local_ctx;
8393 }
8394
Willy Tarreaubce70882009-09-07 11:51:47 +02008395 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008396 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008397 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008398 occ--;
8399 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008400 *vptr = ctx->line + ctx->val;
8401 *vlen = ctx->vlen;
8402 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008403 }
8404 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008405 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008406 }
8407
8408 /* negative occurrence, we scan all the list then walk back */
8409 if (-occ > MAX_HDR_HISTORY)
8410 return 0;
8411
Willy Tarreau294c4732011-12-16 21:35:50 +01008412 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008413 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008414 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8415 len_hist[hist_ptr] = ctx->vlen;
8416 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008417 hist_ptr = 0;
8418 found++;
8419 }
8420 if (-occ > found)
8421 return 0;
8422 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008423 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8424 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8425 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008426 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008427 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008428 if (hist_ptr >= MAX_HDR_HISTORY)
8429 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008430 *vptr = ptr_hist[hist_ptr];
8431 *vlen = len_hist[hist_ptr];
8432 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008433}
8434
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008435/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8436 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8437 * performed over the whole headers. Otherwise it must contain a valid header
8438 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8439 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8440 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8441 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8442 * -1. This function differs from http_get_hdr() in that it only returns full
8443 * line header values and does not stop at commas.
8444 * The return value is 0 if nothing was found, or non-zero otherwise.
8445 */
8446unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8447 struct hdr_idx *idx, int occ,
8448 struct hdr_ctx *ctx, char **vptr, int *vlen)
8449{
8450 struct hdr_ctx local_ctx;
8451 char *ptr_hist[MAX_HDR_HISTORY];
8452 int len_hist[MAX_HDR_HISTORY];
8453 unsigned int hist_ptr;
8454 int found;
8455
8456 if (!ctx) {
8457 local_ctx.idx = 0;
8458 ctx = &local_ctx;
8459 }
8460
8461 if (occ >= 0) {
8462 /* search from the beginning */
8463 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8464 occ--;
8465 if (occ <= 0) {
8466 *vptr = ctx->line + ctx->val;
8467 *vlen = ctx->vlen;
8468 return 1;
8469 }
8470 }
8471 return 0;
8472 }
8473
8474 /* negative occurrence, we scan all the list then walk back */
8475 if (-occ > MAX_HDR_HISTORY)
8476 return 0;
8477
8478 found = hist_ptr = 0;
8479 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8480 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8481 len_hist[hist_ptr] = ctx->vlen;
8482 if (++hist_ptr >= MAX_HDR_HISTORY)
8483 hist_ptr = 0;
8484 found++;
8485 }
8486 if (-occ > found)
8487 return 0;
8488 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8489 * find occurrence -occ, so we have to check [hist_ptr+occ].
8490 */
8491 hist_ptr += occ;
8492 if (hist_ptr >= MAX_HDR_HISTORY)
8493 hist_ptr -= MAX_HDR_HISTORY;
8494 *vptr = ptr_hist[hist_ptr];
8495 *vlen = len_hist[hist_ptr];
8496 return 1;
8497}
8498
Willy Tarreaubaaee002006-06-26 02:48:02 +02008499/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008500 * Print a debug line with a header. Always stop at the first CR or LF char,
8501 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8502 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008503 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008504void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008505{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008506 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008507 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008508 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008509 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8510 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 +02008511
8512 for (max = 0; start + max < end; max++)
8513 if (start[max] == '\r' || start[max] == '\n')
8514 break;
8515
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008516 UBOUND(max, trash.size - trash.len - 3);
8517 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8518 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008519 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008520}
8521
Willy Tarreau0937bc42009-12-22 15:03:09 +01008522/*
8523 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8524 * the required fields are properly allocated and that we only need to (re)init
8525 * them. This should be used before processing any new request.
8526 */
8527void http_init_txn(struct session *s)
8528{
8529 struct http_txn *txn = &s->txn;
8530 struct proxy *fe = s->fe;
8531
8532 txn->flags = 0;
8533 txn->status = -1;
8534
Willy Tarreauf64d1412010-10-07 20:06:11 +02008535 txn->cookie_first_date = 0;
8536 txn->cookie_last_date = 0;
8537
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008538 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008539 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008540 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008541 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008542 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008543 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008544 txn->req.chunk_len = 0LL;
8545 txn->req.body_len = 0LL;
8546 txn->rsp.chunk_len = 0LL;
8547 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008548 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8549 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008550 txn->req.chn = s->req;
8551 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008552
8553 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008554
8555 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8556 if (fe->options2 & PR_O2_REQBUG_OK)
8557 txn->req.err_pos = -1; /* let buggy requests pass */
8558
Willy Tarreau46023632010-01-07 22:51:47 +01008559 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008560 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8561
Willy Tarreau46023632010-01-07 22:51:47 +01008562 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008563 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8564
8565 if (txn->hdr_idx.v)
8566 hdr_idx_init(&txn->hdr_idx);
8567}
8568
8569/* to be used at the end of a transaction */
8570void http_end_txn(struct session *s)
8571{
8572 struct http_txn *txn = &s->txn;
8573
Willy Tarreau75195602014-03-11 15:48:55 +01008574 /* release any possible compression context */
8575 if (s->flags & SN_COMP_READY)
8576 s->comp_algo->end(&s->comp_ctx);
8577 s->comp_algo = NULL;
8578 s->flags &= ~SN_COMP_READY;
8579
Willy Tarreau0937bc42009-12-22 15:03:09 +01008580 /* these ones will have been dynamically allocated */
8581 pool_free2(pool2_requri, txn->uri);
8582 pool_free2(pool2_capture, txn->cli_cookie);
8583 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008584 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008585 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008586
William Lallemanda73203e2012-03-12 12:48:57 +01008587 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008588 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008589 txn->uri = NULL;
8590 txn->srv_cookie = NULL;
8591 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008592
8593 if (txn->req.cap) {
8594 struct cap_hdr *h;
8595 for (h = s->fe->req_cap; h; h = h->next)
8596 pool_free2(h->pool, txn->req.cap[h->index]);
8597 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8598 }
8599
8600 if (txn->rsp.cap) {
8601 struct cap_hdr *h;
8602 for (h = s->fe->rsp_cap; h; h = h->next)
8603 pool_free2(h->pool, txn->rsp.cap[h->index]);
8604 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8605 }
8606
Willy Tarreau0937bc42009-12-22 15:03:09 +01008607}
8608
8609/* to be used at the end of a transaction to prepare a new one */
8610void http_reset_txn(struct session *s)
8611{
8612 http_end_txn(s);
8613 http_init_txn(s);
8614
8615 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008616 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008617 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008618 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008619 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008620 /* re-init store persistence */
8621 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008622 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008623
Willy Tarreau0937bc42009-12-22 15:03:09 +01008624 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008625
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008626 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008627
Willy Tarreau739cfba2010-01-25 23:11:14 +01008628 /* We must trim any excess data from the response buffer, because we
8629 * may have blocked an invalid response from a server that we don't
8630 * want to accidentely forward once we disable the analysers, nor do
8631 * we want those data to come along with next response. A typical
8632 * example of such data would be from a buggy server responding to
8633 * a HEAD with some data, or sending more than the advertised
8634 * content-length.
8635 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008636 if (unlikely(s->rep->buf->i))
8637 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008638
Willy Tarreau0937bc42009-12-22 15:03:09 +01008639 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008640 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008641
Willy Tarreaud04e8582010-05-31 12:31:35 +02008642 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008643 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008644
8645 s->req->rex = TICK_ETERNITY;
8646 s->req->wex = TICK_ETERNITY;
8647 s->req->analyse_exp = TICK_ETERNITY;
8648 s->rep->rex = TICK_ETERNITY;
8649 s->rep->wex = TICK_ETERNITY;
8650 s->rep->analyse_exp = TICK_ETERNITY;
8651}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008652
Willy Tarreauff011f22011-01-06 17:51:27 +01008653void free_http_req_rules(struct list *r) {
8654 struct http_req_rule *tr, *pr;
8655
8656 list_for_each_entry_safe(pr, tr, r, list) {
8657 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008658 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008659 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008660
8661 free(pr);
8662 }
8663}
8664
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008665/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008666struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8667{
8668 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008669 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008670 int cur_arg;
8671
8672 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8673 if (!rule) {
8674 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008675 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008676 }
8677
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008678 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008679 rule->action = HTTP_REQ_ACT_ALLOW;
8680 cur_arg = 1;
8681 } else if (!strcmp(args[0], "deny")) {
8682 rule->action = HTTP_REQ_ACT_DENY;
8683 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008684 } else if (!strcmp(args[0], "tarpit")) {
8685 rule->action = HTTP_REQ_ACT_TARPIT;
8686 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008687 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008688 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008689 cur_arg = 1;
8690
8691 while(*args[cur_arg]) {
8692 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008693 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008694 cur_arg+=2;
8695 continue;
8696 } else
8697 break;
8698 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008699 } else if (!strcmp(args[0], "set-nice")) {
8700 rule->action = HTTP_REQ_ACT_SET_NICE;
8701 cur_arg = 1;
8702
8703 if (!*args[cur_arg] ||
8704 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8705 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8706 file, linenum, args[0]);
8707 goto out_err;
8708 }
8709 rule->arg.nice = atoi(args[cur_arg]);
8710 if (rule->arg.nice < -1024)
8711 rule->arg.nice = -1024;
8712 else if (rule->arg.nice > 1024)
8713 rule->arg.nice = 1024;
8714 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008715 } else if (!strcmp(args[0], "set-tos")) {
8716#ifdef IP_TOS
8717 char *err;
8718 rule->action = HTTP_REQ_ACT_SET_TOS;
8719 cur_arg = 1;
8720
8721 if (!*args[cur_arg] ||
8722 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8723 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8724 file, linenum, args[0]);
8725 goto out_err;
8726 }
8727
8728 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8729 if (err && *err != '\0') {
8730 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8731 file, linenum, err, args[0]);
8732 goto out_err;
8733 }
8734 cur_arg++;
8735#else
8736 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8737 goto out_err;
8738#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008739 } else if (!strcmp(args[0], "set-mark")) {
8740#ifdef SO_MARK
8741 char *err;
8742 rule->action = HTTP_REQ_ACT_SET_MARK;
8743 cur_arg = 1;
8744
8745 if (!*args[cur_arg] ||
8746 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8747 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8748 file, linenum, args[0]);
8749 goto out_err;
8750 }
8751
8752 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8753 if (err && *err != '\0') {
8754 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8755 file, linenum, err, args[0]);
8756 goto out_err;
8757 }
8758 cur_arg++;
8759 global.last_checks |= LSTCHK_NETADM;
8760#else
8761 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8762 goto out_err;
8763#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008764 } else if (!strcmp(args[0], "set-log-level")) {
8765 rule->action = HTTP_REQ_ACT_SET_LOGL;
8766 cur_arg = 1;
8767
8768 if (!*args[cur_arg] ||
8769 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8770 bad_log_level:
8771 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8772 file, linenum, args[0]);
8773 goto out_err;
8774 }
8775 if (strcmp(args[cur_arg], "silent") == 0)
8776 rule->arg.loglevel = -1;
8777 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8778 goto bad_log_level;
8779 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008780 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8781 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8782 cur_arg = 1;
8783
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008784 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8785 (*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 +01008786 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8787 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008788 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008789 }
8790
8791 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8792 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8793 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008794
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008795 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008796 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008797 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8798 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008799 free(proxy->conf.lfs_file);
8800 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8801 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008802 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008803 } else if (strcmp(args[0], "del-header") == 0) {
8804 rule->action = HTTP_REQ_ACT_DEL_HDR;
8805 cur_arg = 1;
8806
8807 if (!*args[cur_arg] ||
8808 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8809 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8810 file, linenum, args[0]);
8811 goto out_err;
8812 }
8813
8814 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8815 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8816
8817 proxy->conf.args.ctx = ARGC_HRQ;
8818 free(proxy->conf.lfs_file);
8819 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8820 proxy->conf.lfs_line = proxy->conf.args.line;
8821 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008822 } else if (strcmp(args[0], "redirect") == 0) {
8823 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008824 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008825
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008826 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008827 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8828 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8829 goto out_err;
8830 }
8831
8832 /* this redirect rule might already contain a parsed condition which
8833 * we'll pass to the http-request rule.
8834 */
8835 rule->action = HTTP_REQ_ACT_REDIR;
8836 rule->arg.redir = redir;
8837 rule->cond = redir->cond;
8838 redir->cond = NULL;
8839 cur_arg = 2;
8840 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008841 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8842 /* http-request add-acl(<reference (acl name)>) <key pattern> */
8843 rule->action = HTTP_REQ_ACT_ADD_ACL;
8844 /*
8845 * '+ 8' for 'add-acl('
8846 * '- 9' for 'add-acl(' + trailing ')'
8847 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008848 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008849
8850 cur_arg = 1;
8851
8852 if (!*args[cur_arg] ||
8853 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8854 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8855 file, linenum, args[0]);
8856 goto out_err;
8857 }
8858
8859 LIST_INIT(&rule->arg.map.key);
8860 proxy->conf.args.ctx = ARGC_HRQ;
8861 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8862 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8863 file, linenum);
8864 free(proxy->conf.lfs_file);
8865 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8866 proxy->conf.lfs_line = proxy->conf.args.line;
8867 cur_arg += 1;
8868 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8869 /* http-request del-acl(<reference (acl name)>) <key pattern> */
8870 rule->action = HTTP_REQ_ACT_DEL_ACL;
8871 /*
8872 * '+ 8' for 'del-acl('
8873 * '- 9' for 'del-acl(' + trailing ')'
8874 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008875 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008876
8877 cur_arg = 1;
8878
8879 if (!*args[cur_arg] ||
8880 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8881 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8882 file, linenum, args[0]);
8883 goto out_err;
8884 }
8885
8886 LIST_INIT(&rule->arg.map.key);
8887 proxy->conf.args.ctx = ARGC_HRQ;
8888 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8889 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8890 file, linenum);
8891 free(proxy->conf.lfs_file);
8892 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8893 proxy->conf.lfs_line = proxy->conf.args.line;
8894 cur_arg += 1;
8895 } else if (strncmp(args[0], "del-map", 7) == 0) {
8896 /* http-request del-map(<reference (map name)>) <key pattern> */
8897 rule->action = HTTP_REQ_ACT_DEL_MAP;
8898 /*
8899 * '+ 8' for 'del-map('
8900 * '- 9' for 'del-map(' + trailing ')'
8901 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008902 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008903
8904 cur_arg = 1;
8905
8906 if (!*args[cur_arg] ||
8907 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8908 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8909 file, linenum, args[0]);
8910 goto out_err;
8911 }
8912
8913 LIST_INIT(&rule->arg.map.key);
8914 proxy->conf.args.ctx = ARGC_HRQ;
8915 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8916 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8917 file, linenum);
8918 free(proxy->conf.lfs_file);
8919 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8920 proxy->conf.lfs_line = proxy->conf.args.line;
8921 cur_arg += 1;
8922 } else if (strncmp(args[0], "set-map", 7) == 0) {
8923 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
8924 rule->action = HTTP_REQ_ACT_SET_MAP;
8925 /*
8926 * '+ 8' for 'set-map('
8927 * '- 9' for 'set-map(' + trailing ')'
8928 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008929 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008930
8931 cur_arg = 1;
8932
8933 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8934 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8935 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8936 file, linenum, args[0]);
8937 goto out_err;
8938 }
8939
8940 LIST_INIT(&rule->arg.map.key);
8941 LIST_INIT(&rule->arg.map.value);
8942 proxy->conf.args.ctx = ARGC_HRQ;
8943
8944 /* key pattern */
8945 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8946 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8947 file, linenum);
8948
8949 /* value pattern */
8950 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
8951 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8952 file, linenum);
8953 free(proxy->conf.lfs_file);
8954 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8955 proxy->conf.lfs_line = proxy->conf.args.line;
8956
8957 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008958 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8959 char *errmsg = NULL;
8960 cur_arg = 1;
8961 /* try in the module list */
8962 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
8963 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8964 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8965 free(errmsg);
8966 goto out_err;
8967 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008968 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008969 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 +01008970 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008971 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008972 }
8973
8974 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8975 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008976 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008977
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008978 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8979 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8980 file, linenum, args[0], errmsg);
8981 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008982 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008983 }
8984 rule->cond = cond;
8985 }
8986 else if (*args[cur_arg]) {
8987 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8988 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8989 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008990 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008991 }
8992
8993 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008994 out_err:
8995 free(rule);
8996 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008997}
8998
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008999/* parse an "http-respose" rule */
9000struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9001{
9002 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009003 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009004 int cur_arg;
9005
9006 rule = calloc(1, sizeof(*rule));
9007 if (!rule) {
9008 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9009 goto out_err;
9010 }
9011
9012 if (!strcmp(args[0], "allow")) {
9013 rule->action = HTTP_RES_ACT_ALLOW;
9014 cur_arg = 1;
9015 } else if (!strcmp(args[0], "deny")) {
9016 rule->action = HTTP_RES_ACT_DENY;
9017 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009018 } else if (!strcmp(args[0], "set-nice")) {
9019 rule->action = HTTP_RES_ACT_SET_NICE;
9020 cur_arg = 1;
9021
9022 if (!*args[cur_arg] ||
9023 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9024 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9025 file, linenum, args[0]);
9026 goto out_err;
9027 }
9028 rule->arg.nice = atoi(args[cur_arg]);
9029 if (rule->arg.nice < -1024)
9030 rule->arg.nice = -1024;
9031 else if (rule->arg.nice > 1024)
9032 rule->arg.nice = 1024;
9033 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009034 } else if (!strcmp(args[0], "set-tos")) {
9035#ifdef IP_TOS
9036 char *err;
9037 rule->action = HTTP_RES_ACT_SET_TOS;
9038 cur_arg = 1;
9039
9040 if (!*args[cur_arg] ||
9041 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9042 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9043 file, linenum, args[0]);
9044 goto out_err;
9045 }
9046
9047 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9048 if (err && *err != '\0') {
9049 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9050 file, linenum, err, args[0]);
9051 goto out_err;
9052 }
9053 cur_arg++;
9054#else
9055 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9056 goto out_err;
9057#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009058 } else if (!strcmp(args[0], "set-mark")) {
9059#ifdef SO_MARK
9060 char *err;
9061 rule->action = HTTP_RES_ACT_SET_MARK;
9062 cur_arg = 1;
9063
9064 if (!*args[cur_arg] ||
9065 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9066 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9067 file, linenum, args[0]);
9068 goto out_err;
9069 }
9070
9071 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9072 if (err && *err != '\0') {
9073 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9074 file, linenum, err, args[0]);
9075 goto out_err;
9076 }
9077 cur_arg++;
9078 global.last_checks |= LSTCHK_NETADM;
9079#else
9080 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9081 goto out_err;
9082#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009083 } else if (!strcmp(args[0], "set-log-level")) {
9084 rule->action = HTTP_RES_ACT_SET_LOGL;
9085 cur_arg = 1;
9086
9087 if (!*args[cur_arg] ||
9088 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9089 bad_log_level:
9090 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9091 file, linenum, args[0]);
9092 goto out_err;
9093 }
9094 if (strcmp(args[cur_arg], "silent") == 0)
9095 rule->arg.loglevel = -1;
9096 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9097 goto bad_log_level;
9098 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009099 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9100 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9101 cur_arg = 1;
9102
9103 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9104 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9105 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9106 file, linenum, args[0]);
9107 goto out_err;
9108 }
9109
9110 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9111 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9112 LIST_INIT(&rule->arg.hdr_add.fmt);
9113
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009114 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009115 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009116 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9117 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009118 free(proxy->conf.lfs_file);
9119 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9120 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009121 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009122 } else if (strcmp(args[0], "del-header") == 0) {
9123 rule->action = HTTP_RES_ACT_DEL_HDR;
9124 cur_arg = 1;
9125
9126 if (!*args[cur_arg] ||
9127 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9128 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9129 file, linenum, args[0]);
9130 goto out_err;
9131 }
9132
9133 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9134 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9135
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009136 proxy->conf.args.ctx = ARGC_HRS;
9137 free(proxy->conf.lfs_file);
9138 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9139 proxy->conf.lfs_line = proxy->conf.args.line;
9140 cur_arg += 1;
9141 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9142 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9143 rule->action = HTTP_RES_ACT_ADD_ACL;
9144 /*
9145 * '+ 8' for 'add-acl('
9146 * '- 9' for 'add-acl(' + trailing ')'
9147 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009148 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009149
9150 cur_arg = 1;
9151
9152 if (!*args[cur_arg] ||
9153 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9154 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9155 file, linenum, args[0]);
9156 goto out_err;
9157 }
9158
9159 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009160 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009161 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9162 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9163 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009164 free(proxy->conf.lfs_file);
9165 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9166 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009167
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009168 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009169 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9170 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9171 rule->action = HTTP_RES_ACT_DEL_ACL;
9172 /*
9173 * '+ 8' for 'del-acl('
9174 * '- 9' for 'del-acl(' + trailing ')'
9175 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009176 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009177
9178 cur_arg = 1;
9179
9180 if (!*args[cur_arg] ||
9181 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9182 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9183 file, linenum, args[0]);
9184 goto out_err;
9185 }
9186
9187 LIST_INIT(&rule->arg.map.key);
9188 proxy->conf.args.ctx = ARGC_HRS;
9189 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9190 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9191 file, linenum);
9192 free(proxy->conf.lfs_file);
9193 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9194 proxy->conf.lfs_line = proxy->conf.args.line;
9195 cur_arg += 1;
9196 } else if (strncmp(args[0], "del-map", 7) == 0) {
9197 /* http-response del-map(<reference (map name)>) <key pattern> */
9198 rule->action = HTTP_RES_ACT_DEL_MAP;
9199 /*
9200 * '+ 8' for 'del-map('
9201 * '- 9' for 'del-map(' + trailing ')'
9202 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009203 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009204
9205 cur_arg = 1;
9206
9207 if (!*args[cur_arg] ||
9208 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9209 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9210 file, linenum, args[0]);
9211 goto out_err;
9212 }
9213
9214 LIST_INIT(&rule->arg.map.key);
9215 proxy->conf.args.ctx = ARGC_HRS;
9216 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9217 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9218 file, linenum);
9219 free(proxy->conf.lfs_file);
9220 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9221 proxy->conf.lfs_line = proxy->conf.args.line;
9222 cur_arg += 1;
9223 } else if (strncmp(args[0], "set-map", 7) == 0) {
9224 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9225 rule->action = HTTP_RES_ACT_SET_MAP;
9226 /*
9227 * '+ 8' for 'set-map('
9228 * '- 9' for 'set-map(' + trailing ')'
9229 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009230 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009231
9232 cur_arg = 1;
9233
9234 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9235 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9236 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9237 file, linenum, args[0]);
9238 goto out_err;
9239 }
9240
9241 LIST_INIT(&rule->arg.map.key);
9242 LIST_INIT(&rule->arg.map.value);
9243
9244 proxy->conf.args.ctx = ARGC_HRS;
9245
9246 /* key pattern */
9247 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9248 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9249 file, linenum);
9250
9251 /* value pattern */
9252 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9253 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9254 file, linenum);
9255
9256 free(proxy->conf.lfs_file);
9257 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9258 proxy->conf.lfs_line = proxy->conf.args.line;
9259
9260 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009261 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9262 char *errmsg = NULL;
9263 cur_arg = 1;
9264 /* try in the module list */
9265 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9266 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9267 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9268 free(errmsg);
9269 goto out_err;
9270 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009271 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009272 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 +02009273 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9274 goto out_err;
9275 }
9276
9277 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9278 struct acl_cond *cond;
9279 char *errmsg = NULL;
9280
9281 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9282 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9283 file, linenum, args[0], errmsg);
9284 free(errmsg);
9285 goto out_err;
9286 }
9287 rule->cond = cond;
9288 }
9289 else if (*args[cur_arg]) {
9290 Alert("parsing [%s:%d]: 'http-response %s' expects"
9291 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9292 file, linenum, args[0], args[cur_arg]);
9293 goto out_err;
9294 }
9295
9296 return rule;
9297 out_err:
9298 free(rule);
9299 return NULL;
9300}
9301
Willy Tarreau4baae242012-12-27 12:00:31 +01009302/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009303 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9304 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009305 */
9306struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009307 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009308{
9309 struct redirect_rule *rule;
9310 int cur_arg;
9311 int type = REDIRECT_TYPE_NONE;
9312 int code = 302;
9313 const char *destination = NULL;
9314 const char *cookie = NULL;
9315 int cookie_set = 0;
9316 unsigned int flags = REDIRECT_FLAG_NONE;
9317 struct acl_cond *cond = NULL;
9318
9319 cur_arg = 0;
9320 while (*(args[cur_arg])) {
9321 if (strcmp(args[cur_arg], "location") == 0) {
9322 if (!*args[cur_arg + 1])
9323 goto missing_arg;
9324
9325 type = REDIRECT_TYPE_LOCATION;
9326 cur_arg++;
9327 destination = args[cur_arg];
9328 }
9329 else if (strcmp(args[cur_arg], "prefix") == 0) {
9330 if (!*args[cur_arg + 1])
9331 goto missing_arg;
9332
9333 type = REDIRECT_TYPE_PREFIX;
9334 cur_arg++;
9335 destination = args[cur_arg];
9336 }
9337 else if (strcmp(args[cur_arg], "scheme") == 0) {
9338 if (!*args[cur_arg + 1])
9339 goto missing_arg;
9340
9341 type = REDIRECT_TYPE_SCHEME;
9342 cur_arg++;
9343 destination = args[cur_arg];
9344 }
9345 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9346 if (!*args[cur_arg + 1])
9347 goto missing_arg;
9348
9349 cur_arg++;
9350 cookie = args[cur_arg];
9351 cookie_set = 1;
9352 }
9353 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9354 if (!*args[cur_arg + 1])
9355 goto missing_arg;
9356
9357 cur_arg++;
9358 cookie = args[cur_arg];
9359 cookie_set = 0;
9360 }
9361 else if (strcmp(args[cur_arg], "code") == 0) {
9362 if (!*args[cur_arg + 1])
9363 goto missing_arg;
9364
9365 cur_arg++;
9366 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009367 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009368 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009369 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009370 args[cur_arg - 1], args[cur_arg]);
9371 return NULL;
9372 }
9373 }
9374 else if (!strcmp(args[cur_arg],"drop-query")) {
9375 flags |= REDIRECT_FLAG_DROP_QS;
9376 }
9377 else if (!strcmp(args[cur_arg],"append-slash")) {
9378 flags |= REDIRECT_FLAG_APPEND_SLASH;
9379 }
9380 else if (strcmp(args[cur_arg], "if") == 0 ||
9381 strcmp(args[cur_arg], "unless") == 0) {
9382 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9383 if (!cond) {
9384 memprintf(errmsg, "error in condition: %s", *errmsg);
9385 return NULL;
9386 }
9387 break;
9388 }
9389 else {
9390 memprintf(errmsg,
9391 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9392 args[cur_arg]);
9393 return NULL;
9394 }
9395 cur_arg++;
9396 }
9397
9398 if (type == REDIRECT_TYPE_NONE) {
9399 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9400 return NULL;
9401 }
9402
9403 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9404 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009405 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009406
9407 if (!use_fmt) {
9408 /* old-style static redirect rule */
9409 rule->rdr_str = strdup(destination);
9410 rule->rdr_len = strlen(destination);
9411 }
9412 else {
9413 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009414
9415 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9416 * if prefix == "/", we don't want to add anything, otherwise it
9417 * makes it hard for the user to configure a self-redirection.
9418 */
9419 proxy->conf.args.ctx = ARGC_RDR;
9420 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009421 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009422 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9423 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009424 free(curproxy->conf.lfs_file);
9425 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9426 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009427 }
9428 }
9429
Willy Tarreau4baae242012-12-27 12:00:31 +01009430 if (cookie) {
9431 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9432 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9433 */
9434 rule->cookie_len = strlen(cookie);
9435 if (cookie_set) {
9436 rule->cookie_str = malloc(rule->cookie_len + 10);
9437 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9438 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9439 rule->cookie_len += 9;
9440 } else {
9441 rule->cookie_str = malloc(rule->cookie_len + 21);
9442 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9443 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9444 rule->cookie_len += 20;
9445 }
9446 }
9447 rule->type = type;
9448 rule->code = code;
9449 rule->flags = flags;
9450 LIST_INIT(&rule->list);
9451 return rule;
9452
9453 missing_arg:
9454 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9455 return NULL;
9456}
9457
Willy Tarreau8797c062007-05-07 00:55:35 +02009458/************************************************************************/
9459/* The code below is dedicated to ACL parsing and matching */
9460/************************************************************************/
9461
9462
Willy Tarreau14174bc2012-04-16 14:34:04 +02009463/* This function ensures that the prerequisites for an L7 fetch are ready,
9464 * which means that a request or response is ready. If some data is missing,
9465 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009466 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9467 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009468 *
9469 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009470 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9471 * decide whether or not an HTTP message is present ;
9472 * 0 if the requested data cannot be fetched or if it is certain that
9473 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009474 * 1 if an HTTP message is ready
9475 */
9476static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009477smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009478 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009479{
9480 struct http_txn *txn = l7;
9481 struct http_msg *msg = &txn->req;
9482
9483 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9484 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9485 */
9486
9487 if (unlikely(!s || !txn))
9488 return 0;
9489
9490 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009491 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009492
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009493 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009494 if (unlikely(!s->req))
9495 return 0;
9496
Willy Tarreauaae75e32013-03-29 12:31:49 +01009497 /* If the buffer does not leave enough free space at the end,
9498 * we must first realign it.
9499 */
9500 if (s->req->buf->p > s->req->buf->data &&
9501 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9502 buffer_slow_realign(s->req->buf);
9503
Willy Tarreau14174bc2012-04-16 14:34:04 +02009504 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009505 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009506 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009507
9508 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009509 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009510 http_msg_analyzer(msg, &txn->hdr_idx);
9511
9512 /* Still no valid request ? */
9513 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009514 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009515 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009516 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009517 }
9518 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009519 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009520 return 0;
9521 }
9522
9523 /* OK we just got a valid HTTP request. We have some minor
9524 * preparation to perform so that further checks can rely
9525 * on HTTP tests.
9526 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009527
9528 /* If the request was parsed but was too large, we must absolutely
9529 * return an error so that it is not processed. At the moment this
9530 * cannot happen, but if the parsers are to change in the future,
9531 * we want this check to be maintained.
9532 */
9533 if (unlikely(s->req->buf->i + s->req->buf->p >
9534 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9535 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009536 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009537 return 1;
9538 }
9539
Willy Tarreau9b28e032012-10-12 23:49:43 +02009540 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009541 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9542 s->flags |= SN_REDIRECTABLE;
9543
Willy Tarreau506d0502013-07-06 13:29:24 +02009544 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9545 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009546 }
9547
Willy Tarreau506d0502013-07-06 13:29:24 +02009548 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009549 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009550 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009551
9552 /* otherwise everything's ready for the request */
9553 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009554 else {
9555 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009556 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9557 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009558 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009559 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009560 }
9561
9562 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009563 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009564 return 1;
9565}
Willy Tarreau8797c062007-05-07 00:55:35 +02009566
Willy Tarreauc0239e02012-04-16 14:42:55 +02009567#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009568 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 +02009569
Willy Tarreau24e32d82012-04-23 23:55:44 +02009570#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009571 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 +02009572
Willy Tarreau8797c062007-05-07 00:55:35 +02009573
9574/* 1. Check on METHOD
9575 * We use the pre-parsed method if it is known, and store its number as an
9576 * integer. If it is unknown, we use the pointer and the length.
9577 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009578static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009579{
9580 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009581 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009582
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009583 len = strlen(text);
9584 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009585
9586 pattern->val.i = meth;
9587 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009588 trash = get_trash_chunk();
9589 if (trash->size < len) {
9590 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9591 len, trash->size);
9592 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009593 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009594 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009595 pattern->len = len;
9596 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009597 else {
9598 pattern->ptr.str = NULL;
9599 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009600 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009601 return 1;
9602}
9603
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009604/* This function fetches the method of current HTTP request and stores
9605 * it in the global pattern struct as a chunk. There are two possibilities :
9606 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9607 * in <len> and <ptr> is NULL ;
9608 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9609 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009610 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009611 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009612static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009613smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009614 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009615{
9616 int meth;
9617 struct http_txn *txn = l7;
9618
Willy Tarreau24e32d82012-04-23 23:55:44 +02009619 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009620
Willy Tarreau8797c062007-05-07 00:55:35 +02009621 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009622 smp->type = SMP_T_METH;
9623 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009624 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009625 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9626 /* ensure the indexes are not affected */
9627 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009628 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009629 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9630 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009631 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009632 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009633 return 1;
9634}
9635
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009636/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009637static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009638{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009639 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009640 struct pattern_list *lst;
9641 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009642
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009643 list_for_each_entry(lst, &expr->patterns, list) {
9644 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009645
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009646 /* well-known method */
9647 if (pattern->val.i != HTTP_METH_OTHER) {
9648 if (smp->data.meth.meth == pattern->val.i)
9649 return pattern;
9650 else
9651 continue;
9652 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009653
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009654 /* Other method, we must compare the strings */
9655 if (pattern->len != smp->data.meth.str.len)
9656 continue;
9657
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009658 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009659 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9660 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9661 return pattern;
9662 }
9663 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009664}
9665
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009666static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009667smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009668 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009669{
9670 struct http_txn *txn = l7;
9671 char *ptr;
9672 int len;
9673
Willy Tarreauc0239e02012-04-16 14:42:55 +02009674 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009675
Willy Tarreau8797c062007-05-07 00:55:35 +02009676 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009677 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009678
9679 while ((len-- > 0) && (*ptr++ != '/'));
9680 if (len <= 0)
9681 return 0;
9682
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009683 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009684 smp->data.str.str = ptr;
9685 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009686
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009687 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009688 return 1;
9689}
9690
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009691static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009692smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009693 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009694{
9695 struct http_txn *txn = l7;
9696 char *ptr;
9697 int len;
9698
Willy Tarreauc0239e02012-04-16 14:42:55 +02009699 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009700
Willy Tarreauf26b2522012-12-14 08:33:14 +01009701 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9702 return 0;
9703
Willy Tarreau8797c062007-05-07 00:55:35 +02009704 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009705 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009706
9707 while ((len-- > 0) && (*ptr++ != '/'));
9708 if (len <= 0)
9709 return 0;
9710
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009711 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009712 smp->data.str.str = ptr;
9713 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009714
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009715 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009716 return 1;
9717}
9718
9719/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009720static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009721smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009722 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009723{
9724 struct http_txn *txn = l7;
9725 char *ptr;
9726 int len;
9727
Willy Tarreauc0239e02012-04-16 14:42:55 +02009728 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009729
Willy Tarreauf26b2522012-12-14 08:33:14 +01009730 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9731 return 0;
9732
Willy Tarreau8797c062007-05-07 00:55:35 +02009733 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009734 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009735
Willy Tarreauf853c462012-04-23 18:53:56 +02009736 smp->type = SMP_T_UINT;
9737 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009738 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009739 return 1;
9740}
9741
9742/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009743static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009744smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009745 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009746{
9747 struct http_txn *txn = l7;
9748
Willy Tarreauc0239e02012-04-16 14:42:55 +02009749 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009750
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009751 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009752 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009753 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009754 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009755 return 1;
9756}
9757
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009758static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009759smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009760 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009761{
9762 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009763 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009764
Willy Tarreauc0239e02012-04-16 14:42:55 +02009765 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009766
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009767 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 +02009768 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009769 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009770
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009771 smp->type = SMP_T_IPV4;
9772 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009773 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009774 return 1;
9775}
9776
9777static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009778smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009779 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009780{
9781 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009782 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009783
Willy Tarreauc0239e02012-04-16 14:42:55 +02009784 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009785
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009786 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 +02009787 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9788 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009789
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009790 smp->type = SMP_T_UINT;
9791 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009792 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009793 return 1;
9794}
9795
Willy Tarreau185b5c42012-04-26 15:11:51 +02009796/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9797 * Accepts an optional argument of type string containing the header field name,
9798 * and an optional argument of type signed or unsigned integer to request an
9799 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009800 * headers are considered from the first one. It does not stop on commas and
9801 * returns full lines instead (useful for User-Agent or Date for example).
9802 */
9803static int
9804smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009805 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009806{
9807 struct http_txn *txn = l7;
9808 struct hdr_idx *idx = &txn->hdr_idx;
9809 struct hdr_ctx *ctx = smp->ctx.a[0];
9810 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9811 int occ = 0;
9812 const char *name_str = NULL;
9813 int name_len = 0;
9814
9815 if (!ctx) {
9816 /* first call */
9817 ctx = &static_hdr_ctx;
9818 ctx->idx = 0;
9819 smp->ctx.a[0] = ctx;
9820 }
9821
9822 if (args) {
9823 if (args[0].type != ARGT_STR)
9824 return 0;
9825 name_str = args[0].data.str.str;
9826 name_len = args[0].data.str.len;
9827
9828 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9829 occ = args[1].data.uint;
9830 }
9831
9832 CHECK_HTTP_MESSAGE_FIRST();
9833
9834 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9835 /* search for header from the beginning */
9836 ctx->idx = 0;
9837
9838 if (!occ && !(opt & SMP_OPT_ITERATE))
9839 /* no explicit occurrence and single fetch => last header by default */
9840 occ = -1;
9841
9842 if (!occ)
9843 /* prepare to report multiple occurrences for ACL fetches */
9844 smp->flags |= SMP_F_NOT_LAST;
9845
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009846 smp->type = SMP_T_STR;
9847 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009848 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9849 return 1;
9850
9851 smp->flags &= ~SMP_F_NOT_LAST;
9852 return 0;
9853}
9854
9855/* 6. Check on HTTP header count. The number of occurrences is returned.
9856 * Accepts exactly 1 argument of type string. It does not stop on commas and
9857 * returns full lines instead (useful for User-Agent or Date for example).
9858 */
9859static int
9860smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009861 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009862{
9863 struct http_txn *txn = l7;
9864 struct hdr_idx *idx = &txn->hdr_idx;
9865 struct hdr_ctx ctx;
9866 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9867 int cnt;
9868
9869 if (!args || args->type != ARGT_STR)
9870 return 0;
9871
9872 CHECK_HTTP_MESSAGE_FIRST();
9873
9874 ctx.idx = 0;
9875 cnt = 0;
9876 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9877 cnt++;
9878
9879 smp->type = SMP_T_UINT;
9880 smp->data.uint = cnt;
9881 smp->flags = SMP_F_VOL_HDR;
9882 return 1;
9883}
9884
9885/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9886 * Accepts an optional argument of type string containing the header field name,
9887 * and an optional argument of type signed or unsigned integer to request an
9888 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009889 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009890 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009891static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009892smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009893 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009894{
9895 struct http_txn *txn = l7;
9896 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009897 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009898 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009899 int occ = 0;
9900 const char *name_str = NULL;
9901 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009902
Willy Tarreaua890d072013-04-02 12:01:06 +02009903 if (!ctx) {
9904 /* first call */
9905 ctx = &static_hdr_ctx;
9906 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009907 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009908 }
9909
Willy Tarreau185b5c42012-04-26 15:11:51 +02009910 if (args) {
9911 if (args[0].type != ARGT_STR)
9912 return 0;
9913 name_str = args[0].data.str.str;
9914 name_len = args[0].data.str.len;
9915
9916 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9917 occ = args[1].data.uint;
9918 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009919
Willy Tarreaue333ec92012-04-16 16:26:40 +02009920 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009921
Willy Tarreau185b5c42012-04-26 15:11:51 +02009922 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009923 /* search for header from the beginning */
9924 ctx->idx = 0;
9925
Willy Tarreau185b5c42012-04-26 15:11:51 +02009926 if (!occ && !(opt & SMP_OPT_ITERATE))
9927 /* no explicit occurrence and single fetch => last header by default */
9928 occ = -1;
9929
9930 if (!occ)
9931 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009932 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009933
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009934 smp->type = SMP_T_STR;
9935 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009936 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 +02009937 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009938
Willy Tarreau37406352012-04-23 16:16:37 +02009939 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009940 return 0;
9941}
9942
Willy Tarreauc11416f2007-06-17 16:58:38 +02009943/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009944 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009945 */
9946static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009947smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009948 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009949{
9950 struct http_txn *txn = l7;
9951 struct hdr_idx *idx = &txn->hdr_idx;
9952 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009953 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009954 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009955
Willy Tarreau24e32d82012-04-23 23:55:44 +02009956 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009957 return 0;
9958
Willy Tarreaue333ec92012-04-16 16:26:40 +02009959 CHECK_HTTP_MESSAGE_FIRST();
9960
Willy Tarreau33a7e692007-06-10 19:45:56 +02009961 ctx.idx = 0;
9962 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009963 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 +02009964 cnt++;
9965
Willy Tarreauf853c462012-04-23 18:53:56 +02009966 smp->type = SMP_T_UINT;
9967 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009968 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009969 return 1;
9970}
9971
Willy Tarreau185b5c42012-04-26 15:11:51 +02009972/* Fetch an HTTP header's integer value. The integer value is returned. It
9973 * takes a mandatory argument of type string and an optional one of type int
9974 * to designate a specific occurrence. It returns an unsigned integer, which
9975 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009976 */
9977static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009978smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009979 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009980{
Willy Tarreauef38c392013-07-22 16:29:32 +02009981 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009982
Willy Tarreauf853c462012-04-23 18:53:56 +02009983 if (ret > 0) {
9984 smp->type = SMP_T_UINT;
9985 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9986 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009987
Willy Tarreaud53e2422012-04-16 17:21:11 +02009988 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009989}
9990
Cyril Bonté69fa9922012-10-25 00:01:06 +02009991/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9992 * and an optional one of type int to designate a specific occurrence.
9993 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009994 */
9995static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009996smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009997 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009998{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009999 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010000
Willy Tarreauef38c392013-07-22 16:29:32 +020010001 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010002 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10003 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010004 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010005 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010006 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010007 if (smp->data.str.len < temp->size - 1) {
10008 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10009 temp->str[smp->data.str.len] = '\0';
10010 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10011 smp->type = SMP_T_IPV6;
10012 break;
10013 }
10014 }
10015 }
10016
Willy Tarreaud53e2422012-04-16 17:21:11 +020010017 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010018 if (!(smp->flags & SMP_F_NOT_LAST))
10019 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010020 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010021 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010022}
10023
Willy Tarreau737b0c12007-06-10 21:28:46 +020010024/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10025 * the first '/' after the possible hostname, and ends before the possible '?'.
10026 */
10027static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010028smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010029 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010030{
10031 struct http_txn *txn = l7;
10032 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010033
Willy Tarreauc0239e02012-04-16 14:42:55 +020010034 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010035
Willy Tarreau9b28e032012-10-12 23:49:43 +020010036 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010037 ptr = http_get_path(txn);
10038 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010039 return 0;
10040
10041 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010042 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010043 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010044
10045 while (ptr < end && *ptr != '?')
10046 ptr++;
10047
Willy Tarreauf853c462012-04-23 18:53:56 +020010048 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010049 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010050 return 1;
10051}
10052
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010053/* This produces a concatenation of the first occurrence of the Host header
10054 * followed by the path component if it begins with a slash ('/'). This means
10055 * that '*' will not be added, resulting in exactly the first Host entry.
10056 * If no Host header is found, then the path is returned as-is. The returned
10057 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010058 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010059 */
10060static int
10061smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010062 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010063{
10064 struct http_txn *txn = l7;
10065 char *ptr, *end, *beg;
10066 struct hdr_ctx ctx;
10067
10068 CHECK_HTTP_MESSAGE_FIRST();
10069
10070 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010071 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010072 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010073
10074 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010075 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010076 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010077 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010078 smp->data.str.len = ctx.vlen;
10079
10080 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010081 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010082 beg = http_get_path(txn);
10083 if (!beg)
10084 beg = end;
10085
10086 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10087
10088 if (beg < ptr && *beg == '/') {
10089 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10090 smp->data.str.len += ptr - beg;
10091 }
10092
10093 smp->flags = SMP_F_VOL_1ST;
10094 return 1;
10095}
10096
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010097/* This produces a 32-bit hash of the concatenation of the first occurrence of
10098 * the Host header followed by the path component if it begins with a slash ('/').
10099 * This means that '*' will not be added, resulting in exactly the first Host
10100 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010101 * is hashed using the path hash followed by a full avalanche hash and provides a
10102 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010103 * high-traffic sites without having to store whole paths.
10104 */
10105static int
10106smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010107 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010108{
10109 struct http_txn *txn = l7;
10110 struct hdr_ctx ctx;
10111 unsigned int hash = 0;
10112 char *ptr, *beg, *end;
10113 int len;
10114
10115 CHECK_HTTP_MESSAGE_FIRST();
10116
10117 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010118 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010119 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10120 ptr = ctx.line + ctx.val;
10121 len = ctx.vlen;
10122 while (len--)
10123 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10124 }
10125
10126 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010127 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010128 beg = http_get_path(txn);
10129 if (!beg)
10130 beg = end;
10131
10132 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10133
10134 if (beg < ptr && *beg == '/') {
10135 while (beg < ptr)
10136 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10137 }
10138 hash = full_hash(hash);
10139
10140 smp->type = SMP_T_UINT;
10141 smp->data.uint = hash;
10142 smp->flags = SMP_F_VOL_1ST;
10143 return 1;
10144}
10145
Willy Tarreau4a550602012-12-09 14:53:32 +010010146/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010147 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10148 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10149 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010150 * that in environments where IPv6 is insignificant, truncating the output to
10151 * 8 bytes would still work.
10152 */
10153static int
10154smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010155 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010156{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010157 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010158 struct connection *cli_conn = objt_conn(l4->si[0].end);
10159
10160 if (!cli_conn)
10161 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010162
Willy Tarreauef38c392013-07-22 16:29:32 +020010163 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010164 return 0;
10165
Willy Tarreau47ca5452012-12-23 20:22:19 +010010166 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +010010167 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10168 temp->len += sizeof(smp->data.uint);
10169
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010170 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010171 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010172 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010173 temp->len += 4;
10174 break;
10175 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010176 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010177 temp->len += 16;
10178 break;
10179 default:
10180 return 0;
10181 }
10182
10183 smp->data.str = *temp;
10184 smp->type = SMP_T_BIN;
10185 return 1;
10186}
10187
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010188static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010189smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010190 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010191{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010192 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10193 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10194 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010195
Willy Tarreau24e32d82012-04-23 23:55:44 +020010196 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010197
Willy Tarreauf853c462012-04-23 18:53:56 +020010198 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010199 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010200 return 1;
10201}
10202
Willy Tarreau7f18e522010-10-22 20:04:13 +020010203/* return a valid test if the current request is the first one on the connection */
10204static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010205smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010206 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010207{
10208 if (!s)
10209 return 0;
10210
Willy Tarreauf853c462012-04-23 18:53:56 +020010211 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010212 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010213 return 1;
10214}
10215
Willy Tarreau34db1082012-04-19 17:16:54 +020010216/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010217static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010218smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010219 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010220{
10221
Willy Tarreau24e32d82012-04-23 23:55:44 +020010222 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010223 return 0;
10224
Willy Tarreauc0239e02012-04-16 14:42:55 +020010225 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010226
Willy Tarreauc0239e02012-04-16 14:42:55 +020010227 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010228 return 0;
10229
Willy Tarreauf853c462012-04-23 18:53:56 +020010230 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010231 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010232 return 1;
10233}
Willy Tarreau8797c062007-05-07 00:55:35 +020010234
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010235/* Accepts exactly 1 argument of type userlist */
10236static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010237smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010238 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010239{
10240
10241 if (!args || args->type != ARGT_USR)
10242 return 0;
10243
10244 CHECK_HTTP_MESSAGE_FIRST();
10245
10246 if (!get_http_auth(l4))
10247 return 0;
10248
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010249 /* if the user does not belong to the userlist or has a wrong password,
10250 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010251 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010252 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010253 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10254 return 0;
10255
10256 /* pat_match_auth() will need the user list */
10257 smp->ctx.a[0] = args->data.usr;
10258
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010259 smp->type = SMP_T_STR;
10260 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010261 smp->data.str.str = l4->txn.auth.user;
10262 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010263
10264 return 1;
10265}
10266
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010267/* Try to find the next occurrence of a cookie name in a cookie header value.
10268 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10269 * the cookie value is returned into *value and *value_l, and the function
10270 * returns a pointer to the next pointer to search from if the value was found.
10271 * Otherwise if the cookie was not found, NULL is returned and neither value
10272 * nor value_l are touched. The input <hdr> string should first point to the
10273 * header's value, and the <hdr_end> pointer must point to the first character
10274 * not part of the value. <list> must be non-zero if value may represent a list
10275 * of values (cookie headers). This makes it faster to abort parsing when no
10276 * list is expected.
10277 */
10278static char *
10279extract_cookie_value(char *hdr, const char *hdr_end,
10280 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010281 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010282{
10283 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10284 char *next;
10285
10286 /* we search at least a cookie name followed by an equal, and more
10287 * generally something like this :
10288 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10289 */
10290 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10291 /* Iterate through all cookies on this line */
10292
10293 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10294 att_beg++;
10295
10296 /* find att_end : this is the first character after the last non
10297 * space before the equal. It may be equal to hdr_end.
10298 */
10299 equal = att_end = att_beg;
10300
10301 while (equal < hdr_end) {
10302 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10303 break;
10304 if (http_is_spht[(unsigned char)*equal++])
10305 continue;
10306 att_end = equal;
10307 }
10308
10309 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10310 * is between <att_beg> and <equal>, both may be identical.
10311 */
10312
10313 /* look for end of cookie if there is an equal sign */
10314 if (equal < hdr_end && *equal == '=') {
10315 /* look for the beginning of the value */
10316 val_beg = equal + 1;
10317 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10318 val_beg++;
10319
10320 /* find the end of the value, respecting quotes */
10321 next = find_cookie_value_end(val_beg, hdr_end);
10322
10323 /* make val_end point to the first white space or delimitor after the value */
10324 val_end = next;
10325 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10326 val_end--;
10327 } else {
10328 val_beg = val_end = next = equal;
10329 }
10330
10331 /* We have nothing to do with attributes beginning with '$'. However,
10332 * they will automatically be removed if a header before them is removed,
10333 * since they're supposed to be linked together.
10334 */
10335 if (*att_beg == '$')
10336 continue;
10337
10338 /* Ignore cookies with no equal sign */
10339 if (equal == next)
10340 continue;
10341
10342 /* Now we have the cookie name between att_beg and att_end, and
10343 * its value between val_beg and val_end.
10344 */
10345
10346 if (att_end - att_beg == cookie_name_l &&
10347 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10348 /* let's return this value and indicate where to go on from */
10349 *value = val_beg;
10350 *value_l = val_end - val_beg;
10351 return next + 1;
10352 }
10353
10354 /* Set-Cookie headers only have the name in the first attr=value part */
10355 if (!list)
10356 break;
10357 }
10358
10359 return NULL;
10360}
10361
William Lallemanda43ba4e2014-01-28 18:14:25 +010010362/* Fetch a captured HTTP request header. The index is the position of
10363 * the "capture" option in the configuration file
10364 */
10365static int
10366smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10367 const struct arg *args, struct sample *smp, const char *kw)
10368{
10369 struct proxy *fe = l4->fe;
10370 struct http_txn *txn = l7;
10371 int idx;
10372
10373 if (!args || args->type != ARGT_UINT)
10374 return 0;
10375
10376 idx = args->data.uint;
10377
10378 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10379 return 0;
10380
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010381 smp->type = SMP_T_STR;
10382 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010383 smp->data.str.str = txn->req.cap[idx];
10384 smp->data.str.len = strlen(txn->req.cap[idx]);
10385
10386 return 1;
10387}
10388
10389/* Fetch a captured HTTP response header. The index is the position of
10390 * the "capture" option in the configuration file
10391 */
10392static int
10393smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10394 const struct arg *args, struct sample *smp, const char *kw)
10395{
10396 struct proxy *fe = l4->fe;
10397 struct http_txn *txn = l7;
10398 int idx;
10399
10400 if (!args || args->type != ARGT_UINT)
10401 return 0;
10402
10403 idx = args->data.uint;
10404
10405 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10406 return 0;
10407
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010408 smp->type = SMP_T_STR;
10409 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010410 smp->data.str.str = txn->rsp.cap[idx];
10411 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10412
10413 return 1;
10414}
10415
William Lallemand65ad6e12014-01-31 15:08:02 +010010416/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10417static int
10418smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10419 const struct arg *args, struct sample *smp, const char *kw)
10420{
10421 struct chunk *temp;
10422 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010423 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010424
10425 if (!txn->uri)
10426 return 0;
10427
William Lallemand96a77852014-02-05 00:30:02 +010010428 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010429
William Lallemand96a77852014-02-05 00:30:02 +010010430 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10431 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010432
William Lallemand96a77852014-02-05 00:30:02 +010010433 temp = get_trash_chunk();
10434 temp->str = txn->uri;
10435 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010436 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010437 smp->type = SMP_T_STR;
10438 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010439
10440 return 1;
10441
10442}
10443
10444/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10445static int
10446smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10447 const struct arg *args, struct sample *smp, const char *kw)
10448{
10449 struct chunk *temp;
10450 struct http_txn *txn = l7;
10451 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010452
10453 if (!txn->uri)
10454 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010455
William Lallemand65ad6e12014-01-31 15:08:02 +010010456 ptr = txn->uri;
10457
10458 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10459 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010460
William Lallemand65ad6e12014-01-31 15:08:02 +010010461 if (!*ptr)
10462 return 0;
10463
10464 ptr++; /* skip the space */
10465
10466 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010467 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010468 if (!ptr)
10469 return 0;
10470 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10471 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010472
10473 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010474 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010475 smp->type = SMP_T_STR;
10476 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010477
10478 return 1;
10479}
10480
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010481/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10482 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10483 */
10484static int
10485smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10486 const struct arg *args, struct sample *smp, const char *kw)
10487{
10488 struct http_txn *txn = l7;
10489
10490 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10491 return 0;
10492
10493 if (txn->req.flags & HTTP_MSGF_VER_11)
10494 smp->data.str.str = "HTTP/1.1";
10495 else
10496 smp->data.str.str = "HTTP/1.0";
10497
10498 smp->data.str.len = 8;
10499 smp->type = SMP_T_STR;
10500 smp->flags = SMP_F_CONST;
10501 return 1;
10502
10503}
10504
10505/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10506 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10507 */
10508static int
10509smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10510 const struct arg *args, struct sample *smp, const char *kw)
10511{
10512 struct http_txn *txn = l7;
10513
10514 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10515 return 0;
10516
10517 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10518 smp->data.str.str = "HTTP/1.1";
10519 else
10520 smp->data.str.str = "HTTP/1.0";
10521
10522 smp->data.str.len = 8;
10523 smp->type = SMP_T_STR;
10524 smp->flags = SMP_F_CONST;
10525 return 1;
10526
10527}
10528
William Lallemand65ad6e12014-01-31 15:08:02 +010010529
Willy Tarreaue333ec92012-04-16 16:26:40 +020010530/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010531 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010532 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010533 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010534 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010535 * Accepts exactly 1 argument of type string. If the input options indicate
10536 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010537 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010538 */
10539static int
Willy Tarreau51539362012-05-08 12:46:28 +020010540smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010541 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010542{
10543 struct http_txn *txn = l7;
10544 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010545 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010546 const struct http_msg *msg;
10547 const char *hdr_name;
10548 int hdr_name_len;
10549 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010550 int occ = 0;
10551 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010552
Willy Tarreau24e32d82012-04-23 23:55:44 +020010553 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010554 return 0;
10555
Willy Tarreaua890d072013-04-02 12:01:06 +020010556 if (!ctx) {
10557 /* first call */
10558 ctx = &static_hdr_ctx;
10559 ctx->idx = 0;
10560 smp->ctx.a[2] = ctx;
10561 }
10562
Willy Tarreaue333ec92012-04-16 16:26:40 +020010563 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010564
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010565 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010566 msg = &txn->req;
10567 hdr_name = "Cookie";
10568 hdr_name_len = 6;
10569 } else {
10570 msg = &txn->rsp;
10571 hdr_name = "Set-Cookie";
10572 hdr_name_len = 10;
10573 }
10574
Willy Tarreau28376d62012-04-26 21:26:10 +020010575 if (!occ && !(opt & SMP_OPT_ITERATE))
10576 /* no explicit occurrence and single fetch => last cookie by default */
10577 occ = -1;
10578
10579 /* OK so basically here, either we want only one value and it's the
10580 * last one, or we want to iterate over all of them and we fetch the
10581 * next one.
10582 */
10583
Willy Tarreau9b28e032012-10-12 23:49:43 +020010584 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010585 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010586 /* search for the header from the beginning, we must first initialize
10587 * the search parameters.
10588 */
Willy Tarreau37406352012-04-23 16:16:37 +020010589 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010590 ctx->idx = 0;
10591 }
10592
Willy Tarreau28376d62012-04-26 21:26:10 +020010593 smp->flags |= SMP_F_VOL_HDR;
10594
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010595 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010596 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10597 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010598 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10599 goto out;
10600
Willy Tarreau24e32d82012-04-23 23:55:44 +020010601 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010602 continue;
10603
Willy Tarreau37406352012-04-23 16:16:37 +020010604 smp->ctx.a[0] = ctx->line + ctx->val;
10605 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010606 }
10607
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010608 smp->type = SMP_T_STR;
10609 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010610 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010611 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010612 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010613 &smp->data.str.str,
10614 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010615 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010616 found = 1;
10617 if (occ >= 0) {
10618 /* one value was returned into smp->data.str.{str,len} */
10619 smp->flags |= SMP_F_NOT_LAST;
10620 return 1;
10621 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010622 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010623 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010624 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010625 /* all cookie headers and values were scanned. If we're looking for the
10626 * last occurrence, we may return it now.
10627 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010628 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010629 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010630 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010631}
10632
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010633/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010634 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010635 * multiple cookies may be parsed on the same line. The returned sample is of
10636 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010637 */
10638static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010639smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010640 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010641{
10642 struct http_txn *txn = l7;
10643 struct hdr_idx *idx = &txn->hdr_idx;
10644 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010645 const struct http_msg *msg;
10646 const char *hdr_name;
10647 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010648 int cnt;
10649 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010650 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010651
Willy Tarreau24e32d82012-04-23 23:55:44 +020010652 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010653 return 0;
10654
Willy Tarreaue333ec92012-04-16 16:26:40 +020010655 CHECK_HTTP_MESSAGE_FIRST();
10656
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010657 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010658 msg = &txn->req;
10659 hdr_name = "Cookie";
10660 hdr_name_len = 6;
10661 } else {
10662 msg = &txn->rsp;
10663 hdr_name = "Set-Cookie";
10664 hdr_name_len = 10;
10665 }
10666
Willy Tarreau9b28e032012-10-12 23:49:43 +020010667 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010668 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010669 ctx.idx = 0;
10670 cnt = 0;
10671
10672 while (1) {
10673 /* Note: val_beg == NULL every time we need to fetch a new header */
10674 if (!val_beg) {
10675 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10676 break;
10677
Willy Tarreau24e32d82012-04-23 23:55:44 +020010678 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010679 continue;
10680
10681 val_beg = ctx.line + ctx.val;
10682 val_end = val_beg + ctx.vlen;
10683 }
10684
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010685 smp->type = SMP_T_STR;
10686 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010687 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010688 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010689 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010690 &smp->data.str.str,
10691 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010692 cnt++;
10693 }
10694 }
10695
Willy Tarreaub169eba2013-12-16 15:14:43 +010010696 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010697 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010698 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010699 return 1;
10700}
10701
Willy Tarreau51539362012-05-08 12:46:28 +020010702/* Fetch an cookie's integer value. The integer value is returned. It
10703 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10704 */
10705static int
10706smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010707 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010708{
Willy Tarreauef38c392013-07-22 16:29:32 +020010709 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010710
10711 if (ret > 0) {
10712 smp->type = SMP_T_UINT;
10713 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10714 }
10715
10716 return ret;
10717}
10718
Willy Tarreau8797c062007-05-07 00:55:35 +020010719/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010720/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010721/************************************************************************/
10722
David Cournapeau16023ee2010-12-23 20:55:41 +090010723/*
10724 * Given a path string and its length, find the position of beginning of the
10725 * query string. Returns NULL if no query string is found in the path.
10726 *
10727 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10728 *
10729 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10730 */
bedis4c75cca2012-10-05 08:38:24 +020010731static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010732{
10733 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010734
bedis4c75cca2012-10-05 08:38:24 +020010735 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010736 return p ? p + 1 : NULL;
10737}
10738
bedis4c75cca2012-10-05 08:38:24 +020010739static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010740{
bedis4c75cca2012-10-05 08:38:24 +020010741 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010742}
10743
10744/*
10745 * Given a url parameter, find the starting position of the first occurence,
10746 * or NULL if the parameter is not found.
10747 *
10748 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10749 * the function will return query_string+8.
10750 */
10751static char*
10752find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010753 char* url_param_name, size_t url_param_name_l,
10754 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010755{
10756 char *pos, *last;
10757
10758 pos = query_string;
10759 last = query_string + query_string_l - url_param_name_l - 1;
10760
10761 while (pos <= last) {
10762 if (pos[url_param_name_l] == '=') {
10763 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10764 return pos;
10765 pos += url_param_name_l + 1;
10766 }
bedis4c75cca2012-10-05 08:38:24 +020010767 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010768 pos++;
10769 pos++;
10770 }
10771 return NULL;
10772}
10773
10774/*
10775 * Given a url parameter name, returns its value and size into *value and
10776 * *value_l respectively, and returns non-zero. If the parameter is not found,
10777 * zero is returned and value/value_l are not touched.
10778 */
10779static int
10780find_url_param_value(char* path, size_t path_l,
10781 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010782 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010783{
10784 char *query_string, *qs_end;
10785 char *arg_start;
10786 char *value_start, *value_end;
10787
bedis4c75cca2012-10-05 08:38:24 +020010788 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010789 if (!query_string)
10790 return 0;
10791
10792 qs_end = path + path_l;
10793 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010794 url_param_name, url_param_name_l,
10795 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010796 if (!arg_start)
10797 return 0;
10798
10799 value_start = arg_start + url_param_name_l + 1;
10800 value_end = value_start;
10801
bedis4c75cca2012-10-05 08:38:24 +020010802 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010803 value_end++;
10804
10805 *value = value_start;
10806 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010807 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010808}
10809
10810static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010811smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010812 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010813{
bedis4c75cca2012-10-05 08:38:24 +020010814 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010815 struct http_txn *txn = l7;
10816 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010817
bedis4c75cca2012-10-05 08:38:24 +020010818 if (!args || args[0].type != ARGT_STR ||
10819 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010820 return 0;
10821
10822 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010823
bedis4c75cca2012-10-05 08:38:24 +020010824 if (args[1].type)
10825 delim = *args[1].data.str.str;
10826
Willy Tarreau9b28e032012-10-12 23:49:43 +020010827 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010828 args->data.str.str, args->data.str.len,
10829 &smp->data.str.str, &smp->data.str.len,
10830 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010831 return 0;
10832
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010833 smp->type = SMP_T_STR;
10834 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010835 return 1;
10836}
10837
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010838/* Return the signed integer value for the specified url parameter (see url_param
10839 * above).
10840 */
10841static int
10842smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010843 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010844{
Willy Tarreauef38c392013-07-22 16:29:32 +020010845 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010846
10847 if (ret > 0) {
10848 smp->type = SMP_T_UINT;
10849 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10850 }
10851
10852 return ret;
10853}
10854
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010855/* This produces a 32-bit hash of the concatenation of the first occurrence of
10856 * the Host header followed by the path component if it begins with a slash ('/').
10857 * This means that '*' will not be added, resulting in exactly the first Host
10858 * entry. If no Host header is found, then the path is used. The resulting value
10859 * is hashed using the url hash followed by a full avalanche hash and provides a
10860 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10861 * high-traffic sites without having to store whole paths.
10862 * this differs from the base32 functions in that it includes the url parameters
10863 * as well as the path
10864 */
10865static int
10866smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010867 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010868{
10869 struct http_txn *txn = l7;
10870 struct hdr_ctx ctx;
10871 unsigned int hash = 0;
10872 char *ptr, *beg, *end;
10873 int len;
10874
10875 CHECK_HTTP_MESSAGE_FIRST();
10876
10877 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010878 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010879 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10880 ptr = ctx.line + ctx.val;
10881 len = ctx.vlen;
10882 while (len--)
10883 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10884 }
10885
10886 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010887 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 +000010888 beg = http_get_path(txn);
10889 if (!beg)
10890 beg = end;
10891
10892 for (ptr = beg; ptr < end ; ptr++);
10893
10894 if (beg < ptr && *beg == '/') {
10895 while (beg < ptr)
10896 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10897 }
10898 hash = full_hash(hash);
10899
10900 smp->type = SMP_T_UINT;
10901 smp->data.uint = hash;
10902 smp->flags = SMP_F_VOL_1ST;
10903 return 1;
10904}
10905
10906/* This concatenates the source address with the 32-bit hash of the Host and
10907 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10908 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10909 * on the source address length. The URL hash is stored before the address so
10910 * that in environments where IPv6 is insignificant, truncating the output to
10911 * 8 bytes would still work.
10912 */
10913static int
10914smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010915 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010916{
10917 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010918 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010919
Willy Tarreaue155ec22013-11-18 18:33:22 +010010920 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010921 return 0;
10922
10923 temp = get_trash_chunk();
10924 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10925 temp->len += sizeof(smp->data.uint);
10926
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010927 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010928 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010929 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010930 temp->len += 4;
10931 break;
10932 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010933 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010934 temp->len += 16;
10935 break;
10936 default:
10937 return 0;
10938 }
10939
10940 smp->data.str = *temp;
10941 smp->type = SMP_T_BIN;
10942 return 1;
10943}
10944
Willy Tarreau185b5c42012-04-26 15:11:51 +020010945/* This function is used to validate the arguments passed to any "hdr" fetch
10946 * keyword. These keywords support an optional positive or negative occurrence
10947 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10948 * is assumed that the types are already the correct ones. Returns 0 on error,
10949 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10950 * error message in case of error, that the caller is responsible for freeing.
10951 * The initial location must either be freeable or NULL.
10952 */
10953static int val_hdr(struct arg *arg, char **err_msg)
10954{
10955 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010956 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010957 return 0;
10958 }
10959 return 1;
10960}
10961
Willy Tarreau276fae92013-07-25 14:36:01 +020010962/* takes an UINT value on input supposed to represent the time since EPOCH,
10963 * adds an optional offset found in args[0] and emits a string representing
10964 * the date in RFC-1123/5322 format.
10965 */
10966static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10967{
10968 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10969 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10970 struct chunk *temp;
10971 struct tm *tm;
10972 time_t curr_date = smp->data.uint;
10973
10974 /* add offset */
10975 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10976 curr_date += args[0].data.sint;
10977
10978 tm = gmtime(&curr_date);
10979
10980 temp = get_trash_chunk();
10981 temp->len = snprintf(temp->str, temp->size - temp->len,
10982 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10983 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10984 tm->tm_hour, tm->tm_min, tm->tm_sec);
10985
10986 smp->data.str = *temp;
10987 smp->type = SMP_T_STR;
10988 return 1;
10989}
10990
Thierry FOURNIERad903512014-04-11 17:51:01 +020010991/* Match language range with language tag. RFC2616 14.4:
10992 *
10993 * A language-range matches a language-tag if it exactly equals
10994 * the tag, or if it exactly equals a prefix of the tag such
10995 * that the first tag character following the prefix is "-".
10996 *
10997 * Return 1 if the strings match, else return 0.
10998 */
10999static inline int language_range_match(const char *range, int range_len,
11000 const char *tag, int tag_len)
11001{
11002 const char *end = range + range_len;
11003 const char *tend = tag + tag_len;
11004 while (range < end) {
11005 if (*range == '-' && tag == tend)
11006 return 1;
11007 if (*range != *tag || tag == tend)
11008 return 0;
11009 range++;
11010 tag++;
11011 }
11012 /* Return true only if the last char of the tag is matched. */
11013 return tag == tend;
11014}
11015
11016/* Arguments: The list of expected value, the number of parts returned and the separator */
11017static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11018{
11019 const char *al = smp->data.str.str;
11020 const char *end = al + smp->data.str.len;
11021 const char *token;
11022 int toklen;
11023 int qvalue;
11024 const char *str;
11025 const char *w;
11026 int best_q = 0;
11027
11028 /* Set the constant to the sample, because the output of the
11029 * function will be peek in the constant configuration string.
11030 */
11031 smp->flags |= SMP_F_CONST;
11032 smp->data.str.size = 0;
11033 smp->data.str.str = "";
11034 smp->data.str.len = 0;
11035
11036 /* Parse the accept language */
11037 while (1) {
11038
11039 /* Jump spaces, quit if the end is detected. */
11040 while (al < end && isspace(*al))
11041 al++;
11042 if (al >= end)
11043 break;
11044
11045 /* Start of the fisrt word. */
11046 token = al;
11047
11048 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
11049 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
11050 al++;
11051 if (al == token)
11052 goto expect_comma;
11053
11054 /* Length of the token. */
11055 toklen = al - token;
11056 qvalue = 1000;
11057
11058 /* Check if the token exists in the list. If the token not exists,
11059 * jump to the next token.
11060 */
11061 str = args[0].data.str.str;
11062 w = str;
11063 while (1) {
11064 if (*str == ';' || *str == '\0') {
11065 if (language_range_match(token, toklen, w, str-w))
11066 goto look_for_q;
11067 if (*str == '\0')
11068 goto expect_comma;
11069 w = str + 1;
11070 }
11071 str++;
11072 }
11073 goto expect_comma;
11074
11075look_for_q:
11076
11077 /* Jump spaces, quit if the end is detected. */
11078 while (al < end && isspace(*al))
11079 al++;
11080 if (al >= end)
11081 goto process_value;
11082
11083 /* If ',' is found, process the result */
11084 if (*al == ',')
11085 goto process_value;
11086
11087 /* If the character is different from ';', look
11088 * for the end of the header part in best effort.
11089 */
11090 if (*al != ';')
11091 goto expect_comma;
11092
11093 /* Assumes that the char is ';', now expect "q=". */
11094 al++;
11095
11096 /* Jump spaces, process value if the end is detected. */
11097 while (al < end && isspace(*al))
11098 al++;
11099 if (al >= end)
11100 goto process_value;
11101
11102 /* Expect 'q'. If no 'q', continue in best effort */
11103 if (*al != 'q')
11104 goto process_value;
11105 al++;
11106
11107 /* Jump spaces, process value if the end is detected. */
11108 while (al < end && isspace(*al))
11109 al++;
11110 if (al >= end)
11111 goto process_value;
11112
11113 /* Expect '='. If no '=', continue in best effort */
11114 if (*al != '=')
11115 goto process_value;
11116 al++;
11117
11118 /* Jump spaces, process value if the end is detected. */
11119 while (al < end && isspace(*al))
11120 al++;
11121 if (al >= end)
11122 goto process_value;
11123
11124 /* Parse the q value. */
11125 qvalue = parse_qvalue(al, &al);
11126
11127process_value:
11128
11129 /* If the new q value is the best q value, then store the associated
11130 * language in the response. If qvalue is the biggest value (1000),
11131 * break the process.
11132 */
11133 if (qvalue > best_q) {
11134 smp->data.str.str = (char *)w;
11135 smp->data.str.len = str - w;
11136 if (qvalue >= 1000)
11137 break;
11138 best_q = qvalue;
11139 }
11140
11141expect_comma:
11142
11143 /* Expect comma or end. If the end is detected, quit the loop. */
11144 while (al < end && *al != ',')
11145 al++;
11146 if (al >= end)
11147 break;
11148
11149 /* Comma is found, jump it and restart the analyzer. */
11150 al++;
11151 }
11152
11153 /* Set default value if required. */
11154 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11155 smp->data.str.str = args[1].data.str.str;
11156 smp->data.str.len = args[1].data.str.len;
11157 }
11158
11159 /* Return true only if a matching language was found. */
11160 return smp->data.str.len != 0;
11161}
11162
William Lallemand73025dd2014-04-24 14:38:37 +020011163/*
11164 * Return the struct http_req_action_kw associated to a keyword.
11165 */
11166struct http_req_action_kw *action_http_req_custom(const char *kw)
11167{
11168 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11169 struct http_req_action_kw_list *kw_list;
11170 int i;
11171
11172 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11173 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11174 if (!strcmp(kw, kw_list->kw[i].kw))
11175 return &kw_list->kw[i];
11176 }
11177 }
11178 }
11179 return NULL;
11180}
11181
11182/*
11183 * Return the struct http_res_action_kw associated to a keyword.
11184 */
11185struct http_res_action_kw *action_http_res_custom(const char *kw)
11186{
11187 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11188 struct http_res_action_kw_list *kw_list;
11189 int i;
11190
11191 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11192 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11193 if (!strcmp(kw, kw_list->kw[i].kw))
11194 return &kw_list->kw[i];
11195 }
11196 }
11197 }
11198 return NULL;
11199}
11200
Willy Tarreau4a568972010-05-12 08:08:50 +020011201/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011202/* All supported ACL keywords must be declared here. */
11203/************************************************************************/
11204
11205/* Note: must not be declared <const> as its list will be overwritten.
11206 * Please take care of keeping this list alphabetically sorted.
11207 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011208static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011209 { "base", "base", PAT_MATCH_STR },
11210 { "base_beg", "base", PAT_MATCH_BEG },
11211 { "base_dir", "base", PAT_MATCH_DIR },
11212 { "base_dom", "base", PAT_MATCH_DOM },
11213 { "base_end", "base", PAT_MATCH_END },
11214 { "base_len", "base", PAT_MATCH_LEN },
11215 { "base_reg", "base", PAT_MATCH_REG },
11216 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011217
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011218 { "cook", "req.cook", PAT_MATCH_STR },
11219 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11220 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11221 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11222 { "cook_end", "req.cook", PAT_MATCH_END },
11223 { "cook_len", "req.cook", PAT_MATCH_LEN },
11224 { "cook_reg", "req.cook", PAT_MATCH_REG },
11225 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011226
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011227 { "hdr", "req.hdr", PAT_MATCH_STR },
11228 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11229 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11230 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11231 { "hdr_end", "req.hdr", PAT_MATCH_END },
11232 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11233 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11234 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011235
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011236 /* these two declarations uses strings with list storage (in place
11237 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11238 * and delete functions are relative to the list management. The parse
11239 * and match method are related to the corresponding fetch methods. This
11240 * is very particular ACL declaration mode.
11241 */
11242 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11243 { "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 +020011244
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011245 { "path", "path", PAT_MATCH_STR },
11246 { "path_beg", "path", PAT_MATCH_BEG },
11247 { "path_dir", "path", PAT_MATCH_DIR },
11248 { "path_dom", "path", PAT_MATCH_DOM },
11249 { "path_end", "path", PAT_MATCH_END },
11250 { "path_len", "path", PAT_MATCH_LEN },
11251 { "path_reg", "path", PAT_MATCH_REG },
11252 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011253
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011254 { "req_ver", "req.ver", PAT_MATCH_STR },
11255 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011256
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011257 { "scook", "res.cook", PAT_MATCH_STR },
11258 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11259 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11260 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11261 { "scook_end", "res.cook", PAT_MATCH_END },
11262 { "scook_len", "res.cook", PAT_MATCH_LEN },
11263 { "scook_reg", "res.cook", PAT_MATCH_REG },
11264 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011265
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011266 { "shdr", "res.hdr", PAT_MATCH_STR },
11267 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11268 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11269 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11270 { "shdr_end", "res.hdr", PAT_MATCH_END },
11271 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11272 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11273 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011274
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011275 { "url", "url", PAT_MATCH_STR },
11276 { "url_beg", "url", PAT_MATCH_BEG },
11277 { "url_dir", "url", PAT_MATCH_DIR },
11278 { "url_dom", "url", PAT_MATCH_DOM },
11279 { "url_end", "url", PAT_MATCH_END },
11280 { "url_len", "url", PAT_MATCH_LEN },
11281 { "url_reg", "url", PAT_MATCH_REG },
11282 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011283
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011284 { "urlp", "urlp", PAT_MATCH_STR },
11285 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11286 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11287 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11288 { "urlp_end", "urlp", PAT_MATCH_END },
11289 { "urlp_len", "urlp", PAT_MATCH_LEN },
11290 { "urlp_reg", "urlp", PAT_MATCH_REG },
11291 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011292
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011293 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011294}};
11295
11296/************************************************************************/
11297/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011298/************************************************************************/
11299/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011300static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011301 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011302 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11303 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11304
William Lallemanda43ba4e2014-01-28 18:14:25 +010011305 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011306 { "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 +020011307
11308 /* retrieve these captures from the HTTP logs */
11309 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11310 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11311 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11312
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011313 { "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 +020011314 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011315
Willy Tarreau409bcde2013-01-08 00:31:00 +010011316 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11317 * are only here to match the ACL's name, are request-only and are used
11318 * for ACL compatibility only.
11319 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011320 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11321 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011322 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11323 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11324
11325 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11326 * only here to match the ACL's name, are request-only and are used for
11327 * ACL compatibility only.
11328 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011329 { "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 +010011330 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11331 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11332 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11333
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011334 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011335 { "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 +010011336 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011337 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011338 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011339
11340 /* HTTP protocol on the request path */
11341 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011342 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011343
11344 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011345 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11346 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011347
11348 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011349 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11350 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011351
Willy Tarreau18ed2562013-01-14 15:56:36 +010011352 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011353 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011354 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11355 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11356
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011357 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011358 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011359 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011360 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11361 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11362 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11363
11364 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011365 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011366 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11367 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11368
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011369 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011370 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011371 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011372 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11373 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11374 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11375
Willy Tarreau409bcde2013-01-08 00:31:00 +010011376 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011377 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011378 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11379 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011380 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011381
11382 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011383 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011384 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11385 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11386 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11387
11388 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011389 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011390 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11391 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011392 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11393 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011394 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11395 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011396 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11397 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011398}};
11399
Willy Tarreau8797c062007-05-07 00:55:35 +020011400
Willy Tarreau276fae92013-07-25 14:36:01 +020011401/* Note: must not be declared <const> as its list will be overwritten */
11402static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011403 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11404 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011405 { NULL, NULL, 0, 0, 0 },
11406}};
11407
Willy Tarreau8797c062007-05-07 00:55:35 +020011408__attribute__((constructor))
11409static void __http_protocol_init(void)
11410{
11411 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011412 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011413 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011414}
11415
11416
Willy Tarreau58f10d72006-12-04 02:26:12 +010011417/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011418 * Local variables:
11419 * c-indent-level: 8
11420 * c-basic-offset: 8
11421 * End:
11422 */