blob: e8bd63142ba0cab1c9a9c11a7d444f3fe09ea268 [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 Tarreauadfb8562008-08-11 15:24:42 +0200547 t->rep->flags |= BF_MAY_FORWARD;
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 Tarreaufa7e1022008-10-19 07:30:41 +0200722 if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_MAY_FORWARD)
723 process_srv_conn(s);
724
725 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreau376580a2008-08-27 18:52:22 +0200726 if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_MAY_FORWARD)) == (BF_EMPTY|BF_MAY_FORWARD) &&
727 s->be->options & PR_O_FORCE_CLO &&
728 s->rep->flags & BF_READ_STATUS) {
729 /* 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;
768 s->req->flags |= BF_MAY_FORWARD;
769 if (s->req->analysers)
770 process_request(s);
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200771 }
772 rqf_req = s->req->flags;
773 }
774
775 if ((rpf_rep ^ s->rep->flags) & BF_MASK_ANALYSER) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200776 /* the analysers must block it themselves */
Willy Tarreau36e6a412008-08-28 11:07:58 +0200777 if (s->rep->prod->state >= SI_ST_EST) {
778 resync = 1;
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200779 s->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau36e6a412008-08-28 11:07:58 +0200780 if (s->rep->analysers)
781 process_response(s);
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200782 }
783 rpf_rep = s->rep->flags;
Willy Tarreaudafde432008-08-17 01:00:46 +0200784 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200785
Willy Tarreaudafde432008-08-17 01:00:46 +0200786 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200788 if (likely((s->rep->cons->state != SI_ST_CLO) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200789 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100790
791 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
792 session_process_counters(s);
793
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200794 if (s->rep->cons->state == SI_ST_EST)
795 stream_sock_data_finish(s->rep->cons->fd);
796
797 if (s->req->cons->state == SI_ST_EST)
798 stream_sock_data_finish(s->req->cons->fd);
799
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200800 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
801 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200802
Willy Tarreau7f875f62008-08-11 17:35:01 +0200803 /* Trick: if a request is being waiting for the server to respond,
804 * and if we know the server can timeout, we don't want the timeout
805 * to expire on the client side first, but we're still interested
806 * in passing data from the client to the server (eg: POST). Thus,
807 * we can cancel the client's request timeout if the server's
808 * request timeout is set and the server has not yet sent a response.
809 */
810
Willy Tarreauba392ce2008-08-16 21:13:23 +0200811 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200812 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200813 s->req->rex = TICK_ETERNITY;
814
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200815 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
816 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +0200817 if (s->req->analysers)
818 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819
820 /* restore t to its place in the task list */
821 task_queue(t);
822
Willy Tarreaua7c52762008-08-16 18:40:18 +0200823#ifdef DEBUG_DEV
824 /* this may only happen when no timeout is set or in case of an FSM bug */
825 if (!t->expire)
826 ABORT_NOW();
827#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200828 *next = t->expire;
829 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200830 }
831
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100832 s->fe->feconn--;
833 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200834 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200835 actconn--;
836
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200837 if (unlikely((global.mode & MODE_DEBUG) &&
838 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200840 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200841 s->uniq_id, s->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200842 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200843 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 write(1, trash, len);
845 }
846
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200847 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100848 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849
850 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200851 if (s->logs.logwait &&
852 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200853 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
854 if (s->fe->to_log & LW_REQ)
855 http_sess_log(s);
856 else
857 tcp_sess_log(s);
858 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200859
860 /* the task MUST not be in the run queue anymore */
861 task_delete(t);
862 session_free(s);
863 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200864 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200865}
866
867
Willy Tarreau42250582007-04-01 01:30:43 +0200868extern const char sess_term_cond[8];
869extern const char sess_fin_state[8];
870extern const char *monthname[12];
871const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
872const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
873 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
874 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200875struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200876struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100877
Willy Tarreau42250582007-04-01 01:30:43 +0200878/*
879 * send a log for the session when we have enough info about it.
880 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100881 */
Willy Tarreau42250582007-04-01 01:30:43 +0200882static void http_sess_log(struct session *s)
883{
884 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
885 struct proxy *fe = s->fe;
886 struct proxy *be = s->be;
887 struct proxy *prx_log;
888 struct http_txn *txn = &s->txn;
889 int tolog;
890 char *uri, *h;
891 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200892 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200893 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200894 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200895 int hdr;
896
897 if (fe->logfac1 < 0 && fe->logfac2 < 0)
898 return;
899 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100900
Willy Tarreau42250582007-04-01 01:30:43 +0200901 if (s->cli_addr.ss_family == AF_INET)
902 inet_ntop(AF_INET,
903 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
904 pn, sizeof(pn));
905 else
906 inet_ntop(AF_INET6,
907 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
908 pn, sizeof(pn));
909
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200910 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200911
912 /* FIXME: let's limit ourselves to frontend logging for now. */
913 tolog = fe->to_log;
914
915 h = tmpline;
916 if (fe->to_log & LW_REQHDR &&
917 txn->req.cap &&
918 (h < tmpline + sizeof(tmpline) - 10)) {
919 *(h++) = ' ';
920 *(h++) = '{';
921 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
922 if (hdr)
923 *(h++) = '|';
924 if (txn->req.cap[hdr] != NULL)
925 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
926 '#', hdr_encode_map, txn->req.cap[hdr]);
927 }
928 *(h++) = '}';
929 }
930
931 if (fe->to_log & LW_RSPHDR &&
932 txn->rsp.cap &&
933 (h < tmpline + sizeof(tmpline) - 7)) {
934 *(h++) = ' ';
935 *(h++) = '{';
936 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
937 if (hdr)
938 *(h++) = '|';
939 if (txn->rsp.cap[hdr] != NULL)
940 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
941 '#', hdr_encode_map, txn->rsp.cap[hdr]);
942 }
943 *(h++) = '}';
944 }
945
946 if (h < tmpline + sizeof(tmpline) - 4) {
947 *(h++) = ' ';
948 *(h++) = '"';
949 uri = txn->uri ? txn->uri : "<BADREQ>";
950 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
951 '#', url_encode_map, uri);
952 *(h++) = '"';
953 }
954 *h = '\0';
955
956 svid = (tolog & LW_SVID) ?
957 (s->data_source != DATA_SRC_STATS) ?
958 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
959
Willy Tarreau70089872008-06-13 21:12:51 +0200960 t_request = -1;
961 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
962 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
963
Willy Tarreau42250582007-04-01 01:30:43 +0200964 send_log(prx_log, LOG_INFO,
965 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
966 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100967 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200968 pn,
969 (s->cli_addr.ss_family == AF_INET) ?
970 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
971 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200972 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200973 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200974 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200975 t_request,
976 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200977 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
978 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
979 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
980 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100981 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200982 txn->cli_cookie ? txn->cli_cookie : "-",
983 txn->srv_cookie ? txn->srv_cookie : "-",
984 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
985 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
986 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
987 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
988 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100989 (s->flags & SN_REDISP)?"+":"",
990 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200991 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
992
993 s->logs.logwait = 0;
994}
995
Willy Tarreau117f59e2007-03-04 18:17:17 +0100996
997/*
998 * Capture headers from message starting at <som> according to header list
999 * <cap_hdr>, and fill the <idx> structure appropriately.
1000 */
1001void capture_headers(char *som, struct hdr_idx *idx,
1002 char **cap, struct cap_hdr *cap_hdr)
1003{
1004 char *eol, *sol, *col, *sov;
1005 int cur_idx;
1006 struct cap_hdr *h;
1007 int len;
1008
1009 sol = som + hdr_idx_first_pos(idx);
1010 cur_idx = hdr_idx_first_idx(idx);
1011
1012 while (cur_idx) {
1013 eol = sol + idx->v[cur_idx].len;
1014
1015 col = sol;
1016 while (col < eol && *col != ':')
1017 col++;
1018
1019 sov = col + 1;
1020 while (sov < eol && http_is_lws[(unsigned char)*sov])
1021 sov++;
1022
1023 for (h = cap_hdr; h; h = h->next) {
1024 if ((h->namelen == col - sol) &&
1025 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1026 if (cap[h->index] == NULL)
1027 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001028 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001029
1030 if (cap[h->index] == NULL) {
1031 Alert("HTTP capture : out of memory.\n");
1032 continue;
1033 }
1034
1035 len = eol - sov;
1036 if (len > h->len)
1037 len = h->len;
1038
1039 memcpy(cap[h->index], sov, len);
1040 cap[h->index][len]=0;
1041 }
1042 }
1043 sol = eol + idx->v[cur_idx].cr + 1;
1044 cur_idx = idx->v[cur_idx].next;
1045 }
1046}
1047
1048
Willy Tarreau42250582007-04-01 01:30:43 +02001049/* either we find an LF at <ptr> or we jump to <bad>.
1050 */
1051#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1052
1053/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1054 * otherwise to <http_msg_ood> with <state> set to <st>.
1055 */
1056#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1057 ptr++; \
1058 if (likely(ptr < end)) \
1059 goto good; \
1060 else { \
1061 state = (st); \
1062 goto http_msg_ood; \
1063 } \
1064 } while (0)
1065
1066
Willy Tarreaubaaee002006-06-26 02:48:02 +02001067/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001068 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001069 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1070 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1071 * will give undefined results.
1072 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1073 * and that msg->sol points to the beginning of the response.
1074 * If a complete line is found (which implies that at least one CR or LF is
1075 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1076 * returned indicating an incomplete line (which does not mean that parts have
1077 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1078 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1079 * upon next call.
1080 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001081 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001082 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1083 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001084 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001085 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001086const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1087 unsigned int state, const char *ptr, const char *end,
1088 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001089{
1090 __label__
1091 http_msg_rpver,
1092 http_msg_rpver_sp,
1093 http_msg_rpcode,
1094 http_msg_rpcode_sp,
1095 http_msg_rpreason,
1096 http_msg_rpline_eol,
1097 http_msg_ood, /* out of data */
1098 http_msg_invalid;
1099
1100 switch (state) {
1101 http_msg_rpver:
1102 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001103 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001104 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1105
1106 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001107 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001108 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1109 }
1110 goto http_msg_invalid;
1111
1112 http_msg_rpver_sp:
1113 case HTTP_MSG_RPVER_SP:
1114 if (likely(!HTTP_IS_LWS(*ptr))) {
1115 msg->sl.st.c = ptr - msg_buf;
1116 goto http_msg_rpcode;
1117 }
1118 if (likely(HTTP_IS_SPHT(*ptr)))
1119 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1120 /* so it's a CR/LF, this is invalid */
1121 goto http_msg_invalid;
1122
1123 http_msg_rpcode:
1124 case HTTP_MSG_RPCODE:
1125 if (likely(!HTTP_IS_LWS(*ptr)))
1126 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1127
1128 if (likely(HTTP_IS_SPHT(*ptr))) {
1129 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1130 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1131 }
1132
1133 /* so it's a CR/LF, so there is no reason phrase */
1134 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1135 http_msg_rsp_reason:
1136 /* FIXME: should we support HTTP responses without any reason phrase ? */
1137 msg->sl.st.r = ptr - msg_buf;
1138 msg->sl.st.r_l = 0;
1139 goto http_msg_rpline_eol;
1140
1141 http_msg_rpcode_sp:
1142 case HTTP_MSG_RPCODE_SP:
1143 if (likely(!HTTP_IS_LWS(*ptr))) {
1144 msg->sl.st.r = ptr - msg_buf;
1145 goto http_msg_rpreason;
1146 }
1147 if (likely(HTTP_IS_SPHT(*ptr)))
1148 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1149 /* so it's a CR/LF, so there is no reason phrase */
1150 goto http_msg_rsp_reason;
1151
1152 http_msg_rpreason:
1153 case HTTP_MSG_RPREASON:
1154 if (likely(!HTTP_IS_CRLF(*ptr)))
1155 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1156 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1157 http_msg_rpline_eol:
1158 /* We have seen the end of line. Note that we do not
1159 * necessarily have the \n yet, but at least we know that we
1160 * have EITHER \r OR \n, otherwise the response would not be
1161 * complete. We can then record the response length and return
1162 * to the caller which will be able to register it.
1163 */
1164 msg->sl.st.l = ptr - msg->sol;
1165 return ptr;
1166
1167#ifdef DEBUG_FULL
1168 default:
1169 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1170 exit(1);
1171#endif
1172 }
1173
1174 http_msg_ood:
1175 /* out of data */
1176 if (ret_state)
1177 *ret_state = state;
1178 if (ret_ptr)
1179 *ret_ptr = (char *)ptr;
1180 return NULL;
1181
1182 http_msg_invalid:
1183 /* invalid message */
1184 if (ret_state)
1185 *ret_state = HTTP_MSG_ERROR;
1186 return NULL;
1187}
1188
1189
1190/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001191 * This function parses a request line between <ptr> and <end>, starting with
1192 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1193 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1194 * will give undefined results.
1195 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1196 * and that msg->sol points to the beginning of the request.
1197 * If a complete line is found (which implies that at least one CR or LF is
1198 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1199 * returned indicating an incomplete line (which does not mean that parts have
1200 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1201 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1202 * upon next call.
1203 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001204 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001205 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1206 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001207 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001208 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001209const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1210 unsigned int state, const char *ptr, const char *end,
1211 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001212{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001213 __label__
1214 http_msg_rqmeth,
1215 http_msg_rqmeth_sp,
1216 http_msg_rquri,
1217 http_msg_rquri_sp,
1218 http_msg_rqver,
1219 http_msg_rqline_eol,
1220 http_msg_ood, /* out of data */
1221 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001222
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 switch (state) {
1224 http_msg_rqmeth:
1225 case HTTP_MSG_RQMETH:
1226 if (likely(HTTP_IS_TOKEN(*ptr)))
1227 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001228
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001230 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1232 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 if (likely(HTTP_IS_CRLF(*ptr))) {
1235 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001236 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 http_msg_req09_uri:
1238 msg->sl.rq.u = ptr - msg_buf;
1239 http_msg_req09_uri_e:
1240 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1241 http_msg_req09_ver:
1242 msg->sl.rq.v = ptr - msg_buf;
1243 msg->sl.rq.v_l = 0;
1244 goto http_msg_rqline_eol;
1245 }
1246 goto http_msg_invalid;
1247
1248 http_msg_rqmeth_sp:
1249 case HTTP_MSG_RQMETH_SP:
1250 if (likely(!HTTP_IS_LWS(*ptr))) {
1251 msg->sl.rq.u = ptr - msg_buf;
1252 goto http_msg_rquri;
1253 }
1254 if (likely(HTTP_IS_SPHT(*ptr)))
1255 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1256 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1257 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259 http_msg_rquri:
1260 case HTTP_MSG_RQURI:
1261 if (likely(!HTTP_IS_LWS(*ptr)))
1262 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 if (likely(HTTP_IS_SPHT(*ptr))) {
1265 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1266 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1267 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1270 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 http_msg_rquri_sp:
1273 case HTTP_MSG_RQURI_SP:
1274 if (likely(!HTTP_IS_LWS(*ptr))) {
1275 msg->sl.rq.v = ptr - msg_buf;
1276 goto http_msg_rqver;
1277 }
1278 if (likely(HTTP_IS_SPHT(*ptr)))
1279 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1280 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1281 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 http_msg_rqver:
1284 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001285 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001287
1288 if (likely(HTTP_IS_CRLF(*ptr))) {
1289 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1290 http_msg_rqline_eol:
1291 /* We have seen the end of line. Note that we do not
1292 * necessarily have the \n yet, but at least we know that we
1293 * have EITHER \r OR \n, otherwise the request would not be
1294 * complete. We can then record the request length and return
1295 * to the caller which will be able to register it.
1296 */
1297 msg->sl.rq.l = ptr - msg->sol;
1298 return ptr;
1299 }
1300
1301 /* neither an HTTP_VER token nor a CRLF */
1302 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304#ifdef DEBUG_FULL
1305 default:
1306 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1307 exit(1);
1308#endif
1309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 http_msg_ood:
1312 /* out of data */
1313 if (ret_state)
1314 *ret_state = state;
1315 if (ret_ptr)
1316 *ret_ptr = (char *)ptr;
1317 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 http_msg_invalid:
1320 /* invalid message */
1321 if (ret_state)
1322 *ret_state = HTTP_MSG_ERROR;
1323 return NULL;
1324}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001325
1326
Willy Tarreau8973c702007-01-21 23:58:29 +01001327/*
1328 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001329 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 * when data are missing and recalled at the exact same location with no
1331 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001332 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1333 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1336{
1337 __label__
1338 http_msg_rqbefore,
1339 http_msg_rqbefore_cr,
1340 http_msg_rqmeth,
1341 http_msg_rqline_end,
1342 http_msg_hdr_first,
1343 http_msg_hdr_name,
1344 http_msg_hdr_l1_sp,
1345 http_msg_hdr_l1_lf,
1346 http_msg_hdr_l1_lws,
1347 http_msg_hdr_val,
1348 http_msg_hdr_l2_lf,
1349 http_msg_hdr_l2_lws,
1350 http_msg_complete_header,
1351 http_msg_last_lf,
1352 http_msg_ood, /* out of data */
1353 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001354
Willy Tarreaue69eada2008-01-27 00:34:10 +01001355 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001357
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001358 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001359 ptr = buf->lr;
1360 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 if (unlikely(ptr >= end))
1363 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001364
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 /*
1367 * First, states that are specific to the response only.
1368 * We check them first so that request and headers are
1369 * closer to each other (accessed more often).
1370 */
1371 http_msg_rpbefore:
1372 case HTTP_MSG_RPBEFORE:
1373 if (likely(HTTP_IS_TOKEN(*ptr))) {
1374 if (likely(ptr == buf->data)) {
1375 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001376 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 } else {
1378#if PARSE_PRESERVE_EMPTY_LINES
1379 /* only skip empty leading lines, don't remove them */
1380 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001381 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001382#else
1383 /* Remove empty leading lines, as recommended by
1384 * RFC2616. This takes a lot of time because we
1385 * must move all the buffer backwards, but this
1386 * is rarely needed. The method above will be
1387 * cleaner when we'll be able to start sending
1388 * the request from any place in the buffer.
1389 */
1390 buf->lr = ptr;
1391 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001392 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001393 msg->sol = buf->data;
1394 ptr = buf->data;
1395 end = buf->r;
1396#endif
1397 }
1398 hdr_idx_init(idx);
1399 state = HTTP_MSG_RPVER;
1400 goto http_msg_rpver;
1401 }
1402
1403 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1404 goto http_msg_invalid;
1405
1406 if (unlikely(*ptr == '\n'))
1407 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1408 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1409 /* stop here */
1410
1411 http_msg_rpbefore_cr:
1412 case HTTP_MSG_RPBEFORE_CR:
1413 EXPECT_LF_HERE(ptr, http_msg_invalid);
1414 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1415 /* stop here */
1416
1417 http_msg_rpver:
1418 case HTTP_MSG_RPVER:
1419 case HTTP_MSG_RPVER_SP:
1420 case HTTP_MSG_RPCODE:
1421 case HTTP_MSG_RPCODE_SP:
1422 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001423 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001424 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001425 if (unlikely(!ptr))
1426 return;
1427
1428 /* we have a full response and we know that we have either a CR
1429 * or an LF at <ptr>.
1430 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001431 //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 +01001432 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1433
1434 msg->sol = ptr;
1435 if (likely(*ptr == '\r'))
1436 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1437 goto http_msg_rpline_end;
1438
1439 http_msg_rpline_end:
1440 case HTTP_MSG_RPLINE_END:
1441 /* msg->sol must point to the first of CR or LF. */
1442 EXPECT_LF_HERE(ptr, http_msg_invalid);
1443 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1444 /* stop here */
1445
1446 /*
1447 * Second, states that are specific to the request only
1448 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 http_msg_rqbefore:
1450 case HTTP_MSG_RQBEFORE:
1451 if (likely(HTTP_IS_TOKEN(*ptr))) {
1452 if (likely(ptr == buf->data)) {
1453 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001454 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 } else {
1456#if PARSE_PRESERVE_EMPTY_LINES
1457 /* only skip empty leading lines, don't remove them */
1458 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001459 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001460#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 /* Remove empty leading lines, as recommended by
1462 * RFC2616. This takes a lot of time because we
1463 * must move all the buffer backwards, but this
1464 * is rarely needed. The method above will be
1465 * cleaner when we'll be able to start sending
1466 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001467 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 buf->lr = ptr;
1469 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001470 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 msg->sol = buf->data;
1472 ptr = buf->data;
1473 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001474#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001476 /* we will need this when keep-alive will be supported
1477 hdr_idx_init(idx);
1478 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 state = HTTP_MSG_RQMETH;
1480 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1484 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 if (unlikely(*ptr == '\n'))
1487 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1488 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001489 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 http_msg_rqbefore_cr:
1492 case HTTP_MSG_RQBEFORE_CR:
1493 EXPECT_LF_HERE(ptr, http_msg_invalid);
1494 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001495 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 http_msg_rqmeth:
1498 case HTTP_MSG_RQMETH:
1499 case HTTP_MSG_RQMETH_SP:
1500 case HTTP_MSG_RQURI:
1501 case HTTP_MSG_RQURI_SP:
1502 case HTTP_MSG_RQVER:
1503 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001504 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 if (unlikely(!ptr))
1506 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 /* we have a full request and we know that we have either a CR
1509 * or an LF at <ptr>.
1510 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001511 //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 +01001512 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 msg->sol = ptr;
1515 if (likely(*ptr == '\r'))
1516 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 http_msg_rqline_end:
1520 case HTTP_MSG_RQLINE_END:
1521 /* check for HTTP/0.9 request : no version information available.
1522 * msg->sol must point to the first of CR or LF.
1523 */
1524 if (unlikely(msg->sl.rq.v_l == 0))
1525 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 EXPECT_LF_HERE(ptr, http_msg_invalid);
1528 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001530
Willy Tarreau8973c702007-01-21 23:58:29 +01001531 /*
1532 * Common states below
1533 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 http_msg_hdr_first:
1535 case HTTP_MSG_HDR_FIRST:
1536 msg->sol = ptr;
1537 if (likely(!HTTP_IS_CRLF(*ptr))) {
1538 goto http_msg_hdr_name;
1539 }
1540
1541 if (likely(*ptr == '\r'))
1542 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1543 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001544
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 http_msg_hdr_name:
1546 case HTTP_MSG_HDR_NAME:
1547 /* assumes msg->sol points to the first char */
1548 if (likely(HTTP_IS_TOKEN(*ptr)))
1549 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 if (likely(*ptr == ':')) {
1552 msg->col = ptr - buf->data;
1553 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1554 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001557
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 http_msg_hdr_l1_sp:
1559 case HTTP_MSG_HDR_L1_SP:
1560 /* assumes msg->sol points to the first char and msg->col to the colon */
1561 if (likely(HTTP_IS_SPHT(*ptr)))
1562 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 /* header value can be basically anything except CR/LF */
1565 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (likely(!HTTP_IS_CRLF(*ptr))) {
1568 goto http_msg_hdr_val;
1569 }
1570
1571 if (likely(*ptr == '\r'))
1572 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1573 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 http_msg_hdr_l1_lf:
1576 case HTTP_MSG_HDR_L1_LF:
1577 EXPECT_LF_HERE(ptr, http_msg_invalid);
1578 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 http_msg_hdr_l1_lws:
1581 case HTTP_MSG_HDR_L1_LWS:
1582 if (likely(HTTP_IS_SPHT(*ptr))) {
1583 /* replace HT,CR,LF with spaces */
1584 for (; buf->data+msg->sov < ptr; msg->sov++)
1585 buf->data[msg->sov] = ' ';
1586 goto http_msg_hdr_l1_sp;
1587 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001588 /* we had a header consisting only in spaces ! */
1589 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_complete_header;
1591
1592 http_msg_hdr_val:
1593 case HTTP_MSG_HDR_VAL:
1594 /* assumes msg->sol points to the first char, msg->col to the
1595 * colon, and msg->sov points to the first character of the
1596 * value.
1597 */
1598 if (likely(!HTTP_IS_CRLF(*ptr)))
1599 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 msg->eol = ptr;
1602 /* Note: we could also copy eol into ->eoh so that we have the
1603 * real header end in case it ends with lots of LWS, but is this
1604 * really needed ?
1605 */
1606 if (likely(*ptr == '\r'))
1607 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1608 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 http_msg_hdr_l2_lf:
1611 case HTTP_MSG_HDR_L2_LF:
1612 EXPECT_LF_HERE(ptr, http_msg_invalid);
1613 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 http_msg_hdr_l2_lws:
1616 case HTTP_MSG_HDR_L2_LWS:
1617 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1618 /* LWS: replace HT,CR,LF with spaces */
1619 for (; msg->eol < ptr; msg->eol++)
1620 *msg->eol = ' ';
1621 goto http_msg_hdr_val;
1622 }
1623 http_msg_complete_header:
1624 /*
1625 * It was a new header, so the last one is finished.
1626 * Assumes msg->sol points to the first char, msg->col to the
1627 * colon, msg->sov points to the first character of the value
1628 * and msg->eol to the first CR or LF so we know how the line
1629 * ends. We insert last header into the index.
1630 */
1631 /*
1632 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1633 write(2, msg->sol, msg->eol-msg->sol);
1634 fprintf(stderr,"\n");
1635 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1638 idx, idx->tail) < 0))
1639 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 msg->sol = ptr;
1642 if (likely(!HTTP_IS_CRLF(*ptr))) {
1643 goto http_msg_hdr_name;
1644 }
1645
1646 if (likely(*ptr == '\r'))
1647 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1648 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001649
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 http_msg_last_lf:
1651 case HTTP_MSG_LAST_LF:
1652 /* Assumes msg->sol points to the first of either CR or LF */
1653 EXPECT_LF_HERE(ptr, http_msg_invalid);
1654 ptr++;
1655 buf->lr = ptr;
1656 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001657 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 return;
1659#ifdef DEBUG_FULL
1660 default:
1661 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1662 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001663#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 }
1665 http_msg_ood:
1666 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001667 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 buf->lr = ptr;
1669 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 http_msg_invalid:
1672 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001673 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 return;
1675}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001676
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001677/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001678 * It normally returns zero, but may return 1 if it absolutely needs to be
1679 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001680 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001681 * functions. Its behaviour is rather simple :
1682 * - all enabled analysers are called in turn from the lower to the higher
1683 * bit.
1684 * - if an analyser does not have enough data, it must return without calling
1685 * other ones. It should also probably reset the BF_MAY_FORWARD bit to ensure
1686 * that unprocessed data will not be forwarded. But that probably depends on
1687 * the protocol. Generally it is not reset in case of errors.
1688 * - if an analyser has enough data, it just has to pass on to the next
1689 * analyser without touching BF_MAY_FORWARD (it is enabled prior to
1690 * analysis).
1691 * - if an analyser thinks it has no added value anymore staying here, it must
1692 * reset its bit from the analysers flags in order not to be called anymore.
1693 *
1694 * In the future, analysers should be able to indicate that they want to be
1695 * called after XXX bytes have been received (or transfered), and the min of
1696 * all's wishes will be used to ring back (unless a special condition occurs).
1697 *
1698 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001700int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 struct buffer *req = t->req;
1703 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001704
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001705 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 +02001706 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001707 t,
1708 req,
1709 req->rex, req->wex,
1710 req->flags,
1711 req->l,
1712 req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001713
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001714 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001715 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001716 struct tcp_rule *rule;
1717 int partial;
1718
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001719 /* We will abort if we encounter a read error. In theory, we
1720 * should not abort if we get a close, it might be valid,
1721 * although very unlikely. FIXME: we'll abort for now, this
1722 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001723 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001724 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001725 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001726 //t->fe->failed_req++;
Willy Tarreaub6866442008-07-14 23:54:42 +02001727 if (!(t->flags & SN_ERR_MASK))
1728 t->flags |= SN_ERR_CLICL;
1729 if (!(t->flags & SN_FINST_MASK))
1730 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001731 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001732 }
1733
1734 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001735 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001736 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001737 t->fe->failed_req++;
1738 if (!(t->flags & SN_ERR_MASK))
1739 t->flags |= SN_ERR_CLITO;
1740 if (!(t->flags & SN_FINST_MASK))
1741 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001742 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001743 }
1744
1745 /* We don't know whether we have enough data, so must proceed
1746 * this way :
1747 * - iterate through all rules in their declaration order
1748 * - if one rule returns MISS, it means the inspect delay is
1749 * not over yet, then return immediately, otherwise consider
1750 * it as a non-match.
1751 * - if one rule returns OK, then return OK
1752 * - if one rule returns KO, then return KO
1753 */
1754
Willy Tarreauffab5b42008-08-17 18:03:28 +02001755 if (req->flags & (BF_READ_NULL | BF_SHUTR) || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001756 partial = 0;
1757 else
1758 partial = ACL_PARTIAL;
1759
1760 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1761 int ret = ACL_PAT_PASS;
1762
1763 if (rule->cond) {
1764 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1765 if (ret == ACL_PAT_MISS) {
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001766 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001767 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001768 if (!tick_isset(req->analyse_exp))
1769 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001770 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001771 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001772
Willy Tarreaub6866442008-07-14 23:54:42 +02001773 ret = acl_pass(ret);
1774 if (rule->cond->pol == ACL_COND_UNLESS)
1775 ret = !ret;
1776 }
1777
1778 if (ret) {
1779 /* we have a matching rule. */
1780 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001781 buffer_abort(req);
1782 buffer_abort(rep);
1783 //FIXME: this delete this
1784 //fd_delete(t->cli_fd);
1785 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001786 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001787 t->fe->failed_req++;
1788 if (!(t->flags & SN_ERR_MASK))
1789 t->flags |= SN_ERR_PRXCOND;
1790 if (!(t->flags & SN_FINST_MASK))
1791 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001792 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001793 }
1794 /* otherwise accept */
1795 break;
1796 }
1797 }
1798
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001799 /* if we get there, it means we have no rule which matches, or
1800 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001801 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001802 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001803 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001804 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001805
Willy Tarreau2df28e82008-08-17 15:20:19 +02001806 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 /*
1808 * Now parse the partial (or complete) lines.
1809 * We will check the request syntax, and also join multi-line
1810 * headers. An index of all the lines will be elaborated while
1811 * parsing.
1812 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001813 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001816 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 * req->data + req->eoh = end of processed headers / start of current one
1818 * req->data + req->eol = end of current header or line (LF or CRLF)
1819 * req->lr = first non-visited byte
1820 * req->r = end of data
1821 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001822
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001824 struct http_txn *txn = &t->txn;
1825 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001827
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001829 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 /* 1: we might have to print this header in debug mode */
1832 if (unlikely((global.mode & MODE_DEBUG) &&
1833 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001834 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001836
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001837 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 eol = sol + msg->sl.rq.l;
1839 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001840
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001841 sol += hdr_idx_first_pos(&txn->hdr_idx);
1842 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001843
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001845 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001846 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001847 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1848 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 }
1850 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001851
Willy Tarreau58f10d72006-12-04 02:26:12 +01001852
1853 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001855 * If not so, we check the FD and buffer states before leaving.
1856 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1858 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001859 *
1860 */
1861
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001862 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 /*
1864 * First, let's catch bad requests.
1865 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001866 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001868
1869 /* 1: Since we are in header mode, if there's no space
1870 * left for headers, we won't be able to free more
1871 * later, so the session will never terminate. We
1872 * must terminate it now.
1873 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001874 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 /* FIXME: check if URI is set and return Status
1876 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001877 */
Willy Tarreau06619262006-12-17 08:37:22 +01001878 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001879 }
1880
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001881 /* 2: have we encountered a read error ? */
1882 else if (req->flags & BF_READ_ERROR) {
1883 /* we cannot return any message on error */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001884 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001885 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001886 //t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001887 if (!(t->flags & SN_ERR_MASK))
1888 t->flags |= SN_ERR_CLICL;
1889 if (!(t->flags & SN_FINST_MASK))
1890 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001891 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001892 }
1893
1894 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001895 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001896 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001897 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001898 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001899 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001900 req->analysers = 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001901 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001902 if (!(t->flags & SN_ERR_MASK))
1903 t->flags |= SN_ERR_CLITO;
1904 if (!(t->flags & SN_FINST_MASK))
1905 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001906 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001907 }
1908
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001909 /* 4: have we encountered a close ? */
1910 else if (req->flags & (BF_READ_NULL | BF_SHUTR)) {
1911 txn->status = 400;
1912 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001913 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001914 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001915 t->fe->failed_req++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001916
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001917 if (!(t->flags & SN_ERR_MASK))
1918 t->flags |= SN_ERR_CLICL;
1919 if (!(t->flags & SN_FINST_MASK))
1920 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001921 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001923
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001924 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001925 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001926 if (!tick_isset(req->analyse_exp))
1927 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001928
1929 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001930 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001931 }
1932
1933
1934 /****************************************************************
1935 * More interesting part now : we know that we have a complete *
1936 * request which at least looks like HTTP. We have an indicator *
1937 * of each header's length, so we can parse them quickly. *
1938 ****************************************************************/
1939
Willy Tarreau2df28e82008-08-17 15:20:19 +02001940 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001941 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001942
Willy Tarreau9cdde232007-05-02 20:58:19 +02001943 /* ensure we keep this pointer to the beginning of the message */
1944 msg->sol = req->data + msg->som;
1945
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946 /*
1947 * 1: identify the method
1948 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001949 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001950
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001951 /* we can make use of server redirect on GET and HEAD */
1952 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1953 t->flags |= SN_REDIRECTABLE;
1954
Willy Tarreau58f10d72006-12-04 02:26:12 +01001955 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001957 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001958 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001959 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001960 if (unlikely((t->fe->monitor_uri_len != 0) &&
1961 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1962 !memcmp(&req->data[msg->sl.rq.u],
1963 t->fe->monitor_uri,
1964 t->fe->monitor_uri_len))) {
1965 /*
1966 * We have found the monitor URI
1967 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001968 struct acl_cond *cond;
1969 cur_proxy = t->fe;
1970
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001971 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001972
1973 /* Check if we want to fail this monitor request or not */
1974 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1975 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001976
1977 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001978 if (cond->pol == ACL_COND_UNLESS)
1979 ret = !ret;
1980
1981 if (ret) {
1982 /* we fail this request, let's return 503 service unavail */
1983 txn->status = 503;
1984 client_retnclose(t, error_message(t, HTTP_ERR_503));
1985 goto return_prx_cond;
1986 }
1987 }
1988
1989 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001990 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001991 client_retnclose(t, &http_200_chunk);
1992 goto return_prx_cond;
1993 }
1994
1995 /*
1996 * 3: Maybe we have to copy the original REQURI for the logs ?
1997 * Note: we cannot log anymore if the request has been
1998 * classified as invalid.
1999 */
2000 if (unlikely(t->logs.logwait & LW_REQ)) {
2001 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002002 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003 int urilen = msg->sl.rq.l;
2004
2005 if (urilen >= REQURI_LEN)
2006 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002007 memcpy(txn->uri, &req->data[msg->som], urilen);
2008 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002009
2010 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02002011 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002012 } else {
2013 Alert("HTTP logging : out of memory.\n");
2014 }
2015 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002016
Willy Tarreau06619262006-12-17 08:37:22 +01002017
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002018 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2019 if (unlikely(msg->sl.rq.v_l == 0)) {
2020 int delta;
2021 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002022 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002023 cur_end = msg->sol + msg->sl.rq.l;
2024 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01002025
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002026 if (msg->sl.rq.u_l == 0) {
2027 /* if no URI was set, add "/" */
2028 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2029 cur_end += delta;
2030 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002031 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002032 /* add HTTP version */
2033 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2034 msg->eoh += delta;
2035 cur_end += delta;
2036 cur_end = (char *)http_parse_reqline(msg, req->data,
2037 HTTP_MSG_RQMETH,
2038 msg->sol, cur_end + 1,
2039 NULL, NULL);
2040 if (unlikely(!cur_end))
2041 goto return_bad_req;
2042
2043 /* we have a full HTTP/1.0 request now and we know that
2044 * we have either a CR or an LF at <ptr>.
2045 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002046 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002047 }
2048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002049
2050 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002051 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002052 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002053 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002054
2055 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002056 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002057 * As opposed to version 1.2, now they will be evaluated in the
2058 * filters order and not in the header order. This means that
2059 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002060 *
2061 * We can now check whether we want to switch to another
2062 * backend, in which case we will re-check the backend's
2063 * filters and various options. In order to support 3-level
2064 * switching, here's how we should proceed :
2065 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002066 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002067 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002068 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002069 * There cannot be any switch from there, so ->be cannot be
2070 * changed anymore.
2071 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002072 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002073 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002074 * The response path will be able to apply either ->be, or
2075 * ->be then ->fe filters in order to match the reverse of
2076 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002077 */
2078
Willy Tarreau06619262006-12-17 08:37:22 +01002079 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002080 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002081 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002082 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002083 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002084
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002085 /* first check whether we have some ACLs set to redirect this request */
2086 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2087 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002088
2089 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002090 if (rule->cond->pol == ACL_COND_UNLESS)
2091 ret = !ret;
2092
2093 if (ret) {
2094 struct chunk rdr = { trash, 0 };
2095 const char *msg_fmt;
2096
2097 /* build redirect message */
2098 switch(rule->code) {
2099 case 303:
2100 rdr.len = strlen(HTTP_303);
2101 msg_fmt = HTTP_303;
2102 break;
2103 case 301:
2104 rdr.len = strlen(HTTP_301);
2105 msg_fmt = HTTP_301;
2106 break;
2107 case 302:
2108 default:
2109 rdr.len = strlen(HTTP_302);
2110 msg_fmt = HTTP_302;
2111 break;
2112 }
2113
2114 if (unlikely(rdr.len > sizeof(trash)))
2115 goto return_bad_req;
2116 memcpy(rdr.str, msg_fmt, rdr.len);
2117
2118 switch(rule->type) {
2119 case REDIRECT_TYPE_PREFIX: {
2120 const char *path;
2121 int pathlen;
2122
2123 path = http_get_path(txn);
2124 /* build message using path */
2125 if (path) {
2126 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2127 } else {
2128 path = "/";
2129 pathlen = 1;
2130 }
2131
2132 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2133 goto return_bad_req;
2134
2135 /* add prefix */
2136 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2137 rdr.len += rule->rdr_len;
2138
2139 /* add path */
2140 memcpy(rdr.str + rdr.len, path, pathlen);
2141 rdr.len += pathlen;
2142 break;
2143 }
2144 case REDIRECT_TYPE_LOCATION:
2145 default:
2146 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2147 goto return_bad_req;
2148
2149 /* add location */
2150 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2151 rdr.len += rule->rdr_len;
2152 break;
2153 }
2154
2155 /* add end of headers */
2156 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2157 rdr.len += 4;
2158
2159 txn->status = rule->code;
2160 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002161 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002162 client_retnclose(t, &rdr);
2163 goto return_prx_cond;
2164 }
2165 }
2166
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002167 /* first check whether we have some ACLs set to block this request */
2168 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002169 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002170
2171 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002172 if (cond->pol == ACL_COND_UNLESS)
2173 ret = !ret;
2174
2175 if (ret) {
2176 txn->status = 403;
2177 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002178 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002179 client_retnclose(t, error_message(t, HTTP_ERR_403));
2180 goto return_prx_cond;
2181 }
2182 }
2183
Willy Tarreau06619262006-12-17 08:37:22 +01002184 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002185 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002186 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2187 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002188 }
2189
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002190 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2191 /* to ensure correct connection accounting on
2192 * the backend, we count the connection for the
2193 * one managing the queue.
2194 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002195 t->be->beconn++;
2196 if (t->be->beconn > t->be->beconn_max)
2197 t->be->beconn_max = t->be->beconn;
2198 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002199 t->flags |= SN_BE_ASSIGNED;
2200 }
2201
Willy Tarreau06619262006-12-17 08:37:22 +01002202 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002203 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002204 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002205 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002206 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002207 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002208 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002209 goto return_prx_cond;
2210 }
2211
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002212 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002213 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002214 !(t->flags & SN_CONN_CLOSED)) {
2215 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002216 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002217 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002218
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002219 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002220 old_idx = 0;
2221
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002222 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2223 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002224 cur_ptr = cur_next;
2225 cur_end = cur_ptr + cur_hdr->len;
2226 cur_next = cur_end + cur_hdr->cr + 1;
2227
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002228 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2229 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002230 /* 3 possibilities :
2231 * - we have already set Connection: close,
2232 * so we remove this line.
2233 * - we have not yet set Connection: close,
2234 * but this line indicates close. We leave
2235 * it untouched and set the flag.
2236 * - we have not yet set Connection: close,
2237 * and this line indicates non-close. We
2238 * replace it.
2239 */
2240 if (t->flags & SN_CONN_CLOSED) {
2241 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002242 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002243 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002244 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2245 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002246 cur_hdr->len = 0;
2247 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002248 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2249 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2250 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002251 cur_next += delta;
2252 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002253 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002254 }
2255 t->flags |= SN_CONN_CLOSED;
2256 }
2257 }
2258 old_idx = cur_idx;
2259 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002260 }
2261 /* add request headers from the rule sets in the same order */
2262 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2263 if (unlikely(http_header_add_tail(req,
2264 &txn->req,
2265 &txn->hdr_idx,
2266 rule_set->req_add[cur_idx])) < 0)
2267 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002268 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002270 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002271 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002272 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002273 /* we have to check the URI and auth for this request.
2274 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002275 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002276 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002277 if (stats_check_uri_auth(t, rule_set))
2278 return 1;
2279 }
2280
Willy Tarreau55ea7572007-06-17 19:56:27 +02002281 /* now check whether we have some switching rules for this request */
2282 if (!(t->flags & SN_BE_ASSIGNED)) {
2283 struct switching_rule *rule;
2284
2285 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2286 int ret;
2287
2288 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002289
2290 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002291 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002292 ret = !ret;
2293
2294 if (ret) {
2295 t->be = rule->be.backend;
2296 t->be->beconn++;
2297 if (t->be->beconn > t->be->beconn_max)
2298 t->be->beconn_max = t->be->beconn;
2299 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002300
2301 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002302 t->rep->rto = t->req->wto = t->be->timeout.server;
2303 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002304 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002305 t->flags |= SN_BE_ASSIGNED;
2306 break;
2307 }
2308 }
2309 }
2310
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002311 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2312 /* No backend was set, but there was a default
2313 * backend set in the frontend, so we use it and
2314 * loop again.
2315 */
2316 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002317 t->be->beconn++;
2318 if (t->be->beconn > t->be->beconn_max)
2319 t->be->beconn_max = t->be->beconn;
2320 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002321
2322 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002323 t->rep->rto = t->req->wto = t->be->timeout.server;
2324 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002325 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002326 t->flags |= SN_BE_ASSIGNED;
2327 }
2328 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002329
Willy Tarreau58f10d72006-12-04 02:26:12 +01002330
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002331 if (!(t->flags & SN_BE_ASSIGNED)) {
2332 /* To ensure correct connection accounting on
2333 * the backend, we count the connection for the
2334 * one managing the queue.
2335 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002336 t->be->beconn++;
2337 if (t->be->beconn > t->be->beconn_max)
2338 t->be->beconn_max = t->be->beconn;
2339 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002340 t->flags |= SN_BE_ASSIGNED;
2341 }
2342
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002343 /*
2344 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002345 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002346 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002347 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002348 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002349
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002350 /*
2351 * If HTTP PROXY is set we simply get remote server address
2352 * parsing incoming request.
2353 */
2354 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2355 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002357
Willy Tarreau2a324282006-12-05 00:05:46 +01002358 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002359 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002360 * so let's do the same now.
2361 */
2362
2363 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002364 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002365 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002366 }
2367
2368
2369 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002370 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002371 * Note that doing so might move headers in the request, but
2372 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002373 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002374 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002375 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2376 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002377 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002378
Willy Tarreau58f10d72006-12-04 02:26:12 +01002379
Willy Tarreau2a324282006-12-05 00:05:46 +01002380 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002381 * 9: add X-Forwarded-For if either the frontend or the backend
2382 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002383 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002384 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002385 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002386 /* Add an X-Forwarded-For header unless the source IP is
2387 * in the 'except' network range.
2388 */
2389 if ((!t->fe->except_mask.s_addr ||
2390 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2391 != t->fe->except_net.s_addr) &&
2392 (!t->be->except_mask.s_addr ||
2393 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2394 != t->be->except_net.s_addr)) {
2395 int len;
2396 unsigned char *pn;
2397 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002398
Ross Westaf72a1d2008-08-03 10:51:45 +02002399 /* Note: we rely on the backend to get the header name to be used for
2400 * x-forwarded-for, because the header is really meant for the backends.
2401 * However, if the backend did not specify any option, we have to rely
2402 * on the frontend's header name.
2403 */
2404 if (t->be->fwdfor_hdr_len) {
2405 len = t->be->fwdfor_hdr_len;
2406 memcpy(trash, t->be->fwdfor_hdr_name, len);
2407 } else {
2408 len = t->fe->fwdfor_hdr_len;
2409 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2410 }
2411 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002412
Ross Westaf72a1d2008-08-03 10:51:45 +02002413 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002414 &txn->hdr_idx, trash, len)) < 0)
2415 goto return_bad_req;
2416 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002417 }
2418 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002419 /* FIXME: for the sake of completeness, we should also support
2420 * 'except' here, although it is mostly useless in this case.
2421 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002422 int len;
2423 char pn[INET6_ADDRSTRLEN];
2424 inet_ntop(AF_INET6,
2425 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2426 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002427
2428 /* Note: we rely on the backend to get the header name to be used for
2429 * x-forwarded-for, because the header is really meant for the backends.
2430 * However, if the backend did not specify any option, we have to rely
2431 * on the frontend's header name.
2432 */
2433 if (t->be->fwdfor_hdr_len) {
2434 len = t->be->fwdfor_hdr_len;
2435 memcpy(trash, t->be->fwdfor_hdr_name, len);
2436 } else {
2437 len = t->fe->fwdfor_hdr_len;
2438 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2439 }
2440 len += sprintf(trash + len, ": %s", pn);
2441
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002442 if (unlikely(http_header_add_tail2(req, &txn->req,
2443 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002444 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002445 }
2446 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002447
Willy Tarreau2a324282006-12-05 00:05:46 +01002448 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002449 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002450 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002451 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002452 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002453 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002454 if ((unlikely(msg->sl.rq.v_l != 8) ||
2455 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2456 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002457 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002458 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002459 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002460 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002461 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2462 * If not assigned, perhaps we are balancing on url_param, but this is a
2463 * POST; and the parameters are in the body, maybe scan there to find our server.
2464 * (unless headers overflowed the buffer?)
2465 */
2466 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2467 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002468 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002469 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2470 /* are there enough bytes here? total == l || r || rlim ?
2471 * len is unsigned, but eoh is int,
2472 * how many bytes of body have we received?
2473 * eoh is the first empty line of the header
2474 */
2475 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002476 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 +02002477
2478 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2479 * We can't assume responsibility for the server's decision,
2480 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2481 * We also can't change our mind later, about which server to choose, so round robin.
2482 */
2483 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2484 struct hdr_ctx ctx;
2485 ctx.idx = 0;
2486 /* Expect is allowed in 1.1, look for it */
2487 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2488 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002489 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002490 /* We can't reliablly stall and wait for data, because of
2491 * .NET clients that don't conform to rfc2616; so, no need for
2492 * the next block to check length expectations.
2493 * We could send 100 status back to the client, but then we need to
2494 * re-write headers, and send the message. And this isn't the right
2495 * place for that action.
2496 * TODO: support Expect elsewhere and delete this block.
2497 */
2498 goto end_check_maybe_wait_for_body;
2499 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002500
2501 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002502 /* nothing to do, we got enough */
2503 } else {
2504 /* limit implies we are supposed to need this many bytes
2505 * to find the parameter. Let's see how many bytes we can wait for.
2506 */
2507 long long hint = len;
2508 struct hdr_ctx ctx;
2509 ctx.idx = 0;
2510 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002511 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2512 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002513 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002514 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002515 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002516 ctx.idx = 0;
2517 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2518 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002519 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002520 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002521 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002522 hint = 0; /* parse failure, untrusted client */
2523 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002524 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002525 msg->hdr_content_len = hint;
2526 else
2527 hint = 0; /* bad client, sent negative length */
2528 }
2529 }
2530 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002531 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002532 hint = t->be->url_param_post_limit;
2533 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002534 if (len < hint) {
2535 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002536 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002537 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002538 /* else... There are no body bytes to wait for */
2539 }
2540 }
2541 }
2542 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002543
Willy Tarreau2a324282006-12-05 00:05:46 +01002544 /*************************************************************
2545 * OK, that's finished for the headers. We have done what we *
2546 * could. Let's switch to the DATA state. *
2547 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002548
Willy Tarreaue393fe22008-08-16 22:18:07 +02002549 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002550 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002551
Willy Tarreau1fa31262007-12-03 00:36:16 +01002552 /* When a connection is tarpitted, we use the tarpit timeout,
2553 * which may be the same as the connect timeout if unspecified.
2554 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002555 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01002556 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002557 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002558 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002559 /* flush the request so that we can drop the connection early
2560 * if the client closes first.
2561 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002562 req->flags &= ~BF_MAY_FORWARD;
2563 req->analysers |= AN_REQ_HTTP_TARPIT;
2564 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2565 if (!req->analyse_exp)
2566 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002567 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002569 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002570 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002571
2572 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002573 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002574 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002575 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002576 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002577 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002578 return_prx_cond:
2579 if (!(t->flags & SN_ERR_MASK))
2580 t->flags |= SN_ERR_PRXCOND;
2581 if (!(t->flags & SN_FINST_MASK))
2582 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002583 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002584 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002585 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002586 }
2587
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002588 if (req->analysers & AN_REQ_HTTP_TARPIT) {
2589 struct http_txn *txn = &t->txn;
2590
2591 /* This connection is being tarpitted. The CLIENT side has
2592 * already set the connect expiration date to the right
2593 * timeout. We just have to check that the client is still
2594 * there and that the timeout has not expired.
2595 */
2596 if ((req->flags & (BF_READ_NULL|BF_READ_ERROR)) == 0 &&
2597 !tick_is_expired(req->analyse_exp, now_ms))
2598 return 0;
2599
2600 /* We will set the queue timer to the time spent, just for
2601 * logging purposes. We fake a 500 server error, so that the
2602 * attacker will not suspect his connection has been tarpitted.
2603 * It will not cause trouble to the logs because we can exclude
2604 * the tarpitted connections by filtering on the 'PT' status flags.
2605 */
2606 trace_term(t, TT_HTTP_SRV_2);
2607 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
2608
2609 txn->status = 500;
2610 if (req->flags != BF_READ_ERROR)
2611 client_retnclose(t, error_message(t, HTTP_ERR_500));
2612
2613 req->analysers = 0;
2614 req->analyse_exp = TICK_ETERNITY;
2615
2616 t->fe->failed_req++;
2617 if (!(t->flags & SN_ERR_MASK))
2618 t->flags |= SN_ERR_PRXCOND;
2619 if (!(t->flags & SN_FINST_MASK))
2620 t->flags |= SN_FINST_T;
2621 return 0;
2622 }
2623
Willy Tarreau2df28e82008-08-17 15:20:19 +02002624 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002625 /* We have to parse the HTTP request body to find any required data.
2626 * "balance url_param check_post" should have been the only way to get
2627 * into this. We were brought here after HTTP header analysis, so all
2628 * related structures are ready.
2629 */
2630 struct http_msg *msg = &t->txn.req;
2631 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2632 long long limit = t->be->url_param_post_limit;
2633 struct hdr_ctx ctx;
2634
2635 ctx.idx = 0;
2636
2637 /* now if we have a length, we'll take the hint */
2638 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2639 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2640 unsigned int chunk = 0;
2641 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2642 char c = msg->sol[body];
2643 if (ishex(c)) {
2644 unsigned int hex = toupper(c) - '0';
2645 if (hex > 9)
2646 hex -= 'A' - '9' - 1;
2647 chunk = (chunk << 4) | hex;
2648 } else
2649 break;
2650 body++;
2651 }
2652 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002653 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002654
2655 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002656 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002657
2658 body += 2; // skip CRLF
2659
2660 /* if we support more then one chunk here, we have to do it again when assigning server
2661 * 1. how much entity data do we have? new var
2662 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2663 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2664 */
2665
2666 if (chunk < limit)
2667 limit = chunk; /* only reading one chunk */
2668 } else {
2669 if (msg->hdr_content_len < limit)
2670 limit = msg->hdr_content_len;
2671 }
2672
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002673 http_body_end:
2674 /* we leave once we know we have nothing left to do. This means that we have
2675 * enough bytes, or that we know we'll not get any more (buffer full, read
2676 * buffer closed).
2677 */
2678 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002679 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_NULL | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02002680 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002681 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002682 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002683 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002684 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002685 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02002686 else {
2687 /* Not enough data. We'll re-use the http-request
2688 * timeout here. Ideally, we should set the timeout
2689 * relative to the accept() date. We just set the
2690 * request timeout once at the beginning of the
2691 * request.
2692 */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002693 req->flags &= ~BF_MAY_FORWARD;
Willy Tarreauc52164a2008-08-17 19:17:57 +02002694 if (!tick_isset(req->analyse_exp))
2695 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
2696 return 0;
2697 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002698 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002699
Willy Tarreau2df28e82008-08-17 15:20:19 +02002700 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2701 * probably reduce one day's debugging session.
2702 */
2703#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002704 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 +02002705 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2706 __FILE__, __LINE__, req->analysers);
2707 ABORT_NOW();
2708 }
2709#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002710 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02002711 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002712}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002713
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002714/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002715 * It normally returns zero, but may return 1 if it absolutely needs to be
2716 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002717 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002718 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002719 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002720int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002721{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002722 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002723 struct buffer *req = t->req;
2724 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002725
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002726 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 +02002727 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002728 t,
2729 rep,
2730 rep->rex, rep->wex,
2731 rep->flags,
2732 rep->l,
2733 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002734
Willy Tarreau2df28e82008-08-17 15:20:19 +02002735 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002736 /*
2737 * Now parse the partial (or complete) lines.
2738 * We will check the response syntax, and also join multi-line
2739 * headers. An index of all the lines will be elaborated while
2740 * parsing.
2741 *
2742 * For the parsing, we use a 28 states FSM.
2743 *
2744 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002745 * rep->data + rep->som = beginning of response
2746 * rep->data + rep->eoh = end of processed headers / start of current one
2747 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002748 * rep->lr = first non-visited byte
2749 * rep->r = end of data
2750 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002751
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002752 int cur_idx;
2753 struct http_msg *msg = &txn->rsp;
2754 struct proxy *cur_proxy;
2755
2756 if (likely(rep->lr < rep->r))
2757 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2758
2759 /* 1: we might have to print this header in debug mode */
2760 if (unlikely((global.mode & MODE_DEBUG) &&
2761 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2762 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2763 char *eol, *sol;
2764
2765 sol = rep->data + msg->som;
2766 eol = sol + msg->sl.rq.l;
2767 debug_hdr("srvrep", t, sol, eol);
2768
2769 sol += hdr_idx_first_pos(&txn->hdr_idx);
2770 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2771
2772 while (cur_idx) {
2773 eol = sol + txn->hdr_idx.v[cur_idx].len;
2774 debug_hdr("srvhdr", t, sol, eol);
2775 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2776 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002777 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002778 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002779
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002780 /*
2781 * Now we quickly check if we have found a full valid response.
2782 * If not so, we check the FD and buffer states before leaving.
2783 * A full response is indicated by the fact that we have seen
2784 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2785 * responses are checked first.
2786 *
2787 * Depending on whether the client is still there or not, we
2788 * may send an error response back or not. Note that normally
2789 * we should only check for HTTP status there, and check I/O
2790 * errors somewhere else.
2791 */
2792
2793 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002794 /* Invalid response */
2795 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2796 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002797 //buffer_shutr(rep);
2798 //buffer_shutw(req);
2799 //fd_delete(req->cons->fd);
2800 //req->cons->state = SI_ST_CLO;
2801 buffer_shutr_now(rep);
2802 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002803 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002804 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002805 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002806 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002807 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002808 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002809 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002810 txn->status = 502;
2811 client_return(t, error_message(t, HTTP_ERR_502));
2812 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002813 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002814 if (!(t->flags & SN_FINST_MASK))
2815 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002816
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002817 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2818 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002819
Willy Tarreaudafde432008-08-17 01:00:46 +02002820 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002821 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002822 /* too large response does not fit in buffer. */
2823 else if (rep->flags & BF_FULL) {
2824 goto hdr_response_bad;
2825 }
2826 /* read error */
2827 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002828 buffer_shutr_now(rep);
2829 buffer_shutw_now(req);
2830 //fd_delete(req->cons->fd);
2831 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002832 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002833 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002834 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002835 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002836 //}
2837 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002838 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002839 txn->status = 502;
2840 client_return(t, error_message(t, HTTP_ERR_502));
2841 if (!(t->flags & SN_ERR_MASK))
2842 t->flags |= SN_ERR_SRVCL;
2843 if (!(t->flags & SN_FINST_MASK))
2844 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002845
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002846 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2847 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002848
Willy Tarreaudafde432008-08-17 01:00:46 +02002849 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002850 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002851 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002852 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002853 buffer_shutr_now(rep);
2854 buffer_shutw_now(req);
2855 //fd_delete(req->cons->fd);
2856 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002857 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002858 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002859 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002860 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002861 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002862 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002863 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002864 txn->status = 504;
2865 client_return(t, error_message(t, HTTP_ERR_504));
2866 if (!(t->flags & SN_ERR_MASK))
2867 t->flags |= SN_ERR_SRVTO;
2868 if (!(t->flags & SN_FINST_MASK))
2869 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002870
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002871 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2872 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002873 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002874 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002875 /* write error to client, or close from server */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002876 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR|BF_READ_NULL)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002877 buffer_shutr_now(rep);
2878 buffer_shutw_now(req);
2879 //fd_delete(req->cons->fd);
2880 //req->cons->state = SI_ST_CLO;
2881 if (t->srv) {
2882 //t->srv->cur_sess--;
2883 t->srv->failed_resp++;
2884 //sess_change_server(t, NULL);
2885 }
2886 t->be->failed_resp++;
2887 rep->analysers = 0;
2888 txn->status = 502;
2889 client_return(t, error_message(t, HTTP_ERR_502));
2890 if (!(t->flags & SN_ERR_MASK))
2891 t->flags |= SN_ERR_SRVCL;
2892 if (!(t->flags & SN_FINST_MASK))
2893 t->flags |= SN_FINST_H;
2894
2895 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2896 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002897
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002898 return 0;
2899 }
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002900 rep->flags &= ~BF_MAY_FORWARD;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002901 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002902 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002903
Willy Tarreau21d2af32008-02-14 20:25:24 +01002904
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002905 /*****************************************************************
2906 * More interesting part now : we know that we have a complete *
2907 * response which at least looks like HTTP. We have an indicator *
2908 * of each header's length, so we can parse them quickly. *
2909 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002910
Willy Tarreau2df28e82008-08-17 15:20:19 +02002911 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002912
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002913 /* ensure we keep this pointer to the beginning of the message */
2914 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002915
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002916 /*
2917 * 1: get the status code and check for cacheability.
2918 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002919
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002920 t->logs.logwait &= ~LW_RESP;
2921 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002922
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002923 switch (txn->status) {
2924 case 200:
2925 case 203:
2926 case 206:
2927 case 300:
2928 case 301:
2929 case 410:
2930 /* RFC2616 @13.4:
2931 * "A response received with a status code of
2932 * 200, 203, 206, 300, 301 or 410 MAY be stored
2933 * by a cache (...) unless a cache-control
2934 * directive prohibits caching."
2935 *
2936 * RFC2616 @9.5: POST method :
2937 * "Responses to this method are not cacheable,
2938 * unless the response includes appropriate
2939 * Cache-Control or Expires header fields."
2940 */
2941 if (likely(txn->meth != HTTP_METH_POST) &&
2942 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2943 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2944 break;
2945 default:
2946 break;
2947 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002948
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002949 /*
2950 * 2: we may need to capture headers
2951 */
2952 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2953 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2954 txn->rsp.cap, t->fe->rsp_cap);
2955
2956 /*
2957 * 3: we will have to evaluate the filters.
2958 * As opposed to version 1.2, now they will be evaluated in the
2959 * filters order and not in the header order. This means that
2960 * each filter has to be validated among all headers.
2961 *
2962 * Filters are tried with ->be first, then with ->fe if it is
2963 * different from ->be.
2964 */
2965
2966 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2967
2968 cur_proxy = t->be;
2969 while (1) {
2970 struct proxy *rule_set = cur_proxy;
2971
2972 /* try headers filters */
2973 if (rule_set->rsp_exp != NULL) {
2974 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2975 return_bad_resp:
2976 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002977 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002978 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002979 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002980 }
2981 cur_proxy->failed_resp++;
2982 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002983 buffer_shutr_now(rep);
2984 buffer_shutw_now(req);
2985 //fd_delete(req->cons->fd);
2986 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002987 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002988 txn->status = 502;
2989 client_return(t, error_message(t, HTTP_ERR_502));
2990 if (!(t->flags & SN_ERR_MASK))
2991 t->flags |= SN_ERR_PRXCOND;
2992 if (!(t->flags & SN_FINST_MASK))
2993 t->flags |= SN_FINST_H;
2994 /* We used to have a free connection slot. Since we'll never use it,
2995 * we have to inform the server that it may be used by another session.
2996 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002997 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2998 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002999 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003000 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003001 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003002
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003003 /* has the response been denied ? */
3004 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01003005 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003006 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003007 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003008 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01003009 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003010 cur_proxy->denied_resp++;
3011 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003012 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003013
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003014 /* We might have to check for "Connection:" */
3015 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3016 !(t->flags & SN_CONN_CLOSED)) {
3017 char *cur_ptr, *cur_end, *cur_next;
3018 int cur_idx, old_idx, delta, val;
3019 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003020
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003021 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3022 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003023
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003024 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3025 cur_hdr = &txn->hdr_idx.v[cur_idx];
3026 cur_ptr = cur_next;
3027 cur_end = cur_ptr + cur_hdr->len;
3028 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003029
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003030 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3031 if (val) {
3032 /* 3 possibilities :
3033 * - we have already set Connection: close,
3034 * so we remove this line.
3035 * - we have not yet set Connection: close,
3036 * but this line indicates close. We leave
3037 * it untouched and set the flag.
3038 * - we have not yet set Connection: close,
3039 * and this line indicates non-close. We
3040 * replace it.
3041 */
3042 if (t->flags & SN_CONN_CLOSED) {
3043 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3044 txn->rsp.eoh += delta;
3045 cur_next += delta;
3046 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3047 txn->hdr_idx.used--;
3048 cur_hdr->len = 0;
3049 } else {
3050 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3051 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3052 "close", 5);
3053 cur_next += delta;
3054 cur_hdr->len += delta;
3055 txn->rsp.eoh += delta;
3056 }
3057 t->flags |= SN_CONN_CLOSED;
3058 }
3059 }
3060 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003061 }
3062 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003063
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003064 /* add response headers from the rule sets in the same order */
3065 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3066 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3067 rule_set->rsp_add[cur_idx])) < 0)
3068 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003069 }
3070
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003071 /* check whether we're already working on the frontend */
3072 if (cur_proxy == t->fe)
3073 break;
3074 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003075 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003076
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003077 /*
3078 * 4: check for server cookie.
3079 */
3080 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3081 || (t->be->options & PR_O_CHK_CACHE))
3082 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003083
Willy Tarreaubaaee002006-06-26 02:48:02 +02003084
Willy Tarreaua15645d2007-03-18 16:22:39 +01003085 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003086 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003087 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003088 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3089 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003090
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003091 /*
3092 * 6: add server cookie in the response if needed
3093 */
3094 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3095 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3096 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003097
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003098 /* the server is known, it's not the one the client requested, we have to
3099 * insert a set-cookie here, except if we want to insert only on POST
3100 * requests and this one isn't. Note that servers which don't have cookies
3101 * (eg: some backup servers) will return a full cookie removal request.
3102 */
3103 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3104 t->be->cookie_name,
3105 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003106
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003107 if (t->be->cookie_domain)
3108 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003109
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003110 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3111 trash, len)) < 0)
3112 goto return_bad_resp;
3113 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003114
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003115 /* Here, we will tell an eventual cache on the client side that we don't
3116 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3117 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3118 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3119 */
3120 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003121
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003122 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3123
3124 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3125 "Cache-control: private", 22)) < 0)
3126 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003127 }
3128 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003129
Willy Tarreaubaaee002006-06-26 02:48:02 +02003130
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003131 /*
3132 * 7: check if result will be cacheable with a cookie.
3133 * We'll block the response if security checks have caught
3134 * nasty things such as a cacheable cookie.
3135 */
3136 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3137 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3138 (t->be->options & PR_O_CHK_CACHE)) {
3139
3140 /* we're in presence of a cacheable response containing
3141 * a set-cookie header. We'll block it as requested by
3142 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003143 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003144 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003145 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003146 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003147 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003148 }
3149 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003150
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003151 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3152 t->be->id, t->srv?t->srv->id:"<dispatch>");
3153 send_log(t->be, LOG_ALERT,
3154 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3155 t->be->id, t->srv?t->srv->id:"<dispatch>");
3156 goto return_srv_prx_502;
3157 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003158
3159 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003160 * 8: add "Connection: close" if needed and not yet set.
3161 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003162 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003163 if (!(t->flags & SN_CONN_CLOSED) &&
3164 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3165 if ((unlikely(msg->sl.st.v_l != 8) ||
3166 unlikely(req->data[msg->som + 7] != '0')) &&
3167 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3168 "Connection: close", 17)) < 0)
3169 goto return_bad_resp;
3170 t->flags |= SN_CONN_CLOSED;
3171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003172
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003173 /*************************************************************
3174 * OK, that's finished for the headers. We have done what we *
3175 * could. Let's switch to the DATA state. *
3176 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003177
Willy Tarreaue393fe22008-08-16 22:18:07 +02003178 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003179 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003180
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003181#ifdef CONFIG_HAP_TCPSPLICE
3182 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3183 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003184 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003185 }
3186#endif
3187 /* if the user wants to log as soon as possible, without counting
3188 * bytes from the server, then this is the right moment. We have
3189 * to temporarily assign bytes_out to log what we currently have.
3190 */
3191 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3192 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3193 t->logs.bytes_out = txn->rsp.eoh;
3194 if (t->fe->to_log & LW_REQ)
3195 http_sess_log(t);
3196 else
3197 tcp_sess_log(t);
3198 t->logs.bytes_out = 0;
3199 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003200
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003201 /* Note: we must not try to cheat by jumping directly to DATA,
3202 * otherwise we would not let the client side wake up.
3203 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003204
Willy Tarreaudafde432008-08-17 01:00:46 +02003205 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003206 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003207
Willy Tarreau2df28e82008-08-17 15:20:19 +02003208 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3209 * probably reduce one day's debugging session.
3210 */
3211#ifdef DEBUG_DEV
3212 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3213 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3214 __FILE__, __LINE__, rep->analysers);
3215 ABORT_NOW();
3216 }
3217#endif
3218 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003219 return 0;
3220}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003221
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003222///*
3223// * Manages the client FSM and its socket. It normally returns zero, but may
3224// * return 1 if it absolutely wants to be called again.
3225// *
3226// * Note: process_cli is the ONLY function allowed to set cli_state to anything
3227// * but CL_STCLOSE.
3228// */
3229//int process_cli(struct session *t)
3230//{
3231// struct buffer *req = t->req;
3232// struct buffer *rep = t->rep;
3233//
3234// 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",
3235// now_ms, __FUNCTION__,
3236// t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/
3237// cli_stnames[t->cli_state],
3238// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3239// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
3240// req->rex, rep->wex,
3241// req->flags, rep->flags,
3242// req->l, rep->l);
3243//
3244// update_state:
3245// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3246// * still set this state (and will do until unix sockets are converted).
3247// */
3248// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
3249// /* we can skip most of the tests at once if some conditions are not met */
3250// if (!((fdtab[t->cli_fd].state == FD_STERROR) ||
3251// (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) ||
3252// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) ||
3253// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3254// (!(rep->flags & BF_SHUTW) &&
3255// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3256// goto update_timeouts;
3257//
3258// /* read or write error */
3259// if (fdtab[t->cli_fd].state == FD_STERROR) {
3260// buffer_shutr(req);
3261// req->flags |= BF_READ_ERROR;
3262// buffer_shutw(rep);
3263// rep->flags |= BF_WRITE_ERROR;
3264// fd_delete(t->cli_fd);
3265// t->cli_state = CL_STCLOSE;
3266// trace_term(t, TT_HTTP_CLI_1);
3267// if (!req->analysers) {
3268// if (!(t->flags & SN_ERR_MASK))
3269// t->flags |= SN_ERR_CLICL;
3270// if (!(t->flags & SN_FINST_MASK)) {
3271// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3272// t->flags |= SN_FINST_Q;
3273// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3274// t->flags |= SN_FINST_C;
3275// else
3276// t->flags |= SN_FINST_D;
3277// }
3278// }
3279// goto update_state;
3280// }
3281// /* last read, or end of server write */
3282// else if (!(req->flags & BF_SHUTR) && /* not already done */
3283// req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3284// buffer_shutr(req);
3285// if (!(rep->flags & BF_SHUTW)) {
3286// EV_FD_CLR(t->cli_fd, DIR_RD);
3287// trace_term(t, TT_HTTP_CLI_2);
3288// } else {
3289// /* output was already closed */
3290// fd_delete(t->cli_fd);
3291// t->cli_state = CL_STCLOSE;
3292// trace_term(t, TT_HTTP_CLI_3);
3293// }
3294// goto update_state;
3295// }
3296// /* last server read and buffer empty : we only check them when we're
3297// * allowed to forward the data.
3298// */
3299// else if (!(rep->flags & BF_SHUTW) && /* not already done */
3300// ((rep->flags & BF_SHUTW_NOW) ||
3301// (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
3302// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) {
3303// buffer_shutw(rep);
3304// if (!(req->flags & BF_SHUTR)) {
3305// EV_FD_CLR(t->cli_fd, DIR_WR);
3306// shutdown(t->cli_fd, SHUT_WR);
3307// trace_term(t, TT_HTTP_CLI_4);
3308// } else {
3309// fd_delete(t->cli_fd);
3310// t->cli_state = CL_STCLOSE;
3311// trace_term(t, TT_HTTP_CLI_5);
3312// }
3313// goto update_state;
3314// }
3315// /* read timeout */
3316// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
3317// buffer_shutr(req);
3318// if (!(rep->flags & BF_SHUTW)) {
3319// EV_FD_CLR(t->cli_fd, DIR_RD);
3320// trace_term(t, TT_HTTP_CLI_6);
3321// } else {
3322// /* output was already closed */
3323// fd_delete(t->cli_fd);
3324// t->cli_state = CL_STCLOSE;
3325// trace_term(t, TT_HTTP_CLI_7);
3326// }
3327// if (!req->analysers) {
3328// if (!(t->flags & SN_ERR_MASK))
3329// t->flags |= SN_ERR_CLITO;
3330// if (!(t->flags & SN_FINST_MASK)) {
3331// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3332// t->flags |= SN_FINST_Q;
3333// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3334// t->flags |= SN_FINST_C;
3335// else
3336// t->flags |= SN_FINST_D;
3337// }
3338// }
3339// goto update_state;
3340// }
3341// /* write timeout */
3342// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
3343// buffer_shutw(rep);
3344// if (!(req->flags & BF_SHUTR)) {
3345// EV_FD_CLR(t->cli_fd, DIR_WR);
3346// shutdown(t->cli_fd, SHUT_WR);
3347// trace_term(t, TT_HTTP_CLI_8);
3348// } else {
3349// fd_delete(t->cli_fd);
3350// t->cli_state = CL_STCLOSE;
3351// trace_term(t, TT_HTTP_CLI_9);
3352// }
3353// if (!req->analysers) {
3354// if (!(t->flags & SN_ERR_MASK))
3355// t->flags |= SN_ERR_CLITO;
3356// if (!(t->flags & SN_FINST_MASK)) {
3357// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3358// t->flags |= SN_FINST_Q;
3359// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3360// t->flags |= SN_FINST_C;
3361// else
3362// t->flags |= SN_FINST_D;
3363// }
3364// }
3365// goto update_state;
3366// }
3367//
3368// update_timeouts:
3369// /* manage read timeout */
3370// if (!(req->flags & BF_SHUTR)) {
3371// if (req->flags & BF_FULL) {
3372// /* no room to read more data */
3373// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3374// /* stop reading until we get some space */
3375// req->rex = TICK_ETERNITY;
3376// }
3377// } else {
3378// EV_FD_COND_S(t->cli_fd, DIR_RD);
3379// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3380// }
3381// }
3382//
3383// /* manage write timeout */
3384// if (!(rep->flags & BF_SHUTW)) {
3385// /* first, we may have to produce data (eg: stats).
3386// * right now, this is limited to the SHUTR state.
3387// */
3388// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
3389// produce_content(t);
3390// if (rep->flags & BF_EMPTY) {
3391// buffer_shutw(rep);
3392// fd_delete(t->cli_fd);
3393// t->cli_state = CL_STCLOSE;
3394// trace_term(t, TT_HTTP_CLI_10);
3395// goto update_state;
3396// }
3397// }
3398//
3399// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
3400// if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
3401// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3402// /* stop writing */
3403// rep->wex = TICK_ETERNITY;
3404// }
3405// } else {
3406// /* buffer not empty */
3407// EV_FD_COND_S(t->cli_fd, DIR_WR);
3408// if (!tick_isset(rep->wex)) {
3409// /* restart writing */
3410// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
3411// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
3412// /* FIXME: to prevent the client from expiring read timeouts during writes,
3413// * we refresh it, except if it was already infinite. */
3414// req->rex = rep->wex;
3415// }
3416// }
3417// }
3418// }
3419// return 0; /* other cases change nothing */
3420// }
3421// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
3422// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3423// int len;
3424// 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);
3425// write(1, trash, len);
3426// }
3427// return 0;
3428// }
3429//#ifdef DEBUG_DEV
3430// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3431// ABORT_NOW();
3432//#endif
3433// return 0;
3434//}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003435
Willy Tarreaubaaee002006-06-26 02:48:02 +02003436
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003437/* Return 1 if the pending connection has failed and should be retried,
3438 * otherwise zero. We may only come here in SI_ST_CON state, which means that
3439 * the socket's file descriptor is known.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003440 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003441int tcp_connection_status(struct session *t)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003442{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003443 struct buffer *req = t->req;
3444 struct buffer *rep = t->rep;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003445 int conn_err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003446
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003447 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3448 now_ms, __FUNCTION__,
3449 cli_stnames[t->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003450 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003451 req->flags, rep->flags,
3452 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003453
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003454 if ((req->flags & BF_SHUTW_NOW) ||
3455 (rep->flags & BF_SHUTW) ||
3456 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3457 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) ||
3458 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3459
3460 trace_term(t, TT_HTTP_SRV_5);
3461 req->wex = TICK_ETERNITY;
3462 fd_delete(req->cons->fd);
3463 if (t->srv) {
3464 t->srv->cur_sess--;
3465 sess_change_server(t, NULL);
3466 }
3467 /* note that this must not return any error because it would be able to
3468 * overwrite the client_retnclose() output.
3469 */
3470 //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3471
3472 // FIXME: should we set rep->MAY_FORWARD ?
3473 buffer_shutw(req);
3474 buffer_shutr(rep);
3475 req->cons->state = SI_ST_CLO;
3476 if (!req->cons->err_type)
3477 req->cons->err_type = SI_ET_CONN_ABRT;
3478 req->cons->err_loc = t->srv;
3479 return 0;
3480 }
3481
3482 /* check for timeouts and asynchronous connect errors */
3483 if (fdtab[req->cons->fd].state == FD_STERROR) {
3484 conn_err = SI_ET_CONN_ERR;
3485 if (!req->cons->err_type)
3486 req->cons->err_type = SI_ET_CONN_ERR;
3487 }
3488 else if (!(req->flags & BF_WRITE_STATUS)) {
3489 /* nothing happened, maybe we timed out */
3490 if (tick_is_expired(req->wex, now_ms)) {
3491 conn_err = SI_ET_CONN_TO;
3492 if (!req->cons->err_type)
3493 req->cons->err_type = SI_ET_CONN_TO;
3494 }
3495 else
3496 return 0; /* let's wait a bit more */
3497 }
3498
3499 if (conn_err) {
3500 fd_delete(req->cons->fd);
3501 req->cons->state = SI_ST_CLO;
3502
3503 if (t->srv) {
3504 t->srv->cur_sess--;
3505 sess_change_server(t, NULL);
3506 req->cons->err_loc = t->srv;
3507 }
3508
3509 /* ensure that we have enough retries left */
3510 if (srv_count_retry_down(t, conn_err))
3511 return 0;
3512
3513 if (conn_err == SI_ET_CONN_ERR) {
3514 /* we encountered an immediate connection error, and we
3515 * will have to retry connecting to the same server, most
3516 * likely leading to the same result. To avoid this, we
3517 * fake a connection timeout to retry after a turn-around
3518 * time of 1 second. We will wait in the previous if block.
3519 */
3520 req->cons->state = SI_ST_TAR;
3521 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
3522 return 0;
3523 }
3524
3525 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3526 /* We're on our last chance, and the REDISP option was specified.
3527 * We will ignore cookie and force to balance or use the dispatcher.
3528 */
3529 /* let's try to offer this slot to anybody */
3530 if (may_dequeue_tasks(t->srv, t->be))
3531 process_srv_queue(t->srv);
3532
3533 /* it's left to the dispatcher to choose a server */
3534 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3535 t->prev_srv = t->srv;
3536 } else {
3537 /* we just want to retry */
3538 if (t->srv)
3539 t->srv->retries++;
3540 t->be->retries++;
3541
3542 /* Now we will try to either reconnect to the same server or
3543 * connect to another server. If the connection gets queued
3544 * because all servers are saturated, then we will go back to
3545 * the idle state where the buffer's consumer is marked as
3546 * unknown.
3547 */
3548 if (srv_retryable_connect(t)) {
3549 /* success or unrecoverable error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003550 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003551 return 0;
3552 }
3553 }
3554
3555 /* We'll rely on the caller to try to get a connection again */
3556 return 1;
3557 }
3558 else {
3559 /* no error and write OK : connection succeeded */
3560 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
3561 req->cons->state = SI_ST_EST;
3562 req->cons->err_type = SI_ET_NONE;
3563 req->cons->err_loc = NULL;
3564
3565 if (req->flags & BF_EMPTY) {
3566 EV_FD_CLR(req->cons->fd, DIR_WR);
3567 req->wex = TICK_ETERNITY;
3568 } else {
3569 EV_FD_SET(req->cons->fd, DIR_WR);
3570 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3571 if (tick_isset(req->wex)) {
3572 /* FIXME: to prevent the server from expiring read timeouts during writes,
3573 * we refresh it. */
3574 rep->rex = req->wex;
3575 }
3576 }
3577
3578 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3579 if (!(rep->flags & BF_HIJACK)) {
3580 EV_FD_SET(req->cons->fd, DIR_RD);
3581 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3582 }
3583 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
3584
3585 /* if the user wants to log as soon as possible, without counting
3586 bytes from the server, then this is the right moment. */
3587 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3588 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3589 tcp_sess_log(t);
3590 }
3591#ifdef CONFIG_HAP_TCPSPLICE
3592 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3593 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003594 tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003595 }
3596#endif
3597 }
3598 else {
3599 rep->analysers |= AN_RTR_HTTP_HDR;
3600 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3601 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3602 /* reset hdr_idx which was already initialized by the request.
3603 * right now, the http parser does it.
3604 * hdr_idx_init(&t->txn.hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003605 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003606 }
3607
3608 if (!rep->analysers)
3609 t->rep->flags |= BF_MAY_FORWARD;
3610 req->wex = TICK_ETERNITY;
3611 return 0;
3612 }
3613}
3614
3615
3616/*
3617 * This function tries to assign a server to a stream_sock interface.
3618 * It may be called only for t->req->cons->state = one of { SI_ST_INI,
3619 * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS
3620 * in case of success, or SI_ST_CLO in case of failure. It returns 1 if
3621 * it returns SI_ST_ASS, otherwise zero.
3622 */
3623int stream_sock_assign_server(struct session *t)
3624{
3625 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3626 now_ms, __FUNCTION__,
3627 cli_stnames[t->cli_state],
3628 t->rep->rex, t->req->wex,
3629 t->req->flags, t->rep->flags,
3630 t->req->l, t->rep->l);
3631
3632 if (t->req->cons->state == SI_ST_TAR) {
3633 /* connection might be aborted */
3634 if ((t->req->flags & BF_SHUTW_NOW) ||
3635 (t->rep->flags & BF_SHUTW) ||
3636 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3637 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003638
Willy Tarreauf8533202008-08-16 14:55:08 +02003639 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003640 t->req->wex = TICK_ETERNITY;
3641
3642 // FIXME: should we set rep->MAY_FORWARD ?
3643 buffer_shutr(t->rep);
3644 buffer_shutw(t->req);
3645 if (!t->req->cons->err_type)
3646 t->req->cons->err_type = SI_ET_CONN_ABRT;
3647 t->req->cons->state = SI_ST_CLO;
3648 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003649 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003650
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003651 if (!tick_is_expired(t->req->wex, now_ms))
3652 return 0; /* still in turn-around */
3653
3654 t->req->cons->state = SI_ST_INI;
3655 }
3656 else if (t->req->cons->state == SI_ST_QUE) {
3657 if (t->pend_pos) {
3658 /* request still in queue... */
3659 if (tick_is_expired(t->req->wex, now_ms)) {
3660 /* ... and timeout expired */
3661 trace_term(t, TT_HTTP_SRV_3);
3662 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003663 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003664 if (t->srv)
3665 t->srv->failed_conns++;
3666 t->be->failed_conns++;
3667
3668 // FIXME: should we set rep->MAY_FORWARD ?
3669 buffer_shutr(t->rep);
3670 buffer_shutw(t->req);
3671 t->req->flags |= BF_WRITE_TIMEOUT;
3672 if (!t->req->cons->err_type)
3673 t->req->cons->err_type = SI_ET_QUEUE_TO;
3674 t->req->cons->state = SI_ST_CLO;
3675 return 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003676 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003677 /* connection remains in queue, check if we have to abort it */
3678 if ((t->req->flags & BF_SHUTW_NOW) ||
3679 (t->rep->flags & BF_SHUTW) ||
3680 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3681 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) {
3682 /* give up */
3683 trace_term(t, TT_HTTP_SRV_1);
3684 t->req->wex = TICK_ETERNITY;
3685 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003686
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003687 // FIXME: should we set rep->MAY_FORWARD ?
3688 buffer_shutr(t->rep);
3689 buffer_shutw(t->req);
3690 if (!t->req->cons->err_type)
3691 t->req->cons->err_type = SI_ET_QUEUE_ABRT;
3692 t->req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003693 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003694 return 0;
3695 }
3696 /* The connection is not in the queue anymore */
3697 t->req->cons->state = SI_ST_INI;
3698 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003699
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003700 /* we may get here from above */
3701 if (t->req->cons->state == SI_ST_INI) {
3702 /* no connection in progress, we have to get a new one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003703
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003704 /* first, check if the connection has been aborted */
3705 if ((t->req->flags & BF_SHUTW_NOW) ||
3706 (t->rep->flags & BF_SHUTW) ||
3707 ((t->req->flags & BF_SHUTR) &&
3708 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003709
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003710 trace_term(t, TT_HTTP_SRV_1);
3711 t->req->wex = TICK_ETERNITY;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003712
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003713 // FIXME: should we set rep->MAY_FORWARD ?
3714 buffer_shutr(t->rep);
3715 buffer_shutw(t->req);
3716 if (!t->req->cons->err_type)
3717 t->req->cons->err_type = SI_ET_CONN_ABRT;
3718 t->req->cons->state = SI_ST_CLO;
3719 return 0;
3720 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003721
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003722 /* try to get a server assigned */
3723 if (srv_redispatch_connect(t) != 0) {
3724 /* we did not get any server, let's check the cause */
3725 if (t->req->cons->state == SI_ST_QUE) {
3726 /* the connection was queued, that's OK */
3727 return 0;
3728 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003729
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003730 trace_term(t, TT_HTTP_SRV_2);
3731 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003732
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003733 // FIXME: should we set rep->MAY_FORWARD ?
3734 buffer_shutr(t->rep);
3735 buffer_shutw(t->req);
3736 t->req->flags |= BF_WRITE_ERROR;
3737 if (!t->req->cons->err_type)
3738 t->req->cons->err_type = SI_ET_CONN_OTHER;
3739 t->req->cons->state = SI_ST_CLO;
3740 return 0;
3741 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003742
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003743 t->req->cons->state = SI_ST_ASS;
3744 /* Once the server is assigned, we have to return because
3745 * the caller might be interested in checking several
3746 * things before connecting.
3747 */
3748 return 1;
3749 }
3750 return 0;
3751}
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003752
Willy Tarreauf8533202008-08-16 14:55:08 +02003753
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003754/*
3755 * This function tries to establish a connection to an assigned server. It also
3756 * performs connection retries. It may only be called with t->req->cons->state
3757 * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI,
3758 * SI_ST_EST, or SI_ST_CLO.
3759 */
3760int stream_sock_connect_server(struct session *t)
3761{
3762 if (t->req->cons->state == SI_ST_ASS) {
3763 /* server assigned to request, we have to try to connect now */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003764
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003765 if (!srv_retryable_connect(t)) {
3766 /* we need to redispatch */
3767 t->req->cons->state = SI_ST_INI;
3768 return 0;
3769 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003770
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003771 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3772 if (t->req->cons->state != SI_ST_CON) {
3773 /* it was an error */
3774 trace_term(t, TT_HTTP_SRV_4);
3775 t->req->wex = TICK_ETERNITY;
3776
3777 // FIXME: should we set rep->MAY_FORWARD ?
3778 buffer_shutr(t->rep);
3779 buffer_shutw(t->req);
3780 t->req->flags |= BF_WRITE_ERROR;
3781 if (!t->req->cons->err_type)
3782 t->req->cons->err_type = SI_ET_CONN_OTHER;
3783 t->req->cons->state = SI_ST_CLO;
3784 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003785 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003786 /* We have a socket and switched to SI_ST_CON */
3787 }
3788
3789 /* we may also get here from above */
3790 if (t->req->cons->state == SI_ST_CON) {
3791 /* connection in progress or just completed */
3792 if (!tcp_connection_status(t))
3793 return 0;
3794 }
3795 return 0;
3796}
3797
3798
3799/*
3800 * Tries to establish a connection to the server and associate it to the
3801 * request buffer's consumer side. It is assumed that this function will not be
3802 * be called with SI_ST_EST nor with BF_MAY_FORWARD cleared. It normally
3803 * returns zero, but may return 1 if it absolutely wants to be called again.
3804 */
3805int process_srv_conn(struct session *t)
3806{
3807 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3808 now_ms, __FUNCTION__,
3809 cli_stnames[t->cli_state],
3810 t->rep->rex, t->req->wex,
3811 t->req->flags, t->rep->flags,
3812 t->req->l, t->rep->l);
3813
3814 do {
3815 if (t->req->cons->state == SI_ST_INI ||
3816 t->req->cons->state == SI_ST_TAR ||
3817 t->req->cons->state == SI_ST_QUE) {
3818 /* try to assign a server */
3819 if (!stream_sock_assign_server(t))
3820 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003821 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003822
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003823 if (t->req->cons->state == SI_ST_ASS &&
3824 t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
3825 /* Server supporting redirection and it is possible.
3826 * Invalid requests are reported as such. It concerns all
3827 * the largest ones.
3828 */
3829 struct http_txn *txn = &t->txn;
3830 struct chunk rdr;
3831 char *path;
3832 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003833
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003834 /* 1: create the response header */
3835 rdr.len = strlen(HTTP_302);
3836 rdr.str = trash;
3837 memcpy(rdr.str, HTTP_302, rdr.len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003838
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003839 /* 2: add the server's prefix */
3840 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3841 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003842
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003843 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3844 rdr.len += t->srv->rdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003845
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003846 /* 3: add the request URI */
3847 path = http_get_path(txn);
3848 if (!path)
3849 goto cancel_redir;
3850 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3851 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3852 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003853
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003854 memcpy(rdr.str + rdr.len, path, len);
3855 rdr.len += len;
3856 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3857 rdr.len += 4;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003858
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003859 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3860 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003861
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003862 /* FIXME: we should increase a counter of redirects per server and per backend. */
3863 if (t->srv)
3864 t->srv->cum_sess++;
3865
3866 t->req->cons->state = SI_ST_CLO;
3867 return 0;
3868 cancel_redir:
3869 //txn->status = 400;
3870 //t->fe->failed_req++;
3871 //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3872 // 400, error_message(t, HTTP_ERR_400));
3873 trace_term(t, TT_HTTP_SRV_4);
3874
3875 // FIXME: should we set rep->MAY_FORWARD ?
3876 buffer_shutw(t->req);
3877 buffer_shutr(t->rep);
3878 if (!t->req->cons->err_type)
3879 t->req->cons->err_type = SI_ET_CONN_OTHER;
3880 t->req->cons->state = SI_ST_CLO;
3881 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003882 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003883
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003884 if (t->req->cons->state == SI_ST_CON ||
3885 t->req->cons->state == SI_ST_ASS) {
3886 stream_sock_connect_server(t);
3887 }
3888 } while (t->req->cons->state != SI_ST_CLO &&
3889 t->req->cons->state != SI_ST_CON &&
3890 t->req->cons->state != SI_ST_EST);
3891 return 0;
3892}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003893
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003894
Willy Tarreaubaaee002006-06-26 02:48:02 +02003895/*
3896 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003897 * called with client socket shut down on input. Right now, only statistics can
3898 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02003899 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003900 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003901 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003902 */
3903int produce_content(struct session *s)
3904{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003905 if (s->data_source == DATA_SRC_NONE) {
3906 s->flags &= ~SN_SELF_GEN;
3907 return 1;
3908 }
3909 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003910 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003911 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003912 if (ret >= 0)
3913 return ret;
3914 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003915 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003916
Willy Tarreau91861262007-10-17 17:06:05 +02003917 /* unknown data source or internal error */
3918 s->txn.status = 500;
3919 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003920 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003921 if (!(s->flags & SN_ERR_MASK))
3922 s->flags |= SN_ERR_PRXCOND;
3923 if (!(s->flags & SN_FINST_MASK))
3924 s->flags |= SN_FINST_R;
3925 s->flags &= ~SN_SELF_GEN;
3926 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003927}
3928
3929
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003930/* Iterate the same filter through all request headers.
3931 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003932 * Since it can manage the switch to another backend, it updates the per-proxy
3933 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003934 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003935int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003936{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003937 char term;
3938 char *cur_ptr, *cur_end, *cur_next;
3939 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003940 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003941 struct hdr_idx_elem *cur_hdr;
3942 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003943
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003944 last_hdr = 0;
3945
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003946 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003947 old_idx = 0;
3948
3949 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003950 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003951 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003952 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003953 (exp->action == ACT_ALLOW ||
3954 exp->action == ACT_DENY ||
3955 exp->action == ACT_TARPIT))
3956 return 0;
3957
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003958 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003959 if (!cur_idx)
3960 break;
3961
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003962 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003963 cur_ptr = cur_next;
3964 cur_end = cur_ptr + cur_hdr->len;
3965 cur_next = cur_end + cur_hdr->cr + 1;
3966
3967 /* Now we have one header between cur_ptr and cur_end,
3968 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003969 */
3970
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003971 /* The annoying part is that pattern matching needs
3972 * that we modify the contents to null-terminate all
3973 * strings before testing them.
3974 */
3975
3976 term = *cur_end;
3977 *cur_end = '\0';
3978
3979 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3980 switch (exp->action) {
3981 case ACT_SETBE:
3982 /* It is not possible to jump a second time.
3983 * FIXME: should we return an HTTP/500 here so that
3984 * the admin knows there's a problem ?
3985 */
3986 if (t->be != t->fe)
3987 break;
3988
3989 /* Swithing Proxy */
3990 t->be = (struct proxy *) exp->replace;
3991
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003992 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003993 * because we have associated req_cap and rsp_cap to the
3994 * frontend, and the beconn will be updated later.
3995 */
3996
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003997 t->rep->rto = t->req->wto = t->be->timeout.server;
3998 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003999 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004000 last_hdr = 1;
4001 break;
4002
4003 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004004 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004005 last_hdr = 1;
4006 break;
4007
4008 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004009 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004010 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004011 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004012 break;
4013
4014 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004015 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004016 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004017 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004018 break;
4019
4020 case ACT_REPLACE:
4021 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4022 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4023 /* FIXME: if the user adds a newline in the replacement, the
4024 * index will not be recalculated for now, and the new line
4025 * will not be counted as a new header.
4026 */
4027
4028 cur_end += delta;
4029 cur_next += delta;
4030 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004031 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004032 break;
4033
4034 case ACT_REMOVE:
4035 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4036 cur_next += delta;
4037
4038 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004039 txn->req.eoh += delta;
4040 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4041 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004042 cur_hdr->len = 0;
4043 cur_end = NULL; /* null-term has been rewritten */
4044 break;
4045
4046 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004047 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004048 if (cur_end)
4049 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004050
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004051 /* keep the link from this header to next one in case of later
4052 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004053 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004054 old_idx = cur_idx;
4055 }
4056 return 0;
4057}
4058
4059
4060/* Apply the filter to the request line.
4061 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4062 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004063 * Since it can manage the switch to another backend, it updates the per-proxy
4064 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004065 */
4066int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4067{
4068 char term;
4069 char *cur_ptr, *cur_end;
4070 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004071 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004072 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004073
Willy Tarreau58f10d72006-12-04 02:26:12 +01004074
Willy Tarreau3d300592007-03-18 18:34:41 +01004075 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004076 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004077 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004078 (exp->action == ACT_ALLOW ||
4079 exp->action == ACT_DENY ||
4080 exp->action == ACT_TARPIT))
4081 return 0;
4082 else if (exp->action == ACT_REMOVE)
4083 return 0;
4084
4085 done = 0;
4086
Willy Tarreau9cdde232007-05-02 20:58:19 +02004087 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004088 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004089
4090 /* Now we have the request line between cur_ptr and cur_end */
4091
4092 /* The annoying part is that pattern matching needs
4093 * that we modify the contents to null-terminate all
4094 * strings before testing them.
4095 */
4096
4097 term = *cur_end;
4098 *cur_end = '\0';
4099
4100 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4101 switch (exp->action) {
4102 case ACT_SETBE:
4103 /* It is not possible to jump a second time.
4104 * FIXME: should we return an HTTP/500 here so that
4105 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004106 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004107 if (t->be != t->fe)
4108 break;
4109
4110 /* Swithing Proxy */
4111 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004113 /* right now, the backend switch is not too much complicated
4114 * because we have associated req_cap and rsp_cap to the
4115 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004116 */
4117
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004118 t->rep->rto = t->req->wto = t->be->timeout.server;
4119 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004120 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004121 done = 1;
4122 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004124 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004125 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004126 done = 1;
4127 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004129 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004130 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004131 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004132 done = 1;
4133 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004134
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004135 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004136 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004137 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004138 done = 1;
4139 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004140
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004141 case ACT_REPLACE:
4142 *cur_end = term; /* restore the string terminator */
4143 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4144 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4145 /* FIXME: if the user adds a newline in the replacement, the
4146 * index will not be recalculated for now, and the new line
4147 * will not be counted as a new header.
4148 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004149
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004150 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004151 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004152
Willy Tarreau9cdde232007-05-02 20:58:19 +02004153 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004154 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004155 HTTP_MSG_RQMETH,
4156 cur_ptr, cur_end + 1,
4157 NULL, NULL);
4158 if (unlikely(!cur_end))
4159 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004160
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004161 /* we have a full request and we know that we have either a CR
4162 * or an LF at <ptr>.
4163 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004164 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4165 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004166 /* there is no point trying this regex on headers */
4167 return 1;
4168 }
4169 }
4170 *cur_end = term; /* restore the string terminator */
4171 return done;
4172}
Willy Tarreau97de6242006-12-27 17:18:38 +01004173
Willy Tarreau58f10d72006-12-04 02:26:12 +01004174
Willy Tarreau58f10d72006-12-04 02:26:12 +01004175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004176/*
4177 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4178 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004179 * unparsable request. Since it can manage the switch to another backend, it
4180 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004181 */
4182int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4183{
Willy Tarreau3d300592007-03-18 18:34:41 +01004184 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004185 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004186 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004187 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004188
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004189 /*
4190 * The interleaving of transformations and verdicts
4191 * makes it difficult to decide to continue or stop
4192 * the evaluation.
4193 */
4194
Willy Tarreau3d300592007-03-18 18:34:41 +01004195 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004196 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4197 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4198 exp = exp->next;
4199 continue;
4200 }
4201
4202 /* Apply the filter to the request line. */
4203 ret = apply_filter_to_req_line(t, req, exp);
4204 if (unlikely(ret < 0))
4205 return -1;
4206
4207 if (likely(ret == 0)) {
4208 /* The filter did not match the request, it can be
4209 * iterated through all headers.
4210 */
4211 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004212 }
4213 exp = exp->next;
4214 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004215 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004216}
4217
4218
Willy Tarreaua15645d2007-03-18 16:22:39 +01004219
Willy Tarreau58f10d72006-12-04 02:26:12 +01004220/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004221 * Manage client-side cookie. It can impact performance by about 2% so it is
4222 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004223 */
4224void manage_client_side_cookies(struct session *t, struct buffer *req)
4225{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004226 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004227 char *p1, *p2, *p3, *p4;
4228 char *del_colon, *del_cookie, *colon;
4229 int app_cookies;
4230
4231 appsess *asession_temp = NULL;
4232 appsess local_asession;
4233
4234 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004235 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004236
Willy Tarreau2a324282006-12-05 00:05:46 +01004237 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004238 * we start with the start line.
4239 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004240 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004241 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004242
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004243 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004244 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004245 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004246
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004247 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004248 cur_ptr = cur_next;
4249 cur_end = cur_ptr + cur_hdr->len;
4250 cur_next = cur_end + cur_hdr->cr + 1;
4251
4252 /* We have one full header between cur_ptr and cur_end, and the
4253 * next header starts at cur_next. We're only interested in
4254 * "Cookie:" headers.
4255 */
4256
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004257 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4258 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004259 old_idx = cur_idx;
4260 continue;
4261 }
4262
4263 /* Now look for cookies. Conforming to RFC2109, we have to support
4264 * attributes whose name begin with a '$', and associate them with
4265 * the right cookie, if we want to delete this cookie.
4266 * So there are 3 cases for each cookie read :
4267 * 1) it's a special attribute, beginning with a '$' : ignore it.
4268 * 2) it's a server id cookie that we *MAY* want to delete : save
4269 * some pointers on it (last semi-colon, beginning of cookie...)
4270 * 3) it's an application cookie : we *MAY* have to delete a previous
4271 * "special" cookie.
4272 * At the end of loop, if a "special" cookie remains, we may have to
4273 * remove it. If no application cookie persists in the header, we
4274 * *MUST* delete it
4275 */
4276
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004277 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004278
Willy Tarreau58f10d72006-12-04 02:26:12 +01004279 /* del_cookie == NULL => nothing to be deleted */
4280 del_colon = del_cookie = NULL;
4281 app_cookies = 0;
4282
4283 while (p1 < cur_end) {
4284 /* skip spaces and colons, but keep an eye on these ones */
4285 while (p1 < cur_end) {
4286 if (*p1 == ';' || *p1 == ',')
4287 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004288 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004289 break;
4290 p1++;
4291 }
4292
4293 if (p1 == cur_end)
4294 break;
4295
4296 /* p1 is at the beginning of the cookie name */
4297 p2 = p1;
4298 while (p2 < cur_end && *p2 != '=')
4299 p2++;
4300
4301 if (p2 == cur_end)
4302 break;
4303
4304 p3 = p2 + 1; /* skips the '=' sign */
4305 if (p3 == cur_end)
4306 break;
4307
4308 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004309 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004310 p4++;
4311
4312 /* here, we have the cookie name between p1 and p2,
4313 * and its value between p3 and p4.
4314 * we can process it :
4315 *
4316 * Cookie: NAME=VALUE;
4317 * | || || |
4318 * | || || +--> p4
4319 * | || |+-------> p3
4320 * | || +--------> p2
4321 * | |+------------> p1
4322 * | +-------------> colon
4323 * +--------------------> cur_ptr
4324 */
4325
4326 if (*p1 == '$') {
4327 /* skip this one */
4328 }
4329 else {
4330 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004331 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004332 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004333 (p4 - p1 >= t->fe->capture_namelen) &&
4334 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004335 int log_len = p4 - p1;
4336
Willy Tarreau086b3b42007-05-13 21:45:51 +02004337 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004338 Alert("HTTP logging : out of memory.\n");
4339 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004340 if (log_len > t->fe->capture_len)
4341 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004342 memcpy(txn->cli_cookie, p1, log_len);
4343 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004344 }
4345 }
4346
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004347 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4348 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004349 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004350 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004351 char *delim;
4352
4353 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4354 * have the server ID betweek p3 and delim, and the original cookie between
4355 * delim+1 and p4. Otherwise, delim==p4 :
4356 *
4357 * Cookie: NAME=SRV~VALUE;
4358 * | || || | |
4359 * | || || | +--> p4
4360 * | || || +--------> delim
4361 * | || |+-----------> p3
4362 * | || +------------> p2
4363 * | |+----------------> p1
4364 * | +-----------------> colon
4365 * +------------------------> cur_ptr
4366 */
4367
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004368 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004369 for (delim = p3; delim < p4; delim++)
4370 if (*delim == COOKIE_DELIM)
4371 break;
4372 }
4373 else
4374 delim = p4;
4375
4376
4377 /* Here, we'll look for the first running server which supports the cookie.
4378 * This allows to share a same cookie between several servers, for example
4379 * to dedicate backup servers to specific servers only.
4380 * However, to prevent clients from sticking to cookie-less backup server
4381 * when they have incidentely learned an empty cookie, we simply ignore
4382 * empty cookies and mark them as invalid.
4383 */
4384 if (delim == p3)
4385 srv = NULL;
4386
4387 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004388 if (srv->cookie && (srv->cklen == delim - p3) &&
4389 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004390 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004391 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004392 txn->flags &= ~TX_CK_MASK;
4393 txn->flags |= TX_CK_VALID;
4394 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004395 t->srv = srv;
4396 break;
4397 } else {
4398 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004399 txn->flags &= ~TX_CK_MASK;
4400 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004401 }
4402 }
4403 srv = srv->next;
4404 }
4405
Willy Tarreau3d300592007-03-18 18:34:41 +01004406 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004407 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004408 txn->flags &= ~TX_CK_MASK;
4409 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004410 }
4411
4412 /* depending on the cookie mode, we may have to either :
4413 * - delete the complete cookie if we're in insert+indirect mode, so that
4414 * the server never sees it ;
4415 * - remove the server id from the cookie value, and tag the cookie as an
4416 * application cookie so that it does not get accidentely removed later,
4417 * if we're in cookie prefix mode
4418 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004419 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004420 int delta; /* negative */
4421
4422 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4423 p4 += delta;
4424 cur_end += delta;
4425 cur_next += delta;
4426 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004427 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004428
4429 del_cookie = del_colon = NULL;
4430 app_cookies++; /* protect the header from deletion */
4431 }
4432 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004433 (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 +01004434 del_cookie = p1;
4435 del_colon = colon;
4436 }
4437 } else {
4438 /* now we know that we must keep this cookie since it's
4439 * not ours. But if we wanted to delete our cookie
4440 * earlier, we cannot remove the complete header, but we
4441 * can remove the previous block itself.
4442 */
4443 app_cookies++;
4444
4445 if (del_cookie != NULL) {
4446 int delta; /* negative */
4447
4448 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4449 p4 += delta;
4450 cur_end += delta;
4451 cur_next += delta;
4452 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004453 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004454 del_cookie = del_colon = NULL;
4455 }
4456 }
4457
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004458 if ((t->be->appsession_name != NULL) &&
4459 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004460 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004461
Willy Tarreau58f10d72006-12-04 02:26:12 +01004462 /* Cool... it's the right one */
4463
4464 asession_temp = &local_asession;
4465
Willy Tarreau63963c62007-05-13 21:29:55 +02004466 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004467 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4468 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4469 return;
4470 }
4471
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004472 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4473 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004474 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004475
Willy Tarreau58f10d72006-12-04 02:26:12 +01004476 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004477 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4478 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004479 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004480 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004481 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004482 Alert("Not enough memory process_cli():asession:calloc().\n");
4483 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4484 return;
4485 }
4486
4487 asession_temp->sessid = local_asession.sessid;
4488 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004489 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004490 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004491 } else {
4492 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004493 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004494 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004495 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004496 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004497 Alert("Found Application Session without matching server.\n");
4498 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004499 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004500 while (srv) {
4501 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004502 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004503 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004504 txn->flags &= ~TX_CK_MASK;
4505 txn->flags |= TX_CK_VALID;
4506 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004507 t->srv = srv;
4508 break;
4509 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004510 txn->flags &= ~TX_CK_MASK;
4511 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004512 }
4513 }
4514 srv = srv->next;
4515 }/* end while(srv) */
4516 }/* end else if server == NULL */
4517
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004518 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004519 asession_temp->request_count++;
4520#if defined(DEBUG_HASH)
4521 Alert("manage_client_side_cookies\n");
4522 appsession_hash_dump(&(t->be->htbl_proxy));
4523#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004524 }/* end if ((t->proxy->appsession_name != NULL) ... */
4525 }
4526
4527 /* we'll have to look for another cookie ... */
4528 p1 = p4;
4529 } /* while (p1 < cur_end) */
4530
4531 /* There's no more cookie on this line.
4532 * We may have marked the last one(s) for deletion.
4533 * We must do this now in two ways :
4534 * - if there is no app cookie, we simply delete the header ;
4535 * - if there are app cookies, we must delete the end of the
4536 * string properly, including the colon/semi-colon before
4537 * the cookie name.
4538 */
4539 if (del_cookie != NULL) {
4540 int delta;
4541 if (app_cookies) {
4542 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4543 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004544 cur_hdr->len += delta;
4545 } else {
4546 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004547
4548 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004549 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4550 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004551 cur_hdr->len = 0;
4552 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004553 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004554 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004555 }
4556
4557 /* keep the link from this header to next one */
4558 old_idx = cur_idx;
4559 } /* end of cookie processing on this header */
4560}
4561
4562
Willy Tarreaua15645d2007-03-18 16:22:39 +01004563/* Iterate the same filter through all response headers contained in <rtr>.
4564 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4565 */
4566int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4567{
4568 char term;
4569 char *cur_ptr, *cur_end, *cur_next;
4570 int cur_idx, old_idx, last_hdr;
4571 struct http_txn *txn = &t->txn;
4572 struct hdr_idx_elem *cur_hdr;
4573 int len, delta;
4574
4575 last_hdr = 0;
4576
4577 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4578 old_idx = 0;
4579
4580 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004581 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004582 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004583 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004584 (exp->action == ACT_ALLOW ||
4585 exp->action == ACT_DENY))
4586 return 0;
4587
4588 cur_idx = txn->hdr_idx.v[old_idx].next;
4589 if (!cur_idx)
4590 break;
4591
4592 cur_hdr = &txn->hdr_idx.v[cur_idx];
4593 cur_ptr = cur_next;
4594 cur_end = cur_ptr + cur_hdr->len;
4595 cur_next = cur_end + cur_hdr->cr + 1;
4596
4597 /* Now we have one header between cur_ptr and cur_end,
4598 * and the next header starts at cur_next.
4599 */
4600
4601 /* The annoying part is that pattern matching needs
4602 * that we modify the contents to null-terminate all
4603 * strings before testing them.
4604 */
4605
4606 term = *cur_end;
4607 *cur_end = '\0';
4608
4609 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4610 switch (exp->action) {
4611 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004612 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004613 last_hdr = 1;
4614 break;
4615
4616 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004617 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004618 last_hdr = 1;
4619 break;
4620
4621 case ACT_REPLACE:
4622 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4623 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4624 /* FIXME: if the user adds a newline in the replacement, the
4625 * index will not be recalculated for now, and the new line
4626 * will not be counted as a new header.
4627 */
4628
4629 cur_end += delta;
4630 cur_next += delta;
4631 cur_hdr->len += delta;
4632 txn->rsp.eoh += delta;
4633 break;
4634
4635 case ACT_REMOVE:
4636 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4637 cur_next += delta;
4638
4639 /* FIXME: this should be a separate function */
4640 txn->rsp.eoh += delta;
4641 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4642 txn->hdr_idx.used--;
4643 cur_hdr->len = 0;
4644 cur_end = NULL; /* null-term has been rewritten */
4645 break;
4646
4647 }
4648 }
4649 if (cur_end)
4650 *cur_end = term; /* restore the string terminator */
4651
4652 /* keep the link from this header to next one in case of later
4653 * removal of next header.
4654 */
4655 old_idx = cur_idx;
4656 }
4657 return 0;
4658}
4659
4660
4661/* Apply the filter to the status line in the response buffer <rtr>.
4662 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4663 * or -1 if a replacement resulted in an invalid status line.
4664 */
4665int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4666{
4667 char term;
4668 char *cur_ptr, *cur_end;
4669 int done;
4670 struct http_txn *txn = &t->txn;
4671 int len, delta;
4672
4673
Willy Tarreau3d300592007-03-18 18:34:41 +01004674 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004675 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004676 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004677 (exp->action == ACT_ALLOW ||
4678 exp->action == ACT_DENY))
4679 return 0;
4680 else if (exp->action == ACT_REMOVE)
4681 return 0;
4682
4683 done = 0;
4684
Willy Tarreau9cdde232007-05-02 20:58:19 +02004685 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004686 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4687
4688 /* Now we have the status line between cur_ptr and cur_end */
4689
4690 /* The annoying part is that pattern matching needs
4691 * that we modify the contents to null-terminate all
4692 * strings before testing them.
4693 */
4694
4695 term = *cur_end;
4696 *cur_end = '\0';
4697
4698 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4699 switch (exp->action) {
4700 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004701 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004702 done = 1;
4703 break;
4704
4705 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004706 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004707 done = 1;
4708 break;
4709
4710 case ACT_REPLACE:
4711 *cur_end = term; /* restore the string terminator */
4712 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4713 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4714 /* FIXME: if the user adds a newline in the replacement, the
4715 * index will not be recalculated for now, and the new line
4716 * will not be counted as a new header.
4717 */
4718
4719 txn->rsp.eoh += delta;
4720 cur_end += delta;
4721
Willy Tarreau9cdde232007-05-02 20:58:19 +02004722 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004723 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004724 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004725 cur_ptr, cur_end + 1,
4726 NULL, NULL);
4727 if (unlikely(!cur_end))
4728 return -1;
4729
4730 /* we have a full respnse and we know that we have either a CR
4731 * or an LF at <ptr>.
4732 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004733 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004734 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4735 /* there is no point trying this regex on headers */
4736 return 1;
4737 }
4738 }
4739 *cur_end = term; /* restore the string terminator */
4740 return done;
4741}
4742
4743
4744
4745/*
4746 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4747 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4748 * unparsable response.
4749 */
4750int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4751{
Willy Tarreau3d300592007-03-18 18:34:41 +01004752 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004753 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004754 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004755 int ret;
4756
4757 /*
4758 * The interleaving of transformations and verdicts
4759 * makes it difficult to decide to continue or stop
4760 * the evaluation.
4761 */
4762
Willy Tarreau3d300592007-03-18 18:34:41 +01004763 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004764 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4765 exp->action == ACT_PASS)) {
4766 exp = exp->next;
4767 continue;
4768 }
4769
4770 /* Apply the filter to the status line. */
4771 ret = apply_filter_to_sts_line(t, rtr, exp);
4772 if (unlikely(ret < 0))
4773 return -1;
4774
4775 if (likely(ret == 0)) {
4776 /* The filter did not match the response, it can be
4777 * iterated through all headers.
4778 */
4779 apply_filter_to_resp_headers(t, rtr, exp);
4780 }
4781 exp = exp->next;
4782 }
4783 return 0;
4784}
4785
4786
4787
4788/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004789 * Manage server-side cookies. It can impact performance by about 2% so it is
4790 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004791 */
4792void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4793{
4794 struct http_txn *txn = &t->txn;
4795 char *p1, *p2, *p3, *p4;
4796
4797 appsess *asession_temp = NULL;
4798 appsess local_asession;
4799
4800 char *cur_ptr, *cur_end, *cur_next;
4801 int cur_idx, old_idx, delta;
4802
Willy Tarreaua15645d2007-03-18 16:22:39 +01004803 /* Iterate through the headers.
4804 * we start with the start line.
4805 */
4806 old_idx = 0;
4807 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4808
4809 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4810 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004811 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004812
4813 cur_hdr = &txn->hdr_idx.v[cur_idx];
4814 cur_ptr = cur_next;
4815 cur_end = cur_ptr + cur_hdr->len;
4816 cur_next = cur_end + cur_hdr->cr + 1;
4817
4818 /* We have one full header between cur_ptr and cur_end, and the
4819 * next header starts at cur_next. We're only interested in
4820 * "Cookie:" headers.
4821 */
4822
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004823 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4824 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004825 old_idx = cur_idx;
4826 continue;
4827 }
4828
4829 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004830 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004831
4832
4833 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004834 if (t->be->cookie_name == NULL &&
4835 t->be->appsession_name == NULL &&
4836 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004837 return;
4838
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004839 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004840
4841 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004842 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4843 break;
4844
4845 /* p1 is at the beginning of the cookie name */
4846 p2 = p1;
4847
4848 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4849 p2++;
4850
4851 if (p2 == cur_end || *p2 == ';') /* next cookie */
4852 break;
4853
4854 p3 = p2 + 1; /* skip the '=' sign */
4855 if (p3 == cur_end)
4856 break;
4857
4858 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004859 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004860 p4++;
4861
4862 /* here, we have the cookie name between p1 and p2,
4863 * and its value between p3 and p4.
4864 * we can process it.
4865 */
4866
4867 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004868 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004869 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004870 (p4 - p1 >= t->be->capture_namelen) &&
4871 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004872 int log_len = p4 - p1;
4873
Willy Tarreau086b3b42007-05-13 21:45:51 +02004874 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004875 Alert("HTTP logging : out of memory.\n");
4876 }
4877
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004878 if (log_len > t->be->capture_len)
4879 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004880 memcpy(txn->srv_cookie, p1, log_len);
4881 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004882 }
4883
4884 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004885 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4886 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004887 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004888 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004889
4890 /* If the cookie is in insert mode on a known server, we'll delete
4891 * this occurrence because we'll insert another one later.
4892 * We'll delete it too if the "indirect" option is set and we're in
4893 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004894 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4895 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004896 /* this header must be deleted */
4897 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4898 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4899 txn->hdr_idx.used--;
4900 cur_hdr->len = 0;
4901 cur_next += delta;
4902 txn->rsp.eoh += delta;
4903
Willy Tarreau3d300592007-03-18 18:34:41 +01004904 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004905 }
4906 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004907 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004908 /* replace bytes p3->p4 with the cookie name associated
4909 * with this server since we know it.
4910 */
4911 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4912 cur_hdr->len += delta;
4913 cur_next += delta;
4914 txn->rsp.eoh += delta;
4915
Willy Tarreau3d300592007-03-18 18:34:41 +01004916 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004917 }
4918 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004919 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004920 /* insert the cookie name associated with this server
4921 * before existing cookie, and insert a delimitor between them..
4922 */
4923 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4924 cur_hdr->len += delta;
4925 cur_next += delta;
4926 txn->rsp.eoh += delta;
4927
4928 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004929 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004930 }
4931 }
4932 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004933 else if ((t->be->appsession_name != NULL) &&
4934 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004935
4936 /* Cool... it's the right one */
4937
4938 size_t server_id_len = strlen(t->srv->id) + 1;
4939 asession_temp = &local_asession;
4940
Willy Tarreau63963c62007-05-13 21:29:55 +02004941 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004942 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4943 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4944 return;
4945 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004946 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4947 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004948 asession_temp->serverid = NULL;
4949
4950 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004951 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4952 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004953 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004954 Alert("Not enough Memory process_srv():asession:calloc().\n");
4955 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4956 return;
4957 }
4958 asession_temp->sessid = local_asession.sessid;
4959 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004960 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004961 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4962 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004963 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004964 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004965 }
4966
Willy Tarreaua15645d2007-03-18 16:22:39 +01004967 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004968 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004969 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4970 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4971 return;
4972 }
4973 asession_temp->serverid[0] = '\0';
4974 }
4975
4976 if (asession_temp->serverid[0] == '\0')
4977 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4978
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004979 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004980 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004981#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004982 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004983 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004984#endif
4985 }/* end if ((t->proxy->appsession_name != NULL) ... */
4986 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4987 } /* we're now at the end of the cookie value */
4988
4989 /* keep the link from this header to next one */
4990 old_idx = cur_idx;
4991 } /* end of cookie processing on this header */
4992}
4993
4994
4995
4996/*
4997 * Check if response is cacheable or not. Updates t->flags.
4998 */
4999void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5000{
5001 struct http_txn *txn = &t->txn;
5002 char *p1, *p2;
5003
5004 char *cur_ptr, *cur_end, *cur_next;
5005 int cur_idx;
5006
Willy Tarreau5df51872007-11-25 16:20:08 +01005007 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005008 return;
5009
5010 /* Iterate through the headers.
5011 * we start with the start line.
5012 */
5013 cur_idx = 0;
5014 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5015
5016 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5017 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005018 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005019
5020 cur_hdr = &txn->hdr_idx.v[cur_idx];
5021 cur_ptr = cur_next;
5022 cur_end = cur_ptr + cur_hdr->len;
5023 cur_next = cur_end + cur_hdr->cr + 1;
5024
5025 /* We have one full header between cur_ptr and cur_end, and the
5026 * next header starts at cur_next. We're only interested in
5027 * "Cookie:" headers.
5028 */
5029
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005030 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5031 if (val) {
5032 if ((cur_end - (cur_ptr + val) >= 8) &&
5033 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5034 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5035 return;
5036 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005037 }
5038
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005039 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5040 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005041 continue;
5042
5043 /* OK, right now we know we have a cache-control header at cur_ptr */
5044
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005045 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005046
5047 if (p1 >= cur_end) /* no more info */
5048 continue;
5049
5050 /* p1 is at the beginning of the value */
5051 p2 = p1;
5052
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005053 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005054 p2++;
5055
5056 /* we have a complete value between p1 and p2 */
5057 if (p2 < cur_end && *p2 == '=') {
5058 /* we have something of the form no-cache="set-cookie" */
5059 if ((cur_end - p1 >= 21) &&
5060 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5061 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005062 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005063 continue;
5064 }
5065
5066 /* OK, so we know that either p2 points to the end of string or to a comma */
5067 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5068 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5069 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5070 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005071 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005072 return;
5073 }
5074
5075 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005076 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005077 continue;
5078 }
5079 }
5080}
5081
5082
Willy Tarreau58f10d72006-12-04 02:26:12 +01005083/*
5084 * Try to retrieve a known appsession in the URI, then the associated server.
5085 * If the server is found, it's assigned to the session.
5086 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005087void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005088{
Willy Tarreau3d300592007-03-18 18:34:41 +01005089 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005090 appsess *asession_temp = NULL;
5091 appsess local_asession;
5092 char *request_line;
5093
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005094 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005095 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005096 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005097 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005098 return;
5099
5100 /* skip ';' */
5101 request_line++;
5102
5103 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005104 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005105 return;
5106
5107 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005108 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005109
5110 /* First try if we already have an appsession */
5111 asession_temp = &local_asession;
5112
Willy Tarreau63963c62007-05-13 21:29:55 +02005113 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005114 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5115 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5116 return;
5117 }
5118
5119 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005120 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5121 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005122 asession_temp->serverid = NULL;
5123
5124 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005125 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5126 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005127 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005128 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005129 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005130 Alert("Not enough memory process_cli():asession:calloc().\n");
5131 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5132 return;
5133 }
5134 asession_temp->sessid = local_asession.sessid;
5135 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005136 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005137 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005138 }
5139 else {
5140 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005141 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005142 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005143
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005144 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005145 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005146
Willy Tarreau58f10d72006-12-04 02:26:12 +01005147#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005148 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005149 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005150#endif
5151 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005152 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005153 Alert("Found Application Session without matching server.\n");
5154 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005155 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005156 while (srv) {
5157 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005158 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005159 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005160 txn->flags &= ~TX_CK_MASK;
5161 txn->flags |= TX_CK_VALID;
5162 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005163 t->srv = srv;
5164 break;
5165 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005166 txn->flags &= ~TX_CK_MASK;
5167 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005168 }
5169 }
5170 srv = srv->next;
5171 }
5172 }
5173}
5174
5175
Willy Tarreaub2513902006-12-17 14:52:38 +01005176/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005177 * In a GET or HEAD request, check if the requested URI matches the stats uri
5178 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005179 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005180 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005181 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005182 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005183 *
5184 * Returns 1 if the session's state changes, otherwise 0.
5185 */
5186int stats_check_uri_auth(struct session *t, struct proxy *backend)
5187{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005188 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005189 struct uri_auth *uri_auth = backend->uri_auth;
5190 struct user_auth *user;
5191 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005192 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005193
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005194 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5195
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005196 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005197 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005198 return 0;
5199
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005200 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005201
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005202 /* the URI is in h */
5203 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005204 return 0;
5205
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005206 h += uri_auth->uri_len;
5207 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5208 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005209 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005210 break;
5211 }
5212 h++;
5213 }
5214
5215 if (uri_auth->refresh) {
5216 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5217 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5218 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005219 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005220 break;
5221 }
5222 h++;
5223 }
5224 }
5225
Willy Tarreau55bb8452007-10-17 18:44:57 +02005226 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 - 4) {
5228 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005229 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005230 break;
5231 }
5232 h++;
5233 }
5234
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005235 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5236
Willy Tarreaub2513902006-12-17 14:52:38 +01005237 /* we are in front of a interceptable URI. Let's check
5238 * if there's an authentication and if it's valid.
5239 */
5240 user = uri_auth->users;
5241 if (!user) {
5242 /* no user auth required, it's OK */
5243 authenticated = 1;
5244 } else {
5245 authenticated = 0;
5246
5247 /* a user list is defined, we have to check.
5248 * skip 21 chars for "Authorization: Basic ".
5249 */
5250
5251 /* FIXME: this should move to an earlier place */
5252 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005253 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5254 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5255 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005256 if (len > 14 &&
5257 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005258 txn->auth_hdr.str = h;
5259 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005260 break;
5261 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005262 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005263 }
5264
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005265 if (txn->auth_hdr.len < 21 ||
5266 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005267 user = NULL;
5268
5269 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005270 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5271 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005272 user->user_pwd, user->user_len)) {
5273 authenticated = 1;
5274 break;
5275 }
5276 user = user->next;
5277 }
5278 }
5279
5280 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005281 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005282
5283 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005284 msg.str = trash;
5285 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005286 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005287 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005288 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005289 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005290 if (!(t->flags & SN_ERR_MASK))
5291 t->flags |= SN_ERR_PRXCOND;
5292 if (!(t->flags & SN_FINST_MASK))
5293 t->flags |= SN_FINST_R;
5294 return 1;
5295 }
5296
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005297 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005298 * data.
5299 */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005300 EV_FD_CLR(t->req->prod->fd, DIR_RD);
Willy Tarreau284c7b32008-06-29 16:38:43 +02005301 buffer_shutr(t->req);
5302 buffer_shutr(t->rep);
Willy Tarreaue393fe22008-08-16 22:18:07 +02005303 buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005304 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005305 t->data_source = DATA_SRC_STATS;
5306 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005307 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005308 produce_content(t);
5309 return 1;
5310}
5311
5312
Willy Tarreaubaaee002006-06-26 02:48:02 +02005313/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005314 * Print a debug line with a header
5315 */
5316void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5317{
5318 int len, max;
5319 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005320 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005321 max = end - start;
5322 UBOUND(max, sizeof(trash) - len - 1);
5323 len += strlcpy2(trash + len, start, max + 1);
5324 trash[len++] = '\n';
5325 write(1, trash, len);
5326}
5327
5328
Willy Tarreau8797c062007-05-07 00:55:35 +02005329/************************************************************************/
5330/* The code below is dedicated to ACL parsing and matching */
5331/************************************************************************/
5332
5333
5334
5335
5336/* 1. Check on METHOD
5337 * We use the pre-parsed method if it is known, and store its number as an
5338 * integer. If it is unknown, we use the pointer and the length.
5339 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005340static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005341{
5342 int len, meth;
5343
Willy Tarreauae8b7962007-06-09 23:10:04 +02005344 len = strlen(*text);
5345 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005346
5347 pattern->val.i = meth;
5348 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005349 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005350 if (!pattern->ptr.str)
5351 return 0;
5352 pattern->len = len;
5353 }
5354 return 1;
5355}
5356
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005357static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005358acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5359 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005360{
5361 int meth;
5362 struct http_txn *txn = l7;
5363
Willy Tarreaub6866442008-07-14 23:54:42 +02005364 if (!txn)
5365 return 0;
5366
Willy Tarreauc11416f2007-06-17 16:58:38 +02005367 if (txn->req.msg_state != HTTP_MSG_BODY)
5368 return 0;
5369
Willy Tarreau8797c062007-05-07 00:55:35 +02005370 meth = txn->meth;
5371 test->i = meth;
5372 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005373 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5374 /* ensure the indexes are not affected */
5375 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005376 test->len = txn->req.sl.rq.m_l;
5377 test->ptr = txn->req.sol;
5378 }
5379 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5380 return 1;
5381}
5382
5383static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5384{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005385 int icase;
5386
Willy Tarreau8797c062007-05-07 00:55:35 +02005387 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005388 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005389
5390 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005391 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005392
5393 /* Other method, we must compare the strings */
5394 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005395 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005396
5397 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5398 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5399 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005400 return ACL_PAT_FAIL;
5401 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005402}
5403
5404/* 2. Check on Request/Status Version
5405 * We simply compare strings here.
5406 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005407static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005408{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005409 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005410 if (!pattern->ptr.str)
5411 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005412 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005413 return 1;
5414}
5415
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005416static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005417acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5418 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005419{
5420 struct http_txn *txn = l7;
5421 char *ptr;
5422 int len;
5423
Willy Tarreaub6866442008-07-14 23:54:42 +02005424 if (!txn)
5425 return 0;
5426
Willy Tarreauc11416f2007-06-17 16:58:38 +02005427 if (txn->req.msg_state != HTTP_MSG_BODY)
5428 return 0;
5429
Willy Tarreau8797c062007-05-07 00:55:35 +02005430 len = txn->req.sl.rq.v_l;
5431 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5432
5433 while ((len-- > 0) && (*ptr++ != '/'));
5434 if (len <= 0)
5435 return 0;
5436
5437 test->ptr = ptr;
5438 test->len = len;
5439
5440 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5441 return 1;
5442}
5443
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005444static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005445acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5446 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005447{
5448 struct http_txn *txn = l7;
5449 char *ptr;
5450 int len;
5451
Willy Tarreaub6866442008-07-14 23:54:42 +02005452 if (!txn)
5453 return 0;
5454
Willy Tarreauc11416f2007-06-17 16:58:38 +02005455 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5456 return 0;
5457
Willy Tarreau8797c062007-05-07 00:55:35 +02005458 len = txn->rsp.sl.st.v_l;
5459 ptr = txn->rsp.sol;
5460
5461 while ((len-- > 0) && (*ptr++ != '/'));
5462 if (len <= 0)
5463 return 0;
5464
5465 test->ptr = ptr;
5466 test->len = len;
5467
5468 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5469 return 1;
5470}
5471
5472/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005473static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005474acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5475 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005476{
5477 struct http_txn *txn = l7;
5478 char *ptr;
5479 int len;
5480
Willy Tarreaub6866442008-07-14 23:54:42 +02005481 if (!txn)
5482 return 0;
5483
Willy Tarreauc11416f2007-06-17 16:58:38 +02005484 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5485 return 0;
5486
Willy Tarreau8797c062007-05-07 00:55:35 +02005487 len = txn->rsp.sl.st.c_l;
5488 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5489
5490 test->i = __strl2ui(ptr, len);
5491 test->flags = ACL_TEST_F_VOL_1ST;
5492 return 1;
5493}
5494
5495/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005496static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005497acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5498 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005499{
5500 struct http_txn *txn = l7;
5501
Willy Tarreaub6866442008-07-14 23:54:42 +02005502 if (!txn)
5503 return 0;
5504
Willy Tarreauc11416f2007-06-17 16:58:38 +02005505 if (txn->req.msg_state != HTTP_MSG_BODY)
5506 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005507
Willy Tarreauc11416f2007-06-17 16:58:38 +02005508 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5509 /* ensure the indexes are not affected */
5510 return 0;
5511
Willy Tarreau8797c062007-05-07 00:55:35 +02005512 test->len = txn->req.sl.rq.u_l;
5513 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5514
Willy Tarreauf3d25982007-05-08 22:45:09 +02005515 /* we do not need to set READ_ONLY because the data is in a buffer */
5516 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005517 return 1;
5518}
5519
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005520static int
5521acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5522 struct acl_expr *expr, struct acl_test *test)
5523{
5524 struct http_txn *txn = l7;
5525
Willy Tarreaub6866442008-07-14 23:54:42 +02005526 if (!txn)
5527 return 0;
5528
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005529 if (txn->req.msg_state != HTTP_MSG_BODY)
5530 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005531
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005532 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5533 /* ensure the indexes are not affected */
5534 return 0;
5535
5536 /* Parse HTTP request */
5537 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5538 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5539 test->i = AF_INET;
5540
5541 /*
5542 * If we are parsing url in frontend space, we prepare backend stage
5543 * to not parse again the same url ! optimization lazyness...
5544 */
5545 if (px->options & PR_O_HTTP_PROXY)
5546 l4->flags |= SN_ADDR_SET;
5547
5548 test->flags = ACL_TEST_F_READ_ONLY;
5549 return 1;
5550}
5551
5552static int
5553acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5554 struct acl_expr *expr, struct acl_test *test)
5555{
5556 struct http_txn *txn = l7;
5557
Willy Tarreaub6866442008-07-14 23:54:42 +02005558 if (!txn)
5559 return 0;
5560
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005561 if (txn->req.msg_state != HTTP_MSG_BODY)
5562 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005563
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005564 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5565 /* ensure the indexes are not affected */
5566 return 0;
5567
5568 /* Same optimization as url_ip */
5569 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5570 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5571
5572 if (px->options & PR_O_HTTP_PROXY)
5573 l4->flags |= SN_ADDR_SET;
5574
5575 test->flags = ACL_TEST_F_READ_ONLY;
5576 return 1;
5577}
5578
Willy Tarreauc11416f2007-06-17 16:58:38 +02005579/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5580 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5581 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005582static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005583acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005584 struct acl_expr *expr, struct acl_test *test)
5585{
5586 struct http_txn *txn = l7;
5587 struct hdr_idx *idx = &txn->hdr_idx;
5588 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005589
Willy Tarreaub6866442008-07-14 23:54:42 +02005590 if (!txn)
5591 return 0;
5592
Willy Tarreau33a7e692007-06-10 19:45:56 +02005593 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5594 /* search for header from the beginning */
5595 ctx->idx = 0;
5596
Willy Tarreau33a7e692007-06-10 19:45:56 +02005597 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5598 test->flags |= ACL_TEST_F_FETCH_MORE;
5599 test->flags |= ACL_TEST_F_VOL_HDR;
5600 test->len = ctx->vlen;
5601 test->ptr = (char *)ctx->line + ctx->val;
5602 return 1;
5603 }
5604
5605 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5606 test->flags |= ACL_TEST_F_VOL_HDR;
5607 return 0;
5608}
5609
Willy Tarreau33a7e692007-06-10 19:45:56 +02005610static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005611acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5612 struct acl_expr *expr, struct acl_test *test)
5613{
5614 struct http_txn *txn = l7;
5615
Willy Tarreaub6866442008-07-14 23:54:42 +02005616 if (!txn)
5617 return 0;
5618
Willy Tarreauc11416f2007-06-17 16:58:38 +02005619 if (txn->req.msg_state != HTTP_MSG_BODY)
5620 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005621
Willy Tarreauc11416f2007-06-17 16:58:38 +02005622 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5623 /* ensure the indexes are not affected */
5624 return 0;
5625
5626 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5627}
5628
5629static int
5630acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5631 struct acl_expr *expr, struct acl_test *test)
5632{
5633 struct http_txn *txn = l7;
5634
Willy Tarreaub6866442008-07-14 23:54:42 +02005635 if (!txn)
5636 return 0;
5637
Willy Tarreauc11416f2007-06-17 16:58:38 +02005638 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5639 return 0;
5640
5641 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5642}
5643
5644/* 6. Check on HTTP header count. The number of occurrences is returned.
5645 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5646 */
5647static int
5648acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005649 struct acl_expr *expr, struct acl_test *test)
5650{
5651 struct http_txn *txn = l7;
5652 struct hdr_idx *idx = &txn->hdr_idx;
5653 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005654 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005655
Willy Tarreaub6866442008-07-14 23:54:42 +02005656 if (!txn)
5657 return 0;
5658
Willy Tarreau33a7e692007-06-10 19:45:56 +02005659 ctx.idx = 0;
5660 cnt = 0;
5661 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5662 cnt++;
5663
5664 test->i = cnt;
5665 test->flags = ACL_TEST_F_VOL_HDR;
5666 return 1;
5667}
5668
Willy Tarreauc11416f2007-06-17 16:58:38 +02005669static int
5670acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5671 struct acl_expr *expr, struct acl_test *test)
5672{
5673 struct http_txn *txn = l7;
5674
Willy Tarreaub6866442008-07-14 23:54:42 +02005675 if (!txn)
5676 return 0;
5677
Willy Tarreauc11416f2007-06-17 16:58:38 +02005678 if (txn->req.msg_state != HTTP_MSG_BODY)
5679 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005680
Willy Tarreauc11416f2007-06-17 16:58:38 +02005681 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5682 /* ensure the indexes are not affected */
5683 return 0;
5684
5685 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5686}
5687
5688static int
5689acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5690 struct acl_expr *expr, struct acl_test *test)
5691{
5692 struct http_txn *txn = l7;
5693
Willy Tarreaub6866442008-07-14 23:54:42 +02005694 if (!txn)
5695 return 0;
5696
Willy Tarreauc11416f2007-06-17 16:58:38 +02005697 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5698 return 0;
5699
5700 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5701}
5702
Willy Tarreau33a7e692007-06-10 19:45:56 +02005703/* 7. Check on HTTP header's integer value. The integer value is returned.
5704 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005705 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005706 */
5707static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005708acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005709 struct acl_expr *expr, struct acl_test *test)
5710{
5711 struct http_txn *txn = l7;
5712 struct hdr_idx *idx = &txn->hdr_idx;
5713 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005714
Willy Tarreaub6866442008-07-14 23:54:42 +02005715 if (!txn)
5716 return 0;
5717
Willy Tarreau33a7e692007-06-10 19:45:56 +02005718 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5719 /* search for header from the beginning */
5720 ctx->idx = 0;
5721
Willy Tarreau33a7e692007-06-10 19:45:56 +02005722 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5723 test->flags |= ACL_TEST_F_FETCH_MORE;
5724 test->flags |= ACL_TEST_F_VOL_HDR;
5725 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5726 return 1;
5727 }
5728
5729 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5730 test->flags |= ACL_TEST_F_VOL_HDR;
5731 return 0;
5732}
5733
Willy Tarreauc11416f2007-06-17 16:58:38 +02005734static int
5735acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5736 struct acl_expr *expr, struct acl_test *test)
5737{
5738 struct http_txn *txn = l7;
5739
Willy Tarreaub6866442008-07-14 23:54:42 +02005740 if (!txn)
5741 return 0;
5742
Willy Tarreauc11416f2007-06-17 16:58:38 +02005743 if (txn->req.msg_state != HTTP_MSG_BODY)
5744 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005745
Willy Tarreauc11416f2007-06-17 16:58:38 +02005746 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5747 /* ensure the indexes are not affected */
5748 return 0;
5749
5750 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5751}
5752
5753static int
5754acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5755 struct acl_expr *expr, struct acl_test *test)
5756{
5757 struct http_txn *txn = l7;
5758
Willy Tarreaub6866442008-07-14 23:54:42 +02005759 if (!txn)
5760 return 0;
5761
Willy Tarreauc11416f2007-06-17 16:58:38 +02005762 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5763 return 0;
5764
5765 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5766}
5767
Willy Tarreau737b0c12007-06-10 21:28:46 +02005768/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5769 * the first '/' after the possible hostname, and ends before the possible '?'.
5770 */
5771static int
5772acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5773 struct acl_expr *expr, struct acl_test *test)
5774{
5775 struct http_txn *txn = l7;
5776 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005777
Willy Tarreaub6866442008-07-14 23:54:42 +02005778 if (!txn)
5779 return 0;
5780
Willy Tarreauc11416f2007-06-17 16:58:38 +02005781 if (txn->req.msg_state != HTTP_MSG_BODY)
5782 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005783
Willy Tarreauc11416f2007-06-17 16:58:38 +02005784 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5785 /* ensure the indexes are not affected */
5786 return 0;
5787
Willy Tarreau21d2af32008-02-14 20:25:24 +01005788 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5789 ptr = http_get_path(txn);
5790 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005791 return 0;
5792
5793 /* OK, we got the '/' ! */
5794 test->ptr = ptr;
5795
5796 while (ptr < end && *ptr != '?')
5797 ptr++;
5798
5799 test->len = ptr - test->ptr;
5800
5801 /* we do not need to set READ_ONLY because the data is in a buffer */
5802 test->flags = ACL_TEST_F_VOL_1ST;
5803 return 1;
5804}
5805
5806
Willy Tarreau8797c062007-05-07 00:55:35 +02005807
5808/************************************************************************/
5809/* All supported keywords must be declared here. */
5810/************************************************************************/
5811
5812/* Note: must not be declared <const> as its list will be overwritten */
5813static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005814 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5815 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5816 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5817 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005818
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005819 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5820 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5821 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5822 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5823 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5824 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5825 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5826 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5827 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005828
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005829 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5830 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5831 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5832 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5833 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5834 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5835 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5836 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5837 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5838 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005839
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005840 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5841 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5842 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5843 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5844 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5845 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5846 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5847 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5848 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005849
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005850 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5851 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5852 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5853 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5854 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5855 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5856 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005857
Willy Tarreauf3d25982007-05-08 22:45:09 +02005858 { NULL, NULL, NULL, NULL },
5859
5860#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005861 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5862 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5863 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5864 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5865 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5866 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5867 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5868
Willy Tarreau8797c062007-05-07 00:55:35 +02005869 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5870 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5871 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5872 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5873 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5874 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5875 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5876 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5877
5878 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5879 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5880 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5881 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5882 { NULL, NULL, NULL, NULL },
5883#endif
5884}};
5885
5886
5887__attribute__((constructor))
5888static void __http_protocol_init(void)
5889{
5890 acl_register_keywords(&acl_kws);
5891}
5892
5893
Willy Tarreau58f10d72006-12-04 02:26:12 +01005894/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005895 * Local variables:
5896 * c-indent-level: 8
5897 * c-basic-offset: 8
5898 * End:
5899 */