blob: b233fd99a01a481fe7b7c10e43332ffaf1c46d8b [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 Tarreaud787e662009-07-07 10:14:51 +02002919 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002920 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002921 req->analyse_exp = TICK_ETERNITY;
2922 return 1;
2923
2924 return_bad_req:
2925 /* We centralize bad requests processing here */
2926 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2927 /* we detected a parsing error. We want to archive this request
2928 * in the dedicated proxy area for later troubleshooting.
2929 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002930 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002931 }
2932
2933 txn->req.msg_state = HTTP_MSG_ERROR;
2934 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002935 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002936
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002937 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002938 if (s->listener->counters)
2939 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002940
2941 return_prx_cond:
2942 if (!(s->flags & SN_ERR_MASK))
2943 s->flags |= SN_ERR_PRXCOND;
2944 if (!(s->flags & SN_FINST_MASK))
2945 s->flags |= SN_FINST_R;
2946
2947 req->analysers = 0;
2948 req->analyse_exp = TICK_ETERNITY;
2949 return 0;
2950}
2951
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002952
Willy Tarreau347a35d2013-11-22 17:51:09 +01002953/* This function prepares an applet to handle the stats. It can deal with the
2954 * "100-continue" expectation, check that admin rules are met for POST requests,
2955 * and program a response message if something was unexpected. It cannot fail
2956 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002957 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002958 * s->target which is supposed to already point to the stats applet. The caller
2959 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002960 */
2961int http_handle_stats(struct session *s, struct channel *req)
2962{
2963 struct stats_admin_rule *stats_admin_rule;
2964 struct stream_interface *si = s->rep->prod;
2965 struct http_txn *txn = &s->txn;
2966 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002967 struct uri_auth *uri_auth = s->be->uri_auth;
2968 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002969 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002970
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002971 appctx = si_appctx(si);
2972 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2973 appctx->st1 = appctx->st2 = 0;
2974 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2975 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002976 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
2977 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002978
2979 uri = msg->chn->buf->p + msg->sl.rq.u;
2980 lookup = uri + uri_auth->uri_len;
2981
2982 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2983 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002984 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002985 break;
2986 }
2987 }
2988
2989 if (uri_auth->refresh) {
2990 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2991 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002992 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002993 break;
2994 }
2995 }
2996 }
2997
2998 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2999 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003000 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003001 break;
3002 }
3003 }
3004
3005 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3006 if (memcmp(h, ";st=", 4) == 0) {
3007 int i;
3008 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003009 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003010 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3011 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003012 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003013 break;
3014 }
3015 }
3016 break;
3017 }
3018 }
3019
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003020 appctx->ctx.stats.scope_str = 0;
3021 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003022 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3023 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3024 int itx = 0;
3025 const char *h2;
3026 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3027 const char *err;
3028
3029 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3030 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003031 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003032 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3033 itx++;
3034 h++;
3035 }
3036
3037 if (itx > STAT_SCOPE_TXT_MAXLEN)
3038 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003039 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003040
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003041 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003042 memcpy(scope_txt, h2, itx);
3043 scope_txt[itx] = '\0';
3044 err = invalid_char(scope_txt);
3045 if (err) {
3046 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003047 appctx->ctx.stats.scope_str = 0;
3048 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003049 }
3050 break;
3051 }
3052 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003053
3054 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003055 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003056 int ret = 1;
3057
3058 if (stats_admin_rule->cond) {
3059 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3060 ret = acl_pass(ret);
3061 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3062 ret = !ret;
3063 }
3064
3065 if (ret) {
3066 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003067 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003068 break;
3069 }
3070 }
3071
3072 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003073 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003074 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003075 /* we'll need the request body, possibly after sending 100-continue */
3076 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003077 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003078 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003079 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003080 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3081 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003082 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003083 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003084 else {
3085 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003086 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003087 }
3088
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003089 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003090 return 1;
3091}
3092
Lukas Tribus67db8df2013-06-23 17:37:13 +02003093/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3094 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3095 */
3096static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3097{
3098#ifdef IP_TOS
3099 if (from.ss_family == AF_INET)
3100 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3101#endif
3102#ifdef IPV6_TCLASS
3103 if (from.ss_family == AF_INET6) {
3104 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3105 /* v4-mapped addresses need IP_TOS */
3106 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3107 else
3108 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3109 }
3110#endif
3111}
3112
Willy Tarreau20b0de52012-12-24 15:45:22 +01003113/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003114 * transaction <txn>. Returns the first rule that prevents further processing
3115 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3116 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3117 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003118 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003119static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003120http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003121{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003122 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003123 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003124 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003125
Willy Tarreauff011f22011-01-06 17:51:27 +01003126 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003127 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003128 continue;
3129
Willy Tarreau96257ec2012-12-27 10:46:37 +01003130 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003131 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003132 int ret;
3133
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003134 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003135 ret = acl_pass(ret);
3136
Willy Tarreauff011f22011-01-06 17:51:27 +01003137 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003138 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003139
3140 if (!ret) /* condition not matched */
3141 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003142 }
3143
Willy Tarreau20b0de52012-12-24 15:45:22 +01003144
Willy Tarreau96257ec2012-12-27 10:46:37 +01003145 switch (rule->action) {
3146 case HTTP_REQ_ACT_ALLOW:
3147 return NULL; /* "allow" rules are OK */
3148
3149 case HTTP_REQ_ACT_DENY:
3150 txn->flags |= TX_CLDENY;
3151 return rule;
3152
Willy Tarreauccbcc372012-12-27 12:37:57 +01003153 case HTTP_REQ_ACT_TARPIT:
3154 txn->flags |= TX_CLTARPIT;
3155 return rule;
3156
Willy Tarreau96257ec2012-12-27 10:46:37 +01003157 case HTTP_REQ_ACT_AUTH:
3158 return rule;
3159
Willy Tarreau81499eb2012-12-27 12:19:02 +01003160 case HTTP_REQ_ACT_REDIR:
3161 return rule;
3162
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003163 case HTTP_REQ_ACT_SET_NICE:
3164 s->task->nice = rule->arg.nice;
3165 break;
3166
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003167 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003168 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003169 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003170 break;
3171
Willy Tarreau51347ed2013-06-11 19:34:13 +02003172 case HTTP_REQ_ACT_SET_MARK:
3173#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003174 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003175 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003176#endif
3177 break;
3178
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003179 case HTTP_REQ_ACT_SET_LOGL:
3180 s->logs.level = rule->arg.loglevel;
3181 break;
3182
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003183 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003184 case HTTP_REQ_ACT_SET_HDR:
3185 ctx.idx = 0;
3186 /* remove all occurrences of the header */
3187 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3188 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3189 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003190 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003191 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3192 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003193 /* now fall through to header addition */
3194
3195 case HTTP_REQ_ACT_ADD_HDR:
3196 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3197 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3198 trash.len = rule->arg.hdr_add.name_len;
3199 trash.str[trash.len++] = ':';
3200 trash.str[trash.len++] = ' ';
3201 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3202 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3203 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003204
3205 case HTTP_REQ_ACT_DEL_ACL:
3206 case HTTP_REQ_ACT_DEL_MAP: {
3207 struct pat_ref *ref;
3208 char *key;
3209 int len;
3210
3211 /* collect reference */
3212 ref = pat_ref_lookup(rule->arg.map.ref);
3213 if (!ref)
3214 continue;
3215
3216 /* collect key */
3217 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3218 key = trash.str;
3219 key[len] = '\0';
3220
3221 /* perform update */
3222 /* returned code: 1=ok, 0=ko */
3223 pat_ref_delete(ref, key);
3224
3225 break;
3226 }
3227
3228 case HTTP_REQ_ACT_ADD_ACL: {
3229 struct pat_ref *ref;
3230 char *key;
3231 struct chunk *trash_key;
3232 int len;
3233
3234 trash_key = get_trash_chunk();
3235
3236 /* collect reference */
3237 ref = pat_ref_lookup(rule->arg.map.ref);
3238 if (!ref)
3239 continue;
3240
3241 /* collect key */
3242 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3243 key = trash_key->str;
3244 key[len] = '\0';
3245
3246 /* perform update */
3247 /* add entry only if it does not already exist */
3248 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003249 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003250
3251 break;
3252 }
3253
3254 case HTTP_REQ_ACT_SET_MAP: {
3255 struct pat_ref *ref;
3256 char *key, *value;
3257 struct chunk *trash_key, *trash_value;
3258 int len;
3259
3260 trash_key = get_trash_chunk();
3261 trash_value = get_trash_chunk();
3262
3263 /* collect reference */
3264 ref = pat_ref_lookup(rule->arg.map.ref);
3265 if (!ref)
3266 continue;
3267
3268 /* collect key */
3269 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3270 key = trash_key->str;
3271 key[len] = '\0';
3272
3273 /* collect value */
3274 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3275 value = trash_value->str;
3276 value[len] = '\0';
3277
3278 /* perform update */
3279 if (pat_ref_find_elt(ref, key) != NULL)
3280 /* update entry if it exists */
3281 pat_ref_set(ref, key, value, NULL);
3282 else
3283 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003284 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003285
3286 break;
3287 }
William Lallemand73025dd2014-04-24 14:38:37 +02003288
3289 case HTTP_REQ_ACT_CUSTOM_CONT:
3290 rule->action_ptr(rule, px, s, txn);
3291 break;
3292
3293 case HTTP_REQ_ACT_CUSTOM_STOP:
3294 rule->action_ptr(rule, px, s, txn);
3295 return rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003296 }
3297 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003298
3299 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003300 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003301}
3302
Willy Tarreau71241ab2012-12-27 11:30:54 +01003303
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003304/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3305 * transaction <txn>. Returns the first rule that prevents further processing
3306 * of the response (deny, ...) or NULL if it executed all rules or stopped
3307 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3308 * rule.
3309 */
3310static struct http_res_rule *
3311http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3312{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003313 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003314 struct http_res_rule *rule;
3315 struct hdr_ctx ctx;
3316
3317 list_for_each_entry(rule, rules, list) {
3318 if (rule->action >= HTTP_RES_ACT_MAX)
3319 continue;
3320
3321 /* check optional condition */
3322 if (rule->cond) {
3323 int ret;
3324
3325 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3326 ret = acl_pass(ret);
3327
3328 if (rule->cond->pol == ACL_COND_UNLESS)
3329 ret = !ret;
3330
3331 if (!ret) /* condition not matched */
3332 continue;
3333 }
3334
3335
3336 switch (rule->action) {
3337 case HTTP_RES_ACT_ALLOW:
3338 return NULL; /* "allow" rules are OK */
3339
3340 case HTTP_RES_ACT_DENY:
3341 txn->flags |= TX_SVDENY;
3342 return rule;
3343
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003344 case HTTP_RES_ACT_SET_NICE:
3345 s->task->nice = rule->arg.nice;
3346 break;
3347
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003348 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003349 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003350 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003351 break;
3352
Willy Tarreau51347ed2013-06-11 19:34:13 +02003353 case HTTP_RES_ACT_SET_MARK:
3354#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003355 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003356 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003357#endif
3358 break;
3359
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003360 case HTTP_RES_ACT_SET_LOGL:
3361 s->logs.level = rule->arg.loglevel;
3362 break;
3363
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003364 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003365 case HTTP_RES_ACT_SET_HDR:
3366 ctx.idx = 0;
3367 /* remove all occurrences of the header */
3368 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3369 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3370 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3371 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003372 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3373 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003374 /* now fall through to header addition */
3375
3376 case HTTP_RES_ACT_ADD_HDR:
3377 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3378 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3379 trash.len = rule->arg.hdr_add.name_len;
3380 trash.str[trash.len++] = ':';
3381 trash.str[trash.len++] = ' ';
3382 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3383 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3384 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003385
3386 case HTTP_RES_ACT_DEL_ACL:
3387 case HTTP_RES_ACT_DEL_MAP: {
3388 struct pat_ref *ref;
3389 char *key;
3390 int len;
3391
3392 /* collect reference */
3393 ref = pat_ref_lookup(rule->arg.map.ref);
3394 if (!ref)
3395 continue;
3396
3397 /* collect key */
3398 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3399 key = trash.str;
3400 key[len] = '\0';
3401
3402 /* perform update */
3403 /* returned code: 1=ok, 0=ko */
3404 pat_ref_delete(ref, key);
3405
3406 break;
3407 }
3408
3409 case HTTP_RES_ACT_ADD_ACL: {
3410 struct pat_ref *ref;
3411 char *key;
3412 struct chunk *trash_key;
3413 int len;
3414
3415 trash_key = get_trash_chunk();
3416
3417 /* collect reference */
3418 ref = pat_ref_lookup(rule->arg.map.ref);
3419 if (!ref)
3420 continue;
3421
3422 /* collect key */
3423 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3424 key = trash_key->str;
3425 key[len] = '\0';
3426
3427 /* perform update */
3428 /* check if the entry already exists */
3429 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003430 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003431
3432 break;
3433 }
3434
3435 case HTTP_RES_ACT_SET_MAP: {
3436 struct pat_ref *ref;
3437 char *key, *value;
3438 struct chunk *trash_key, *trash_value;
3439 int len;
3440
3441 trash_key = get_trash_chunk();
3442 trash_value = get_trash_chunk();
3443
3444 /* collect reference */
3445 ref = pat_ref_lookup(rule->arg.map.ref);
3446 if (!ref)
3447 continue;
3448
3449 /* collect key */
3450 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3451 key = trash_key->str;
3452 key[len] = '\0';
3453
3454 /* collect value */
3455 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3456 value = trash_value->str;
3457 value[len] = '\0';
3458
3459 /* perform update */
3460 if (pat_ref_find_elt(ref, key) != NULL)
3461 /* update entry if it exists */
3462 pat_ref_set(ref, key, value, NULL);
3463 else
3464 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003465 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003466
3467 break;
3468 }
William Lallemand73025dd2014-04-24 14:38:37 +02003469
3470 case HTTP_RES_ACT_CUSTOM_CONT:
3471 rule->action_ptr(rule, px, s, txn);
3472 break;
3473
3474 case HTTP_RES_ACT_CUSTOM_STOP:
3475 rule->action_ptr(rule, px, s, txn);
3476 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003477 }
3478 }
3479
3480 /* we reached the end of the rules, nothing to report */
3481 return NULL;
3482}
3483
3484
Willy Tarreau71241ab2012-12-27 11:30:54 +01003485/* Perform an HTTP redirect based on the information in <rule>. The function
3486 * returns non-zero on success, or zero in case of a, irrecoverable error such
3487 * as too large a request to build a valid response.
3488 */
3489static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3490{
3491 struct http_msg *msg = &txn->req;
3492 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003493 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003494
3495 /* build redirect message */
3496 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003497 case 308:
3498 msg_fmt = HTTP_308;
3499 break;
3500 case 307:
3501 msg_fmt = HTTP_307;
3502 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003503 case 303:
3504 msg_fmt = HTTP_303;
3505 break;
3506 case 301:
3507 msg_fmt = HTTP_301;
3508 break;
3509 case 302:
3510 default:
3511 msg_fmt = HTTP_302;
3512 break;
3513 }
3514
3515 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3516 return 0;
3517
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003518 location = trash.str + trash.len;
3519
Willy Tarreau71241ab2012-12-27 11:30:54 +01003520 switch(rule->type) {
3521 case REDIRECT_TYPE_SCHEME: {
3522 const char *path;
3523 const char *host;
3524 struct hdr_ctx ctx;
3525 int pathlen;
3526 int hostlen;
3527
3528 host = "";
3529 hostlen = 0;
3530 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003531 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003532 host = ctx.line + ctx.val;
3533 hostlen = ctx.vlen;
3534 }
3535
3536 path = http_get_path(txn);
3537 /* build message using path */
3538 if (path) {
3539 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3540 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3541 int qs = 0;
3542 while (qs < pathlen) {
3543 if (path[qs] == '?') {
3544 pathlen = qs;
3545 break;
3546 }
3547 qs++;
3548 }
3549 }
3550 } else {
3551 path = "/";
3552 pathlen = 1;
3553 }
3554
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003555 if (rule->rdr_str) { /* this is an old "redirect" rule */
3556 /* check if we can add scheme + "://" + host + path */
3557 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3558 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003559
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003560 /* add scheme */
3561 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3562 trash.len += rule->rdr_len;
3563 }
3564 else {
3565 /* add scheme with executing log format */
3566 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003567
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003568 /* check if we can add scheme + "://" + host + path */
3569 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3570 return 0;
3571 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003572 /* add "://" */
3573 memcpy(trash.str + trash.len, "://", 3);
3574 trash.len += 3;
3575
3576 /* add host */
3577 memcpy(trash.str + trash.len, host, hostlen);
3578 trash.len += hostlen;
3579
3580 /* add path */
3581 memcpy(trash.str + trash.len, path, pathlen);
3582 trash.len += pathlen;
3583
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003584 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003585 if (trash.len && trash.str[trash.len - 1] != '/' &&
3586 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3587 if (trash.len > trash.size - 5)
3588 return 0;
3589 trash.str[trash.len] = '/';
3590 trash.len++;
3591 }
3592
3593 break;
3594 }
3595 case REDIRECT_TYPE_PREFIX: {
3596 const char *path;
3597 int pathlen;
3598
3599 path = http_get_path(txn);
3600 /* build message using path */
3601 if (path) {
3602 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3603 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3604 int qs = 0;
3605 while (qs < pathlen) {
3606 if (path[qs] == '?') {
3607 pathlen = qs;
3608 break;
3609 }
3610 qs++;
3611 }
3612 }
3613 } else {
3614 path = "/";
3615 pathlen = 1;
3616 }
3617
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003618 if (rule->rdr_str) { /* this is an old "redirect" rule */
3619 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3620 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003621
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003622 /* add prefix. Note that if prefix == "/", we don't want to
3623 * add anything, otherwise it makes it hard for the user to
3624 * configure a self-redirection.
3625 */
3626 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3627 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3628 trash.len += rule->rdr_len;
3629 }
3630 }
3631 else {
3632 /* add prefix with executing log format */
3633 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3634
3635 /* Check length */
3636 if (trash.len + pathlen > trash.size - 4)
3637 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003638 }
3639
3640 /* add path */
3641 memcpy(trash.str + trash.len, path, pathlen);
3642 trash.len += pathlen;
3643
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003644 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003645 if (trash.len && trash.str[trash.len - 1] != '/' &&
3646 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3647 if (trash.len > trash.size - 5)
3648 return 0;
3649 trash.str[trash.len] = '/';
3650 trash.len++;
3651 }
3652
3653 break;
3654 }
3655 case REDIRECT_TYPE_LOCATION:
3656 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003657 if (rule->rdr_str) { /* this is an old "redirect" rule */
3658 if (trash.len + rule->rdr_len > trash.size - 4)
3659 return 0;
3660
3661 /* add location */
3662 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3663 trash.len += rule->rdr_len;
3664 }
3665 else {
3666 /* add location with executing log format */
3667 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003668
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003669 /* Check left length */
3670 if (trash.len > trash.size - 4)
3671 return 0;
3672 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003673 break;
3674 }
3675
3676 if (rule->cookie_len) {
3677 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3678 trash.len += 14;
3679 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3680 trash.len += rule->cookie_len;
3681 memcpy(trash.str + trash.len, "\r\n", 2);
3682 trash.len += 2;
3683 }
3684
3685 /* add end of headers and the keep-alive/close status.
3686 * We may choose to set keep-alive if the Location begins
3687 * with a slash, because the client will come back to the
3688 * same server.
3689 */
3690 txn->status = rule->code;
3691 /* let's log the request time */
3692 s->logs.tv_request = now;
3693
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003694 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003695 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3696 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3697 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3698 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3699 /* keep-alive possible */
3700 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3701 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3702 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3703 trash.len += 30;
3704 } else {
3705 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3706 trash.len += 24;
3707 }
3708 }
3709 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3710 trash.len += 4;
3711 bo_inject(txn->rsp.chn, trash.str, trash.len);
3712 /* "eat" the request */
3713 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003714 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003715 msg->sov = 0;
3716 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3717 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3718 txn->req.msg_state = HTTP_MSG_CLOSED;
3719 txn->rsp.msg_state = HTTP_MSG_DONE;
3720 } else {
3721 /* keep-alive not possible */
3722 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3723 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3724 trash.len += 29;
3725 } else {
3726 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3727 trash.len += 23;
3728 }
3729 stream_int_retnclose(txn->req.chn->prod, &trash);
3730 txn->req.chn->analysers = 0;
3731 }
3732
3733 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003734 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003735 if (!(s->flags & SN_FINST_MASK))
3736 s->flags |= SN_FINST_R;
3737
3738 return 1;
3739}
3740
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003741/* This stream analyser runs all HTTP request processing which is common to
3742 * frontends and backends, which means blocking ACLs, filters, connection-close,
3743 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003744 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003745 * either needs more data or wants to immediately abort the request (eg: deny,
3746 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003747 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003748int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003749{
Willy Tarreaud787e662009-07-07 10:14:51 +02003750 struct http_txn *txn = &s->txn;
3751 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003752 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003753 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003754 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003755 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003756
Willy Tarreau655dce92009-11-08 13:10:58 +01003757 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003758 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003759 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003760 return 0;
3761 }
3762
Willy Tarreau3a816292009-07-07 10:55:49 +02003763 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003764 req->analyse_exp = TICK_ETERNITY;
3765
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003766 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 +02003767 now_ms, __FUNCTION__,
3768 s,
3769 req,
3770 req->rex, req->wex,
3771 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003772 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003773 req->analysers);
3774
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003775 /* first check whether we have some ACLs set to block this request */
3776 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003777 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003778
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003779 ret = acl_pass(ret);
3780 if (cond->pol == ACL_COND_UNLESS)
3781 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003782
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003783 if (ret) {
3784 txn->status = 403;
3785 /* let's log the request time */
3786 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003787 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003788 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003789 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003790 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003791 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003792
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003793 /* just in case we have some per-backend tracking */
3794 session_inc_be_http_req_ctr(s);
3795
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003796 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003797 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003798
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003799 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003800 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003801 if (stats_check_uri(s->rep->prod, txn, px)) {
3802 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003803 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3804 txn->status = 500;
3805 s->logs.tv_request = now;
3806 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003807
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003808 if (!(s->flags & SN_ERR_MASK))
3809 s->flags |= SN_ERR_RESOURCE;
3810 goto return_prx_cond;
3811 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003812 /* parse the whole stats request and extract the relevant information */
3813 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003814 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003815 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003816 }
3817
Willy Tarreau3b44e722013-11-16 10:28:23 +01003818 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3819 * blocked by an http-request rule.
3820 */
3821 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003822 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003823 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003824 }
Willy Tarreau06619262006-12-17 08:37:22 +01003825
Willy Tarreau3b44e722013-11-16 10:28:23 +01003826 /* return a 403 if either rule has blocked */
3827 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003828 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003829 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003830 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003831 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003832 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003833 s->fe->fe_counters.denied_req++;
3834 if (s->fe != s->be)
3835 s->be->be_counters.denied_req++;
3836 if (s->listener->counters)
3837 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003838 goto return_prx_cond;
3839 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003840
3841 /* When a connection is tarpitted, we use the tarpit timeout,
3842 * which may be the same as the connect timeout if unspecified.
3843 * If unset, then set it to zero because we really want it to
3844 * eventually expire. We build the tarpit as an analyser.
3845 */
3846 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003847 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003848 /* wipe the request out so that we can drop the connection early
3849 * if the client closes first.
3850 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003851 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003852 req->analysers = 0; /* remove switching rules etc... */
3853 req->analysers |= AN_REQ_HTTP_TARPIT;
3854 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3855 if (!req->analyse_exp)
3856 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003857 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003858 s->fe->fe_counters.denied_req++;
3859 if (s->fe != s->be)
3860 s->be->be_counters.denied_req++;
3861 if (s->listener->counters)
3862 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003863 return 1;
3864 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003865 }
Willy Tarreau06619262006-12-17 08:37:22 +01003866
Willy Tarreau70dffda2014-01-30 03:07:23 +01003867 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003868 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003869 * Option httpclose by itself sets tunnel mode where headers are mangled.
3870 * However, if another mode is set, it will affect it (eg: server-close/
3871 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3872 * if FE and BE have the same settings (common). The method consists in
3873 * checking if options changed between the two calls (implying that either
3874 * one is non-null, or one of them is non-null and we are there for the first
3875 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003876 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003877
Willy Tarreau416ce612014-01-31 15:45:34 +01003878 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003879 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003880 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003881
Willy Tarreau70dffda2014-01-30 03:07:23 +01003882 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3883 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3884 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3885 tmp = TX_CON_WANT_TUN;
3886
3887 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3888 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3889 tmp = TX_CON_WANT_TUN;
3890 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003891
3892 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003893 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3894 /* option httpclose + server_close => forceclose */
3895 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3896 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3897 tmp = TX_CON_WANT_CLO;
3898 else
3899 tmp = TX_CON_WANT_SCL;
3900 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003901
3902 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3903 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003904 tmp = TX_CON_WANT_CLO;
3905
Willy Tarreau5b154472009-12-21 20:11:07 +01003906 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3907 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003908
Willy Tarreau416ce612014-01-31 15:45:34 +01003909 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3910 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003911 /* parse the Connection header and possibly clean it */
3912 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003913 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003914 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3915 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003916 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003917 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003918 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003919 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003920 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003921
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003922 /* check if client or config asks for explicit close in KAL/SCL */
3923 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3924 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3925 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003926 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003927 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003928 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003929 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3930 }
Willy Tarreau78599912009-10-17 20:12:21 +02003931
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003932 /* we can be blocked here because the request needs to be authenticated,
3933 * either to pass or to access stats.
3934 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003935 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003936 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003937
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003938 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003939 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003940
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003941 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Willy Tarreaue6d24162014-04-28 16:59:15 +02003942 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003943 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003944 /* on 401 we still count one error, because normal browsing
3945 * won't significantly increase the counter but brute force
3946 * attempts will.
3947 */
3948 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003949 goto return_prx_cond;
3950 }
3951
Willy Tarreau5e9edce2014-04-28 11:13:33 +02003952 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3953 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3954 goto return_bad_req;
3955 req->analyse_exp = TICK_ETERNITY;
3956 return 1;
3957 }
3958
3959 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_CUSTOM_STOP) {
3960 req->analyse_exp = TICK_ETERNITY;
3961 return 1;
3962 }
3963
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003964 /* add request headers from the rule sets in the same order */
3965 list_for_each_entry(wl, &px->req_add, list) {
3966 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003967 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003968 ret = acl_pass(ret);
3969 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3970 ret = !ret;
3971 if (!ret)
3972 continue;
3973 }
3974
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003975 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003976 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003977 }
3978
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003979 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003980 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003981 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3982 s->fe->fe_counters.intercepted_req++;
3983
3984 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3985 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3986 if (!(s->flags & SN_FINST_MASK))
3987 s->flags |= SN_FINST_R;
3988
3989 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02003990
3991 /* we may want to compress the stats page */
3992 if (s->fe->comp || s->be->comp)
3993 select_compression_request_header(s, req->buf);
3994
3995 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003996 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) |
3997 AN_REQ_HTTP_XFER_BODY | AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE | AN_RES_HTTP_XFER_BODY;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003998 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003999 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004000
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004001 /* check whether we have some ACLs set to redirect this request */
4002 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004003 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004004 int ret;
4005
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004006 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004007 ret = acl_pass(ret);
4008 if (rule->cond->pol == ACL_COND_UNLESS)
4009 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004010 if (!ret)
4011 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004012 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004013 if (!http_apply_redirect_rule(rule, s, txn))
4014 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01004015
Willy Tarreau71241ab2012-12-27 11:30:54 +01004016 req->analyse_exp = TICK_ETERNITY;
4017 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004018 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004019
Willy Tarreau2be39392010-01-03 17:24:51 +01004020 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4021 * If this happens, then the data will not come immediately, so we must
4022 * send all what we have without waiting. Note that due to the small gain
4023 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004024 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004025 * itself once used.
4026 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004027 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004028
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004029 /* that's OK for us now, let's move on to next analysers */
4030 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004031
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004032 return_bad_req:
4033 /* We centralize bad requests processing here */
4034 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4035 /* we detected a parsing error. We want to archive this request
4036 * in the dedicated proxy area for later troubleshooting.
4037 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004038 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004039 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004040
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004041 txn->req.msg_state = HTTP_MSG_ERROR;
4042 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004043 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004044
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004045 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004046 if (s->listener->counters)
4047 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004048
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004049 return_prx_cond:
4050 if (!(s->flags & SN_ERR_MASK))
4051 s->flags |= SN_ERR_PRXCOND;
4052 if (!(s->flags & SN_FINST_MASK))
4053 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004054
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004055 req->analysers = 0;
4056 req->analyse_exp = TICK_ETERNITY;
4057 return 0;
4058}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004059
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004060/* This function performs all the processing enabled for the current request.
4061 * It returns 1 if the processing can continue on next analysers, or zero if it
4062 * needs more data, encounters an error, or wants to immediately abort the
4063 * request. It relies on buffers flags, and updates s->req->analysers.
4064 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004065int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004066{
4067 struct http_txn *txn = &s->txn;
4068 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004069 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004070
Willy Tarreau655dce92009-11-08 13:10:58 +01004071 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004072 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004073 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004074 return 0;
4075 }
4076
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004077 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 +02004078 now_ms, __FUNCTION__,
4079 s,
4080 req,
4081 req->rex, req->wex,
4082 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004083 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004084 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004085
William Lallemand82fe75c2012-10-23 10:25:10 +02004086 if (s->fe->comp || s->be->comp)
4087 select_compression_request_header(s, req->buf);
4088
Willy Tarreau59234e92008-11-30 23:51:27 +01004089 /*
4090 * Right now, we know that we have processed the entire headers
4091 * and that unwanted requests have been filtered out. We can do
4092 * whatever we want with the remaining request. Also, now we
4093 * may have separate values for ->fe, ->be.
4094 */
Willy Tarreau06619262006-12-17 08:37:22 +01004095
Willy Tarreau59234e92008-11-30 23:51:27 +01004096 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004097 * If HTTP PROXY is set we simply get remote server address parsing
4098 * incoming request. Note that this requires that a connection is
4099 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004100 */
4101 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004102 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004103 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004104
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004105 /* Note that for now we don't reuse existing proxy connections */
4106 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004107 txn->req.msg_state = HTTP_MSG_ERROR;
4108 txn->status = 500;
4109 req->analysers = 0;
4110 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4111
4112 if (!(s->flags & SN_ERR_MASK))
4113 s->flags |= SN_ERR_RESOURCE;
4114 if (!(s->flags & SN_FINST_MASK))
4115 s->flags |= SN_FINST_R;
4116
4117 return 0;
4118 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004119
4120 path = http_get_path(txn);
4121 url2sa(req->buf->p + msg->sl.rq.u,
4122 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004123 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004124 /* if the path was found, we have to remove everything between
4125 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4126 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4127 * u_l characters by a single "/".
4128 */
4129 if (path) {
4130 char *cur_ptr = req->buf->p;
4131 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4132 int delta;
4133
4134 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4135 http_msg_move_end(&txn->req, delta);
4136 cur_end += delta;
4137 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4138 goto return_bad_req;
4139 }
4140 else {
4141 char *cur_ptr = req->buf->p;
4142 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4143 int delta;
4144
4145 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4146 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4147 http_msg_move_end(&txn->req, delta);
4148 cur_end += delta;
4149 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4150 goto return_bad_req;
4151 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004152 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004153
Willy Tarreau59234e92008-11-30 23:51:27 +01004154 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004155 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004156 * Note that doing so might move headers in the request, but
4157 * the fields will stay coherent and the URI will not move.
4158 * This should only be performed in the backend.
4159 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004160 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004161 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4162 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004163
Willy Tarreau59234e92008-11-30 23:51:27 +01004164 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004165 * 8: the appsession cookie was looked up very early in 1.2,
4166 * so let's do the same now.
4167 */
4168
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004169 /* It needs to look into the URI unless persistence must be ignored */
4170 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004171 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 +01004172 }
4173
William Lallemanda73203e2012-03-12 12:48:57 +01004174 /* add unique-id if "header-unique-id" is specified */
4175
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004176 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4177 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4178 goto return_bad_req;
4179 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004180 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004181 }
William Lallemanda73203e2012-03-12 12:48:57 +01004182
4183 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004184 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4185 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004186 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004187 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004188 goto return_bad_req;
4189 }
4190
Cyril Bontéb21570a2009-11-29 20:04:48 +01004191 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004192 * 9: add X-Forwarded-For if either the frontend or the backend
4193 * asks for it.
4194 */
4195 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004196 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004197 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004198 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4199 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004200 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004201 /* The header is set to be added only if none is present
4202 * and we found it, so don't do anything.
4203 */
4204 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004205 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004206 /* Add an X-Forwarded-For header unless the source IP is
4207 * in the 'except' network range.
4208 */
4209 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004210 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004211 != s->fe->except_net.s_addr) &&
4212 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004213 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004214 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004215 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004216 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004217 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004218
4219 /* Note: we rely on the backend to get the header name to be used for
4220 * x-forwarded-for, because the header is really meant for the backends.
4221 * However, if the backend did not specify any option, we have to rely
4222 * on the frontend's header name.
4223 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004224 if (s->be->fwdfor_hdr_len) {
4225 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004226 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004227 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004228 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004229 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004230 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004231 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 +01004232
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004233 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004234 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004235 }
4236 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004237 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004238 /* FIXME: for the sake of completeness, we should also support
4239 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004240 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004241 int len;
4242 char pn[INET6_ADDRSTRLEN];
4243 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004244 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004245 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004246
Willy Tarreau59234e92008-11-30 23:51:27 +01004247 /* Note: we rely on the backend to get the header name to be used for
4248 * x-forwarded-for, because the header is really meant for the backends.
4249 * However, if the backend did not specify any option, we have to rely
4250 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004251 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004252 if (s->be->fwdfor_hdr_len) {
4253 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004254 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004255 } else {
4256 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004257 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004258 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004259 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004260
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004261 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004262 goto return_bad_req;
4263 }
4264 }
4265
4266 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004267 * 10: add X-Original-To if either the frontend or the backend
4268 * asks for it.
4269 */
4270 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4271
4272 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004273 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004274 /* Add an X-Original-To header unless the destination IP is
4275 * in the 'except' network range.
4276 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004277 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004278
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004279 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004280 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004281 (((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 +02004282 != s->fe->except_to.s_addr) &&
4283 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004284 (((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 +02004285 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004286 int len;
4287 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004288 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004289
4290 /* Note: we rely on the backend to get the header name to be used for
4291 * x-original-to, because the header is really meant for the backends.
4292 * However, if the backend did not specify any option, we have to rely
4293 * on the frontend's header name.
4294 */
4295 if (s->be->orgto_hdr_len) {
4296 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004297 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004298 } else {
4299 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004300 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004301 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004302 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 +02004303
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004304 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004305 goto return_bad_req;
4306 }
4307 }
4308 }
4309
Willy Tarreau50fc7772012-11-11 22:19:57 +01004310 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4311 * If an "Upgrade" token is found, the header is left untouched in order not to have
4312 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4313 * "Upgrade" is present in the Connection header.
4314 */
4315 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4316 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004317 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4318 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004319 unsigned int want_flags = 0;
4320
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004321 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004322 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004323 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4324 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004325 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004326 want_flags |= TX_CON_CLO_SET;
4327 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004328 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004329 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4330 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004331 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004332 want_flags |= TX_CON_KAL_SET;
4333 }
4334
4335 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004336 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004337 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004338
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004339
Willy Tarreau522d6c02009-12-06 18:49:18 +01004340 /* If we have no server assigned yet and we're balancing on url_param
4341 * with a POST request, we may be interested in checking the body for
4342 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004343 */
4344 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4345 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004346 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004347 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004348 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004349 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004350
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004351 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004352 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004353#ifdef TCP_QUICKACK
4354 /* We expect some data from the client. Unless we know for sure
4355 * we already have a full request, we have to re-enable quick-ack
4356 * in case we previously disabled it, otherwise we might cause
4357 * the client to delay further data.
4358 */
4359 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004360 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004361 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004362 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004363 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004364#endif
4365 }
Willy Tarreau03945942009-12-22 16:50:27 +01004366
Willy Tarreau59234e92008-11-30 23:51:27 +01004367 /*************************************************************
4368 * OK, that's finished for the headers. We have done what we *
4369 * could. Let's switch to the DATA state. *
4370 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004371 req->analyse_exp = TICK_ETERNITY;
4372 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004373
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004374 /* if the server closes the connection, we want to immediately react
4375 * and close the socket to save packets and syscalls.
4376 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004377 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4378 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004379
Willy Tarreau59234e92008-11-30 23:51:27 +01004380 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004381 /* OK let's go on with the BODY now */
4382 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004383
Willy Tarreau59234e92008-11-30 23:51:27 +01004384 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004385 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004386 /* we detected a parsing error. We want to archive this request
4387 * in the dedicated proxy area for later troubleshooting.
4388 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004389 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004390 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004391
Willy Tarreau59234e92008-11-30 23:51:27 +01004392 txn->req.msg_state = HTTP_MSG_ERROR;
4393 txn->status = 400;
4394 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004395 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004396
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004397 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004398 if (s->listener->counters)
4399 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004400
Willy Tarreau59234e92008-11-30 23:51:27 +01004401 if (!(s->flags & SN_ERR_MASK))
4402 s->flags |= SN_ERR_PRXCOND;
4403 if (!(s->flags & SN_FINST_MASK))
4404 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004405 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004406}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004407
Willy Tarreau60b85b02008-11-30 23:28:40 +01004408/* This function is an analyser which processes the HTTP tarpit. It always
4409 * returns zero, at the beginning because it prevents any other processing
4410 * from occurring, and at the end because it terminates the request.
4411 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004412int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004413{
4414 struct http_txn *txn = &s->txn;
4415
4416 /* This connection is being tarpitted. The CLIENT side has
4417 * already set the connect expiration date to the right
4418 * timeout. We just have to check that the client is still
4419 * there and that the timeout has not expired.
4420 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004421 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004422 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004423 !tick_is_expired(req->analyse_exp, now_ms))
4424 return 0;
4425
4426 /* We will set the queue timer to the time spent, just for
4427 * logging purposes. We fake a 500 server error, so that the
4428 * attacker will not suspect his connection has been tarpitted.
4429 * It will not cause trouble to the logs because we can exclude
4430 * the tarpitted connections by filtering on the 'PT' status flags.
4431 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004432 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4433
4434 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004435 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004436 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004437
4438 req->analysers = 0;
4439 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004440
Willy Tarreau60b85b02008-11-30 23:28:40 +01004441 if (!(s->flags & SN_ERR_MASK))
4442 s->flags |= SN_ERR_PRXCOND;
4443 if (!(s->flags & SN_FINST_MASK))
4444 s->flags |= SN_FINST_T;
4445 return 0;
4446}
4447
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004448/* This function is an analyser which waits for the HTTP request body. It waits
4449 * for either the buffer to be full, or the full advertised contents to have
4450 * reached the buffer. It must only be called after the standard HTTP request
4451 * processing has occurred, because it expects the request to be parsed and will
4452 * look for the Expect header. It may send a 100-Continue interim response. It
4453 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4454 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4455 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004456 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004457int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004458{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004459 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004460 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004461
4462 /* We have to parse the HTTP request body to find any required data.
4463 * "balance url_param check_post" should have been the only way to get
4464 * into this. We were brought here after HTTP header analysis, so all
4465 * related structures are ready.
4466 */
4467
Willy Tarreau890988f2014-04-10 11:59:33 +02004468 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4469 /* This is the first call */
4470 if (msg->msg_state < HTTP_MSG_BODY)
4471 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004472
Willy Tarreau890988f2014-04-10 11:59:33 +02004473 if (msg->msg_state < HTTP_MSG_100_SENT) {
4474 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4475 * send an HTTP/1.1 100 Continue intermediate response.
4476 */
4477 if (msg->flags & HTTP_MSGF_VER_11) {
4478 struct hdr_ctx ctx;
4479 ctx.idx = 0;
4480 /* Expect is allowed in 1.1, look for it */
4481 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4482 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4483 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4484 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004485 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004486 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004487 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004488
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004489 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004490 * req->buf->p still points to the beginning of the message. We
4491 * must save the body in msg->next because it survives buffer
4492 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004493 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004494 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004495
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004496 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004497 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4498 else
4499 msg->msg_state = HTTP_MSG_DATA;
4500 }
4501
Willy Tarreau890988f2014-04-10 11:59:33 +02004502 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4503 /* We're in content-length mode, we just have to wait for enough data. */
4504 if (req->buf->i - msg->sov < msg->body_len)
4505 goto missing_data;
4506
4507 /* OK we have everything we need now */
4508 goto http_end;
4509 }
4510
4511 /* OK here we're parsing a chunked-encoded message */
4512
Willy Tarreau522d6c02009-12-06 18:49:18 +01004513 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004514 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004515 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004516 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004517 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004518 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004519
Willy Tarreau115acb92009-12-26 13:56:06 +01004520 if (!ret)
4521 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004522 else if (ret < 0) {
4523 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004524 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004525 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004526 }
4527
Willy Tarreaud98cf932009-12-27 22:54:55 +01004528 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004529 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004530 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004531 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004532 if (msg->msg_state == HTTP_MSG_TRAILERS)
4533 goto http_end;
4534
Willy Tarreau226071e2014-04-10 11:55:45 +02004535 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004536 goto http_end;
4537
4538 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004539 /* we get here if we need to wait for more data. If the buffer is full,
4540 * we have the maximum we can expect.
4541 */
4542 if (buffer_full(req->buf, global.tune.maxrewrite))
4543 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004544
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004545 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004546 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004547 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004548
4549 if (!(s->flags & SN_ERR_MASK))
4550 s->flags |= SN_ERR_CLITO;
4551 if (!(s->flags & SN_FINST_MASK))
4552 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004553 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004554 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004555
4556 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004557 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004558 /* Not enough data. We'll re-use the http-request
4559 * timeout here. Ideally, we should set the timeout
4560 * relative to the accept() date. We just set the
4561 * request timeout once at the beginning of the
4562 * request.
4563 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004564 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004565 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004566 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004567 return 0;
4568 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004569
4570 http_end:
4571 /* The situation will not evolve, so let's give up on the analysis. */
4572 s->logs.tv_request = now; /* update the request timer to reflect full request */
4573 req->analysers &= ~an_bit;
4574 req->analyse_exp = TICK_ETERNITY;
4575 return 1;
4576
4577 return_bad_req: /* let's centralize all bad requests */
4578 txn->req.msg_state = HTTP_MSG_ERROR;
4579 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004580 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004581
Willy Tarreau79ebac62010-06-07 13:47:49 +02004582 if (!(s->flags & SN_ERR_MASK))
4583 s->flags |= SN_ERR_PRXCOND;
4584 if (!(s->flags & SN_FINST_MASK))
4585 s->flags |= SN_FINST_R;
4586
Willy Tarreau522d6c02009-12-06 18:49:18 +01004587 return_err_msg:
4588 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004589 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004590 if (s->listener->counters)
4591 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004592 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004593}
4594
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004595/* send a server's name with an outgoing request over an established connection.
4596 * Note: this function is designed to be called once the request has been scheduled
4597 * for being forwarded. This is the reason why it rewinds the buffer before
4598 * proceeding.
4599 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004600int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004601
4602 struct hdr_ctx ctx;
4603
Mark Lamourinec2247f02012-01-04 13:02:01 -05004604 char *hdr_name = be->server_id_hdr_name;
4605 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004606 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004607 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004608 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004609
William Lallemandd9e90662012-01-30 17:27:17 +01004610 ctx.idx = 0;
4611
Willy Tarreau211cdec2014-04-17 20:18:08 +02004612 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004613 if (old_o) {
4614 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004615 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004616 txn->req.next += old_o;
4617 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004618 }
4619
Willy Tarreau9b28e032012-10-12 23:49:43 +02004620 old_i = chn->buf->i;
4621 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 -05004622 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004623 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004624 }
4625
4626 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004627 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004628 memcpy(hdr_val, hdr_name, hdr_name_len);
4629 hdr_val += hdr_name_len;
4630 *hdr_val++ = ':';
4631 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004632 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4633 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004634
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004635 if (old_o) {
4636 /* If this was a forwarded request, we must readjust the amount of
4637 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004638 * variations. Note that the current state is >= HTTP_MSG_BODY,
4639 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004640 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004641 old_o += chn->buf->i - old_i;
4642 b_adv(chn->buf, old_o);
4643 txn->req.next -= old_o;
4644 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004645 }
4646
Mark Lamourinec2247f02012-01-04 13:02:01 -05004647 return 0;
4648}
4649
Willy Tarreau610ecce2010-01-04 21:15:02 +01004650/* Terminate current transaction and prepare a new one. This is very tricky
4651 * right now but it works.
4652 */
4653void http_end_txn_clean_session(struct session *s)
4654{
Willy Tarreau068621e2013-12-23 15:11:25 +01004655 int prev_status = s->txn.status;
4656
Willy Tarreau610ecce2010-01-04 21:15:02 +01004657 /* FIXME: We need a more portable way of releasing a backend's and a
4658 * server's connections. We need a safer way to reinitialize buffer
4659 * flags. We also need a more accurate method for computing per-request
4660 * data.
4661 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662
Willy Tarreau4213a112013-12-15 10:25:42 +01004663 /* unless we're doing keep-alive, we want to quickly close the connection
4664 * to the server.
4665 */
4666 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4667 !si_conn_ready(s->req->cons)) {
4668 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4669 si_shutr(s->req->cons);
4670 si_shutw(s->req->cons);
4671 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004672
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004673 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004674 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004675 if (unlikely(s->srv_conn))
4676 sess_change_server(s, NULL);
4677 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678
4679 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4680 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004681 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004682
4683 if (s->txn.status) {
4684 int n;
4685
4686 n = s->txn.status / 100;
4687 if (n < 1 || n > 5)
4688 n = 0;
4689
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004690 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004691 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004692 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004693 s->fe->fe_counters.p.http.comp_rsp++;
4694 }
Willy Tarreau24657792010-02-26 10:30:28 +01004695 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004696 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004697 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004698 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004699 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004700 s->be->be_counters.p.http.comp_rsp++;
4701 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004702 }
4703
4704 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004705 s->logs.bytes_in -= s->req->buf->i;
4706 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004707
4708 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004709 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004710 !(s->flags & SN_MONITOR) &&
4711 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4712 s->do_log(s);
4713 }
4714
4715 s->logs.accept_date = date; /* user-visible date for logging */
4716 s->logs.tv_accept = now; /* corrected date for internal use */
4717 tv_zero(&s->logs.tv_request);
4718 s->logs.t_queue = -1;
4719 s->logs.t_connect = -1;
4720 s->logs.t_data = -1;
4721 s->logs.t_close = 0;
4722 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4723 s->logs.srv_queue_size = 0; /* we will get this number soon */
4724
Willy Tarreau9b28e032012-10-12 23:49:43 +02004725 s->logs.bytes_in = s->req->total = s->req->buf->i;
4726 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004727
4728 if (s->pend_pos)
4729 pendconn_free(s->pend_pos);
4730
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004731 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004732 if (s->flags & SN_CURR_SESS) {
4733 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004734 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004735 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004736 if (may_dequeue_tasks(objt_server(s->target), s->be))
4737 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004738 }
4739
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004740 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004741
Willy Tarreau4213a112013-12-15 10:25:42 +01004742 /* only release our endpoint if we don't intend to reuse the
4743 * connection.
4744 */
4745 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4746 !si_conn_ready(s->req->cons)) {
4747 si_release_endpoint(s->req->cons);
4748 }
4749
Willy Tarreau610ecce2010-01-04 21:15:02 +01004750 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004751 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004752 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004753 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004754 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004755 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4756 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 +02004757 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 +01004758 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004759
Willy Tarreau610ecce2010-01-04 21:15:02 +01004760 s->txn.meth = 0;
4761 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004762 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004763
4764 if (prev_status == 401 || prev_status == 407) {
4765 /* In HTTP keep-alive mode, if we receive a 401, we still have
4766 * a chance of being able to send the visitor again to the same
4767 * server over the same connection. This is required by some
4768 * broken protocols such as NTLM, and anyway whenever there is
4769 * an opportunity for sending the challenge to the proper place,
4770 * it's better to do it (at least it helps with debugging).
4771 */
4772 s->txn.flags |= TX_PREFER_LAST;
4773 }
4774
Willy Tarreauee55dc02010-06-01 10:56:34 +02004775 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004776 s->req->cons->flags |= SI_FL_INDEP_STR;
4777
Willy Tarreau96e31212011-05-30 18:10:30 +02004778 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004779 s->req->flags |= CF_NEVER_WAIT;
4780 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004781 }
4782
Willy Tarreau610ecce2010-01-04 21:15:02 +01004783 /* if the request buffer is not empty, it means we're
4784 * about to process another request, so send pending
4785 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004786 * Just don't do this if the buffer is close to be full,
4787 * because the request will wait for it to flush a little
4788 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004789 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004790 if (s->req->buf->i) {
4791 if (s->rep->buf->o &&
4792 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4793 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004794 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004795 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004796
4797 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004798 channel_auto_read(s->req);
4799 channel_auto_close(s->req);
4800 channel_auto_read(s->rep);
4801 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004802
Willy Tarreau27375622013-12-17 00:00:28 +01004803 /* we're in keep-alive with an idle connection, monitor it */
4804 si_idle_conn(s->req->cons);
4805
Willy Tarreau342b11c2010-11-24 16:22:09 +01004806 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004807 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004808}
4809
4810
4811/* This function updates the request state machine according to the response
4812 * state machine and buffer flags. It returns 1 if it changes anything (flag
4813 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4814 * it is only used to find when a request/response couple is complete. Both
4815 * this function and its equivalent should loop until both return zero. It
4816 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4817 */
4818int http_sync_req_state(struct session *s)
4819{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004820 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004822 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 unsigned int old_state = txn->req.msg_state;
4824
Willy Tarreau610ecce2010-01-04 21:15:02 +01004825 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4826 return 0;
4827
4828 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004829 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004830 * We can shut the read side unless we want to abort_on_close,
4831 * or we have a POST request. The issue with POST requests is
4832 * that some browsers still send a CRLF after the request, and
4833 * this CRLF must be read so that it does not remain in the kernel
4834 * buffers, otherwise a close could cause an RST on some systems
4835 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004836 * Note that if we're using keep-alive on the client side, we'd
4837 * rather poll now and keep the polling enabled for the whole
4838 * session's life than enabling/disabling it between each
4839 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004840 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004841 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4842 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4843 !(s->be->options & PR_O_ABRT_CLOSE) &&
4844 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004845 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004846
Willy Tarreau40f151a2012-12-20 12:10:09 +01004847 /* if the server closes the connection, we want to immediately react
4848 * and close the socket to save packets and syscalls.
4849 */
4850 chn->cons->flags |= SI_FL_NOHALF;
4851
Willy Tarreau610ecce2010-01-04 21:15:02 +01004852 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4853 goto wait_other_side;
4854
4855 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4856 /* The server has not finished to respond, so we
4857 * don't want to move in order not to upset it.
4858 */
4859 goto wait_other_side;
4860 }
4861
4862 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4863 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004864 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004865 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004866 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004867 goto wait_other_side;
4868 }
4869
4870 /* When we get here, it means that both the request and the
4871 * response have finished receiving. Depending on the connection
4872 * mode, we'll have to wait for the last bytes to leave in either
4873 * direction, and sometimes for a close to be effective.
4874 */
4875
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004876 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4877 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004878 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4879 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004880 }
4881 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4882 /* Option forceclose is set, or either side wants to close,
4883 * let's enforce it now that we're not expecting any new
4884 * data to come. The caller knows the session is complete
4885 * once both states are CLOSED.
4886 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004887 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4888 channel_shutr_now(chn);
4889 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004890 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004891 }
4892 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004893 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4894 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004895 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004896 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4897 channel_auto_read(chn);
4898 txn->req.msg_state = HTTP_MSG_TUNNEL;
4899 chn->flags |= CF_NEVER_WAIT;
4900 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004901 }
4902
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004903 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004904 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004905 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004906
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004907 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004908 txn->req.msg_state = HTTP_MSG_CLOSING;
4909 goto http_msg_closing;
4910 }
4911 else {
4912 txn->req.msg_state = HTTP_MSG_CLOSED;
4913 goto http_msg_closed;
4914 }
4915 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004916 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004917 }
4918
4919 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4920 http_msg_closing:
4921 /* nothing else to forward, just waiting for the output buffer
4922 * to be empty and for the shutw_now to take effect.
4923 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004924 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004925 txn->req.msg_state = HTTP_MSG_CLOSED;
4926 goto http_msg_closed;
4927 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004928 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004929 txn->req.msg_state = HTTP_MSG_ERROR;
4930 goto wait_other_side;
4931 }
4932 }
4933
4934 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4935 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004936 /* see above in MSG_DONE why we only do this in these states */
4937 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4938 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4939 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004940 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 goto wait_other_side;
4942 }
4943
4944 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004945 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004946}
4947
4948
4949/* This function updates the response state machine according to the request
4950 * state machine and buffer flags. It returns 1 if it changes anything (flag
4951 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4952 * it is only used to find when a request/response couple is complete. Both
4953 * this function and its equivalent should loop until both return zero. It
4954 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4955 */
4956int http_sync_res_state(struct session *s)
4957{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004958 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004960 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004961 unsigned int old_state = txn->rsp.msg_state;
4962
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4964 return 0;
4965
4966 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4967 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004968 * still monitor the server connection for a possible close
4969 * while the request is being uploaded, so we don't disable
4970 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004971 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004972 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004973
4974 if (txn->req.msg_state == HTTP_MSG_ERROR)
4975 goto wait_other_side;
4976
4977 if (txn->req.msg_state < HTTP_MSG_DONE) {
4978 /* The client seems to still be sending data, probably
4979 * because we got an error response during an upload.
4980 * We have the choice of either breaking the connection
4981 * or letting it pass through. Let's do the later.
4982 */
4983 goto wait_other_side;
4984 }
4985
4986 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4987 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004988 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004990 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991 goto wait_other_side;
4992 }
4993
4994 /* When we get here, it means that both the request and the
4995 * response have finished receiving. Depending on the connection
4996 * mode, we'll have to wait for the last bytes to leave in either
4997 * direction, and sometimes for a close to be effective.
4998 */
4999
5000 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5001 /* Server-close mode : shut read and wait for the request
5002 * side to close its output buffer. The caller will detect
5003 * when we're in DONE and the other is in CLOSED and will
5004 * catch that for the final cleanup.
5005 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005006 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5007 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005008 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005009 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5010 /* Option forceclose is set, or either side wants to close,
5011 * let's enforce it now that we're not expecting any new
5012 * data to come. The caller knows the session is complete
5013 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005014 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005015 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5016 channel_shutr_now(chn);
5017 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005018 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005019 }
5020 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005021 /* The last possible modes are keep-alive and tunnel. Tunnel will
5022 * need to forward remaining data. Keep-alive will need to monitor
5023 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005024 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005025 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005026 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005027 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5028 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005029 }
5030
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005031 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005032 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005033 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005034 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5035 goto http_msg_closing;
5036 }
5037 else {
5038 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5039 goto http_msg_closed;
5040 }
5041 }
5042 goto wait_other_side;
5043 }
5044
5045 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5046 http_msg_closing:
5047 /* nothing else to forward, just waiting for the output buffer
5048 * to be empty and for the shutw_now to take effect.
5049 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005050 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005051 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5052 goto http_msg_closed;
5053 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005054 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005055 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005056 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005057 if (objt_server(s->target))
5058 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005059 goto wait_other_side;
5060 }
5061 }
5062
5063 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5064 http_msg_closed:
5065 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005066 bi_erase(chn);
5067 channel_auto_close(chn);
5068 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005069 goto wait_other_side;
5070 }
5071
5072 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005073 /* We force the response to leave immediately if we're waiting for the
5074 * other side, since there is no pending shutdown to push it out.
5075 */
5076 if (!channel_is_empty(chn))
5077 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005078 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005079}
5080
5081
5082/* Resync the request and response state machines. Return 1 if either state
5083 * changes.
5084 */
5085int http_resync_states(struct session *s)
5086{
5087 struct http_txn *txn = &s->txn;
5088 int old_req_state = txn->req.msg_state;
5089 int old_res_state = txn->rsp.msg_state;
5090
Willy Tarreau610ecce2010-01-04 21:15:02 +01005091 http_sync_req_state(s);
5092 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005093 if (!http_sync_res_state(s))
5094 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 if (!http_sync_req_state(s))
5096 break;
5097 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005098
Willy Tarreau610ecce2010-01-04 21:15:02 +01005099 /* OK, both state machines agree on a compatible state.
5100 * There are a few cases we're interested in :
5101 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5102 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5103 * directions, so let's simply disable both analysers.
5104 * - HTTP_MSG_CLOSED on the response only means we must abort the
5105 * request.
5106 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5107 * with server-close mode means we've completed one request and we
5108 * must re-initialize the server connection.
5109 */
5110
5111 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5112 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5113 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5114 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5115 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005116 channel_auto_close(s->req);
5117 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005118 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005119 channel_auto_close(s->rep);
5120 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005122 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5123 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005124 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005125 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005126 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005127 channel_auto_close(s->rep);
5128 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005129 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005130 channel_abort(s->req);
5131 channel_auto_close(s->req);
5132 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005133 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005134 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005135 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5136 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005137 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005138 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5139 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5140 /* server-close/keep-alive: terminate this transaction,
5141 * possibly killing the server connection and reinitialize
5142 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143 */
5144 http_end_txn_clean_session(s);
5145 }
5146
Willy Tarreau610ecce2010-01-04 21:15:02 +01005147 return txn->req.msg_state != old_req_state ||
5148 txn->rsp.msg_state != old_res_state;
5149}
5150
Willy Tarreaud98cf932009-12-27 22:54:55 +01005151/* This function is an analyser which forwards request body (including chunk
5152 * sizes if any). It is called as soon as we must forward, even if we forward
5153 * zero byte. The only situation where it must not be called is when we're in
5154 * tunnel mode and we want to forward till the close. It's used both to forward
5155 * remaining data and to resync after end of body. It expects the msg_state to
5156 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5157 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005158 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005159 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005160 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005161int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005162{
5163 struct http_txn *txn = &s->txn;
5164 struct http_msg *msg = &s->txn.req;
5165
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005166 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5167 return 0;
5168
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005169 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005170 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005171 /* Output closed while we were sending data. We must abort and
5172 * wake the other side up.
5173 */
5174 msg->msg_state = HTTP_MSG_ERROR;
5175 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005176 return 1;
5177 }
5178
Willy Tarreaud98cf932009-12-27 22:54:55 +01005179 /* Note that we don't have to send 100-continue back because we don't
5180 * need the data to complete our job, and it's up to the server to
5181 * decide whether to return 100, 417 or anything else in return of
5182 * an "Expect: 100-continue" header.
5183 */
5184
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005185 if (msg->sov) {
5186 /* we have msg->sov which points to the first byte of message
5187 * body, and req->buf.p still points to the beginning of the
5188 * message. We forward the headers now, as we don't need them
5189 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005190 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005191 b_adv(req->buf, msg->sov);
5192 msg->next -= msg->sov;
5193 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005194
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005195 /* The previous analysers guarantee that the state is somewhere
5196 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5197 * msg->next are always correct.
5198 */
5199 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5200 if (msg->flags & HTTP_MSGF_TE_CHNK)
5201 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5202 else
5203 msg->msg_state = HTTP_MSG_DATA;
5204 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005205 }
5206
Willy Tarreau7ba23542014-04-17 21:50:00 +02005207 /* Some post-connect processing might want us to refrain from starting to
5208 * forward data. Currently, the only reason for this is "balance url_param"
5209 * whichs need to parse/process the request after we've enabled forwarding.
5210 */
5211 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5212 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5213 channel_auto_connect(req);
5214 goto missing_data;
5215 }
5216 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5217 }
5218
Willy Tarreau80a92c02014-03-12 10:41:13 +01005219 /* in most states, we should abort in case of early close */
5220 channel_auto_close(req);
5221
Willy Tarreauefdf0942014-04-24 20:08:57 +02005222 if (req->to_forward) {
5223 /* We can't process the buffer's contents yet */
5224 req->flags |= CF_WAKE_WRITE;
5225 goto missing_data;
5226 }
5227
Willy Tarreaud98cf932009-12-27 22:54:55 +01005228 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005229 if (msg->msg_state == HTTP_MSG_DATA) {
5230 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005231 /* we may have some pending data starting at req->buf->p */
5232 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005233 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005234 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005235 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005236 msg->next += msg->chunk_len;
5237 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005238
5239 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005240 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005241 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005242 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005243 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005244 }
5245 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005246 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005247 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005248 * TRAILERS state.
5249 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005250 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005251
Willy Tarreau54d23df2012-10-25 19:04:45 +02005252 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005253 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005254 else if (ret < 0) {
5255 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005256 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005257 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005258 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005259 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005260 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005261 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005262 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005263 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005264 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005265
5266 if (ret == 0)
5267 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005268 else if (ret < 0) {
5269 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005270 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005271 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005272 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005273 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005274 /* we're in MSG_CHUNK_SIZE now */
5275 }
5276 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005277 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005278
5279 if (ret == 0)
5280 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005281 else if (ret < 0) {
5282 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005283 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005284 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005285 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005286 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005287 /* we're in HTTP_MSG_DONE now */
5288 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005290 int old_state = msg->msg_state;
5291
Willy Tarreau610ecce2010-01-04 21:15:02 +01005292 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005293
5294 /* we may have some pending data starting at req->buf->p
5295 * such as last chunk of data or trailers.
5296 */
5297 b_adv(req->buf, msg->next);
5298 msg->next = 0;
5299
Willy Tarreau4fe41902010-06-07 22:27:41 +02005300 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005301 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5302 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005303 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 if (http_resync_states(s)) {
5305 /* some state changes occurred, maybe the analyser
5306 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005307 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005308 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005309 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005310 /* request errors are most likely due to
5311 * the server aborting the transfer.
5312 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005313 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005314 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005315 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005316 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005318 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005320 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005321
5322 /* If "option abortonclose" is set on the backend, we
5323 * want to monitor the client's connection and forward
5324 * any shutdown notification to the server, which will
5325 * decide whether to close or to go on processing the
5326 * request.
5327 */
5328 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005329 channel_auto_read(req);
5330 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005331 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005332 else if (s->txn.meth == HTTP_METH_POST) {
5333 /* POST requests may require to read extra CRLF
5334 * sent by broken browsers and which could cause
5335 * an RST to be sent upon close on some systems
5336 * (eg: Linux).
5337 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005338 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005339 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005340
Willy Tarreau610ecce2010-01-04 21:15:02 +01005341 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005342 }
5343 }
5344
Willy Tarreaud98cf932009-12-27 22:54:55 +01005345 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005346 /* we may have some pending data starting at req->buf->p */
5347 b_adv(req->buf, msg->next);
5348 msg->next = 0;
5349 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5350
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005351 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005352 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005353 if (!(s->flags & SN_ERR_MASK))
5354 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005355 if (!(s->flags & SN_FINST_MASK)) {
5356 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5357 s->flags |= SN_FINST_H;
5358 else
5359 s->flags |= SN_FINST_D;
5360 }
5361
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005362 s->fe->fe_counters.cli_aborts++;
5363 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005364 if (objt_server(s->target))
5365 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005366
5367 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005368 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005369
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005370 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005371 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005372 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005373
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005374 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005375 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005376 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005377 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005378 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005379
Willy Tarreau5c620922011-05-11 19:56:11 +02005380 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005381 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005382 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005383 * modes are already handled by the stream sock layer. We must not do
5384 * this in content-length mode because it could present the MSG_MORE
5385 * flag with the last block of forwarded data, which would cause an
5386 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005387 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005388 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005389 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005390
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005391 return 0;
5392
Willy Tarreaud98cf932009-12-27 22:54:55 +01005393 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005394 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005395 if (s->listener->counters)
5396 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005397
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005398 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005399 /* we may have some pending data starting at req->buf->p */
5400 b_adv(req->buf, msg->next);
5401 msg->next = 0;
5402
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005403 txn->req.msg_state = HTTP_MSG_ERROR;
5404 if (txn->status) {
5405 /* Note: we don't send any error if some data were already sent */
5406 stream_int_retnclose(req->prod, NULL);
5407 } else {
5408 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005409 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005410 }
5411 req->analysers = 0;
5412 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005413
5414 if (!(s->flags & SN_ERR_MASK))
5415 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005416 if (!(s->flags & SN_FINST_MASK)) {
5417 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5418 s->flags |= SN_FINST_H;
5419 else
5420 s->flags |= SN_FINST_D;
5421 }
5422 return 0;
5423
5424 aborted_xfer:
5425 txn->req.msg_state = HTTP_MSG_ERROR;
5426 if (txn->status) {
5427 /* Note: we don't send any error if some data were already sent */
5428 stream_int_retnclose(req->prod, NULL);
5429 } else {
5430 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005431 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005432 }
5433 req->analysers = 0;
5434 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5435
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005436 s->fe->fe_counters.srv_aborts++;
5437 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005438 if (objt_server(s->target))
5439 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005440
5441 if (!(s->flags & SN_ERR_MASK))
5442 s->flags |= SN_ERR_SRVCL;
5443 if (!(s->flags & SN_FINST_MASK)) {
5444 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5445 s->flags |= SN_FINST_H;
5446 else
5447 s->flags |= SN_FINST_D;
5448 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005449 return 0;
5450}
5451
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005452/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5453 * processing can continue on next analysers, or zero if it either needs more
5454 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5455 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5456 * when it has nothing left to do, and may remove any analyser when it wants to
5457 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005458 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005459int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005460{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005461 struct http_txn *txn = &s->txn;
5462 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005463 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005464 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005465 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005466 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005467
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005468 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 +02005469 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005470 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005471 rep,
5472 rep->rex, rep->wex,
5473 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005474 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005475 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005476
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005477 /*
5478 * Now parse the partial (or complete) lines.
5479 * We will check the response syntax, and also join multi-line
5480 * headers. An index of all the lines will be elaborated while
5481 * parsing.
5482 *
5483 * For the parsing, we use a 28 states FSM.
5484 *
5485 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005486 * rep->buf->p = beginning of response
5487 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5488 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005489 * msg->eol = end of current header or line (LF or CRLF)
5490 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005491 */
5492
Willy Tarreau628c40c2014-04-24 19:11:26 +02005493 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005494 /* There's a protected area at the end of the buffer for rewriting
5495 * purposes. We don't want to start to parse the request if the
5496 * protected area is affected, because we may have to move processed
5497 * data later, which is much more complicated.
5498 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005499 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005500 if (unlikely(!channel_reserved(rep))) {
5501 /* some data has still not left the buffer, wake us once that's done */
5502 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5503 goto abort_response;
5504 channel_dont_close(rep);
5505 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005506 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005507 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005508 }
5509
Willy Tarreau379357a2013-06-08 12:55:46 +02005510 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5511 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5512 buffer_slow_realign(rep->buf);
5513
Willy Tarreau9b28e032012-10-12 23:49:43 +02005514 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005515 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005516 }
5517
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005518 /* 1: we might have to print this header in debug mode */
5519 if (unlikely((global.mode & MODE_DEBUG) &&
5520 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005521 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005522 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005523
Willy Tarreau9b28e032012-10-12 23:49:43 +02005524 sol = rep->buf->p;
5525 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005526 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005527
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005528 sol += hdr_idx_first_pos(&txn->hdr_idx);
5529 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005530
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005531 while (cur_idx) {
5532 eol = sol + txn->hdr_idx.v[cur_idx].len;
5533 debug_hdr("srvhdr", s, sol, eol);
5534 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5535 cur_idx = txn->hdr_idx.v[cur_idx].next;
5536 }
5537 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005538
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005539 /*
5540 * Now we quickly check if we have found a full valid response.
5541 * If not so, we check the FD and buffer states before leaving.
5542 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005543 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005544 * responses are checked first.
5545 *
5546 * Depending on whether the client is still there or not, we
5547 * may send an error response back or not. Note that normally
5548 * we should only check for HTTP status there, and check I/O
5549 * errors somewhere else.
5550 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005551
Willy Tarreau655dce92009-11-08 13:10:58 +01005552 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005553 /* Invalid response */
5554 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5555 /* we detected a parsing error. We want to archive this response
5556 * in the dedicated proxy area for later troubleshooting.
5557 */
5558 hdr_response_bad:
5559 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005560 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005561
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005562 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005563 if (objt_server(s->target)) {
5564 objt_server(s->target)->counters.failed_resp++;
5565 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005566 }
Willy Tarreau64648412010-03-05 10:41:54 +01005567 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005568 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005569 rep->analysers = 0;
5570 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005571 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005572 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005573 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005574
5575 if (!(s->flags & SN_ERR_MASK))
5576 s->flags |= SN_ERR_PRXCOND;
5577 if (!(s->flags & SN_FINST_MASK))
5578 s->flags |= SN_FINST_H;
5579
5580 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005581 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005582
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005583 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005584 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005585 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005586 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005587 goto hdr_response_bad;
5588 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005589
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005590 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005591 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005592 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005593 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005594 else if (txn->flags & TX_NOT_FIRST)
5595 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005596
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005597 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005598 if (objt_server(s->target)) {
5599 objt_server(s->target)->counters.failed_resp++;
5600 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005601 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005602
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005603 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005604 rep->analysers = 0;
5605 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005606 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005607 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005608 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005609
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005610 if (!(s->flags & SN_ERR_MASK))
5611 s->flags |= SN_ERR_SRVCL;
5612 if (!(s->flags & SN_FINST_MASK))
5613 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005614 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005615 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005616
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005617 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005618 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005619 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005620 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005621 else if (txn->flags & TX_NOT_FIRST)
5622 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005623
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005624 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005625 if (objt_server(s->target)) {
5626 objt_server(s->target)->counters.failed_resp++;
5627 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005628 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005629
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005630 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005631 rep->analysers = 0;
5632 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005633 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005634 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005635 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005636
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005637 if (!(s->flags & SN_ERR_MASK))
5638 s->flags |= SN_ERR_SRVTO;
5639 if (!(s->flags & SN_FINST_MASK))
5640 s->flags |= SN_FINST_H;
5641 return 0;
5642 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005643
Willy Tarreauf003d372012-11-26 13:35:37 +01005644 /* client abort with an abortonclose */
5645 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5646 s->fe->fe_counters.cli_aborts++;
5647 s->be->be_counters.cli_aborts++;
5648 if (objt_server(s->target))
5649 objt_server(s->target)->counters.cli_aborts++;
5650
5651 rep->analysers = 0;
5652 channel_auto_close(rep);
5653
5654 txn->status = 400;
5655 bi_erase(rep);
5656 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5657
5658 if (!(s->flags & SN_ERR_MASK))
5659 s->flags |= SN_ERR_CLICL;
5660 if (!(s->flags & SN_FINST_MASK))
5661 s->flags |= SN_FINST_H;
5662
5663 /* process_session() will take care of the error */
5664 return 0;
5665 }
5666
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005667 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005668 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005669 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005670 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005671 else if (txn->flags & TX_NOT_FIRST)
5672 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005673
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005674 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005675 if (objt_server(s->target)) {
5676 objt_server(s->target)->counters.failed_resp++;
5677 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005678 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005679
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005680 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005681 rep->analysers = 0;
5682 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005683 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005684 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005685 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005686
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005687 if (!(s->flags & SN_ERR_MASK))
5688 s->flags |= SN_ERR_SRVCL;
5689 if (!(s->flags & SN_FINST_MASK))
5690 s->flags |= SN_FINST_H;
5691 return 0;
5692 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005693
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005694 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005695 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005696 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005697 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005698 else if (txn->flags & TX_NOT_FIRST)
5699 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005700
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005701 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005702 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005703 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005704
5705 if (!(s->flags & SN_ERR_MASK))
5706 s->flags |= SN_ERR_CLICL;
5707 if (!(s->flags & SN_FINST_MASK))
5708 s->flags |= SN_FINST_H;
5709
5710 /* process_session() will take care of the error */
5711 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005712 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005713
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005714 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005715 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005716 return 0;
5717 }
5718
5719 /* More interesting part now : we know that we have a complete
5720 * response which at least looks like HTTP. We have an indicator
5721 * of each header's length, so we can parse them quickly.
5722 */
5723
5724 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005725 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005726
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005727 /*
5728 * 1: get the status code
5729 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005730 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005731 if (n < 1 || n > 5)
5732 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005733 /* when the client triggers a 4xx from the server, it's most often due
5734 * to a missing object or permission. These events should be tracked
5735 * because if they happen often, it may indicate a brute force or a
5736 * vulnerability scan.
5737 */
5738 if (n == 4)
5739 session_inc_http_err_ctr(s);
5740
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005741 if (objt_server(s->target))
5742 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005743
Willy Tarreau5b154472009-12-21 20:11:07 +01005744 /* check if the response is HTTP/1.1 or above */
5745 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005746 ((rep->buf->p[5] > '1') ||
5747 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005748 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005749
5750 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005751 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 +01005752
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005753 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005754 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005755
Willy Tarreau9b28e032012-10-12 23:49:43 +02005756 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005757
Willy Tarreau39650402010-03-15 19:44:39 +01005758 /* Adjust server's health based on status code. Note: status codes 501
5759 * and 505 are triggered on demand by client request, so we must not
5760 * count them as server failures.
5761 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005762 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005763 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005764 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005765 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005766 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005767 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005768
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005769 /*
5770 * 2: check for cacheability.
5771 */
5772
5773 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005774 case 100:
5775 /*
5776 * We may be facing a 100-continue response, in which case this
5777 * is not the right response, and we're waiting for the next one.
5778 * Let's allow this response to go to the client and wait for the
5779 * next one.
5780 */
5781 hdr_idx_init(&txn->hdr_idx);
5782 msg->next -= channel_forward(rep, msg->next);
5783 msg->msg_state = HTTP_MSG_RPBEFORE;
5784 txn->status = 0;
5785 s->logs.t_data = -1; /* was not a response yet */
5786 goto next_one;
5787
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005788 case 200:
5789 case 203:
5790 case 206:
5791 case 300:
5792 case 301:
5793 case 410:
5794 /* RFC2616 @13.4:
5795 * "A response received with a status code of
5796 * 200, 203, 206, 300, 301 or 410 MAY be stored
5797 * by a cache (...) unless a cache-control
5798 * directive prohibits caching."
5799 *
5800 * RFC2616 @9.5: POST method :
5801 * "Responses to this method are not cacheable,
5802 * unless the response includes appropriate
5803 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005804 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005805 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005806 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005807 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5808 break;
5809 default:
5810 break;
5811 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005812
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005813 /*
5814 * 3: we may need to capture headers
5815 */
5816 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005817 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005818 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005819 txn->rsp.cap, s->fe->rsp_cap);
5820
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005821 /* 4: determine the transfer-length.
5822 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5823 * the presence of a message-body in a RESPONSE and its transfer length
5824 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005825 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005826 * All responses to the HEAD request method MUST NOT include a
5827 * message-body, even though the presence of entity-header fields
5828 * might lead one to believe they do. All 1xx (informational), 204
5829 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5830 * message-body. All other responses do include a message-body,
5831 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005832 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005833 * 1. Any response which "MUST NOT" include a message-body (such as the
5834 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5835 * always terminated by the first empty line after the header fields,
5836 * regardless of the entity-header fields present in the message.
5837 *
5838 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5839 * the "chunked" transfer-coding (Section 6.2) is used, the
5840 * transfer-length is defined by the use of this transfer-coding.
5841 * If a Transfer-Encoding header field is present and the "chunked"
5842 * transfer-coding is not present, the transfer-length is defined by
5843 * the sender closing the connection.
5844 *
5845 * 3. If a Content-Length header field is present, its decimal value in
5846 * OCTETs represents both the entity-length and the transfer-length.
5847 * If a message is received with both a Transfer-Encoding header
5848 * field and a Content-Length header field, the latter MUST be ignored.
5849 *
5850 * 4. If the message uses the media type "multipart/byteranges", and
5851 * the transfer-length is not otherwise specified, then this self-
5852 * delimiting media type defines the transfer-length. This media
5853 * type MUST NOT be used unless the sender knows that the recipient
5854 * can parse it; the presence in a request of a Range header with
5855 * multiple byte-range specifiers from a 1.1 client implies that the
5856 * client can parse multipart/byteranges responses.
5857 *
5858 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005859 */
5860
5861 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005862 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005863 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005864 * FIXME: should we parse anyway and return an error on chunked encoding ?
5865 */
5866 if (txn->meth == HTTP_METH_HEAD ||
5867 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005868 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005869 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005870 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005871 goto skip_content_length;
5872 }
5873
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005874 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005875 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005876 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005877 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005878 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005879 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5880 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005881 /* bad transfer-encoding (chunked followed by something else) */
5882 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005883 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005884 break;
5885 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005886 }
5887
5888 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5889 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005890 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005891 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005892 signed long long cl;
5893
Willy Tarreauad14f752011-09-02 20:33:27 +02005894 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005895 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005896 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005897 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005898
Willy Tarreauad14f752011-09-02 20:33:27 +02005899 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005900 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005901 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005902 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005903
Willy Tarreauad14f752011-09-02 20:33:27 +02005904 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005905 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005906 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005907 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005908
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005909 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005910 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005911 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005912 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005913
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005914 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005915 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005916 }
5917
William Lallemand82fe75c2012-10-23 10:25:10 +02005918 if (s->fe->comp || s->be->comp)
5919 select_compression_response_header(s, rep->buf);
5920
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005921skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005922 /* Now we have to check if we need to modify the Connection header.
5923 * This is more difficult on the response than it is on the request,
5924 * because we can have two different HTTP versions and we don't know
5925 * how the client will interprete a response. For instance, let's say
5926 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5927 * HTTP/1.1 response without any header. Maybe it will bound itself to
5928 * HTTP/1.0 because it only knows about it, and will consider the lack
5929 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5930 * the lack of header as a keep-alive. Thus we will use two flags
5931 * indicating how a request MAY be understood by the client. In case
5932 * of multiple possibilities, we'll fix the header to be explicit. If
5933 * ambiguous cases such as both close and keepalive are seen, then we
5934 * will fall back to explicit close. Note that we won't take risks with
5935 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005936 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005937 */
5938
Willy Tarreaudc008c52010-02-01 16:20:08 +01005939 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5940 txn->status == 101)) {
5941 /* Either we've established an explicit tunnel, or we're
5942 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005943 * to understand the next protocols. We have to switch to tunnel
5944 * mode, so that we transfer the request and responses then let
5945 * this protocol pass unmodified. When we later implement specific
5946 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005947 * header which contains information about that protocol for
5948 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005949 */
5950 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5951 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005952 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5953 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005954 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5955 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005956 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005957
Willy Tarreau70dffda2014-01-30 03:07:23 +01005958 /* this situation happens when combining pretend-keepalive with httpclose. */
5959 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005960 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5961 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005962 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5963
Willy Tarreau60466522010-01-18 19:08:45 +01005964 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005965 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005966 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5967 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005968
Willy Tarreau60466522010-01-18 19:08:45 +01005969 /* now adjust header transformations depending on current state */
5970 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5971 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5972 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005973 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005974 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005975 }
Willy Tarreau60466522010-01-18 19:08:45 +01005976 else { /* SCL / KAL */
5977 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005978 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005979 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005980 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005981
Willy Tarreau60466522010-01-18 19:08:45 +01005982 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005983 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005984
Willy Tarreau60466522010-01-18 19:08:45 +01005985 /* Some keep-alive responses are converted to Server-close if
5986 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005987 */
Willy Tarreau60466522010-01-18 19:08:45 +01005988 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5989 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005990 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005991 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005992 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005993 }
5994
Willy Tarreau7959a552013-09-23 16:44:27 +02005995 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005996 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005997
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005998 /* end of job, return OK */
5999 rep->analysers &= ~an_bit;
6000 rep->analyse_exp = TICK_ETERNITY;
6001 channel_auto_close(rep);
6002 return 1;
6003
6004 abort_keep_alive:
6005 /* A keep-alive request to the server failed on a network error.
6006 * The client is required to retry. We need to close without returning
6007 * any other information so that the client retries.
6008 */
6009 txn->status = 0;
6010 rep->analysers = 0;
6011 s->req->analysers = 0;
6012 channel_auto_close(rep);
6013 s->logs.logwait = 0;
6014 s->logs.level = 0;
6015 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6016 bi_erase(rep);
6017 stream_int_retnclose(rep->cons, NULL);
6018 return 0;
6019}
6020
6021/* This function performs all the processing enabled for the current response.
6022 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6023 * and updates s->rep->analysers. It might make sense to explode it into several
6024 * other functions. It works like process_request (see indications above).
6025 */
6026int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6027{
6028 struct http_txn *txn = &s->txn;
6029 struct http_msg *msg = &txn->rsp;
6030 struct proxy *cur_proxy;
6031 struct cond_wordlist *wl;
6032 struct http_res_rule *http_res_last_rule = NULL;
6033
6034 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6035 now_ms, __FUNCTION__,
6036 s,
6037 rep,
6038 rep->rex, rep->wex,
6039 rep->flags,
6040 rep->buf->i,
6041 rep->analysers);
6042
6043 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6044 return 0;
6045
6046 rep->analysers &= ~an_bit;
6047 rep->analyse_exp = TICK_ETERNITY;
6048
Willy Tarreau70730dd2014-04-24 18:06:27 +02006049 /* The stats applet needs to adjust the Connection header but we don't
6050 * apply any filter there.
6051 */
6052 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6053 goto skip_filters;
6054
Willy Tarreau58975672014-04-24 21:13:57 +02006055 /*
6056 * We will have to evaluate the filters.
6057 * As opposed to version 1.2, now they will be evaluated in the
6058 * filters order and not in the header order. This means that
6059 * each filter has to be validated among all headers.
6060 *
6061 * Filters are tried with ->be first, then with ->fe if it is
6062 * different from ->be.
6063 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006064
Willy Tarreau58975672014-04-24 21:13:57 +02006065 cur_proxy = s->be;
6066 while (1) {
6067 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006068
Willy Tarreau58975672014-04-24 21:13:57 +02006069 /* evaluate http-response rules */
6070 if (!http_res_last_rule)
6071 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 +02006072
Willy Tarreau58975672014-04-24 21:13:57 +02006073 /* try headers filters */
6074 if (rule_set->rsp_exp != NULL) {
6075 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6076 return_bad_resp:
6077 if (objt_server(s->target)) {
6078 objt_server(s->target)->counters.failed_resp++;
6079 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006080 }
Willy Tarreau58975672014-04-24 21:13:57 +02006081 s->be->be_counters.failed_resp++;
6082 return_srv_prx_502:
6083 rep->analysers = 0;
6084 txn->status = 502;
6085 s->logs.t_data = -1; /* was not a valid response */
6086 rep->prod->flags |= SI_FL_NOLINGER;
6087 bi_erase(rep);
6088 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6089 if (!(s->flags & SN_ERR_MASK))
6090 s->flags |= SN_ERR_PRXCOND;
6091 if (!(s->flags & SN_FINST_MASK))
6092 s->flags |= SN_FINST_H;
6093 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006094 }
Willy Tarreau58975672014-04-24 21:13:57 +02006095 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006096
Willy Tarreau58975672014-04-24 21:13:57 +02006097 /* has the response been denied ? */
6098 if (txn->flags & TX_SVDENY) {
6099 if (objt_server(s->target))
6100 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006101
Willy Tarreau58975672014-04-24 21:13:57 +02006102 s->be->be_counters.denied_resp++;
6103 s->fe->fe_counters.denied_resp++;
6104 if (s->listener->counters)
6105 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006106
Willy Tarreau58975672014-04-24 21:13:57 +02006107 goto return_srv_prx_502;
6108 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006109
Willy Tarreau58975672014-04-24 21:13:57 +02006110 /* add response headers from the rule sets in the same order */
6111 list_for_each_entry(wl, &rule_set->rsp_add, list) {
6112 if (txn->status < 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006113 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006114 if (wl->cond) {
6115 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6116 ret = acl_pass(ret);
6117 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6118 ret = !ret;
6119 if (!ret)
6120 continue;
6121 }
6122 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6123 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006124 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006125
Willy Tarreau58975672014-04-24 21:13:57 +02006126 /* check whether we're already working on the frontend */
6127 if (cur_proxy == s->fe)
6128 break;
6129 cur_proxy = s->fe;
6130 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006131
Willy Tarreau58975672014-04-24 21:13:57 +02006132 /* OK that's all we can do for 1xx responses */
6133 if (unlikely(txn->status < 200))
6134 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006135
Willy Tarreau58975672014-04-24 21:13:57 +02006136 /*
6137 * Now check for a server cookie.
6138 */
6139 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6140 (s->be->options & PR_O_CHK_CACHE))
6141 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006142
Willy Tarreau58975672014-04-24 21:13:57 +02006143 /*
6144 * Check for cache-control or pragma headers if required.
6145 */
6146 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
6147 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006148
Willy Tarreau58975672014-04-24 21:13:57 +02006149 /*
6150 * Add server cookie in the response if needed
6151 */
6152 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6153 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6154 (!(s->flags & SN_DIRECT) ||
6155 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6156 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6157 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6158 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6159 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6160 !(s->flags & SN_IGNORE_PRST)) {
6161 /* the server is known, it's not the one the client requested, or the
6162 * cookie's last seen date needs to be refreshed. We have to
6163 * insert a set-cookie here, except if we want to insert only on POST
6164 * requests and this one isn't. Note that servers which don't have cookies
6165 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006166 */
Willy Tarreau58975672014-04-24 21:13:57 +02006167 if (!objt_server(s->target)->cookie) {
6168 chunk_printf(&trash,
6169 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6170 s->be->cookie_name);
6171 }
6172 else {
6173 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006174
Willy Tarreau58975672014-04-24 21:13:57 +02006175 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6176 /* emit last_date, which is mandatory */
6177 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6178 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6179 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006180
Willy Tarreau58975672014-04-24 21:13:57 +02006181 if (s->be->cookie_maxlife) {
6182 /* emit first_date, which is either the original one or
6183 * the current date.
6184 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006185 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006186 s30tob64(txn->cookie_first_date ?
6187 txn->cookie_first_date >> 2 :
6188 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006189 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006190 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006191 }
Willy Tarreau58975672014-04-24 21:13:57 +02006192 chunk_appendf(&trash, "; path=/");
6193 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006194
Willy Tarreau58975672014-04-24 21:13:57 +02006195 if (s->be->cookie_domain)
6196 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006197
Willy Tarreau58975672014-04-24 21:13:57 +02006198 if (s->be->ck_opts & PR_CK_HTTPONLY)
6199 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006200
Willy Tarreau58975672014-04-24 21:13:57 +02006201 if (s->be->ck_opts & PR_CK_SECURE)
6202 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006203
Willy Tarreau58975672014-04-24 21:13:57 +02006204 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6205 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006206
Willy Tarreau58975672014-04-24 21:13:57 +02006207 txn->flags &= ~TX_SCK_MASK;
6208 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6209 /* the server did not change, only the date was updated */
6210 txn->flags |= TX_SCK_UPDATED;
6211 else
6212 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006213
Willy Tarreau58975672014-04-24 21:13:57 +02006214 /* Here, we will tell an eventual cache on the client side that we don't
6215 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6216 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6217 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006218 */
Willy Tarreau58975672014-04-24 21:13:57 +02006219 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006220
Willy Tarreau58975672014-04-24 21:13:57 +02006221 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006222
Willy Tarreau58975672014-04-24 21:13:57 +02006223 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6224 "Cache-control: private", 22) < 0))
6225 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006226 }
Willy Tarreau58975672014-04-24 21:13:57 +02006227 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006228
Willy Tarreau58975672014-04-24 21:13:57 +02006229 /*
6230 * Check if result will be cacheable with a cookie.
6231 * We'll block the response if security checks have caught
6232 * nasty things such as a cacheable cookie.
6233 */
6234 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6235 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6236 (s->be->options & PR_O_CHK_CACHE)) {
6237 /* we're in presence of a cacheable response containing
6238 * a set-cookie header. We'll block it as requested by
6239 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006240 */
Willy Tarreau58975672014-04-24 21:13:57 +02006241 if (objt_server(s->target))
6242 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006243
Willy Tarreau58975672014-04-24 21:13:57 +02006244 s->be->be_counters.denied_resp++;
6245 s->fe->fe_counters.denied_resp++;
6246 if (s->listener->counters)
6247 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006248
Willy Tarreau58975672014-04-24 21:13:57 +02006249 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6250 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6251 send_log(s->be, LOG_ALERT,
6252 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6253 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6254 goto return_srv_prx_502;
6255 }
Willy Tarreau03945942009-12-22 16:50:27 +01006256
Willy Tarreau70730dd2014-04-24 18:06:27 +02006257 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006258 /*
6259 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6260 * If an "Upgrade" token is found, the header is left untouched in order
6261 * not to have to deal with some client bugs : some of them fail an upgrade
6262 * if anything but "Upgrade" is present in the Connection header.
6263 */
6264 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6265 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6266 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6267 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6268 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006269
Willy Tarreau58975672014-04-24 21:13:57 +02006270 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6271 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6272 /* we want a keep-alive response here. Keep-alive header
6273 * required if either side is not 1.1.
6274 */
6275 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6276 want_flags |= TX_CON_KAL_SET;
6277 }
6278 else {
6279 /* we want a close response here. Close header required if
6280 * the server is 1.1, regardless of the client.
6281 */
6282 if (msg->flags & HTTP_MSGF_VER_11)
6283 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006284 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006285
Willy Tarreau58975672014-04-24 21:13:57 +02006286 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6287 http_change_connection_header(txn, msg, want_flags);
6288 }
6289
6290 skip_header_mangling:
6291 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6292 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6293 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006294
Willy Tarreau58975672014-04-24 21:13:57 +02006295 /* if the user wants to log as soon as possible, without counting
6296 * bytes from the server, then this is the right moment. We have
6297 * to temporarily assign bytes_out to log what we currently have.
6298 */
6299 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6300 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6301 s->logs.bytes_out = txn->rsp.eoh;
6302 s->do_log(s);
6303 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006304 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006305 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006306}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006307
Willy Tarreaud98cf932009-12-27 22:54:55 +01006308/* This function is an analyser which forwards response body (including chunk
6309 * sizes if any). It is called as soon as we must forward, even if we forward
6310 * zero byte. The only situation where it must not be called is when we're in
6311 * tunnel mode and we want to forward till the close. It's used both to forward
6312 * remaining data and to resync after end of body. It expects the msg_state to
6313 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6314 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006315 *
6316 * It is capable of compressing response data both in content-length mode and
6317 * in chunked mode. The state machines follows different flows depending on
6318 * whether content-length and chunked modes are used, since there are no
6319 * trailers in content-length :
6320 *
6321 * chk-mode cl-mode
6322 * ,----- BODY -----.
6323 * / \
6324 * V size > 0 V chk-mode
6325 * .--> SIZE -------------> DATA -------------> CRLF
6326 * | | size == 0 | last byte |
6327 * | v final crlf v inspected |
6328 * | TRAILERS -----------> DONE |
6329 * | |
6330 * `----------------------------------------------'
6331 *
6332 * Compression only happens in the DATA state, and must be flushed in final
6333 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6334 * is performed at once on final states for all bytes parsed, or when leaving
6335 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006336 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006337int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006338{
6339 struct http_txn *txn = &s->txn;
6340 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006341 static struct buffer *tmpbuf = NULL;
6342 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006343 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006344
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006345 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6346 return 0;
6347
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006348 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006349 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006350 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006351 /* Output closed while we were sending data. We must abort and
6352 * wake the other side up.
6353 */
6354 msg->msg_state = HTTP_MSG_ERROR;
6355 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006356 return 1;
6357 }
6358
Willy Tarreau4fe41902010-06-07 22:27:41 +02006359 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006360 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006361
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006362 if (msg->sov) {
6363 /* we have msg->sov which points to the first byte of message
6364 * body, and res->buf.p still points to the beginning of the
6365 * message. We forward the headers now, as we don't need them
6366 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006367 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006368 b_adv(res->buf, msg->sov);
6369 msg->next -= msg->sov;
6370 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006371
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006372 /* The previous analysers guarantee that the state is somewhere
6373 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6374 * msg->next are always correct.
6375 */
6376 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6377 if (msg->flags & HTTP_MSGF_TE_CHNK)
6378 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6379 else
6380 msg->msg_state = HTTP_MSG_DATA;
6381 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006382 }
6383
Willy Tarreauefdf0942014-04-24 20:08:57 +02006384 if (res->to_forward) {
6385 /* We can't process the buffer's contents yet */
6386 res->flags |= CF_WAKE_WRITE;
6387 goto missing_data;
6388 }
6389
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006390 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6391 /* We need a compression buffer in the DATA state to put the
6392 * output of compressed data, and in CRLF state to let the
6393 * TRAILERS state finish the job of removing the trailing CRLF.
6394 */
6395 if (unlikely(tmpbuf == NULL)) {
6396 /* this is the first time we need the compression buffer */
6397 tmpbuf = pool_alloc2(pool2_buffer);
6398 if (tmpbuf == NULL)
6399 goto aborted_xfer; /* no memory */
6400 }
6401
6402 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006403 if (ret < 0) {
6404 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006405 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006406 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006407 compressing = 1;
6408 }
6409
Willy Tarreaud98cf932009-12-27 22:54:55 +01006410 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006411 switch (msg->msg_state - HTTP_MSG_DATA) {
6412 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006413 /* we may have some pending data starting at res->buf->p */
6414 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006415 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006416 if (ret < 0)
6417 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006418
Willy Tarreaud5a67832014-04-21 10:54:27 +02006419 if (msg->chunk_len) {
6420 /* input empty or output full */
6421 if (res->buf->i > msg->next)
6422 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006423 goto missing_data;
6424 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006425 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006426 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006427 if (msg->chunk_len > res->buf->i - msg->next) {
6428 /* output full */
6429 res->flags |= CF_WAKE_WRITE;
6430 goto missing_data;
6431 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006432 msg->next += msg->chunk_len;
6433 msg->chunk_len = 0;
6434 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006435
6436 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006437 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006438 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006439 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006440 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006441 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006442 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006443 /* fall through for HTTP_MSG_CHUNK_CRLF */
6444
6445 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6446 /* we want the CRLF after the data */
6447
6448 ret = http_skip_chunk_crlf(msg);
6449 if (ret == 0)
6450 goto missing_data;
6451 else if (ret < 0) {
6452 if (msg->err_pos >= 0)
6453 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6454 goto return_bad_res;
6455 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006456 /* we're in MSG_CHUNK_SIZE now, fall through */
6457
6458 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006459 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006460 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006461 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006462 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006463
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006464 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006465 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006466 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006467 else if (ret < 0) {
6468 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006469 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006470 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006471 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006472 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006473 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006474
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006475 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006476 if (unlikely(compressing)) {
6477 /* we need to flush output contents before syncing FSMs */
6478 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6479 compressing = 0;
6480 }
6481
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006482 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006483 if (ret == 0)
6484 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006485 else if (ret < 0) {
6486 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006487 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006488 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006489 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006490 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006491
6492 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006493 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006494 if (unlikely(compressing)) {
6495 /* we need to flush output contents before syncing FSMs */
6496 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6497 compressing = 0;
6498 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006499
Willy Tarreauc623c172014-04-18 09:53:50 +02006500 /* we may have some pending data starting at res->buf->p
6501 * such as a last chunk of data or trailers.
6502 */
6503 b_adv(res->buf, msg->next);
6504 msg->next = 0;
6505
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006506 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006507 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006508 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6509 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006510 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006511
Willy Tarreau610ecce2010-01-04 21:15:02 +01006512 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006513 /* some state changes occurred, maybe the analyser
6514 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006515 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006516 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006517 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006518 /* response errors are most likely due to
6519 * the client aborting the transfer.
6520 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006521 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006522 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006523 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006524 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006525 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006526 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006527 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006528 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006529 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006530 }
6531 }
6532
Willy Tarreaud98cf932009-12-27 22:54:55 +01006533 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006534 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006535 if (unlikely(compressing)) {
6536 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006537 compressing = 0;
6538 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006539
Willy Tarreauc623c172014-04-18 09:53:50 +02006540 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6541 b_adv(res->buf, msg->next);
6542 msg->next = 0;
6543 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6544 }
6545
Willy Tarreauf003d372012-11-26 13:35:37 +01006546 if (res->flags & CF_SHUTW)
6547 goto aborted_xfer;
6548
6549 /* stop waiting for data if the input is closed before the end. If the
6550 * client side was already closed, it means that the client has aborted,
6551 * so we don't want to count this as a server abort. Otherwise it's a
6552 * server abort.
6553 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006554 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006555 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006556 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006557 if (!(s->flags & SN_ERR_MASK))
6558 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006559 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006560 if (objt_server(s->target))
6561 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006562 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006563 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006564
Willy Tarreau40dba092010-03-04 18:14:51 +01006565 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006566 if (!s->req->analysers)
6567 goto return_bad_res;
6568
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006569 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006570 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006571 * Similarly, with keep-alive on the client side, we don't want to forward a
6572 * close.
6573 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006574 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006575 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6576 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006577 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006578
Willy Tarreau5c620922011-05-11 19:56:11 +02006579 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006580 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006581 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006582 * modes are already handled by the stream sock layer. We must not do
6583 * this in content-length mode because it could present the MSG_MORE
6584 * flag with the last block of forwarded data, which would cause an
6585 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006586 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006587 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006588 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006589
Willy Tarreaud98cf932009-12-27 22:54:55 +01006590 /* the session handler will take care of timeouts and errors */
6591 return 0;
6592
Willy Tarreau40dba092010-03-04 18:14:51 +01006593 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006594 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006595 if (objt_server(s->target))
6596 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006597
6598 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006599 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006600 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006601 compressing = 0;
6602 }
6603
Willy Tarreauc623c172014-04-18 09:53:50 +02006604 /* we may have some pending data starting at res->buf->p */
6605 if (s->comp_algo == NULL) {
6606 b_adv(res->buf, msg->next);
6607 msg->next = 0;
6608 }
6609
Willy Tarreaud98cf932009-12-27 22:54:55 +01006610 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006611 /* don't send any error message as we're in the body */
6612 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006613 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006614 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006615 if (objt_server(s->target))
6616 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006617
6618 if (!(s->flags & SN_ERR_MASK))
6619 s->flags |= SN_ERR_PRXCOND;
6620 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006621 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006622 return 0;
6623
6624 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006625 if (unlikely(compressing)) {
6626 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6627 compressing = 0;
6628 }
6629
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006630 txn->rsp.msg_state = HTTP_MSG_ERROR;
6631 /* don't send any error message as we're in the body */
6632 stream_int_retnclose(res->cons, NULL);
6633 res->analysers = 0;
6634 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6635
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006636 s->fe->fe_counters.cli_aborts++;
6637 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006638 if (objt_server(s->target))
6639 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006640
6641 if (!(s->flags & SN_ERR_MASK))
6642 s->flags |= SN_ERR_CLICL;
6643 if (!(s->flags & SN_FINST_MASK))
6644 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006645 return 0;
6646}
6647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006648/* Iterate the same filter through all request headers.
6649 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006650 * Since it can manage the switch to another backend, it updates the per-proxy
6651 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006652 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006653int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006655 char term;
6656 char *cur_ptr, *cur_end, *cur_next;
6657 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006658 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006659 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006660 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006662 last_hdr = 0;
6663
Willy Tarreau9b28e032012-10-12 23:49:43 +02006664 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006665 old_idx = 0;
6666
6667 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006668 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006669 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006670 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006671 (exp->action == ACT_ALLOW ||
6672 exp->action == ACT_DENY ||
6673 exp->action == ACT_TARPIT))
6674 return 0;
6675
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006676 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006677 if (!cur_idx)
6678 break;
6679
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006680 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006681 cur_ptr = cur_next;
6682 cur_end = cur_ptr + cur_hdr->len;
6683 cur_next = cur_end + cur_hdr->cr + 1;
6684
6685 /* Now we have one header between cur_ptr and cur_end,
6686 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006687 */
6688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006689 /* The annoying part is that pattern matching needs
6690 * that we modify the contents to null-terminate all
6691 * strings before testing them.
6692 */
6693
6694 term = *cur_end;
6695 *cur_end = '\0';
6696
6697 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6698 switch (exp->action) {
6699 case ACT_SETBE:
6700 /* It is not possible to jump a second time.
6701 * FIXME: should we return an HTTP/500 here so that
6702 * the admin knows there's a problem ?
6703 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006704 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006705 break;
6706
6707 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006708 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006709 last_hdr = 1;
6710 break;
6711
6712 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006713 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006714 last_hdr = 1;
6715 break;
6716
6717 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006718 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006719 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006720 break;
6721
6722 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006723 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006724 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006725 break;
6726
6727 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006728 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6729 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006730 /* FIXME: if the user adds a newline in the replacement, the
6731 * index will not be recalculated for now, and the new line
6732 * will not be counted as a new header.
6733 */
6734
6735 cur_end += delta;
6736 cur_next += delta;
6737 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006738 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006739 break;
6740
6741 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006742 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006743 cur_next += delta;
6744
Willy Tarreaufa355d42009-11-29 18:12:29 +01006745 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006746 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6747 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006748 cur_hdr->len = 0;
6749 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006750 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006751 break;
6752
6753 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006754 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006755 if (cur_end)
6756 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006758 /* keep the link from this header to next one in case of later
6759 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006760 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006761 old_idx = cur_idx;
6762 }
6763 return 0;
6764}
6765
6766
6767/* Apply the filter to the request line.
6768 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6769 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006770 * Since it can manage the switch to another backend, it updates the per-proxy
6771 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006772 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006773int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006774{
6775 char term;
6776 char *cur_ptr, *cur_end;
6777 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006778 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006779 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006780
Willy Tarreau3d300592007-03-18 18:34:41 +01006781 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006782 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006783 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006784 (exp->action == ACT_ALLOW ||
6785 exp->action == ACT_DENY ||
6786 exp->action == ACT_TARPIT))
6787 return 0;
6788 else if (exp->action == ACT_REMOVE)
6789 return 0;
6790
6791 done = 0;
6792
Willy Tarreau9b28e032012-10-12 23:49:43 +02006793 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006794 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006795
6796 /* Now we have the request line between cur_ptr and cur_end */
6797
6798 /* The annoying part is that pattern matching needs
6799 * that we modify the contents to null-terminate all
6800 * strings before testing them.
6801 */
6802
6803 term = *cur_end;
6804 *cur_end = '\0';
6805
6806 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6807 switch (exp->action) {
6808 case ACT_SETBE:
6809 /* It is not possible to jump a second time.
6810 * FIXME: should we return an HTTP/500 here so that
6811 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006812 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006813 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006814 break;
6815
6816 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006817 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006818 done = 1;
6819 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006820
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006821 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006822 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006823 done = 1;
6824 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006825
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006826 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006827 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006828 done = 1;
6829 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006831 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006832 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006833 done = 1;
6834 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006836 case ACT_REPLACE:
6837 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006838 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6839 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006840 /* FIXME: if the user adds a newline in the replacement, the
6841 * index will not be recalculated for now, and the new line
6842 * will not be counted as a new header.
6843 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006844
Willy Tarreaufa355d42009-11-29 18:12:29 +01006845 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006846 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006847 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006848 HTTP_MSG_RQMETH,
6849 cur_ptr, cur_end + 1,
6850 NULL, NULL);
6851 if (unlikely(!cur_end))
6852 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006853
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006854 /* we have a full request and we know that we have either a CR
6855 * or an LF at <ptr>.
6856 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006857 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6858 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006859 /* there is no point trying this regex on headers */
6860 return 1;
6861 }
6862 }
6863 *cur_end = term; /* restore the string terminator */
6864 return done;
6865}
Willy Tarreau97de6242006-12-27 17:18:38 +01006866
Willy Tarreau58f10d72006-12-04 02:26:12 +01006867
Willy Tarreau58f10d72006-12-04 02:26:12 +01006868
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006869/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006870 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006871 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006872 * unparsable request. Since it can manage the switch to another backend, it
6873 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006874 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006875int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006876{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006877 struct http_txn *txn = &s->txn;
6878 struct hdr_exp *exp;
6879
6880 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006881 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006882
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006883 /*
6884 * The interleaving of transformations and verdicts
6885 * makes it difficult to decide to continue or stop
6886 * the evaluation.
6887 */
6888
Willy Tarreau6c123b12010-01-28 20:22:06 +01006889 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6890 break;
6891
Willy Tarreau3d300592007-03-18 18:34:41 +01006892 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006893 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006894 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006895 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006896
6897 /* if this filter had a condition, evaluate it now and skip to
6898 * next filter if the condition does not match.
6899 */
6900 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006901 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006902 ret = acl_pass(ret);
6903 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6904 ret = !ret;
6905
6906 if (!ret)
6907 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006908 }
6909
6910 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006911 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006912 if (unlikely(ret < 0))
6913 return -1;
6914
6915 if (likely(ret == 0)) {
6916 /* The filter did not match the request, it can be
6917 * iterated through all headers.
6918 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006919 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006920 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006922 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923}
6924
6925
Willy Tarreaua15645d2007-03-18 16:22:39 +01006926
Willy Tarreau58f10d72006-12-04 02:26:12 +01006927/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006928 * Try to retrieve the server associated to the appsession.
6929 * If the server is found, it's assigned to the session.
6930 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006931void manage_client_side_appsession(struct session *s, const char *buf, int len) {
6932 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006933 appsess *asession = NULL;
6934 char *sessid_temp = NULL;
6935
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006936 if (len > s->be->appsession_len) {
6937 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006938 }
6939
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006940 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006941 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006942 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006943 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006944 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006945 }
6946
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006947 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006948 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006949 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006950 return;
6951 }
6952
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006953 memcpy(txn->sessid, buf, len);
6954 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006955 }
6956
6957 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6958 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006959 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006960 return;
6961 }
6962
Cyril Bontéb21570a2009-11-29 20:04:48 +01006963 memcpy(sessid_temp, buf, len);
6964 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006965
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006966 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006967 /* free previously allocated memory */
6968 pool_free2(apools.sessid, sessid_temp);
6969
6970 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006971 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
6972 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006973 asession->request_count++;
6974
6975 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006976 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006977
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006978 while (srv) {
6979 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006980 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006981 (s->be->options & PR_O_PERSIST) ||
6982 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006983 /* we found the server and it's usable */
6984 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006985 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006986 s->flags |= SN_DIRECT | SN_ASSIGNED;
6987 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006988
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006989 break;
6990 } else {
6991 txn->flags &= ~TX_CK_MASK;
6992 txn->flags |= TX_CK_DOWN;
6993 }
6994 }
6995 srv = srv->next;
6996 }
6997 }
6998 }
6999}
7000
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007001/* Find the end of a cookie value contained between <s> and <e>. It works the
7002 * same way as with headers above except that the semi-colon also ends a token.
7003 * See RFC2965 for more information. Note that it requires a valid header to
7004 * return a valid result.
7005 */
7006char *find_cookie_value_end(char *s, const char *e)
7007{
7008 int quoted, qdpair;
7009
7010 quoted = qdpair = 0;
7011 for (; s < e; s++) {
7012 if (qdpair) qdpair = 0;
7013 else if (quoted) {
7014 if (*s == '\\') qdpair = 1;
7015 else if (*s == '"') quoted = 0;
7016 }
7017 else if (*s == '"') quoted = 1;
7018 else if (*s == ',' || *s == ';') return s;
7019 }
7020 return s;
7021}
7022
7023/* Delete a value in a header between delimiters <from> and <next> in buffer
7024 * <buf>. The number of characters displaced is returned, and the pointer to
7025 * the first delimiter is updated if required. The function tries as much as
7026 * possible to respect the following principles :
7027 * - replace <from> delimiter by the <next> one unless <from> points to a
7028 * colon, in which case <next> is simply removed
7029 * - set exactly one space character after the new first delimiter, unless
7030 * there are not enough characters in the block being moved to do so.
7031 * - remove unneeded spaces before the previous delimiter and after the new
7032 * one.
7033 *
7034 * It is the caller's responsibility to ensure that :
7035 * - <from> points to a valid delimiter or the colon ;
7036 * - <next> points to a valid delimiter or the final CR/LF ;
7037 * - there are non-space chars before <from> ;
7038 * - there is a CR/LF at or after <next>.
7039 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007040int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007041{
7042 char *prev = *from;
7043
7044 if (*prev == ':') {
7045 /* We're removing the first value, preserve the colon and add a
7046 * space if possible.
7047 */
7048 if (!http_is_crlf[(unsigned char)*next])
7049 next++;
7050 prev++;
7051 if (prev < next)
7052 *prev++ = ' ';
7053
7054 while (http_is_spht[(unsigned char)*next])
7055 next++;
7056 } else {
7057 /* Remove useless spaces before the old delimiter. */
7058 while (http_is_spht[(unsigned char)*(prev-1)])
7059 prev--;
7060 *from = prev;
7061
7062 /* copy the delimiter and if possible a space if we're
7063 * not at the end of the line.
7064 */
7065 if (!http_is_crlf[(unsigned char)*next]) {
7066 *prev++ = *next++;
7067 if (prev + 1 < next)
7068 *prev++ = ' ';
7069 while (http_is_spht[(unsigned char)*next])
7070 next++;
7071 }
7072 }
7073 return buffer_replace2(buf, prev, next, NULL, 0);
7074}
7075
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007076/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007077 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007078 * desirable to call it only when needed. This code is quite complex because
7079 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7080 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007082void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007084 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007085 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007086 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007087 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7088 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007089
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007090 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007091 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007092 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007094 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007096 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007097
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007098 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007099 hdr_beg = hdr_next;
7100 hdr_end = hdr_beg + cur_hdr->len;
7101 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 /* We have one full header between hdr_beg and hdr_end, and the
7104 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105 * "Cookie:" headers.
7106 */
7107
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007108 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007109 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007110 old_idx = cur_idx;
7111 continue;
7112 }
7113
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007114 del_from = NULL; /* nothing to be deleted */
7115 preserve_hdr = 0; /* assume we may kill the whole header */
7116
Willy Tarreau58f10d72006-12-04 02:26:12 +01007117 /* Now look for cookies. Conforming to RFC2109, we have to support
7118 * attributes whose name begin with a '$', and associate them with
7119 * the right cookie, if we want to delete this cookie.
7120 * So there are 3 cases for each cookie read :
7121 * 1) it's a special attribute, beginning with a '$' : ignore it.
7122 * 2) it's a server id cookie that we *MAY* want to delete : save
7123 * some pointers on it (last semi-colon, beginning of cookie...)
7124 * 3) it's an application cookie : we *MAY* have to delete a previous
7125 * "special" cookie.
7126 * At the end of loop, if a "special" cookie remains, we may have to
7127 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007128 * *MUST* delete it.
7129 *
7130 * Note: RFC2965 is unclear about the processing of spaces around
7131 * the equal sign in the ATTR=VALUE form. A careful inspection of
7132 * the RFC explicitly allows spaces before it, and not within the
7133 * tokens (attrs or values). An inspection of RFC2109 allows that
7134 * too but section 10.1.3 lets one think that spaces may be allowed
7135 * after the equal sign too, resulting in some (rare) buggy
7136 * implementations trying to do that. So let's do what servers do.
7137 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7138 * allowed quoted strings in values, with any possible character
7139 * after a backslash, including control chars and delimitors, which
7140 * causes parsing to become ambiguous. Browsers also allow spaces
7141 * within values even without quotes.
7142 *
7143 * We have to keep multiple pointers in order to support cookie
7144 * removal at the beginning, middle or end of header without
7145 * corrupting the header. All of these headers are valid :
7146 *
7147 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7148 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7149 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7150 * | | | | | | | | |
7151 * | | | | | | | | hdr_end <--+
7152 * | | | | | | | +--> next
7153 * | | | | | | +----> val_end
7154 * | | | | | +-----------> val_beg
7155 * | | | | +--------------> equal
7156 * | | | +----------------> att_end
7157 * | | +---------------------> att_beg
7158 * | +--------------------------> prev
7159 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007160 */
7161
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007162 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7163 /* Iterate through all cookies on this line */
7164
7165 /* find att_beg */
7166 att_beg = prev + 1;
7167 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7168 att_beg++;
7169
7170 /* find att_end : this is the first character after the last non
7171 * space before the equal. It may be equal to hdr_end.
7172 */
7173 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007174
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007175 while (equal < hdr_end) {
7176 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007178 if (http_is_spht[(unsigned char)*equal++])
7179 continue;
7180 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007181 }
7182
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007183 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7184 * is between <att_beg> and <equal>, both may be identical.
7185 */
7186
7187 /* look for end of cookie if there is an equal sign */
7188 if (equal < hdr_end && *equal == '=') {
7189 /* look for the beginning of the value */
7190 val_beg = equal + 1;
7191 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7192 val_beg++;
7193
7194 /* find the end of the value, respecting quotes */
7195 next = find_cookie_value_end(val_beg, hdr_end);
7196
7197 /* make val_end point to the first white space or delimitor after the value */
7198 val_end = next;
7199 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7200 val_end--;
7201 } else {
7202 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007203 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007204
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007205 /* We have nothing to do with attributes beginning with '$'. However,
7206 * they will automatically be removed if a header before them is removed,
7207 * since they're supposed to be linked together.
7208 */
7209 if (*att_beg == '$')
7210 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007211
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007212 /* Ignore cookies with no equal sign */
7213 if (equal == next) {
7214 /* This is not our cookie, so we must preserve it. But if we already
7215 * scheduled another cookie for removal, we cannot remove the
7216 * complete header, but we can remove the previous block itself.
7217 */
7218 preserve_hdr = 1;
7219 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007220 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007221 val_end += delta;
7222 next += delta;
7223 hdr_end += delta;
7224 hdr_next += delta;
7225 cur_hdr->len += delta;
7226 http_msg_move_end(&txn->req, delta);
7227 prev = del_from;
7228 del_from = NULL;
7229 }
7230 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007231 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007232
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007233 /* if there are spaces around the equal sign, we need to
7234 * strip them otherwise we'll get trouble for cookie captures,
7235 * or even for rewrites. Since this happens extremely rarely,
7236 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007237 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007238 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7239 int stripped_before = 0;
7240 int stripped_after = 0;
7241
7242 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007243 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007244 equal += stripped_before;
7245 val_beg += stripped_before;
7246 }
7247
7248 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007249 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007250 val_beg += stripped_after;
7251 stripped_before += stripped_after;
7252 }
7253
7254 val_end += stripped_before;
7255 next += stripped_before;
7256 hdr_end += stripped_before;
7257 hdr_next += stripped_before;
7258 cur_hdr->len += stripped_before;
7259 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007260 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007261 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007263 /* First, let's see if we want to capture this cookie. We check
7264 * that we don't already have a client side cookie, because we
7265 * can only capture one. Also as an optimisation, we ignore
7266 * cookies shorter than the declared name.
7267 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007268 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7269 (val_end - att_beg >= s->fe->capture_namelen) &&
7270 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007271 int log_len = val_end - att_beg;
7272
7273 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7274 Alert("HTTP logging : out of memory.\n");
7275 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007276 if (log_len > s->fe->capture_len)
7277 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007278 memcpy(txn->cli_cookie, att_beg, log_len);
7279 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007280 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007282
Willy Tarreaubca99692010-10-06 19:25:55 +02007283 /* Persistence cookies in passive, rewrite or insert mode have the
7284 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007285 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007286 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007287 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007288 * For cookies in prefix mode, the form is :
7289 *
7290 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007291 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007292 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7293 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7294 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007295 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007296
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007297 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7298 * have the server ID between val_beg and delim, and the original cookie between
7299 * delim+1 and val_end. Otherwise, delim==val_end :
7300 *
7301 * Cookie: NAME=SRV; # in all but prefix modes
7302 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7303 * | || || | |+-> next
7304 * | || || | +--> val_end
7305 * | || || +---------> delim
7306 * | || |+------------> val_beg
7307 * | || +-------------> att_end = equal
7308 * | |+-----------------> att_beg
7309 * | +------------------> prev
7310 * +-------------------------> hdr_beg
7311 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007312
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007313 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007314 for (delim = val_beg; delim < val_end; delim++)
7315 if (*delim == COOKIE_DELIM)
7316 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007317 } else {
7318 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007319 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007320 /* Now check if the cookie contains a date field, which would
7321 * appear after a vertical bar ('|') just after the server name
7322 * and before the delimiter.
7323 */
7324 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7325 if (vbar1) {
7326 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007327 * right is the last seen date. It is a base64 encoded
7328 * 30-bit value representing the UNIX date since the
7329 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007330 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007331 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007332 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007333 if (val_end - vbar1 >= 5) {
7334 val = b64tos30(vbar1);
7335 if (val > 0)
7336 txn->cookie_last_date = val << 2;
7337 }
7338 /* look for a second vertical bar */
7339 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7340 if (vbar1 && (val_end - vbar1 > 5)) {
7341 val = b64tos30(vbar1 + 1);
7342 if (val > 0)
7343 txn->cookie_first_date = val << 2;
7344 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007345 }
7346 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007347
Willy Tarreauf64d1412010-10-07 20:06:11 +02007348 /* if the cookie has an expiration date and the proxy wants to check
7349 * it, then we do that now. We first check if the cookie is too old,
7350 * then only if it has expired. We detect strict overflow because the
7351 * time resolution here is not great (4 seconds). Cookies with dates
7352 * in the future are ignored if their offset is beyond one day. This
7353 * allows an admin to fix timezone issues without expiring everyone
7354 * and at the same time avoids keeping unwanted side effects for too
7355 * long.
7356 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007357 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7358 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007359 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007360 txn->flags &= ~TX_CK_MASK;
7361 txn->flags |= TX_CK_OLD;
7362 delim = val_beg; // let's pretend we have not found the cookie
7363 txn->cookie_first_date = 0;
7364 txn->cookie_last_date = 0;
7365 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007366 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7367 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007368 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007369 txn->flags &= ~TX_CK_MASK;
7370 txn->flags |= TX_CK_EXPIRED;
7371 delim = val_beg; // let's pretend we have not found the cookie
7372 txn->cookie_first_date = 0;
7373 txn->cookie_last_date = 0;
7374 }
7375
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007376 /* Here, we'll look for the first running server which supports the cookie.
7377 * This allows to share a same cookie between several servers, for example
7378 * to dedicate backup servers to specific servers only.
7379 * However, to prevent clients from sticking to cookie-less backup server
7380 * when they have incidentely learned an empty cookie, we simply ignore
7381 * empty cookies and mark them as invalid.
7382 * The same behaviour is applied when persistence must be ignored.
7383 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007384 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007385 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007386
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007387 while (srv) {
7388 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7389 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7390 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007391 (s->be->options & PR_O_PERSIST) ||
7392 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007393 /* we found the server and we can use it */
7394 txn->flags &= ~TX_CK_MASK;
7395 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007396 s->flags |= SN_DIRECT | SN_ASSIGNED;
7397 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007398 break;
7399 } else {
7400 /* we found a server, but it's down,
7401 * mark it as such and go on in case
7402 * another one is available.
7403 */
7404 txn->flags &= ~TX_CK_MASK;
7405 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007406 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007407 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007408 srv = srv->next;
7409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007410
Willy Tarreauf64d1412010-10-07 20:06:11 +02007411 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007412 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007413 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007414 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007415 txn->flags |= TX_CK_UNUSED;
7416 else
7417 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007418 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007419
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007420 /* depending on the cookie mode, we may have to either :
7421 * - delete the complete cookie if we're in insert+indirect mode, so that
7422 * the server never sees it ;
7423 * - remove the server id from the cookie value, and tag the cookie as an
7424 * application cookie so that it does not get accidentely removed later,
7425 * if we're in cookie prefix mode
7426 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007427 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007428 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007429
Willy Tarreau9b28e032012-10-12 23:49:43 +02007430 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007431 val_end += delta;
7432 next += delta;
7433 hdr_end += delta;
7434 hdr_next += delta;
7435 cur_hdr->len += delta;
7436 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007437
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007438 del_from = NULL;
7439 preserve_hdr = 1; /* we want to keep this cookie */
7440 }
7441 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007442 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007443 del_from = prev;
7444 }
7445 } else {
7446 /* This is not our cookie, so we must preserve it. But if we already
7447 * scheduled another cookie for removal, we cannot remove the
7448 * complete header, but we can remove the previous block itself.
7449 */
7450 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007451
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007452 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007453 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007454 if (att_beg >= del_from)
7455 att_beg += delta;
7456 if (att_end >= del_from)
7457 att_end += delta;
7458 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007459 val_end += delta;
7460 next += delta;
7461 hdr_end += delta;
7462 hdr_next += delta;
7463 cur_hdr->len += delta;
7464 http_msg_move_end(&txn->req, delta);
7465 prev = del_from;
7466 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007469
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007470 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007471 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472 int cmp_len, value_len;
7473 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007474
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007475 if (s->be->options2 & PR_O2_AS_PFX) {
7476 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7477 value_begin = att_beg + s->be->appsession_name_len;
7478 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007479 } else {
7480 cmp_len = att_end - att_beg;
7481 value_begin = val_beg;
7482 value_len = val_end - val_beg;
7483 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007484
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007486 if (cmp_len == s->be->appsession_name_len &&
7487 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7488 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007489 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490 }
7491
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007492 /* continue with next cookie on this header line */
7493 att_beg = next;
7494 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007495
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007496 /* There are no more cookies on this line.
7497 * We may still have one (or several) marked for deletion at the
7498 * end of the line. We must do this now in two ways :
7499 * - if some cookies must be preserved, we only delete from the
7500 * mark to the end of line ;
7501 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007502 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007503 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007504 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007505 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007506 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007507 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007508 cur_hdr->len += delta;
7509 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007510 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007511
7512 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007513 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7514 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007516 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007517 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007518 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007519 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007520 }
7521
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007523 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007525}
7526
7527
Willy Tarreaua15645d2007-03-18 16:22:39 +01007528/* Iterate the same filter through all response headers contained in <rtr>.
7529 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7530 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007531int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007532{
7533 char term;
7534 char *cur_ptr, *cur_end, *cur_next;
7535 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007536 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007537 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007538 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007539
7540 last_hdr = 0;
7541
Willy Tarreau9b28e032012-10-12 23:49:43 +02007542 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007543 old_idx = 0;
7544
7545 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007546 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007547 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007548 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007549 (exp->action == ACT_ALLOW ||
7550 exp->action == ACT_DENY))
7551 return 0;
7552
7553 cur_idx = txn->hdr_idx.v[old_idx].next;
7554 if (!cur_idx)
7555 break;
7556
7557 cur_hdr = &txn->hdr_idx.v[cur_idx];
7558 cur_ptr = cur_next;
7559 cur_end = cur_ptr + cur_hdr->len;
7560 cur_next = cur_end + cur_hdr->cr + 1;
7561
7562 /* Now we have one header between cur_ptr and cur_end,
7563 * and the next header starts at cur_next.
7564 */
7565
7566 /* The annoying part is that pattern matching needs
7567 * that we modify the contents to null-terminate all
7568 * strings before testing them.
7569 */
7570
7571 term = *cur_end;
7572 *cur_end = '\0';
7573
7574 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7575 switch (exp->action) {
7576 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007577 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007578 last_hdr = 1;
7579 break;
7580
7581 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007582 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007583 last_hdr = 1;
7584 break;
7585
7586 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007587 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7588 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007589 /* FIXME: if the user adds a newline in the replacement, the
7590 * index will not be recalculated for now, and the new line
7591 * will not be counted as a new header.
7592 */
7593
7594 cur_end += delta;
7595 cur_next += delta;
7596 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007597 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007598 break;
7599
7600 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007601 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007602 cur_next += delta;
7603
Willy Tarreaufa355d42009-11-29 18:12:29 +01007604 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7606 txn->hdr_idx.used--;
7607 cur_hdr->len = 0;
7608 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007609 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007610 break;
7611
7612 }
7613 }
7614 if (cur_end)
7615 *cur_end = term; /* restore the string terminator */
7616
7617 /* keep the link from this header to next one in case of later
7618 * removal of next header.
7619 */
7620 old_idx = cur_idx;
7621 }
7622 return 0;
7623}
7624
7625
7626/* Apply the filter to the status line in the response buffer <rtr>.
7627 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7628 * or -1 if a replacement resulted in an invalid status line.
7629 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007630int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007631{
7632 char term;
7633 char *cur_ptr, *cur_end;
7634 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007635 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007636 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007637
7638
Willy Tarreau3d300592007-03-18 18:34:41 +01007639 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007641 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007642 (exp->action == ACT_ALLOW ||
7643 exp->action == ACT_DENY))
7644 return 0;
7645 else if (exp->action == ACT_REMOVE)
7646 return 0;
7647
7648 done = 0;
7649
Willy Tarreau9b28e032012-10-12 23:49:43 +02007650 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007651 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007652
7653 /* Now we have the status line between cur_ptr and cur_end */
7654
7655 /* The annoying part is that pattern matching needs
7656 * that we modify the contents to null-terminate all
7657 * strings before testing them.
7658 */
7659
7660 term = *cur_end;
7661 *cur_end = '\0';
7662
7663 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7664 switch (exp->action) {
7665 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007666 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007667 done = 1;
7668 break;
7669
7670 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007671 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007672 done = 1;
7673 break;
7674
7675 case ACT_REPLACE:
7676 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007677 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7678 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679 /* FIXME: if the user adds a newline in the replacement, the
7680 * index will not be recalculated for now, and the new line
7681 * will not be counted as a new header.
7682 */
7683
Willy Tarreaufa355d42009-11-29 18:12:29 +01007684 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007686 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007687 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007688 cur_ptr, cur_end + 1,
7689 NULL, NULL);
7690 if (unlikely(!cur_end))
7691 return -1;
7692
7693 /* we have a full respnse and we know that we have either a CR
7694 * or an LF at <ptr>.
7695 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007696 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007697 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007698 /* there is no point trying this regex on headers */
7699 return 1;
7700 }
7701 }
7702 *cur_end = term; /* restore the string terminator */
7703 return done;
7704}
7705
7706
7707
7708/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007709 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007710 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7711 * unparsable response.
7712 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007713int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007714{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007715 struct http_txn *txn = &s->txn;
7716 struct hdr_exp *exp;
7717
7718 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007719 int ret;
7720
7721 /*
7722 * The interleaving of transformations and verdicts
7723 * makes it difficult to decide to continue or stop
7724 * the evaluation.
7725 */
7726
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007727 if (txn->flags & TX_SVDENY)
7728 break;
7729
Willy Tarreau3d300592007-03-18 18:34:41 +01007730 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007731 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7732 exp->action == ACT_PASS)) {
7733 exp = exp->next;
7734 continue;
7735 }
7736
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007737 /* if this filter had a condition, evaluate it now and skip to
7738 * next filter if the condition does not match.
7739 */
7740 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007741 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007742 ret = acl_pass(ret);
7743 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7744 ret = !ret;
7745 if (!ret)
7746 continue;
7747 }
7748
Willy Tarreaua15645d2007-03-18 16:22:39 +01007749 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007750 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007751 if (unlikely(ret < 0))
7752 return -1;
7753
7754 if (likely(ret == 0)) {
7755 /* The filter did not match the response, it can be
7756 * iterated through all headers.
7757 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007758 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007759 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007760 }
7761 return 0;
7762}
7763
7764
Willy Tarreaua15645d2007-03-18 16:22:39 +01007765/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007766 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007767 * desirable to call it only when needed. This function is also used when we
7768 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007769 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007770void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007771{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007772 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007773 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007774 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007775 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007776 char *hdr_beg, *hdr_end, *hdr_next;
7777 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007778
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 /* Iterate through the headers.
7780 * we start with the start line.
7781 */
7782 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007783 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784
7785 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7786 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007787 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007788
7789 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007790 hdr_beg = hdr_next;
7791 hdr_end = hdr_beg + cur_hdr->len;
7792 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793
Willy Tarreau24581ba2010-08-31 22:39:35 +02007794 /* We have one full header between hdr_beg and hdr_end, and the
7795 * next header starts at hdr_next. We're only interested in
7796 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007797 */
7798
Willy Tarreau24581ba2010-08-31 22:39:35 +02007799 is_cookie2 = 0;
7800 prev = hdr_beg + 10;
7801 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007802 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007803 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7804 if (!val) {
7805 old_idx = cur_idx;
7806 continue;
7807 }
7808 is_cookie2 = 1;
7809 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007810 }
7811
Willy Tarreau24581ba2010-08-31 22:39:35 +02007812 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7813 * <prev> points to the colon.
7814 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007815 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007816
Willy Tarreau24581ba2010-08-31 22:39:35 +02007817 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7818 * check-cache is enabled) and we are not interested in checking
7819 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007820 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007821 if (s->be->cookie_name == NULL &&
7822 s->be->appsession_name == NULL &&
7823 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824 return;
7825
Willy Tarreau24581ba2010-08-31 22:39:35 +02007826 /* OK so now we know we have to process this response cookie.
7827 * The format of the Set-Cookie header is slightly different
7828 * from the format of the Cookie header in that it does not
7829 * support the comma as a cookie delimiter (thus the header
7830 * cannot be folded) because the Expires attribute described in
7831 * the original Netscape's spec may contain an unquoted date
7832 * with a comma inside. We have to live with this because
7833 * many browsers don't support Max-Age and some browsers don't
7834 * support quoted strings. However the Set-Cookie2 header is
7835 * clean.
7836 *
7837 * We have to keep multiple pointers in order to support cookie
7838 * removal at the beginning, middle or end of header without
7839 * corrupting the header (in case of set-cookie2). A special
7840 * pointer, <scav> points to the beginning of the set-cookie-av
7841 * fields after the first semi-colon. The <next> pointer points
7842 * either to the end of line (set-cookie) or next unquoted comma
7843 * (set-cookie2). All of these headers are valid :
7844 *
7845 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7846 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7847 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7848 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7849 * | | | | | | | | | |
7850 * | | | | | | | | +-> next hdr_end <--+
7851 * | | | | | | | +------------> scav
7852 * | | | | | | +--------------> val_end
7853 * | | | | | +--------------------> val_beg
7854 * | | | | +----------------------> equal
7855 * | | | +------------------------> att_end
7856 * | | +----------------------------> att_beg
7857 * | +------------------------------> prev
7858 * +-----------------------------------------> hdr_beg
7859 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860
Willy Tarreau24581ba2010-08-31 22:39:35 +02007861 for (; prev < hdr_end; prev = next) {
7862 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007863
Willy Tarreau24581ba2010-08-31 22:39:35 +02007864 /* find att_beg */
7865 att_beg = prev + 1;
7866 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7867 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007868
Willy Tarreau24581ba2010-08-31 22:39:35 +02007869 /* find att_end : this is the first character after the last non
7870 * space before the equal. It may be equal to hdr_end.
7871 */
7872 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007873
Willy Tarreau24581ba2010-08-31 22:39:35 +02007874 while (equal < hdr_end) {
7875 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7876 break;
7877 if (http_is_spht[(unsigned char)*equal++])
7878 continue;
7879 att_end = equal;
7880 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007881
Willy Tarreau24581ba2010-08-31 22:39:35 +02007882 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7883 * is between <att_beg> and <equal>, both may be identical.
7884 */
7885
7886 /* look for end of cookie if there is an equal sign */
7887 if (equal < hdr_end && *equal == '=') {
7888 /* look for the beginning of the value */
7889 val_beg = equal + 1;
7890 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7891 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007892
Willy Tarreau24581ba2010-08-31 22:39:35 +02007893 /* find the end of the value, respecting quotes */
7894 next = find_cookie_value_end(val_beg, hdr_end);
7895
7896 /* make val_end point to the first white space or delimitor after the value */
7897 val_end = next;
7898 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7899 val_end--;
7900 } else {
7901 /* <equal> points to next comma, semi-colon or EOL */
7902 val_beg = val_end = next = equal;
7903 }
7904
7905 if (next < hdr_end) {
7906 /* Set-Cookie2 supports multiple cookies, and <next> points to
7907 * a colon or semi-colon before the end. So skip all attr-value
7908 * pairs and look for the next comma. For Set-Cookie, since
7909 * commas are permitted in values, skip to the end.
7910 */
7911 if (is_cookie2)
7912 next = find_hdr_value_end(next, hdr_end);
7913 else
7914 next = hdr_end;
7915 }
7916
7917 /* Now everything is as on the diagram above */
7918
7919 /* Ignore cookies with no equal sign */
7920 if (equal == val_end)
7921 continue;
7922
7923 /* If there are spaces around the equal sign, we need to
7924 * strip them otherwise we'll get trouble for cookie captures,
7925 * or even for rewrites. Since this happens extremely rarely,
7926 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007927 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007928 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7929 int stripped_before = 0;
7930 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007931
Willy Tarreau24581ba2010-08-31 22:39:35 +02007932 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007933 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007934 equal += stripped_before;
7935 val_beg += stripped_before;
7936 }
7937
7938 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007939 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007940 val_beg += stripped_after;
7941 stripped_before += stripped_after;
7942 }
7943
7944 val_end += stripped_before;
7945 next += stripped_before;
7946 hdr_end += stripped_before;
7947 hdr_next += stripped_before;
7948 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007949 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007950 }
7951
7952 /* First, let's see if we want to capture this cookie. We check
7953 * that we don't already have a server side cookie, because we
7954 * can only capture one. Also as an optimisation, we ignore
7955 * cookies shorter than the declared name.
7956 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007957 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007958 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007959 (val_end - att_beg >= s->fe->capture_namelen) &&
7960 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007961 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007962 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007963 Alert("HTTP logging : out of memory.\n");
7964 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007965 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007966 if (log_len > s->fe->capture_len)
7967 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007968 memcpy(txn->srv_cookie, att_beg, log_len);
7969 txn->srv_cookie[log_len] = 0;
7970 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 }
7972
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007973 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007975 if (!(s->flags & SN_IGNORE_PRST) &&
7976 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7977 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007978 /* assume passive cookie by default */
7979 txn->flags &= ~TX_SCK_MASK;
7980 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981
7982 /* If the cookie is in insert mode on a known server, we'll delete
7983 * this occurrence because we'll insert another one later.
7984 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007985 * a direct access.
7986 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007987 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007988 /* The "preserve" flag was set, we don't want to touch the
7989 * server's cookie.
7990 */
7991 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007992 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
7993 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007994 /* this cookie must be deleted */
7995 if (*prev == ':' && next == hdr_end) {
7996 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007997 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007998 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7999 txn->hdr_idx.used--;
8000 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008001 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008002 hdr_next += delta;
8003 http_msg_move_end(&txn->rsp, delta);
8004 /* note: while both invalid now, <next> and <hdr_end>
8005 * are still equal, so the for() will stop as expected.
8006 */
8007 } else {
8008 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008009 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008010 next = prev;
8011 hdr_end += delta;
8012 hdr_next += delta;
8013 cur_hdr->len += delta;
8014 http_msg_move_end(&txn->rsp, delta);
8015 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008016 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008017 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008018 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008020 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008021 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 * with this server since we know it.
8023 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008024 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008025 next += delta;
8026 hdr_end += delta;
8027 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008029 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008030
Willy Tarreauf1348312010-10-07 15:54:11 +02008031 txn->flags &= ~TX_SCK_MASK;
8032 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008034 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008035 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008036 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008037 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008038 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008039 next += delta;
8040 hdr_end += delta;
8041 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008043 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008044
Willy Tarreau827aee92011-03-10 16:55:02 +01008045 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008046 txn->flags &= ~TX_SCK_MASK;
8047 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048 }
8049 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008050 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008051 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008052 int cmp_len, value_len;
8053 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008055 if (s->be->options2 & PR_O2_AS_PFX) {
8056 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8057 value_begin = att_beg + s->be->appsession_name_len;
8058 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008059 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008060 cmp_len = att_end - att_beg;
8061 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008062 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008063 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008064
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008065 if ((cmp_len == s->be->appsession_name_len) &&
8066 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008067 /* free a possibly previously allocated memory */
8068 pool_free2(apools.sessid, txn->sessid);
8069
Cyril Bontéb21570a2009-11-29 20:04:48 +01008070 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008071 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008072 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008073 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008074 return;
8075 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008076 memcpy(txn->sessid, value_begin, value_len);
8077 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008079 }
8080 /* that's done for this cookie, check the next one on the same
8081 * line when next != hdr_end (only if is_cookie2).
8082 */
8083 }
8084 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008086 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008087
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008088 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008089 appsess *asession = NULL;
8090 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008091 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008092 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008093 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008094 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8095 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008096 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008097 return;
8098 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008099 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8100
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008101 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8102 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008103 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8104 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008105 return;
8106 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008107 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8108 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008109
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008110 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008111 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008112 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008113 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8114 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008115 return;
8116 }
8117 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008118 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008119
8120 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008121 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008122 }
8123
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008124 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008125 asession->request_count++;
8126 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008127}
8128
8129
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008131 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008133void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008134{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008135 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008136 char *p1, *p2;
8137
8138 char *cur_ptr, *cur_end, *cur_next;
8139 int cur_idx;
8140
Willy Tarreau5df51872007-11-25 16:20:08 +01008141 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008142 return;
8143
8144 /* Iterate through the headers.
8145 * we start with the start line.
8146 */
8147 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008148 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008149
8150 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8151 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008152 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153
8154 cur_hdr = &txn->hdr_idx.v[cur_idx];
8155 cur_ptr = cur_next;
8156 cur_end = cur_ptr + cur_hdr->len;
8157 cur_next = cur_end + cur_hdr->cr + 1;
8158
8159 /* We have one full header between cur_ptr and cur_end, and the
8160 * next header starts at cur_next. We're only interested in
8161 * "Cookie:" headers.
8162 */
8163
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008164 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8165 if (val) {
8166 if ((cur_end - (cur_ptr + val) >= 8) &&
8167 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8168 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8169 return;
8170 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008171 }
8172
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008173 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8174 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175 continue;
8176
8177 /* OK, right now we know we have a cache-control header at cur_ptr */
8178
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008179 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180
8181 if (p1 >= cur_end) /* no more info */
8182 continue;
8183
8184 /* p1 is at the beginning of the value */
8185 p2 = p1;
8186
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008187 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008188 p2++;
8189
8190 /* we have a complete value between p1 and p2 */
8191 if (p2 < cur_end && *p2 == '=') {
8192 /* we have something of the form no-cache="set-cookie" */
8193 if ((cur_end - p1 >= 21) &&
8194 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8195 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008196 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197 continue;
8198 }
8199
8200 /* OK, so we know that either p2 points to the end of string or to a comma */
8201 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008202 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008203 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8204 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8205 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008206 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207 return;
8208 }
8209
8210 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008211 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008212 continue;
8213 }
8214 }
8215}
8216
8217
Willy Tarreau58f10d72006-12-04 02:26:12 +01008218/*
8219 * Try to retrieve a known appsession in the URI, then the associated server.
8220 * If the server is found, it's assigned to the session.
8221 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008222void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008223{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008224 char *end_params, *first_param, *cur_param, *next_param;
8225 char separator;
8226 int value_len;
8227
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008228 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008229
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008230 if (s->be->appsession_name == NULL ||
8231 (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 +01008232 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008233 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008234
Cyril Bontéb21570a2009-11-29 20:04:48 +01008235 first_param = NULL;
8236 switch (mode) {
8237 case PR_O2_AS_M_PP:
8238 first_param = memchr(begin, ';', len);
8239 break;
8240 case PR_O2_AS_M_QS:
8241 first_param = memchr(begin, '?', len);
8242 break;
8243 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008244
Cyril Bontéb21570a2009-11-29 20:04:48 +01008245 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008246 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008248
Cyril Bontéb21570a2009-11-29 20:04:48 +01008249 switch (mode) {
8250 case PR_O2_AS_M_PP:
8251 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8252 end_params = (char *) begin + len;
8253 }
8254 separator = ';';
8255 break;
8256 case PR_O2_AS_M_QS:
8257 end_params = (char *) begin + len;
8258 separator = '&';
8259 break;
8260 default:
8261 /* unknown mode, shouldn't happen */
8262 return;
8263 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008264
Cyril Bontéb21570a2009-11-29 20:04:48 +01008265 cur_param = next_param = end_params;
8266 while (cur_param > first_param) {
8267 cur_param--;
8268 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8269 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008270 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8271 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8272 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008273 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008274 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8275 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008276 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008277 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008278 }
8279 break;
8280 }
8281 next_param = cur_param;
8282 }
8283 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008284#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008285 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008286 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008287#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008288}
8289
Willy Tarreaub2513902006-12-17 14:52:38 +01008290/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008291 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008292 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008293 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008294 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008295 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008296 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008297 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008298 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008299int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008300{
8301 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008302 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008303 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008304
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008305 if (!uri_auth)
8306 return 0;
8307
Cyril Bonté70be45d2010-10-12 00:14:35 +02008308 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008309 return 0;
8310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008311 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008312 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008313 return 0;
8314
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008315 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008316 return 0;
8317
Willy Tarreaub2513902006-12-17 14:52:38 +01008318 return 1;
8319}
8320
Willy Tarreau4076a152009-04-02 15:18:36 +02008321/*
8322 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008323 * By default it tries to report the error position as msg->err_pos. However if
8324 * this one is not set, it will then report msg->next, which is the last known
8325 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008326 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008327 */
8328void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008329 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008330 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008331{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008332 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008333 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008334
Willy Tarreau9b28e032012-10-12 23:49:43 +02008335 es->len = MIN(chn->buf->i, sizeof(es->buf));
8336 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008337 len1 = MIN(len1, es->len);
8338 len2 = es->len - len1; /* remaining data if buffer wraps */
8339
Willy Tarreau9b28e032012-10-12 23:49:43 +02008340 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008341 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008342 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008343
Willy Tarreau4076a152009-04-02 15:18:36 +02008344 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008345 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008346 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008347 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008348
Willy Tarreau4076a152009-04-02 15:18:36 +02008349 es->when = date; // user-visible date
8350 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008351 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008352 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008353 if (objt_conn(s->req->prod->end))
8354 es->src = __objt_conn(s->req->prod->end)->addr.from;
8355 else
8356 memset(&es->src, 0, sizeof(es->src));
8357
Willy Tarreau078272e2010-12-12 12:46:33 +01008358 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008359 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008360 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008361 es->s_flags = s->flags;
8362 es->t_flags = s->txn.flags;
8363 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008364 es->b_out = chn->buf->o;
8365 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008366 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008367 es->m_clen = msg->chunk_len;
8368 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008369}
Willy Tarreaub2513902006-12-17 14:52:38 +01008370
Willy Tarreau294c4732011-12-16 21:35:50 +01008371/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8372 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8373 * performed over the whole headers. Otherwise it must contain a valid header
8374 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8375 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8376 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8377 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008378 * -1. The value fetch stops at commas, so this function is suited for use with
8379 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008380 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008381 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008382unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008383 struct hdr_idx *idx, int occ,
8384 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008385{
Willy Tarreau294c4732011-12-16 21:35:50 +01008386 struct hdr_ctx local_ctx;
8387 char *ptr_hist[MAX_HDR_HISTORY];
8388 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008389 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008390 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008391
Willy Tarreau294c4732011-12-16 21:35:50 +01008392 if (!ctx) {
8393 local_ctx.idx = 0;
8394 ctx = &local_ctx;
8395 }
8396
Willy Tarreaubce70882009-09-07 11:51:47 +02008397 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008398 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008399 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008400 occ--;
8401 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008402 *vptr = ctx->line + ctx->val;
8403 *vlen = ctx->vlen;
8404 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008405 }
8406 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008407 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008408 }
8409
8410 /* negative occurrence, we scan all the list then walk back */
8411 if (-occ > MAX_HDR_HISTORY)
8412 return 0;
8413
Willy Tarreau294c4732011-12-16 21:35:50 +01008414 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008415 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008416 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8417 len_hist[hist_ptr] = ctx->vlen;
8418 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008419 hist_ptr = 0;
8420 found++;
8421 }
8422 if (-occ > found)
8423 return 0;
8424 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008425 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8426 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8427 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008428 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008429 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008430 if (hist_ptr >= MAX_HDR_HISTORY)
8431 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008432 *vptr = ptr_hist[hist_ptr];
8433 *vlen = len_hist[hist_ptr];
8434 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008435}
8436
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008437/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8438 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8439 * performed over the whole headers. Otherwise it must contain a valid header
8440 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8441 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8442 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8443 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8444 * -1. This function differs from http_get_hdr() in that it only returns full
8445 * line header values and does not stop at commas.
8446 * The return value is 0 if nothing was found, or non-zero otherwise.
8447 */
8448unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8449 struct hdr_idx *idx, int occ,
8450 struct hdr_ctx *ctx, char **vptr, int *vlen)
8451{
8452 struct hdr_ctx local_ctx;
8453 char *ptr_hist[MAX_HDR_HISTORY];
8454 int len_hist[MAX_HDR_HISTORY];
8455 unsigned int hist_ptr;
8456 int found;
8457
8458 if (!ctx) {
8459 local_ctx.idx = 0;
8460 ctx = &local_ctx;
8461 }
8462
8463 if (occ >= 0) {
8464 /* search from the beginning */
8465 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8466 occ--;
8467 if (occ <= 0) {
8468 *vptr = ctx->line + ctx->val;
8469 *vlen = ctx->vlen;
8470 return 1;
8471 }
8472 }
8473 return 0;
8474 }
8475
8476 /* negative occurrence, we scan all the list then walk back */
8477 if (-occ > MAX_HDR_HISTORY)
8478 return 0;
8479
8480 found = hist_ptr = 0;
8481 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8482 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8483 len_hist[hist_ptr] = ctx->vlen;
8484 if (++hist_ptr >= MAX_HDR_HISTORY)
8485 hist_ptr = 0;
8486 found++;
8487 }
8488 if (-occ > found)
8489 return 0;
8490 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8491 * find occurrence -occ, so we have to check [hist_ptr+occ].
8492 */
8493 hist_ptr += occ;
8494 if (hist_ptr >= MAX_HDR_HISTORY)
8495 hist_ptr -= MAX_HDR_HISTORY;
8496 *vptr = ptr_hist[hist_ptr];
8497 *vlen = len_hist[hist_ptr];
8498 return 1;
8499}
8500
Willy Tarreaubaaee002006-06-26 02:48:02 +02008501/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008502 * Print a debug line with a header. Always stop at the first CR or LF char,
8503 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8504 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008505 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008506void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008507{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008508 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008509 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008510 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008511 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8512 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 +02008513
8514 for (max = 0; start + max < end; max++)
8515 if (start[max] == '\r' || start[max] == '\n')
8516 break;
8517
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008518 UBOUND(max, trash.size - trash.len - 3);
8519 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8520 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008521 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008522}
8523
Willy Tarreau0937bc42009-12-22 15:03:09 +01008524/*
8525 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8526 * the required fields are properly allocated and that we only need to (re)init
8527 * them. This should be used before processing any new request.
8528 */
8529void http_init_txn(struct session *s)
8530{
8531 struct http_txn *txn = &s->txn;
8532 struct proxy *fe = s->fe;
8533
8534 txn->flags = 0;
8535 txn->status = -1;
8536
Willy Tarreauf64d1412010-10-07 20:06:11 +02008537 txn->cookie_first_date = 0;
8538 txn->cookie_last_date = 0;
8539
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008540 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008541 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008542 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008543 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008544 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008545 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008546 txn->req.chunk_len = 0LL;
8547 txn->req.body_len = 0LL;
8548 txn->rsp.chunk_len = 0LL;
8549 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008550 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8551 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008552 txn->req.chn = s->req;
8553 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008554
8555 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008556
8557 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8558 if (fe->options2 & PR_O2_REQBUG_OK)
8559 txn->req.err_pos = -1; /* let buggy requests pass */
8560
Willy Tarreau46023632010-01-07 22:51:47 +01008561 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008562 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8563
Willy Tarreau46023632010-01-07 22:51:47 +01008564 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008565 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8566
8567 if (txn->hdr_idx.v)
8568 hdr_idx_init(&txn->hdr_idx);
8569}
8570
8571/* to be used at the end of a transaction */
8572void http_end_txn(struct session *s)
8573{
8574 struct http_txn *txn = &s->txn;
8575
Willy Tarreau75195602014-03-11 15:48:55 +01008576 /* release any possible compression context */
8577 if (s->flags & SN_COMP_READY)
8578 s->comp_algo->end(&s->comp_ctx);
8579 s->comp_algo = NULL;
8580 s->flags &= ~SN_COMP_READY;
8581
Willy Tarreau0937bc42009-12-22 15:03:09 +01008582 /* these ones will have been dynamically allocated */
8583 pool_free2(pool2_requri, txn->uri);
8584 pool_free2(pool2_capture, txn->cli_cookie);
8585 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008586 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008587 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008588
William Lallemanda73203e2012-03-12 12:48:57 +01008589 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008590 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008591 txn->uri = NULL;
8592 txn->srv_cookie = NULL;
8593 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008594
8595 if (txn->req.cap) {
8596 struct cap_hdr *h;
8597 for (h = s->fe->req_cap; h; h = h->next)
8598 pool_free2(h->pool, txn->req.cap[h->index]);
8599 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8600 }
8601
8602 if (txn->rsp.cap) {
8603 struct cap_hdr *h;
8604 for (h = s->fe->rsp_cap; h; h = h->next)
8605 pool_free2(h->pool, txn->rsp.cap[h->index]);
8606 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8607 }
8608
Willy Tarreau0937bc42009-12-22 15:03:09 +01008609}
8610
8611/* to be used at the end of a transaction to prepare a new one */
8612void http_reset_txn(struct session *s)
8613{
8614 http_end_txn(s);
8615 http_init_txn(s);
8616
8617 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008618 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008619 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008620 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008621 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008622 /* re-init store persistence */
8623 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008624 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008625
Willy Tarreau0937bc42009-12-22 15:03:09 +01008626 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008627
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008628 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008629
Willy Tarreau739cfba2010-01-25 23:11:14 +01008630 /* We must trim any excess data from the response buffer, because we
8631 * may have blocked an invalid response from a server that we don't
8632 * want to accidentely forward once we disable the analysers, nor do
8633 * we want those data to come along with next response. A typical
8634 * example of such data would be from a buggy server responding to
8635 * a HEAD with some data, or sending more than the advertised
8636 * content-length.
8637 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008638 if (unlikely(s->rep->buf->i))
8639 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008640
Willy Tarreau0937bc42009-12-22 15:03:09 +01008641 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008642 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008643
Willy Tarreaud04e8582010-05-31 12:31:35 +02008644 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008645 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008646
8647 s->req->rex = TICK_ETERNITY;
8648 s->req->wex = TICK_ETERNITY;
8649 s->req->analyse_exp = TICK_ETERNITY;
8650 s->rep->rex = TICK_ETERNITY;
8651 s->rep->wex = TICK_ETERNITY;
8652 s->rep->analyse_exp = TICK_ETERNITY;
8653}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008654
Willy Tarreauff011f22011-01-06 17:51:27 +01008655void free_http_req_rules(struct list *r) {
8656 struct http_req_rule *tr, *pr;
8657
8658 list_for_each_entry_safe(pr, tr, r, list) {
8659 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008660 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008661 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008662
8663 free(pr);
8664 }
8665}
8666
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008667/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008668struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8669{
8670 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008671 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008672 int cur_arg;
8673
8674 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8675 if (!rule) {
8676 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008677 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008678 }
8679
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008680 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008681 rule->action = HTTP_REQ_ACT_ALLOW;
8682 cur_arg = 1;
8683 } else if (!strcmp(args[0], "deny")) {
8684 rule->action = HTTP_REQ_ACT_DENY;
8685 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008686 } else if (!strcmp(args[0], "tarpit")) {
8687 rule->action = HTTP_REQ_ACT_TARPIT;
8688 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008689 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008690 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008691 cur_arg = 1;
8692
8693 while(*args[cur_arg]) {
8694 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008695 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008696 cur_arg+=2;
8697 continue;
8698 } else
8699 break;
8700 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008701 } else if (!strcmp(args[0], "set-nice")) {
8702 rule->action = HTTP_REQ_ACT_SET_NICE;
8703 cur_arg = 1;
8704
8705 if (!*args[cur_arg] ||
8706 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8707 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8708 file, linenum, args[0]);
8709 goto out_err;
8710 }
8711 rule->arg.nice = atoi(args[cur_arg]);
8712 if (rule->arg.nice < -1024)
8713 rule->arg.nice = -1024;
8714 else if (rule->arg.nice > 1024)
8715 rule->arg.nice = 1024;
8716 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008717 } else if (!strcmp(args[0], "set-tos")) {
8718#ifdef IP_TOS
8719 char *err;
8720 rule->action = HTTP_REQ_ACT_SET_TOS;
8721 cur_arg = 1;
8722
8723 if (!*args[cur_arg] ||
8724 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8725 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8726 file, linenum, args[0]);
8727 goto out_err;
8728 }
8729
8730 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8731 if (err && *err != '\0') {
8732 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8733 file, linenum, err, args[0]);
8734 goto out_err;
8735 }
8736 cur_arg++;
8737#else
8738 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8739 goto out_err;
8740#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008741 } else if (!strcmp(args[0], "set-mark")) {
8742#ifdef SO_MARK
8743 char *err;
8744 rule->action = HTTP_REQ_ACT_SET_MARK;
8745 cur_arg = 1;
8746
8747 if (!*args[cur_arg] ||
8748 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8749 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8750 file, linenum, args[0]);
8751 goto out_err;
8752 }
8753
8754 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8755 if (err && *err != '\0') {
8756 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8757 file, linenum, err, args[0]);
8758 goto out_err;
8759 }
8760 cur_arg++;
8761 global.last_checks |= LSTCHK_NETADM;
8762#else
8763 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8764 goto out_err;
8765#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008766 } else if (!strcmp(args[0], "set-log-level")) {
8767 rule->action = HTTP_REQ_ACT_SET_LOGL;
8768 cur_arg = 1;
8769
8770 if (!*args[cur_arg] ||
8771 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8772 bad_log_level:
8773 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8774 file, linenum, args[0]);
8775 goto out_err;
8776 }
8777 if (strcmp(args[cur_arg], "silent") == 0)
8778 rule->arg.loglevel = -1;
8779 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8780 goto bad_log_level;
8781 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008782 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8783 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8784 cur_arg = 1;
8785
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008786 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8787 (*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 +01008788 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8789 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008790 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008791 }
8792
8793 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8794 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8795 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008796
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008797 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008798 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008799 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8800 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008801 free(proxy->conf.lfs_file);
8802 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8803 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008804 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008805 } else if (strcmp(args[0], "del-header") == 0) {
8806 rule->action = HTTP_REQ_ACT_DEL_HDR;
8807 cur_arg = 1;
8808
8809 if (!*args[cur_arg] ||
8810 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8811 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8812 file, linenum, args[0]);
8813 goto out_err;
8814 }
8815
8816 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8817 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8818
8819 proxy->conf.args.ctx = ARGC_HRQ;
8820 free(proxy->conf.lfs_file);
8821 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8822 proxy->conf.lfs_line = proxy->conf.args.line;
8823 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008824 } else if (strcmp(args[0], "redirect") == 0) {
8825 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008826 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008827
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008828 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008829 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8830 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8831 goto out_err;
8832 }
8833
8834 /* this redirect rule might already contain a parsed condition which
8835 * we'll pass to the http-request rule.
8836 */
8837 rule->action = HTTP_REQ_ACT_REDIR;
8838 rule->arg.redir = redir;
8839 rule->cond = redir->cond;
8840 redir->cond = NULL;
8841 cur_arg = 2;
8842 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008843 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8844 /* http-request add-acl(<reference (acl name)>) <key pattern> */
8845 rule->action = HTTP_REQ_ACT_ADD_ACL;
8846 /*
8847 * '+ 8' for 'add-acl('
8848 * '- 9' for 'add-acl(' + trailing ')'
8849 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008850 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008851
8852 cur_arg = 1;
8853
8854 if (!*args[cur_arg] ||
8855 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8856 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8857 file, linenum, args[0]);
8858 goto out_err;
8859 }
8860
8861 LIST_INIT(&rule->arg.map.key);
8862 proxy->conf.args.ctx = ARGC_HRQ;
8863 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8864 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8865 file, linenum);
8866 free(proxy->conf.lfs_file);
8867 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8868 proxy->conf.lfs_line = proxy->conf.args.line;
8869 cur_arg += 1;
8870 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8871 /* http-request del-acl(<reference (acl name)>) <key pattern> */
8872 rule->action = HTTP_REQ_ACT_DEL_ACL;
8873 /*
8874 * '+ 8' for 'del-acl('
8875 * '- 9' for 'del-acl(' + trailing ')'
8876 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008877 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008878
8879 cur_arg = 1;
8880
8881 if (!*args[cur_arg] ||
8882 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8883 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8884 file, linenum, args[0]);
8885 goto out_err;
8886 }
8887
8888 LIST_INIT(&rule->arg.map.key);
8889 proxy->conf.args.ctx = ARGC_HRQ;
8890 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8891 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8892 file, linenum);
8893 free(proxy->conf.lfs_file);
8894 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8895 proxy->conf.lfs_line = proxy->conf.args.line;
8896 cur_arg += 1;
8897 } else if (strncmp(args[0], "del-map", 7) == 0) {
8898 /* http-request del-map(<reference (map name)>) <key pattern> */
8899 rule->action = HTTP_REQ_ACT_DEL_MAP;
8900 /*
8901 * '+ 8' for 'del-map('
8902 * '- 9' for 'del-map(' + trailing ')'
8903 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008904 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008905
8906 cur_arg = 1;
8907
8908 if (!*args[cur_arg] ||
8909 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8910 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8911 file, linenum, args[0]);
8912 goto out_err;
8913 }
8914
8915 LIST_INIT(&rule->arg.map.key);
8916 proxy->conf.args.ctx = ARGC_HRQ;
8917 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8918 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8919 file, linenum);
8920 free(proxy->conf.lfs_file);
8921 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8922 proxy->conf.lfs_line = proxy->conf.args.line;
8923 cur_arg += 1;
8924 } else if (strncmp(args[0], "set-map", 7) == 0) {
8925 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
8926 rule->action = HTTP_REQ_ACT_SET_MAP;
8927 /*
8928 * '+ 8' for 'set-map('
8929 * '- 9' for 'set-map(' + trailing ')'
8930 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008931 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008932
8933 cur_arg = 1;
8934
8935 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8936 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8937 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8938 file, linenum, args[0]);
8939 goto out_err;
8940 }
8941
8942 LIST_INIT(&rule->arg.map.key);
8943 LIST_INIT(&rule->arg.map.value);
8944 proxy->conf.args.ctx = ARGC_HRQ;
8945
8946 /* key pattern */
8947 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8948 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8949 file, linenum);
8950
8951 /* value pattern */
8952 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
8953 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8954 file, linenum);
8955 free(proxy->conf.lfs_file);
8956 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8957 proxy->conf.lfs_line = proxy->conf.args.line;
8958
8959 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008960 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8961 char *errmsg = NULL;
8962 cur_arg = 1;
8963 /* try in the module list */
8964 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
8965 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8966 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8967 free(errmsg);
8968 goto out_err;
8969 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008970 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008971 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 +01008972 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008973 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008974 }
8975
8976 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8977 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008978 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008979
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008980 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8981 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8982 file, linenum, args[0], errmsg);
8983 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008984 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008985 }
8986 rule->cond = cond;
8987 }
8988 else if (*args[cur_arg]) {
8989 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8990 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8991 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008992 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008993 }
8994
8995 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008996 out_err:
8997 free(rule);
8998 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008999}
9000
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009001/* parse an "http-respose" rule */
9002struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9003{
9004 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009005 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009006 int cur_arg;
9007
9008 rule = calloc(1, sizeof(*rule));
9009 if (!rule) {
9010 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9011 goto out_err;
9012 }
9013
9014 if (!strcmp(args[0], "allow")) {
9015 rule->action = HTTP_RES_ACT_ALLOW;
9016 cur_arg = 1;
9017 } else if (!strcmp(args[0], "deny")) {
9018 rule->action = HTTP_RES_ACT_DENY;
9019 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009020 } else if (!strcmp(args[0], "set-nice")) {
9021 rule->action = HTTP_RES_ACT_SET_NICE;
9022 cur_arg = 1;
9023
9024 if (!*args[cur_arg] ||
9025 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9026 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9027 file, linenum, args[0]);
9028 goto out_err;
9029 }
9030 rule->arg.nice = atoi(args[cur_arg]);
9031 if (rule->arg.nice < -1024)
9032 rule->arg.nice = -1024;
9033 else if (rule->arg.nice > 1024)
9034 rule->arg.nice = 1024;
9035 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009036 } else if (!strcmp(args[0], "set-tos")) {
9037#ifdef IP_TOS
9038 char *err;
9039 rule->action = HTTP_RES_ACT_SET_TOS;
9040 cur_arg = 1;
9041
9042 if (!*args[cur_arg] ||
9043 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9044 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9045 file, linenum, args[0]);
9046 goto out_err;
9047 }
9048
9049 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9050 if (err && *err != '\0') {
9051 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9052 file, linenum, err, args[0]);
9053 goto out_err;
9054 }
9055 cur_arg++;
9056#else
9057 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9058 goto out_err;
9059#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009060 } else if (!strcmp(args[0], "set-mark")) {
9061#ifdef SO_MARK
9062 char *err;
9063 rule->action = HTTP_RES_ACT_SET_MARK;
9064 cur_arg = 1;
9065
9066 if (!*args[cur_arg] ||
9067 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9068 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9069 file, linenum, args[0]);
9070 goto out_err;
9071 }
9072
9073 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9074 if (err && *err != '\0') {
9075 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9076 file, linenum, err, args[0]);
9077 goto out_err;
9078 }
9079 cur_arg++;
9080 global.last_checks |= LSTCHK_NETADM;
9081#else
9082 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9083 goto out_err;
9084#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009085 } else if (!strcmp(args[0], "set-log-level")) {
9086 rule->action = HTTP_RES_ACT_SET_LOGL;
9087 cur_arg = 1;
9088
9089 if (!*args[cur_arg] ||
9090 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9091 bad_log_level:
9092 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9093 file, linenum, args[0]);
9094 goto out_err;
9095 }
9096 if (strcmp(args[cur_arg], "silent") == 0)
9097 rule->arg.loglevel = -1;
9098 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9099 goto bad_log_level;
9100 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009101 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9102 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9103 cur_arg = 1;
9104
9105 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9106 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9107 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9108 file, linenum, args[0]);
9109 goto out_err;
9110 }
9111
9112 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9113 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9114 LIST_INIT(&rule->arg.hdr_add.fmt);
9115
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009116 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009117 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009118 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9119 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009120 free(proxy->conf.lfs_file);
9121 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9122 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009123 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009124 } else if (strcmp(args[0], "del-header") == 0) {
9125 rule->action = HTTP_RES_ACT_DEL_HDR;
9126 cur_arg = 1;
9127
9128 if (!*args[cur_arg] ||
9129 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9130 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9131 file, linenum, args[0]);
9132 goto out_err;
9133 }
9134
9135 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9136 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9137
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009138 proxy->conf.args.ctx = ARGC_HRS;
9139 free(proxy->conf.lfs_file);
9140 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9141 proxy->conf.lfs_line = proxy->conf.args.line;
9142 cur_arg += 1;
9143 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9144 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9145 rule->action = HTTP_RES_ACT_ADD_ACL;
9146 /*
9147 * '+ 8' for 'add-acl('
9148 * '- 9' for 'add-acl(' + trailing ')'
9149 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009150 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009151
9152 cur_arg = 1;
9153
9154 if (!*args[cur_arg] ||
9155 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9156 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9157 file, linenum, args[0]);
9158 goto out_err;
9159 }
9160
9161 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009162 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009163 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9164 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9165 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009166 free(proxy->conf.lfs_file);
9167 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9168 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009169
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009170 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009171 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9172 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9173 rule->action = HTTP_RES_ACT_DEL_ACL;
9174 /*
9175 * '+ 8' for 'del-acl('
9176 * '- 9' for 'del-acl(' + trailing ')'
9177 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009178 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009179
9180 cur_arg = 1;
9181
9182 if (!*args[cur_arg] ||
9183 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9184 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9185 file, linenum, args[0]);
9186 goto out_err;
9187 }
9188
9189 LIST_INIT(&rule->arg.map.key);
9190 proxy->conf.args.ctx = ARGC_HRS;
9191 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9192 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9193 file, linenum);
9194 free(proxy->conf.lfs_file);
9195 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9196 proxy->conf.lfs_line = proxy->conf.args.line;
9197 cur_arg += 1;
9198 } else if (strncmp(args[0], "del-map", 7) == 0) {
9199 /* http-response del-map(<reference (map name)>) <key pattern> */
9200 rule->action = HTTP_RES_ACT_DEL_MAP;
9201 /*
9202 * '+ 8' for 'del-map('
9203 * '- 9' for 'del-map(' + trailing ')'
9204 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009205 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009206
9207 cur_arg = 1;
9208
9209 if (!*args[cur_arg] ||
9210 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9211 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9212 file, linenum, args[0]);
9213 goto out_err;
9214 }
9215
9216 LIST_INIT(&rule->arg.map.key);
9217 proxy->conf.args.ctx = ARGC_HRS;
9218 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9219 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9220 file, linenum);
9221 free(proxy->conf.lfs_file);
9222 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9223 proxy->conf.lfs_line = proxy->conf.args.line;
9224 cur_arg += 1;
9225 } else if (strncmp(args[0], "set-map", 7) == 0) {
9226 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9227 rule->action = HTTP_RES_ACT_SET_MAP;
9228 /*
9229 * '+ 8' for 'set-map('
9230 * '- 9' for 'set-map(' + trailing ')'
9231 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009232 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009233
9234 cur_arg = 1;
9235
9236 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9237 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9238 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9239 file, linenum, args[0]);
9240 goto out_err;
9241 }
9242
9243 LIST_INIT(&rule->arg.map.key);
9244 LIST_INIT(&rule->arg.map.value);
9245
9246 proxy->conf.args.ctx = ARGC_HRS;
9247
9248 /* key pattern */
9249 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9250 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9251 file, linenum);
9252
9253 /* value pattern */
9254 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9255 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9256 file, linenum);
9257
9258 free(proxy->conf.lfs_file);
9259 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9260 proxy->conf.lfs_line = proxy->conf.args.line;
9261
9262 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009263 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9264 char *errmsg = NULL;
9265 cur_arg = 1;
9266 /* try in the module list */
9267 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9268 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9269 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9270 free(errmsg);
9271 goto out_err;
9272 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009273 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009274 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 +02009275 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9276 goto out_err;
9277 }
9278
9279 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9280 struct acl_cond *cond;
9281 char *errmsg = NULL;
9282
9283 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9284 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9285 file, linenum, args[0], errmsg);
9286 free(errmsg);
9287 goto out_err;
9288 }
9289 rule->cond = cond;
9290 }
9291 else if (*args[cur_arg]) {
9292 Alert("parsing [%s:%d]: 'http-response %s' expects"
9293 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9294 file, linenum, args[0], args[cur_arg]);
9295 goto out_err;
9296 }
9297
9298 return rule;
9299 out_err:
9300 free(rule);
9301 return NULL;
9302}
9303
Willy Tarreau4baae242012-12-27 12:00:31 +01009304/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009305 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9306 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009307 */
9308struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009309 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009310{
9311 struct redirect_rule *rule;
9312 int cur_arg;
9313 int type = REDIRECT_TYPE_NONE;
9314 int code = 302;
9315 const char *destination = NULL;
9316 const char *cookie = NULL;
9317 int cookie_set = 0;
9318 unsigned int flags = REDIRECT_FLAG_NONE;
9319 struct acl_cond *cond = NULL;
9320
9321 cur_arg = 0;
9322 while (*(args[cur_arg])) {
9323 if (strcmp(args[cur_arg], "location") == 0) {
9324 if (!*args[cur_arg + 1])
9325 goto missing_arg;
9326
9327 type = REDIRECT_TYPE_LOCATION;
9328 cur_arg++;
9329 destination = args[cur_arg];
9330 }
9331 else if (strcmp(args[cur_arg], "prefix") == 0) {
9332 if (!*args[cur_arg + 1])
9333 goto missing_arg;
9334
9335 type = REDIRECT_TYPE_PREFIX;
9336 cur_arg++;
9337 destination = args[cur_arg];
9338 }
9339 else if (strcmp(args[cur_arg], "scheme") == 0) {
9340 if (!*args[cur_arg + 1])
9341 goto missing_arg;
9342
9343 type = REDIRECT_TYPE_SCHEME;
9344 cur_arg++;
9345 destination = args[cur_arg];
9346 }
9347 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9348 if (!*args[cur_arg + 1])
9349 goto missing_arg;
9350
9351 cur_arg++;
9352 cookie = args[cur_arg];
9353 cookie_set = 1;
9354 }
9355 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9356 if (!*args[cur_arg + 1])
9357 goto missing_arg;
9358
9359 cur_arg++;
9360 cookie = args[cur_arg];
9361 cookie_set = 0;
9362 }
9363 else if (strcmp(args[cur_arg], "code") == 0) {
9364 if (!*args[cur_arg + 1])
9365 goto missing_arg;
9366
9367 cur_arg++;
9368 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009369 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009370 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009371 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009372 args[cur_arg - 1], args[cur_arg]);
9373 return NULL;
9374 }
9375 }
9376 else if (!strcmp(args[cur_arg],"drop-query")) {
9377 flags |= REDIRECT_FLAG_DROP_QS;
9378 }
9379 else if (!strcmp(args[cur_arg],"append-slash")) {
9380 flags |= REDIRECT_FLAG_APPEND_SLASH;
9381 }
9382 else if (strcmp(args[cur_arg], "if") == 0 ||
9383 strcmp(args[cur_arg], "unless") == 0) {
9384 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9385 if (!cond) {
9386 memprintf(errmsg, "error in condition: %s", *errmsg);
9387 return NULL;
9388 }
9389 break;
9390 }
9391 else {
9392 memprintf(errmsg,
9393 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9394 args[cur_arg]);
9395 return NULL;
9396 }
9397 cur_arg++;
9398 }
9399
9400 if (type == REDIRECT_TYPE_NONE) {
9401 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9402 return NULL;
9403 }
9404
9405 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9406 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009407 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009408
9409 if (!use_fmt) {
9410 /* old-style static redirect rule */
9411 rule->rdr_str = strdup(destination);
9412 rule->rdr_len = strlen(destination);
9413 }
9414 else {
9415 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009416
9417 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9418 * if prefix == "/", we don't want to add anything, otherwise it
9419 * makes it hard for the user to configure a self-redirection.
9420 */
9421 proxy->conf.args.ctx = ARGC_RDR;
9422 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009423 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009424 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9425 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009426 free(curproxy->conf.lfs_file);
9427 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9428 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009429 }
9430 }
9431
Willy Tarreau4baae242012-12-27 12:00:31 +01009432 if (cookie) {
9433 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9434 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9435 */
9436 rule->cookie_len = strlen(cookie);
9437 if (cookie_set) {
9438 rule->cookie_str = malloc(rule->cookie_len + 10);
9439 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9440 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9441 rule->cookie_len += 9;
9442 } else {
9443 rule->cookie_str = malloc(rule->cookie_len + 21);
9444 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9445 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9446 rule->cookie_len += 20;
9447 }
9448 }
9449 rule->type = type;
9450 rule->code = code;
9451 rule->flags = flags;
9452 LIST_INIT(&rule->list);
9453 return rule;
9454
9455 missing_arg:
9456 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9457 return NULL;
9458}
9459
Willy Tarreau8797c062007-05-07 00:55:35 +02009460/************************************************************************/
9461/* The code below is dedicated to ACL parsing and matching */
9462/************************************************************************/
9463
9464
Willy Tarreau14174bc2012-04-16 14:34:04 +02009465/* This function ensures that the prerequisites for an L7 fetch are ready,
9466 * which means that a request or response is ready. If some data is missing,
9467 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009468 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9469 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009470 *
9471 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009472 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9473 * decide whether or not an HTTP message is present ;
9474 * 0 if the requested data cannot be fetched or if it is certain that
9475 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009476 * 1 if an HTTP message is ready
9477 */
9478static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009479smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009480 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009481{
9482 struct http_txn *txn = l7;
9483 struct http_msg *msg = &txn->req;
9484
9485 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9486 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9487 */
9488
9489 if (unlikely(!s || !txn))
9490 return 0;
9491
9492 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009493 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009494
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009495 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009496 if (unlikely(!s->req))
9497 return 0;
9498
Willy Tarreauaae75e32013-03-29 12:31:49 +01009499 /* If the buffer does not leave enough free space at the end,
9500 * we must first realign it.
9501 */
9502 if (s->req->buf->p > s->req->buf->data &&
9503 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9504 buffer_slow_realign(s->req->buf);
9505
Willy Tarreau14174bc2012-04-16 14:34:04 +02009506 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009507 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009508 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009509
9510 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009511 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009512 http_msg_analyzer(msg, &txn->hdr_idx);
9513
9514 /* Still no valid request ? */
9515 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009516 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009517 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009518 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009519 }
9520 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009521 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009522 return 0;
9523 }
9524
9525 /* OK we just got a valid HTTP request. We have some minor
9526 * preparation to perform so that further checks can rely
9527 * on HTTP tests.
9528 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009529
9530 /* If the request was parsed but was too large, we must absolutely
9531 * return an error so that it is not processed. At the moment this
9532 * cannot happen, but if the parsers are to change in the future,
9533 * we want this check to be maintained.
9534 */
9535 if (unlikely(s->req->buf->i + s->req->buf->p >
9536 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9537 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009538 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009539 return 1;
9540 }
9541
Willy Tarreau9b28e032012-10-12 23:49:43 +02009542 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009543 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9544 s->flags |= SN_REDIRECTABLE;
9545
Willy Tarreau506d0502013-07-06 13:29:24 +02009546 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9547 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009548 }
9549
Willy Tarreau506d0502013-07-06 13:29:24 +02009550 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009551 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009552 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009553
9554 /* otherwise everything's ready for the request */
9555 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009556 else {
9557 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009558 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9559 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009560 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009561 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009562 }
9563
9564 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009565 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009566 return 1;
9567}
Willy Tarreau8797c062007-05-07 00:55:35 +02009568
Willy Tarreauc0239e02012-04-16 14:42:55 +02009569#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009570 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 +02009571
Willy Tarreau24e32d82012-04-23 23:55:44 +02009572#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009573 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 +02009574
Willy Tarreau8797c062007-05-07 00:55:35 +02009575
9576/* 1. Check on METHOD
9577 * We use the pre-parsed method if it is known, and store its number as an
9578 * integer. If it is unknown, we use the pointer and the length.
9579 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009580static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009581{
9582 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009583 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009584
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009585 len = strlen(text);
9586 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009587
9588 pattern->val.i = meth;
9589 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009590 trash = get_trash_chunk();
9591 if (trash->size < len) {
9592 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9593 len, trash->size);
9594 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009595 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009596 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009597 pattern->len = len;
9598 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009599 else {
9600 pattern->ptr.str = NULL;
9601 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009602 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009603 return 1;
9604}
9605
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009606/* This function fetches the method of current HTTP request and stores
9607 * it in the global pattern struct as a chunk. There are two possibilities :
9608 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9609 * in <len> and <ptr> is NULL ;
9610 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9611 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009612 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009613 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009614static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009615smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009616 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009617{
9618 int meth;
9619 struct http_txn *txn = l7;
9620
Willy Tarreau24e32d82012-04-23 23:55:44 +02009621 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009622
Willy Tarreau8797c062007-05-07 00:55:35 +02009623 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009624 smp->type = SMP_T_METH;
9625 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009626 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009627 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9628 /* ensure the indexes are not affected */
9629 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009630 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009631 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9632 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009633 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009634 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009635 return 1;
9636}
9637
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009638/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009639static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009640{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009641 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009642 struct pattern_list *lst;
9643 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009644
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009645 list_for_each_entry(lst, &expr->patterns, list) {
9646 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009647
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009648 /* well-known method */
9649 if (pattern->val.i != HTTP_METH_OTHER) {
9650 if (smp->data.meth.meth == pattern->val.i)
9651 return pattern;
9652 else
9653 continue;
9654 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009655
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009656 /* Other method, we must compare the strings */
9657 if (pattern->len != smp->data.meth.str.len)
9658 continue;
9659
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009660 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009661 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9662 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9663 return pattern;
9664 }
9665 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009666}
9667
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009668static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009669smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009670 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009671{
9672 struct http_txn *txn = l7;
9673 char *ptr;
9674 int len;
9675
Willy Tarreauc0239e02012-04-16 14:42:55 +02009676 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009677
Willy Tarreau8797c062007-05-07 00:55:35 +02009678 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009679 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009680
9681 while ((len-- > 0) && (*ptr++ != '/'));
9682 if (len <= 0)
9683 return 0;
9684
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009685 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009686 smp->data.str.str = ptr;
9687 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009688
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009689 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009690 return 1;
9691}
9692
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009693static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009694smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009695 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009696{
9697 struct http_txn *txn = l7;
9698 char *ptr;
9699 int len;
9700
Willy Tarreauc0239e02012-04-16 14:42:55 +02009701 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009702
Willy Tarreauf26b2522012-12-14 08:33:14 +01009703 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9704 return 0;
9705
Willy Tarreau8797c062007-05-07 00:55:35 +02009706 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009707 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009708
9709 while ((len-- > 0) && (*ptr++ != '/'));
9710 if (len <= 0)
9711 return 0;
9712
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009713 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009714 smp->data.str.str = ptr;
9715 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009716
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009717 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009718 return 1;
9719}
9720
9721/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009722static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009723smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009724 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009725{
9726 struct http_txn *txn = l7;
9727 char *ptr;
9728 int len;
9729
Willy Tarreauc0239e02012-04-16 14:42:55 +02009730 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009731
Willy Tarreauf26b2522012-12-14 08:33:14 +01009732 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9733 return 0;
9734
Willy Tarreau8797c062007-05-07 00:55:35 +02009735 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009736 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009737
Willy Tarreauf853c462012-04-23 18:53:56 +02009738 smp->type = SMP_T_UINT;
9739 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009740 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009741 return 1;
9742}
9743
9744/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009745static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009746smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009747 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009748{
9749 struct http_txn *txn = l7;
9750
Willy Tarreauc0239e02012-04-16 14:42:55 +02009751 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009752
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009753 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009754 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009755 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009756 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009757 return 1;
9758}
9759
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009760static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009761smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009762 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009763{
9764 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009765 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009766
Willy Tarreauc0239e02012-04-16 14:42:55 +02009767 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009768
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009769 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 +02009770 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009771 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009772
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009773 smp->type = SMP_T_IPV4;
9774 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009775 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009776 return 1;
9777}
9778
9779static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009780smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009781 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009782{
9783 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009784 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009785
Willy Tarreauc0239e02012-04-16 14:42:55 +02009786 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009787
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009788 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 +02009789 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9790 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009791
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009792 smp->type = SMP_T_UINT;
9793 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009794 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009795 return 1;
9796}
9797
Willy Tarreau185b5c42012-04-26 15:11:51 +02009798/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9799 * Accepts an optional argument of type string containing the header field name,
9800 * and an optional argument of type signed or unsigned integer to request an
9801 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009802 * headers are considered from the first one. It does not stop on commas and
9803 * returns full lines instead (useful for User-Agent or Date for example).
9804 */
9805static int
9806smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009807 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009808{
9809 struct http_txn *txn = l7;
9810 struct hdr_idx *idx = &txn->hdr_idx;
9811 struct hdr_ctx *ctx = smp->ctx.a[0];
9812 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9813 int occ = 0;
9814 const char *name_str = NULL;
9815 int name_len = 0;
9816
9817 if (!ctx) {
9818 /* first call */
9819 ctx = &static_hdr_ctx;
9820 ctx->idx = 0;
9821 smp->ctx.a[0] = ctx;
9822 }
9823
9824 if (args) {
9825 if (args[0].type != ARGT_STR)
9826 return 0;
9827 name_str = args[0].data.str.str;
9828 name_len = args[0].data.str.len;
9829
9830 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9831 occ = args[1].data.uint;
9832 }
9833
9834 CHECK_HTTP_MESSAGE_FIRST();
9835
9836 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9837 /* search for header from the beginning */
9838 ctx->idx = 0;
9839
9840 if (!occ && !(opt & SMP_OPT_ITERATE))
9841 /* no explicit occurrence and single fetch => last header by default */
9842 occ = -1;
9843
9844 if (!occ)
9845 /* prepare to report multiple occurrences for ACL fetches */
9846 smp->flags |= SMP_F_NOT_LAST;
9847
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009848 smp->type = SMP_T_STR;
9849 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009850 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9851 return 1;
9852
9853 smp->flags &= ~SMP_F_NOT_LAST;
9854 return 0;
9855}
9856
9857/* 6. Check on HTTP header count. The number of occurrences is returned.
9858 * Accepts exactly 1 argument of type string. It does not stop on commas and
9859 * returns full lines instead (useful for User-Agent or Date for example).
9860 */
9861static int
9862smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009863 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009864{
9865 struct http_txn *txn = l7;
9866 struct hdr_idx *idx = &txn->hdr_idx;
9867 struct hdr_ctx ctx;
9868 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9869 int cnt;
9870
9871 if (!args || args->type != ARGT_STR)
9872 return 0;
9873
9874 CHECK_HTTP_MESSAGE_FIRST();
9875
9876 ctx.idx = 0;
9877 cnt = 0;
9878 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9879 cnt++;
9880
9881 smp->type = SMP_T_UINT;
9882 smp->data.uint = cnt;
9883 smp->flags = SMP_F_VOL_HDR;
9884 return 1;
9885}
9886
9887/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9888 * Accepts an optional argument of type string containing the header field name,
9889 * and an optional argument of type signed or unsigned integer to request an
9890 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009891 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009892 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009893static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009894smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009895 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009896{
9897 struct http_txn *txn = l7;
9898 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009899 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009900 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009901 int occ = 0;
9902 const char *name_str = NULL;
9903 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009904
Willy Tarreaua890d072013-04-02 12:01:06 +02009905 if (!ctx) {
9906 /* first call */
9907 ctx = &static_hdr_ctx;
9908 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009909 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009910 }
9911
Willy Tarreau185b5c42012-04-26 15:11:51 +02009912 if (args) {
9913 if (args[0].type != ARGT_STR)
9914 return 0;
9915 name_str = args[0].data.str.str;
9916 name_len = args[0].data.str.len;
9917
9918 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9919 occ = args[1].data.uint;
9920 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009921
Willy Tarreaue333ec92012-04-16 16:26:40 +02009922 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009923
Willy Tarreau185b5c42012-04-26 15:11:51 +02009924 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009925 /* search for header from the beginning */
9926 ctx->idx = 0;
9927
Willy Tarreau185b5c42012-04-26 15:11:51 +02009928 if (!occ && !(opt & SMP_OPT_ITERATE))
9929 /* no explicit occurrence and single fetch => last header by default */
9930 occ = -1;
9931
9932 if (!occ)
9933 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009934 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009935
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009936 smp->type = SMP_T_STR;
9937 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009938 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 +02009939 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009940
Willy Tarreau37406352012-04-23 16:16:37 +02009941 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009942 return 0;
9943}
9944
Willy Tarreauc11416f2007-06-17 16:58:38 +02009945/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009946 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009947 */
9948static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009949smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009950 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009951{
9952 struct http_txn *txn = l7;
9953 struct hdr_idx *idx = &txn->hdr_idx;
9954 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009955 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009956 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009957
Willy Tarreau24e32d82012-04-23 23:55:44 +02009958 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009959 return 0;
9960
Willy Tarreaue333ec92012-04-16 16:26:40 +02009961 CHECK_HTTP_MESSAGE_FIRST();
9962
Willy Tarreau33a7e692007-06-10 19:45:56 +02009963 ctx.idx = 0;
9964 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009965 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 +02009966 cnt++;
9967
Willy Tarreauf853c462012-04-23 18:53:56 +02009968 smp->type = SMP_T_UINT;
9969 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009970 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009971 return 1;
9972}
9973
Willy Tarreau185b5c42012-04-26 15:11:51 +02009974/* Fetch an HTTP header's integer value. The integer value is returned. It
9975 * takes a mandatory argument of type string and an optional one of type int
9976 * to designate a specific occurrence. It returns an unsigned integer, which
9977 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009978 */
9979static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009980smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009981 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009982{
Willy Tarreauef38c392013-07-22 16:29:32 +02009983 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009984
Willy Tarreauf853c462012-04-23 18:53:56 +02009985 if (ret > 0) {
9986 smp->type = SMP_T_UINT;
9987 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9988 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009989
Willy Tarreaud53e2422012-04-16 17:21:11 +02009990 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009991}
9992
Cyril Bonté69fa9922012-10-25 00:01:06 +02009993/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9994 * and an optional one of type int to designate a specific occurrence.
9995 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009996 */
9997static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009998smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009999 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010000{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010001 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010002
Willy Tarreauef38c392013-07-22 16:29:32 +020010003 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010004 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10005 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010006 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010007 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010008 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010009 if (smp->data.str.len < temp->size - 1) {
10010 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10011 temp->str[smp->data.str.len] = '\0';
10012 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10013 smp->type = SMP_T_IPV6;
10014 break;
10015 }
10016 }
10017 }
10018
Willy Tarreaud53e2422012-04-16 17:21:11 +020010019 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010020 if (!(smp->flags & SMP_F_NOT_LAST))
10021 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010022 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010023 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010024}
10025
Willy Tarreau737b0c12007-06-10 21:28:46 +020010026/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10027 * the first '/' after the possible hostname, and ends before the possible '?'.
10028 */
10029static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010030smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010031 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010032{
10033 struct http_txn *txn = l7;
10034 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010035
Willy Tarreauc0239e02012-04-16 14:42:55 +020010036 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010037
Willy Tarreau9b28e032012-10-12 23:49:43 +020010038 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010039 ptr = http_get_path(txn);
10040 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010041 return 0;
10042
10043 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010044 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010045 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010046
10047 while (ptr < end && *ptr != '?')
10048 ptr++;
10049
Willy Tarreauf853c462012-04-23 18:53:56 +020010050 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010051 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010052 return 1;
10053}
10054
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010055/* This produces a concatenation of the first occurrence of the Host header
10056 * followed by the path component if it begins with a slash ('/'). This means
10057 * that '*' will not be added, resulting in exactly the first Host entry.
10058 * If no Host header is found, then the path is returned as-is. The returned
10059 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010060 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010061 */
10062static int
10063smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010064 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010065{
10066 struct http_txn *txn = l7;
10067 char *ptr, *end, *beg;
10068 struct hdr_ctx ctx;
10069
10070 CHECK_HTTP_MESSAGE_FIRST();
10071
10072 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010073 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010074 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010075
10076 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010077 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010078 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010079 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010080 smp->data.str.len = ctx.vlen;
10081
10082 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010083 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010084 beg = http_get_path(txn);
10085 if (!beg)
10086 beg = end;
10087
10088 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10089
10090 if (beg < ptr && *beg == '/') {
10091 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10092 smp->data.str.len += ptr - beg;
10093 }
10094
10095 smp->flags = SMP_F_VOL_1ST;
10096 return 1;
10097}
10098
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010099/* This produces a 32-bit hash of the concatenation of the first occurrence of
10100 * the Host header followed by the path component if it begins with a slash ('/').
10101 * This means that '*' will not be added, resulting in exactly the first Host
10102 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010103 * is hashed using the path hash followed by a full avalanche hash and provides a
10104 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010105 * high-traffic sites without having to store whole paths.
10106 */
10107static int
10108smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010109 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010110{
10111 struct http_txn *txn = l7;
10112 struct hdr_ctx ctx;
10113 unsigned int hash = 0;
10114 char *ptr, *beg, *end;
10115 int len;
10116
10117 CHECK_HTTP_MESSAGE_FIRST();
10118
10119 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010120 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010121 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10122 ptr = ctx.line + ctx.val;
10123 len = ctx.vlen;
10124 while (len--)
10125 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10126 }
10127
10128 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010129 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010130 beg = http_get_path(txn);
10131 if (!beg)
10132 beg = end;
10133
10134 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10135
10136 if (beg < ptr && *beg == '/') {
10137 while (beg < ptr)
10138 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10139 }
10140 hash = full_hash(hash);
10141
10142 smp->type = SMP_T_UINT;
10143 smp->data.uint = hash;
10144 smp->flags = SMP_F_VOL_1ST;
10145 return 1;
10146}
10147
Willy Tarreau4a550602012-12-09 14:53:32 +010010148/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010149 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10150 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10151 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010152 * that in environments where IPv6 is insignificant, truncating the output to
10153 * 8 bytes would still work.
10154 */
10155static int
10156smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010157 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010158{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010159 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010160 struct connection *cli_conn = objt_conn(l4->si[0].end);
10161
10162 if (!cli_conn)
10163 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010164
Willy Tarreauef38c392013-07-22 16:29:32 +020010165 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010166 return 0;
10167
Willy Tarreau47ca5452012-12-23 20:22:19 +010010168 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +010010169 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10170 temp->len += sizeof(smp->data.uint);
10171
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010172 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010173 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010174 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010175 temp->len += 4;
10176 break;
10177 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010178 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010179 temp->len += 16;
10180 break;
10181 default:
10182 return 0;
10183 }
10184
10185 smp->data.str = *temp;
10186 smp->type = SMP_T_BIN;
10187 return 1;
10188}
10189
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010190static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010191smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010192 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010193{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010194 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10195 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10196 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010197
Willy Tarreau24e32d82012-04-23 23:55:44 +020010198 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010199
Willy Tarreauf853c462012-04-23 18:53:56 +020010200 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010201 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010202 return 1;
10203}
10204
Willy Tarreau7f18e522010-10-22 20:04:13 +020010205/* return a valid test if the current request is the first one on the connection */
10206static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010207smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010208 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010209{
10210 if (!s)
10211 return 0;
10212
Willy Tarreauf853c462012-04-23 18:53:56 +020010213 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010214 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010215 return 1;
10216}
10217
Willy Tarreau34db1082012-04-19 17:16:54 +020010218/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010219static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010220smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010221 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010222{
10223
Willy Tarreau24e32d82012-04-23 23:55:44 +020010224 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010225 return 0;
10226
Willy Tarreauc0239e02012-04-16 14:42:55 +020010227 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010228
Willy Tarreauc0239e02012-04-16 14:42:55 +020010229 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010230 return 0;
10231
Willy Tarreauf853c462012-04-23 18:53:56 +020010232 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010233 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010234 return 1;
10235}
Willy Tarreau8797c062007-05-07 00:55:35 +020010236
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010237/* Accepts exactly 1 argument of type userlist */
10238static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010239smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010240 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010241{
10242
10243 if (!args || args->type != ARGT_USR)
10244 return 0;
10245
10246 CHECK_HTTP_MESSAGE_FIRST();
10247
10248 if (!get_http_auth(l4))
10249 return 0;
10250
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010251 /* if the user does not belong to the userlist or has a wrong password,
10252 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010253 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010254 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010255 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10256 return 0;
10257
10258 /* pat_match_auth() will need the user list */
10259 smp->ctx.a[0] = args->data.usr;
10260
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010261 smp->type = SMP_T_STR;
10262 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010263 smp->data.str.str = l4->txn.auth.user;
10264 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010265
10266 return 1;
10267}
10268
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010269/* Try to find the next occurrence of a cookie name in a cookie header value.
10270 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10271 * the cookie value is returned into *value and *value_l, and the function
10272 * returns a pointer to the next pointer to search from if the value was found.
10273 * Otherwise if the cookie was not found, NULL is returned and neither value
10274 * nor value_l are touched. The input <hdr> string should first point to the
10275 * header's value, and the <hdr_end> pointer must point to the first character
10276 * not part of the value. <list> must be non-zero if value may represent a list
10277 * of values (cookie headers). This makes it faster to abort parsing when no
10278 * list is expected.
10279 */
10280static char *
10281extract_cookie_value(char *hdr, const char *hdr_end,
10282 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010283 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010284{
10285 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10286 char *next;
10287
10288 /* we search at least a cookie name followed by an equal, and more
10289 * generally something like this :
10290 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10291 */
10292 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10293 /* Iterate through all cookies on this line */
10294
10295 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10296 att_beg++;
10297
10298 /* find att_end : this is the first character after the last non
10299 * space before the equal. It may be equal to hdr_end.
10300 */
10301 equal = att_end = att_beg;
10302
10303 while (equal < hdr_end) {
10304 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10305 break;
10306 if (http_is_spht[(unsigned char)*equal++])
10307 continue;
10308 att_end = equal;
10309 }
10310
10311 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10312 * is between <att_beg> and <equal>, both may be identical.
10313 */
10314
10315 /* look for end of cookie if there is an equal sign */
10316 if (equal < hdr_end && *equal == '=') {
10317 /* look for the beginning of the value */
10318 val_beg = equal + 1;
10319 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10320 val_beg++;
10321
10322 /* find the end of the value, respecting quotes */
10323 next = find_cookie_value_end(val_beg, hdr_end);
10324
10325 /* make val_end point to the first white space or delimitor after the value */
10326 val_end = next;
10327 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10328 val_end--;
10329 } else {
10330 val_beg = val_end = next = equal;
10331 }
10332
10333 /* We have nothing to do with attributes beginning with '$'. However,
10334 * they will automatically be removed if a header before them is removed,
10335 * since they're supposed to be linked together.
10336 */
10337 if (*att_beg == '$')
10338 continue;
10339
10340 /* Ignore cookies with no equal sign */
10341 if (equal == next)
10342 continue;
10343
10344 /* Now we have the cookie name between att_beg and att_end, and
10345 * its value between val_beg and val_end.
10346 */
10347
10348 if (att_end - att_beg == cookie_name_l &&
10349 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10350 /* let's return this value and indicate where to go on from */
10351 *value = val_beg;
10352 *value_l = val_end - val_beg;
10353 return next + 1;
10354 }
10355
10356 /* Set-Cookie headers only have the name in the first attr=value part */
10357 if (!list)
10358 break;
10359 }
10360
10361 return NULL;
10362}
10363
William Lallemanda43ba4e2014-01-28 18:14:25 +010010364/* Fetch a captured HTTP request header. The index is the position of
10365 * the "capture" option in the configuration file
10366 */
10367static int
10368smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10369 const struct arg *args, struct sample *smp, const char *kw)
10370{
10371 struct proxy *fe = l4->fe;
10372 struct http_txn *txn = l7;
10373 int idx;
10374
10375 if (!args || args->type != ARGT_UINT)
10376 return 0;
10377
10378 idx = args->data.uint;
10379
10380 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10381 return 0;
10382
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010383 smp->type = SMP_T_STR;
10384 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010385 smp->data.str.str = txn->req.cap[idx];
10386 smp->data.str.len = strlen(txn->req.cap[idx]);
10387
10388 return 1;
10389}
10390
10391/* Fetch a captured HTTP response header. The index is the position of
10392 * the "capture" option in the configuration file
10393 */
10394static int
10395smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10396 const struct arg *args, struct sample *smp, const char *kw)
10397{
10398 struct proxy *fe = l4->fe;
10399 struct http_txn *txn = l7;
10400 int idx;
10401
10402 if (!args || args->type != ARGT_UINT)
10403 return 0;
10404
10405 idx = args->data.uint;
10406
10407 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10408 return 0;
10409
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010410 smp->type = SMP_T_STR;
10411 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010412 smp->data.str.str = txn->rsp.cap[idx];
10413 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10414
10415 return 1;
10416}
10417
William Lallemand65ad6e12014-01-31 15:08:02 +010010418/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10419static int
10420smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10421 const struct arg *args, struct sample *smp, const char *kw)
10422{
10423 struct chunk *temp;
10424 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010425 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010426
10427 if (!txn->uri)
10428 return 0;
10429
William Lallemand96a77852014-02-05 00:30:02 +010010430 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010431
William Lallemand96a77852014-02-05 00:30:02 +010010432 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10433 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010434
William Lallemand96a77852014-02-05 00:30:02 +010010435 temp = get_trash_chunk();
10436 temp->str = txn->uri;
10437 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010438 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010439 smp->type = SMP_T_STR;
10440 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010441
10442 return 1;
10443
10444}
10445
10446/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10447static int
10448smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10449 const struct arg *args, struct sample *smp, const char *kw)
10450{
10451 struct chunk *temp;
10452 struct http_txn *txn = l7;
10453 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010454
10455 if (!txn->uri)
10456 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010457
William Lallemand65ad6e12014-01-31 15:08:02 +010010458 ptr = txn->uri;
10459
10460 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10461 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010462
William Lallemand65ad6e12014-01-31 15:08:02 +010010463 if (!*ptr)
10464 return 0;
10465
10466 ptr++; /* skip the space */
10467
10468 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010469 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010470 if (!ptr)
10471 return 0;
10472 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10473 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010474
10475 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010476 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010477 smp->type = SMP_T_STR;
10478 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010479
10480 return 1;
10481}
10482
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010483/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10484 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10485 */
10486static int
10487smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10488 const struct arg *args, struct sample *smp, const char *kw)
10489{
10490 struct http_txn *txn = l7;
10491
10492 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10493 return 0;
10494
10495 if (txn->req.flags & HTTP_MSGF_VER_11)
10496 smp->data.str.str = "HTTP/1.1";
10497 else
10498 smp->data.str.str = "HTTP/1.0";
10499
10500 smp->data.str.len = 8;
10501 smp->type = SMP_T_STR;
10502 smp->flags = SMP_F_CONST;
10503 return 1;
10504
10505}
10506
10507/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10508 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10509 */
10510static int
10511smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10512 const struct arg *args, struct sample *smp, const char *kw)
10513{
10514 struct http_txn *txn = l7;
10515
10516 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10517 return 0;
10518
10519 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10520 smp->data.str.str = "HTTP/1.1";
10521 else
10522 smp->data.str.str = "HTTP/1.0";
10523
10524 smp->data.str.len = 8;
10525 smp->type = SMP_T_STR;
10526 smp->flags = SMP_F_CONST;
10527 return 1;
10528
10529}
10530
William Lallemand65ad6e12014-01-31 15:08:02 +010010531
Willy Tarreaue333ec92012-04-16 16:26:40 +020010532/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010533 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010534 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010535 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010536 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010537 * Accepts exactly 1 argument of type string. If the input options indicate
10538 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010539 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010540 */
10541static int
Willy Tarreau51539362012-05-08 12:46:28 +020010542smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010543 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010544{
10545 struct http_txn *txn = l7;
10546 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010547 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010548 const struct http_msg *msg;
10549 const char *hdr_name;
10550 int hdr_name_len;
10551 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010552 int occ = 0;
10553 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010554
Willy Tarreau24e32d82012-04-23 23:55:44 +020010555 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010556 return 0;
10557
Willy Tarreaua890d072013-04-02 12:01:06 +020010558 if (!ctx) {
10559 /* first call */
10560 ctx = &static_hdr_ctx;
10561 ctx->idx = 0;
10562 smp->ctx.a[2] = ctx;
10563 }
10564
Willy Tarreaue333ec92012-04-16 16:26:40 +020010565 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010566
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010567 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010568 msg = &txn->req;
10569 hdr_name = "Cookie";
10570 hdr_name_len = 6;
10571 } else {
10572 msg = &txn->rsp;
10573 hdr_name = "Set-Cookie";
10574 hdr_name_len = 10;
10575 }
10576
Willy Tarreau28376d62012-04-26 21:26:10 +020010577 if (!occ && !(opt & SMP_OPT_ITERATE))
10578 /* no explicit occurrence and single fetch => last cookie by default */
10579 occ = -1;
10580
10581 /* OK so basically here, either we want only one value and it's the
10582 * last one, or we want to iterate over all of them and we fetch the
10583 * next one.
10584 */
10585
Willy Tarreau9b28e032012-10-12 23:49:43 +020010586 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010587 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010588 /* search for the header from the beginning, we must first initialize
10589 * the search parameters.
10590 */
Willy Tarreau37406352012-04-23 16:16:37 +020010591 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010592 ctx->idx = 0;
10593 }
10594
Willy Tarreau28376d62012-04-26 21:26:10 +020010595 smp->flags |= SMP_F_VOL_HDR;
10596
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010597 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010598 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10599 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010600 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10601 goto out;
10602
Willy Tarreau24e32d82012-04-23 23:55:44 +020010603 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010604 continue;
10605
Willy Tarreau37406352012-04-23 16:16:37 +020010606 smp->ctx.a[0] = ctx->line + ctx->val;
10607 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010608 }
10609
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010610 smp->type = SMP_T_STR;
10611 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010612 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010613 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010614 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010615 &smp->data.str.str,
10616 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010617 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010618 found = 1;
10619 if (occ >= 0) {
10620 /* one value was returned into smp->data.str.{str,len} */
10621 smp->flags |= SMP_F_NOT_LAST;
10622 return 1;
10623 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010624 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010625 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010626 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010627 /* all cookie headers and values were scanned. If we're looking for the
10628 * last occurrence, we may return it now.
10629 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010630 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010631 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010632 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010633}
10634
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010635/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010636 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010637 * multiple cookies may be parsed on the same line. The returned sample is of
10638 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010639 */
10640static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010641smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010642 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010643{
10644 struct http_txn *txn = l7;
10645 struct hdr_idx *idx = &txn->hdr_idx;
10646 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010647 const struct http_msg *msg;
10648 const char *hdr_name;
10649 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010650 int cnt;
10651 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010652 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010653
Willy Tarreau24e32d82012-04-23 23:55:44 +020010654 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010655 return 0;
10656
Willy Tarreaue333ec92012-04-16 16:26:40 +020010657 CHECK_HTTP_MESSAGE_FIRST();
10658
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010659 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010660 msg = &txn->req;
10661 hdr_name = "Cookie";
10662 hdr_name_len = 6;
10663 } else {
10664 msg = &txn->rsp;
10665 hdr_name = "Set-Cookie";
10666 hdr_name_len = 10;
10667 }
10668
Willy Tarreau9b28e032012-10-12 23:49:43 +020010669 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010670 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010671 ctx.idx = 0;
10672 cnt = 0;
10673
10674 while (1) {
10675 /* Note: val_beg == NULL every time we need to fetch a new header */
10676 if (!val_beg) {
10677 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10678 break;
10679
Willy Tarreau24e32d82012-04-23 23:55:44 +020010680 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010681 continue;
10682
10683 val_beg = ctx.line + ctx.val;
10684 val_end = val_beg + ctx.vlen;
10685 }
10686
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010687 smp->type = SMP_T_STR;
10688 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010689 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010690 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010691 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010692 &smp->data.str.str,
10693 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010694 cnt++;
10695 }
10696 }
10697
Willy Tarreaub169eba2013-12-16 15:14:43 +010010698 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010699 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010700 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010701 return 1;
10702}
10703
Willy Tarreau51539362012-05-08 12:46:28 +020010704/* Fetch an cookie's integer value. The integer value is returned. It
10705 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10706 */
10707static int
10708smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010709 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010710{
Willy Tarreauef38c392013-07-22 16:29:32 +020010711 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010712
10713 if (ret > 0) {
10714 smp->type = SMP_T_UINT;
10715 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10716 }
10717
10718 return ret;
10719}
10720
Willy Tarreau8797c062007-05-07 00:55:35 +020010721/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010722/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010723/************************************************************************/
10724
David Cournapeau16023ee2010-12-23 20:55:41 +090010725/*
10726 * Given a path string and its length, find the position of beginning of the
10727 * query string. Returns NULL if no query string is found in the path.
10728 *
10729 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10730 *
10731 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10732 */
bedis4c75cca2012-10-05 08:38:24 +020010733static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010734{
10735 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010736
bedis4c75cca2012-10-05 08:38:24 +020010737 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010738 return p ? p + 1 : NULL;
10739}
10740
bedis4c75cca2012-10-05 08:38:24 +020010741static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010742{
bedis4c75cca2012-10-05 08:38:24 +020010743 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010744}
10745
10746/*
10747 * Given a url parameter, find the starting position of the first occurence,
10748 * or NULL if the parameter is not found.
10749 *
10750 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10751 * the function will return query_string+8.
10752 */
10753static char*
10754find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010755 char* url_param_name, size_t url_param_name_l,
10756 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010757{
10758 char *pos, *last;
10759
10760 pos = query_string;
10761 last = query_string + query_string_l - url_param_name_l - 1;
10762
10763 while (pos <= last) {
10764 if (pos[url_param_name_l] == '=') {
10765 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10766 return pos;
10767 pos += url_param_name_l + 1;
10768 }
bedis4c75cca2012-10-05 08:38:24 +020010769 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010770 pos++;
10771 pos++;
10772 }
10773 return NULL;
10774}
10775
10776/*
10777 * Given a url parameter name, returns its value and size into *value and
10778 * *value_l respectively, and returns non-zero. If the parameter is not found,
10779 * zero is returned and value/value_l are not touched.
10780 */
10781static int
10782find_url_param_value(char* path, size_t path_l,
10783 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010784 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010785{
10786 char *query_string, *qs_end;
10787 char *arg_start;
10788 char *value_start, *value_end;
10789
bedis4c75cca2012-10-05 08:38:24 +020010790 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010791 if (!query_string)
10792 return 0;
10793
10794 qs_end = path + path_l;
10795 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010796 url_param_name, url_param_name_l,
10797 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010798 if (!arg_start)
10799 return 0;
10800
10801 value_start = arg_start + url_param_name_l + 1;
10802 value_end = value_start;
10803
bedis4c75cca2012-10-05 08:38:24 +020010804 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010805 value_end++;
10806
10807 *value = value_start;
10808 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010809 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010810}
10811
10812static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010813smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010814 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010815{
bedis4c75cca2012-10-05 08:38:24 +020010816 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010817 struct http_txn *txn = l7;
10818 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010819
bedis4c75cca2012-10-05 08:38:24 +020010820 if (!args || args[0].type != ARGT_STR ||
10821 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010822 return 0;
10823
10824 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010825
bedis4c75cca2012-10-05 08:38:24 +020010826 if (args[1].type)
10827 delim = *args[1].data.str.str;
10828
Willy Tarreau9b28e032012-10-12 23:49:43 +020010829 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010830 args->data.str.str, args->data.str.len,
10831 &smp->data.str.str, &smp->data.str.len,
10832 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010833 return 0;
10834
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010835 smp->type = SMP_T_STR;
10836 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010837 return 1;
10838}
10839
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010840/* Return the signed integer value for the specified url parameter (see url_param
10841 * above).
10842 */
10843static int
10844smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010845 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010846{
Willy Tarreauef38c392013-07-22 16:29:32 +020010847 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010848
10849 if (ret > 0) {
10850 smp->type = SMP_T_UINT;
10851 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10852 }
10853
10854 return ret;
10855}
10856
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010857/* This produces a 32-bit hash of the concatenation of the first occurrence of
10858 * the Host header followed by the path component if it begins with a slash ('/').
10859 * This means that '*' will not be added, resulting in exactly the first Host
10860 * entry. If no Host header is found, then the path is used. The resulting value
10861 * is hashed using the url hash followed by a full avalanche hash and provides a
10862 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10863 * high-traffic sites without having to store whole paths.
10864 * this differs from the base32 functions in that it includes the url parameters
10865 * as well as the path
10866 */
10867static int
10868smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010869 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010870{
10871 struct http_txn *txn = l7;
10872 struct hdr_ctx ctx;
10873 unsigned int hash = 0;
10874 char *ptr, *beg, *end;
10875 int len;
10876
10877 CHECK_HTTP_MESSAGE_FIRST();
10878
10879 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010880 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010881 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10882 ptr = ctx.line + ctx.val;
10883 len = ctx.vlen;
10884 while (len--)
10885 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10886 }
10887
10888 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010889 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 +000010890 beg = http_get_path(txn);
10891 if (!beg)
10892 beg = end;
10893
10894 for (ptr = beg; ptr < end ; ptr++);
10895
10896 if (beg < ptr && *beg == '/') {
10897 while (beg < ptr)
10898 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10899 }
10900 hash = full_hash(hash);
10901
10902 smp->type = SMP_T_UINT;
10903 smp->data.uint = hash;
10904 smp->flags = SMP_F_VOL_1ST;
10905 return 1;
10906}
10907
10908/* This concatenates the source address with the 32-bit hash of the Host and
10909 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10910 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10911 * on the source address length. The URL hash is stored before the address so
10912 * that in environments where IPv6 is insignificant, truncating the output to
10913 * 8 bytes would still work.
10914 */
10915static int
10916smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010917 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010918{
10919 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010920 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010921
Willy Tarreaue155ec22013-11-18 18:33:22 +010010922 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010923 return 0;
10924
10925 temp = get_trash_chunk();
10926 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10927 temp->len += sizeof(smp->data.uint);
10928
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010929 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010930 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010931 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010932 temp->len += 4;
10933 break;
10934 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010935 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010936 temp->len += 16;
10937 break;
10938 default:
10939 return 0;
10940 }
10941
10942 smp->data.str = *temp;
10943 smp->type = SMP_T_BIN;
10944 return 1;
10945}
10946
Willy Tarreau185b5c42012-04-26 15:11:51 +020010947/* This function is used to validate the arguments passed to any "hdr" fetch
10948 * keyword. These keywords support an optional positive or negative occurrence
10949 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10950 * is assumed that the types are already the correct ones. Returns 0 on error,
10951 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10952 * error message in case of error, that the caller is responsible for freeing.
10953 * The initial location must either be freeable or NULL.
10954 */
10955static int val_hdr(struct arg *arg, char **err_msg)
10956{
10957 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010958 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010959 return 0;
10960 }
10961 return 1;
10962}
10963
Willy Tarreau276fae92013-07-25 14:36:01 +020010964/* takes an UINT value on input supposed to represent the time since EPOCH,
10965 * adds an optional offset found in args[0] and emits a string representing
10966 * the date in RFC-1123/5322 format.
10967 */
10968static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10969{
10970 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10971 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10972 struct chunk *temp;
10973 struct tm *tm;
10974 time_t curr_date = smp->data.uint;
10975
10976 /* add offset */
10977 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10978 curr_date += args[0].data.sint;
10979
10980 tm = gmtime(&curr_date);
10981
10982 temp = get_trash_chunk();
10983 temp->len = snprintf(temp->str, temp->size - temp->len,
10984 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10985 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10986 tm->tm_hour, tm->tm_min, tm->tm_sec);
10987
10988 smp->data.str = *temp;
10989 smp->type = SMP_T_STR;
10990 return 1;
10991}
10992
Thierry FOURNIERad903512014-04-11 17:51:01 +020010993/* Match language range with language tag. RFC2616 14.4:
10994 *
10995 * A language-range matches a language-tag if it exactly equals
10996 * the tag, or if it exactly equals a prefix of the tag such
10997 * that the first tag character following the prefix is "-".
10998 *
10999 * Return 1 if the strings match, else return 0.
11000 */
11001static inline int language_range_match(const char *range, int range_len,
11002 const char *tag, int tag_len)
11003{
11004 const char *end = range + range_len;
11005 const char *tend = tag + tag_len;
11006 while (range < end) {
11007 if (*range == '-' && tag == tend)
11008 return 1;
11009 if (*range != *tag || tag == tend)
11010 return 0;
11011 range++;
11012 tag++;
11013 }
11014 /* Return true only if the last char of the tag is matched. */
11015 return tag == tend;
11016}
11017
11018/* Arguments: The list of expected value, the number of parts returned and the separator */
11019static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11020{
11021 const char *al = smp->data.str.str;
11022 const char *end = al + smp->data.str.len;
11023 const char *token;
11024 int toklen;
11025 int qvalue;
11026 const char *str;
11027 const char *w;
11028 int best_q = 0;
11029
11030 /* Set the constant to the sample, because the output of the
11031 * function will be peek in the constant configuration string.
11032 */
11033 smp->flags |= SMP_F_CONST;
11034 smp->data.str.size = 0;
11035 smp->data.str.str = "";
11036 smp->data.str.len = 0;
11037
11038 /* Parse the accept language */
11039 while (1) {
11040
11041 /* Jump spaces, quit if the end is detected. */
11042 while (al < end && isspace(*al))
11043 al++;
11044 if (al >= end)
11045 break;
11046
11047 /* Start of the fisrt word. */
11048 token = al;
11049
11050 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
11051 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
11052 al++;
11053 if (al == token)
11054 goto expect_comma;
11055
11056 /* Length of the token. */
11057 toklen = al - token;
11058 qvalue = 1000;
11059
11060 /* Check if the token exists in the list. If the token not exists,
11061 * jump to the next token.
11062 */
11063 str = args[0].data.str.str;
11064 w = str;
11065 while (1) {
11066 if (*str == ';' || *str == '\0') {
11067 if (language_range_match(token, toklen, w, str-w))
11068 goto look_for_q;
11069 if (*str == '\0')
11070 goto expect_comma;
11071 w = str + 1;
11072 }
11073 str++;
11074 }
11075 goto expect_comma;
11076
11077look_for_q:
11078
11079 /* Jump spaces, quit if the end is detected. */
11080 while (al < end && isspace(*al))
11081 al++;
11082 if (al >= end)
11083 goto process_value;
11084
11085 /* If ',' is found, process the result */
11086 if (*al == ',')
11087 goto process_value;
11088
11089 /* If the character is different from ';', look
11090 * for the end of the header part in best effort.
11091 */
11092 if (*al != ';')
11093 goto expect_comma;
11094
11095 /* Assumes that the char is ';', now expect "q=". */
11096 al++;
11097
11098 /* Jump spaces, process value if the end is detected. */
11099 while (al < end && isspace(*al))
11100 al++;
11101 if (al >= end)
11102 goto process_value;
11103
11104 /* Expect 'q'. If no 'q', continue in best effort */
11105 if (*al != 'q')
11106 goto process_value;
11107 al++;
11108
11109 /* Jump spaces, process value if the end is detected. */
11110 while (al < end && isspace(*al))
11111 al++;
11112 if (al >= end)
11113 goto process_value;
11114
11115 /* Expect '='. If no '=', continue in best effort */
11116 if (*al != '=')
11117 goto process_value;
11118 al++;
11119
11120 /* Jump spaces, process value if the end is detected. */
11121 while (al < end && isspace(*al))
11122 al++;
11123 if (al >= end)
11124 goto process_value;
11125
11126 /* Parse the q value. */
11127 qvalue = parse_qvalue(al, &al);
11128
11129process_value:
11130
11131 /* If the new q value is the best q value, then store the associated
11132 * language in the response. If qvalue is the biggest value (1000),
11133 * break the process.
11134 */
11135 if (qvalue > best_q) {
11136 smp->data.str.str = (char *)w;
11137 smp->data.str.len = str - w;
11138 if (qvalue >= 1000)
11139 break;
11140 best_q = qvalue;
11141 }
11142
11143expect_comma:
11144
11145 /* Expect comma or end. If the end is detected, quit the loop. */
11146 while (al < end && *al != ',')
11147 al++;
11148 if (al >= end)
11149 break;
11150
11151 /* Comma is found, jump it and restart the analyzer. */
11152 al++;
11153 }
11154
11155 /* Set default value if required. */
11156 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11157 smp->data.str.str = args[1].data.str.str;
11158 smp->data.str.len = args[1].data.str.len;
11159 }
11160
11161 /* Return true only if a matching language was found. */
11162 return smp->data.str.len != 0;
11163}
11164
William Lallemand73025dd2014-04-24 14:38:37 +020011165/*
11166 * Return the struct http_req_action_kw associated to a keyword.
11167 */
11168struct http_req_action_kw *action_http_req_custom(const char *kw)
11169{
11170 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11171 struct http_req_action_kw_list *kw_list;
11172 int i;
11173
11174 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11175 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11176 if (!strcmp(kw, kw_list->kw[i].kw))
11177 return &kw_list->kw[i];
11178 }
11179 }
11180 }
11181 return NULL;
11182}
11183
11184/*
11185 * Return the struct http_res_action_kw associated to a keyword.
11186 */
11187struct http_res_action_kw *action_http_res_custom(const char *kw)
11188{
11189 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11190 struct http_res_action_kw_list *kw_list;
11191 int i;
11192
11193 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11194 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11195 if (!strcmp(kw, kw_list->kw[i].kw))
11196 return &kw_list->kw[i];
11197 }
11198 }
11199 }
11200 return NULL;
11201}
11202
Willy Tarreau4a568972010-05-12 08:08:50 +020011203/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011204/* All supported ACL keywords must be declared here. */
11205/************************************************************************/
11206
11207/* Note: must not be declared <const> as its list will be overwritten.
11208 * Please take care of keeping this list alphabetically sorted.
11209 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011210static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011211 { "base", "base", PAT_MATCH_STR },
11212 { "base_beg", "base", PAT_MATCH_BEG },
11213 { "base_dir", "base", PAT_MATCH_DIR },
11214 { "base_dom", "base", PAT_MATCH_DOM },
11215 { "base_end", "base", PAT_MATCH_END },
11216 { "base_len", "base", PAT_MATCH_LEN },
11217 { "base_reg", "base", PAT_MATCH_REG },
11218 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011219
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011220 { "cook", "req.cook", PAT_MATCH_STR },
11221 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11222 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11223 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11224 { "cook_end", "req.cook", PAT_MATCH_END },
11225 { "cook_len", "req.cook", PAT_MATCH_LEN },
11226 { "cook_reg", "req.cook", PAT_MATCH_REG },
11227 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011228
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011229 { "hdr", "req.hdr", PAT_MATCH_STR },
11230 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11231 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11232 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11233 { "hdr_end", "req.hdr", PAT_MATCH_END },
11234 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11235 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11236 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011237
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011238 /* these two declarations uses strings with list storage (in place
11239 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11240 * and delete functions are relative to the list management. The parse
11241 * and match method are related to the corresponding fetch methods. This
11242 * is very particular ACL declaration mode.
11243 */
11244 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11245 { "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 +020011246
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011247 { "path", "path", PAT_MATCH_STR },
11248 { "path_beg", "path", PAT_MATCH_BEG },
11249 { "path_dir", "path", PAT_MATCH_DIR },
11250 { "path_dom", "path", PAT_MATCH_DOM },
11251 { "path_end", "path", PAT_MATCH_END },
11252 { "path_len", "path", PAT_MATCH_LEN },
11253 { "path_reg", "path", PAT_MATCH_REG },
11254 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011255
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011256 { "req_ver", "req.ver", PAT_MATCH_STR },
11257 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011258
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011259 { "scook", "res.cook", PAT_MATCH_STR },
11260 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11261 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11262 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11263 { "scook_end", "res.cook", PAT_MATCH_END },
11264 { "scook_len", "res.cook", PAT_MATCH_LEN },
11265 { "scook_reg", "res.cook", PAT_MATCH_REG },
11266 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011267
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011268 { "shdr", "res.hdr", PAT_MATCH_STR },
11269 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11270 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11271 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11272 { "shdr_end", "res.hdr", PAT_MATCH_END },
11273 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11274 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11275 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011276
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011277 { "url", "url", PAT_MATCH_STR },
11278 { "url_beg", "url", PAT_MATCH_BEG },
11279 { "url_dir", "url", PAT_MATCH_DIR },
11280 { "url_dom", "url", PAT_MATCH_DOM },
11281 { "url_end", "url", PAT_MATCH_END },
11282 { "url_len", "url", PAT_MATCH_LEN },
11283 { "url_reg", "url", PAT_MATCH_REG },
11284 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011285
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011286 { "urlp", "urlp", PAT_MATCH_STR },
11287 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11288 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11289 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11290 { "urlp_end", "urlp", PAT_MATCH_END },
11291 { "urlp_len", "urlp", PAT_MATCH_LEN },
11292 { "urlp_reg", "urlp", PAT_MATCH_REG },
11293 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011294
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011295 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011296}};
11297
11298/************************************************************************/
11299/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011300/************************************************************************/
11301/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011302static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011303 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011304 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11305 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11306
William Lallemanda43ba4e2014-01-28 18:14:25 +010011307 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011308 { "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 +020011309
11310 /* retrieve these captures from the HTTP logs */
11311 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11312 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11313 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11314
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011315 { "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 +020011316 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011317
Willy Tarreau409bcde2013-01-08 00:31:00 +010011318 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11319 * are only here to match the ACL's name, are request-only and are used
11320 * for ACL compatibility only.
11321 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011322 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11323 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011324 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11325 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11326
11327 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11328 * only here to match the ACL's name, are request-only and are used for
11329 * ACL compatibility only.
11330 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011331 { "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 +010011332 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11333 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11334 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11335
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011336 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011337 { "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 +010011338 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011339 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011340 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011341
11342 /* HTTP protocol on the request path */
11343 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011344 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011345
11346 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011347 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11348 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011349
11350 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011351 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11352 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011353
Willy Tarreau18ed2562013-01-14 15:56:36 +010011354 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011355 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011356 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11357 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11358
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011359 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011360 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011361 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011362 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11363 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11364 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11365
11366 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011367 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011368 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11369 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11370
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011371 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011372 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011373 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011374 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11375 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11376 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11377
Willy Tarreau409bcde2013-01-08 00:31:00 +010011378 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011379 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011380 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11381 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011382 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011383
11384 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011385 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011386 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11387 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11388 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11389
11390 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011391 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011392 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11393 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011394 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11395 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011396 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11397 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011398 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11399 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011400}};
11401
Willy Tarreau8797c062007-05-07 00:55:35 +020011402
Willy Tarreau276fae92013-07-25 14:36:01 +020011403/* Note: must not be declared <const> as its list will be overwritten */
11404static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011405 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11406 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011407 { NULL, NULL, 0, 0, 0 },
11408}};
11409
Willy Tarreau8797c062007-05-07 00:55:35 +020011410__attribute__((constructor))
11411static void __http_protocol_init(void)
11412{
11413 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011414 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011415 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011416}
11417
11418
Willy Tarreau58f10d72006-12-04 02:26:12 +010011419/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011420 * Local variables:
11421 * c-indent-level: 8
11422 * c-basic-offset: 8
11423 * End:
11424 */