blob: 1432fe393687e77d8cd462fd82a45aa72e9a61fa [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 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 Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/fd.h>
46#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010047#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/session.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020053#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/task.h>
55
Willy Tarreau6d1a9882007-01-07 02:03:04 +010056#ifdef CONFIG_HAP_TCPSPLICE
57#include <libtcpsplice.h>
58#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau58f10d72006-12-04 02:26:12 +010060#define DEBUG_PARSE_NO_SPEEDUP
61#undef DEBUG_PARSE_NO_SPEEDUP
62
Willy Tarreau976f1ee2006-12-17 10:06:03 +010063/* This is used to perform a quick jump as an alternative to a break/continue
64 * instruction. The first argument is the label for normal operation, and the
65 * second one is the break/continue instruction in the no_speedup mode.
66 */
67
68#ifdef DEBUG_PARSE_NO_SPEEDUP
69#define QUICK_JUMP(x,y) y
70#else
71#define QUICK_JUMP(x,y) goto x
72#endif
73
Willy Tarreau1c47f852006-07-09 08:22:27 +020074/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010075const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020076 "HTTP/1.0 200 OK\r\n"
77 "Cache-Control: no-cache\r\n"
78 "Connection: close\r\n"
79 "Content-Type: text/html\r\n"
80 "\r\n"
81 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const struct chunk http_200_chunk = {
84 .str = (char *)&HTTP_200,
85 .len = sizeof(HTTP_200)-1
86};
87
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088const char *HTTP_301 =
89 "HTTP/1.0 301 Moved Permantenly\r\n"
90 "Cache-Control: no-cache\r\n"
91 "Connection: close\r\n"
92 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Willy Tarreau0f772532006-12-23 20:51:41 +010094const char *HTTP_302 =
95 "HTTP/1.0 302 Found\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
98 "Location: "; /* not terminated since it will be concatenated with the URL */
99
100/* same as 302 except that the browser MUST retry with the GET method */
101const char *HTTP_303 =
102 "HTTP/1.0 303 See Other\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Connection: close\r\n"
105 "Location: "; /* not terminated since it will be concatenated with the URL */
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
108const char *HTTP_401_fmt =
109 "HTTP/1.0 401 Unauthorized\r\n"
110 "Cache-Control: no-cache\r\n"
111 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200112 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
114 "\r\n"
115 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
116
Willy Tarreau0f772532006-12-23 20:51:41 +0100117
118const int http_err_codes[HTTP_ERR_SIZE] = {
119 [HTTP_ERR_400] = 400,
120 [HTTP_ERR_403] = 403,
121 [HTTP_ERR_408] = 408,
122 [HTTP_ERR_500] = 500,
123 [HTTP_ERR_502] = 502,
124 [HTTP_ERR_503] = 503,
125 [HTTP_ERR_504] = 504,
126};
127
Willy Tarreau80587432006-12-24 17:47:20 +0100128static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100129 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100130 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "\r\n"
135 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
136
137 [HTTP_ERR_403] =
138 "HTTP/1.0 403 Forbidden\r\n"
139 "Cache-Control: no-cache\r\n"
140 "Connection: close\r\n"
141 "Content-Type: text/html\r\n"
142 "\r\n"
143 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
144
145 [HTTP_ERR_408] =
146 "HTTP/1.0 408 Request Time-out\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
152
153 [HTTP_ERR_500] =
154 "HTTP/1.0 500 Server Error\r\n"
155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
160
161 [HTTP_ERR_502] =
162 "HTTP/1.0 502 Bad Gateway\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
168
169 [HTTP_ERR_503] =
170 "HTTP/1.0 503 Service Unavailable\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
176
177 [HTTP_ERR_504] =
178 "HTTP/1.0 504 Gateway Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
184
185};
186
Willy Tarreau80587432006-12-24 17:47:20 +0100187/* We must put the messages here since GCC cannot initialize consts depending
188 * on strlen().
189 */
190struct chunk http_err_chunks[HTTP_ERR_SIZE];
191
Willy Tarreau42250582007-04-01 01:30:43 +0200192#define FD_SETS_ARE_BITFIELDS
193#ifdef FD_SETS_ARE_BITFIELDS
194/*
195 * This map is used with all the FD_* macros to check whether a particular bit
196 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
197 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
198 * byte should be encoded. Be careful to always pass bytes from 0 to 255
199 * exclusively to the macros.
200 */
201fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
203
204#else
205#error "Check if your OS uses bitfields for fd_sets"
206#endif
207
Willy Tarreau80587432006-12-24 17:47:20 +0100208void init_proto_http()
209{
Willy Tarreau42250582007-04-01 01:30:43 +0200210 int i;
211 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100212 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200213
Willy Tarreau80587432006-12-24 17:47:20 +0100214 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
215 if (!http_err_msgs[msg]) {
216 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
217 abort();
218 }
219
220 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
221 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
222 }
Willy Tarreau42250582007-04-01 01:30:43 +0200223
224 /* initialize the log header encoding map : '{|}"#' should be encoded with
225 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
226 * URL encoding only requires '"', '#' to be encoded as well as non-
227 * printable characters above.
228 */
229 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
230 memset(url_encode_map, 0, sizeof(url_encode_map));
231 for (i = 0; i < 32; i++) {
232 FD_SET(i, hdr_encode_map);
233 FD_SET(i, url_encode_map);
234 }
235 for (i = 127; i < 256; i++) {
236 FD_SET(i, hdr_encode_map);
237 FD_SET(i, url_encode_map);
238 }
239
240 tmp = "\"#{|}";
241 while (*tmp) {
242 FD_SET(*tmp, hdr_encode_map);
243 tmp++;
244 }
245
246 tmp = "\"#";
247 while (*tmp) {
248 FD_SET(*tmp, url_encode_map);
249 tmp++;
250 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200251
252 /* memory allocations */
253 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200254 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100255}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256
Willy Tarreau53b6c742006-12-17 13:37:46 +0100257/*
258 * We have 26 list of methods (1 per first letter), each of which can have
259 * up to 3 entries (2 valid, 1 null).
260 */
261struct http_method_desc {
262 http_meth_t meth;
263 int len;
264 const char text[8];
265};
266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100267const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100268 ['C' - 'A'] = {
269 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
270 },
271 ['D' - 'A'] = {
272 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
273 },
274 ['G' - 'A'] = {
275 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
276 },
277 ['H' - 'A'] = {
278 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
279 },
280 ['P' - 'A'] = {
281 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
282 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
283 },
284 ['T' - 'A'] = {
285 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
286 },
287 /* rest is empty like this :
288 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
289 */
290};
291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100292/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200293 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100294 * RFC2616 for those chars.
295 */
296
297const char http_is_spht[256] = {
298 [' '] = 1, ['\t'] = 1,
299};
300
301const char http_is_crlf[256] = {
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_lws[256] = {
306 [' '] = 1, ['\t'] = 1,
307 ['\r'] = 1, ['\n'] = 1,
308};
309
310const char http_is_sep[256] = {
311 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
312 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
313 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
314 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
315 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
316};
317
318const char http_is_ctl[256] = {
319 [0 ... 31] = 1,
320 [127] = 1,
321};
322
323/*
324 * A token is any ASCII char that is neither a separator nor a CTL char.
325 * Do not overwrite values in assignment since gcc-2.95 will not handle
326 * them correctly. Instead, define every non-CTL char's status.
327 */
328const char http_is_token[256] = {
329 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
330 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
331 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
332 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
333 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
334 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
335 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
336 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
337 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
338 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
339 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
340 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
341 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
342 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
343 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
344 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
345 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
346 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
347 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
348 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
349 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
350 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
351 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
352 ['|'] = 1, ['}'] = 0, ['~'] = 1,
353};
354
355
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100356/*
357 * An http ver_token is any ASCII which can be found in an HTTP version,
358 * which includes 'H', 'T', 'P', '/', '.' and any digit.
359 */
360const char http_is_ver_token[256] = {
361 ['.'] = 1, ['/'] = 1,
362 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
363 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
364 ['H'] = 1, ['P'] = 1, ['T'] = 1,
365};
366
367
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200369static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau42250582007-04-01 01:30:43 +0200372static void http_sess_log(struct session *s);
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
375 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
376 * CRLF. Text length is measured first, so it cannot be NULL.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text)
383{
384 int bytes, len;
385
386 len = strlen(text);
387 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
388 if (!bytes)
389 return -1;
390 msg->eoh += bytes;
391 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
392}
393
394/*
395 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
396 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
397 * the buffer is only opened and the space reserved, but nothing is copied.
398 * The header is also automatically added to the index <hdr_idx>, and the end
399 * of headers is automatically adjusted. The number of bytes added is returned
400 * on success, otherwise <0 is returned indicating an error.
401 */
402int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
403 struct hdr_idx *hdr_idx, const char *text, int len)
404{
405 int bytes;
406
407 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
408 if (!bytes)
409 return -1;
410 msg->eoh += bytes;
411 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
414/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100415 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
416 * If so, returns the position of the first non-space character relative to
417 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
418 * to return a pointer to the place after the first space. Returns 0 if the
419 * header name does not match. Checks are case-insensitive.
420 */
421int http_header_match2(const char *hdr, const char *end,
422 const char *name, int len)
423{
424 const char *val;
425
426 if (hdr + len >= end)
427 return 0;
428 if (hdr[len] != ':')
429 return 0;
430 if (strncasecmp(hdr, name, len) != 0)
431 return 0;
432 val = hdr + len + 1;
433 while (val < end && HTTP_IS_SPHT(*val))
434 val++;
435 if ((val >= end) && (len + 2 <= end - hdr))
436 return len + 2; /* we may replace starting from second space */
437 return val - hdr;
438}
439
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440/* Find the end of the header value contained between <s> and <e>.
441 * See RFC2616, par 2.2 for more information. Note that it requires
442 * a valid header to return a valid result.
443 */
444const char *find_hdr_value_end(const char *s, const char *e)
445{
446 int quoted, qdpair;
447
448 quoted = qdpair = 0;
449 for (; s < e; s++) {
450 if (qdpair) qdpair = 0;
451 else if (quoted && *s == '\\') qdpair = 1;
452 else if (quoted && *s == '"') quoted = 0;
453 else if (*s == '"') quoted = 1;
454 else if (*s == ',') return s;
455 }
456 return s;
457}
458
459/* Find the first or next occurrence of header <name> in message buffer <sol>
460 * using headers index <idx>, and return it in the <ctx> structure. This
461 * structure holds everything necessary to use the header and find next
462 * occurrence. If its <idx> member is 0, the header is searched from the
463 * beginning. Otherwise, the next occurrence is returned. The function returns
464 * 1 when it finds a value, and 0 when there is no more.
465 */
466int http_find_header2(const char *name, int len,
467 const char *sol, struct hdr_idx *idx,
468 struct hdr_ctx *ctx)
469{
470 __label__ return_hdr, next_hdr;
471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200540/* This function shuts down the buffers on the server side, and sets indicators
541 * accordingly. The server's fd is supposed to already be closed. Note that if
542 * <status> is 0, or if the message pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
544void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100545 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
Willy Tarreau3da77c52008-08-29 09:58:42 +0200547 buffer_write_ena(t->rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +0200548 buffer_shutw(t->req);
549 buffer_shutr(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100550 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100551 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100552 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100553 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554 }
555 if (!(t->flags & SN_ERR_MASK))
556 t->flags |= err;
557 if (!(t->flags & SN_FINST_MASK))
558 t->flags |= finst;
559}
560
Willy Tarreau80587432006-12-24 17:47:20 +0100561/* This function returns the appropriate error location for the given session
562 * and message.
563 */
564
565struct chunk *error_message(struct session *s, int msgnum)
566{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200567 if (s->be->errmsg[msgnum].str)
568 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100569 else if (s->fe->errmsg[msgnum].str)
570 return &s->fe->errmsg[msgnum];
571 else
572 return &http_err_chunks[msgnum];
573}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574
Willy Tarreau53b6c742006-12-17 13:37:46 +0100575/*
576 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
577 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
578 */
579static http_meth_t find_http_meth(const char *str, const int len)
580{
581 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100582 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100583
584 m = ((unsigned)*str - 'A');
585
586 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100587 for (h = http_methods[m]; h->len > 0; h++) {
588 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100589 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100590 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100591 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 };
593 return HTTP_METH_OTHER;
594 }
595 return HTTP_METH_NONE;
596
597}
598
Willy Tarreau21d2af32008-02-14 20:25:24 +0100599/* Parse the URI from the given transaction (which is assumed to be in request
600 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
601 * It is returned otherwise.
602 */
603static char *
604http_get_path(struct http_txn *txn)
605{
606 char *ptr, *end;
607
608 ptr = txn->req.sol + txn->req.sl.rq.u;
609 end = ptr + txn->req.sl.rq.u_l;
610
611 if (ptr >= end)
612 return NULL;
613
614 /* RFC2616, par. 5.1.2 :
615 * Request-URI = "*" | absuri | abspath | authority
616 */
617
618 if (*ptr == '*')
619 return NULL;
620
621 if (isalpha((unsigned char)*ptr)) {
622 /* this is a scheme as described by RFC3986, par. 3.1 */
623 ptr++;
624 while (ptr < end &&
625 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
626 ptr++;
627 /* skip '://' */
628 if (ptr == end || *ptr++ != ':')
629 return NULL;
630 if (ptr == end || *ptr++ != '/')
631 return NULL;
632 if (ptr == end || *ptr++ != '/')
633 return NULL;
634 }
635 /* skip [user[:passwd]@]host[:[port]] */
636
637 while (ptr < end && *ptr != '/')
638 ptr++;
639
640 if (ptr == end)
641 return NULL;
642
643 /* OK, we got the '/' ! */
644 return ptr;
645}
646
Willy Tarreaudafde432008-08-17 01:00:46 +0200647/* Processes the client, server, request and response jobs of a session task,
648 * then puts it back to the wait queue in a clean state, or cleans up its
649 * resources if it must be deleted. Returns in <next> the date the task wants
650 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
651 * nothing too many times, the request and response buffers flags are monitored
652 * and each function is called only if at least another function has changed at
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200653 * least one flag it is interested in.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200655void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656{
657 struct session *s = t->context;
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200658 int resync;
659 unsigned int rqf_cli, rpf_cli;
660 unsigned int rqf_srv, rpf_srv;
661 unsigned int rqf_req, rpf_rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200663 /* check server-side errors during data phase */
664 if (s->req->cons->state == SI_ST_EST) {
665 stream_sock_data_check_errors(s->req->cons->fd);
666 /* When a server-side connection is released, we have to
667 * count it and check for pending connections on this server.
668 */
669 if (unlikely(s->req->cons->state == SI_ST_CLO)) {
670 /* Count server-side errors (but not timeouts). */
671 if (s->req->flags & BF_WRITE_ERROR) {
672 s->be->failed_resp++;
673 if (s->srv)
674 s->srv->failed_resp++;
675 }
676
677 if (s->srv) {
678 s->srv->cur_sess--;
679 sess_change_server(s, NULL);
680 if (may_dequeue_tasks(s->srv, s->be))
681 process_srv_queue(s->srv);
682 }
683 }
684 }
685
686 /* check client-side errors during data phase */
687 if (s->rep->cons->state == SI_ST_EST)
688 stream_sock_data_check_errors(s->rep->cons->fd);
689
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200690 /* force one first pass everywhere */
691 rqf_cli = rqf_srv = rqf_req = ~s->req->flags;
692 rpf_cli = rpf_srv = rpf_rep = ~s->rep->flags;
Willy Tarreau507385d2008-08-17 13:04:25 +0200693
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694 do {
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200695 resync = 0;
Willy Tarreaudafde432008-08-17 01:00:46 +0200696
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200697 if (s->rep->cons->state != SI_ST_CLO) {
698 if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) ||
699 ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) {
700 resync = 1;
701 stream_sock_data_update(s->rep->cons->fd);
702 rqf_cli = s->req->flags;
703 rpf_cli = s->rep->flags;
704
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200705 if (unlikely((s->rep->cons->state == SI_ST_CLO) &&
706 (global.mode & MODE_DEBUG) &&
707 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
708 int len;
709 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200710 s->uniq_id, s->be->id, (unsigned short)s->rep->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200711 write(1, trash, len);
712 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200713 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200714 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200715
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200716
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200717 if (s->req->cons->state != SI_ST_CLO) {
718 if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) ||
719 ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) {
720 resync = 1;
721
Willy Tarreau3da77c52008-08-29 09:58:42 +0200722 if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_WRITE_ENA)
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200723 process_srv_conn(s);
724
725 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreau3da77c52008-08-29 09:58:42 +0200726 if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
Willy Tarreau376580a2008-08-27 18:52:22 +0200727 s->be->options & PR_O_FORCE_CLO &&
Willy Tarreau3da77c52008-08-29 09:58:42 +0200728 s->rep->flags & BF_READ_ACTIVITY) {
Willy Tarreau376580a2008-08-27 18:52:22 +0200729 /* We want to force the connection to the server to close,
730 * and the server has begun to respond. That's the right
731 * time.
732 */
733 buffer_shutw_now(s->req);
734 }
735
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200736 stream_sock_data_update(s->req->cons->fd);
Willy Tarreau376580a2008-08-27 18:52:22 +0200737
738 /* When a server-side connection is released, we have to
739 * count it and check for pending connections on this server.
740 */
741 if (s->req->cons->state == SI_ST_CLO) {
742 if (s->srv) {
743 s->srv->cur_sess--;
744 sess_change_server(s, NULL);
745 if (may_dequeue_tasks(s->srv, s->be))
746 process_srv_queue(s->srv);
747 }
748 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200749 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200750 rqf_srv = s->req->flags;
751 rpf_srv = s->rep->flags;
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200752
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200753 if (unlikely((s->req->cons->state == SI_ST_CLO) &&
754 (global.mode & MODE_DEBUG) &&
755 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
756 int len;
757 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200758 s->uniq_id, s->be->id, (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200759 write(1, trash, len);
760 }
761 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200762 }
763
764 if ((rqf_req ^ s->req->flags) & BF_MASK_ANALYSER) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200765 /* the analysers must block it themselves */
Willy Tarreau36e6a412008-08-28 11:07:58 +0200766 if (s->req->prod->state >= SI_ST_EST) {
767 resync = 1;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200768 buffer_write_ena(s->req);
Willy Tarreau36e6a412008-08-28 11:07:58 +0200769 if (s->req->analysers)
770 process_request(s);
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200771 }
772 rqf_req = s->req->flags;
773 }
774
Willy Tarreau72b179a2008-08-28 16:01:32 +0200775 if (unlikely(s->rep->flags & BF_HIJACK)) {
776 /* In inject mode, we wake up everytime something has
777 * happened on the write side of the buffer.
778 */
Willy Tarreau3da77c52008-08-29 09:58:42 +0200779 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
Willy Tarreau72b179a2008-08-28 16:01:32 +0200780 !(s->rep->flags & BF_FULL)) {
781 if (produce_content(s) != 0)
782 resync = 1; /* completed, better re-check flags */
783 }
784 }
785 else if (s->rep->prod->state >= SI_ST_EST) {
786 if ((rpf_rep ^ s->rep->flags) & BF_MASK_ANALYSER) {
787 /* the analysers must block it themselves */
Willy Tarreau36e6a412008-08-28 11:07:58 +0200788 resync = 1;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200789 buffer_write_ena(s->rep);
Willy Tarreau36e6a412008-08-28 11:07:58 +0200790 if (s->rep->analysers)
791 process_response(s);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200792 rpf_rep = s->rep->flags;
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200793 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200794 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200795
Willy Tarreaudafde432008-08-17 01:00:46 +0200796 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200798 if (likely((s->rep->cons->state != SI_ST_CLO) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200799 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100800
801 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
802 session_process_counters(s);
803
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200804 if (s->rep->cons->state == SI_ST_EST)
805 stream_sock_data_finish(s->rep->cons->fd);
806
807 if (s->req->cons->state == SI_ST_EST)
808 stream_sock_data_finish(s->req->cons->fd);
809
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200810 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
811 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812
Willy Tarreau7f875f62008-08-11 17:35:01 +0200813 /* Trick: if a request is being waiting for the server to respond,
814 * and if we know the server can timeout, we don't want the timeout
815 * to expire on the client side first, but we're still interested
816 * in passing data from the client to the server (eg: POST). Thus,
817 * we can cancel the client's request timeout if the server's
818 * request timeout is set and the server has not yet sent a response.
819 */
820
Willy Tarreau3da77c52008-08-29 09:58:42 +0200821 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200822 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200823 s->req->rex = TICK_ETERNITY;
824
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200825 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
826 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +0200827 if (s->req->analysers)
828 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829
830 /* restore t to its place in the task list */
831 task_queue(t);
832
Willy Tarreaua7c52762008-08-16 18:40:18 +0200833#ifdef DEBUG_DEV
834 /* this may only happen when no timeout is set or in case of an FSM bug */
835 if (!t->expire)
836 ABORT_NOW();
837#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200838 *next = t->expire;
839 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200840 }
841
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100842 s->fe->feconn--;
843 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200844 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200845 actconn--;
846
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200847 if (unlikely((global.mode & MODE_DEBUG) &&
848 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200850 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200851 s->uniq_id, s->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200852 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200853 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200854 write(1, trash, len);
855 }
856
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200857 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100858 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200859
860 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200861 if (s->logs.logwait &&
862 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200863 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
864 if (s->fe->to_log & LW_REQ)
865 http_sess_log(s);
866 else
867 tcp_sess_log(s);
868 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200869
870 /* the task MUST not be in the run queue anymore */
871 task_delete(t);
872 session_free(s);
873 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200874 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200875}
876
877
Willy Tarreau42250582007-04-01 01:30:43 +0200878extern const char sess_term_cond[8];
879extern const char sess_fin_state[8];
880extern const char *monthname[12];
881const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
882const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
883 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
884 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200885struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200886struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100887
Willy Tarreau42250582007-04-01 01:30:43 +0200888/*
889 * send a log for the session when we have enough info about it.
890 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100891 */
Willy Tarreau42250582007-04-01 01:30:43 +0200892static void http_sess_log(struct session *s)
893{
894 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
895 struct proxy *fe = s->fe;
896 struct proxy *be = s->be;
897 struct proxy *prx_log;
898 struct http_txn *txn = &s->txn;
899 int tolog;
900 char *uri, *h;
901 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200902 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200903 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200904 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200905 int hdr;
906
907 if (fe->logfac1 < 0 && fe->logfac2 < 0)
908 return;
909 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100910
Willy Tarreau42250582007-04-01 01:30:43 +0200911 if (s->cli_addr.ss_family == AF_INET)
912 inet_ntop(AF_INET,
913 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
914 pn, sizeof(pn));
915 else
916 inet_ntop(AF_INET6,
917 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
918 pn, sizeof(pn));
919
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200920 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200921
922 /* FIXME: let's limit ourselves to frontend logging for now. */
923 tolog = fe->to_log;
924
925 h = tmpline;
926 if (fe->to_log & LW_REQHDR &&
927 txn->req.cap &&
928 (h < tmpline + sizeof(tmpline) - 10)) {
929 *(h++) = ' ';
930 *(h++) = '{';
931 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
932 if (hdr)
933 *(h++) = '|';
934 if (txn->req.cap[hdr] != NULL)
935 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
936 '#', hdr_encode_map, txn->req.cap[hdr]);
937 }
938 *(h++) = '}';
939 }
940
941 if (fe->to_log & LW_RSPHDR &&
942 txn->rsp.cap &&
943 (h < tmpline + sizeof(tmpline) - 7)) {
944 *(h++) = ' ';
945 *(h++) = '{';
946 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
947 if (hdr)
948 *(h++) = '|';
949 if (txn->rsp.cap[hdr] != NULL)
950 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
951 '#', hdr_encode_map, txn->rsp.cap[hdr]);
952 }
953 *(h++) = '}';
954 }
955
956 if (h < tmpline + sizeof(tmpline) - 4) {
957 *(h++) = ' ';
958 *(h++) = '"';
959 uri = txn->uri ? txn->uri : "<BADREQ>";
960 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
961 '#', url_encode_map, uri);
962 *(h++) = '"';
963 }
964 *h = '\0';
965
966 svid = (tolog & LW_SVID) ?
967 (s->data_source != DATA_SRC_STATS) ?
968 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
969
Willy Tarreau70089872008-06-13 21:12:51 +0200970 t_request = -1;
971 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
972 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
973
Willy Tarreau42250582007-04-01 01:30:43 +0200974 send_log(prx_log, LOG_INFO,
975 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
976 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100977 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200978 pn,
979 (s->cli_addr.ss_family == AF_INET) ?
980 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
981 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200982 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200983 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200984 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200985 t_request,
986 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200987 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
988 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
989 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
990 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100991 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200992 txn->cli_cookie ? txn->cli_cookie : "-",
993 txn->srv_cookie ? txn->srv_cookie : "-",
994 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
995 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
996 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
997 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
998 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100999 (s->flags & SN_REDISP)?"+":"",
1000 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001001 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1002
1003 s->logs.logwait = 0;
1004}
1005
Willy Tarreau117f59e2007-03-04 18:17:17 +01001006
1007/*
1008 * Capture headers from message starting at <som> according to header list
1009 * <cap_hdr>, and fill the <idx> structure appropriately.
1010 */
1011void capture_headers(char *som, struct hdr_idx *idx,
1012 char **cap, struct cap_hdr *cap_hdr)
1013{
1014 char *eol, *sol, *col, *sov;
1015 int cur_idx;
1016 struct cap_hdr *h;
1017 int len;
1018
1019 sol = som + hdr_idx_first_pos(idx);
1020 cur_idx = hdr_idx_first_idx(idx);
1021
1022 while (cur_idx) {
1023 eol = sol + idx->v[cur_idx].len;
1024
1025 col = sol;
1026 while (col < eol && *col != ':')
1027 col++;
1028
1029 sov = col + 1;
1030 while (sov < eol && http_is_lws[(unsigned char)*sov])
1031 sov++;
1032
1033 for (h = cap_hdr; h; h = h->next) {
1034 if ((h->namelen == col - sol) &&
1035 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1036 if (cap[h->index] == NULL)
1037 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001038 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001039
1040 if (cap[h->index] == NULL) {
1041 Alert("HTTP capture : out of memory.\n");
1042 continue;
1043 }
1044
1045 len = eol - sov;
1046 if (len > h->len)
1047 len = h->len;
1048
1049 memcpy(cap[h->index], sov, len);
1050 cap[h->index][len]=0;
1051 }
1052 }
1053 sol = eol + idx->v[cur_idx].cr + 1;
1054 cur_idx = idx->v[cur_idx].next;
1055 }
1056}
1057
1058
Willy Tarreau42250582007-04-01 01:30:43 +02001059/* either we find an LF at <ptr> or we jump to <bad>.
1060 */
1061#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1062
1063/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1064 * otherwise to <http_msg_ood> with <state> set to <st>.
1065 */
1066#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1067 ptr++; \
1068 if (likely(ptr < end)) \
1069 goto good; \
1070 else { \
1071 state = (st); \
1072 goto http_msg_ood; \
1073 } \
1074 } while (0)
1075
1076
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001078 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001079 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1080 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1081 * will give undefined results.
1082 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1083 * and that msg->sol points to the beginning of the response.
1084 * If a complete line is found (which implies that at least one CR or LF is
1085 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1086 * returned indicating an incomplete line (which does not mean that parts have
1087 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1088 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1089 * upon next call.
1090 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001091 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001092 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1093 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001094 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001095 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001096const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1097 unsigned int state, const char *ptr, const char *end,
1098 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001099{
1100 __label__
1101 http_msg_rpver,
1102 http_msg_rpver_sp,
1103 http_msg_rpcode,
1104 http_msg_rpcode_sp,
1105 http_msg_rpreason,
1106 http_msg_rpline_eol,
1107 http_msg_ood, /* out of data */
1108 http_msg_invalid;
1109
1110 switch (state) {
1111 http_msg_rpver:
1112 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001113 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001114 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1115
1116 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001117 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1119 }
1120 goto http_msg_invalid;
1121
1122 http_msg_rpver_sp:
1123 case HTTP_MSG_RPVER_SP:
1124 if (likely(!HTTP_IS_LWS(*ptr))) {
1125 msg->sl.st.c = ptr - msg_buf;
1126 goto http_msg_rpcode;
1127 }
1128 if (likely(HTTP_IS_SPHT(*ptr)))
1129 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1130 /* so it's a CR/LF, this is invalid */
1131 goto http_msg_invalid;
1132
1133 http_msg_rpcode:
1134 case HTTP_MSG_RPCODE:
1135 if (likely(!HTTP_IS_LWS(*ptr)))
1136 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1137
1138 if (likely(HTTP_IS_SPHT(*ptr))) {
1139 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1140 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1141 }
1142
1143 /* so it's a CR/LF, so there is no reason phrase */
1144 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1145 http_msg_rsp_reason:
1146 /* FIXME: should we support HTTP responses without any reason phrase ? */
1147 msg->sl.st.r = ptr - msg_buf;
1148 msg->sl.st.r_l = 0;
1149 goto http_msg_rpline_eol;
1150
1151 http_msg_rpcode_sp:
1152 case HTTP_MSG_RPCODE_SP:
1153 if (likely(!HTTP_IS_LWS(*ptr))) {
1154 msg->sl.st.r = ptr - msg_buf;
1155 goto http_msg_rpreason;
1156 }
1157 if (likely(HTTP_IS_SPHT(*ptr)))
1158 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1159 /* so it's a CR/LF, so there is no reason phrase */
1160 goto http_msg_rsp_reason;
1161
1162 http_msg_rpreason:
1163 case HTTP_MSG_RPREASON:
1164 if (likely(!HTTP_IS_CRLF(*ptr)))
1165 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1166 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1167 http_msg_rpline_eol:
1168 /* We have seen the end of line. Note that we do not
1169 * necessarily have the \n yet, but at least we know that we
1170 * have EITHER \r OR \n, otherwise the response would not be
1171 * complete. We can then record the response length and return
1172 * to the caller which will be able to register it.
1173 */
1174 msg->sl.st.l = ptr - msg->sol;
1175 return ptr;
1176
1177#ifdef DEBUG_FULL
1178 default:
1179 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1180 exit(1);
1181#endif
1182 }
1183
1184 http_msg_ood:
1185 /* out of data */
1186 if (ret_state)
1187 *ret_state = state;
1188 if (ret_ptr)
1189 *ret_ptr = (char *)ptr;
1190 return NULL;
1191
1192 http_msg_invalid:
1193 /* invalid message */
1194 if (ret_state)
1195 *ret_state = HTTP_MSG_ERROR;
1196 return NULL;
1197}
1198
1199
1200/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001201 * This function parses a request line between <ptr> and <end>, starting with
1202 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1203 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1204 * will give undefined results.
1205 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1206 * and that msg->sol points to the beginning of the request.
1207 * If a complete line is found (which implies that at least one CR or LF is
1208 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1209 * returned indicating an incomplete line (which does not mean that parts have
1210 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1211 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1212 * upon next call.
1213 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001214 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001215 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1216 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001217 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001218 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001219const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1220 unsigned int state, const char *ptr, const char *end,
1221 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001222{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 __label__
1224 http_msg_rqmeth,
1225 http_msg_rqmeth_sp,
1226 http_msg_rquri,
1227 http_msg_rquri_sp,
1228 http_msg_rqver,
1229 http_msg_rqline_eol,
1230 http_msg_ood, /* out of data */
1231 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233 switch (state) {
1234 http_msg_rqmeth:
1235 case HTTP_MSG_RQMETH:
1236 if (likely(HTTP_IS_TOKEN(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001238
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001240 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1242 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001243
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 if (likely(HTTP_IS_CRLF(*ptr))) {
1245 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001246 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 http_msg_req09_uri:
1248 msg->sl.rq.u = ptr - msg_buf;
1249 http_msg_req09_uri_e:
1250 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1251 http_msg_req09_ver:
1252 msg->sl.rq.v = ptr - msg_buf;
1253 msg->sl.rq.v_l = 0;
1254 goto http_msg_rqline_eol;
1255 }
1256 goto http_msg_invalid;
1257
1258 http_msg_rqmeth_sp:
1259 case HTTP_MSG_RQMETH_SP:
1260 if (likely(!HTTP_IS_LWS(*ptr))) {
1261 msg->sl.rq.u = ptr - msg_buf;
1262 goto http_msg_rquri;
1263 }
1264 if (likely(HTTP_IS_SPHT(*ptr)))
1265 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1266 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1267 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 http_msg_rquri:
1270 case HTTP_MSG_RQURI:
1271 if (likely(!HTTP_IS_LWS(*ptr)))
1272 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 if (likely(HTTP_IS_SPHT(*ptr))) {
1275 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1276 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1277 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001278
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1280 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001281
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001282 http_msg_rquri_sp:
1283 case HTTP_MSG_RQURI_SP:
1284 if (likely(!HTTP_IS_LWS(*ptr))) {
1285 msg->sl.rq.v = ptr - msg_buf;
1286 goto http_msg_rqver;
1287 }
1288 if (likely(HTTP_IS_SPHT(*ptr)))
1289 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1290 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1291 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001292
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 http_msg_rqver:
1294 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001295 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001297
1298 if (likely(HTTP_IS_CRLF(*ptr))) {
1299 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1300 http_msg_rqline_eol:
1301 /* We have seen the end of line. Note that we do not
1302 * necessarily have the \n yet, but at least we know that we
1303 * have EITHER \r OR \n, otherwise the request would not be
1304 * complete. We can then record the request length and return
1305 * to the caller which will be able to register it.
1306 */
1307 msg->sl.rq.l = ptr - msg->sol;
1308 return ptr;
1309 }
1310
1311 /* neither an HTTP_VER token nor a CRLF */
1312 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001313
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314#ifdef DEBUG_FULL
1315 default:
1316 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1317 exit(1);
1318#endif
1319 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 http_msg_ood:
1322 /* out of data */
1323 if (ret_state)
1324 *ret_state = state;
1325 if (ret_ptr)
1326 *ret_ptr = (char *)ptr;
1327 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001328
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001329 http_msg_invalid:
1330 /* invalid message */
1331 if (ret_state)
1332 *ret_state = HTTP_MSG_ERROR;
1333 return NULL;
1334}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001335
1336
Willy Tarreau8973c702007-01-21 23:58:29 +01001337/*
1338 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001339 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 * when data are missing and recalled at the exact same location with no
1341 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001342 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1343 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001344 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1346{
1347 __label__
1348 http_msg_rqbefore,
1349 http_msg_rqbefore_cr,
1350 http_msg_rqmeth,
1351 http_msg_rqline_end,
1352 http_msg_hdr_first,
1353 http_msg_hdr_name,
1354 http_msg_hdr_l1_sp,
1355 http_msg_hdr_l1_lf,
1356 http_msg_hdr_l1_lws,
1357 http_msg_hdr_val,
1358 http_msg_hdr_l2_lf,
1359 http_msg_hdr_l2_lws,
1360 http_msg_complete_header,
1361 http_msg_last_lf,
1362 http_msg_ood, /* out of data */
1363 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001364
Willy Tarreaue69eada2008-01-27 00:34:10 +01001365 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001368 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 ptr = buf->lr;
1370 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001371
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (unlikely(ptr >= end))
1373 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001374
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001376 /*
1377 * First, states that are specific to the response only.
1378 * We check them first so that request and headers are
1379 * closer to each other (accessed more often).
1380 */
1381 http_msg_rpbefore:
1382 case HTTP_MSG_RPBEFORE:
1383 if (likely(HTTP_IS_TOKEN(*ptr))) {
1384 if (likely(ptr == buf->data)) {
1385 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001386 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001387 } else {
1388#if PARSE_PRESERVE_EMPTY_LINES
1389 /* only skip empty leading lines, don't remove them */
1390 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001391 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001392#else
1393 /* Remove empty leading lines, as recommended by
1394 * RFC2616. This takes a lot of time because we
1395 * must move all the buffer backwards, but this
1396 * is rarely needed. The method above will be
1397 * cleaner when we'll be able to start sending
1398 * the request from any place in the buffer.
1399 */
1400 buf->lr = ptr;
1401 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001402 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001403 msg->sol = buf->data;
1404 ptr = buf->data;
1405 end = buf->r;
1406#endif
1407 }
1408 hdr_idx_init(idx);
1409 state = HTTP_MSG_RPVER;
1410 goto http_msg_rpver;
1411 }
1412
1413 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1414 goto http_msg_invalid;
1415
1416 if (unlikely(*ptr == '\n'))
1417 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1418 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1419 /* stop here */
1420
1421 http_msg_rpbefore_cr:
1422 case HTTP_MSG_RPBEFORE_CR:
1423 EXPECT_LF_HERE(ptr, http_msg_invalid);
1424 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1425 /* stop here */
1426
1427 http_msg_rpver:
1428 case HTTP_MSG_RPVER:
1429 case HTTP_MSG_RPVER_SP:
1430 case HTTP_MSG_RPCODE:
1431 case HTTP_MSG_RPCODE_SP:
1432 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001433 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001434 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 if (unlikely(!ptr))
1436 return;
1437
1438 /* we have a full response and we know that we have either a CR
1439 * or an LF at <ptr>.
1440 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001441 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001442 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1443
1444 msg->sol = ptr;
1445 if (likely(*ptr == '\r'))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1447 goto http_msg_rpline_end;
1448
1449 http_msg_rpline_end:
1450 case HTTP_MSG_RPLINE_END:
1451 /* msg->sol must point to the first of CR or LF. */
1452 EXPECT_LF_HERE(ptr, http_msg_invalid);
1453 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1454 /* stop here */
1455
1456 /*
1457 * Second, states that are specific to the request only
1458 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 http_msg_rqbefore:
1460 case HTTP_MSG_RQBEFORE:
1461 if (likely(HTTP_IS_TOKEN(*ptr))) {
1462 if (likely(ptr == buf->data)) {
1463 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001464 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 } else {
1466#if PARSE_PRESERVE_EMPTY_LINES
1467 /* only skip empty leading lines, don't remove them */
1468 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001469 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001470#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 /* Remove empty leading lines, as recommended by
1472 * RFC2616. This takes a lot of time because we
1473 * must move all the buffer backwards, but this
1474 * is rarely needed. The method above will be
1475 * cleaner when we'll be able to start sending
1476 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001477 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 buf->lr = ptr;
1479 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001480 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 msg->sol = buf->data;
1482 ptr = buf->data;
1483 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001484#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001486 /* we will need this when keep-alive will be supported
1487 hdr_idx_init(idx);
1488 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001489 state = HTTP_MSG_RQMETH;
1490 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1494 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 if (unlikely(*ptr == '\n'))
1497 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1498 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 http_msg_rqbefore_cr:
1502 case HTTP_MSG_RQBEFORE_CR:
1503 EXPECT_LF_HERE(ptr, http_msg_invalid);
1504 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001506
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 http_msg_rqmeth:
1508 case HTTP_MSG_RQMETH:
1509 case HTTP_MSG_RQMETH_SP:
1510 case HTTP_MSG_RQURI:
1511 case HTTP_MSG_RQURI_SP:
1512 case HTTP_MSG_RQVER:
1513 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001514 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 if (unlikely(!ptr))
1516 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 /* we have a full request and we know that we have either a CR
1519 * or an LF at <ptr>.
1520 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001521 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 msg->sol = ptr;
1525 if (likely(*ptr == '\r'))
1526 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 http_msg_rqline_end:
1530 case HTTP_MSG_RQLINE_END:
1531 /* check for HTTP/0.9 request : no version information available.
1532 * msg->sol must point to the first of CR or LF.
1533 */
1534 if (unlikely(msg->sl.rq.v_l == 0))
1535 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 EXPECT_LF_HERE(ptr, http_msg_invalid);
1538 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001539 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001540
Willy Tarreau8973c702007-01-21 23:58:29 +01001541 /*
1542 * Common states below
1543 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001544 http_msg_hdr_first:
1545 case HTTP_MSG_HDR_FIRST:
1546 msg->sol = ptr;
1547 if (likely(!HTTP_IS_CRLF(*ptr))) {
1548 goto http_msg_hdr_name;
1549 }
1550
1551 if (likely(*ptr == '\r'))
1552 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1553 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001554
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 http_msg_hdr_name:
1556 case HTTP_MSG_HDR_NAME:
1557 /* assumes msg->sol points to the first char */
1558 if (likely(HTTP_IS_TOKEN(*ptr)))
1559 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001560
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 if (likely(*ptr == ':')) {
1562 msg->col = ptr - buf->data;
1563 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1564 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 http_msg_hdr_l1_sp:
1569 case HTTP_MSG_HDR_L1_SP:
1570 /* assumes msg->sol points to the first char and msg->col to the colon */
1571 if (likely(HTTP_IS_SPHT(*ptr)))
1572 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001573
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 /* header value can be basically anything except CR/LF */
1575 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001576
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 if (likely(!HTTP_IS_CRLF(*ptr))) {
1578 goto http_msg_hdr_val;
1579 }
1580
1581 if (likely(*ptr == '\r'))
1582 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1583 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001584
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 http_msg_hdr_l1_lf:
1586 case HTTP_MSG_HDR_L1_LF:
1587 EXPECT_LF_HERE(ptr, http_msg_invalid);
1588 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001589
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 http_msg_hdr_l1_lws:
1591 case HTTP_MSG_HDR_L1_LWS:
1592 if (likely(HTTP_IS_SPHT(*ptr))) {
1593 /* replace HT,CR,LF with spaces */
1594 for (; buf->data+msg->sov < ptr; msg->sov++)
1595 buf->data[msg->sov] = ' ';
1596 goto http_msg_hdr_l1_sp;
1597 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001598 /* we had a header consisting only in spaces ! */
1599 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 goto http_msg_complete_header;
1601
1602 http_msg_hdr_val:
1603 case HTTP_MSG_HDR_VAL:
1604 /* assumes msg->sol points to the first char, msg->col to the
1605 * colon, and msg->sov points to the first character of the
1606 * value.
1607 */
1608 if (likely(!HTTP_IS_CRLF(*ptr)))
1609 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 msg->eol = ptr;
1612 /* Note: we could also copy eol into ->eoh so that we have the
1613 * real header end in case it ends with lots of LWS, but is this
1614 * really needed ?
1615 */
1616 if (likely(*ptr == '\r'))
1617 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1618 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 http_msg_hdr_l2_lf:
1621 case HTTP_MSG_HDR_L2_LF:
1622 EXPECT_LF_HERE(ptr, http_msg_invalid);
1623 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 http_msg_hdr_l2_lws:
1626 case HTTP_MSG_HDR_L2_LWS:
1627 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1628 /* LWS: replace HT,CR,LF with spaces */
1629 for (; msg->eol < ptr; msg->eol++)
1630 *msg->eol = ' ';
1631 goto http_msg_hdr_val;
1632 }
1633 http_msg_complete_header:
1634 /*
1635 * It was a new header, so the last one is finished.
1636 * Assumes msg->sol points to the first char, msg->col to the
1637 * colon, msg->sov points to the first character of the value
1638 * and msg->eol to the first CR or LF so we know how the line
1639 * ends. We insert last header into the index.
1640 */
1641 /*
1642 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1643 write(2, msg->sol, msg->eol-msg->sol);
1644 fprintf(stderr,"\n");
1645 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1648 idx, idx->tail) < 0))
1649 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 msg->sol = ptr;
1652 if (likely(!HTTP_IS_CRLF(*ptr))) {
1653 goto http_msg_hdr_name;
1654 }
1655
1656 if (likely(*ptr == '\r'))
1657 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1658 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 http_msg_last_lf:
1661 case HTTP_MSG_LAST_LF:
1662 /* Assumes msg->sol points to the first of either CR or LF */
1663 EXPECT_LF_HERE(ptr, http_msg_invalid);
1664 ptr++;
1665 buf->lr = ptr;
1666 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001667 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 return;
1669#ifdef DEBUG_FULL
1670 default:
1671 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1672 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001673#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 }
1675 http_msg_ood:
1676 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001677 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 buf->lr = ptr;
1679 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001680
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 http_msg_invalid:
1682 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001683 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 return;
1685}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001686
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001687/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001688 * It normally returns zero, but may return 1 if it absolutely needs to be
1689 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001690 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001691 * functions. Its behaviour is rather simple :
1692 * - all enabled analysers are called in turn from the lower to the higher
1693 * bit.
1694 * - if an analyser does not have enough data, it must return without calling
Willy Tarreau3da77c52008-08-29 09:58:42 +02001695 * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001696 * that unprocessed data will not be forwarded. But that probably depends on
1697 * the protocol. Generally it is not reset in case of errors.
1698 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreau3da77c52008-08-29 09:58:42 +02001699 * analyser without touching BF_WRITE_ENA (it is enabled prior to
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001700 * analysis).
1701 * - if an analyser thinks it has no added value anymore staying here, it must
1702 * reset its bit from the analysers flags in order not to be called anymore.
1703 *
1704 * In the future, analysers should be able to indicate that they want to be
1705 * called after XXX bytes have been received (or transfered), and the min of
1706 * all's wishes will be used to ring back (unless a special condition occurs).
1707 *
1708 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001710int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 struct buffer *req = t->req;
1713 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001714
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001715 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001716 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001717 t,
1718 req,
1719 req->rex, req->wex,
1720 req->flags,
1721 req->l,
1722 req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001723
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001724 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001725 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001726 struct tcp_rule *rule;
1727 int partial;
1728
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001729 /* We will abort if we encounter a read error. In theory, we
1730 * should not abort if we get a close, it might be valid,
1731 * although very unlikely. FIXME: we'll abort for now, this
1732 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001733 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001734 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001735 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001736 //t->fe->failed_req++;
Willy Tarreaub6866442008-07-14 23:54:42 +02001737 if (!(t->flags & SN_ERR_MASK))
1738 t->flags |= SN_ERR_CLICL;
1739 if (!(t->flags & SN_FINST_MASK))
1740 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001741 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001742 }
1743
1744 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001745 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001746 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001747 t->fe->failed_req++;
1748 if (!(t->flags & SN_ERR_MASK))
1749 t->flags |= SN_ERR_CLITO;
1750 if (!(t->flags & SN_FINST_MASK))
1751 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001752 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001753 }
1754
1755 /* We don't know whether we have enough data, so must proceed
1756 * this way :
1757 * - iterate through all rules in their declaration order
1758 * - if one rule returns MISS, it means the inspect delay is
1759 * not over yet, then return immediately, otherwise consider
1760 * it as a non-match.
1761 * - if one rule returns OK, then return OK
1762 * - if one rule returns KO, then return KO
1763 */
1764
Willy Tarreauffab5b42008-08-17 18:03:28 +02001765 if (req->flags & (BF_READ_NULL | BF_SHUTR) || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001766 partial = 0;
1767 else
1768 partial = ACL_PARTIAL;
1769
1770 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1771 int ret = ACL_PAT_PASS;
1772
1773 if (rule->cond) {
1774 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1775 if (ret == ACL_PAT_MISS) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02001776 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001777 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001778 if (!tick_isset(req->analyse_exp))
1779 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001780 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001781 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001782
Willy Tarreaub6866442008-07-14 23:54:42 +02001783 ret = acl_pass(ret);
1784 if (rule->cond->pol == ACL_COND_UNLESS)
1785 ret = !ret;
1786 }
1787
1788 if (ret) {
1789 /* we have a matching rule. */
1790 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001791 buffer_abort(req);
1792 buffer_abort(rep);
1793 //FIXME: this delete this
1794 //fd_delete(t->cli_fd);
1795 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001796 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001797 t->fe->failed_req++;
1798 if (!(t->flags & SN_ERR_MASK))
1799 t->flags |= SN_ERR_PRXCOND;
1800 if (!(t->flags & SN_FINST_MASK))
1801 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001802 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001803 }
1804 /* otherwise accept */
1805 break;
1806 }
1807 }
1808
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001809 /* if we get there, it means we have no rule which matches, or
1810 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001811 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001812 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001813 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001814 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001815
Willy Tarreau2df28e82008-08-17 15:20:19 +02001816 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 /*
1818 * Now parse the partial (or complete) lines.
1819 * We will check the request syntax, and also join multi-line
1820 * headers. An index of all the lines will be elaborated while
1821 * parsing.
1822 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001823 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001826 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 * req->data + req->eoh = end of processed headers / start of current one
1828 * req->data + req->eol = end of current header or line (LF or CRLF)
1829 * req->lr = first non-visited byte
1830 * req->r = end of data
1831 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001832
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001834 struct http_txn *txn = &t->txn;
1835 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001839 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001840
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 /* 1: we might have to print this header in debug mode */
1842 if (unlikely((global.mode & MODE_DEBUG) &&
1843 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001844 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001846
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001847 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001848 eol = sol + msg->sl.rq.l;
1849 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001850
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001851 sol += hdr_idx_first_pos(&txn->hdr_idx);
1852 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001853
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001855 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001857 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1858 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001859 }
1860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001861
Willy Tarreau58f10d72006-12-04 02:26:12 +01001862
1863 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001865 * If not so, we check the FD and buffer states before leaving.
1866 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1868 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001869 *
1870 */
1871
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001872 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 /*
1874 * First, let's catch bad requests.
1875 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001876 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001878
1879 /* 1: Since we are in header mode, if there's no space
1880 * left for headers, we won't be able to free more
1881 * later, so the session will never terminate. We
1882 * must terminate it now.
1883 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001884 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001885 /* FIXME: check if URI is set and return Status
1886 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001887 */
Willy Tarreau06619262006-12-17 08:37:22 +01001888 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001889 }
1890
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001891 /* 2: have we encountered a read error ? */
1892 else if (req->flags & BF_READ_ERROR) {
1893 /* we cannot return any message on error */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001894 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001895 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001896 //t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001897 if (!(t->flags & SN_ERR_MASK))
1898 t->flags |= SN_ERR_CLICL;
1899 if (!(t->flags & SN_FINST_MASK))
1900 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001901 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001902 }
1903
1904 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001905 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001906 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001907 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001908 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001909 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001910 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001911 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001912 if (!(t->flags & SN_ERR_MASK))
1913 t->flags |= SN_ERR_CLITO;
1914 if (!(t->flags & SN_FINST_MASK))
1915 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001916 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001917 }
1918
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001919 /* 4: have we encountered a close ? */
1920 else if (req->flags & (BF_READ_NULL | BF_SHUTR)) {
1921 txn->status = 400;
1922 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001923 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001924 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001925 t->fe->failed_req++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001926
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001927 if (!(t->flags & SN_ERR_MASK))
1928 t->flags |= SN_ERR_CLICL;
1929 if (!(t->flags & SN_FINST_MASK))
1930 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001931 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001932 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001933
Willy Tarreau3da77c52008-08-29 09:58:42 +02001934 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001935 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001936 if (!tick_isset(req->analyse_exp))
1937 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001938
1939 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001940 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001941 }
1942
1943
1944 /****************************************************************
1945 * More interesting part now : we know that we have a complete *
1946 * request which at least looks like HTTP. We have an indicator *
1947 * of each header's length, so we can parse them quickly. *
1948 ****************************************************************/
1949
Willy Tarreau2df28e82008-08-17 15:20:19 +02001950 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001951 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001952
Willy Tarreau9cdde232007-05-02 20:58:19 +02001953 /* ensure we keep this pointer to the beginning of the message */
1954 msg->sol = req->data + msg->som;
1955
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956 /*
1957 * 1: identify the method
1958 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001959 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001960
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001961 /* we can make use of server redirect on GET and HEAD */
1962 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1963 t->flags |= SN_REDIRECTABLE;
1964
Willy Tarreau58f10d72006-12-04 02:26:12 +01001965 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001967 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001968 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001969 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001970 if (unlikely((t->fe->monitor_uri_len != 0) &&
1971 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1972 !memcmp(&req->data[msg->sl.rq.u],
1973 t->fe->monitor_uri,
1974 t->fe->monitor_uri_len))) {
1975 /*
1976 * We have found the monitor URI
1977 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001978 struct acl_cond *cond;
1979 cur_proxy = t->fe;
1980
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001981 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001982
1983 /* Check if we want to fail this monitor request or not */
1984 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1985 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001986
1987 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001988 if (cond->pol == ACL_COND_UNLESS)
1989 ret = !ret;
1990
1991 if (ret) {
1992 /* we fail this request, let's return 503 service unavail */
1993 txn->status = 503;
1994 client_retnclose(t, error_message(t, HTTP_ERR_503));
1995 goto return_prx_cond;
1996 }
1997 }
1998
1999 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002000 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001 client_retnclose(t, &http_200_chunk);
2002 goto return_prx_cond;
2003 }
2004
2005 /*
2006 * 3: Maybe we have to copy the original REQURI for the logs ?
2007 * Note: we cannot log anymore if the request has been
2008 * classified as invalid.
2009 */
2010 if (unlikely(t->logs.logwait & LW_REQ)) {
2011 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002012 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002013 int urilen = msg->sl.rq.l;
2014
2015 if (urilen >= REQURI_LEN)
2016 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002017 memcpy(txn->uri, &req->data[msg->som], urilen);
2018 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002019
2020 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02002021 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002022 } else {
2023 Alert("HTTP logging : out of memory.\n");
2024 }
2025 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002026
Willy Tarreau06619262006-12-17 08:37:22 +01002027
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002028 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2029 if (unlikely(msg->sl.rq.v_l == 0)) {
2030 int delta;
2031 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002032 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002033 cur_end = msg->sol + msg->sl.rq.l;
2034 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01002035
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002036 if (msg->sl.rq.u_l == 0) {
2037 /* if no URI was set, add "/" */
2038 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2039 cur_end += delta;
2040 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002041 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002042 /* add HTTP version */
2043 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2044 msg->eoh += delta;
2045 cur_end += delta;
2046 cur_end = (char *)http_parse_reqline(msg, req->data,
2047 HTTP_MSG_RQMETH,
2048 msg->sol, cur_end + 1,
2049 NULL, NULL);
2050 if (unlikely(!cur_end))
2051 goto return_bad_req;
2052
2053 /* we have a full HTTP/1.0 request now and we know that
2054 * we have either a CR or an LF at <ptr>.
2055 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002056 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002057 }
2058
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002059
2060 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002061 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002062 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002063 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002064
2065 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002066 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002067 * As opposed to version 1.2, now they will be evaluated in the
2068 * filters order and not in the header order. This means that
2069 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002070 *
2071 * We can now check whether we want to switch to another
2072 * backend, in which case we will re-check the backend's
2073 * filters and various options. In order to support 3-level
2074 * switching, here's how we should proceed :
2075 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002076 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002077 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002078 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002079 * There cannot be any switch from there, so ->be cannot be
2080 * changed anymore.
2081 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002082 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002083 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002084 * The response path will be able to apply either ->be, or
2085 * ->be then ->fe filters in order to match the reverse of
2086 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002087 */
2088
Willy Tarreau06619262006-12-17 08:37:22 +01002089 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002090 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002091 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002092 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002093 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002094
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002095 /* first check whether we have some ACLs set to redirect this request */
2096 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2097 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002098
2099 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002100 if (rule->cond->pol == ACL_COND_UNLESS)
2101 ret = !ret;
2102
2103 if (ret) {
2104 struct chunk rdr = { trash, 0 };
2105 const char *msg_fmt;
2106
2107 /* build redirect message */
2108 switch(rule->code) {
2109 case 303:
2110 rdr.len = strlen(HTTP_303);
2111 msg_fmt = HTTP_303;
2112 break;
2113 case 301:
2114 rdr.len = strlen(HTTP_301);
2115 msg_fmt = HTTP_301;
2116 break;
2117 case 302:
2118 default:
2119 rdr.len = strlen(HTTP_302);
2120 msg_fmt = HTTP_302;
2121 break;
2122 }
2123
2124 if (unlikely(rdr.len > sizeof(trash)))
2125 goto return_bad_req;
2126 memcpy(rdr.str, msg_fmt, rdr.len);
2127
2128 switch(rule->type) {
2129 case REDIRECT_TYPE_PREFIX: {
2130 const char *path;
2131 int pathlen;
2132
2133 path = http_get_path(txn);
2134 /* build message using path */
2135 if (path) {
2136 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2137 } else {
2138 path = "/";
2139 pathlen = 1;
2140 }
2141
2142 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2143 goto return_bad_req;
2144
2145 /* add prefix */
2146 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2147 rdr.len += rule->rdr_len;
2148
2149 /* add path */
2150 memcpy(rdr.str + rdr.len, path, pathlen);
2151 rdr.len += pathlen;
2152 break;
2153 }
2154 case REDIRECT_TYPE_LOCATION:
2155 default:
2156 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2157 goto return_bad_req;
2158
2159 /* add location */
2160 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2161 rdr.len += rule->rdr_len;
2162 break;
2163 }
2164
2165 /* add end of headers */
2166 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2167 rdr.len += 4;
2168
2169 txn->status = rule->code;
2170 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002171 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002172 client_retnclose(t, &rdr);
2173 goto return_prx_cond;
2174 }
2175 }
2176
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002177 /* first check whether we have some ACLs set to block this request */
2178 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002179 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002180
2181 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002182 if (cond->pol == ACL_COND_UNLESS)
2183 ret = !ret;
2184
2185 if (ret) {
2186 txn->status = 403;
2187 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002188 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002189 client_retnclose(t, error_message(t, HTTP_ERR_403));
2190 goto return_prx_cond;
2191 }
2192 }
2193
Willy Tarreau06619262006-12-17 08:37:22 +01002194 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002195 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002196 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2197 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002198 }
2199
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002200 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2201 /* to ensure correct connection accounting on
2202 * the backend, we count the connection for the
2203 * one managing the queue.
2204 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002205 t->be->beconn++;
2206 if (t->be->beconn > t->be->beconn_max)
2207 t->be->beconn_max = t->be->beconn;
2208 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002209 t->flags |= SN_BE_ASSIGNED;
2210 }
2211
Willy Tarreau06619262006-12-17 08:37:22 +01002212 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002213 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002214 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002215 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002216 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002217 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002218 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002219 goto return_prx_cond;
2220 }
2221
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002222 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002223 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002224 !(t->flags & SN_CONN_CLOSED)) {
2225 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002226 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002227 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002228
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002229 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002230 old_idx = 0;
2231
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002232 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2233 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002234 cur_ptr = cur_next;
2235 cur_end = cur_ptr + cur_hdr->len;
2236 cur_next = cur_end + cur_hdr->cr + 1;
2237
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002238 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2239 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002240 /* 3 possibilities :
2241 * - we have already set Connection: close,
2242 * so we remove this line.
2243 * - we have not yet set Connection: close,
2244 * but this line indicates close. We leave
2245 * it untouched and set the flag.
2246 * - we have not yet set Connection: close,
2247 * and this line indicates non-close. We
2248 * replace it.
2249 */
2250 if (t->flags & SN_CONN_CLOSED) {
2251 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002252 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002253 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002254 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2255 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002256 cur_hdr->len = 0;
2257 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002258 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2259 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2260 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002261 cur_next += delta;
2262 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002263 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002264 }
2265 t->flags |= SN_CONN_CLOSED;
2266 }
2267 }
2268 old_idx = cur_idx;
2269 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002270 }
2271 /* add request headers from the rule sets in the same order */
2272 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2273 if (unlikely(http_header_add_tail(req,
2274 &txn->req,
2275 &txn->hdr_idx,
2276 rule_set->req_add[cur_idx])) < 0)
2277 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002278 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002279
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002280 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002281 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002282 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002283 /* we have to check the URI and auth for this request.
2284 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002285 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002286 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002287 if (stats_check_uri_auth(t, rule_set))
2288 return 1;
2289 }
2290
Willy Tarreau55ea7572007-06-17 19:56:27 +02002291 /* now check whether we have some switching rules for this request */
2292 if (!(t->flags & SN_BE_ASSIGNED)) {
2293 struct switching_rule *rule;
2294
2295 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2296 int ret;
2297
2298 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002299
2300 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002301 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002302 ret = !ret;
2303
2304 if (ret) {
2305 t->be = rule->be.backend;
2306 t->be->beconn++;
2307 if (t->be->beconn > t->be->beconn_max)
2308 t->be->beconn_max = t->be->beconn;
2309 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002310
2311 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002312 t->rep->rto = t->req->wto = t->be->timeout.server;
2313 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002314 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002315 t->flags |= SN_BE_ASSIGNED;
2316 break;
2317 }
2318 }
2319 }
2320
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002321 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2322 /* No backend was set, but there was a default
2323 * backend set in the frontend, so we use it and
2324 * loop again.
2325 */
2326 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002327 t->be->beconn++;
2328 if (t->be->beconn > t->be->beconn_max)
2329 t->be->beconn_max = t->be->beconn;
2330 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002331
2332 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002333 t->rep->rto = t->req->wto = t->be->timeout.server;
2334 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002335 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002336 t->flags |= SN_BE_ASSIGNED;
2337 }
2338 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002339
Willy Tarreau58f10d72006-12-04 02:26:12 +01002340
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002341 if (!(t->flags & SN_BE_ASSIGNED)) {
2342 /* To ensure correct connection accounting on
2343 * the backend, we count the connection for the
2344 * one managing the queue.
2345 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002346 t->be->beconn++;
2347 if (t->be->beconn > t->be->beconn_max)
2348 t->be->beconn_max = t->be->beconn;
2349 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002350 t->flags |= SN_BE_ASSIGNED;
2351 }
2352
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002353 /*
2354 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002355 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002356 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002357 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002358 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002359
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002360 /*
2361 * If HTTP PROXY is set we simply get remote server address
2362 * parsing incoming request.
2363 */
2364 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2365 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2366 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002367
Willy Tarreau2a324282006-12-05 00:05:46 +01002368 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002369 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002370 * so let's do the same now.
2371 */
2372
2373 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002374 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002375 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002376 }
2377
2378
2379 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002380 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002381 * Note that doing so might move headers in the request, but
2382 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002383 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002384 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002385 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2386 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002387 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002388
Willy Tarreau58f10d72006-12-04 02:26:12 +01002389
Willy Tarreau2a324282006-12-05 00:05:46 +01002390 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002391 * 9: add X-Forwarded-For if either the frontend or the backend
2392 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002393 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002394 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002395 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002396 /* Add an X-Forwarded-For header unless the source IP is
2397 * in the 'except' network range.
2398 */
2399 if ((!t->fe->except_mask.s_addr ||
2400 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2401 != t->fe->except_net.s_addr) &&
2402 (!t->be->except_mask.s_addr ||
2403 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2404 != t->be->except_net.s_addr)) {
2405 int len;
2406 unsigned char *pn;
2407 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002408
Ross Westaf72a1d2008-08-03 10:51:45 +02002409 /* Note: we rely on the backend to get the header name to be used for
2410 * x-forwarded-for, because the header is really meant for the backends.
2411 * However, if the backend did not specify any option, we have to rely
2412 * on the frontend's header name.
2413 */
2414 if (t->be->fwdfor_hdr_len) {
2415 len = t->be->fwdfor_hdr_len;
2416 memcpy(trash, t->be->fwdfor_hdr_name, len);
2417 } else {
2418 len = t->fe->fwdfor_hdr_len;
2419 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2420 }
2421 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002422
Ross Westaf72a1d2008-08-03 10:51:45 +02002423 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002424 &txn->hdr_idx, trash, len)) < 0)
2425 goto return_bad_req;
2426 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002427 }
2428 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002429 /* FIXME: for the sake of completeness, we should also support
2430 * 'except' here, although it is mostly useless in this case.
2431 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002432 int len;
2433 char pn[INET6_ADDRSTRLEN];
2434 inet_ntop(AF_INET6,
2435 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2436 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002437
2438 /* Note: we rely on the backend to get the header name to be used for
2439 * x-forwarded-for, because the header is really meant for the backends.
2440 * However, if the backend did not specify any option, we have to rely
2441 * on the frontend's header name.
2442 */
2443 if (t->be->fwdfor_hdr_len) {
2444 len = t->be->fwdfor_hdr_len;
2445 memcpy(trash, t->be->fwdfor_hdr_name, len);
2446 } else {
2447 len = t->fe->fwdfor_hdr_len;
2448 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2449 }
2450 len += sprintf(trash + len, ": %s", pn);
2451
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002452 if (unlikely(http_header_add_tail2(req, &txn->req,
2453 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002454 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002455 }
2456 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002457
Willy Tarreau2a324282006-12-05 00:05:46 +01002458 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002459 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002460 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002461 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002462 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002463 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002464 if ((unlikely(msg->sl.rq.v_l != 8) ||
2465 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2466 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002467 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002468 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002469 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002470 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002471 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2472 * If not assigned, perhaps we are balancing on url_param, but this is a
2473 * POST; and the parameters are in the body, maybe scan there to find our server.
2474 * (unless headers overflowed the buffer?)
2475 */
2476 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2477 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002478 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002479 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2480 /* are there enough bytes here? total == l || r || rlim ?
2481 * len is unsigned, but eoh is int,
2482 * how many bytes of body have we received?
2483 * eoh is the first empty line of the header
2484 */
2485 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002486 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002487
2488 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2489 * We can't assume responsibility for the server's decision,
2490 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2491 * We also can't change our mind later, about which server to choose, so round robin.
2492 */
2493 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2494 struct hdr_ctx ctx;
2495 ctx.idx = 0;
2496 /* Expect is allowed in 1.1, look for it */
2497 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2498 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002499 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002500 /* We can't reliablly stall and wait for data, because of
2501 * .NET clients that don't conform to rfc2616; so, no need for
2502 * the next block to check length expectations.
2503 * We could send 100 status back to the client, but then we need to
2504 * re-write headers, and send the message. And this isn't the right
2505 * place for that action.
2506 * TODO: support Expect elsewhere and delete this block.
2507 */
2508 goto end_check_maybe_wait_for_body;
2509 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002510
2511 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002512 /* nothing to do, we got enough */
2513 } else {
2514 /* limit implies we are supposed to need this many bytes
2515 * to find the parameter. Let's see how many bytes we can wait for.
2516 */
2517 long long hint = len;
2518 struct hdr_ctx ctx;
2519 ctx.idx = 0;
2520 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002521 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002522 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002523 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002524 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002525 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002526 ctx.idx = 0;
2527 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2528 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002529 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002530 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002531 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002532 hint = 0; /* parse failure, untrusted client */
2533 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002534 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002535 msg->hdr_content_len = hint;
2536 else
2537 hint = 0; /* bad client, sent negative length */
2538 }
2539 }
2540 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002541 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002542 hint = t->be->url_param_post_limit;
2543 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002544 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002545 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002546 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002547 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002548 /* else... There are no body bytes to wait for */
2549 }
2550 }
2551 }
2552 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002553
Willy Tarreau2a324282006-12-05 00:05:46 +01002554 /*************************************************************
2555 * OK, that's finished for the headers. We have done what we *
2556 * could. Let's switch to the DATA state. *
2557 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002558
Willy Tarreaue393fe22008-08-16 22:18:07 +02002559 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002560 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002561
Willy Tarreau1fa31262007-12-03 00:36:16 +01002562 /* When a connection is tarpitted, we use the tarpit timeout,
2563 * which may be the same as the connect timeout if unspecified.
2564 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002565 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01002566 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002567 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002568 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002569 /* flush the request so that we can drop the connection early
2570 * if the client closes first.
2571 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002572 buffer_write_dis(req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002573 req->analysers |= AN_REQ_HTTP_TARPIT;
2574 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2575 if (!req->analyse_exp)
2576 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002577 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002579 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002580 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002581
2582 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002583 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002584 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002585 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002586 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002587 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002588 return_prx_cond:
2589 if (!(t->flags & SN_ERR_MASK))
2590 t->flags |= SN_ERR_PRXCOND;
2591 if (!(t->flags & SN_FINST_MASK))
2592 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002593 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002594 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002595 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002596 }
2597
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002598 if (req->analysers & AN_REQ_HTTP_TARPIT) {
2599 struct http_txn *txn = &t->txn;
2600
2601 /* This connection is being tarpitted. The CLIENT side has
2602 * already set the connect expiration date to the right
2603 * timeout. We just have to check that the client is still
2604 * there and that the timeout has not expired.
2605 */
2606 if ((req->flags & (BF_READ_NULL|BF_READ_ERROR)) == 0 &&
2607 !tick_is_expired(req->analyse_exp, now_ms))
2608 return 0;
2609
2610 /* We will set the queue timer to the time spent, just for
2611 * logging purposes. We fake a 500 server error, so that the
2612 * attacker will not suspect his connection has been tarpitted.
2613 * It will not cause trouble to the logs because we can exclude
2614 * the tarpitted connections by filtering on the 'PT' status flags.
2615 */
2616 trace_term(t, TT_HTTP_SRV_2);
2617 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
2618
2619 txn->status = 500;
2620 if (req->flags != BF_READ_ERROR)
2621 client_retnclose(t, error_message(t, HTTP_ERR_500));
2622
2623 req->analysers = 0;
2624 req->analyse_exp = TICK_ETERNITY;
2625
2626 t->fe->failed_req++;
2627 if (!(t->flags & SN_ERR_MASK))
2628 t->flags |= SN_ERR_PRXCOND;
2629 if (!(t->flags & SN_FINST_MASK))
2630 t->flags |= SN_FINST_T;
2631 return 0;
2632 }
2633
Willy Tarreau2df28e82008-08-17 15:20:19 +02002634 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002635 /* We have to parse the HTTP request body to find any required data.
2636 * "balance url_param check_post" should have been the only way to get
2637 * into this. We were brought here after HTTP header analysis, so all
2638 * related structures are ready.
2639 */
2640 struct http_msg *msg = &t->txn.req;
2641 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2642 long long limit = t->be->url_param_post_limit;
2643 struct hdr_ctx ctx;
2644
2645 ctx.idx = 0;
2646
2647 /* now if we have a length, we'll take the hint */
2648 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2649 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2650 unsigned int chunk = 0;
2651 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2652 char c = msg->sol[body];
2653 if (ishex(c)) {
2654 unsigned int hex = toupper(c) - '0';
2655 if (hex > 9)
2656 hex -= 'A' - '9' - 1;
2657 chunk = (chunk << 4) | hex;
2658 } else
2659 break;
2660 body++;
2661 }
2662 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002663 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002664
2665 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002666 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002667
2668 body += 2; // skip CRLF
2669
2670 /* if we support more then one chunk here, we have to do it again when assigning server
2671 * 1. how much entity data do we have? new var
2672 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2673 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2674 */
2675
2676 if (chunk < limit)
2677 limit = chunk; /* only reading one chunk */
2678 } else {
2679 if (msg->hdr_content_len < limit)
2680 limit = msg->hdr_content_len;
2681 }
2682
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002683 http_body_end:
2684 /* we leave once we know we have nothing left to do. This means that we have
2685 * enough bytes, or that we know we'll not get any more (buffer full, read
2686 * buffer closed).
2687 */
2688 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002689 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_NULL | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02002690 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002691 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002692 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002693 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002694 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002695 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02002696 else {
2697 /* Not enough data. We'll re-use the http-request
2698 * timeout here. Ideally, we should set the timeout
2699 * relative to the accept() date. We just set the
2700 * request timeout once at the beginning of the
2701 * request.
2702 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002703 buffer_write_dis(req);
Willy Tarreauc52164a2008-08-17 19:17:57 +02002704 if (!tick_isset(req->analyse_exp))
2705 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
2706 return 0;
2707 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002708 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002709
Willy Tarreau2df28e82008-08-17 15:20:19 +02002710 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2711 * probably reduce one day's debugging session.
2712 */
2713#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002714 if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY)) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02002715 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2716 __FILE__, __LINE__, req->analysers);
2717 ABORT_NOW();
2718 }
2719#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002720 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02002721 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002722}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002723
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002724/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002725 * It normally returns zero, but may return 1 if it absolutely needs to be
2726 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002727 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002728 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002729 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002730int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002731{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002732 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002733 struct buffer *req = t->req;
2734 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002735
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002736 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002737 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002738 t,
2739 rep,
2740 rep->rex, rep->wex,
2741 rep->flags,
2742 rep->l,
2743 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002744
Willy Tarreau2df28e82008-08-17 15:20:19 +02002745 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002746 /*
2747 * Now parse the partial (or complete) lines.
2748 * We will check the response syntax, and also join multi-line
2749 * headers. An index of all the lines will be elaborated while
2750 * parsing.
2751 *
2752 * For the parsing, we use a 28 states FSM.
2753 *
2754 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002755 * rep->data + rep->som = beginning of response
2756 * rep->data + rep->eoh = end of processed headers / start of current one
2757 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002758 * rep->lr = first non-visited byte
2759 * rep->r = end of data
2760 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002761
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002762 int cur_idx;
2763 struct http_msg *msg = &txn->rsp;
2764 struct proxy *cur_proxy;
2765
2766 if (likely(rep->lr < rep->r))
2767 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2768
2769 /* 1: we might have to print this header in debug mode */
2770 if (unlikely((global.mode & MODE_DEBUG) &&
2771 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2772 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2773 char *eol, *sol;
2774
2775 sol = rep->data + msg->som;
2776 eol = sol + msg->sl.rq.l;
2777 debug_hdr("srvrep", t, sol, eol);
2778
2779 sol += hdr_idx_first_pos(&txn->hdr_idx);
2780 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2781
2782 while (cur_idx) {
2783 eol = sol + txn->hdr_idx.v[cur_idx].len;
2784 debug_hdr("srvhdr", t, sol, eol);
2785 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2786 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002787 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002788 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002789
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002790 /*
2791 * Now we quickly check if we have found a full valid response.
2792 * If not so, we check the FD and buffer states before leaving.
2793 * A full response is indicated by the fact that we have seen
2794 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2795 * responses are checked first.
2796 *
2797 * Depending on whether the client is still there or not, we
2798 * may send an error response back or not. Note that normally
2799 * we should only check for HTTP status there, and check I/O
2800 * errors somewhere else.
2801 */
2802
2803 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002804 /* Invalid response */
2805 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2806 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002807 //buffer_shutr(rep);
2808 //buffer_shutw(req);
2809 //fd_delete(req->cons->fd);
2810 //req->cons->state = SI_ST_CLO;
2811 buffer_shutr_now(rep);
2812 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002813 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002814 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002815 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002816 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002817 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002818 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002819 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002820 txn->status = 502;
2821 client_return(t, error_message(t, HTTP_ERR_502));
2822 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002823 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002824 if (!(t->flags & SN_FINST_MASK))
2825 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002826
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002827 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2828 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002829
Willy Tarreaudafde432008-08-17 01:00:46 +02002830 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002831 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002832 /* too large response does not fit in buffer. */
2833 else if (rep->flags & BF_FULL) {
2834 goto hdr_response_bad;
2835 }
2836 /* read error */
2837 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002838 buffer_shutr_now(rep);
2839 buffer_shutw_now(req);
2840 //fd_delete(req->cons->fd);
2841 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002842 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002843 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002844 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002845 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002846 //}
2847 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002848 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002849 txn->status = 502;
2850 client_return(t, error_message(t, HTTP_ERR_502));
2851 if (!(t->flags & SN_ERR_MASK))
2852 t->flags |= SN_ERR_SRVCL;
2853 if (!(t->flags & SN_FINST_MASK))
2854 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002855
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002856 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2857 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002858
Willy Tarreaudafde432008-08-17 01:00:46 +02002859 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002860 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002861 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002862 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002863 buffer_shutr_now(rep);
2864 buffer_shutw_now(req);
2865 //fd_delete(req->cons->fd);
2866 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002867 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002868 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002869 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002870 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002871 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002872 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002873 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002874 txn->status = 504;
2875 client_return(t, error_message(t, HTTP_ERR_504));
2876 if (!(t->flags & SN_ERR_MASK))
2877 t->flags |= SN_ERR_SRVTO;
2878 if (!(t->flags & SN_FINST_MASK))
2879 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002880
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002881 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2882 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002883 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002884 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002885 /* write error to client, or close from server */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002886 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR|BF_READ_NULL)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002887 buffer_shutr_now(rep);
2888 buffer_shutw_now(req);
2889 //fd_delete(req->cons->fd);
2890 //req->cons->state = SI_ST_CLO;
2891 if (t->srv) {
2892 //t->srv->cur_sess--;
2893 t->srv->failed_resp++;
2894 //sess_change_server(t, NULL);
2895 }
2896 t->be->failed_resp++;
2897 rep->analysers = 0;
2898 txn->status = 502;
2899 client_return(t, error_message(t, HTTP_ERR_502));
2900 if (!(t->flags & SN_ERR_MASK))
2901 t->flags |= SN_ERR_SRVCL;
2902 if (!(t->flags & SN_FINST_MASK))
2903 t->flags |= SN_FINST_H;
2904
2905 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2906 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002907
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002908 return 0;
2909 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002910 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002911 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002912 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002913
Willy Tarreau21d2af32008-02-14 20:25:24 +01002914
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002915 /*****************************************************************
2916 * More interesting part now : we know that we have a complete *
2917 * response which at least looks like HTTP. We have an indicator *
2918 * of each header's length, so we can parse them quickly. *
2919 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002920
Willy Tarreau2df28e82008-08-17 15:20:19 +02002921 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002922
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002923 /* ensure we keep this pointer to the beginning of the message */
2924 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002925
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002926 /*
2927 * 1: get the status code and check for cacheability.
2928 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002929
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002930 t->logs.logwait &= ~LW_RESP;
2931 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002932
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002933 switch (txn->status) {
2934 case 200:
2935 case 203:
2936 case 206:
2937 case 300:
2938 case 301:
2939 case 410:
2940 /* RFC2616 @13.4:
2941 * "A response received with a status code of
2942 * 200, 203, 206, 300, 301 or 410 MAY be stored
2943 * by a cache (...) unless a cache-control
2944 * directive prohibits caching."
2945 *
2946 * RFC2616 @9.5: POST method :
2947 * "Responses to this method are not cacheable,
2948 * unless the response includes appropriate
2949 * Cache-Control or Expires header fields."
2950 */
2951 if (likely(txn->meth != HTTP_METH_POST) &&
2952 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2953 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2954 break;
2955 default:
2956 break;
2957 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002958
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002959 /*
2960 * 2: we may need to capture headers
2961 */
2962 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2963 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2964 txn->rsp.cap, t->fe->rsp_cap);
2965
2966 /*
2967 * 3: we will have to evaluate the filters.
2968 * As opposed to version 1.2, now they will be evaluated in the
2969 * filters order and not in the header order. This means that
2970 * each filter has to be validated among all headers.
2971 *
2972 * Filters are tried with ->be first, then with ->fe if it is
2973 * different from ->be.
2974 */
2975
2976 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2977
2978 cur_proxy = t->be;
2979 while (1) {
2980 struct proxy *rule_set = cur_proxy;
2981
2982 /* try headers filters */
2983 if (rule_set->rsp_exp != NULL) {
2984 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2985 return_bad_resp:
2986 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002987 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002988 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002989 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002990 }
2991 cur_proxy->failed_resp++;
2992 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002993 buffer_shutr_now(rep);
2994 buffer_shutw_now(req);
2995 //fd_delete(req->cons->fd);
2996 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002997 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002998 txn->status = 502;
2999 client_return(t, error_message(t, HTTP_ERR_502));
3000 if (!(t->flags & SN_ERR_MASK))
3001 t->flags |= SN_ERR_PRXCOND;
3002 if (!(t->flags & SN_FINST_MASK))
3003 t->flags |= SN_FINST_H;
3004 /* We used to have a free connection slot. Since we'll never use it,
3005 * we have to inform the server that it may be used by another session.
3006 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003007 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
3008 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02003009 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003010 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003011 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003012
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003013 /* has the response been denied ? */
3014 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01003015 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003016 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003017 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003018 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01003019 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003020 cur_proxy->denied_resp++;
3021 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003022 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003023
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003024 /* We might have to check for "Connection:" */
3025 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3026 !(t->flags & SN_CONN_CLOSED)) {
3027 char *cur_ptr, *cur_end, *cur_next;
3028 int cur_idx, old_idx, delta, val;
3029 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003030
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003031 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3032 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003033
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3035 cur_hdr = &txn->hdr_idx.v[cur_idx];
3036 cur_ptr = cur_next;
3037 cur_end = cur_ptr + cur_hdr->len;
3038 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003039
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003040 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3041 if (val) {
3042 /* 3 possibilities :
3043 * - we have already set Connection: close,
3044 * so we remove this line.
3045 * - we have not yet set Connection: close,
3046 * but this line indicates close. We leave
3047 * it untouched and set the flag.
3048 * - we have not yet set Connection: close,
3049 * and this line indicates non-close. We
3050 * replace it.
3051 */
3052 if (t->flags & SN_CONN_CLOSED) {
3053 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3054 txn->rsp.eoh += delta;
3055 cur_next += delta;
3056 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3057 txn->hdr_idx.used--;
3058 cur_hdr->len = 0;
3059 } else {
3060 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3061 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3062 "close", 5);
3063 cur_next += delta;
3064 cur_hdr->len += delta;
3065 txn->rsp.eoh += delta;
3066 }
3067 t->flags |= SN_CONN_CLOSED;
3068 }
3069 }
3070 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003071 }
3072 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003073
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003074 /* add response headers from the rule sets in the same order */
3075 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3076 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3077 rule_set->rsp_add[cur_idx])) < 0)
3078 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003079 }
3080
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003081 /* check whether we're already working on the frontend */
3082 if (cur_proxy == t->fe)
3083 break;
3084 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003085 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003086
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003087 /*
3088 * 4: check for server cookie.
3089 */
3090 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3091 || (t->be->options & PR_O_CHK_CACHE))
3092 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003093
Willy Tarreaubaaee002006-06-26 02:48:02 +02003094
Willy Tarreaua15645d2007-03-18 16:22:39 +01003095 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003096 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003097 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003098 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3099 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003100
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003101 /*
3102 * 6: add server cookie in the response if needed
3103 */
3104 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3105 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3106 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003107
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003108 /* the server is known, it's not the one the client requested, we have to
3109 * insert a set-cookie here, except if we want to insert only on POST
3110 * requests and this one isn't. Note that servers which don't have cookies
3111 * (eg: some backup servers) will return a full cookie removal request.
3112 */
3113 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3114 t->be->cookie_name,
3115 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003116
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003117 if (t->be->cookie_domain)
3118 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003119
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003120 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3121 trash, len)) < 0)
3122 goto return_bad_resp;
3123 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003124
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003125 /* Here, we will tell an eventual cache on the client side that we don't
3126 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3127 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3128 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3129 */
3130 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003131
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003132 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3133
3134 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3135 "Cache-control: private", 22)) < 0)
3136 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003137 }
3138 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003139
Willy Tarreaubaaee002006-06-26 02:48:02 +02003140
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003141 /*
3142 * 7: check if result will be cacheable with a cookie.
3143 * We'll block the response if security checks have caught
3144 * nasty things such as a cacheable cookie.
3145 */
3146 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3147 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3148 (t->be->options & PR_O_CHK_CACHE)) {
3149
3150 /* we're in presence of a cacheable response containing
3151 * a set-cookie header. We'll block it as requested by
3152 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003153 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003154 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003155 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003156 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003157 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003158 }
3159 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003160
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003161 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3162 t->be->id, t->srv?t->srv->id:"<dispatch>");
3163 send_log(t->be, LOG_ALERT,
3164 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3165 t->be->id, t->srv?t->srv->id:"<dispatch>");
3166 goto return_srv_prx_502;
3167 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003168
3169 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003170 * 8: add "Connection: close" if needed and not yet set.
3171 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003172 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003173 if (!(t->flags & SN_CONN_CLOSED) &&
3174 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3175 if ((unlikely(msg->sl.st.v_l != 8) ||
3176 unlikely(req->data[msg->som + 7] != '0')) &&
3177 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3178 "Connection: close", 17)) < 0)
3179 goto return_bad_resp;
3180 t->flags |= SN_CONN_CLOSED;
3181 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003182
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003183 /*************************************************************
3184 * OK, that's finished for the headers. We have done what we *
3185 * could. Let's switch to the DATA state. *
3186 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003187
Willy Tarreaue393fe22008-08-16 22:18:07 +02003188 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003189 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003190
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003191#ifdef CONFIG_HAP_TCPSPLICE
3192 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3193 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003194 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003195 }
3196#endif
3197 /* if the user wants to log as soon as possible, without counting
3198 * bytes from the server, then this is the right moment. We have
3199 * to temporarily assign bytes_out to log what we currently have.
3200 */
3201 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3202 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3203 t->logs.bytes_out = txn->rsp.eoh;
3204 if (t->fe->to_log & LW_REQ)
3205 http_sess_log(t);
3206 else
3207 tcp_sess_log(t);
3208 t->logs.bytes_out = 0;
3209 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003210
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003211 /* Note: we must not try to cheat by jumping directly to DATA,
3212 * otherwise we would not let the client side wake up.
3213 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003214
Willy Tarreaudafde432008-08-17 01:00:46 +02003215 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003216 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003217
Willy Tarreau2df28e82008-08-17 15:20:19 +02003218 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3219 * probably reduce one day's debugging session.
3220 */
3221#ifdef DEBUG_DEV
3222 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3223 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3224 __FILE__, __LINE__, rep->analysers);
3225 ABORT_NOW();
3226 }
3227#endif
3228 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003229 return 0;
3230}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003231
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003232///*
3233// * Manages the client FSM and its socket. It normally returns zero, but may
3234// * return 1 if it absolutely wants to be called again.
3235// *
3236// * Note: process_cli is the ONLY function allowed to set cli_state to anything
3237// * but CL_STCLOSE.
3238// */
3239//int process_cli(struct session *t)
3240//{
3241// struct buffer *req = t->req;
3242// struct buffer *rep = t->rep;
3243//
3244// DPRINTF(stderr,"[%u] %s: fd=%d[%d] c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3245// now_ms, __FUNCTION__,
3246// t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/
3247// cli_stnames[t->cli_state],
3248// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3249// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
3250// req->rex, rep->wex,
3251// req->flags, rep->flags,
3252// req->l, rep->l);
3253//
3254// update_state:
3255// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3256// * still set this state (and will do until unix sockets are converted).
3257// */
3258// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
3259// /* we can skip most of the tests at once if some conditions are not met */
3260// if (!((fdtab[t->cli_fd].state == FD_STERROR) ||
3261// (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) ||
3262// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) ||
3263// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3264// (!(rep->flags & BF_SHUTW) &&
3265// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3266// goto update_timeouts;
3267//
3268// /* read or write error */
3269// if (fdtab[t->cli_fd].state == FD_STERROR) {
3270// buffer_shutr(req);
3271// req->flags |= BF_READ_ERROR;
3272// buffer_shutw(rep);
3273// rep->flags |= BF_WRITE_ERROR;
3274// fd_delete(t->cli_fd);
3275// t->cli_state = CL_STCLOSE;
3276// trace_term(t, TT_HTTP_CLI_1);
3277// if (!req->analysers) {
3278// if (!(t->flags & SN_ERR_MASK))
3279// t->flags |= SN_ERR_CLICL;
3280// if (!(t->flags & SN_FINST_MASK)) {
3281// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3282// t->flags |= SN_FINST_Q;
3283// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3284// t->flags |= SN_FINST_C;
3285// else
3286// t->flags |= SN_FINST_D;
3287// }
3288// }
3289// goto update_state;
3290// }
3291// /* last read, or end of server write */
3292// else if (!(req->flags & BF_SHUTR) && /* not already done */
3293// req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3294// buffer_shutr(req);
3295// if (!(rep->flags & BF_SHUTW)) {
3296// EV_FD_CLR(t->cli_fd, DIR_RD);
3297// trace_term(t, TT_HTTP_CLI_2);
3298// } else {
3299// /* output was already closed */
3300// fd_delete(t->cli_fd);
3301// t->cli_state = CL_STCLOSE;
3302// trace_term(t, TT_HTTP_CLI_3);
3303// }
3304// goto update_state;
3305// }
3306// /* last server read and buffer empty : we only check them when we're
3307// * allowed to forward the data.
3308// */
3309// else if (!(rep->flags & BF_SHUTW) && /* not already done */
3310// ((rep->flags & BF_SHUTW_NOW) ||
3311// (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
3312// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) {
3313// buffer_shutw(rep);
3314// if (!(req->flags & BF_SHUTR)) {
3315// EV_FD_CLR(t->cli_fd, DIR_WR);
3316// shutdown(t->cli_fd, SHUT_WR);
3317// trace_term(t, TT_HTTP_CLI_4);
3318// } else {
3319// fd_delete(t->cli_fd);
3320// t->cli_state = CL_STCLOSE;
3321// trace_term(t, TT_HTTP_CLI_5);
3322// }
3323// goto update_state;
3324// }
3325// /* read timeout */
3326// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
3327// buffer_shutr(req);
3328// if (!(rep->flags & BF_SHUTW)) {
3329// EV_FD_CLR(t->cli_fd, DIR_RD);
3330// trace_term(t, TT_HTTP_CLI_6);
3331// } else {
3332// /* output was already closed */
3333// fd_delete(t->cli_fd);
3334// t->cli_state = CL_STCLOSE;
3335// trace_term(t, TT_HTTP_CLI_7);
3336// }
3337// if (!req->analysers) {
3338// if (!(t->flags & SN_ERR_MASK))
3339// t->flags |= SN_ERR_CLITO;
3340// if (!(t->flags & SN_FINST_MASK)) {
3341// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3342// t->flags |= SN_FINST_Q;
3343// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3344// t->flags |= SN_FINST_C;
3345// else
3346// t->flags |= SN_FINST_D;
3347// }
3348// }
3349// goto update_state;
3350// }
3351// /* write timeout */
3352// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
3353// buffer_shutw(rep);
3354// if (!(req->flags & BF_SHUTR)) {
3355// EV_FD_CLR(t->cli_fd, DIR_WR);
3356// shutdown(t->cli_fd, SHUT_WR);
3357// trace_term(t, TT_HTTP_CLI_8);
3358// } else {
3359// fd_delete(t->cli_fd);
3360// t->cli_state = CL_STCLOSE;
3361// trace_term(t, TT_HTTP_CLI_9);
3362// }
3363// if (!req->analysers) {
3364// if (!(t->flags & SN_ERR_MASK))
3365// t->flags |= SN_ERR_CLITO;
3366// if (!(t->flags & SN_FINST_MASK)) {
3367// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3368// t->flags |= SN_FINST_Q;
3369// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3370// t->flags |= SN_FINST_C;
3371// else
3372// t->flags |= SN_FINST_D;
3373// }
3374// }
3375// goto update_state;
3376// }
3377//
3378// update_timeouts:
3379// /* manage read timeout */
3380// if (!(req->flags & BF_SHUTR)) {
3381// if (req->flags & BF_FULL) {
3382// /* no room to read more data */
3383// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3384// /* stop reading until we get some space */
3385// req->rex = TICK_ETERNITY;
3386// }
3387// } else {
3388// EV_FD_COND_S(t->cli_fd, DIR_RD);
3389// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3390// }
3391// }
3392//
3393// /* manage write timeout */
3394// if (!(rep->flags & BF_SHUTW)) {
3395// /* first, we may have to produce data (eg: stats).
3396// * right now, this is limited to the SHUTR state.
3397// */
3398// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
3399// produce_content(t);
3400// if (rep->flags & BF_EMPTY) {
3401// buffer_shutw(rep);
3402// fd_delete(t->cli_fd);
3403// t->cli_state = CL_STCLOSE;
3404// trace_term(t, TT_HTTP_CLI_10);
3405// goto update_state;
3406// }
3407// }
3408//
3409// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
3410// if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
3411// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3412// /* stop writing */
3413// rep->wex = TICK_ETERNITY;
3414// }
3415// } else {
3416// /* buffer not empty */
3417// EV_FD_COND_S(t->cli_fd, DIR_WR);
3418// if (!tick_isset(rep->wex)) {
3419// /* restart writing */
3420// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
3421// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
3422// /* FIXME: to prevent the client from expiring read timeouts during writes,
3423// * we refresh it, except if it was already infinite. */
3424// req->rex = rep->wex;
3425// }
3426// }
3427// }
3428// }
3429// return 0; /* other cases change nothing */
3430// }
3431// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
3432// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3433// int len;
3434// len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)req->cons->fd);
3435// write(1, trash, len);
3436// }
3437// return 0;
3438// }
3439//#ifdef DEBUG_DEV
3440// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3441// ABORT_NOW();
3442//#endif
3443// return 0;
3444//}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003445
Willy Tarreaubaaee002006-06-26 02:48:02 +02003446
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003447/* Return 1 if the pending connection has failed and should be retried,
3448 * otherwise zero. We may only come here in SI_ST_CON state, which means that
3449 * the socket's file descriptor is known.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003450 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003451int tcp_connection_status(struct session *t)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003452{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003453 struct buffer *req = t->req;
3454 struct buffer *rep = t->rep;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003455 int conn_err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003456
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003457 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3458 now_ms, __FUNCTION__,
3459 cli_stnames[t->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003460 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003461 req->flags, rep->flags,
3462 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003463
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003464 if ((req->flags & BF_SHUTW_NOW) ||
3465 (rep->flags & BF_SHUTW) ||
3466 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreau3da77c52008-08-29 09:58:42 +02003467 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003468 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3469
3470 trace_term(t, TT_HTTP_SRV_5);
3471 req->wex = TICK_ETERNITY;
3472 fd_delete(req->cons->fd);
3473 if (t->srv) {
3474 t->srv->cur_sess--;
3475 sess_change_server(t, NULL);
3476 }
3477 /* note that this must not return any error because it would be able to
3478 * overwrite the client_retnclose() output.
3479 */
3480 //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3481
3482 // FIXME: should we set rep->MAY_FORWARD ?
3483 buffer_shutw(req);
3484 buffer_shutr(rep);
3485 req->cons->state = SI_ST_CLO;
3486 if (!req->cons->err_type)
3487 req->cons->err_type = SI_ET_CONN_ABRT;
3488 req->cons->err_loc = t->srv;
3489 return 0;
3490 }
3491
3492 /* check for timeouts and asynchronous connect errors */
3493 if (fdtab[req->cons->fd].state == FD_STERROR) {
3494 conn_err = SI_ET_CONN_ERR;
3495 if (!req->cons->err_type)
3496 req->cons->err_type = SI_ET_CONN_ERR;
3497 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003498 else if (!(req->flags & BF_WRITE_ACTIVITY)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003499 /* nothing happened, maybe we timed out */
3500 if (tick_is_expired(req->wex, now_ms)) {
3501 conn_err = SI_ET_CONN_TO;
3502 if (!req->cons->err_type)
3503 req->cons->err_type = SI_ET_CONN_TO;
3504 }
3505 else
3506 return 0; /* let's wait a bit more */
3507 }
3508
3509 if (conn_err) {
3510 fd_delete(req->cons->fd);
3511 req->cons->state = SI_ST_CLO;
3512
3513 if (t->srv) {
3514 t->srv->cur_sess--;
3515 sess_change_server(t, NULL);
3516 req->cons->err_loc = t->srv;
3517 }
3518
3519 /* ensure that we have enough retries left */
3520 if (srv_count_retry_down(t, conn_err))
3521 return 0;
3522
3523 if (conn_err == SI_ET_CONN_ERR) {
3524 /* we encountered an immediate connection error, and we
3525 * will have to retry connecting to the same server, most
3526 * likely leading to the same result. To avoid this, we
3527 * fake a connection timeout to retry after a turn-around
3528 * time of 1 second. We will wait in the previous if block.
3529 */
3530 req->cons->state = SI_ST_TAR;
3531 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
3532 return 0;
3533 }
3534
3535 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3536 /* We're on our last chance, and the REDISP option was specified.
3537 * We will ignore cookie and force to balance or use the dispatcher.
3538 */
3539 /* let's try to offer this slot to anybody */
3540 if (may_dequeue_tasks(t->srv, t->be))
3541 process_srv_queue(t->srv);
3542
3543 /* it's left to the dispatcher to choose a server */
3544 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3545 t->prev_srv = t->srv;
3546 } else {
3547 /* we just want to retry */
3548 if (t->srv)
3549 t->srv->retries++;
3550 t->be->retries++;
3551
3552 /* Now we will try to either reconnect to the same server or
3553 * connect to another server. If the connection gets queued
3554 * because all servers are saturated, then we will go back to
3555 * the idle state where the buffer's consumer is marked as
3556 * unknown.
3557 */
3558 if (srv_retryable_connect(t)) {
3559 /* success or unrecoverable error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003560 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003561 return 0;
3562 }
3563 }
3564
3565 /* We'll rely on the caller to try to get a connection again */
3566 return 1;
3567 }
3568 else {
3569 /* no error and write OK : connection succeeded */
3570 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
3571 req->cons->state = SI_ST_EST;
3572 req->cons->err_type = SI_ET_NONE;
3573 req->cons->err_loc = NULL;
3574
3575 if (req->flags & BF_EMPTY) {
3576 EV_FD_CLR(req->cons->fd, DIR_WR);
3577 req->wex = TICK_ETERNITY;
3578 } else {
3579 EV_FD_SET(req->cons->fd, DIR_WR);
3580 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3581 if (tick_isset(req->wex)) {
3582 /* FIXME: to prevent the server from expiring read timeouts during writes,
3583 * we refresh it. */
3584 rep->rex = req->wex;
3585 }
3586 }
3587
3588 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3589 if (!(rep->flags & BF_HIJACK)) {
3590 EV_FD_SET(req->cons->fd, DIR_RD);
3591 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3592 }
3593 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
3594
3595 /* if the user wants to log as soon as possible, without counting
3596 bytes from the server, then this is the right moment. */
3597 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3598 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3599 tcp_sess_log(t);
3600 }
3601#ifdef CONFIG_HAP_TCPSPLICE
3602 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3603 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003604 tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003605 }
3606#endif
3607 }
3608 else {
3609 rep->analysers |= AN_RTR_HTTP_HDR;
3610 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3611 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3612 /* reset hdr_idx which was already initialized by the request.
3613 * right now, the http parser does it.
3614 * hdr_idx_init(&t->txn.hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003615 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003616 }
3617
3618 if (!rep->analysers)
Willy Tarreau3da77c52008-08-29 09:58:42 +02003619 buffer_write_ena(t->rep);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003620 req->wex = TICK_ETERNITY;
3621 return 0;
3622 }
3623}
3624
3625
3626/*
3627 * This function tries to assign a server to a stream_sock interface.
3628 * It may be called only for t->req->cons->state = one of { SI_ST_INI,
3629 * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS
3630 * in case of success, or SI_ST_CLO in case of failure. It returns 1 if
3631 * it returns SI_ST_ASS, otherwise zero.
3632 */
3633int stream_sock_assign_server(struct session *t)
3634{
3635 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3636 now_ms, __FUNCTION__,
3637 cli_stnames[t->cli_state],
3638 t->rep->rex, t->req->wex,
3639 t->req->flags, t->rep->flags,
3640 t->req->l, t->rep->l);
3641
3642 if (t->req->cons->state == SI_ST_TAR) {
3643 /* connection might be aborted */
3644 if ((t->req->flags & BF_SHUTW_NOW) ||
3645 (t->rep->flags & BF_SHUTW) ||
3646 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3647 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003648
Willy Tarreauf8533202008-08-16 14:55:08 +02003649 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003650 t->req->wex = TICK_ETERNITY;
3651
3652 // FIXME: should we set rep->MAY_FORWARD ?
3653 buffer_shutr(t->rep);
3654 buffer_shutw(t->req);
3655 if (!t->req->cons->err_type)
3656 t->req->cons->err_type = SI_ET_CONN_ABRT;
3657 t->req->cons->state = SI_ST_CLO;
3658 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003659 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003660
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003661 if (!tick_is_expired(t->req->wex, now_ms))
3662 return 0; /* still in turn-around */
3663
3664 t->req->cons->state = SI_ST_INI;
3665 }
3666 else if (t->req->cons->state == SI_ST_QUE) {
3667 if (t->pend_pos) {
3668 /* request still in queue... */
3669 if (tick_is_expired(t->req->wex, now_ms)) {
3670 /* ... and timeout expired */
3671 trace_term(t, TT_HTTP_SRV_3);
3672 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003673 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003674 if (t->srv)
3675 t->srv->failed_conns++;
3676 t->be->failed_conns++;
3677
3678 // FIXME: should we set rep->MAY_FORWARD ?
3679 buffer_shutr(t->rep);
3680 buffer_shutw(t->req);
3681 t->req->flags |= BF_WRITE_TIMEOUT;
3682 if (!t->req->cons->err_type)
3683 t->req->cons->err_type = SI_ET_QUEUE_TO;
3684 t->req->cons->state = SI_ST_CLO;
3685 return 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003686 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003687 /* connection remains in queue, check if we have to abort it */
3688 if ((t->req->flags & BF_SHUTW_NOW) ||
3689 (t->rep->flags & BF_SHUTW) ||
3690 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3691 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) {
3692 /* give up */
3693 trace_term(t, TT_HTTP_SRV_1);
3694 t->req->wex = TICK_ETERNITY;
3695 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003696
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003697 // FIXME: should we set rep->MAY_FORWARD ?
3698 buffer_shutr(t->rep);
3699 buffer_shutw(t->req);
3700 if (!t->req->cons->err_type)
3701 t->req->cons->err_type = SI_ET_QUEUE_ABRT;
3702 t->req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003703 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003704 return 0;
3705 }
3706 /* The connection is not in the queue anymore */
3707 t->req->cons->state = SI_ST_INI;
3708 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003709
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003710 /* we may get here from above */
3711 if (t->req->cons->state == SI_ST_INI) {
3712 /* no connection in progress, we have to get a new one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003713
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003714 /* first, check if the connection has been aborted */
3715 if ((t->req->flags & BF_SHUTW_NOW) ||
3716 (t->rep->flags & BF_SHUTW) ||
3717 ((t->req->flags & BF_SHUTR) &&
3718 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003719
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003720 trace_term(t, TT_HTTP_SRV_1);
3721 t->req->wex = TICK_ETERNITY;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003722
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003723 // FIXME: should we set rep->MAY_FORWARD ?
3724 buffer_shutr(t->rep);
3725 buffer_shutw(t->req);
3726 if (!t->req->cons->err_type)
3727 t->req->cons->err_type = SI_ET_CONN_ABRT;
3728 t->req->cons->state = SI_ST_CLO;
3729 return 0;
3730 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003731
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003732 /* try to get a server assigned */
3733 if (srv_redispatch_connect(t) != 0) {
3734 /* we did not get any server, let's check the cause */
3735 if (t->req->cons->state == SI_ST_QUE) {
3736 /* the connection was queued, that's OK */
3737 return 0;
3738 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003739
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003740 trace_term(t, TT_HTTP_SRV_2);
3741 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003742
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003743 // FIXME: should we set rep->MAY_FORWARD ?
3744 buffer_shutr(t->rep);
3745 buffer_shutw(t->req);
3746 t->req->flags |= BF_WRITE_ERROR;
3747 if (!t->req->cons->err_type)
3748 t->req->cons->err_type = SI_ET_CONN_OTHER;
3749 t->req->cons->state = SI_ST_CLO;
3750 return 0;
3751 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003752
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003753 t->req->cons->state = SI_ST_ASS;
3754 /* Once the server is assigned, we have to return because
3755 * the caller might be interested in checking several
3756 * things before connecting.
3757 */
3758 return 1;
3759 }
3760 return 0;
3761}
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003762
Willy Tarreauf8533202008-08-16 14:55:08 +02003763
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003764/*
3765 * This function tries to establish a connection to an assigned server. It also
3766 * performs connection retries. It may only be called with t->req->cons->state
3767 * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI,
3768 * SI_ST_EST, or SI_ST_CLO.
3769 */
3770int stream_sock_connect_server(struct session *t)
3771{
3772 if (t->req->cons->state == SI_ST_ASS) {
3773 /* server assigned to request, we have to try to connect now */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003774
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003775 if (!srv_retryable_connect(t)) {
3776 /* we need to redispatch */
3777 t->req->cons->state = SI_ST_INI;
3778 return 0;
3779 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003780
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003781 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3782 if (t->req->cons->state != SI_ST_CON) {
3783 /* it was an error */
3784 trace_term(t, TT_HTTP_SRV_4);
3785 t->req->wex = TICK_ETERNITY;
3786
3787 // FIXME: should we set rep->MAY_FORWARD ?
3788 buffer_shutr(t->rep);
3789 buffer_shutw(t->req);
3790 t->req->flags |= BF_WRITE_ERROR;
3791 if (!t->req->cons->err_type)
3792 t->req->cons->err_type = SI_ET_CONN_OTHER;
3793 t->req->cons->state = SI_ST_CLO;
3794 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003795 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003796 /* We have a socket and switched to SI_ST_CON */
3797 }
3798
3799 /* we may also get here from above */
3800 if (t->req->cons->state == SI_ST_CON) {
3801 /* connection in progress or just completed */
3802 if (!tcp_connection_status(t))
3803 return 0;
3804 }
3805 return 0;
3806}
3807
3808
3809/*
3810 * Tries to establish a connection to the server and associate it to the
3811 * request buffer's consumer side. It is assumed that this function will not be
Willy Tarreau3da77c52008-08-29 09:58:42 +02003812 * be called with SI_ST_EST nor with BF_WRITE_ENA cleared. It normally
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003813 * returns zero, but may return 1 if it absolutely wants to be called again.
3814 */
3815int process_srv_conn(struct session *t)
3816{
3817 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3818 now_ms, __FUNCTION__,
3819 cli_stnames[t->cli_state],
3820 t->rep->rex, t->req->wex,
3821 t->req->flags, t->rep->flags,
3822 t->req->l, t->rep->l);
3823
3824 do {
3825 if (t->req->cons->state == SI_ST_INI ||
3826 t->req->cons->state == SI_ST_TAR ||
3827 t->req->cons->state == SI_ST_QUE) {
3828 /* try to assign a server */
3829 if (!stream_sock_assign_server(t))
3830 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003831 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003832
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003833 if (t->req->cons->state == SI_ST_ASS &&
3834 t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
3835 /* Server supporting redirection and it is possible.
3836 * Invalid requests are reported as such. It concerns all
3837 * the largest ones.
3838 */
3839 struct http_txn *txn = &t->txn;
3840 struct chunk rdr;
3841 char *path;
3842 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003843
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003844 /* 1: create the response header */
3845 rdr.len = strlen(HTTP_302);
3846 rdr.str = trash;
3847 memcpy(rdr.str, HTTP_302, rdr.len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003848
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003849 /* 2: add the server's prefix */
3850 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3851 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003852
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003853 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3854 rdr.len += t->srv->rdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003855
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003856 /* 3: add the request URI */
3857 path = http_get_path(txn);
3858 if (!path)
3859 goto cancel_redir;
3860 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3861 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3862 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003863
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003864 memcpy(rdr.str + rdr.len, path, len);
3865 rdr.len += len;
3866 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3867 rdr.len += 4;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003868
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003869 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3870 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003871
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003872 /* FIXME: we should increase a counter of redirects per server and per backend. */
3873 if (t->srv)
3874 t->srv->cum_sess++;
3875
3876 t->req->cons->state = SI_ST_CLO;
3877 return 0;
3878 cancel_redir:
3879 //txn->status = 400;
3880 //t->fe->failed_req++;
3881 //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3882 // 400, error_message(t, HTTP_ERR_400));
3883 trace_term(t, TT_HTTP_SRV_4);
3884
3885 // FIXME: should we set rep->MAY_FORWARD ?
3886 buffer_shutw(t->req);
3887 buffer_shutr(t->rep);
3888 if (!t->req->cons->err_type)
3889 t->req->cons->err_type = SI_ET_CONN_OTHER;
3890 t->req->cons->state = SI_ST_CLO;
3891 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003892 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003893
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003894 if (t->req->cons->state == SI_ST_CON ||
3895 t->req->cons->state == SI_ST_ASS) {
3896 stream_sock_connect_server(t);
3897 }
3898 } while (t->req->cons->state != SI_ST_CLO &&
3899 t->req->cons->state != SI_ST_CON &&
3900 t->req->cons->state != SI_ST_EST);
3901 return 0;
3902}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003903
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003904
Willy Tarreaubaaee002006-06-26 02:48:02 +02003905/*
3906 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003907 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003908 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3909 * buffer, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003910 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003911 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003912 */
3913int produce_content(struct session *s)
3914{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003915 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau72b179a2008-08-28 16:01:32 +02003916 buffer_stop_hijack(s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003917 return 1;
3918 }
3919 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003920 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003921 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003922 if (ret >= 0)
3923 return ret;
3924 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003925 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003926
Willy Tarreau91861262007-10-17 17:06:05 +02003927 /* unknown data source or internal error */
3928 s->txn.status = 500;
3929 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003930 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003931 if (!(s->flags & SN_ERR_MASK))
3932 s->flags |= SN_ERR_PRXCOND;
3933 if (!(s->flags & SN_FINST_MASK))
3934 s->flags |= SN_FINST_R;
Willy Tarreau72b179a2008-08-28 16:01:32 +02003935 buffer_stop_hijack(s->rep);
Willy Tarreau91861262007-10-17 17:06:05 +02003936 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003937}
3938
3939
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003940/* Iterate the same filter through all request headers.
3941 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003942 * Since it can manage the switch to another backend, it updates the per-proxy
3943 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003944 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003945int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003946{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003947 char term;
3948 char *cur_ptr, *cur_end, *cur_next;
3949 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003950 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003951 struct hdr_idx_elem *cur_hdr;
3952 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003953
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003954 last_hdr = 0;
3955
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003956 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003957 old_idx = 0;
3958
3959 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003960 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003961 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003962 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003963 (exp->action == ACT_ALLOW ||
3964 exp->action == ACT_DENY ||
3965 exp->action == ACT_TARPIT))
3966 return 0;
3967
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003968 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003969 if (!cur_idx)
3970 break;
3971
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003972 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003973 cur_ptr = cur_next;
3974 cur_end = cur_ptr + cur_hdr->len;
3975 cur_next = cur_end + cur_hdr->cr + 1;
3976
3977 /* Now we have one header between cur_ptr and cur_end,
3978 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003979 */
3980
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003981 /* The annoying part is that pattern matching needs
3982 * that we modify the contents to null-terminate all
3983 * strings before testing them.
3984 */
3985
3986 term = *cur_end;
3987 *cur_end = '\0';
3988
3989 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3990 switch (exp->action) {
3991 case ACT_SETBE:
3992 /* It is not possible to jump a second time.
3993 * FIXME: should we return an HTTP/500 here so that
3994 * the admin knows there's a problem ?
3995 */
3996 if (t->be != t->fe)
3997 break;
3998
3999 /* Swithing Proxy */
4000 t->be = (struct proxy *) exp->replace;
4001
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004002 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004003 * because we have associated req_cap and rsp_cap to the
4004 * frontend, and the beconn will be updated later.
4005 */
4006
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004007 t->rep->rto = t->req->wto = t->be->timeout.server;
4008 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004009 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004010 last_hdr = 1;
4011 break;
4012
4013 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004014 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004015 last_hdr = 1;
4016 break;
4017
4018 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004019 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004020 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004021 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004022 break;
4023
4024 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004025 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004026 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004027 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004028 break;
4029
4030 case ACT_REPLACE:
4031 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4032 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4033 /* FIXME: if the user adds a newline in the replacement, the
4034 * index will not be recalculated for now, and the new line
4035 * will not be counted as a new header.
4036 */
4037
4038 cur_end += delta;
4039 cur_next += delta;
4040 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004041 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004042 break;
4043
4044 case ACT_REMOVE:
4045 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4046 cur_next += delta;
4047
4048 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004049 txn->req.eoh += delta;
4050 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4051 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004052 cur_hdr->len = 0;
4053 cur_end = NULL; /* null-term has been rewritten */
4054 break;
4055
4056 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004057 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004058 if (cur_end)
4059 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004061 /* keep the link from this header to next one in case of later
4062 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004063 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004064 old_idx = cur_idx;
4065 }
4066 return 0;
4067}
4068
4069
4070/* Apply the filter to the request line.
4071 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4072 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004073 * Since it can manage the switch to another backend, it updates the per-proxy
4074 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004075 */
4076int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4077{
4078 char term;
4079 char *cur_ptr, *cur_end;
4080 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004081 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004082 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004083
Willy Tarreau58f10d72006-12-04 02:26:12 +01004084
Willy Tarreau3d300592007-03-18 18:34:41 +01004085 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004086 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004087 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004088 (exp->action == ACT_ALLOW ||
4089 exp->action == ACT_DENY ||
4090 exp->action == ACT_TARPIT))
4091 return 0;
4092 else if (exp->action == ACT_REMOVE)
4093 return 0;
4094
4095 done = 0;
4096
Willy Tarreau9cdde232007-05-02 20:58:19 +02004097 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004098 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004099
4100 /* Now we have the request line between cur_ptr and cur_end */
4101
4102 /* The annoying part is that pattern matching needs
4103 * that we modify the contents to null-terminate all
4104 * strings before testing them.
4105 */
4106
4107 term = *cur_end;
4108 *cur_end = '\0';
4109
4110 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4111 switch (exp->action) {
4112 case ACT_SETBE:
4113 /* It is not possible to jump a second time.
4114 * FIXME: should we return an HTTP/500 here so that
4115 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004116 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004117 if (t->be != t->fe)
4118 break;
4119
4120 /* Swithing Proxy */
4121 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004122
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004123 /* right now, the backend switch is not too much complicated
4124 * because we have associated req_cap and rsp_cap to the
4125 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004126 */
4127
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004128 t->rep->rto = t->req->wto = t->be->timeout.server;
4129 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004130 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004131 done = 1;
4132 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004133
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004134 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004135 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004136 done = 1;
4137 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004139 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004140 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004141 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004142 done = 1;
4143 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004144
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004145 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004146 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004147 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004148 done = 1;
4149 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004150
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004151 case ACT_REPLACE:
4152 *cur_end = term; /* restore the string terminator */
4153 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4154 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4155 /* FIXME: if the user adds a newline in the replacement, the
4156 * index will not be recalculated for now, and the new line
4157 * will not be counted as a new header.
4158 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004159
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004160 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004161 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004162
Willy Tarreau9cdde232007-05-02 20:58:19 +02004163 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004164 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004165 HTTP_MSG_RQMETH,
4166 cur_ptr, cur_end + 1,
4167 NULL, NULL);
4168 if (unlikely(!cur_end))
4169 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004171 /* we have a full request and we know that we have either a CR
4172 * or an LF at <ptr>.
4173 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004174 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4175 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004176 /* there is no point trying this regex on headers */
4177 return 1;
4178 }
4179 }
4180 *cur_end = term; /* restore the string terminator */
4181 return done;
4182}
Willy Tarreau97de6242006-12-27 17:18:38 +01004183
Willy Tarreau58f10d72006-12-04 02:26:12 +01004184
Willy Tarreau58f10d72006-12-04 02:26:12 +01004185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004186/*
4187 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4188 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004189 * unparsable request. Since it can manage the switch to another backend, it
4190 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004191 */
4192int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4193{
Willy Tarreau3d300592007-03-18 18:34:41 +01004194 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004195 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004196 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004197 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004198
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004199 /*
4200 * The interleaving of transformations and verdicts
4201 * makes it difficult to decide to continue or stop
4202 * the evaluation.
4203 */
4204
Willy Tarreau3d300592007-03-18 18:34:41 +01004205 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004206 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4207 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4208 exp = exp->next;
4209 continue;
4210 }
4211
4212 /* Apply the filter to the request line. */
4213 ret = apply_filter_to_req_line(t, req, exp);
4214 if (unlikely(ret < 0))
4215 return -1;
4216
4217 if (likely(ret == 0)) {
4218 /* The filter did not match the request, it can be
4219 * iterated through all headers.
4220 */
4221 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004222 }
4223 exp = exp->next;
4224 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004225 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004226}
4227
4228
Willy Tarreaua15645d2007-03-18 16:22:39 +01004229
Willy Tarreau58f10d72006-12-04 02:26:12 +01004230/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004231 * Manage client-side cookie. It can impact performance by about 2% so it is
4232 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004233 */
4234void manage_client_side_cookies(struct session *t, struct buffer *req)
4235{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004236 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004237 char *p1, *p2, *p3, *p4;
4238 char *del_colon, *del_cookie, *colon;
4239 int app_cookies;
4240
4241 appsess *asession_temp = NULL;
4242 appsess local_asession;
4243
4244 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004245 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004246
Willy Tarreau2a324282006-12-05 00:05:46 +01004247 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004248 * we start with the start line.
4249 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004250 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004251 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004252
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004253 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004254 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004255 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004256
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004257 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004258 cur_ptr = cur_next;
4259 cur_end = cur_ptr + cur_hdr->len;
4260 cur_next = cur_end + cur_hdr->cr + 1;
4261
4262 /* We have one full header between cur_ptr and cur_end, and the
4263 * next header starts at cur_next. We're only interested in
4264 * "Cookie:" headers.
4265 */
4266
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004267 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4268 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004269 old_idx = cur_idx;
4270 continue;
4271 }
4272
4273 /* Now look for cookies. Conforming to RFC2109, we have to support
4274 * attributes whose name begin with a '$', and associate them with
4275 * the right cookie, if we want to delete this cookie.
4276 * So there are 3 cases for each cookie read :
4277 * 1) it's a special attribute, beginning with a '$' : ignore it.
4278 * 2) it's a server id cookie that we *MAY* want to delete : save
4279 * some pointers on it (last semi-colon, beginning of cookie...)
4280 * 3) it's an application cookie : we *MAY* have to delete a previous
4281 * "special" cookie.
4282 * At the end of loop, if a "special" cookie remains, we may have to
4283 * remove it. If no application cookie persists in the header, we
4284 * *MUST* delete it
4285 */
4286
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004287 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004288
Willy Tarreau58f10d72006-12-04 02:26:12 +01004289 /* del_cookie == NULL => nothing to be deleted */
4290 del_colon = del_cookie = NULL;
4291 app_cookies = 0;
4292
4293 while (p1 < cur_end) {
4294 /* skip spaces and colons, but keep an eye on these ones */
4295 while (p1 < cur_end) {
4296 if (*p1 == ';' || *p1 == ',')
4297 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004298 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004299 break;
4300 p1++;
4301 }
4302
4303 if (p1 == cur_end)
4304 break;
4305
4306 /* p1 is at the beginning of the cookie name */
4307 p2 = p1;
4308 while (p2 < cur_end && *p2 != '=')
4309 p2++;
4310
4311 if (p2 == cur_end)
4312 break;
4313
4314 p3 = p2 + 1; /* skips the '=' sign */
4315 if (p3 == cur_end)
4316 break;
4317
4318 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004319 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004320 p4++;
4321
4322 /* here, we have the cookie name between p1 and p2,
4323 * and its value between p3 and p4.
4324 * we can process it :
4325 *
4326 * Cookie: NAME=VALUE;
4327 * | || || |
4328 * | || || +--> p4
4329 * | || |+-------> p3
4330 * | || +--------> p2
4331 * | |+------------> p1
4332 * | +-------------> colon
4333 * +--------------------> cur_ptr
4334 */
4335
4336 if (*p1 == '$') {
4337 /* skip this one */
4338 }
4339 else {
4340 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004341 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004342 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004343 (p4 - p1 >= t->fe->capture_namelen) &&
4344 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004345 int log_len = p4 - p1;
4346
Willy Tarreau086b3b42007-05-13 21:45:51 +02004347 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004348 Alert("HTTP logging : out of memory.\n");
4349 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004350 if (log_len > t->fe->capture_len)
4351 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004352 memcpy(txn->cli_cookie, p1, log_len);
4353 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004354 }
4355 }
4356
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004357 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4358 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004359 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004360 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004361 char *delim;
4362
4363 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4364 * have the server ID betweek p3 and delim, and the original cookie between
4365 * delim+1 and p4. Otherwise, delim==p4 :
4366 *
4367 * Cookie: NAME=SRV~VALUE;
4368 * | || || | |
4369 * | || || | +--> p4
4370 * | || || +--------> delim
4371 * | || |+-----------> p3
4372 * | || +------------> p2
4373 * | |+----------------> p1
4374 * | +-----------------> colon
4375 * +------------------------> cur_ptr
4376 */
4377
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004378 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004379 for (delim = p3; delim < p4; delim++)
4380 if (*delim == COOKIE_DELIM)
4381 break;
4382 }
4383 else
4384 delim = p4;
4385
4386
4387 /* Here, we'll look for the first running server which supports the cookie.
4388 * This allows to share a same cookie between several servers, for example
4389 * to dedicate backup servers to specific servers only.
4390 * However, to prevent clients from sticking to cookie-less backup server
4391 * when they have incidentely learned an empty cookie, we simply ignore
4392 * empty cookies and mark them as invalid.
4393 */
4394 if (delim == p3)
4395 srv = NULL;
4396
4397 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004398 if (srv->cookie && (srv->cklen == delim - p3) &&
4399 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004400 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004401 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004402 txn->flags &= ~TX_CK_MASK;
4403 txn->flags |= TX_CK_VALID;
4404 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004405 t->srv = srv;
4406 break;
4407 } else {
4408 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004409 txn->flags &= ~TX_CK_MASK;
4410 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004411 }
4412 }
4413 srv = srv->next;
4414 }
4415
Willy Tarreau3d300592007-03-18 18:34:41 +01004416 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004417 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004418 txn->flags &= ~TX_CK_MASK;
4419 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004420 }
4421
4422 /* depending on the cookie mode, we may have to either :
4423 * - delete the complete cookie if we're in insert+indirect mode, so that
4424 * the server never sees it ;
4425 * - remove the server id from the cookie value, and tag the cookie as an
4426 * application cookie so that it does not get accidentely removed later,
4427 * if we're in cookie prefix mode
4428 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004429 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004430 int delta; /* negative */
4431
4432 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4433 p4 += delta;
4434 cur_end += delta;
4435 cur_next += delta;
4436 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004437 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004438
4439 del_cookie = del_colon = NULL;
4440 app_cookies++; /* protect the header from deletion */
4441 }
4442 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004443 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004444 del_cookie = p1;
4445 del_colon = colon;
4446 }
4447 } else {
4448 /* now we know that we must keep this cookie since it's
4449 * not ours. But if we wanted to delete our cookie
4450 * earlier, we cannot remove the complete header, but we
4451 * can remove the previous block itself.
4452 */
4453 app_cookies++;
4454
4455 if (del_cookie != NULL) {
4456 int delta; /* negative */
4457
4458 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4459 p4 += delta;
4460 cur_end += delta;
4461 cur_next += delta;
4462 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004463 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004464 del_cookie = del_colon = NULL;
4465 }
4466 }
4467
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004468 if ((t->be->appsession_name != NULL) &&
4469 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004470 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004471
Willy Tarreau58f10d72006-12-04 02:26:12 +01004472 /* Cool... it's the right one */
4473
4474 asession_temp = &local_asession;
4475
Willy Tarreau63963c62007-05-13 21:29:55 +02004476 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004477 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4478 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4479 return;
4480 }
4481
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004482 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4483 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004484 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004485
Willy Tarreau58f10d72006-12-04 02:26:12 +01004486 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004487 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4488 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004489 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004490 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004491 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004492 Alert("Not enough memory process_cli():asession:calloc().\n");
4493 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4494 return;
4495 }
4496
4497 asession_temp->sessid = local_asession.sessid;
4498 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004499 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004500 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004501 } else {
4502 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004503 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004504 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004505 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004506 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004507 Alert("Found Application Session without matching server.\n");
4508 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004509 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004510 while (srv) {
4511 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004512 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004513 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004514 txn->flags &= ~TX_CK_MASK;
4515 txn->flags |= TX_CK_VALID;
4516 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004517 t->srv = srv;
4518 break;
4519 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004520 txn->flags &= ~TX_CK_MASK;
4521 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004522 }
4523 }
4524 srv = srv->next;
4525 }/* end while(srv) */
4526 }/* end else if server == NULL */
4527
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004528 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004529 asession_temp->request_count++;
4530#if defined(DEBUG_HASH)
4531 Alert("manage_client_side_cookies\n");
4532 appsession_hash_dump(&(t->be->htbl_proxy));
4533#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004534 }/* end if ((t->proxy->appsession_name != NULL) ... */
4535 }
4536
4537 /* we'll have to look for another cookie ... */
4538 p1 = p4;
4539 } /* while (p1 < cur_end) */
4540
4541 /* There's no more cookie on this line.
4542 * We may have marked the last one(s) for deletion.
4543 * We must do this now in two ways :
4544 * - if there is no app cookie, we simply delete the header ;
4545 * - if there are app cookies, we must delete the end of the
4546 * string properly, including the colon/semi-colon before
4547 * the cookie name.
4548 */
4549 if (del_cookie != NULL) {
4550 int delta;
4551 if (app_cookies) {
4552 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4553 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004554 cur_hdr->len += delta;
4555 } else {
4556 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004557
4558 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004559 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4560 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004561 cur_hdr->len = 0;
4562 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004563 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004564 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004565 }
4566
4567 /* keep the link from this header to next one */
4568 old_idx = cur_idx;
4569 } /* end of cookie processing on this header */
4570}
4571
4572
Willy Tarreaua15645d2007-03-18 16:22:39 +01004573/* Iterate the same filter through all response headers contained in <rtr>.
4574 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4575 */
4576int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4577{
4578 char term;
4579 char *cur_ptr, *cur_end, *cur_next;
4580 int cur_idx, old_idx, last_hdr;
4581 struct http_txn *txn = &t->txn;
4582 struct hdr_idx_elem *cur_hdr;
4583 int len, delta;
4584
4585 last_hdr = 0;
4586
4587 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4588 old_idx = 0;
4589
4590 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004591 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004592 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004593 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004594 (exp->action == ACT_ALLOW ||
4595 exp->action == ACT_DENY))
4596 return 0;
4597
4598 cur_idx = txn->hdr_idx.v[old_idx].next;
4599 if (!cur_idx)
4600 break;
4601
4602 cur_hdr = &txn->hdr_idx.v[cur_idx];
4603 cur_ptr = cur_next;
4604 cur_end = cur_ptr + cur_hdr->len;
4605 cur_next = cur_end + cur_hdr->cr + 1;
4606
4607 /* Now we have one header between cur_ptr and cur_end,
4608 * and the next header starts at cur_next.
4609 */
4610
4611 /* The annoying part is that pattern matching needs
4612 * that we modify the contents to null-terminate all
4613 * strings before testing them.
4614 */
4615
4616 term = *cur_end;
4617 *cur_end = '\0';
4618
4619 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4620 switch (exp->action) {
4621 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004622 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004623 last_hdr = 1;
4624 break;
4625
4626 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004627 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004628 last_hdr = 1;
4629 break;
4630
4631 case ACT_REPLACE:
4632 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4633 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4634 /* FIXME: if the user adds a newline in the replacement, the
4635 * index will not be recalculated for now, and the new line
4636 * will not be counted as a new header.
4637 */
4638
4639 cur_end += delta;
4640 cur_next += delta;
4641 cur_hdr->len += delta;
4642 txn->rsp.eoh += delta;
4643 break;
4644
4645 case ACT_REMOVE:
4646 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4647 cur_next += delta;
4648
4649 /* FIXME: this should be a separate function */
4650 txn->rsp.eoh += delta;
4651 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4652 txn->hdr_idx.used--;
4653 cur_hdr->len = 0;
4654 cur_end = NULL; /* null-term has been rewritten */
4655 break;
4656
4657 }
4658 }
4659 if (cur_end)
4660 *cur_end = term; /* restore the string terminator */
4661
4662 /* keep the link from this header to next one in case of later
4663 * removal of next header.
4664 */
4665 old_idx = cur_idx;
4666 }
4667 return 0;
4668}
4669
4670
4671/* Apply the filter to the status line in the response buffer <rtr>.
4672 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4673 * or -1 if a replacement resulted in an invalid status line.
4674 */
4675int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4676{
4677 char term;
4678 char *cur_ptr, *cur_end;
4679 int done;
4680 struct http_txn *txn = &t->txn;
4681 int len, delta;
4682
4683
Willy Tarreau3d300592007-03-18 18:34:41 +01004684 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004685 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004686 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004687 (exp->action == ACT_ALLOW ||
4688 exp->action == ACT_DENY))
4689 return 0;
4690 else if (exp->action == ACT_REMOVE)
4691 return 0;
4692
4693 done = 0;
4694
Willy Tarreau9cdde232007-05-02 20:58:19 +02004695 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004696 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4697
4698 /* Now we have the status line between cur_ptr and cur_end */
4699
4700 /* The annoying part is that pattern matching needs
4701 * that we modify the contents to null-terminate all
4702 * strings before testing them.
4703 */
4704
4705 term = *cur_end;
4706 *cur_end = '\0';
4707
4708 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4709 switch (exp->action) {
4710 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004711 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004712 done = 1;
4713 break;
4714
4715 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004716 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004717 done = 1;
4718 break;
4719
4720 case ACT_REPLACE:
4721 *cur_end = term; /* restore the string terminator */
4722 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4723 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4724 /* FIXME: if the user adds a newline in the replacement, the
4725 * index will not be recalculated for now, and the new line
4726 * will not be counted as a new header.
4727 */
4728
4729 txn->rsp.eoh += delta;
4730 cur_end += delta;
4731
Willy Tarreau9cdde232007-05-02 20:58:19 +02004732 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004733 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004734 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004735 cur_ptr, cur_end + 1,
4736 NULL, NULL);
4737 if (unlikely(!cur_end))
4738 return -1;
4739
4740 /* we have a full respnse and we know that we have either a CR
4741 * or an LF at <ptr>.
4742 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004743 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004744 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4745 /* there is no point trying this regex on headers */
4746 return 1;
4747 }
4748 }
4749 *cur_end = term; /* restore the string terminator */
4750 return done;
4751}
4752
4753
4754
4755/*
4756 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4757 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4758 * unparsable response.
4759 */
4760int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4761{
Willy Tarreau3d300592007-03-18 18:34:41 +01004762 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004763 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004764 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004765 int ret;
4766
4767 /*
4768 * The interleaving of transformations and verdicts
4769 * makes it difficult to decide to continue or stop
4770 * the evaluation.
4771 */
4772
Willy Tarreau3d300592007-03-18 18:34:41 +01004773 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004774 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4775 exp->action == ACT_PASS)) {
4776 exp = exp->next;
4777 continue;
4778 }
4779
4780 /* Apply the filter to the status line. */
4781 ret = apply_filter_to_sts_line(t, rtr, exp);
4782 if (unlikely(ret < 0))
4783 return -1;
4784
4785 if (likely(ret == 0)) {
4786 /* The filter did not match the response, it can be
4787 * iterated through all headers.
4788 */
4789 apply_filter_to_resp_headers(t, rtr, exp);
4790 }
4791 exp = exp->next;
4792 }
4793 return 0;
4794}
4795
4796
4797
4798/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004799 * Manage server-side cookies. It can impact performance by about 2% so it is
4800 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004801 */
4802void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4803{
4804 struct http_txn *txn = &t->txn;
4805 char *p1, *p2, *p3, *p4;
4806
4807 appsess *asession_temp = NULL;
4808 appsess local_asession;
4809
4810 char *cur_ptr, *cur_end, *cur_next;
4811 int cur_idx, old_idx, delta;
4812
Willy Tarreaua15645d2007-03-18 16:22:39 +01004813 /* Iterate through the headers.
4814 * we start with the start line.
4815 */
4816 old_idx = 0;
4817 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4818
4819 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4820 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004821 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004822
4823 cur_hdr = &txn->hdr_idx.v[cur_idx];
4824 cur_ptr = cur_next;
4825 cur_end = cur_ptr + cur_hdr->len;
4826 cur_next = cur_end + cur_hdr->cr + 1;
4827
4828 /* We have one full header between cur_ptr and cur_end, and the
4829 * next header starts at cur_next. We're only interested in
4830 * "Cookie:" headers.
4831 */
4832
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004833 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4834 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004835 old_idx = cur_idx;
4836 continue;
4837 }
4838
4839 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004840 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004841
4842
4843 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004844 if (t->be->cookie_name == NULL &&
4845 t->be->appsession_name == NULL &&
4846 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004847 return;
4848
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004849 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004850
4851 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004852 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4853 break;
4854
4855 /* p1 is at the beginning of the cookie name */
4856 p2 = p1;
4857
4858 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4859 p2++;
4860
4861 if (p2 == cur_end || *p2 == ';') /* next cookie */
4862 break;
4863
4864 p3 = p2 + 1; /* skip the '=' sign */
4865 if (p3 == cur_end)
4866 break;
4867
4868 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004869 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004870 p4++;
4871
4872 /* here, we have the cookie name between p1 and p2,
4873 * and its value between p3 and p4.
4874 * we can process it.
4875 */
4876
4877 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004878 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004879 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004880 (p4 - p1 >= t->be->capture_namelen) &&
4881 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004882 int log_len = p4 - p1;
4883
Willy Tarreau086b3b42007-05-13 21:45:51 +02004884 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004885 Alert("HTTP logging : out of memory.\n");
4886 }
4887
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004888 if (log_len > t->be->capture_len)
4889 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004890 memcpy(txn->srv_cookie, p1, log_len);
4891 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004892 }
4893
4894 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004895 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4896 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004897 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004898 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004899
4900 /* If the cookie is in insert mode on a known server, we'll delete
4901 * this occurrence because we'll insert another one later.
4902 * We'll delete it too if the "indirect" option is set and we're in
4903 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004904 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4905 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004906 /* this header must be deleted */
4907 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4908 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4909 txn->hdr_idx.used--;
4910 cur_hdr->len = 0;
4911 cur_next += delta;
4912 txn->rsp.eoh += delta;
4913
Willy Tarreau3d300592007-03-18 18:34:41 +01004914 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004915 }
4916 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004917 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004918 /* replace bytes p3->p4 with the cookie name associated
4919 * with this server since we know it.
4920 */
4921 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4922 cur_hdr->len += delta;
4923 cur_next += delta;
4924 txn->rsp.eoh += delta;
4925
Willy Tarreau3d300592007-03-18 18:34:41 +01004926 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004927 }
4928 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004929 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004930 /* insert the cookie name associated with this server
4931 * before existing cookie, and insert a delimitor between them..
4932 */
4933 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4934 cur_hdr->len += delta;
4935 cur_next += delta;
4936 txn->rsp.eoh += delta;
4937
4938 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004939 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004940 }
4941 }
4942 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004943 else if ((t->be->appsession_name != NULL) &&
4944 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004945
4946 /* Cool... it's the right one */
4947
4948 size_t server_id_len = strlen(t->srv->id) + 1;
4949 asession_temp = &local_asession;
4950
Willy Tarreau63963c62007-05-13 21:29:55 +02004951 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004952 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4953 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4954 return;
4955 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004956 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4957 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004958 asession_temp->serverid = NULL;
4959
4960 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004961 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4962 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004963 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004964 Alert("Not enough Memory process_srv():asession:calloc().\n");
4965 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4966 return;
4967 }
4968 asession_temp->sessid = local_asession.sessid;
4969 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004970 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004971 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4972 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004973 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004974 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004975 }
4976
Willy Tarreaua15645d2007-03-18 16:22:39 +01004977 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004978 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004979 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4980 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4981 return;
4982 }
4983 asession_temp->serverid[0] = '\0';
4984 }
4985
4986 if (asession_temp->serverid[0] == '\0')
4987 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4988
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004989 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004990 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004991#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004992 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004993 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004994#endif
4995 }/* end if ((t->proxy->appsession_name != NULL) ... */
4996 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4997 } /* we're now at the end of the cookie value */
4998
4999 /* keep the link from this header to next one */
5000 old_idx = cur_idx;
5001 } /* end of cookie processing on this header */
5002}
5003
5004
5005
5006/*
5007 * Check if response is cacheable or not. Updates t->flags.
5008 */
5009void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5010{
5011 struct http_txn *txn = &t->txn;
5012 char *p1, *p2;
5013
5014 char *cur_ptr, *cur_end, *cur_next;
5015 int cur_idx;
5016
Willy Tarreau5df51872007-11-25 16:20:08 +01005017 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005018 return;
5019
5020 /* Iterate through the headers.
5021 * we start with the start line.
5022 */
5023 cur_idx = 0;
5024 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5025
5026 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5027 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005028 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005029
5030 cur_hdr = &txn->hdr_idx.v[cur_idx];
5031 cur_ptr = cur_next;
5032 cur_end = cur_ptr + cur_hdr->len;
5033 cur_next = cur_end + cur_hdr->cr + 1;
5034
5035 /* We have one full header between cur_ptr and cur_end, and the
5036 * next header starts at cur_next. We're only interested in
5037 * "Cookie:" headers.
5038 */
5039
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005040 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5041 if (val) {
5042 if ((cur_end - (cur_ptr + val) >= 8) &&
5043 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5044 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5045 return;
5046 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005047 }
5048
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005049 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5050 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005051 continue;
5052
5053 /* OK, right now we know we have a cache-control header at cur_ptr */
5054
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005055 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005056
5057 if (p1 >= cur_end) /* no more info */
5058 continue;
5059
5060 /* p1 is at the beginning of the value */
5061 p2 = p1;
5062
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005063 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005064 p2++;
5065
5066 /* we have a complete value between p1 and p2 */
5067 if (p2 < cur_end && *p2 == '=') {
5068 /* we have something of the form no-cache="set-cookie" */
5069 if ((cur_end - p1 >= 21) &&
5070 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5071 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005072 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005073 continue;
5074 }
5075
5076 /* OK, so we know that either p2 points to the end of string or to a comma */
5077 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5078 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5079 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5080 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005081 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005082 return;
5083 }
5084
5085 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005086 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005087 continue;
5088 }
5089 }
5090}
5091
5092
Willy Tarreau58f10d72006-12-04 02:26:12 +01005093/*
5094 * Try to retrieve a known appsession in the URI, then the associated server.
5095 * If the server is found, it's assigned to the session.
5096 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005097void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005098{
Willy Tarreau3d300592007-03-18 18:34:41 +01005099 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005100 appsess *asession_temp = NULL;
5101 appsess local_asession;
5102 char *request_line;
5103
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005104 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005105 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005106 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005107 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005108 return;
5109
5110 /* skip ';' */
5111 request_line++;
5112
5113 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005114 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005115 return;
5116
5117 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005118 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005119
5120 /* First try if we already have an appsession */
5121 asession_temp = &local_asession;
5122
Willy Tarreau63963c62007-05-13 21:29:55 +02005123 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005124 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5125 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5126 return;
5127 }
5128
5129 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005130 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5131 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005132 asession_temp->serverid = NULL;
5133
5134 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005135 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5136 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005137 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005138 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005139 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005140 Alert("Not enough memory process_cli():asession:calloc().\n");
5141 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5142 return;
5143 }
5144 asession_temp->sessid = local_asession.sessid;
5145 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005146 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005147 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005148 }
5149 else {
5150 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005151 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005152 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005153
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005154 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005155 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005156
Willy Tarreau58f10d72006-12-04 02:26:12 +01005157#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005158 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005159 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005160#endif
5161 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005162 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005163 Alert("Found Application Session without matching server.\n");
5164 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005165 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005166 while (srv) {
5167 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005168 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005169 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005170 txn->flags &= ~TX_CK_MASK;
5171 txn->flags |= TX_CK_VALID;
5172 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005173 t->srv = srv;
5174 break;
5175 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005176 txn->flags &= ~TX_CK_MASK;
5177 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005178 }
5179 }
5180 srv = srv->next;
5181 }
5182 }
5183}
5184
5185
Willy Tarreaub2513902006-12-17 14:52:38 +01005186/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005187 * In a GET or HEAD request, check if the requested URI matches the stats uri
5188 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005189 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005190 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005191 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005192 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005193 *
5194 * Returns 1 if the session's state changes, otherwise 0.
5195 */
5196int stats_check_uri_auth(struct session *t, struct proxy *backend)
5197{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005198 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005199 struct uri_auth *uri_auth = backend->uri_auth;
5200 struct user_auth *user;
5201 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005202 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005203
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005204 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5205
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005206 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005207 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005208 return 0;
5209
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005210 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005211
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005212 /* the URI is in h */
5213 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005214 return 0;
5215
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005216 h += uri_auth->uri_len;
5217 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5218 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005219 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005220 break;
5221 }
5222 h++;
5223 }
5224
5225 if (uri_auth->refresh) {
5226 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5227 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5228 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005229 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005230 break;
5231 }
5232 h++;
5233 }
5234 }
5235
Willy Tarreau55bb8452007-10-17 18:44:57 +02005236 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5237 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5238 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005239 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005240 break;
5241 }
5242 h++;
5243 }
5244
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005245 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5246
Willy Tarreaub2513902006-12-17 14:52:38 +01005247 /* we are in front of a interceptable URI. Let's check
5248 * if there's an authentication and if it's valid.
5249 */
5250 user = uri_auth->users;
5251 if (!user) {
5252 /* no user auth required, it's OK */
5253 authenticated = 1;
5254 } else {
5255 authenticated = 0;
5256
5257 /* a user list is defined, we have to check.
5258 * skip 21 chars for "Authorization: Basic ".
5259 */
5260
5261 /* FIXME: this should move to an earlier place */
5262 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005263 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5264 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5265 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005266 if (len > 14 &&
5267 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005268 txn->auth_hdr.str = h;
5269 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005270 break;
5271 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005272 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005273 }
5274
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005275 if (txn->auth_hdr.len < 21 ||
5276 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005277 user = NULL;
5278
5279 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005280 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5281 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005282 user->user_pwd, user->user_len)) {
5283 authenticated = 1;
5284 break;
5285 }
5286 user = user->next;
5287 }
5288 }
5289
5290 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005291 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005292
5293 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005294 msg.str = trash;
5295 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005296 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005297 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005298 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005299 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005300 if (!(t->flags & SN_ERR_MASK))
5301 t->flags |= SN_ERR_PRXCOND;
5302 if (!(t->flags & SN_FINST_MASK))
5303 t->flags |= SN_FINST_R;
5304 return 1;
5305 }
5306
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005307 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005308 * data.
5309 */
Willy Tarreau72b179a2008-08-28 16:01:32 +02005310 buffer_shutw_now(t->req);
5311 buffer_shutr_now(t->rep);
5312 buffer_start_hijack(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02005313 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005314 t->data_source = DATA_SRC_STATS;
5315 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005316 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005317 produce_content(t);
5318 return 1;
5319}
5320
5321
Willy Tarreaubaaee002006-06-26 02:48:02 +02005322/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005323 * Print a debug line with a header
5324 */
5325void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5326{
5327 int len, max;
5328 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005329 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005330 max = end - start;
5331 UBOUND(max, sizeof(trash) - len - 1);
5332 len += strlcpy2(trash + len, start, max + 1);
5333 trash[len++] = '\n';
5334 write(1, trash, len);
5335}
5336
5337
Willy Tarreau8797c062007-05-07 00:55:35 +02005338/************************************************************************/
5339/* The code below is dedicated to ACL parsing and matching */
5340/************************************************************************/
5341
5342
5343
5344
5345/* 1. Check on METHOD
5346 * We use the pre-parsed method if it is known, and store its number as an
5347 * integer. If it is unknown, we use the pointer and the length.
5348 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005349static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005350{
5351 int len, meth;
5352
Willy Tarreauae8b7962007-06-09 23:10:04 +02005353 len = strlen(*text);
5354 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005355
5356 pattern->val.i = meth;
5357 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005358 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005359 if (!pattern->ptr.str)
5360 return 0;
5361 pattern->len = len;
5362 }
5363 return 1;
5364}
5365
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005366static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005367acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5368 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005369{
5370 int meth;
5371 struct http_txn *txn = l7;
5372
Willy Tarreaub6866442008-07-14 23:54:42 +02005373 if (!txn)
5374 return 0;
5375
Willy Tarreauc11416f2007-06-17 16:58:38 +02005376 if (txn->req.msg_state != HTTP_MSG_BODY)
5377 return 0;
5378
Willy Tarreau8797c062007-05-07 00:55:35 +02005379 meth = txn->meth;
5380 test->i = meth;
5381 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005382 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5383 /* ensure the indexes are not affected */
5384 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005385 test->len = txn->req.sl.rq.m_l;
5386 test->ptr = txn->req.sol;
5387 }
5388 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5389 return 1;
5390}
5391
5392static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5393{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005394 int icase;
5395
Willy Tarreau8797c062007-05-07 00:55:35 +02005396 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005397 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005398
5399 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005400 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005401
5402 /* Other method, we must compare the strings */
5403 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005404 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005405
5406 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5407 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5408 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005409 return ACL_PAT_FAIL;
5410 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005411}
5412
5413/* 2. Check on Request/Status Version
5414 * We simply compare strings here.
5415 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005416static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005417{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005418 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005419 if (!pattern->ptr.str)
5420 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005421 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005422 return 1;
5423}
5424
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005425static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005426acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5427 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005428{
5429 struct http_txn *txn = l7;
5430 char *ptr;
5431 int len;
5432
Willy Tarreaub6866442008-07-14 23:54:42 +02005433 if (!txn)
5434 return 0;
5435
Willy Tarreauc11416f2007-06-17 16:58:38 +02005436 if (txn->req.msg_state != HTTP_MSG_BODY)
5437 return 0;
5438
Willy Tarreau8797c062007-05-07 00:55:35 +02005439 len = txn->req.sl.rq.v_l;
5440 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5441
5442 while ((len-- > 0) && (*ptr++ != '/'));
5443 if (len <= 0)
5444 return 0;
5445
5446 test->ptr = ptr;
5447 test->len = len;
5448
5449 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5450 return 1;
5451}
5452
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005453static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005454acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5455 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005456{
5457 struct http_txn *txn = l7;
5458 char *ptr;
5459 int len;
5460
Willy Tarreaub6866442008-07-14 23:54:42 +02005461 if (!txn)
5462 return 0;
5463
Willy Tarreauc11416f2007-06-17 16:58:38 +02005464 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5465 return 0;
5466
Willy Tarreau8797c062007-05-07 00:55:35 +02005467 len = txn->rsp.sl.st.v_l;
5468 ptr = txn->rsp.sol;
5469
5470 while ((len-- > 0) && (*ptr++ != '/'));
5471 if (len <= 0)
5472 return 0;
5473
5474 test->ptr = ptr;
5475 test->len = len;
5476
5477 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5478 return 1;
5479}
5480
5481/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005482static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005483acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5484 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005485{
5486 struct http_txn *txn = l7;
5487 char *ptr;
5488 int len;
5489
Willy Tarreaub6866442008-07-14 23:54:42 +02005490 if (!txn)
5491 return 0;
5492
Willy Tarreauc11416f2007-06-17 16:58:38 +02005493 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5494 return 0;
5495
Willy Tarreau8797c062007-05-07 00:55:35 +02005496 len = txn->rsp.sl.st.c_l;
5497 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5498
5499 test->i = __strl2ui(ptr, len);
5500 test->flags = ACL_TEST_F_VOL_1ST;
5501 return 1;
5502}
5503
5504/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005505static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005506acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5507 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005508{
5509 struct http_txn *txn = l7;
5510
Willy Tarreaub6866442008-07-14 23:54:42 +02005511 if (!txn)
5512 return 0;
5513
Willy Tarreauc11416f2007-06-17 16:58:38 +02005514 if (txn->req.msg_state != HTTP_MSG_BODY)
5515 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005516
Willy Tarreauc11416f2007-06-17 16:58:38 +02005517 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5518 /* ensure the indexes are not affected */
5519 return 0;
5520
Willy Tarreau8797c062007-05-07 00:55:35 +02005521 test->len = txn->req.sl.rq.u_l;
5522 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5523
Willy Tarreauf3d25982007-05-08 22:45:09 +02005524 /* we do not need to set READ_ONLY because the data is in a buffer */
5525 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005526 return 1;
5527}
5528
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005529static int
5530acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5531 struct acl_expr *expr, struct acl_test *test)
5532{
5533 struct http_txn *txn = l7;
5534
Willy Tarreaub6866442008-07-14 23:54:42 +02005535 if (!txn)
5536 return 0;
5537
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005538 if (txn->req.msg_state != HTTP_MSG_BODY)
5539 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005540
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005541 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5542 /* ensure the indexes are not affected */
5543 return 0;
5544
5545 /* Parse HTTP request */
5546 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5547 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5548 test->i = AF_INET;
5549
5550 /*
5551 * If we are parsing url in frontend space, we prepare backend stage
5552 * to not parse again the same url ! optimization lazyness...
5553 */
5554 if (px->options & PR_O_HTTP_PROXY)
5555 l4->flags |= SN_ADDR_SET;
5556
5557 test->flags = ACL_TEST_F_READ_ONLY;
5558 return 1;
5559}
5560
5561static int
5562acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5563 struct acl_expr *expr, struct acl_test *test)
5564{
5565 struct http_txn *txn = l7;
5566
Willy Tarreaub6866442008-07-14 23:54:42 +02005567 if (!txn)
5568 return 0;
5569
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005570 if (txn->req.msg_state != HTTP_MSG_BODY)
5571 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005572
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005573 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5574 /* ensure the indexes are not affected */
5575 return 0;
5576
5577 /* Same optimization as url_ip */
5578 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5579 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5580
5581 if (px->options & PR_O_HTTP_PROXY)
5582 l4->flags |= SN_ADDR_SET;
5583
5584 test->flags = ACL_TEST_F_READ_ONLY;
5585 return 1;
5586}
5587
Willy Tarreauc11416f2007-06-17 16:58:38 +02005588/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5589 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5590 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005591static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005592acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005593 struct acl_expr *expr, struct acl_test *test)
5594{
5595 struct http_txn *txn = l7;
5596 struct hdr_idx *idx = &txn->hdr_idx;
5597 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005598
Willy Tarreaub6866442008-07-14 23:54:42 +02005599 if (!txn)
5600 return 0;
5601
Willy Tarreau33a7e692007-06-10 19:45:56 +02005602 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5603 /* search for header from the beginning */
5604 ctx->idx = 0;
5605
Willy Tarreau33a7e692007-06-10 19:45:56 +02005606 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5607 test->flags |= ACL_TEST_F_FETCH_MORE;
5608 test->flags |= ACL_TEST_F_VOL_HDR;
5609 test->len = ctx->vlen;
5610 test->ptr = (char *)ctx->line + ctx->val;
5611 return 1;
5612 }
5613
5614 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5615 test->flags |= ACL_TEST_F_VOL_HDR;
5616 return 0;
5617}
5618
Willy Tarreau33a7e692007-06-10 19:45:56 +02005619static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005620acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5621 struct acl_expr *expr, struct acl_test *test)
5622{
5623 struct http_txn *txn = l7;
5624
Willy Tarreaub6866442008-07-14 23:54:42 +02005625 if (!txn)
5626 return 0;
5627
Willy Tarreauc11416f2007-06-17 16:58:38 +02005628 if (txn->req.msg_state != HTTP_MSG_BODY)
5629 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005630
Willy Tarreauc11416f2007-06-17 16:58:38 +02005631 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5632 /* ensure the indexes are not affected */
5633 return 0;
5634
5635 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5636}
5637
5638static int
5639acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5640 struct acl_expr *expr, struct acl_test *test)
5641{
5642 struct http_txn *txn = l7;
5643
Willy Tarreaub6866442008-07-14 23:54:42 +02005644 if (!txn)
5645 return 0;
5646
Willy Tarreauc11416f2007-06-17 16:58:38 +02005647 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5648 return 0;
5649
5650 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5651}
5652
5653/* 6. Check on HTTP header count. The number of occurrences is returned.
5654 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5655 */
5656static int
5657acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005658 struct acl_expr *expr, struct acl_test *test)
5659{
5660 struct http_txn *txn = l7;
5661 struct hdr_idx *idx = &txn->hdr_idx;
5662 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005663 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005664
Willy Tarreaub6866442008-07-14 23:54:42 +02005665 if (!txn)
5666 return 0;
5667
Willy Tarreau33a7e692007-06-10 19:45:56 +02005668 ctx.idx = 0;
5669 cnt = 0;
5670 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5671 cnt++;
5672
5673 test->i = cnt;
5674 test->flags = ACL_TEST_F_VOL_HDR;
5675 return 1;
5676}
5677
Willy Tarreauc11416f2007-06-17 16:58:38 +02005678static int
5679acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5680 struct acl_expr *expr, struct acl_test *test)
5681{
5682 struct http_txn *txn = l7;
5683
Willy Tarreaub6866442008-07-14 23:54:42 +02005684 if (!txn)
5685 return 0;
5686
Willy Tarreauc11416f2007-06-17 16:58:38 +02005687 if (txn->req.msg_state != HTTP_MSG_BODY)
5688 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005689
Willy Tarreauc11416f2007-06-17 16:58:38 +02005690 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5691 /* ensure the indexes are not affected */
5692 return 0;
5693
5694 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5695}
5696
5697static int
5698acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5699 struct acl_expr *expr, struct acl_test *test)
5700{
5701 struct http_txn *txn = l7;
5702
Willy Tarreaub6866442008-07-14 23:54:42 +02005703 if (!txn)
5704 return 0;
5705
Willy Tarreauc11416f2007-06-17 16:58:38 +02005706 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5707 return 0;
5708
5709 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5710}
5711
Willy Tarreau33a7e692007-06-10 19:45:56 +02005712/* 7. Check on HTTP header's integer value. The integer value is returned.
5713 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005714 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005715 */
5716static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005717acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005718 struct acl_expr *expr, struct acl_test *test)
5719{
5720 struct http_txn *txn = l7;
5721 struct hdr_idx *idx = &txn->hdr_idx;
5722 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005723
Willy Tarreaub6866442008-07-14 23:54:42 +02005724 if (!txn)
5725 return 0;
5726
Willy Tarreau33a7e692007-06-10 19:45:56 +02005727 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5728 /* search for header from the beginning */
5729 ctx->idx = 0;
5730
Willy Tarreau33a7e692007-06-10 19:45:56 +02005731 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5732 test->flags |= ACL_TEST_F_FETCH_MORE;
5733 test->flags |= ACL_TEST_F_VOL_HDR;
5734 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5735 return 1;
5736 }
5737
5738 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5739 test->flags |= ACL_TEST_F_VOL_HDR;
5740 return 0;
5741}
5742
Willy Tarreauc11416f2007-06-17 16:58:38 +02005743static int
5744acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5745 struct acl_expr *expr, struct acl_test *test)
5746{
5747 struct http_txn *txn = l7;
5748
Willy Tarreaub6866442008-07-14 23:54:42 +02005749 if (!txn)
5750 return 0;
5751
Willy Tarreauc11416f2007-06-17 16:58:38 +02005752 if (txn->req.msg_state != HTTP_MSG_BODY)
5753 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005754
Willy Tarreauc11416f2007-06-17 16:58:38 +02005755 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5756 /* ensure the indexes are not affected */
5757 return 0;
5758
5759 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5760}
5761
5762static int
5763acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5764 struct acl_expr *expr, struct acl_test *test)
5765{
5766 struct http_txn *txn = l7;
5767
Willy Tarreaub6866442008-07-14 23:54:42 +02005768 if (!txn)
5769 return 0;
5770
Willy Tarreauc11416f2007-06-17 16:58:38 +02005771 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5772 return 0;
5773
5774 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5775}
5776
Willy Tarreau737b0c12007-06-10 21:28:46 +02005777/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5778 * the first '/' after the possible hostname, and ends before the possible '?'.
5779 */
5780static int
5781acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5782 struct acl_expr *expr, struct acl_test *test)
5783{
5784 struct http_txn *txn = l7;
5785 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005786
Willy Tarreaub6866442008-07-14 23:54:42 +02005787 if (!txn)
5788 return 0;
5789
Willy Tarreauc11416f2007-06-17 16:58:38 +02005790 if (txn->req.msg_state != HTTP_MSG_BODY)
5791 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005792
Willy Tarreauc11416f2007-06-17 16:58:38 +02005793 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5794 /* ensure the indexes are not affected */
5795 return 0;
5796
Willy Tarreau21d2af32008-02-14 20:25:24 +01005797 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5798 ptr = http_get_path(txn);
5799 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005800 return 0;
5801
5802 /* OK, we got the '/' ! */
5803 test->ptr = ptr;
5804
5805 while (ptr < end && *ptr != '?')
5806 ptr++;
5807
5808 test->len = ptr - test->ptr;
5809
5810 /* we do not need to set READ_ONLY because the data is in a buffer */
5811 test->flags = ACL_TEST_F_VOL_1ST;
5812 return 1;
5813}
5814
5815
Willy Tarreau8797c062007-05-07 00:55:35 +02005816
5817/************************************************************************/
5818/* All supported keywords must be declared here. */
5819/************************************************************************/
5820
5821/* Note: must not be declared <const> as its list will be overwritten */
5822static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005823 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5824 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5825 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5826 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005827
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005828 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5829 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5830 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5831 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5832 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5833 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5834 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5835 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5836 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005837
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005838 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5839 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5840 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5841 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5842 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5843 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5844 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5845 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5846 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5847 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005848
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005849 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5850 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5851 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5852 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5853 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5854 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5855 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5856 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5857 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005858
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005859 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5860 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5861 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5862 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5863 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5864 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5865 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005866
Willy Tarreauf3d25982007-05-08 22:45:09 +02005867 { NULL, NULL, NULL, NULL },
5868
5869#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005870 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5871 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5872 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5873 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5874 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5875 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5876 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5877
Willy Tarreau8797c062007-05-07 00:55:35 +02005878 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5879 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5880 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5881 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5882 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5883 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5884 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5885 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5886
5887 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5888 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5889 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5890 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5891 { NULL, NULL, NULL, NULL },
5892#endif
5893}};
5894
5895
5896__attribute__((constructor))
5897static void __http_protocol_init(void)
5898{
5899 acl_register_keywords(&acl_kws);
5900}
5901
5902
Willy Tarreau58f10d72006-12-04 02:26:12 +01005903/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005904 * Local variables:
5905 * c-indent-level: 8
5906 * c-basic-offset: 8
5907 * End:
5908 */