blob: bc8d3fbf23832f4bb4f503dd0142a647661d6e2b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/fd.h>
46#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010047#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/session.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020053#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/task.h>
55
Willy Tarreau6d1a9882007-01-07 02:03:04 +010056#ifdef CONFIG_HAP_TCPSPLICE
57#include <libtcpsplice.h>
58#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau58f10d72006-12-04 02:26:12 +010060#define DEBUG_PARSE_NO_SPEEDUP
61#undef DEBUG_PARSE_NO_SPEEDUP
62
Willy Tarreau976f1ee2006-12-17 10:06:03 +010063/* This is used to perform a quick jump as an alternative to a break/continue
64 * instruction. The first argument is the label for normal operation, and the
65 * second one is the break/continue instruction in the no_speedup mode.
66 */
67
68#ifdef DEBUG_PARSE_NO_SPEEDUP
69#define QUICK_JUMP(x,y) y
70#else
71#define QUICK_JUMP(x,y) goto x
72#endif
73
Willy Tarreau1c47f852006-07-09 08:22:27 +020074/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010075const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020076 "HTTP/1.0 200 OK\r\n"
77 "Cache-Control: no-cache\r\n"
78 "Connection: close\r\n"
79 "Content-Type: text/html\r\n"
80 "\r\n"
81 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const struct chunk http_200_chunk = {
84 .str = (char *)&HTTP_200,
85 .len = sizeof(HTTP_200)-1
86};
87
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088const char *HTTP_301 =
89 "HTTP/1.0 301 Moved Permantenly\r\n"
90 "Cache-Control: no-cache\r\n"
91 "Connection: close\r\n"
92 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Willy Tarreau0f772532006-12-23 20:51:41 +010094const char *HTTP_302 =
95 "HTTP/1.0 302 Found\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
98 "Location: "; /* not terminated since it will be concatenated with the URL */
99
100/* same as 302 except that the browser MUST retry with the GET method */
101const char *HTTP_303 =
102 "HTTP/1.0 303 See Other\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Connection: close\r\n"
105 "Location: "; /* not terminated since it will be concatenated with the URL */
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
108const char *HTTP_401_fmt =
109 "HTTP/1.0 401 Unauthorized\r\n"
110 "Cache-Control: no-cache\r\n"
111 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200112 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
114 "\r\n"
115 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
116
Willy Tarreau0f772532006-12-23 20:51:41 +0100117
118const int http_err_codes[HTTP_ERR_SIZE] = {
119 [HTTP_ERR_400] = 400,
120 [HTTP_ERR_403] = 403,
121 [HTTP_ERR_408] = 408,
122 [HTTP_ERR_500] = 500,
123 [HTTP_ERR_502] = 502,
124 [HTTP_ERR_503] = 503,
125 [HTTP_ERR_504] = 504,
126};
127
Willy Tarreau80587432006-12-24 17:47:20 +0100128static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100129 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100130 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "\r\n"
135 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
136
137 [HTTP_ERR_403] =
138 "HTTP/1.0 403 Forbidden\r\n"
139 "Cache-Control: no-cache\r\n"
140 "Connection: close\r\n"
141 "Content-Type: text/html\r\n"
142 "\r\n"
143 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
144
145 [HTTP_ERR_408] =
146 "HTTP/1.0 408 Request Time-out\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
152
153 [HTTP_ERR_500] =
154 "HTTP/1.0 500 Server Error\r\n"
155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
160
161 [HTTP_ERR_502] =
162 "HTTP/1.0 502 Bad Gateway\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
168
169 [HTTP_ERR_503] =
170 "HTTP/1.0 503 Service Unavailable\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
176
177 [HTTP_ERR_504] =
178 "HTTP/1.0 504 Gateway Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
184
185};
186
Willy Tarreau80587432006-12-24 17:47:20 +0100187/* We must put the messages here since GCC cannot initialize consts depending
188 * on strlen().
189 */
190struct chunk http_err_chunks[HTTP_ERR_SIZE];
191
Willy Tarreau42250582007-04-01 01:30:43 +0200192#define FD_SETS_ARE_BITFIELDS
193#ifdef FD_SETS_ARE_BITFIELDS
194/*
195 * This map is used with all the FD_* macros to check whether a particular bit
196 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
197 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
198 * byte should be encoded. Be careful to always pass bytes from 0 to 255
199 * exclusively to the macros.
200 */
201fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
203
204#else
205#error "Check if your OS uses bitfields for fd_sets"
206#endif
207
Willy Tarreau80587432006-12-24 17:47:20 +0100208void init_proto_http()
209{
Willy Tarreau42250582007-04-01 01:30:43 +0200210 int i;
211 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100212 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200213
Willy Tarreau80587432006-12-24 17:47:20 +0100214 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
215 if (!http_err_msgs[msg]) {
216 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
217 abort();
218 }
219
220 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
221 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
222 }
Willy Tarreau42250582007-04-01 01:30:43 +0200223
224 /* initialize the log header encoding map : '{|}"#' should be encoded with
225 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
226 * URL encoding only requires '"', '#' to be encoded as well as non-
227 * printable characters above.
228 */
229 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
230 memset(url_encode_map, 0, sizeof(url_encode_map));
231 for (i = 0; i < 32; i++) {
232 FD_SET(i, hdr_encode_map);
233 FD_SET(i, url_encode_map);
234 }
235 for (i = 127; i < 256; i++) {
236 FD_SET(i, hdr_encode_map);
237 FD_SET(i, url_encode_map);
238 }
239
240 tmp = "\"#{|}";
241 while (*tmp) {
242 FD_SET(*tmp, hdr_encode_map);
243 tmp++;
244 }
245
246 tmp = "\"#";
247 while (*tmp) {
248 FD_SET(*tmp, url_encode_map);
249 tmp++;
250 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200251
252 /* memory allocations */
253 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200254 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100255}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256
Willy Tarreau53b6c742006-12-17 13:37:46 +0100257/*
258 * We have 26 list of methods (1 per first letter), each of which can have
259 * up to 3 entries (2 valid, 1 null).
260 */
261struct http_method_desc {
262 http_meth_t meth;
263 int len;
264 const char text[8];
265};
266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100267const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100268 ['C' - 'A'] = {
269 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
270 },
271 ['D' - 'A'] = {
272 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
273 },
274 ['G' - 'A'] = {
275 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
276 },
277 ['H' - 'A'] = {
278 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
279 },
280 ['P' - 'A'] = {
281 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
282 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
283 },
284 ['T' - 'A'] = {
285 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
286 },
287 /* rest is empty like this :
288 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
289 */
290};
291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100292/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200293 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100294 * RFC2616 for those chars.
295 */
296
297const char http_is_spht[256] = {
298 [' '] = 1, ['\t'] = 1,
299};
300
301const char http_is_crlf[256] = {
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_lws[256] = {
306 [' '] = 1, ['\t'] = 1,
307 ['\r'] = 1, ['\n'] = 1,
308};
309
310const char http_is_sep[256] = {
311 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
312 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
313 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
314 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
315 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
316};
317
318const char http_is_ctl[256] = {
319 [0 ... 31] = 1,
320 [127] = 1,
321};
322
323/*
324 * A token is any ASCII char that is neither a separator nor a CTL char.
325 * Do not overwrite values in assignment since gcc-2.95 will not handle
326 * them correctly. Instead, define every non-CTL char's status.
327 */
328const char http_is_token[256] = {
329 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
330 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
331 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
332 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
333 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
334 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
335 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
336 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
337 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
338 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
339 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
340 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
341 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
342 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
343 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
344 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
345 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
346 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
347 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
348 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
349 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
350 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
351 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
352 ['|'] = 1, ['}'] = 0, ['~'] = 1,
353};
354
355
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100356/*
357 * An http ver_token is any ASCII which can be found in an HTTP version,
358 * which includes 'H', 'T', 'P', '/', '.' and any digit.
359 */
360const char http_is_ver_token[256] = {
361 ['.'] = 1, ['/'] = 1,
362 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
363 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
364 ['H'] = 1, ['P'] = 1, ['T'] = 1,
365};
366
367
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200369static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau42250582007-04-01 01:30:43 +0200372static void http_sess_log(struct session *s);
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
375 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
376 * CRLF. Text length is measured first, so it cannot be NULL.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text)
383{
384 int bytes, len;
385
386 len = strlen(text);
387 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
388 if (!bytes)
389 return -1;
390 msg->eoh += bytes;
391 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
392}
393
394/*
395 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
396 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
397 * the buffer is only opened and the space reserved, but nothing is copied.
398 * The header is also automatically added to the index <hdr_idx>, and the end
399 * of headers is automatically adjusted. The number of bytes added is returned
400 * on success, otherwise <0 is returned indicating an error.
401 */
402int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
403 struct hdr_idx *hdr_idx, const char *text, int len)
404{
405 int bytes;
406
407 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
408 if (!bytes)
409 return -1;
410 msg->eoh += bytes;
411 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
414/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100415 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
416 * If so, returns the position of the first non-space character relative to
417 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
418 * to return a pointer to the place after the first space. Returns 0 if the
419 * header name does not match. Checks are case-insensitive.
420 */
421int http_header_match2(const char *hdr, const char *end,
422 const char *name, int len)
423{
424 const char *val;
425
426 if (hdr + len >= end)
427 return 0;
428 if (hdr[len] != ':')
429 return 0;
430 if (strncasecmp(hdr, name, len) != 0)
431 return 0;
432 val = hdr + len + 1;
433 while (val < end && HTTP_IS_SPHT(*val))
434 val++;
435 if ((val >= end) && (len + 2 <= end - hdr))
436 return len + 2; /* we may replace starting from second space */
437 return val - hdr;
438}
439
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440/* Find the end of the header value contained between <s> and <e>.
441 * See RFC2616, par 2.2 for more information. Note that it requires
442 * a valid header to return a valid result.
443 */
444const char *find_hdr_value_end(const char *s, const char *e)
445{
446 int quoted, qdpair;
447
448 quoted = qdpair = 0;
449 for (; s < e; s++) {
450 if (qdpair) qdpair = 0;
451 else if (quoted && *s == '\\') qdpair = 1;
452 else if (quoted && *s == '"') quoted = 0;
453 else if (*s == '"') quoted = 1;
454 else if (*s == ',') return s;
455 }
456 return s;
457}
458
459/* Find the first or next occurrence of header <name> in message buffer <sol>
460 * using headers index <idx>, and return it in the <ctx> structure. This
461 * structure holds everything necessary to use the header and find next
462 * occurrence. If its <idx> member is 0, the header is searched from the
463 * beginning. Otherwise, the next occurrence is returned. The function returns
464 * 1 when it finds a value, and 0 when there is no more.
465 */
466int http_find_header2(const char *name, int len,
467 const char *sol, struct hdr_idx *idx,
468 struct hdr_ctx *ctx)
469{
470 __label__ return_hdr, next_hdr;
471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200540/* This function shuts down the buffers on the server side, and sets indicators
541 * accordingly. The server's fd is supposed to already be closed. Note that if
542 * <status> is 0, or if the message pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
544void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100545 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
Willy Tarreau3da77c52008-08-29 09:58:42 +0200547 buffer_write_ena(t->rep);
Willy Tarreauba392ce2008-08-16 21:13:23 +0200548 buffer_shutw(t->req);
549 buffer_shutr(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100550 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100551 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100552 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100553 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554 }
555 if (!(t->flags & SN_ERR_MASK))
556 t->flags |= err;
557 if (!(t->flags & SN_FINST_MASK))
558 t->flags |= finst;
559}
560
Willy Tarreau80587432006-12-24 17:47:20 +0100561/* This function returns the appropriate error location for the given session
562 * and message.
563 */
564
565struct chunk *error_message(struct session *s, int msgnum)
566{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200567 if (s->be->errmsg[msgnum].str)
568 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100569 else if (s->fe->errmsg[msgnum].str)
570 return &s->fe->errmsg[msgnum];
571 else
572 return &http_err_chunks[msgnum];
573}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574
Willy Tarreau53b6c742006-12-17 13:37:46 +0100575/*
576 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
577 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
578 */
579static http_meth_t find_http_meth(const char *str, const int len)
580{
581 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100582 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100583
584 m = ((unsigned)*str - 'A');
585
586 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100587 for (h = http_methods[m]; h->len > 0; h++) {
588 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100589 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100590 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100591 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 };
593 return HTTP_METH_OTHER;
594 }
595 return HTTP_METH_NONE;
596
597}
598
Willy Tarreau21d2af32008-02-14 20:25:24 +0100599/* Parse the URI from the given transaction (which is assumed to be in request
600 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
601 * It is returned otherwise.
602 */
603static char *
604http_get_path(struct http_txn *txn)
605{
606 char *ptr, *end;
607
608 ptr = txn->req.sol + txn->req.sl.rq.u;
609 end = ptr + txn->req.sl.rq.u_l;
610
611 if (ptr >= end)
612 return NULL;
613
614 /* RFC2616, par. 5.1.2 :
615 * Request-URI = "*" | absuri | abspath | authority
616 */
617
618 if (*ptr == '*')
619 return NULL;
620
621 if (isalpha((unsigned char)*ptr)) {
622 /* this is a scheme as described by RFC3986, par. 3.1 */
623 ptr++;
624 while (ptr < end &&
625 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
626 ptr++;
627 /* skip '://' */
628 if (ptr == end || *ptr++ != ':')
629 return NULL;
630 if (ptr == end || *ptr++ != '/')
631 return NULL;
632 if (ptr == end || *ptr++ != '/')
633 return NULL;
634 }
635 /* skip [user[:passwd]@]host[:[port]] */
636
637 while (ptr < end && *ptr != '/')
638 ptr++;
639
640 if (ptr == end)
641 return NULL;
642
643 /* OK, we got the '/' ! */
644 return ptr;
645}
646
Willy Tarreaudafde432008-08-17 01:00:46 +0200647/* Processes the client, server, request and response jobs of a session task,
648 * then puts it back to the wait queue in a clean state, or cleans up its
649 * resources if it must be deleted. Returns in <next> the date the task wants
650 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
651 * nothing too many times, the request and response buffers flags are monitored
652 * and each function is called only if at least another function has changed at
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200653 * least one flag it is interested in.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200655void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656{
657 struct session *s = t->context;
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200658 int resync;
659 unsigned int rqf_cli, rpf_cli;
660 unsigned int rqf_srv, rpf_srv;
661 unsigned int rqf_req, rpf_rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200663 /* check server-side errors during data phase */
664 if (s->req->cons->state == SI_ST_EST) {
665 stream_sock_data_check_errors(s->req->cons->fd);
666 /* When a server-side connection is released, we have to
667 * count it and check for pending connections on this server.
668 */
669 if (unlikely(s->req->cons->state == SI_ST_CLO)) {
670 /* Count server-side errors (but not timeouts). */
671 if (s->req->flags & BF_WRITE_ERROR) {
672 s->be->failed_resp++;
673 if (s->srv)
674 s->srv->failed_resp++;
675 }
676
677 if (s->srv) {
678 s->srv->cur_sess--;
679 sess_change_server(s, NULL);
680 if (may_dequeue_tasks(s->srv, s->be))
681 process_srv_queue(s->srv);
682 }
683 }
684 }
685
686 /* check client-side errors during data phase */
687 if (s->rep->cons->state == SI_ST_EST)
688 stream_sock_data_check_errors(s->rep->cons->fd);
689
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200690 /* force one first pass everywhere */
691 rqf_cli = rqf_srv = rqf_req = ~s->req->flags;
692 rpf_cli = rpf_srv = rpf_rep = ~s->rep->flags;
Willy Tarreau507385d2008-08-17 13:04:25 +0200693
Willy Tarreaubaaee002006-06-26 02:48:02 +0200694 do {
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200695 resync = 0;
Willy Tarreaudafde432008-08-17 01:00:46 +0200696
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200697 if (s->rep->cons->state != SI_ST_CLO) {
698 if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) ||
699 ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) {
700 resync = 1;
701 stream_sock_data_update(s->rep->cons->fd);
702 rqf_cli = s->req->flags;
703 rpf_cli = s->rep->flags;
704
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200705 if (unlikely((s->rep->cons->state == SI_ST_CLO) &&
706 (global.mode & MODE_DEBUG) &&
707 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
708 int len;
709 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200710 s->uniq_id, s->be->id, (unsigned short)s->rep->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200711 write(1, trash, len);
712 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200713 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200714 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200715
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200716
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200717 if (s->req->cons->state != SI_ST_CLO) {
718 if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) ||
719 ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) {
720 resync = 1;
721
Willy Tarreau3da77c52008-08-29 09:58:42 +0200722 if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_WRITE_ENA)
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200723 process_srv_conn(s);
724
725 if (s->req->cons->state == SI_ST_EST) {
Willy Tarreau3da77c52008-08-29 09:58:42 +0200726 if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) &&
Willy Tarreau376580a2008-08-27 18:52:22 +0200727 s->be->options & PR_O_FORCE_CLO &&
Willy Tarreau3da77c52008-08-29 09:58:42 +0200728 s->rep->flags & BF_READ_ACTIVITY) {
Willy Tarreau376580a2008-08-27 18:52:22 +0200729 /* We want to force the connection to the server to close,
730 * and the server has begun to respond. That's the right
731 * time.
732 */
733 buffer_shutw_now(s->req);
734 }
735
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200736 stream_sock_data_update(s->req->cons->fd);
Willy Tarreau376580a2008-08-27 18:52:22 +0200737
738 /* When a server-side connection is released, we have to
739 * count it and check for pending connections on this server.
740 */
741 if (s->req->cons->state == SI_ST_CLO) {
742 if (s->srv) {
743 s->srv->cur_sess--;
744 sess_change_server(s, NULL);
745 if (may_dequeue_tasks(s->srv, s->be))
746 process_srv_queue(s->srv);
747 }
748 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200749 }
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200750 rqf_srv = s->req->flags;
751 rpf_srv = s->rep->flags;
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200752
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200753 if (unlikely((s->req->cons->state == SI_ST_CLO) &&
754 (global.mode & MODE_DEBUG) &&
755 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
756 int len;
757 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200758 s->uniq_id, s->be->id, (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200759 write(1, trash, len);
760 }
761 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200762 }
763
764 if ((rqf_req ^ s->req->flags) & BF_MASK_ANALYSER) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200765 /* the analysers must block it themselves */
Willy Tarreau36e6a412008-08-28 11:07:58 +0200766 if (s->req->prod->state >= SI_ST_EST) {
767 resync = 1;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200768 buffer_write_ena(s->req);
Willy Tarreau36e6a412008-08-28 11:07:58 +0200769 if (s->req->analysers)
770 process_request(s);
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200771 }
772 rqf_req = s->req->flags;
773 }
774
Willy Tarreau72b179a2008-08-28 16:01:32 +0200775 if (unlikely(s->rep->flags & BF_HIJACK)) {
776 /* In inject mode, we wake up everytime something has
777 * happened on the write side of the buffer.
778 */
Willy Tarreau3da77c52008-08-29 09:58:42 +0200779 if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) &&
Willy Tarreau72b179a2008-08-28 16:01:32 +0200780 !(s->rep->flags & BF_FULL)) {
781 if (produce_content(s) != 0)
782 resync = 1; /* completed, better re-check flags */
783 }
784 }
785 else if (s->rep->prod->state >= SI_ST_EST) {
786 if ((rpf_rep ^ s->rep->flags) & BF_MASK_ANALYSER) {
787 /* the analysers must block it themselves */
Willy Tarreau36e6a412008-08-28 11:07:58 +0200788 resync = 1;
Willy Tarreau3da77c52008-08-29 09:58:42 +0200789 buffer_write_ena(s->rep);
Willy Tarreau36e6a412008-08-28 11:07:58 +0200790 if (s->rep->analysers)
791 process_response(s);
Willy Tarreau72b179a2008-08-28 16:01:32 +0200792 rpf_rep = s->rep->flags;
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200793 }
Willy Tarreaudafde432008-08-17 01:00:46 +0200794 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200795
Willy Tarreaudafde432008-08-17 01:00:46 +0200796 } while (resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200797
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200798 if (likely((s->rep->cons->state != SI_ST_CLO) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200799 (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100800
801 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
802 session_process_counters(s);
803
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200804 if (s->rep->cons->state == SI_ST_EST)
805 stream_sock_data_finish(s->rep->cons->fd);
806
807 if (s->req->cons->state == SI_ST_EST)
808 stream_sock_data_finish(s->req->cons->fd);
809
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200810 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
811 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812
Willy Tarreau7f875f62008-08-11 17:35:01 +0200813 /* Trick: if a request is being waiting for the server to respond,
814 * and if we know the server can timeout, we don't want the timeout
815 * to expire on the client side first, but we're still interested
816 * in passing data from the client to the server (eg: POST). Thus,
817 * we can cancel the client's request timeout if the server's
818 * request timeout is set and the server has not yet sent a response.
819 */
820
Willy Tarreau3da77c52008-08-29 09:58:42 +0200821 if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 &&
Willy Tarreau26ed74d2008-08-17 12:11:14 +0200822 (tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
Willy Tarreau7f875f62008-08-11 17:35:01 +0200823 s->req->rex = TICK_ETERNITY;
824
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200825 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
826 tick_first(s->rep->rex, s->rep->wex));
Willy Tarreauffab5b42008-08-17 18:03:28 +0200827 if (s->req->analysers)
828 t->expire = tick_first(t->expire, s->req->analyse_exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829
Willy Tarreaucb651252008-08-29 13:57:30 +0200830#ifdef DEBUG_FULL
831 fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u\n",
832 now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex);
833#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834 /* restore t to its place in the task list */
835 task_queue(t);
836
Willy Tarreaua7c52762008-08-16 18:40:18 +0200837#ifdef DEBUG_DEV
838 /* this may only happen when no timeout is set or in case of an FSM bug */
839 if (!t->expire)
840 ABORT_NOW();
841#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200842 *next = t->expire;
843 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 }
845
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100846 s->fe->feconn--;
847 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200848 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849 actconn--;
850
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200851 if (unlikely((global.mode & MODE_DEBUG) &&
852 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200853 int len;
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200854 len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200855 s->uniq_id, s->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +0200856 (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd,
Willy Tarreauf495ddf2008-08-17 14:38:41 +0200857 s->term_trace);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200858 write(1, trash, len);
859 }
860
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200861 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100862 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200863
864 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200865 if (s->logs.logwait &&
866 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200867 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
868 if (s->fe->to_log & LW_REQ)
869 http_sess_log(s);
870 else
871 tcp_sess_log(s);
872 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873
874 /* the task MUST not be in the run queue anymore */
875 task_delete(t);
876 session_free(s);
877 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200878 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200879}
880
881
Willy Tarreau42250582007-04-01 01:30:43 +0200882extern const char sess_term_cond[8];
883extern const char sess_fin_state[8];
884extern const char *monthname[12];
885const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
886const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
887 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
888 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200889struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200890struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100891
Willy Tarreau42250582007-04-01 01:30:43 +0200892/*
893 * send a log for the session when we have enough info about it.
894 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100895 */
Willy Tarreau42250582007-04-01 01:30:43 +0200896static void http_sess_log(struct session *s)
897{
898 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
899 struct proxy *fe = s->fe;
900 struct proxy *be = s->be;
901 struct proxy *prx_log;
902 struct http_txn *txn = &s->txn;
903 int tolog;
904 char *uri, *h;
905 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200906 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200907 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200908 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200909 int hdr;
910
911 if (fe->logfac1 < 0 && fe->logfac2 < 0)
912 return;
913 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100914
Willy Tarreau42250582007-04-01 01:30:43 +0200915 if (s->cli_addr.ss_family == AF_INET)
916 inet_ntop(AF_INET,
917 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
918 pn, sizeof(pn));
919 else
920 inet_ntop(AF_INET6,
921 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
922 pn, sizeof(pn));
923
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200924 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200925
926 /* FIXME: let's limit ourselves to frontend logging for now. */
927 tolog = fe->to_log;
928
929 h = tmpline;
930 if (fe->to_log & LW_REQHDR &&
931 txn->req.cap &&
932 (h < tmpline + sizeof(tmpline) - 10)) {
933 *(h++) = ' ';
934 *(h++) = '{';
935 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
936 if (hdr)
937 *(h++) = '|';
938 if (txn->req.cap[hdr] != NULL)
939 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
940 '#', hdr_encode_map, txn->req.cap[hdr]);
941 }
942 *(h++) = '}';
943 }
944
945 if (fe->to_log & LW_RSPHDR &&
946 txn->rsp.cap &&
947 (h < tmpline + sizeof(tmpline) - 7)) {
948 *(h++) = ' ';
949 *(h++) = '{';
950 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
951 if (hdr)
952 *(h++) = '|';
953 if (txn->rsp.cap[hdr] != NULL)
954 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
955 '#', hdr_encode_map, txn->rsp.cap[hdr]);
956 }
957 *(h++) = '}';
958 }
959
960 if (h < tmpline + sizeof(tmpline) - 4) {
961 *(h++) = ' ';
962 *(h++) = '"';
963 uri = txn->uri ? txn->uri : "<BADREQ>";
964 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
965 '#', url_encode_map, uri);
966 *(h++) = '"';
967 }
968 *h = '\0';
969
970 svid = (tolog & LW_SVID) ?
971 (s->data_source != DATA_SRC_STATS) ?
972 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
973
Willy Tarreau70089872008-06-13 21:12:51 +0200974 t_request = -1;
975 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
976 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
977
Willy Tarreau42250582007-04-01 01:30:43 +0200978 send_log(prx_log, LOG_INFO,
979 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
980 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100981 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200982 pn,
983 (s->cli_addr.ss_family == AF_INET) ?
984 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
985 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200986 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200987 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200988 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200989 t_request,
990 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200991 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
992 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
993 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
994 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100995 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200996 txn->cli_cookie ? txn->cli_cookie : "-",
997 txn->srv_cookie ? txn->srv_cookie : "-",
998 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
999 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1000 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1001 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1002 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001003 (s->flags & SN_REDISP)?"+":"",
1004 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001005 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1006
1007 s->logs.logwait = 0;
1008}
1009
Willy Tarreau117f59e2007-03-04 18:17:17 +01001010
1011/*
1012 * Capture headers from message starting at <som> according to header list
1013 * <cap_hdr>, and fill the <idx> structure appropriately.
1014 */
1015void capture_headers(char *som, struct hdr_idx *idx,
1016 char **cap, struct cap_hdr *cap_hdr)
1017{
1018 char *eol, *sol, *col, *sov;
1019 int cur_idx;
1020 struct cap_hdr *h;
1021 int len;
1022
1023 sol = som + hdr_idx_first_pos(idx);
1024 cur_idx = hdr_idx_first_idx(idx);
1025
1026 while (cur_idx) {
1027 eol = sol + idx->v[cur_idx].len;
1028
1029 col = sol;
1030 while (col < eol && *col != ':')
1031 col++;
1032
1033 sov = col + 1;
1034 while (sov < eol && http_is_lws[(unsigned char)*sov])
1035 sov++;
1036
1037 for (h = cap_hdr; h; h = h->next) {
1038 if ((h->namelen == col - sol) &&
1039 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1040 if (cap[h->index] == NULL)
1041 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001042 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001043
1044 if (cap[h->index] == NULL) {
1045 Alert("HTTP capture : out of memory.\n");
1046 continue;
1047 }
1048
1049 len = eol - sov;
1050 if (len > h->len)
1051 len = h->len;
1052
1053 memcpy(cap[h->index], sov, len);
1054 cap[h->index][len]=0;
1055 }
1056 }
1057 sol = eol + idx->v[cur_idx].cr + 1;
1058 cur_idx = idx->v[cur_idx].next;
1059 }
1060}
1061
1062
Willy Tarreau42250582007-04-01 01:30:43 +02001063/* either we find an LF at <ptr> or we jump to <bad>.
1064 */
1065#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1066
1067/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1068 * otherwise to <http_msg_ood> with <state> set to <st>.
1069 */
1070#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1071 ptr++; \
1072 if (likely(ptr < end)) \
1073 goto good; \
1074 else { \
1075 state = (st); \
1076 goto http_msg_ood; \
1077 } \
1078 } while (0)
1079
1080
Willy Tarreaubaaee002006-06-26 02:48:02 +02001081/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001082 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001083 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1084 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1085 * will give undefined results.
1086 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1087 * and that msg->sol points to the beginning of the response.
1088 * If a complete line is found (which implies that at least one CR or LF is
1089 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1090 * returned indicating an incomplete line (which does not mean that parts have
1091 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1092 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1093 * upon next call.
1094 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001095 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001096 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1097 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001098 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001099 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001100const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1101 unsigned int state, const char *ptr, const char *end,
1102 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001103{
1104 __label__
1105 http_msg_rpver,
1106 http_msg_rpver_sp,
1107 http_msg_rpcode,
1108 http_msg_rpcode_sp,
1109 http_msg_rpreason,
1110 http_msg_rpline_eol,
1111 http_msg_ood, /* out of data */
1112 http_msg_invalid;
1113
1114 switch (state) {
1115 http_msg_rpver:
1116 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001117 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1119
1120 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001121 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001122 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1123 }
1124 goto http_msg_invalid;
1125
1126 http_msg_rpver_sp:
1127 case HTTP_MSG_RPVER_SP:
1128 if (likely(!HTTP_IS_LWS(*ptr))) {
1129 msg->sl.st.c = ptr - msg_buf;
1130 goto http_msg_rpcode;
1131 }
1132 if (likely(HTTP_IS_SPHT(*ptr)))
1133 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1134 /* so it's a CR/LF, this is invalid */
1135 goto http_msg_invalid;
1136
1137 http_msg_rpcode:
1138 case HTTP_MSG_RPCODE:
1139 if (likely(!HTTP_IS_LWS(*ptr)))
1140 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1141
1142 if (likely(HTTP_IS_SPHT(*ptr))) {
1143 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1144 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1145 }
1146
1147 /* so it's a CR/LF, so there is no reason phrase */
1148 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1149 http_msg_rsp_reason:
1150 /* FIXME: should we support HTTP responses without any reason phrase ? */
1151 msg->sl.st.r = ptr - msg_buf;
1152 msg->sl.st.r_l = 0;
1153 goto http_msg_rpline_eol;
1154
1155 http_msg_rpcode_sp:
1156 case HTTP_MSG_RPCODE_SP:
1157 if (likely(!HTTP_IS_LWS(*ptr))) {
1158 msg->sl.st.r = ptr - msg_buf;
1159 goto http_msg_rpreason;
1160 }
1161 if (likely(HTTP_IS_SPHT(*ptr)))
1162 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1163 /* so it's a CR/LF, so there is no reason phrase */
1164 goto http_msg_rsp_reason;
1165
1166 http_msg_rpreason:
1167 case HTTP_MSG_RPREASON:
1168 if (likely(!HTTP_IS_CRLF(*ptr)))
1169 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1170 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1171 http_msg_rpline_eol:
1172 /* We have seen the end of line. Note that we do not
1173 * necessarily have the \n yet, but at least we know that we
1174 * have EITHER \r OR \n, otherwise the response would not be
1175 * complete. We can then record the response length and return
1176 * to the caller which will be able to register it.
1177 */
1178 msg->sl.st.l = ptr - msg->sol;
1179 return ptr;
1180
1181#ifdef DEBUG_FULL
1182 default:
1183 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1184 exit(1);
1185#endif
1186 }
1187
1188 http_msg_ood:
1189 /* out of data */
1190 if (ret_state)
1191 *ret_state = state;
1192 if (ret_ptr)
1193 *ret_ptr = (char *)ptr;
1194 return NULL;
1195
1196 http_msg_invalid:
1197 /* invalid message */
1198 if (ret_state)
1199 *ret_state = HTTP_MSG_ERROR;
1200 return NULL;
1201}
1202
1203
1204/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001205 * This function parses a request line between <ptr> and <end>, starting with
1206 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1207 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1208 * will give undefined results.
1209 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1210 * and that msg->sol points to the beginning of the request.
1211 * If a complete line is found (which implies that at least one CR or LF is
1212 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1213 * returned indicating an incomplete line (which does not mean that parts have
1214 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1215 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1216 * upon next call.
1217 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001218 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001219 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1220 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001221 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001222 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001223const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1224 unsigned int state, const char *ptr, const char *end,
1225 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001226{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001227 __label__
1228 http_msg_rqmeth,
1229 http_msg_rqmeth_sp,
1230 http_msg_rquri,
1231 http_msg_rquri_sp,
1232 http_msg_rqver,
1233 http_msg_rqline_eol,
1234 http_msg_ood, /* out of data */
1235 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 switch (state) {
1238 http_msg_rqmeth:
1239 case HTTP_MSG_RQMETH:
1240 if (likely(HTTP_IS_TOKEN(*ptr)))
1241 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001244 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001245 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1246 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001247
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 if (likely(HTTP_IS_CRLF(*ptr))) {
1249 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001250 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001251 http_msg_req09_uri:
1252 msg->sl.rq.u = ptr - msg_buf;
1253 http_msg_req09_uri_e:
1254 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1255 http_msg_req09_ver:
1256 msg->sl.rq.v = ptr - msg_buf;
1257 msg->sl.rq.v_l = 0;
1258 goto http_msg_rqline_eol;
1259 }
1260 goto http_msg_invalid;
1261
1262 http_msg_rqmeth_sp:
1263 case HTTP_MSG_RQMETH_SP:
1264 if (likely(!HTTP_IS_LWS(*ptr))) {
1265 msg->sl.rq.u = ptr - msg_buf;
1266 goto http_msg_rquri;
1267 }
1268 if (likely(HTTP_IS_SPHT(*ptr)))
1269 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1270 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1271 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 http_msg_rquri:
1274 case HTTP_MSG_RQURI:
1275 if (likely(!HTTP_IS_LWS(*ptr)))
1276 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001277
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 if (likely(HTTP_IS_SPHT(*ptr))) {
1279 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1280 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1284 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 http_msg_rquri_sp:
1287 case HTTP_MSG_RQURI_SP:
1288 if (likely(!HTTP_IS_LWS(*ptr))) {
1289 msg->sl.rq.v = ptr - msg_buf;
1290 goto http_msg_rqver;
1291 }
1292 if (likely(HTTP_IS_SPHT(*ptr)))
1293 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1294 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1295 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 http_msg_rqver:
1298 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001299 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001301
1302 if (likely(HTTP_IS_CRLF(*ptr))) {
1303 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1304 http_msg_rqline_eol:
1305 /* We have seen the end of line. Note that we do not
1306 * necessarily have the \n yet, but at least we know that we
1307 * have EITHER \r OR \n, otherwise the request would not be
1308 * complete. We can then record the request length and return
1309 * to the caller which will be able to register it.
1310 */
1311 msg->sl.rq.l = ptr - msg->sol;
1312 return ptr;
1313 }
1314
1315 /* neither an HTTP_VER token nor a CRLF */
1316 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318#ifdef DEBUG_FULL
1319 default:
1320 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1321 exit(1);
1322#endif
1323 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 http_msg_ood:
1326 /* out of data */
1327 if (ret_state)
1328 *ret_state = state;
1329 if (ret_ptr)
1330 *ret_ptr = (char *)ptr;
1331 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 http_msg_invalid:
1334 /* invalid message */
1335 if (ret_state)
1336 *ret_state = HTTP_MSG_ERROR;
1337 return NULL;
1338}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001339
1340
Willy Tarreau8973c702007-01-21 23:58:29 +01001341/*
1342 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001343 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001344 * when data are missing and recalled at the exact same location with no
1345 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001346 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1347 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1350{
1351 __label__
1352 http_msg_rqbefore,
1353 http_msg_rqbefore_cr,
1354 http_msg_rqmeth,
1355 http_msg_rqline_end,
1356 http_msg_hdr_first,
1357 http_msg_hdr_name,
1358 http_msg_hdr_l1_sp,
1359 http_msg_hdr_l1_lf,
1360 http_msg_hdr_l1_lws,
1361 http_msg_hdr_val,
1362 http_msg_hdr_l2_lf,
1363 http_msg_hdr_l2_lws,
1364 http_msg_complete_header,
1365 http_msg_last_lf,
1366 http_msg_ood, /* out of data */
1367 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001368
Willy Tarreaue69eada2008-01-27 00:34:10 +01001369 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001371
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001372 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 ptr = buf->lr;
1374 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 if (unlikely(ptr >= end))
1377 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001378
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001380 /*
1381 * First, states that are specific to the response only.
1382 * We check them first so that request and headers are
1383 * closer to each other (accessed more often).
1384 */
1385 http_msg_rpbefore:
1386 case HTTP_MSG_RPBEFORE:
1387 if (likely(HTTP_IS_TOKEN(*ptr))) {
1388 if (likely(ptr == buf->data)) {
1389 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001390 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001391 } else {
1392#if PARSE_PRESERVE_EMPTY_LINES
1393 /* only skip empty leading lines, don't remove them */
1394 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001395 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001396#else
1397 /* Remove empty leading lines, as recommended by
1398 * RFC2616. This takes a lot of time because we
1399 * must move all the buffer backwards, but this
1400 * is rarely needed. The method above will be
1401 * cleaner when we'll be able to start sending
1402 * the request from any place in the buffer.
1403 */
1404 buf->lr = ptr;
1405 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001406 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001407 msg->sol = buf->data;
1408 ptr = buf->data;
1409 end = buf->r;
1410#endif
1411 }
1412 hdr_idx_init(idx);
1413 state = HTTP_MSG_RPVER;
1414 goto http_msg_rpver;
1415 }
1416
1417 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1418 goto http_msg_invalid;
1419
1420 if (unlikely(*ptr == '\n'))
1421 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1423 /* stop here */
1424
1425 http_msg_rpbefore_cr:
1426 case HTTP_MSG_RPBEFORE_CR:
1427 EXPECT_LF_HERE(ptr, http_msg_invalid);
1428 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1429 /* stop here */
1430
1431 http_msg_rpver:
1432 case HTTP_MSG_RPVER:
1433 case HTTP_MSG_RPVER_SP:
1434 case HTTP_MSG_RPCODE:
1435 case HTTP_MSG_RPCODE_SP:
1436 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001437 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001438 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 if (unlikely(!ptr))
1440 return;
1441
1442 /* we have a full response and we know that we have either a CR
1443 * or an LF at <ptr>.
1444 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001445 //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 +01001446 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1447
1448 msg->sol = ptr;
1449 if (likely(*ptr == '\r'))
1450 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1451 goto http_msg_rpline_end;
1452
1453 http_msg_rpline_end:
1454 case HTTP_MSG_RPLINE_END:
1455 /* msg->sol must point to the first of CR or LF. */
1456 EXPECT_LF_HERE(ptr, http_msg_invalid);
1457 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1458 /* stop here */
1459
1460 /*
1461 * Second, states that are specific to the request only
1462 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 http_msg_rqbefore:
1464 case HTTP_MSG_RQBEFORE:
1465 if (likely(HTTP_IS_TOKEN(*ptr))) {
1466 if (likely(ptr == buf->data)) {
1467 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 } else {
1470#if PARSE_PRESERVE_EMPTY_LINES
1471 /* only skip empty leading lines, don't remove them */
1472 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001473 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001474#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 /* Remove empty leading lines, as recommended by
1476 * RFC2616. This takes a lot of time because we
1477 * must move all the buffer backwards, but this
1478 * is rarely needed. The method above will be
1479 * cleaner when we'll be able to start sending
1480 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001481 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 buf->lr = ptr;
1483 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001484 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 msg->sol = buf->data;
1486 ptr = buf->data;
1487 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001488#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001490 /* we will need this when keep-alive will be supported
1491 hdr_idx_init(idx);
1492 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001493 state = HTTP_MSG_RQMETH;
1494 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1498 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 if (unlikely(*ptr == '\n'))
1501 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1502 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001503 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 http_msg_rqbefore_cr:
1506 case HTTP_MSG_RQBEFORE_CR:
1507 EXPECT_LF_HERE(ptr, http_msg_invalid);
1508 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001509 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 http_msg_rqmeth:
1512 case HTTP_MSG_RQMETH:
1513 case HTTP_MSG_RQMETH_SP:
1514 case HTTP_MSG_RQURI:
1515 case HTTP_MSG_RQURI_SP:
1516 case HTTP_MSG_RQVER:
1517 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001518 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 if (unlikely(!ptr))
1520 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 /* we have a full request and we know that we have either a CR
1523 * or an LF at <ptr>.
1524 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001525 //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 +01001526 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 msg->sol = ptr;
1529 if (likely(*ptr == '\r'))
1530 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001532
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 http_msg_rqline_end:
1534 case HTTP_MSG_RQLINE_END:
1535 /* check for HTTP/0.9 request : no version information available.
1536 * msg->sol must point to the first of CR or LF.
1537 */
1538 if (unlikely(msg->sl.rq.v_l == 0))
1539 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001540
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 EXPECT_LF_HERE(ptr, http_msg_invalid);
1542 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001543 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001544
Willy Tarreau8973c702007-01-21 23:58:29 +01001545 /*
1546 * Common states below
1547 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 http_msg_hdr_first:
1549 case HTTP_MSG_HDR_FIRST:
1550 msg->sol = ptr;
1551 if (likely(!HTTP_IS_CRLF(*ptr))) {
1552 goto http_msg_hdr_name;
1553 }
1554
1555 if (likely(*ptr == '\r'))
1556 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1557 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 http_msg_hdr_name:
1560 case HTTP_MSG_HDR_NAME:
1561 /* assumes msg->sol points to the first char */
1562 if (likely(HTTP_IS_TOKEN(*ptr)))
1563 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 if (likely(*ptr == ':')) {
1566 msg->col = ptr - buf->data;
1567 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1568 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 http_msg_hdr_l1_sp:
1573 case HTTP_MSG_HDR_L1_SP:
1574 /* assumes msg->sol points to the first char and msg->col to the colon */
1575 if (likely(HTTP_IS_SPHT(*ptr)))
1576 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001577
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 /* header value can be basically anything except CR/LF */
1579 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 if (likely(!HTTP_IS_CRLF(*ptr))) {
1582 goto http_msg_hdr_val;
1583 }
1584
1585 if (likely(*ptr == '\r'))
1586 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1587 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 http_msg_hdr_l1_lf:
1590 case HTTP_MSG_HDR_L1_LF:
1591 EXPECT_LF_HERE(ptr, http_msg_invalid);
1592 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 http_msg_hdr_l1_lws:
1595 case HTTP_MSG_HDR_L1_LWS:
1596 if (likely(HTTP_IS_SPHT(*ptr))) {
1597 /* replace HT,CR,LF with spaces */
1598 for (; buf->data+msg->sov < ptr; msg->sov++)
1599 buf->data[msg->sov] = ' ';
1600 goto http_msg_hdr_l1_sp;
1601 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001602 /* we had a header consisting only in spaces ! */
1603 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 goto http_msg_complete_header;
1605
1606 http_msg_hdr_val:
1607 case HTTP_MSG_HDR_VAL:
1608 /* assumes msg->sol points to the first char, msg->col to the
1609 * colon, and msg->sov points to the first character of the
1610 * value.
1611 */
1612 if (likely(!HTTP_IS_CRLF(*ptr)))
1613 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 msg->eol = ptr;
1616 /* Note: we could also copy eol into ->eoh so that we have the
1617 * real header end in case it ends with lots of LWS, but is this
1618 * really needed ?
1619 */
1620 if (likely(*ptr == '\r'))
1621 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1622 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001623
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 http_msg_hdr_l2_lf:
1625 case HTTP_MSG_HDR_L2_LF:
1626 EXPECT_LF_HERE(ptr, http_msg_invalid);
1627 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 http_msg_hdr_l2_lws:
1630 case HTTP_MSG_HDR_L2_LWS:
1631 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1632 /* LWS: replace HT,CR,LF with spaces */
1633 for (; msg->eol < ptr; msg->eol++)
1634 *msg->eol = ' ';
1635 goto http_msg_hdr_val;
1636 }
1637 http_msg_complete_header:
1638 /*
1639 * It was a new header, so the last one is finished.
1640 * Assumes msg->sol points to the first char, msg->col to the
1641 * colon, msg->sov points to the first character of the value
1642 * and msg->eol to the first CR or LF so we know how the line
1643 * ends. We insert last header into the index.
1644 */
1645 /*
1646 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1647 write(2, msg->sol, msg->eol-msg->sol);
1648 fprintf(stderr,"\n");
1649 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1652 idx, idx->tail) < 0))
1653 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001654
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 msg->sol = ptr;
1656 if (likely(!HTTP_IS_CRLF(*ptr))) {
1657 goto http_msg_hdr_name;
1658 }
1659
1660 if (likely(*ptr == '\r'))
1661 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1662 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 http_msg_last_lf:
1665 case HTTP_MSG_LAST_LF:
1666 /* Assumes msg->sol points to the first of either CR or LF */
1667 EXPECT_LF_HERE(ptr, http_msg_invalid);
1668 ptr++;
1669 buf->lr = ptr;
1670 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001671 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 return;
1673#ifdef DEBUG_FULL
1674 default:
1675 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1676 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001677#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 }
1679 http_msg_ood:
1680 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001681 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 buf->lr = ptr;
1683 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 http_msg_invalid:
1686 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001687 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 return;
1689}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001690
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001691/* This function performs all the processing enabled for the current request.
Willy Tarreaudafde432008-08-17 01:00:46 +02001692 * It normally returns zero, but may return 1 if it absolutely needs to be
1693 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02001694 * t->req->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001695 * functions. Its behaviour is rather simple :
1696 * - all enabled analysers are called in turn from the lower to the higher
1697 * bit.
1698 * - if an analyser does not have enough data, it must return without calling
Willy Tarreau3da77c52008-08-29 09:58:42 +02001699 * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001700 * that unprocessed data will not be forwarded. But that probably depends on
1701 * the protocol. Generally it is not reset in case of errors.
1702 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreau3da77c52008-08-29 09:58:42 +02001703 * analyser without touching BF_WRITE_ENA (it is enabled prior to
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001704 * analysis).
1705 * - if an analyser thinks it has no added value anymore staying here, it must
1706 * reset its bit from the analysers flags in order not to be called anymore.
1707 *
1708 * In the future, analysers should be able to indicate that they want to be
1709 * called after XXX bytes have been received (or transfered), and the min of
1710 * all's wishes will be used to ring back (unless a special condition occurs).
1711 *
1712 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001714int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 struct buffer *req = t->req;
1717 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001718
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001719 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 +02001720 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02001721 t,
1722 req,
1723 req->rex, req->wex,
1724 req->flags,
1725 req->l,
1726 req->analysers);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001727
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001728 /* The tcp-inspect analyser is always called alone */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001729 if (req->analysers & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001730 struct tcp_rule *rule;
1731 int partial;
1732
Willy Tarreauf495ddf2008-08-17 14:38:41 +02001733 /* We will abort if we encounter a read error. In theory, we
1734 * should not abort if we get a close, it might be valid,
1735 * although very unlikely. FIXME: we'll abort for now, this
1736 * will be easier to change later.
Willy Tarreaub6866442008-07-14 23:54:42 +02001737 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001738 if (req->flags & BF_READ_ERROR) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001739 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001740 //t->fe->failed_req++;
Willy Tarreaub6866442008-07-14 23:54:42 +02001741 if (!(t->flags & SN_ERR_MASK))
1742 t->flags |= SN_ERR_CLICL;
1743 if (!(t->flags & SN_FINST_MASK))
1744 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001745 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001746 }
1747
1748 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001749 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreau2df28e82008-08-17 15:20:19 +02001750 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001751 t->fe->failed_req++;
1752 if (!(t->flags & SN_ERR_MASK))
1753 t->flags |= SN_ERR_CLITO;
1754 if (!(t->flags & SN_FINST_MASK))
1755 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001756 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001757 }
1758
1759 /* We don't know whether we have enough data, so must proceed
1760 * this way :
1761 * - iterate through all rules in their declaration order
1762 * - if one rule returns MISS, it means the inspect delay is
1763 * not over yet, then return immediately, otherwise consider
1764 * it as a non-match.
1765 * - if one rule returns OK, then return OK
1766 * - if one rule returns KO, then return KO
1767 */
1768
Willy Tarreauffab5b42008-08-17 18:03:28 +02001769 if (req->flags & (BF_READ_NULL | BF_SHUTR) || tick_is_expired(req->analyse_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001770 partial = 0;
1771 else
1772 partial = ACL_PARTIAL;
1773
1774 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1775 int ret = ACL_PAT_PASS;
1776
1777 if (rule->cond) {
1778 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1779 if (ret == ACL_PAT_MISS) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02001780 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001781 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001782 if (!tick_isset(req->analyse_exp))
1783 req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
Willy Tarreaudafde432008-08-17 01:00:46 +02001784 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001785 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001786
Willy Tarreaub6866442008-07-14 23:54:42 +02001787 ret = acl_pass(ret);
1788 if (rule->cond->pol == ACL_COND_UNLESS)
1789 ret = !ret;
1790 }
1791
1792 if (ret) {
1793 /* we have a matching rule. */
1794 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001795 buffer_abort(req);
1796 buffer_abort(rep);
1797 //FIXME: this delete this
1798 //fd_delete(t->cli_fd);
1799 //t->cli_state = CL_STCLOSE;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001800 req->analysers = 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001801 t->fe->failed_req++;
1802 if (!(t->flags & SN_ERR_MASK))
1803 t->flags |= SN_ERR_PRXCOND;
1804 if (!(t->flags & SN_FINST_MASK))
1805 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001806 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02001807 }
1808 /* otherwise accept */
1809 break;
1810 }
1811 }
1812
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001813 /* if we get there, it means we have no rule which matches, or
1814 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001815 */
Willy Tarreau2df28e82008-08-17 15:20:19 +02001816 req->analysers &= ~AN_REQ_INSPECT;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001817 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001818 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001819
Willy Tarreau2df28e82008-08-17 15:20:19 +02001820 if (req->analysers & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 /*
1822 * Now parse the partial (or complete) lines.
1823 * We will check the request syntax, and also join multi-line
1824 * headers. An index of all the lines will be elaborated while
1825 * parsing.
1826 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001827 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001829 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001830 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 * req->data + req->eoh = end of processed headers / start of current one
1832 * req->data + req->eol = end of current header or line (LF or CRLF)
1833 * req->lr = first non-visited byte
1834 * req->r = end of data
1835 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001838 struct http_txn *txn = &t->txn;
1839 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001841
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001843 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 /* 1: we might have to print this header in debug mode */
1846 if (unlikely((global.mode & MODE_DEBUG) &&
1847 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001848 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001850
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001851 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001852 eol = sol + msg->sl.rq.l;
1853 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001854
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001855 sol += hdr_idx_first_pos(&txn->hdr_idx);
1856 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001857
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001859 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001861 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1862 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 }
1864 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001865
Willy Tarreau58f10d72006-12-04 02:26:12 +01001866
1867 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001868 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001869 * If not so, we check the FD and buffer states before leaving.
1870 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001871 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1872 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001873 *
1874 */
1875
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001876 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 /*
1878 * First, let's catch bad requests.
1879 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001880 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001882
1883 /* 1: Since we are in header mode, if there's no space
1884 * left for headers, we won't be able to free more
1885 * later, so the session will never terminate. We
1886 * must terminate it now.
1887 */
Willy Tarreaue393fe22008-08-16 22:18:07 +02001888 if (unlikely(req->flags & BF_FULL)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 /* FIXME: check if URI is set and return Status
1890 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001891 */
Willy Tarreau06619262006-12-17 08:37:22 +01001892 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001893 }
1894
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001895 /* 2: have we encountered a read error ? */
1896 else if (req->flags & BF_READ_ERROR) {
1897 /* we cannot return any message on error */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001898 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001899 req->analysers = 0;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001900 //t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001901 if (!(t->flags & SN_ERR_MASK))
1902 t->flags |= SN_ERR_CLICL;
1903 if (!(t->flags & SN_FINST_MASK))
1904 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001905 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001906 }
1907
1908 /* 3: has the read timeout expired ? */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001909 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001910 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001911 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001912 client_retnclose(t, error_message(t, HTTP_ERR_408));
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 Tarreauc0dde7a2007-01-01 21:38:07 +01001915 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001916 if (!(t->flags & SN_ERR_MASK))
1917 t->flags |= SN_ERR_CLITO;
1918 if (!(t->flags & SN_FINST_MASK))
1919 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001920 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921 }
1922
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001923 /* 4: have we encountered a close ? */
1924 else if (req->flags & (BF_READ_NULL | BF_SHUTR)) {
1925 txn->status = 400;
1926 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001927 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau2df28e82008-08-17 15:20:19 +02001928 req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001929 t->fe->failed_req++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001930
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001931 if (!(t->flags & SN_ERR_MASK))
1932 t->flags |= SN_ERR_CLICL;
1933 if (!(t->flags & SN_FINST_MASK))
1934 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001935 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001936 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001937
Willy Tarreau3da77c52008-08-29 09:58:42 +02001938 buffer_write_dis(req);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001939 /* just set the request timeout once at the beginning of the request */
Willy Tarreauffab5b42008-08-17 18:03:28 +02001940 if (!tick_isset(req->analyse_exp))
1941 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001942
1943 /* we're not ready yet */
Willy Tarreaudafde432008-08-17 01:00:46 +02001944 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001945 }
1946
1947
1948 /****************************************************************
1949 * More interesting part now : we know that we have a complete *
1950 * request which at least looks like HTTP. We have an indicator *
1951 * of each header's length, so we can parse them quickly. *
1952 ****************************************************************/
1953
Willy Tarreau2df28e82008-08-17 15:20:19 +02001954 req->analysers &= ~AN_REQ_HTTP_HDR;
Willy Tarreauffab5b42008-08-17 18:03:28 +02001955 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001956
Willy Tarreau9cdde232007-05-02 20:58:19 +02001957 /* ensure we keep this pointer to the beginning of the message */
1958 msg->sol = req->data + msg->som;
1959
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001960 /*
1961 * 1: identify the method
1962 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001963 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001964
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001965 /* we can make use of server redirect on GET and HEAD */
1966 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1967 t->flags |= SN_REDIRECTABLE;
1968
Willy Tarreau58f10d72006-12-04 02:26:12 +01001969 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001970 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001971 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001973 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974 if (unlikely((t->fe->monitor_uri_len != 0) &&
1975 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1976 !memcmp(&req->data[msg->sl.rq.u],
1977 t->fe->monitor_uri,
1978 t->fe->monitor_uri_len))) {
1979 /*
1980 * We have found the monitor URI
1981 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001982 struct acl_cond *cond;
1983 cur_proxy = t->fe;
1984
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001985 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001986
1987 /* Check if we want to fail this monitor request or not */
1988 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1989 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001990
1991 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001992 if (cond->pol == ACL_COND_UNLESS)
1993 ret = !ret;
1994
1995 if (ret) {
1996 /* we fail this request, let's return 503 service unavail */
1997 txn->status = 503;
1998 client_retnclose(t, error_message(t, HTTP_ERR_503));
1999 goto return_prx_cond;
2000 }
2001 }
2002
2003 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002004 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002005 client_retnclose(t, &http_200_chunk);
2006 goto return_prx_cond;
2007 }
2008
2009 /*
2010 * 3: Maybe we have to copy the original REQURI for the logs ?
2011 * Note: we cannot log anymore if the request has been
2012 * classified as invalid.
2013 */
2014 if (unlikely(t->logs.logwait & LW_REQ)) {
2015 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02002016 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002017 int urilen = msg->sl.rq.l;
2018
2019 if (urilen >= REQURI_LEN)
2020 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002021 memcpy(txn->uri, &req->data[msg->som], urilen);
2022 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002023
2024 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02002025 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002026 } else {
2027 Alert("HTTP logging : out of memory.\n");
2028 }
2029 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002030
Willy Tarreau06619262006-12-17 08:37:22 +01002031
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002032 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2033 if (unlikely(msg->sl.rq.v_l == 0)) {
2034 int delta;
2035 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002036 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002037 cur_end = msg->sol + msg->sl.rq.l;
2038 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01002039
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002040 if (msg->sl.rq.u_l == 0) {
2041 /* if no URI was set, add "/" */
2042 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
2043 cur_end += delta;
2044 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01002045 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002046 /* add HTTP version */
2047 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
2048 msg->eoh += delta;
2049 cur_end += delta;
2050 cur_end = (char *)http_parse_reqline(msg, req->data,
2051 HTTP_MSG_RQMETH,
2052 msg->sol, cur_end + 1,
2053 NULL, NULL);
2054 if (unlikely(!cur_end))
2055 goto return_bad_req;
2056
2057 /* we have a full HTTP/1.0 request now and we know that
2058 * we have either a CR or an LF at <ptr>.
2059 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002060 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01002061 }
2062
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002063
2064 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002065 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01002066 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002067 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002068
2069 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002070 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002071 * As opposed to version 1.2, now they will be evaluated in the
2072 * filters order and not in the header order. This means that
2073 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01002074 *
2075 * We can now check whether we want to switch to another
2076 * backend, in which case we will re-check the backend's
2077 * filters and various options. In order to support 3-level
2078 * switching, here's how we should proceed :
2079 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002080 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01002081 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002082 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01002083 * There cannot be any switch from there, so ->be cannot be
2084 * changed anymore.
2085 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002086 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002087 *
Willy Tarreau830ff452006-12-17 19:31:23 +01002088 * The response path will be able to apply either ->be, or
2089 * ->be then ->fe filters in order to match the reverse of
2090 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002091 */
2092
Willy Tarreau06619262006-12-17 08:37:22 +01002093 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002094 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002095 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002096 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01002097 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002098
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002099 /* first check whether we have some ACLs set to redirect this request */
2100 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
2101 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002102
2103 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002104 if (rule->cond->pol == ACL_COND_UNLESS)
2105 ret = !ret;
2106
2107 if (ret) {
2108 struct chunk rdr = { trash, 0 };
2109 const char *msg_fmt;
2110
2111 /* build redirect message */
2112 switch(rule->code) {
2113 case 303:
2114 rdr.len = strlen(HTTP_303);
2115 msg_fmt = HTTP_303;
2116 break;
2117 case 301:
2118 rdr.len = strlen(HTTP_301);
2119 msg_fmt = HTTP_301;
2120 break;
2121 case 302:
2122 default:
2123 rdr.len = strlen(HTTP_302);
2124 msg_fmt = HTTP_302;
2125 break;
2126 }
2127
2128 if (unlikely(rdr.len > sizeof(trash)))
2129 goto return_bad_req;
2130 memcpy(rdr.str, msg_fmt, rdr.len);
2131
2132 switch(rule->type) {
2133 case REDIRECT_TYPE_PREFIX: {
2134 const char *path;
2135 int pathlen;
2136
2137 path = http_get_path(txn);
2138 /* build message using path */
2139 if (path) {
2140 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2141 } else {
2142 path = "/";
2143 pathlen = 1;
2144 }
2145
2146 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2147 goto return_bad_req;
2148
2149 /* add prefix */
2150 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2151 rdr.len += rule->rdr_len;
2152
2153 /* add path */
2154 memcpy(rdr.str + rdr.len, path, pathlen);
2155 rdr.len += pathlen;
2156 break;
2157 }
2158 case REDIRECT_TYPE_LOCATION:
2159 default:
2160 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2161 goto return_bad_req;
2162
2163 /* add location */
2164 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2165 rdr.len += rule->rdr_len;
2166 break;
2167 }
2168
2169 /* add end of headers */
2170 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2171 rdr.len += 4;
2172
2173 txn->status = rule->code;
2174 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002175 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002176 client_retnclose(t, &rdr);
2177 goto return_prx_cond;
2178 }
2179 }
2180
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002181 /* first check whether we have some ACLs set to block this request */
2182 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002183 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002184
2185 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002186 if (cond->pol == ACL_COND_UNLESS)
2187 ret = !ret;
2188
2189 if (ret) {
2190 txn->status = 403;
2191 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002192 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002193 client_retnclose(t, error_message(t, HTTP_ERR_403));
2194 goto return_prx_cond;
2195 }
2196 }
2197
Willy Tarreau06619262006-12-17 08:37:22 +01002198 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002199 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002200 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2201 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002202 }
2203
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002204 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2205 /* to ensure correct connection accounting on
2206 * the backend, we count the connection for the
2207 * one managing the queue.
2208 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002209 t->be->beconn++;
2210 if (t->be->beconn > t->be->beconn_max)
2211 t->be->beconn_max = t->be->beconn;
2212 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002213 t->flags |= SN_BE_ASSIGNED;
2214 }
2215
Willy Tarreau06619262006-12-17 08:37:22 +01002216 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002217 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002218 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002219 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002220 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002221 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002222 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002223 goto return_prx_cond;
2224 }
2225
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002226 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002227 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002228 !(t->flags & SN_CONN_CLOSED)) {
2229 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002230 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002231 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002232
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002233 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002234 old_idx = 0;
2235
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002236 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2237 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002238 cur_ptr = cur_next;
2239 cur_end = cur_ptr + cur_hdr->len;
2240 cur_next = cur_end + cur_hdr->cr + 1;
2241
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002242 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2243 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002244 /* 3 possibilities :
2245 * - we have already set Connection: close,
2246 * so we remove this line.
2247 * - we have not yet set Connection: close,
2248 * but this line indicates close. We leave
2249 * it untouched and set the flag.
2250 * - we have not yet set Connection: close,
2251 * and this line indicates non-close. We
2252 * replace it.
2253 */
2254 if (t->flags & SN_CONN_CLOSED) {
2255 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002256 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002257 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002258 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2259 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002260 cur_hdr->len = 0;
2261 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002262 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2263 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2264 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002265 cur_next += delta;
2266 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002267 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002268 }
2269 t->flags |= SN_CONN_CLOSED;
2270 }
2271 }
2272 old_idx = cur_idx;
2273 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002274 }
2275 /* add request headers from the rule sets in the same order */
2276 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2277 if (unlikely(http_header_add_tail(req,
2278 &txn->req,
2279 &txn->hdr_idx,
2280 rule_set->req_add[cur_idx])) < 0)
2281 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002282 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002284 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002285 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002286 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002287 /* we have to check the URI and auth for this request.
2288 * FIXME!!! that one is rather dangerous, we want to
Willy Tarreau2df28e82008-08-17 15:20:19 +02002289 * make it follow standard rules (eg: clear req->analysers).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002290 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002291 if (stats_check_uri_auth(t, rule_set))
2292 return 1;
2293 }
2294
Willy Tarreau55ea7572007-06-17 19:56:27 +02002295 /* now check whether we have some switching rules for this request */
2296 if (!(t->flags & SN_BE_ASSIGNED)) {
2297 struct switching_rule *rule;
2298
2299 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2300 int ret;
2301
2302 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002303
2304 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002305 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002306 ret = !ret;
2307
2308 if (ret) {
2309 t->be = rule->be.backend;
2310 t->be->beconn++;
2311 if (t->be->beconn > t->be->beconn_max)
2312 t->be->beconn_max = t->be->beconn;
2313 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002314
2315 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002316 t->rep->rto = t->req->wto = t->be->timeout.server;
2317 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002318 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002319 t->flags |= SN_BE_ASSIGNED;
2320 break;
2321 }
2322 }
2323 }
2324
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002325 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2326 /* No backend was set, but there was a default
2327 * backend set in the frontend, so we use it and
2328 * loop again.
2329 */
2330 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002331 t->be->beconn++;
2332 if (t->be->beconn > t->be->beconn_max)
2333 t->be->beconn_max = t->be->beconn;
2334 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002335
2336 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002337 t->rep->rto = t->req->wto = t->be->timeout.server;
2338 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002339 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002340 t->flags |= SN_BE_ASSIGNED;
2341 }
2342 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002343
Willy Tarreau58f10d72006-12-04 02:26:12 +01002344
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002345 if (!(t->flags & SN_BE_ASSIGNED)) {
2346 /* To ensure correct connection accounting on
2347 * the backend, we count the connection for the
2348 * one managing the queue.
2349 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002350 t->be->beconn++;
2351 if (t->be->beconn > t->be->beconn_max)
2352 t->be->beconn_max = t->be->beconn;
2353 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002354 t->flags |= SN_BE_ASSIGNED;
2355 }
2356
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002357 /*
2358 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002359 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002360 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002361 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002362 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002363
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002364 /*
2365 * If HTTP PROXY is set we simply get remote server address
2366 * parsing incoming request.
2367 */
2368 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2369 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2370 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002371
Willy Tarreau2a324282006-12-05 00:05:46 +01002372 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002373 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002374 * so let's do the same now.
2375 */
2376
2377 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002378 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002379 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002380 }
2381
2382
2383 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002384 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002385 * Note that doing so might move headers in the request, but
2386 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002387 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002388 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002389 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2390 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002391 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002392
Willy Tarreau58f10d72006-12-04 02:26:12 +01002393
Willy Tarreau2a324282006-12-05 00:05:46 +01002394 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002395 * 9: add X-Forwarded-For if either the frontend or the backend
2396 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002397 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002398 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002399 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002400 /* Add an X-Forwarded-For header unless the source IP is
2401 * in the 'except' network range.
2402 */
2403 if ((!t->fe->except_mask.s_addr ||
2404 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2405 != t->fe->except_net.s_addr) &&
2406 (!t->be->except_mask.s_addr ||
2407 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2408 != t->be->except_net.s_addr)) {
2409 int len;
2410 unsigned char *pn;
2411 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002412
Ross Westaf72a1d2008-08-03 10:51:45 +02002413 /* Note: we rely on the backend to get the header name to be used for
2414 * x-forwarded-for, because the header is really meant for the backends.
2415 * However, if the backend did not specify any option, we have to rely
2416 * on the frontend's header name.
2417 */
2418 if (t->be->fwdfor_hdr_len) {
2419 len = t->be->fwdfor_hdr_len;
2420 memcpy(trash, t->be->fwdfor_hdr_name, len);
2421 } else {
2422 len = t->fe->fwdfor_hdr_len;
2423 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2424 }
2425 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002426
Ross Westaf72a1d2008-08-03 10:51:45 +02002427 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002428 &txn->hdr_idx, trash, len)) < 0)
2429 goto return_bad_req;
2430 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002431 }
2432 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002433 /* FIXME: for the sake of completeness, we should also support
2434 * 'except' here, although it is mostly useless in this case.
2435 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002436 int len;
2437 char pn[INET6_ADDRSTRLEN];
2438 inet_ntop(AF_INET6,
2439 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2440 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002441
2442 /* Note: we rely on the backend to get the header name to be used for
2443 * x-forwarded-for, because the header is really meant for the backends.
2444 * However, if the backend did not specify any option, we have to rely
2445 * on the frontend's header name.
2446 */
2447 if (t->be->fwdfor_hdr_len) {
2448 len = t->be->fwdfor_hdr_len;
2449 memcpy(trash, t->be->fwdfor_hdr_name, len);
2450 } else {
2451 len = t->fe->fwdfor_hdr_len;
2452 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2453 }
2454 len += sprintf(trash + len, ": %s", pn);
2455
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002456 if (unlikely(http_header_add_tail2(req, &txn->req,
2457 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002458 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002459 }
2460 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002461
Willy Tarreau2a324282006-12-05 00:05:46 +01002462 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002463 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002464 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002465 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002466 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002467 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002468 if ((unlikely(msg->sl.rq.v_l != 8) ||
2469 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2470 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002471 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002472 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002473 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002474 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002475 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2476 * If not assigned, perhaps we are balancing on url_param, but this is a
2477 * POST; and the parameters are in the body, maybe scan there to find our server.
2478 * (unless headers overflowed the buffer?)
2479 */
2480 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2481 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaue393fe22008-08-16 22:18:07 +02002482 t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002483 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2484 /* are there enough bytes here? total == l || r || rlim ?
2485 * len is unsigned, but eoh is int,
2486 * how many bytes of body have we received?
2487 * eoh is the first empty line of the header
2488 */
2489 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002490 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 +02002491
2492 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2493 * We can't assume responsibility for the server's decision,
2494 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2495 * We also can't change our mind later, about which server to choose, so round robin.
2496 */
2497 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2498 struct hdr_ctx ctx;
2499 ctx.idx = 0;
2500 /* Expect is allowed in 1.1, look for it */
2501 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2502 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002503 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002504 /* We can't reliablly stall and wait for data, because of
2505 * .NET clients that don't conform to rfc2616; so, no need for
2506 * the next block to check length expectations.
2507 * We could send 100 status back to the client, but then we need to
2508 * re-write headers, and send the message. And this isn't the right
2509 * place for that action.
2510 * TODO: support Expect elsewhere and delete this block.
2511 */
2512 goto end_check_maybe_wait_for_body;
2513 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002514
2515 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002516 /* nothing to do, we got enough */
2517 } else {
2518 /* limit implies we are supposed to need this many bytes
2519 * to find the parameter. Let's see how many bytes we can wait for.
2520 */
2521 long long hint = len;
2522 struct hdr_ctx ctx;
2523 ctx.idx = 0;
2524 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002525 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002526 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002527 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002528 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002529 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002530 ctx.idx = 0;
2531 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2532 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002533 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002534 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002535 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002536 hint = 0; /* parse failure, untrusted client */
2537 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002538 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002539 msg->hdr_content_len = hint;
2540 else
2541 hint = 0; /* bad client, sent negative length */
2542 }
2543 }
2544 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002545 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002546 hint = t->be->url_param_post_limit;
2547 /* now do we really need to buffer more data? */
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002548 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002549 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002550 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002551 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002552 /* else... There are no body bytes to wait for */
2553 }
2554 }
2555 }
2556 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002557
Willy Tarreau2a324282006-12-05 00:05:46 +01002558 /*************************************************************
2559 * OK, that's finished for the headers. We have done what we *
2560 * could. Let's switch to the DATA state. *
2561 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002562
Willy Tarreaue393fe22008-08-16 22:18:07 +02002563 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002564 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002565
Willy Tarreau1fa31262007-12-03 00:36:16 +01002566 /* When a connection is tarpitted, we use the tarpit timeout,
2567 * which may be the same as the connect timeout if unspecified.
2568 * If unset, then set it to zero because we really want it to
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002569 * eventually expire. We build the tarpit as an analyser.
Willy Tarreau2a324282006-12-05 00:05:46 +01002570 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002571 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaue393fe22008-08-16 22:18:07 +02002572 buffer_flush(t->req);
Willy Tarreau2a324282006-12-05 00:05:46 +01002573 /* flush the request so that we can drop the connection early
2574 * if the client closes first.
2575 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002576 buffer_write_dis(req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002577 req->analysers |= AN_REQ_HTTP_TARPIT;
2578 req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2579 if (!req->analyse_exp)
2580 req->analyse_exp = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002581 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002582
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002583 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002584 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002585
2586 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002587 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002588 txn->status = 400;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002589 req->analysers = 0;
Willy Tarreau80587432006-12-24 17:47:20 +01002590 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002591 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002592 return_prx_cond:
2593 if (!(t->flags & SN_ERR_MASK))
2594 t->flags |= SN_ERR_PRXCOND;
2595 if (!(t->flags & SN_FINST_MASK))
2596 t->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002597 return 0;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002598 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002599 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002600 }
2601
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002602 if (req->analysers & AN_REQ_HTTP_TARPIT) {
2603 struct http_txn *txn = &t->txn;
2604
2605 /* This connection is being tarpitted. The CLIENT side has
2606 * already set the connect expiration date to the right
2607 * timeout. We just have to check that the client is still
2608 * there and that the timeout has not expired.
2609 */
2610 if ((req->flags & (BF_READ_NULL|BF_READ_ERROR)) == 0 &&
2611 !tick_is_expired(req->analyse_exp, now_ms))
2612 return 0;
2613
2614 /* We will set the queue timer to the time spent, just for
2615 * logging purposes. We fake a 500 server error, so that the
2616 * attacker will not suspect his connection has been tarpitted.
2617 * It will not cause trouble to the logs because we can exclude
2618 * the tarpitted connections by filtering on the 'PT' status flags.
2619 */
2620 trace_term(t, TT_HTTP_SRV_2);
2621 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
2622
2623 txn->status = 500;
2624 if (req->flags != BF_READ_ERROR)
2625 client_retnclose(t, error_message(t, HTTP_ERR_500));
2626
2627 req->analysers = 0;
2628 req->analyse_exp = TICK_ETERNITY;
2629
2630 t->fe->failed_req++;
2631 if (!(t->flags & SN_ERR_MASK))
2632 t->flags |= SN_ERR_PRXCOND;
2633 if (!(t->flags & SN_FINST_MASK))
2634 t->flags |= SN_FINST_T;
2635 return 0;
2636 }
2637
Willy Tarreau2df28e82008-08-17 15:20:19 +02002638 if (req->analysers & AN_REQ_HTTP_BODY) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002639 /* We have to parse the HTTP request body to find any required data.
2640 * "balance url_param check_post" should have been the only way to get
2641 * into this. We were brought here after HTTP header analysis, so all
2642 * related structures are ready.
2643 */
2644 struct http_msg *msg = &t->txn.req;
2645 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2646 long long limit = t->be->url_param_post_limit;
2647 struct hdr_ctx ctx;
2648
2649 ctx.idx = 0;
2650
2651 /* now if we have a length, we'll take the hint */
2652 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2653 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2654 unsigned int chunk = 0;
2655 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2656 char c = msg->sol[body];
2657 if (ishex(c)) {
2658 unsigned int hex = toupper(c) - '0';
2659 if (hex > 9)
2660 hex -= 'A' - '9' - 1;
2661 chunk = (chunk << 4) | hex;
2662 } else
2663 break;
2664 body++;
2665 }
2666 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002667 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002668
2669 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002670 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002671
2672 body += 2; // skip CRLF
2673
2674 /* if we support more then one chunk here, we have to do it again when assigning server
2675 * 1. how much entity data do we have? new var
2676 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2677 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2678 */
2679
2680 if (chunk < limit)
2681 limit = chunk; /* only reading one chunk */
2682 } else {
2683 if (msg->hdr_content_len < limit)
2684 limit = msg->hdr_content_len;
2685 }
2686
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002687 http_body_end:
2688 /* we leave once we know we have nothing left to do. This means that we have
2689 * enough bytes, or that we know we'll not get any more (buffer full, read
2690 * buffer closed).
2691 */
2692 if (req->l - body >= limit || /* enough bytes! */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002693 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_NULL | BF_READ_TIMEOUT) ||
Willy Tarreauc52164a2008-08-17 19:17:57 +02002694 tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002695 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002696 t->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau2df28e82008-08-17 15:20:19 +02002697 req->analysers &= ~AN_REQ_HTTP_BODY;
Willy Tarreauffab5b42008-08-17 18:03:28 +02002698 req->analyse_exp = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002699 }
Willy Tarreauc52164a2008-08-17 19:17:57 +02002700 else {
2701 /* Not enough data. We'll re-use the http-request
2702 * timeout here. Ideally, we should set the timeout
2703 * relative to the accept() date. We just set the
2704 * request timeout once at the beginning of the
2705 * request.
2706 */
Willy Tarreau3da77c52008-08-29 09:58:42 +02002707 buffer_write_dis(req);
Willy Tarreauc52164a2008-08-17 19:17:57 +02002708 if (!tick_isset(req->analyse_exp))
2709 req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
2710 return 0;
2711 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002712 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002713
Willy Tarreau2df28e82008-08-17 15:20:19 +02002714 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2715 * probably reduce one day's debugging session.
2716 */
2717#ifdef DEBUG_DEV
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002718 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 +02002719 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2720 __FILE__, __LINE__, req->analysers);
2721 ABORT_NOW();
2722 }
2723#endif
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002724 req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY;
Willy Tarreaudafde432008-08-17 01:00:46 +02002725 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002726}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002727
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002728/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002729 * It normally returns zero, but may return 1 if it absolutely needs to be
2730 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002731 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002732 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002733 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002734int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002735{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002736 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002737 struct buffer *req = t->req;
2738 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002739
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002740 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 +02002741 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002742 t,
2743 rep,
2744 rep->rex, rep->wex,
2745 rep->flags,
2746 rep->l,
2747 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002748
Willy Tarreau2df28e82008-08-17 15:20:19 +02002749 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002750 /*
2751 * Now parse the partial (or complete) lines.
2752 * We will check the response syntax, and also join multi-line
2753 * headers. An index of all the lines will be elaborated while
2754 * parsing.
2755 *
2756 * For the parsing, we use a 28 states FSM.
2757 *
2758 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002759 * rep->data + rep->som = beginning of response
2760 * rep->data + rep->eoh = end of processed headers / start of current one
2761 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002762 * rep->lr = first non-visited byte
2763 * rep->r = end of data
2764 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002765
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002766 int cur_idx;
2767 struct http_msg *msg = &txn->rsp;
2768 struct proxy *cur_proxy;
2769
2770 if (likely(rep->lr < rep->r))
2771 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2772
2773 /* 1: we might have to print this header in debug mode */
2774 if (unlikely((global.mode & MODE_DEBUG) &&
2775 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2776 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2777 char *eol, *sol;
2778
2779 sol = rep->data + msg->som;
2780 eol = sol + msg->sl.rq.l;
2781 debug_hdr("srvrep", t, sol, eol);
2782
2783 sol += hdr_idx_first_pos(&txn->hdr_idx);
2784 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2785
2786 while (cur_idx) {
2787 eol = sol + txn->hdr_idx.v[cur_idx].len;
2788 debug_hdr("srvhdr", t, sol, eol);
2789 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2790 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002791 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002792 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002793
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002794 /*
2795 * Now we quickly check if we have found a full valid response.
2796 * If not so, we check the FD and buffer states before leaving.
2797 * A full response is indicated by the fact that we have seen
2798 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2799 * responses are checked first.
2800 *
2801 * Depending on whether the client is still there or not, we
2802 * may send an error response back or not. Note that normally
2803 * we should only check for HTTP status there, and check I/O
2804 * errors somewhere else.
2805 */
2806
2807 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002808 /* Invalid response */
2809 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2810 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002811 //buffer_shutr(rep);
2812 //buffer_shutw(req);
2813 //fd_delete(req->cons->fd);
2814 //req->cons->state = SI_ST_CLO;
2815 buffer_shutr_now(rep);
2816 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002817 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002818 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002819 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002820 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002821 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002822 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002823 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002824 txn->status = 502;
2825 client_return(t, error_message(t, HTTP_ERR_502));
2826 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002827 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002828 if (!(t->flags & SN_FINST_MASK))
2829 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002830
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002831 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2832 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002833
Willy Tarreaudafde432008-08-17 01:00:46 +02002834 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002835 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002836 /* too large response does not fit in buffer. */
2837 else if (rep->flags & BF_FULL) {
2838 goto hdr_response_bad;
2839 }
2840 /* read error */
2841 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002842 buffer_shutr_now(rep);
2843 buffer_shutw_now(req);
2844 //fd_delete(req->cons->fd);
2845 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002846 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002847 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002848 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002849 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002850 //}
2851 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002852 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002853 txn->status = 502;
2854 client_return(t, error_message(t, HTTP_ERR_502));
2855 if (!(t->flags & SN_ERR_MASK))
2856 t->flags |= SN_ERR_SRVCL;
2857 if (!(t->flags & SN_FINST_MASK))
2858 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002859
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002860 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2861 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002862
Willy Tarreaudafde432008-08-17 01:00:46 +02002863 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002864 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002865 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002866 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002867 buffer_shutr_now(rep);
2868 buffer_shutw_now(req);
2869 //fd_delete(req->cons->fd);
2870 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002871 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002872 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002873 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002874 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002875 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002876 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002877 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002878 txn->status = 504;
2879 client_return(t, error_message(t, HTTP_ERR_504));
2880 if (!(t->flags & SN_ERR_MASK))
2881 t->flags |= SN_ERR_SRVTO;
2882 if (!(t->flags & SN_FINST_MASK))
2883 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002884
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002885 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2886 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002887 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002888 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002889 /* write error to client, or close from server */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002890 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR|BF_READ_NULL)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002891 buffer_shutr_now(rep);
2892 buffer_shutw_now(req);
2893 //fd_delete(req->cons->fd);
2894 //req->cons->state = SI_ST_CLO;
2895 if (t->srv) {
2896 //t->srv->cur_sess--;
2897 t->srv->failed_resp++;
2898 //sess_change_server(t, NULL);
2899 }
2900 t->be->failed_resp++;
2901 rep->analysers = 0;
2902 txn->status = 502;
2903 client_return(t, error_message(t, HTTP_ERR_502));
2904 if (!(t->flags & SN_ERR_MASK))
2905 t->flags |= SN_ERR_SRVCL;
2906 if (!(t->flags & SN_FINST_MASK))
2907 t->flags |= SN_FINST_H;
2908
2909 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2910 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002911
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002912 return 0;
2913 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002914 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002915 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002916 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002917
Willy Tarreau21d2af32008-02-14 20:25:24 +01002918
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002919 /*****************************************************************
2920 * More interesting part now : we know that we have a complete *
2921 * response which at least looks like HTTP. We have an indicator *
2922 * of each header's length, so we can parse them quickly. *
2923 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002924
Willy Tarreau2df28e82008-08-17 15:20:19 +02002925 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002926
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002927 /* ensure we keep this pointer to the beginning of the message */
2928 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002929
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002930 /*
2931 * 1: get the status code and check for cacheability.
2932 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002933
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002934 t->logs.logwait &= ~LW_RESP;
2935 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002936
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002937 switch (txn->status) {
2938 case 200:
2939 case 203:
2940 case 206:
2941 case 300:
2942 case 301:
2943 case 410:
2944 /* RFC2616 @13.4:
2945 * "A response received with a status code of
2946 * 200, 203, 206, 300, 301 or 410 MAY be stored
2947 * by a cache (...) unless a cache-control
2948 * directive prohibits caching."
2949 *
2950 * RFC2616 @9.5: POST method :
2951 * "Responses to this method are not cacheable,
2952 * unless the response includes appropriate
2953 * Cache-Control or Expires header fields."
2954 */
2955 if (likely(txn->meth != HTTP_METH_POST) &&
2956 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2957 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2958 break;
2959 default:
2960 break;
2961 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002962
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002963 /*
2964 * 2: we may need to capture headers
2965 */
2966 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2967 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2968 txn->rsp.cap, t->fe->rsp_cap);
2969
2970 /*
2971 * 3: we will have to evaluate the filters.
2972 * As opposed to version 1.2, now they will be evaluated in the
2973 * filters order and not in the header order. This means that
2974 * each filter has to be validated among all headers.
2975 *
2976 * Filters are tried with ->be first, then with ->fe if it is
2977 * different from ->be.
2978 */
2979
2980 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2981
2982 cur_proxy = t->be;
2983 while (1) {
2984 struct proxy *rule_set = cur_proxy;
2985
2986 /* try headers filters */
2987 if (rule_set->rsp_exp != NULL) {
2988 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2989 return_bad_resp:
2990 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002991 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002992 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002993 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002994 }
2995 cur_proxy->failed_resp++;
2996 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002997 buffer_shutr_now(rep);
2998 buffer_shutw_now(req);
2999 //fd_delete(req->cons->fd);
3000 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02003001 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003002 txn->status = 502;
3003 client_return(t, error_message(t, HTTP_ERR_502));
3004 if (!(t->flags & SN_ERR_MASK))
3005 t->flags |= SN_ERR_PRXCOND;
3006 if (!(t->flags & SN_FINST_MASK))
3007 t->flags |= SN_FINST_H;
3008 /* We used to have a free connection slot. Since we'll never use it,
3009 * we have to inform the server that it may be used by another session.
3010 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003011 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
3012 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02003013 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003014 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003015 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003016
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003017 /* has the response been denied ? */
3018 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01003019 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003020 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003021 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003022 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01003023 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003024 cur_proxy->denied_resp++;
3025 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003026 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003027
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003028 /* We might have to check for "Connection:" */
3029 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3030 !(t->flags & SN_CONN_CLOSED)) {
3031 char *cur_ptr, *cur_end, *cur_next;
3032 int cur_idx, old_idx, delta, val;
3033 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003034
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003035 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3036 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003037
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003038 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3039 cur_hdr = &txn->hdr_idx.v[cur_idx];
3040 cur_ptr = cur_next;
3041 cur_end = cur_ptr + cur_hdr->len;
3042 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003043
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003044 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3045 if (val) {
3046 /* 3 possibilities :
3047 * - we have already set Connection: close,
3048 * so we remove this line.
3049 * - we have not yet set Connection: close,
3050 * but this line indicates close. We leave
3051 * it untouched and set the flag.
3052 * - we have not yet set Connection: close,
3053 * and this line indicates non-close. We
3054 * replace it.
3055 */
3056 if (t->flags & SN_CONN_CLOSED) {
3057 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3058 txn->rsp.eoh += delta;
3059 cur_next += delta;
3060 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3061 txn->hdr_idx.used--;
3062 cur_hdr->len = 0;
3063 } else {
3064 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3065 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3066 "close", 5);
3067 cur_next += delta;
3068 cur_hdr->len += delta;
3069 txn->rsp.eoh += delta;
3070 }
3071 t->flags |= SN_CONN_CLOSED;
3072 }
3073 }
3074 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003075 }
3076 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003077
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003078 /* add response headers from the rule sets in the same order */
3079 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3080 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3081 rule_set->rsp_add[cur_idx])) < 0)
3082 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003083 }
3084
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003085 /* check whether we're already working on the frontend */
3086 if (cur_proxy == t->fe)
3087 break;
3088 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003089 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003090
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003091 /*
3092 * 4: check for server cookie.
3093 */
3094 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3095 || (t->be->options & PR_O_CHK_CACHE))
3096 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003097
Willy Tarreaubaaee002006-06-26 02:48:02 +02003098
Willy Tarreaua15645d2007-03-18 16:22:39 +01003099 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003100 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003101 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003102 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3103 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003104
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003105 /*
3106 * 6: add server cookie in the response if needed
3107 */
3108 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3109 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3110 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003111
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003112 /* the server is known, it's not the one the client requested, we have to
3113 * insert a set-cookie here, except if we want to insert only on POST
3114 * requests and this one isn't. Note that servers which don't have cookies
3115 * (eg: some backup servers) will return a full cookie removal request.
3116 */
3117 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3118 t->be->cookie_name,
3119 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003120
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003121 if (t->be->cookie_domain)
3122 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003123
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003124 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3125 trash, len)) < 0)
3126 goto return_bad_resp;
3127 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003128
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003129 /* Here, we will tell an eventual cache on the client side that we don't
3130 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3131 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3132 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3133 */
3134 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003135
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003136 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3137
3138 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3139 "Cache-control: private", 22)) < 0)
3140 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003141 }
3142 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003143
Willy Tarreaubaaee002006-06-26 02:48:02 +02003144
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003145 /*
3146 * 7: check if result will be cacheable with a cookie.
3147 * We'll block the response if security checks have caught
3148 * nasty things such as a cacheable cookie.
3149 */
3150 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3151 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3152 (t->be->options & PR_O_CHK_CACHE)) {
3153
3154 /* we're in presence of a cacheable response containing
3155 * a set-cookie header. We'll block it as requested by
3156 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003157 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003158 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003159 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003160 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003161 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003162 }
3163 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003164
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003165 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3166 t->be->id, t->srv?t->srv->id:"<dispatch>");
3167 send_log(t->be, LOG_ALERT,
3168 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3169 t->be->id, t->srv?t->srv->id:"<dispatch>");
3170 goto return_srv_prx_502;
3171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003172
3173 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003174 * 8: add "Connection: close" if needed and not yet set.
3175 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003176 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003177 if (!(t->flags & SN_CONN_CLOSED) &&
3178 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3179 if ((unlikely(msg->sl.st.v_l != 8) ||
3180 unlikely(req->data[msg->som + 7] != '0')) &&
3181 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3182 "Connection: close", 17)) < 0)
3183 goto return_bad_resp;
3184 t->flags |= SN_CONN_CLOSED;
3185 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003186
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003187 /*************************************************************
3188 * OK, that's finished for the headers. We have done what we *
3189 * could. Let's switch to the DATA state. *
3190 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003191
Willy Tarreaue393fe22008-08-16 22:18:07 +02003192 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003193 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003194
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003195#ifdef CONFIG_HAP_TCPSPLICE
3196 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3197 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003198 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003199 }
3200#endif
3201 /* if the user wants to log as soon as possible, without counting
3202 * bytes from the server, then this is the right moment. We have
3203 * to temporarily assign bytes_out to log what we currently have.
3204 */
3205 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3206 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3207 t->logs.bytes_out = txn->rsp.eoh;
3208 if (t->fe->to_log & LW_REQ)
3209 http_sess_log(t);
3210 else
3211 tcp_sess_log(t);
3212 t->logs.bytes_out = 0;
3213 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003214
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003215 /* Note: we must not try to cheat by jumping directly to DATA,
3216 * otherwise we would not let the client side wake up.
3217 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003218
Willy Tarreaudafde432008-08-17 01:00:46 +02003219 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003220 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003221
Willy Tarreau2df28e82008-08-17 15:20:19 +02003222 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3223 * probably reduce one day's debugging session.
3224 */
3225#ifdef DEBUG_DEV
3226 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3227 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3228 __FILE__, __LINE__, rep->analysers);
3229 ABORT_NOW();
3230 }
3231#endif
3232 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003233 return 0;
3234}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003235
Willy Tarreauf9839bd2008-08-27 23:57:16 +02003236///*
3237// * Manages the client FSM and its socket. It normally returns zero, but may
3238// * return 1 if it absolutely wants to be called again.
3239// *
3240// * Note: process_cli is the ONLY function allowed to set cli_state to anything
3241// * but CL_STCLOSE.
3242// */
3243//int process_cli(struct session *t)
3244//{
3245// struct buffer *req = t->req;
3246// struct buffer *rep = t->rep;
3247//
3248// 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",
3249// now_ms, __FUNCTION__,
3250// t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/
3251// cli_stnames[t->cli_state],
3252// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0,
3253// t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0,
3254// req->rex, rep->wex,
3255// req->flags, rep->flags,
3256// req->l, rep->l);
3257//
3258// update_state:
3259// /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3260// * still set this state (and will do until unix sockets are converted).
3261// */
3262// if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
3263// /* we can skip most of the tests at once if some conditions are not met */
3264// if (!((fdtab[t->cli_fd].state == FD_STERROR) ||
3265// (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) ||
3266// (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) ||
3267// (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) ||
3268// (!(rep->flags & BF_SHUTW) &&
3269// (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR))))
3270// goto update_timeouts;
3271//
3272// /* read or write error */
3273// if (fdtab[t->cli_fd].state == FD_STERROR) {
3274// buffer_shutr(req);
3275// req->flags |= BF_READ_ERROR;
3276// buffer_shutw(rep);
3277// rep->flags |= BF_WRITE_ERROR;
3278// fd_delete(t->cli_fd);
3279// t->cli_state = CL_STCLOSE;
3280// trace_term(t, TT_HTTP_CLI_1);
3281// if (!req->analysers) {
3282// if (!(t->flags & SN_ERR_MASK))
3283// t->flags |= SN_ERR_CLICL;
3284// if (!(t->flags & SN_FINST_MASK)) {
3285// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3286// t->flags |= SN_FINST_Q;
3287// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3288// t->flags |= SN_FINST_C;
3289// else
3290// t->flags |= SN_FINST_D;
3291// }
3292// }
3293// goto update_state;
3294// }
3295// /* last read, or end of server write */
3296// else if (!(req->flags & BF_SHUTR) && /* not already done */
3297// req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) {
3298// buffer_shutr(req);
3299// if (!(rep->flags & BF_SHUTW)) {
3300// EV_FD_CLR(t->cli_fd, DIR_RD);
3301// trace_term(t, TT_HTTP_CLI_2);
3302// } else {
3303// /* output was already closed */
3304// fd_delete(t->cli_fd);
3305// t->cli_state = CL_STCLOSE;
3306// trace_term(t, TT_HTTP_CLI_3);
3307// }
3308// goto update_state;
3309// }
3310// /* last server read and buffer empty : we only check them when we're
3311// * allowed to forward the data.
3312// */
3313// else if (!(rep->flags & BF_SHUTW) && /* not already done */
3314// ((rep->flags & BF_SHUTW_NOW) ||
3315// (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD &&
3316// rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) {
3317// buffer_shutw(rep);
3318// if (!(req->flags & BF_SHUTR)) {
3319// EV_FD_CLR(t->cli_fd, DIR_WR);
3320// shutdown(t->cli_fd, SHUT_WR);
3321// trace_term(t, TT_HTTP_CLI_4);
3322// } else {
3323// fd_delete(t->cli_fd);
3324// t->cli_state = CL_STCLOSE;
3325// trace_term(t, TT_HTTP_CLI_5);
3326// }
3327// goto update_state;
3328// }
3329// /* read timeout */
3330// else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) {
3331// buffer_shutr(req);
3332// if (!(rep->flags & BF_SHUTW)) {
3333// EV_FD_CLR(t->cli_fd, DIR_RD);
3334// trace_term(t, TT_HTTP_CLI_6);
3335// } else {
3336// /* output was already closed */
3337// fd_delete(t->cli_fd);
3338// t->cli_state = CL_STCLOSE;
3339// trace_term(t, TT_HTTP_CLI_7);
3340// }
3341// if (!req->analysers) {
3342// if (!(t->flags & SN_ERR_MASK))
3343// t->flags |= SN_ERR_CLITO;
3344// if (!(t->flags & SN_FINST_MASK)) {
3345// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3346// t->flags |= SN_FINST_Q;
3347// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3348// t->flags |= SN_FINST_C;
3349// else
3350// t->flags |= SN_FINST_D;
3351// }
3352// }
3353// goto update_state;
3354// }
3355// /* write timeout */
3356// else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) {
3357// buffer_shutw(rep);
3358// if (!(req->flags & BF_SHUTR)) {
3359// EV_FD_CLR(t->cli_fd, DIR_WR);
3360// shutdown(t->cli_fd, SHUT_WR);
3361// trace_term(t, TT_HTTP_CLI_8);
3362// } else {
3363// fd_delete(t->cli_fd);
3364// t->cli_state = CL_STCLOSE;
3365// trace_term(t, TT_HTTP_CLI_9);
3366// }
3367// if (!req->analysers) {
3368// if (!(t->flags & SN_ERR_MASK))
3369// t->flags |= SN_ERR_CLITO;
3370// if (!(t->flags & SN_FINST_MASK)) {
3371// if (req->cons->err_type <= SI_ET_QUEUE_ABRT)
3372// t->flags |= SN_FINST_Q;
3373// else if (req->cons->err_type <= SI_ET_CONN_OTHER)
3374// t->flags |= SN_FINST_C;
3375// else
3376// t->flags |= SN_FINST_D;
3377// }
3378// }
3379// goto update_state;
3380// }
3381//
3382// update_timeouts:
3383// /* manage read timeout */
3384// if (!(req->flags & BF_SHUTR)) {
3385// if (req->flags & BF_FULL) {
3386// /* no room to read more data */
3387// if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3388// /* stop reading until we get some space */
3389// req->rex = TICK_ETERNITY;
3390// }
3391// } else {
3392// EV_FD_COND_S(t->cli_fd, DIR_RD);
3393// req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3394// }
3395// }
3396//
3397// /* manage write timeout */
3398// if (!(rep->flags & BF_SHUTW)) {
3399// /* first, we may have to produce data (eg: stats).
3400// * right now, this is limited to the SHUTR state.
3401// */
3402// if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) {
3403// produce_content(t);
3404// if (rep->flags & BF_EMPTY) {
3405// buffer_shutw(rep);
3406// fd_delete(t->cli_fd);
3407// t->cli_state = CL_STCLOSE;
3408// trace_term(t, TT_HTTP_CLI_10);
3409// goto update_state;
3410// }
3411// }
3412//
3413// /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
3414// if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) {
3415// if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3416// /* stop writing */
3417// rep->wex = TICK_ETERNITY;
3418// }
3419// } else {
3420// /* buffer not empty */
3421// EV_FD_COND_S(t->cli_fd, DIR_WR);
3422// if (!tick_isset(rep->wex)) {
3423// /* restart writing */
3424// rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
3425// if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) {
3426// /* FIXME: to prevent the client from expiring read timeouts during writes,
3427// * we refresh it, except if it was already infinite. */
3428// req->rex = rep->wex;
3429// }
3430// }
3431// }
3432// }
3433// return 0; /* other cases change nothing */
3434// }
3435// else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
3436// if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3437// int len;
3438// 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);
3439// write(1, trash, len);
3440// }
3441// return 0;
3442// }
3443//#ifdef DEBUG_DEV
3444// fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3445// ABORT_NOW();
3446//#endif
3447// return 0;
3448//}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003449
Willy Tarreaubaaee002006-06-26 02:48:02 +02003450
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003451/* Return 1 if the pending connection has failed and should be retried,
3452 * otherwise zero. We may only come here in SI_ST_CON state, which means that
3453 * the socket's file descriptor is known.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003454 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003455int tcp_connection_status(struct session *t)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003456{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003457 struct buffer *req = t->req;
3458 struct buffer *rep = t->rep;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003459 int conn_err = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003460
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003461 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3462 now_ms, __FUNCTION__,
3463 cli_stnames[t->cli_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003464 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003465 req->flags, rep->flags,
3466 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003467
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003468 if ((req->flags & BF_SHUTW_NOW) ||
3469 (rep->flags & BF_SHUTW) ||
3470 ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreau3da77c52008-08-29 09:58:42 +02003471 ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) ||
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003472 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3473
3474 trace_term(t, TT_HTTP_SRV_5);
3475 req->wex = TICK_ETERNITY;
3476 fd_delete(req->cons->fd);
3477 if (t->srv) {
3478 t->srv->cur_sess--;
3479 sess_change_server(t, NULL);
3480 }
3481 /* note that this must not return any error because it would be able to
3482 * overwrite the client_retnclose() output.
3483 */
3484 //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
3485
3486 // FIXME: should we set rep->MAY_FORWARD ?
3487 buffer_shutw(req);
3488 buffer_shutr(rep);
3489 req->cons->state = SI_ST_CLO;
3490 if (!req->cons->err_type)
3491 req->cons->err_type = SI_ET_CONN_ABRT;
3492 req->cons->err_loc = t->srv;
3493 return 0;
3494 }
3495
3496 /* check for timeouts and asynchronous connect errors */
3497 if (fdtab[req->cons->fd].state == FD_STERROR) {
3498 conn_err = SI_ET_CONN_ERR;
3499 if (!req->cons->err_type)
3500 req->cons->err_type = SI_ET_CONN_ERR;
3501 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02003502 else if (!(req->flags & BF_WRITE_ACTIVITY)) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003503 /* nothing happened, maybe we timed out */
3504 if (tick_is_expired(req->wex, now_ms)) {
3505 conn_err = SI_ET_CONN_TO;
3506 if (!req->cons->err_type)
3507 req->cons->err_type = SI_ET_CONN_TO;
3508 }
3509 else
3510 return 0; /* let's wait a bit more */
3511 }
3512
3513 if (conn_err) {
3514 fd_delete(req->cons->fd);
3515 req->cons->state = SI_ST_CLO;
3516
3517 if (t->srv) {
3518 t->srv->cur_sess--;
3519 sess_change_server(t, NULL);
3520 req->cons->err_loc = t->srv;
3521 }
3522
3523 /* ensure that we have enough retries left */
3524 if (srv_count_retry_down(t, conn_err))
3525 return 0;
3526
3527 if (conn_err == SI_ET_CONN_ERR) {
3528 /* we encountered an immediate connection error, and we
3529 * will have to retry connecting to the same server, most
3530 * likely leading to the same result. To avoid this, we
3531 * fake a connection timeout to retry after a turn-around
3532 * time of 1 second. We will wait in the previous if block.
3533 */
3534 req->cons->state = SI_ST_TAR;
3535 req->wex = tick_add(now_ms, MS_TO_TICKS(1000));
3536 return 0;
3537 }
3538
3539 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3540 /* We're on our last chance, and the REDISP option was specified.
3541 * We will ignore cookie and force to balance or use the dispatcher.
3542 */
3543 /* let's try to offer this slot to anybody */
3544 if (may_dequeue_tasks(t->srv, t->be))
3545 process_srv_queue(t->srv);
3546
3547 /* it's left to the dispatcher to choose a server */
3548 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3549 t->prev_srv = t->srv;
3550 } else {
3551 /* we just want to retry */
3552 if (t->srv)
3553 t->srv->retries++;
3554 t->be->retries++;
3555
3556 /* Now we will try to either reconnect to the same server or
3557 * connect to another server. If the connection gets queued
3558 * because all servers are saturated, then we will go back to
3559 * the idle state where the buffer's consumer is marked as
3560 * unknown.
3561 */
3562 if (srv_retryable_connect(t)) {
3563 /* success or unrecoverable error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003564 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003565 return 0;
3566 }
3567 }
3568
3569 /* We'll rely on the caller to try to get a connection again */
3570 return 1;
3571 }
3572 else {
3573 /* no error and write OK : connection succeeded */
3574 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
3575 req->cons->state = SI_ST_EST;
3576 req->cons->err_type = SI_ET_NONE;
3577 req->cons->err_loc = NULL;
3578
3579 if (req->flags & BF_EMPTY) {
3580 EV_FD_CLR(req->cons->fd, DIR_WR);
3581 req->wex = TICK_ETERNITY;
3582 } else {
3583 EV_FD_SET(req->cons->fd, DIR_WR);
3584 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3585 if (tick_isset(req->wex)) {
3586 /* FIXME: to prevent the server from expiring read timeouts during writes,
3587 * we refresh it. */
3588 rep->rex = req->wex;
3589 }
3590 }
3591
3592 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3593 if (!(rep->flags & BF_HIJACK)) {
3594 EV_FD_SET(req->cons->fd, DIR_RD);
3595 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3596 }
3597 buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
3598
3599 /* if the user wants to log as soon as possible, without counting
3600 bytes from the server, then this is the right moment. */
3601 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3602 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3603 tcp_sess_log(t);
3604 }
3605#ifdef CONFIG_HAP_TCPSPLICE
3606 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3607 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003608 tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003609 }
3610#endif
3611 }
3612 else {
3613 rep->analysers |= AN_RTR_HTTP_HDR;
3614 buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
3615 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3616 /* reset hdr_idx which was already initialized by the request.
3617 * right now, the http parser does it.
3618 * hdr_idx_init(&t->txn.hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003619 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003620 }
3621
3622 if (!rep->analysers)
Willy Tarreau3da77c52008-08-29 09:58:42 +02003623 buffer_write_ena(t->rep);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003624 req->wex = TICK_ETERNITY;
3625 return 0;
3626 }
3627}
3628
3629
3630/*
3631 * This function tries to assign a server to a stream_sock interface.
3632 * It may be called only for t->req->cons->state = one of { SI_ST_INI,
3633 * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS
3634 * in case of success, or SI_ST_CLO in case of failure. It returns 1 if
3635 * it returns SI_ST_ASS, otherwise zero.
3636 */
3637int stream_sock_assign_server(struct session *t)
3638{
3639 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3640 now_ms, __FUNCTION__,
3641 cli_stnames[t->cli_state],
3642 t->rep->rex, t->req->wex,
3643 t->req->flags, t->rep->flags,
3644 t->req->l, t->rep->l);
3645
3646 if (t->req->cons->state == SI_ST_TAR) {
3647 /* connection might be aborted */
3648 if ((t->req->flags & BF_SHUTW_NOW) ||
3649 (t->rep->flags & BF_SHUTW) ||
3650 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3651 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003652
Willy Tarreauf8533202008-08-16 14:55:08 +02003653 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003654 t->req->wex = TICK_ETERNITY;
3655
3656 // FIXME: should we set rep->MAY_FORWARD ?
3657 buffer_shutr(t->rep);
3658 buffer_shutw(t->req);
3659 if (!t->req->cons->err_type)
3660 t->req->cons->err_type = SI_ET_CONN_ABRT;
3661 t->req->cons->state = SI_ST_CLO;
3662 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003663 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003664
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003665 if (!tick_is_expired(t->req->wex, now_ms))
3666 return 0; /* still in turn-around */
3667
3668 t->req->cons->state = SI_ST_INI;
3669 }
3670 else if (t->req->cons->state == SI_ST_QUE) {
3671 if (t->pend_pos) {
3672 /* request still in queue... */
3673 if (tick_is_expired(t->req->wex, now_ms)) {
3674 /* ... and timeout expired */
3675 trace_term(t, TT_HTTP_SRV_3);
3676 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003677 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003678 if (t->srv)
3679 t->srv->failed_conns++;
3680 t->be->failed_conns++;
3681
3682 // FIXME: should we set rep->MAY_FORWARD ?
3683 buffer_shutr(t->rep);
3684 buffer_shutw(t->req);
3685 t->req->flags |= BF_WRITE_TIMEOUT;
3686 if (!t->req->cons->err_type)
3687 t->req->cons->err_type = SI_ET_QUEUE_TO;
3688 t->req->cons->state = SI_ST_CLO;
3689 return 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003690 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003691 /* connection remains in queue, check if we have to abort it */
3692 if ((t->req->flags & BF_SHUTW_NOW) ||
3693 (t->rep->flags & BF_SHUTW) ||
3694 ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */
3695 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) {
3696 /* give up */
3697 trace_term(t, TT_HTTP_SRV_1);
3698 t->req->wex = TICK_ETERNITY;
3699 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003700
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003701 // FIXME: should we set rep->MAY_FORWARD ?
3702 buffer_shutr(t->rep);
3703 buffer_shutw(t->req);
3704 if (!t->req->cons->err_type)
3705 t->req->cons->err_type = SI_ET_QUEUE_ABRT;
3706 t->req->cons->state = SI_ST_CLO;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003707 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003708 return 0;
3709 }
3710 /* The connection is not in the queue anymore */
3711 t->req->cons->state = SI_ST_INI;
3712 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003713
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003714 /* we may get here from above */
3715 if (t->req->cons->state == SI_ST_INI) {
3716 /* no connection in progress, we have to get a new one */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003717
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003718 /* first, check if the connection has been aborted */
3719 if ((t->req->flags & BF_SHUTW_NOW) ||
3720 (t->rep->flags & BF_SHUTW) ||
3721 ((t->req->flags & BF_SHUTR) &&
3722 (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003723
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003724 trace_term(t, TT_HTTP_SRV_1);
3725 t->req->wex = TICK_ETERNITY;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003726
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003727 // FIXME: should we set rep->MAY_FORWARD ?
3728 buffer_shutr(t->rep);
3729 buffer_shutw(t->req);
3730 if (!t->req->cons->err_type)
3731 t->req->cons->err_type = SI_ET_CONN_ABRT;
3732 t->req->cons->state = SI_ST_CLO;
3733 return 0;
3734 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003735
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003736 /* try to get a server assigned */
3737 if (srv_redispatch_connect(t) != 0) {
3738 /* we did not get any server, let's check the cause */
3739 if (t->req->cons->state == SI_ST_QUE) {
3740 /* the connection was queued, that's OK */
3741 return 0;
3742 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003743
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003744 trace_term(t, TT_HTTP_SRV_2);
3745 t->req->wex = TICK_ETERNITY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003746
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003747 // FIXME: should we set rep->MAY_FORWARD ?
3748 buffer_shutr(t->rep);
3749 buffer_shutw(t->req);
3750 t->req->flags |= BF_WRITE_ERROR;
3751 if (!t->req->cons->err_type)
3752 t->req->cons->err_type = SI_ET_CONN_OTHER;
3753 t->req->cons->state = SI_ST_CLO;
3754 return 0;
3755 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003756
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003757 t->req->cons->state = SI_ST_ASS;
3758 /* Once the server is assigned, we have to return because
3759 * the caller might be interested in checking several
3760 * things before connecting.
3761 */
3762 return 1;
3763 }
3764 return 0;
3765}
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003766
Willy Tarreauf8533202008-08-16 14:55:08 +02003767
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003768/*
3769 * This function tries to establish a connection to an assigned server. It also
3770 * performs connection retries. It may only be called with t->req->cons->state
3771 * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI,
3772 * SI_ST_EST, or SI_ST_CLO.
3773 */
3774int stream_sock_connect_server(struct session *t)
3775{
3776 if (t->req->cons->state == SI_ST_ASS) {
3777 /* server assigned to request, we have to try to connect now */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003778
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003779 if (!srv_retryable_connect(t)) {
3780 /* we need to redispatch */
3781 t->req->cons->state = SI_ST_INI;
3782 return 0;
3783 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003784
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003785 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3786 if (t->req->cons->state != SI_ST_CON) {
3787 /* it was an error */
3788 trace_term(t, TT_HTTP_SRV_4);
3789 t->req->wex = TICK_ETERNITY;
3790
3791 // FIXME: should we set rep->MAY_FORWARD ?
3792 buffer_shutr(t->rep);
3793 buffer_shutw(t->req);
3794 t->req->flags |= BF_WRITE_ERROR;
3795 if (!t->req->cons->err_type)
3796 t->req->cons->err_type = SI_ET_CONN_OTHER;
3797 t->req->cons->state = SI_ST_CLO;
3798 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003799 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003800 /* We have a socket and switched to SI_ST_CON */
3801 }
3802
3803 /* we may also get here from above */
3804 if (t->req->cons->state == SI_ST_CON) {
3805 /* connection in progress or just completed */
3806 if (!tcp_connection_status(t))
3807 return 0;
3808 }
3809 return 0;
3810}
3811
3812
3813/*
3814 * Tries to establish a connection to the server and associate it to the
3815 * request buffer's consumer side. It is assumed that this function will not be
Willy Tarreau3da77c52008-08-29 09:58:42 +02003816 * be called with SI_ST_EST nor with BF_WRITE_ENA cleared. It normally
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003817 * returns zero, but may return 1 if it absolutely wants to be called again.
3818 */
3819int process_srv_conn(struct session *t)
3820{
3821 DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
3822 now_ms, __FUNCTION__,
3823 cli_stnames[t->cli_state],
3824 t->rep->rex, t->req->wex,
3825 t->req->flags, t->rep->flags,
3826 t->req->l, t->rep->l);
3827
3828 do {
3829 if (t->req->cons->state == SI_ST_INI ||
3830 t->req->cons->state == SI_ST_TAR ||
3831 t->req->cons->state == SI_ST_QUE) {
3832 /* try to assign a server */
3833 if (!stream_sock_assign_server(t))
3834 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003835 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003836
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003837 if (t->req->cons->state == SI_ST_ASS &&
3838 t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) {
3839 /* Server supporting redirection and it is possible.
3840 * Invalid requests are reported as such. It concerns all
3841 * the largest ones.
3842 */
3843 struct http_txn *txn = &t->txn;
3844 struct chunk rdr;
3845 char *path;
3846 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003847
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003848 /* 1: create the response header */
3849 rdr.len = strlen(HTTP_302);
3850 rdr.str = trash;
3851 memcpy(rdr.str, HTTP_302, rdr.len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003852
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003853 /* 2: add the server's prefix */
3854 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3855 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003856
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003857 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3858 rdr.len += t->srv->rdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003859
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003860 /* 3: add the request URI */
3861 path = http_get_path(txn);
3862 if (!path)
3863 goto cancel_redir;
3864 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3865 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3866 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003867
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003868 memcpy(rdr.str + rdr.len, path, len);
3869 rdr.len += len;
3870 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3871 rdr.len += 4;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003872
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003873 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
3874 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003875
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003876 /* FIXME: we should increase a counter of redirects per server and per backend. */
3877 if (t->srv)
3878 t->srv->cum_sess++;
3879
3880 t->req->cons->state = SI_ST_CLO;
3881 return 0;
3882 cancel_redir:
3883 //txn->status = 400;
3884 //t->fe->failed_req++;
3885 //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3886 // 400, error_message(t, HTTP_ERR_400));
3887 trace_term(t, TT_HTTP_SRV_4);
3888
3889 // FIXME: should we set rep->MAY_FORWARD ?
3890 buffer_shutw(t->req);
3891 buffer_shutr(t->rep);
3892 if (!t->req->cons->err_type)
3893 t->req->cons->err_type = SI_ET_CONN_OTHER;
3894 t->req->cons->state = SI_ST_CLO;
3895 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003896 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003897
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003898 if (t->req->cons->state == SI_ST_CON ||
3899 t->req->cons->state == SI_ST_ASS) {
3900 stream_sock_connect_server(t);
3901 }
3902 } while (t->req->cons->state != SI_ST_CLO &&
3903 t->req->cons->state != SI_ST_CON &&
3904 t->req->cons->state != SI_ST_EST);
3905 return 0;
3906}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003907
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003908
Willy Tarreaubaaee002006-06-26 02:48:02 +02003909/*
3910 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003911 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003912 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3913 * buffer, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003914 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003915 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003916 */
3917int produce_content(struct session *s)
3918{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003919 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau72b179a2008-08-28 16:01:32 +02003920 buffer_stop_hijack(s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003921 return 1;
3922 }
3923 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003924 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003925 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003926 if (ret >= 0)
3927 return ret;
3928 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003929 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003930
Willy Tarreau91861262007-10-17 17:06:05 +02003931 /* unknown data source or internal error */
3932 s->txn.status = 500;
3933 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003934 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003935 if (!(s->flags & SN_ERR_MASK))
3936 s->flags |= SN_ERR_PRXCOND;
3937 if (!(s->flags & SN_FINST_MASK))
3938 s->flags |= SN_FINST_R;
Willy Tarreau72b179a2008-08-28 16:01:32 +02003939 buffer_stop_hijack(s->rep);
Willy Tarreau91861262007-10-17 17:06:05 +02003940 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003941}
3942
3943
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003944/* Iterate the same filter through all request headers.
3945 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003946 * Since it can manage the switch to another backend, it updates the per-proxy
3947 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003948 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003949int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003950{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003951 char term;
3952 char *cur_ptr, *cur_end, *cur_next;
3953 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003954 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003955 struct hdr_idx_elem *cur_hdr;
3956 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003957
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003958 last_hdr = 0;
3959
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003960 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003961 old_idx = 0;
3962
3963 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003964 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003965 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003966 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003967 (exp->action == ACT_ALLOW ||
3968 exp->action == ACT_DENY ||
3969 exp->action == ACT_TARPIT))
3970 return 0;
3971
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003972 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003973 if (!cur_idx)
3974 break;
3975
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003976 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003977 cur_ptr = cur_next;
3978 cur_end = cur_ptr + cur_hdr->len;
3979 cur_next = cur_end + cur_hdr->cr + 1;
3980
3981 /* Now we have one header between cur_ptr and cur_end,
3982 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003983 */
3984
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003985 /* The annoying part is that pattern matching needs
3986 * that we modify the contents to null-terminate all
3987 * strings before testing them.
3988 */
3989
3990 term = *cur_end;
3991 *cur_end = '\0';
3992
3993 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3994 switch (exp->action) {
3995 case ACT_SETBE:
3996 /* It is not possible to jump a second time.
3997 * FIXME: should we return an HTTP/500 here so that
3998 * the admin knows there's a problem ?
3999 */
4000 if (t->be != t->fe)
4001 break;
4002
4003 /* Swithing Proxy */
4004 t->be = (struct proxy *) exp->replace;
4005
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004006 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004007 * because we have associated req_cap and rsp_cap to the
4008 * frontend, and the beconn will be updated later.
4009 */
4010
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004011 t->rep->rto = t->req->wto = t->be->timeout.server;
4012 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004013 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004014 last_hdr = 1;
4015 break;
4016
4017 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004018 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004019 last_hdr = 1;
4020 break;
4021
4022 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004023 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004024 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004025 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004026 break;
4027
4028 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004029 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004030 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004031 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004032 break;
4033
4034 case ACT_REPLACE:
4035 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4036 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4037 /* FIXME: if the user adds a newline in the replacement, the
4038 * index will not be recalculated for now, and the new line
4039 * will not be counted as a new header.
4040 */
4041
4042 cur_end += delta;
4043 cur_next += delta;
4044 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004045 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004046 break;
4047
4048 case ACT_REMOVE:
4049 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4050 cur_next += delta;
4051
4052 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004053 txn->req.eoh += delta;
4054 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4055 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004056 cur_hdr->len = 0;
4057 cur_end = NULL; /* null-term has been rewritten */
4058 break;
4059
4060 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004061 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004062 if (cur_end)
4063 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004065 /* keep the link from this header to next one in case of later
4066 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004067 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004068 old_idx = cur_idx;
4069 }
4070 return 0;
4071}
4072
4073
4074/* Apply the filter to the request line.
4075 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4076 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004077 * Since it can manage the switch to another backend, it updates the per-proxy
4078 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004079 */
4080int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4081{
4082 char term;
4083 char *cur_ptr, *cur_end;
4084 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004085 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004086 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004087
Willy Tarreau58f10d72006-12-04 02:26:12 +01004088
Willy Tarreau3d300592007-03-18 18:34:41 +01004089 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004090 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004091 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004092 (exp->action == ACT_ALLOW ||
4093 exp->action == ACT_DENY ||
4094 exp->action == ACT_TARPIT))
4095 return 0;
4096 else if (exp->action == ACT_REMOVE)
4097 return 0;
4098
4099 done = 0;
4100
Willy Tarreau9cdde232007-05-02 20:58:19 +02004101 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004102 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004103
4104 /* Now we have the request line between cur_ptr and cur_end */
4105
4106 /* The annoying part is that pattern matching needs
4107 * that we modify the contents to null-terminate all
4108 * strings before testing them.
4109 */
4110
4111 term = *cur_end;
4112 *cur_end = '\0';
4113
4114 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4115 switch (exp->action) {
4116 case ACT_SETBE:
4117 /* It is not possible to jump a second time.
4118 * FIXME: should we return an HTTP/500 here so that
4119 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004120 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004121 if (t->be != t->fe)
4122 break;
4123
4124 /* Swithing Proxy */
4125 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004126
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004127 /* right now, the backend switch is not too much complicated
4128 * because we have associated req_cap and rsp_cap to the
4129 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004130 */
4131
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004132 t->rep->rto = t->req->wto = t->be->timeout.server;
4133 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004134 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004135 done = 1;
4136 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004137
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004138 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004139 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004140 done = 1;
4141 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004142
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004143 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004144 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004145 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004146 done = 1;
4147 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004149 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004150 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004151 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004152 done = 1;
4153 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004154
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004155 case ACT_REPLACE:
4156 *cur_end = term; /* restore the string terminator */
4157 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4158 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4159 /* FIXME: if the user adds a newline in the replacement, the
4160 * index will not be recalculated for now, and the new line
4161 * will not be counted as a new header.
4162 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004163
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004164 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004165 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004166
Willy Tarreau9cdde232007-05-02 20:58:19 +02004167 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004168 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004169 HTTP_MSG_RQMETH,
4170 cur_ptr, cur_end + 1,
4171 NULL, NULL);
4172 if (unlikely(!cur_end))
4173 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004175 /* we have a full request and we know that we have either a CR
4176 * or an LF at <ptr>.
4177 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004178 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4179 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004180 /* there is no point trying this regex on headers */
4181 return 1;
4182 }
4183 }
4184 *cur_end = term; /* restore the string terminator */
4185 return done;
4186}
Willy Tarreau97de6242006-12-27 17:18:38 +01004187
Willy Tarreau58f10d72006-12-04 02:26:12 +01004188
Willy Tarreau58f10d72006-12-04 02:26:12 +01004189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004190/*
4191 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4192 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004193 * unparsable request. Since it can manage the switch to another backend, it
4194 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004195 */
4196int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4197{
Willy Tarreau3d300592007-03-18 18:34:41 +01004198 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004199 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004200 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004201 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004202
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004203 /*
4204 * The interleaving of transformations and verdicts
4205 * makes it difficult to decide to continue or stop
4206 * the evaluation.
4207 */
4208
Willy Tarreau3d300592007-03-18 18:34:41 +01004209 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004210 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4211 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4212 exp = exp->next;
4213 continue;
4214 }
4215
4216 /* Apply the filter to the request line. */
4217 ret = apply_filter_to_req_line(t, req, exp);
4218 if (unlikely(ret < 0))
4219 return -1;
4220
4221 if (likely(ret == 0)) {
4222 /* The filter did not match the request, it can be
4223 * iterated through all headers.
4224 */
4225 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004226 }
4227 exp = exp->next;
4228 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004229 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004230}
4231
4232
Willy Tarreaua15645d2007-03-18 16:22:39 +01004233
Willy Tarreau58f10d72006-12-04 02:26:12 +01004234/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004235 * Manage client-side cookie. It can impact performance by about 2% so it is
4236 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004237 */
4238void manage_client_side_cookies(struct session *t, struct buffer *req)
4239{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004240 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004241 char *p1, *p2, *p3, *p4;
4242 char *del_colon, *del_cookie, *colon;
4243 int app_cookies;
4244
4245 appsess *asession_temp = NULL;
4246 appsess local_asession;
4247
4248 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004249 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004250
Willy Tarreau2a324282006-12-05 00:05:46 +01004251 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004252 * we start with the start line.
4253 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004254 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004255 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004256
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004257 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004258 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004259 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004260
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004261 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004262 cur_ptr = cur_next;
4263 cur_end = cur_ptr + cur_hdr->len;
4264 cur_next = cur_end + cur_hdr->cr + 1;
4265
4266 /* We have one full header between cur_ptr and cur_end, and the
4267 * next header starts at cur_next. We're only interested in
4268 * "Cookie:" headers.
4269 */
4270
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004271 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4272 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004273 old_idx = cur_idx;
4274 continue;
4275 }
4276
4277 /* Now look for cookies. Conforming to RFC2109, we have to support
4278 * attributes whose name begin with a '$', and associate them with
4279 * the right cookie, if we want to delete this cookie.
4280 * So there are 3 cases for each cookie read :
4281 * 1) it's a special attribute, beginning with a '$' : ignore it.
4282 * 2) it's a server id cookie that we *MAY* want to delete : save
4283 * some pointers on it (last semi-colon, beginning of cookie...)
4284 * 3) it's an application cookie : we *MAY* have to delete a previous
4285 * "special" cookie.
4286 * At the end of loop, if a "special" cookie remains, we may have to
4287 * remove it. If no application cookie persists in the header, we
4288 * *MUST* delete it
4289 */
4290
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004291 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004292
Willy Tarreau58f10d72006-12-04 02:26:12 +01004293 /* del_cookie == NULL => nothing to be deleted */
4294 del_colon = del_cookie = NULL;
4295 app_cookies = 0;
4296
4297 while (p1 < cur_end) {
4298 /* skip spaces and colons, but keep an eye on these ones */
4299 while (p1 < cur_end) {
4300 if (*p1 == ';' || *p1 == ',')
4301 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004302 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004303 break;
4304 p1++;
4305 }
4306
4307 if (p1 == cur_end)
4308 break;
4309
4310 /* p1 is at the beginning of the cookie name */
4311 p2 = p1;
4312 while (p2 < cur_end && *p2 != '=')
4313 p2++;
4314
4315 if (p2 == cur_end)
4316 break;
4317
4318 p3 = p2 + 1; /* skips the '=' sign */
4319 if (p3 == cur_end)
4320 break;
4321
4322 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004323 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004324 p4++;
4325
4326 /* here, we have the cookie name between p1 and p2,
4327 * and its value between p3 and p4.
4328 * we can process it :
4329 *
4330 * Cookie: NAME=VALUE;
4331 * | || || |
4332 * | || || +--> p4
4333 * | || |+-------> p3
4334 * | || +--------> p2
4335 * | |+------------> p1
4336 * | +-------------> colon
4337 * +--------------------> cur_ptr
4338 */
4339
4340 if (*p1 == '$') {
4341 /* skip this one */
4342 }
4343 else {
4344 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004345 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004346 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004347 (p4 - p1 >= t->fe->capture_namelen) &&
4348 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004349 int log_len = p4 - p1;
4350
Willy Tarreau086b3b42007-05-13 21:45:51 +02004351 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004352 Alert("HTTP logging : out of memory.\n");
4353 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004354 if (log_len > t->fe->capture_len)
4355 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004356 memcpy(txn->cli_cookie, p1, log_len);
4357 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004358 }
4359 }
4360
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004361 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4362 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004363 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004364 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004365 char *delim;
4366
4367 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4368 * have the server ID betweek p3 and delim, and the original cookie between
4369 * delim+1 and p4. Otherwise, delim==p4 :
4370 *
4371 * Cookie: NAME=SRV~VALUE;
4372 * | || || | |
4373 * | || || | +--> p4
4374 * | || || +--------> delim
4375 * | || |+-----------> p3
4376 * | || +------------> p2
4377 * | |+----------------> p1
4378 * | +-----------------> colon
4379 * +------------------------> cur_ptr
4380 */
4381
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004382 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004383 for (delim = p3; delim < p4; delim++)
4384 if (*delim == COOKIE_DELIM)
4385 break;
4386 }
4387 else
4388 delim = p4;
4389
4390
4391 /* Here, we'll look for the first running server which supports the cookie.
4392 * This allows to share a same cookie between several servers, for example
4393 * to dedicate backup servers to specific servers only.
4394 * However, to prevent clients from sticking to cookie-less backup server
4395 * when they have incidentely learned an empty cookie, we simply ignore
4396 * empty cookies and mark them as invalid.
4397 */
4398 if (delim == p3)
4399 srv = NULL;
4400
4401 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004402 if (srv->cookie && (srv->cklen == delim - p3) &&
4403 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004404 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004405 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004406 txn->flags &= ~TX_CK_MASK;
4407 txn->flags |= TX_CK_VALID;
4408 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004409 t->srv = srv;
4410 break;
4411 } else {
4412 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004413 txn->flags &= ~TX_CK_MASK;
4414 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004415 }
4416 }
4417 srv = srv->next;
4418 }
4419
Willy Tarreau3d300592007-03-18 18:34:41 +01004420 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004421 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004422 txn->flags &= ~TX_CK_MASK;
4423 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004424 }
4425
4426 /* depending on the cookie mode, we may have to either :
4427 * - delete the complete cookie if we're in insert+indirect mode, so that
4428 * the server never sees it ;
4429 * - remove the server id from the cookie value, and tag the cookie as an
4430 * application cookie so that it does not get accidentely removed later,
4431 * if we're in cookie prefix mode
4432 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004433 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004434 int delta; /* negative */
4435
4436 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4437 p4 += delta;
4438 cur_end += delta;
4439 cur_next += delta;
4440 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004441 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004442
4443 del_cookie = del_colon = NULL;
4444 app_cookies++; /* protect the header from deletion */
4445 }
4446 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004447 (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 +01004448 del_cookie = p1;
4449 del_colon = colon;
4450 }
4451 } else {
4452 /* now we know that we must keep this cookie since it's
4453 * not ours. But if we wanted to delete our cookie
4454 * earlier, we cannot remove the complete header, but we
4455 * can remove the previous block itself.
4456 */
4457 app_cookies++;
4458
4459 if (del_cookie != NULL) {
4460 int delta; /* negative */
4461
4462 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4463 p4 += delta;
4464 cur_end += delta;
4465 cur_next += delta;
4466 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004467 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004468 del_cookie = del_colon = NULL;
4469 }
4470 }
4471
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004472 if ((t->be->appsession_name != NULL) &&
4473 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004474 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004475
Willy Tarreau58f10d72006-12-04 02:26:12 +01004476 /* Cool... it's the right one */
4477
4478 asession_temp = &local_asession;
4479
Willy Tarreau63963c62007-05-13 21:29:55 +02004480 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004481 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4482 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4483 return;
4484 }
4485
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004486 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4487 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004488 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004489
Willy Tarreau58f10d72006-12-04 02:26:12 +01004490 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004491 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4492 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004493 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004494 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004495 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004496 Alert("Not enough memory process_cli():asession:calloc().\n");
4497 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4498 return;
4499 }
4500
4501 asession_temp->sessid = local_asession.sessid;
4502 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004503 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004504 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004505 } else {
4506 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004507 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004508 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004509 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004510 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004511 Alert("Found Application Session without matching server.\n");
4512 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004513 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004514 while (srv) {
4515 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004516 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004517 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004518 txn->flags &= ~TX_CK_MASK;
4519 txn->flags |= TX_CK_VALID;
4520 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004521 t->srv = srv;
4522 break;
4523 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004524 txn->flags &= ~TX_CK_MASK;
4525 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004526 }
4527 }
4528 srv = srv->next;
4529 }/* end while(srv) */
4530 }/* end else if server == NULL */
4531
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004532 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004533 asession_temp->request_count++;
4534#if defined(DEBUG_HASH)
4535 Alert("manage_client_side_cookies\n");
4536 appsession_hash_dump(&(t->be->htbl_proxy));
4537#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004538 }/* end if ((t->proxy->appsession_name != NULL) ... */
4539 }
4540
4541 /* we'll have to look for another cookie ... */
4542 p1 = p4;
4543 } /* while (p1 < cur_end) */
4544
4545 /* There's no more cookie on this line.
4546 * We may have marked the last one(s) for deletion.
4547 * We must do this now in two ways :
4548 * - if there is no app cookie, we simply delete the header ;
4549 * - if there are app cookies, we must delete the end of the
4550 * string properly, including the colon/semi-colon before
4551 * the cookie name.
4552 */
4553 if (del_cookie != NULL) {
4554 int delta;
4555 if (app_cookies) {
4556 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4557 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004558 cur_hdr->len += delta;
4559 } else {
4560 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004561
4562 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004563 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4564 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004565 cur_hdr->len = 0;
4566 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004567 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004568 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004569 }
4570
4571 /* keep the link from this header to next one */
4572 old_idx = cur_idx;
4573 } /* end of cookie processing on this header */
4574}
4575
4576
Willy Tarreaua15645d2007-03-18 16:22:39 +01004577/* Iterate the same filter through all response headers contained in <rtr>.
4578 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4579 */
4580int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4581{
4582 char term;
4583 char *cur_ptr, *cur_end, *cur_next;
4584 int cur_idx, old_idx, last_hdr;
4585 struct http_txn *txn = &t->txn;
4586 struct hdr_idx_elem *cur_hdr;
4587 int len, delta;
4588
4589 last_hdr = 0;
4590
4591 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4592 old_idx = 0;
4593
4594 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004595 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004596 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004597 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004598 (exp->action == ACT_ALLOW ||
4599 exp->action == ACT_DENY))
4600 return 0;
4601
4602 cur_idx = txn->hdr_idx.v[old_idx].next;
4603 if (!cur_idx)
4604 break;
4605
4606 cur_hdr = &txn->hdr_idx.v[cur_idx];
4607 cur_ptr = cur_next;
4608 cur_end = cur_ptr + cur_hdr->len;
4609 cur_next = cur_end + cur_hdr->cr + 1;
4610
4611 /* Now we have one header between cur_ptr and cur_end,
4612 * and the next header starts at cur_next.
4613 */
4614
4615 /* The annoying part is that pattern matching needs
4616 * that we modify the contents to null-terminate all
4617 * strings before testing them.
4618 */
4619
4620 term = *cur_end;
4621 *cur_end = '\0';
4622
4623 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4624 switch (exp->action) {
4625 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004626 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004627 last_hdr = 1;
4628 break;
4629
4630 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004631 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004632 last_hdr = 1;
4633 break;
4634
4635 case ACT_REPLACE:
4636 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4637 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4638 /* FIXME: if the user adds a newline in the replacement, the
4639 * index will not be recalculated for now, and the new line
4640 * will not be counted as a new header.
4641 */
4642
4643 cur_end += delta;
4644 cur_next += delta;
4645 cur_hdr->len += delta;
4646 txn->rsp.eoh += delta;
4647 break;
4648
4649 case ACT_REMOVE:
4650 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4651 cur_next += delta;
4652
4653 /* FIXME: this should be a separate function */
4654 txn->rsp.eoh += delta;
4655 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4656 txn->hdr_idx.used--;
4657 cur_hdr->len = 0;
4658 cur_end = NULL; /* null-term has been rewritten */
4659 break;
4660
4661 }
4662 }
4663 if (cur_end)
4664 *cur_end = term; /* restore the string terminator */
4665
4666 /* keep the link from this header to next one in case of later
4667 * removal of next header.
4668 */
4669 old_idx = cur_idx;
4670 }
4671 return 0;
4672}
4673
4674
4675/* Apply the filter to the status line in the response buffer <rtr>.
4676 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4677 * or -1 if a replacement resulted in an invalid status line.
4678 */
4679int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4680{
4681 char term;
4682 char *cur_ptr, *cur_end;
4683 int done;
4684 struct http_txn *txn = &t->txn;
4685 int len, delta;
4686
4687
Willy Tarreau3d300592007-03-18 18:34:41 +01004688 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004689 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004690 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004691 (exp->action == ACT_ALLOW ||
4692 exp->action == ACT_DENY))
4693 return 0;
4694 else if (exp->action == ACT_REMOVE)
4695 return 0;
4696
4697 done = 0;
4698
Willy Tarreau9cdde232007-05-02 20:58:19 +02004699 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004700 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4701
4702 /* Now we have the status line between cur_ptr and cur_end */
4703
4704 /* The annoying part is that pattern matching needs
4705 * that we modify the contents to null-terminate all
4706 * strings before testing them.
4707 */
4708
4709 term = *cur_end;
4710 *cur_end = '\0';
4711
4712 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4713 switch (exp->action) {
4714 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004715 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004716 done = 1;
4717 break;
4718
4719 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004720 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004721 done = 1;
4722 break;
4723
4724 case ACT_REPLACE:
4725 *cur_end = term; /* restore the string terminator */
4726 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4727 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4728 /* FIXME: if the user adds a newline in the replacement, the
4729 * index will not be recalculated for now, and the new line
4730 * will not be counted as a new header.
4731 */
4732
4733 txn->rsp.eoh += delta;
4734 cur_end += delta;
4735
Willy Tarreau9cdde232007-05-02 20:58:19 +02004736 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004737 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004738 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004739 cur_ptr, cur_end + 1,
4740 NULL, NULL);
4741 if (unlikely(!cur_end))
4742 return -1;
4743
4744 /* we have a full respnse and we know that we have either a CR
4745 * or an LF at <ptr>.
4746 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004747 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004748 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4749 /* there is no point trying this regex on headers */
4750 return 1;
4751 }
4752 }
4753 *cur_end = term; /* restore the string terminator */
4754 return done;
4755}
4756
4757
4758
4759/*
4760 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4761 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4762 * unparsable response.
4763 */
4764int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4765{
Willy Tarreau3d300592007-03-18 18:34:41 +01004766 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004767 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004768 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004769 int ret;
4770
4771 /*
4772 * The interleaving of transformations and verdicts
4773 * makes it difficult to decide to continue or stop
4774 * the evaluation.
4775 */
4776
Willy Tarreau3d300592007-03-18 18:34:41 +01004777 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004778 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4779 exp->action == ACT_PASS)) {
4780 exp = exp->next;
4781 continue;
4782 }
4783
4784 /* Apply the filter to the status line. */
4785 ret = apply_filter_to_sts_line(t, rtr, exp);
4786 if (unlikely(ret < 0))
4787 return -1;
4788
4789 if (likely(ret == 0)) {
4790 /* The filter did not match the response, it can be
4791 * iterated through all headers.
4792 */
4793 apply_filter_to_resp_headers(t, rtr, exp);
4794 }
4795 exp = exp->next;
4796 }
4797 return 0;
4798}
4799
4800
4801
4802/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004803 * Manage server-side cookies. It can impact performance by about 2% so it is
4804 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004805 */
4806void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4807{
4808 struct http_txn *txn = &t->txn;
4809 char *p1, *p2, *p3, *p4;
4810
4811 appsess *asession_temp = NULL;
4812 appsess local_asession;
4813
4814 char *cur_ptr, *cur_end, *cur_next;
4815 int cur_idx, old_idx, delta;
4816
Willy Tarreaua15645d2007-03-18 16:22:39 +01004817 /* Iterate through the headers.
4818 * we start with the start line.
4819 */
4820 old_idx = 0;
4821 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4822
4823 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4824 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004825 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004826
4827 cur_hdr = &txn->hdr_idx.v[cur_idx];
4828 cur_ptr = cur_next;
4829 cur_end = cur_ptr + cur_hdr->len;
4830 cur_next = cur_end + cur_hdr->cr + 1;
4831
4832 /* We have one full header between cur_ptr and cur_end, and the
4833 * next header starts at cur_next. We're only interested in
4834 * "Cookie:" headers.
4835 */
4836
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004837 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4838 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004839 old_idx = cur_idx;
4840 continue;
4841 }
4842
4843 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004844 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004845
4846
4847 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004848 if (t->be->cookie_name == NULL &&
4849 t->be->appsession_name == NULL &&
4850 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004851 return;
4852
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004853 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004854
4855 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004856 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4857 break;
4858
4859 /* p1 is at the beginning of the cookie name */
4860 p2 = p1;
4861
4862 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4863 p2++;
4864
4865 if (p2 == cur_end || *p2 == ';') /* next cookie */
4866 break;
4867
4868 p3 = p2 + 1; /* skip the '=' sign */
4869 if (p3 == cur_end)
4870 break;
4871
4872 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004873 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004874 p4++;
4875
4876 /* here, we have the cookie name between p1 and p2,
4877 * and its value between p3 and p4.
4878 * we can process it.
4879 */
4880
4881 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004882 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004883 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004884 (p4 - p1 >= t->be->capture_namelen) &&
4885 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004886 int log_len = p4 - p1;
4887
Willy Tarreau086b3b42007-05-13 21:45:51 +02004888 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004889 Alert("HTTP logging : out of memory.\n");
4890 }
4891
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004892 if (log_len > t->be->capture_len)
4893 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004894 memcpy(txn->srv_cookie, p1, log_len);
4895 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004896 }
4897
4898 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004899 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4900 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004901 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004902 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004903
4904 /* If the cookie is in insert mode on a known server, we'll delete
4905 * this occurrence because we'll insert another one later.
4906 * We'll delete it too if the "indirect" option is set and we're in
4907 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004908 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4909 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004910 /* this header must be deleted */
4911 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4912 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4913 txn->hdr_idx.used--;
4914 cur_hdr->len = 0;
4915 cur_next += delta;
4916 txn->rsp.eoh += delta;
4917
Willy Tarreau3d300592007-03-18 18:34:41 +01004918 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004919 }
4920 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004921 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004922 /* replace bytes p3->p4 with the cookie name associated
4923 * with this server since we know it.
4924 */
4925 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4926 cur_hdr->len += delta;
4927 cur_next += delta;
4928 txn->rsp.eoh += delta;
4929
Willy Tarreau3d300592007-03-18 18:34:41 +01004930 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004931 }
4932 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004933 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004934 /* insert the cookie name associated with this server
4935 * before existing cookie, and insert a delimitor between them..
4936 */
4937 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4938 cur_hdr->len += delta;
4939 cur_next += delta;
4940 txn->rsp.eoh += delta;
4941
4942 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004943 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004944 }
4945 }
4946 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004947 else if ((t->be->appsession_name != NULL) &&
4948 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004949
4950 /* Cool... it's the right one */
4951
4952 size_t server_id_len = strlen(t->srv->id) + 1;
4953 asession_temp = &local_asession;
4954
Willy Tarreau63963c62007-05-13 21:29:55 +02004955 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004956 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4957 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4958 return;
4959 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004960 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4961 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004962 asession_temp->serverid = NULL;
4963
4964 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004965 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4966 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004967 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004968 Alert("Not enough Memory process_srv():asession:calloc().\n");
4969 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4970 return;
4971 }
4972 asession_temp->sessid = local_asession.sessid;
4973 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004974 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004975 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4976 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004977 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004978 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004979 }
4980
Willy Tarreaua15645d2007-03-18 16:22:39 +01004981 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004982 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004983 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4984 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4985 return;
4986 }
4987 asession_temp->serverid[0] = '\0';
4988 }
4989
4990 if (asession_temp->serverid[0] == '\0')
4991 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4992
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004993 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004994 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004995#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004996 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004997 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004998#endif
4999 }/* end if ((t->proxy->appsession_name != NULL) ... */
5000 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5001 } /* we're now at the end of the cookie value */
5002
5003 /* keep the link from this header to next one */
5004 old_idx = cur_idx;
5005 } /* end of cookie processing on this header */
5006}
5007
5008
5009
5010/*
5011 * Check if response is cacheable or not. Updates t->flags.
5012 */
5013void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5014{
5015 struct http_txn *txn = &t->txn;
5016 char *p1, *p2;
5017
5018 char *cur_ptr, *cur_end, *cur_next;
5019 int cur_idx;
5020
Willy Tarreau5df51872007-11-25 16:20:08 +01005021 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005022 return;
5023
5024 /* Iterate through the headers.
5025 * we start with the start line.
5026 */
5027 cur_idx = 0;
5028 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5029
5030 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5031 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005032 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005033
5034 cur_hdr = &txn->hdr_idx.v[cur_idx];
5035 cur_ptr = cur_next;
5036 cur_end = cur_ptr + cur_hdr->len;
5037 cur_next = cur_end + cur_hdr->cr + 1;
5038
5039 /* We have one full header between cur_ptr and cur_end, and the
5040 * next header starts at cur_next. We're only interested in
5041 * "Cookie:" headers.
5042 */
5043
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005044 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5045 if (val) {
5046 if ((cur_end - (cur_ptr + val) >= 8) &&
5047 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5048 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5049 return;
5050 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005051 }
5052
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005053 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5054 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005055 continue;
5056
5057 /* OK, right now we know we have a cache-control header at cur_ptr */
5058
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005059 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005060
5061 if (p1 >= cur_end) /* no more info */
5062 continue;
5063
5064 /* p1 is at the beginning of the value */
5065 p2 = p1;
5066
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005067 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005068 p2++;
5069
5070 /* we have a complete value between p1 and p2 */
5071 if (p2 < cur_end && *p2 == '=') {
5072 /* we have something of the form no-cache="set-cookie" */
5073 if ((cur_end - p1 >= 21) &&
5074 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5075 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005076 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005077 continue;
5078 }
5079
5080 /* OK, so we know that either p2 points to the end of string or to a comma */
5081 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5082 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5083 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5084 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005085 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005086 return;
5087 }
5088
5089 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005090 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005091 continue;
5092 }
5093 }
5094}
5095
5096
Willy Tarreau58f10d72006-12-04 02:26:12 +01005097/*
5098 * Try to retrieve a known appsession in the URI, then the associated server.
5099 * If the server is found, it's assigned to the session.
5100 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005101void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005102{
Willy Tarreau3d300592007-03-18 18:34:41 +01005103 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005104 appsess *asession_temp = NULL;
5105 appsess local_asession;
5106 char *request_line;
5107
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005108 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005109 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005110 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005111 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005112 return;
5113
5114 /* skip ';' */
5115 request_line++;
5116
5117 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005118 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005119 return;
5120
5121 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005122 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005123
5124 /* First try if we already have an appsession */
5125 asession_temp = &local_asession;
5126
Willy Tarreau63963c62007-05-13 21:29:55 +02005127 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005128 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5129 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5130 return;
5131 }
5132
5133 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005134 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5135 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005136 asession_temp->serverid = NULL;
5137
5138 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005139 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5140 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005141 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005142 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005143 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005144 Alert("Not enough memory process_cli():asession:calloc().\n");
5145 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5146 return;
5147 }
5148 asession_temp->sessid = local_asession.sessid;
5149 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005150 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005151 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005152 }
5153 else {
5154 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005155 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005156 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005157
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005158 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005159 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005160
Willy Tarreau58f10d72006-12-04 02:26:12 +01005161#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005162 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005163 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005164#endif
5165 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005166 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005167 Alert("Found Application Session without matching server.\n");
5168 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005169 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005170 while (srv) {
5171 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005172 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005173 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005174 txn->flags &= ~TX_CK_MASK;
5175 txn->flags |= TX_CK_VALID;
5176 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005177 t->srv = srv;
5178 break;
5179 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005180 txn->flags &= ~TX_CK_MASK;
5181 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005182 }
5183 }
5184 srv = srv->next;
5185 }
5186 }
5187}
5188
5189
Willy Tarreaub2513902006-12-17 14:52:38 +01005190/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005191 * In a GET or HEAD request, check if the requested URI matches the stats uri
5192 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005193 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005194 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005195 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005196 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005197 *
5198 * Returns 1 if the session's state changes, otherwise 0.
5199 */
5200int stats_check_uri_auth(struct session *t, struct proxy *backend)
5201{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005202 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005203 struct uri_auth *uri_auth = backend->uri_auth;
5204 struct user_auth *user;
5205 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005206 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005207
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005208 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5209
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005210 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005211 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005212 return 0;
5213
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005214 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005215
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005216 /* the URI is in h */
5217 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005218 return 0;
5219
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005220 h += uri_auth->uri_len;
5221 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5222 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005223 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005224 break;
5225 }
5226 h++;
5227 }
5228
5229 if (uri_auth->refresh) {
5230 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5231 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5232 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005233 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005234 break;
5235 }
5236 h++;
5237 }
5238 }
5239
Willy Tarreau55bb8452007-10-17 18:44:57 +02005240 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5241 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5242 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005243 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005244 break;
5245 }
5246 h++;
5247 }
5248
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005249 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5250
Willy Tarreaub2513902006-12-17 14:52:38 +01005251 /* we are in front of a interceptable URI. Let's check
5252 * if there's an authentication and if it's valid.
5253 */
5254 user = uri_auth->users;
5255 if (!user) {
5256 /* no user auth required, it's OK */
5257 authenticated = 1;
5258 } else {
5259 authenticated = 0;
5260
5261 /* a user list is defined, we have to check.
5262 * skip 21 chars for "Authorization: Basic ".
5263 */
5264
5265 /* FIXME: this should move to an earlier place */
5266 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005267 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5268 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5269 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005270 if (len > 14 &&
5271 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005272 txn->auth_hdr.str = h;
5273 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005274 break;
5275 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005276 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005277 }
5278
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005279 if (txn->auth_hdr.len < 21 ||
5280 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005281 user = NULL;
5282
5283 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005284 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5285 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005286 user->user_pwd, user->user_len)) {
5287 authenticated = 1;
5288 break;
5289 }
5290 user = user->next;
5291 }
5292 }
5293
5294 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005295 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005296
5297 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005298 msg.str = trash;
5299 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005300 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005301 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005302 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005303 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005304 if (!(t->flags & SN_ERR_MASK))
5305 t->flags |= SN_ERR_PRXCOND;
5306 if (!(t->flags & SN_FINST_MASK))
5307 t->flags |= SN_FINST_R;
5308 return 1;
5309 }
5310
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005311 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005312 * data.
5313 */
Willy Tarreau72b179a2008-08-28 16:01:32 +02005314 buffer_shutw_now(t->req);
5315 buffer_shutr_now(t->rep);
5316 buffer_start_hijack(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02005317 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005318 t->data_source = DATA_SRC_STATS;
5319 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005320 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005321 produce_content(t);
5322 return 1;
5323}
5324
5325
Willy Tarreaubaaee002006-06-26 02:48:02 +02005326/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005327 * Print a debug line with a header
5328 */
5329void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5330{
5331 int len, max;
5332 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005333 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005334 max = end - start;
5335 UBOUND(max, sizeof(trash) - len - 1);
5336 len += strlcpy2(trash + len, start, max + 1);
5337 trash[len++] = '\n';
5338 write(1, trash, len);
5339}
5340
5341
Willy Tarreau8797c062007-05-07 00:55:35 +02005342/************************************************************************/
5343/* The code below is dedicated to ACL parsing and matching */
5344/************************************************************************/
5345
5346
5347
5348
5349/* 1. Check on METHOD
5350 * We use the pre-parsed method if it is known, and store its number as an
5351 * integer. If it is unknown, we use the pointer and the length.
5352 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005353static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005354{
5355 int len, meth;
5356
Willy Tarreauae8b7962007-06-09 23:10:04 +02005357 len = strlen(*text);
5358 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005359
5360 pattern->val.i = meth;
5361 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005362 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005363 if (!pattern->ptr.str)
5364 return 0;
5365 pattern->len = len;
5366 }
5367 return 1;
5368}
5369
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005370static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005371acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5372 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005373{
5374 int meth;
5375 struct http_txn *txn = l7;
5376
Willy Tarreaub6866442008-07-14 23:54:42 +02005377 if (!txn)
5378 return 0;
5379
Willy Tarreauc11416f2007-06-17 16:58:38 +02005380 if (txn->req.msg_state != HTTP_MSG_BODY)
5381 return 0;
5382
Willy Tarreau8797c062007-05-07 00:55:35 +02005383 meth = txn->meth;
5384 test->i = meth;
5385 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005386 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5387 /* ensure the indexes are not affected */
5388 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005389 test->len = txn->req.sl.rq.m_l;
5390 test->ptr = txn->req.sol;
5391 }
5392 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5393 return 1;
5394}
5395
5396static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5397{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005398 int icase;
5399
Willy Tarreau8797c062007-05-07 00:55:35 +02005400 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005401 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005402
5403 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005404 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005405
5406 /* Other method, we must compare the strings */
5407 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005408 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005409
5410 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5411 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5412 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005413 return ACL_PAT_FAIL;
5414 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005415}
5416
5417/* 2. Check on Request/Status Version
5418 * We simply compare strings here.
5419 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005420static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005421{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005422 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005423 if (!pattern->ptr.str)
5424 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005425 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005426 return 1;
5427}
5428
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005429static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005430acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5431 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005432{
5433 struct http_txn *txn = l7;
5434 char *ptr;
5435 int len;
5436
Willy Tarreaub6866442008-07-14 23:54:42 +02005437 if (!txn)
5438 return 0;
5439
Willy Tarreauc11416f2007-06-17 16:58:38 +02005440 if (txn->req.msg_state != HTTP_MSG_BODY)
5441 return 0;
5442
Willy Tarreau8797c062007-05-07 00:55:35 +02005443 len = txn->req.sl.rq.v_l;
5444 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5445
5446 while ((len-- > 0) && (*ptr++ != '/'));
5447 if (len <= 0)
5448 return 0;
5449
5450 test->ptr = ptr;
5451 test->len = len;
5452
5453 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5454 return 1;
5455}
5456
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005457static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005458acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5459 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005460{
5461 struct http_txn *txn = l7;
5462 char *ptr;
5463 int len;
5464
Willy Tarreaub6866442008-07-14 23:54:42 +02005465 if (!txn)
5466 return 0;
5467
Willy Tarreauc11416f2007-06-17 16:58:38 +02005468 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5469 return 0;
5470
Willy Tarreau8797c062007-05-07 00:55:35 +02005471 len = txn->rsp.sl.st.v_l;
5472 ptr = txn->rsp.sol;
5473
5474 while ((len-- > 0) && (*ptr++ != '/'));
5475 if (len <= 0)
5476 return 0;
5477
5478 test->ptr = ptr;
5479 test->len = len;
5480
5481 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5482 return 1;
5483}
5484
5485/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005486static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005487acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5488 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005489{
5490 struct http_txn *txn = l7;
5491 char *ptr;
5492 int len;
5493
Willy Tarreaub6866442008-07-14 23:54:42 +02005494 if (!txn)
5495 return 0;
5496
Willy Tarreauc11416f2007-06-17 16:58:38 +02005497 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5498 return 0;
5499
Willy Tarreau8797c062007-05-07 00:55:35 +02005500 len = txn->rsp.sl.st.c_l;
5501 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5502
5503 test->i = __strl2ui(ptr, len);
5504 test->flags = ACL_TEST_F_VOL_1ST;
5505 return 1;
5506}
5507
5508/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005509static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005510acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5511 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005512{
5513 struct http_txn *txn = l7;
5514
Willy Tarreaub6866442008-07-14 23:54:42 +02005515 if (!txn)
5516 return 0;
5517
Willy Tarreauc11416f2007-06-17 16:58:38 +02005518 if (txn->req.msg_state != HTTP_MSG_BODY)
5519 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005520
Willy Tarreauc11416f2007-06-17 16:58:38 +02005521 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5522 /* ensure the indexes are not affected */
5523 return 0;
5524
Willy Tarreau8797c062007-05-07 00:55:35 +02005525 test->len = txn->req.sl.rq.u_l;
5526 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5527
Willy Tarreauf3d25982007-05-08 22:45:09 +02005528 /* we do not need to set READ_ONLY because the data is in a buffer */
5529 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005530 return 1;
5531}
5532
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005533static int
5534acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5535 struct acl_expr *expr, struct acl_test *test)
5536{
5537 struct http_txn *txn = l7;
5538
Willy Tarreaub6866442008-07-14 23:54:42 +02005539 if (!txn)
5540 return 0;
5541
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005542 if (txn->req.msg_state != HTTP_MSG_BODY)
5543 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005544
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005545 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5546 /* ensure the indexes are not affected */
5547 return 0;
5548
5549 /* Parse HTTP request */
5550 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5551 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5552 test->i = AF_INET;
5553
5554 /*
5555 * If we are parsing url in frontend space, we prepare backend stage
5556 * to not parse again the same url ! optimization lazyness...
5557 */
5558 if (px->options & PR_O_HTTP_PROXY)
5559 l4->flags |= SN_ADDR_SET;
5560
5561 test->flags = ACL_TEST_F_READ_ONLY;
5562 return 1;
5563}
5564
5565static int
5566acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5567 struct acl_expr *expr, struct acl_test *test)
5568{
5569 struct http_txn *txn = l7;
5570
Willy Tarreaub6866442008-07-14 23:54:42 +02005571 if (!txn)
5572 return 0;
5573
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005574 if (txn->req.msg_state != HTTP_MSG_BODY)
5575 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005576
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005577 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5578 /* ensure the indexes are not affected */
5579 return 0;
5580
5581 /* Same optimization as url_ip */
5582 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5583 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5584
5585 if (px->options & PR_O_HTTP_PROXY)
5586 l4->flags |= SN_ADDR_SET;
5587
5588 test->flags = ACL_TEST_F_READ_ONLY;
5589 return 1;
5590}
5591
Willy Tarreauc11416f2007-06-17 16:58:38 +02005592/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5593 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5594 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005595static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005596acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005597 struct acl_expr *expr, struct acl_test *test)
5598{
5599 struct http_txn *txn = l7;
5600 struct hdr_idx *idx = &txn->hdr_idx;
5601 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005602
Willy Tarreaub6866442008-07-14 23:54:42 +02005603 if (!txn)
5604 return 0;
5605
Willy Tarreau33a7e692007-06-10 19:45:56 +02005606 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5607 /* search for header from the beginning */
5608 ctx->idx = 0;
5609
Willy Tarreau33a7e692007-06-10 19:45:56 +02005610 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5611 test->flags |= ACL_TEST_F_FETCH_MORE;
5612 test->flags |= ACL_TEST_F_VOL_HDR;
5613 test->len = ctx->vlen;
5614 test->ptr = (char *)ctx->line + ctx->val;
5615 return 1;
5616 }
5617
5618 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5619 test->flags |= ACL_TEST_F_VOL_HDR;
5620 return 0;
5621}
5622
Willy Tarreau33a7e692007-06-10 19:45:56 +02005623static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005624acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5625 struct acl_expr *expr, struct acl_test *test)
5626{
5627 struct http_txn *txn = l7;
5628
Willy Tarreaub6866442008-07-14 23:54:42 +02005629 if (!txn)
5630 return 0;
5631
Willy Tarreauc11416f2007-06-17 16:58:38 +02005632 if (txn->req.msg_state != HTTP_MSG_BODY)
5633 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005634
Willy Tarreauc11416f2007-06-17 16:58:38 +02005635 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5636 /* ensure the indexes are not affected */
5637 return 0;
5638
5639 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5640}
5641
5642static int
5643acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5644 struct acl_expr *expr, struct acl_test *test)
5645{
5646 struct http_txn *txn = l7;
5647
Willy Tarreaub6866442008-07-14 23:54:42 +02005648 if (!txn)
5649 return 0;
5650
Willy Tarreauc11416f2007-06-17 16:58:38 +02005651 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5652 return 0;
5653
5654 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5655}
5656
5657/* 6. Check on HTTP header count. The number of occurrences is returned.
5658 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5659 */
5660static int
5661acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005662 struct acl_expr *expr, struct acl_test *test)
5663{
5664 struct http_txn *txn = l7;
5665 struct hdr_idx *idx = &txn->hdr_idx;
5666 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005667 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005668
Willy Tarreaub6866442008-07-14 23:54:42 +02005669 if (!txn)
5670 return 0;
5671
Willy Tarreau33a7e692007-06-10 19:45:56 +02005672 ctx.idx = 0;
5673 cnt = 0;
5674 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5675 cnt++;
5676
5677 test->i = cnt;
5678 test->flags = ACL_TEST_F_VOL_HDR;
5679 return 1;
5680}
5681
Willy Tarreauc11416f2007-06-17 16:58:38 +02005682static int
5683acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5684 struct acl_expr *expr, struct acl_test *test)
5685{
5686 struct http_txn *txn = l7;
5687
Willy Tarreaub6866442008-07-14 23:54:42 +02005688 if (!txn)
5689 return 0;
5690
Willy Tarreauc11416f2007-06-17 16:58:38 +02005691 if (txn->req.msg_state != HTTP_MSG_BODY)
5692 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005693
Willy Tarreauc11416f2007-06-17 16:58:38 +02005694 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5695 /* ensure the indexes are not affected */
5696 return 0;
5697
5698 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5699}
5700
5701static int
5702acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5703 struct acl_expr *expr, struct acl_test *test)
5704{
5705 struct http_txn *txn = l7;
5706
Willy Tarreaub6866442008-07-14 23:54:42 +02005707 if (!txn)
5708 return 0;
5709
Willy Tarreauc11416f2007-06-17 16:58:38 +02005710 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5711 return 0;
5712
5713 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5714}
5715
Willy Tarreau33a7e692007-06-10 19:45:56 +02005716/* 7. Check on HTTP header's integer value. The integer value is returned.
5717 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005718 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005719 */
5720static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005721acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005722 struct acl_expr *expr, struct acl_test *test)
5723{
5724 struct http_txn *txn = l7;
5725 struct hdr_idx *idx = &txn->hdr_idx;
5726 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005727
Willy Tarreaub6866442008-07-14 23:54:42 +02005728 if (!txn)
5729 return 0;
5730
Willy Tarreau33a7e692007-06-10 19:45:56 +02005731 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5732 /* search for header from the beginning */
5733 ctx->idx = 0;
5734
Willy Tarreau33a7e692007-06-10 19:45:56 +02005735 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5736 test->flags |= ACL_TEST_F_FETCH_MORE;
5737 test->flags |= ACL_TEST_F_VOL_HDR;
5738 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5739 return 1;
5740 }
5741
5742 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5743 test->flags |= ACL_TEST_F_VOL_HDR;
5744 return 0;
5745}
5746
Willy Tarreauc11416f2007-06-17 16:58:38 +02005747static int
5748acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5749 struct acl_expr *expr, struct acl_test *test)
5750{
5751 struct http_txn *txn = l7;
5752
Willy Tarreaub6866442008-07-14 23:54:42 +02005753 if (!txn)
5754 return 0;
5755
Willy Tarreauc11416f2007-06-17 16:58:38 +02005756 if (txn->req.msg_state != HTTP_MSG_BODY)
5757 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005758
Willy Tarreauc11416f2007-06-17 16:58:38 +02005759 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5760 /* ensure the indexes are not affected */
5761 return 0;
5762
5763 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5764}
5765
5766static int
5767acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5768 struct acl_expr *expr, struct acl_test *test)
5769{
5770 struct http_txn *txn = l7;
5771
Willy Tarreaub6866442008-07-14 23:54:42 +02005772 if (!txn)
5773 return 0;
5774
Willy Tarreauc11416f2007-06-17 16:58:38 +02005775 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5776 return 0;
5777
5778 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5779}
5780
Willy Tarreau737b0c12007-06-10 21:28:46 +02005781/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5782 * the first '/' after the possible hostname, and ends before the possible '?'.
5783 */
5784static int
5785acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5786 struct acl_expr *expr, struct acl_test *test)
5787{
5788 struct http_txn *txn = l7;
5789 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005790
Willy Tarreaub6866442008-07-14 23:54:42 +02005791 if (!txn)
5792 return 0;
5793
Willy Tarreauc11416f2007-06-17 16:58:38 +02005794 if (txn->req.msg_state != HTTP_MSG_BODY)
5795 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005796
Willy Tarreauc11416f2007-06-17 16:58:38 +02005797 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5798 /* ensure the indexes are not affected */
5799 return 0;
5800
Willy Tarreau21d2af32008-02-14 20:25:24 +01005801 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5802 ptr = http_get_path(txn);
5803 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005804 return 0;
5805
5806 /* OK, we got the '/' ! */
5807 test->ptr = ptr;
5808
5809 while (ptr < end && *ptr != '?')
5810 ptr++;
5811
5812 test->len = ptr - test->ptr;
5813
5814 /* we do not need to set READ_ONLY because the data is in a buffer */
5815 test->flags = ACL_TEST_F_VOL_1ST;
5816 return 1;
5817}
5818
5819
Willy Tarreau8797c062007-05-07 00:55:35 +02005820
5821/************************************************************************/
5822/* All supported keywords must be declared here. */
5823/************************************************************************/
5824
5825/* Note: must not be declared <const> as its list will be overwritten */
5826static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005827 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5828 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5829 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5830 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005831
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005832 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5833 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5834 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5835 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5836 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5837 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5838 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5839 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5840 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005841
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005842 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5843 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5844 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5845 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5846 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5847 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5848 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5849 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5850 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5851 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005852
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005853 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5854 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5855 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5856 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5857 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5858 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5859 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5860 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5861 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005862
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005863 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5864 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5865 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5866 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5867 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5868 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5869 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005870
Willy Tarreauf3d25982007-05-08 22:45:09 +02005871 { NULL, NULL, NULL, NULL },
5872
5873#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005874 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5875 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5876 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5877 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5878 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5879 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5880 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5881
Willy Tarreau8797c062007-05-07 00:55:35 +02005882 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5883 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5884 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5885 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5886 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5887 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5888 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5889 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5890
5891 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5892 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5893 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5894 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5895 { NULL, NULL, NULL, NULL },
5896#endif
5897}};
5898
5899
5900__attribute__((constructor))
5901static void __http_protocol_init(void)
5902{
5903 acl_register_keywords(&acl_kws);
5904}
5905
5906
Willy Tarreau58f10d72006-12-04 02:26:12 +01005907/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005908 * Local variables:
5909 * c-indent-level: 8
5910 * c-basic-offset: 8
5911 * End:
5912 */