blob: 9d7041fee627683187521514955fde4c10642d75 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaua15645d2007-03-18 16:22:39 +01004 * Copyright 2000-2007 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>
33#include <common/time.h>
34#include <common/uri_auth.h>
35#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau8797c062007-05-07 00:55:35 +020037#include <types/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <types/capture.h>
39#include <types/client.h>
40#include <types/global.h>
41#include <types/httperr.h>
42#include <types/polling.h>
43#include <types/proxy.h>
44#include <types/server.h>
45
Willy Tarreau8797c062007-05-07 00:55:35 +020046#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/backend.h>
48#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
51#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010052#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/proto_http.h>
54#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020055#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/session.h>
57#include <proto/task.h>
58
Willy Tarreau6d1a9882007-01-07 02:03:04 +010059#ifdef CONFIG_HAP_TCPSPLICE
60#include <libtcpsplice.h>
61#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Willy Tarreau58f10d72006-12-04 02:26:12 +010063#define DEBUG_PARSE_NO_SPEEDUP
64#undef DEBUG_PARSE_NO_SPEEDUP
65
Willy Tarreau976f1ee2006-12-17 10:06:03 +010066/* This is used to perform a quick jump as an alternative to a break/continue
67 * instruction. The first argument is the label for normal operation, and the
68 * second one is the break/continue instruction in the no_speedup mode.
69 */
70
71#ifdef DEBUG_PARSE_NO_SPEEDUP
72#define QUICK_JUMP(x,y) y
73#else
74#define QUICK_JUMP(x,y) goto x
75#endif
76
Willy Tarreau1c47f852006-07-09 08:22:27 +020077/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010078const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020079 "HTTP/1.0 200 OK\r\n"
80 "Cache-Control: no-cache\r\n"
81 "Connection: close\r\n"
82 "Content-Type: text/html\r\n"
83 "\r\n"
84 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
85
Willy Tarreau0f772532006-12-23 20:51:41 +010086const struct chunk http_200_chunk = {
87 .str = (char *)&HTTP_200,
88 .len = sizeof(HTTP_200)-1
89};
90
91const char *HTTP_302 =
92 "HTTP/1.0 302 Found\r\n"
93 "Cache-Control: no-cache\r\n"
94 "Connection: close\r\n"
95 "Location: "; /* not terminated since it will be concatenated with the URL */
96
97/* same as 302 except that the browser MUST retry with the GET method */
98const char *HTTP_303 =
99 "HTTP/1.0 303 See Other\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Connection: close\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
105const char *HTTP_401_fmt =
106 "HTTP/1.0 401 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200109 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
116 [HTTP_ERR_400] = 400,
117 [HTTP_ERR_403] = 403,
118 [HTTP_ERR_408] = 408,
119 [HTTP_ERR_500] = 500,
120 [HTTP_ERR_502] = 502,
121 [HTTP_ERR_503] = 503,
122 [HTTP_ERR_504] = 504,
123};
124
Willy Tarreau80587432006-12-24 17:47:20 +0100125static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100127 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
133
134 [HTTP_ERR_403] =
135 "HTTP/1.0 403 Forbidden\r\n"
136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
141
142 [HTTP_ERR_408] =
143 "HTTP/1.0 408 Request Time-out\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
149
150 [HTTP_ERR_500] =
151 "HTTP/1.0 500 Server Error\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
157
158 [HTTP_ERR_502] =
159 "HTTP/1.0 502 Bad Gateway\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
165
166 [HTTP_ERR_503] =
167 "HTTP/1.0 503 Service Unavailable\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
173
174 [HTTP_ERR_504] =
175 "HTTP/1.0 504 Gateway Time-out\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
181
182};
183
Willy Tarreau80587432006-12-24 17:47:20 +0100184/* We must put the messages here since GCC cannot initialize consts depending
185 * on strlen().
186 */
187struct chunk http_err_chunks[HTTP_ERR_SIZE];
188
Willy Tarreau42250582007-04-01 01:30:43 +0200189#define FD_SETS_ARE_BITFIELDS
190#ifdef FD_SETS_ARE_BITFIELDS
191/*
192 * This map is used with all the FD_* macros to check whether a particular bit
193 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
194 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
195 * byte should be encoded. Be careful to always pass bytes from 0 to 255
196 * exclusively to the macros.
197 */
198fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
199fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
200
201#else
202#error "Check if your OS uses bitfields for fd_sets"
203#endif
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205void init_proto_http()
206{
Willy Tarreau42250582007-04-01 01:30:43 +0200207 int i;
208 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100209 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200210
Willy Tarreau80587432006-12-24 17:47:20 +0100211 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
212 if (!http_err_msgs[msg]) {
213 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
214 abort();
215 }
216
217 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
218 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
219 }
Willy Tarreau42250582007-04-01 01:30:43 +0200220
221 /* initialize the log header encoding map : '{|}"#' should be encoded with
222 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
223 * URL encoding only requires '"', '#' to be encoded as well as non-
224 * printable characters above.
225 */
226 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
227 memset(url_encode_map, 0, sizeof(url_encode_map));
228 for (i = 0; i < 32; i++) {
229 FD_SET(i, hdr_encode_map);
230 FD_SET(i, url_encode_map);
231 }
232 for (i = 127; i < 256; i++) {
233 FD_SET(i, hdr_encode_map);
234 FD_SET(i, url_encode_map);
235 }
236
237 tmp = "\"#{|}";
238 while (*tmp) {
239 FD_SET(*tmp, hdr_encode_map);
240 tmp++;
241 }
242
243 tmp = "\"#";
244 while (*tmp) {
245 FD_SET(*tmp, url_encode_map);
246 tmp++;
247 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200248
249 /* memory allocations */
250 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200251 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100252}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253
Willy Tarreau53b6c742006-12-17 13:37:46 +0100254/*
255 * We have 26 list of methods (1 per first letter), each of which can have
256 * up to 3 entries (2 valid, 1 null).
257 */
258struct http_method_desc {
259 http_meth_t meth;
260 int len;
261 const char text[8];
262};
263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100264const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100265 ['C' - 'A'] = {
266 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
267 },
268 ['D' - 'A'] = {
269 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
270 },
271 ['G' - 'A'] = {
272 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
273 },
274 ['H' - 'A'] = {
275 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
276 },
277 ['P' - 'A'] = {
278 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
279 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
280 },
281 ['T' - 'A'] = {
282 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
283 },
284 /* rest is empty like this :
285 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
286 */
287};
288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289/* It is about twice as fast on recent architectures to lookup a byte in a
290 * table than two perform a boolean AND or OR between two tests. Refer to
291 * RFC2616 for those chars.
292 */
293
294const char http_is_spht[256] = {
295 [' '] = 1, ['\t'] = 1,
296};
297
298const char http_is_crlf[256] = {
299 ['\r'] = 1, ['\n'] = 1,
300};
301
302const char http_is_lws[256] = {
303 [' '] = 1, ['\t'] = 1,
304 ['\r'] = 1, ['\n'] = 1,
305};
306
307const char http_is_sep[256] = {
308 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
309 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
310 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
311 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
312 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
313};
314
315const char http_is_ctl[256] = {
316 [0 ... 31] = 1,
317 [127] = 1,
318};
319
320/*
321 * A token is any ASCII char that is neither a separator nor a CTL char.
322 * Do not overwrite values in assignment since gcc-2.95 will not handle
323 * them correctly. Instead, define every non-CTL char's status.
324 */
325const char http_is_token[256] = {
326 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
327 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
328 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
329 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
330 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
331 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
332 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
333 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
334 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
335 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
336 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
337 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
338 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
339 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
340 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
341 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
342 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
343 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
344 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
345 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
346 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
347 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
348 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
349 ['|'] = 1, ['}'] = 0, ['~'] = 1,
350};
351
352
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100353/*
354 * An http ver_token is any ASCII which can be found in an HTTP version,
355 * which includes 'H', 'T', 'P', '/', '.' and any digit.
356 */
357const char http_is_ver_token[256] = {
358 ['.'] = 1, ['/'] = 1,
359 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
360 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
361 ['H'] = 1, ['P'] = 1, ['T'] = 1,
362};
363
364
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365#ifdef DEBUG_FULL
366static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
367static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
368#endif
369
Willy Tarreau42250582007-04-01 01:30:43 +0200370static void http_sess_log(struct session *s);
371
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100372/*
373 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
374 * CRLF. Text length is measured first, so it cannot be NULL.
375 * The header is also automatically added to the index <hdr_idx>, and the end
376 * of headers is automatically adjusted. The number of bytes added is returned
377 * on success, otherwise <0 is returned indicating an error.
378 */
379int http_header_add_tail(struct buffer *b, struct http_msg *msg,
380 struct hdr_idx *hdr_idx, const char *text)
381{
382 int bytes, len;
383
384 len = strlen(text);
385 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
386 if (!bytes)
387 return -1;
388 msg->eoh += bytes;
389 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
390}
391
392/*
393 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
394 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
395 * the buffer is only opened and the space reserved, but nothing is copied.
396 * The header is also automatically added to the index <hdr_idx>, and the end
397 * of headers is automatically adjusted. The number of bytes added is returned
398 * on success, otherwise <0 is returned indicating an error.
399 */
400int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
401 struct hdr_idx *hdr_idx, const char *text, int len)
402{
403 int bytes;
404
405 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
406 if (!bytes)
407 return -1;
408 msg->eoh += bytes;
409 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
410}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
412/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100413 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
414 * If so, returns the position of the first non-space character relative to
415 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
416 * to return a pointer to the place after the first space. Returns 0 if the
417 * header name does not match. Checks are case-insensitive.
418 */
419int http_header_match2(const char *hdr, const char *end,
420 const char *name, int len)
421{
422 const char *val;
423
424 if (hdr + len >= end)
425 return 0;
426 if (hdr[len] != ':')
427 return 0;
428 if (strncasecmp(hdr, name, len) != 0)
429 return 0;
430 val = hdr + len + 1;
431 while (val < end && HTTP_IS_SPHT(*val))
432 val++;
433 if ((val >= end) && (len + 2 <= end - hdr))
434 return len + 2; /* we may replace starting from second space */
435 return val - hdr;
436}
437
Willy Tarreau33a7e692007-06-10 19:45:56 +0200438/* Find the end of the header value contained between <s> and <e>.
439 * See RFC2616, par 2.2 for more information. Note that it requires
440 * a valid header to return a valid result.
441 */
442const char *find_hdr_value_end(const char *s, const char *e)
443{
444 int quoted, qdpair;
445
446 quoted = qdpair = 0;
447 for (; s < e; s++) {
448 if (qdpair) qdpair = 0;
449 else if (quoted && *s == '\\') qdpair = 1;
450 else if (quoted && *s == '"') quoted = 0;
451 else if (*s == '"') quoted = 1;
452 else if (*s == ',') return s;
453 }
454 return s;
455}
456
457/* Find the first or next occurrence of header <name> in message buffer <sol>
458 * using headers index <idx>, and return it in the <ctx> structure. This
459 * structure holds everything necessary to use the header and find next
460 * occurrence. If its <idx> member is 0, the header is searched from the
461 * beginning. Otherwise, the next occurrence is returned. The function returns
462 * 1 when it finds a value, and 0 when there is no more.
463 */
464int http_find_header2(const char *name, int len,
465 const char *sol, struct hdr_idx *idx,
466 struct hdr_ctx *ctx)
467{
468 __label__ return_hdr, next_hdr;
469 const char *eol, *sov;
470 int cur_idx;
471
472 if (ctx->idx) {
473 /* We have previously returned a value, let's search
474 * another one on the same line.
475 */
476 cur_idx = ctx->idx;
477 sol = ctx->line;
478 sov = sol + ctx->val + ctx->vlen;
479 eol = sol + idx->v[cur_idx].len;
480
481 if (sov >= eol)
482 /* no more values in this header */
483 goto next_hdr;
484
485 /* values remaining for this header, skip the comma */
486 sov++;
487 while (sov < eol && http_is_lws[(unsigned char)*sov])
488 sov++;
489
490 goto return_hdr;
491 }
492
493 /* first request for this header */
494 sol += hdr_idx_first_pos(idx);
495 cur_idx = hdr_idx_first_idx(idx);
496
497 while (cur_idx) {
498 eol = sol + idx->v[cur_idx].len;
499
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200500 if (len == 0) {
501 /* No argument was passed, we want any header.
502 * To achieve this, we simply build a fake request. */
503 while (sol + len < eol && sol[len] != ':')
504 len++;
505 name = sol;
506 }
507
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 if ((len < eol - sol) &&
509 (sol[len] == ':') &&
510 (strncasecmp(sol, name, len) == 0)) {
511
512 sov = sol + len + 1;
513 while (sov < eol && http_is_lws[(unsigned char)*sov])
514 sov++;
515 return_hdr:
516 ctx->line = sol;
517 ctx->idx = cur_idx;
518 ctx->val = sov - sol;
519
520 eol = find_hdr_value_end(sov, eol);
521 ctx->vlen = eol - sov;
522 return 1;
523 }
524 next_hdr:
525 sol = eol + idx->v[cur_idx].cr + 1;
526 cur_idx = idx->v[cur_idx].next;
527 }
528 return 0;
529}
530
531int http_find_header(const char *name,
532 const char *sol, struct hdr_idx *idx,
533 struct hdr_ctx *ctx)
534{
535 return http_find_header2(name, strlen(name), sol, idx, ctx);
536}
537
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100539 * indicators accordingly. Note that if <status> is 0, or if the message
540 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200541 */
542void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100543 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544{
545 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100546 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100547 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100548 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100549 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550 }
551 if (!(t->flags & SN_ERR_MASK))
552 t->flags |= err;
553 if (!(t->flags & SN_FINST_MASK))
554 t->flags |= finst;
555}
556
Willy Tarreau80587432006-12-24 17:47:20 +0100557/* This function returns the appropriate error location for the given session
558 * and message.
559 */
560
561struct chunk *error_message(struct session *s, int msgnum)
562{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200563 if (s->be->errmsg[msgnum].str)
564 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100565 else if (s->fe->errmsg[msgnum].str)
566 return &s->fe->errmsg[msgnum];
567 else
568 return &http_err_chunks[msgnum];
569}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570
Willy Tarreau53b6c742006-12-17 13:37:46 +0100571/*
572 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
573 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
574 */
575static http_meth_t find_http_meth(const char *str, const int len)
576{
577 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100578 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100579
580 m = ((unsigned)*str - 'A');
581
582 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 for (h = http_methods[m]; h->len > 0; h++) {
584 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100585 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100586 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100587 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100588 };
589 return HTTP_METH_OTHER;
590 }
591 return HTTP_METH_NONE;
592
593}
594
Willy Tarreaubaaee002006-06-26 02:48:02 +0200595/* Processes the client and server jobs of a session task, then
596 * puts it back to the wait queue in a clean state, or
597 * cleans up its resources if it must be deleted. Returns
598 * the time the task accepts to wait, or TIME_ETERNITY for
599 * infinity.
600 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200601void process_session(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602{
603 struct session *s = t->context;
604 int fsm_resync = 0;
605
606 do {
607 fsm_resync = 0;
608 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
609 fsm_resync |= process_cli(s);
610 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
611 fsm_resync |= process_srv(s);
612 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
613 } while (fsm_resync);
614
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200615 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100616
617 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
618 session_process_counters(s);
619
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200620 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
621 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200622
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200623 tv_min(&t->expire, &s->req->rex, &s->req->wex);
624 tv_bound(&t->expire, &s->req->cex);
625 tv_bound(&t->expire, &s->rep->rex);
626 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreau036fae02008-01-06 13:24:40 +0100627 if (s->cli_state == CL_STHEADERS)
628 tv_bound(&t->expire, &s->txn.exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629
630 /* restore t to its place in the task list */
631 task_queue(t);
632
633#ifdef DEBUG_FULL
634 /* DEBUG code : this should never ever happen, otherwise it indicates
635 * that a task still has something to do and will provoke a quick loop.
636 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200637 if (tv_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200638 exit(100);
639#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200640 *next = t->expire;
641 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 }
643
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100644 s->fe->feconn--;
645 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200646 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 actconn--;
648
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200649 if (unlikely((global.mode & MODE_DEBUG) &&
650 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100652 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200653 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100654 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 write(1, trash, len);
656 }
657
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200658 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100659 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660
661 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200662 if (s->logs.logwait &&
663 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200664 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
665 if (s->fe->to_log & LW_REQ)
666 http_sess_log(s);
667 else
668 tcp_sess_log(s);
669 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670
671 /* the task MUST not be in the run queue anymore */
672 task_delete(t);
673 session_free(s);
674 task_free(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200675 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676}
677
678
Willy Tarreau42250582007-04-01 01:30:43 +0200679extern const char sess_term_cond[8];
680extern const char sess_fin_state[8];
681extern const char *monthname[12];
682const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
683const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
684 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
685 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200686struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200687struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100688
Willy Tarreau42250582007-04-01 01:30:43 +0200689/*
690 * send a log for the session when we have enough info about it.
691 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100692 */
Willy Tarreau42250582007-04-01 01:30:43 +0200693static void http_sess_log(struct session *s)
694{
695 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
696 struct proxy *fe = s->fe;
697 struct proxy *be = s->be;
698 struct proxy *prx_log;
699 struct http_txn *txn = &s->txn;
700 int tolog;
701 char *uri, *h;
702 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200703 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200704 static char tmpline[MAX_SYSLOG_LEN];
705 int hdr;
706
707 if (fe->logfac1 < 0 && fe->logfac2 < 0)
708 return;
709 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100710
Willy Tarreau42250582007-04-01 01:30:43 +0200711 if (s->cli_addr.ss_family == AF_INET)
712 inet_ntop(AF_INET,
713 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
714 pn, sizeof(pn));
715 else
716 inet_ntop(AF_INET6,
717 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
718 pn, sizeof(pn));
719
Willy Tarreaufe944602007-10-25 10:34:16 +0200720 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200721
722 /* FIXME: let's limit ourselves to frontend logging for now. */
723 tolog = fe->to_log;
724
725 h = tmpline;
726 if (fe->to_log & LW_REQHDR &&
727 txn->req.cap &&
728 (h < tmpline + sizeof(tmpline) - 10)) {
729 *(h++) = ' ';
730 *(h++) = '{';
731 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
732 if (hdr)
733 *(h++) = '|';
734 if (txn->req.cap[hdr] != NULL)
735 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
736 '#', hdr_encode_map, txn->req.cap[hdr]);
737 }
738 *(h++) = '}';
739 }
740
741 if (fe->to_log & LW_RSPHDR &&
742 txn->rsp.cap &&
743 (h < tmpline + sizeof(tmpline) - 7)) {
744 *(h++) = ' ';
745 *(h++) = '{';
746 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
747 if (hdr)
748 *(h++) = '|';
749 if (txn->rsp.cap[hdr] != NULL)
750 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
751 '#', hdr_encode_map, txn->rsp.cap[hdr]);
752 }
753 *(h++) = '}';
754 }
755
756 if (h < tmpline + sizeof(tmpline) - 4) {
757 *(h++) = ' ';
758 *(h++) = '"';
759 uri = txn->uri ? txn->uri : "<BADREQ>";
760 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
761 '#', url_encode_map, uri);
762 *(h++) = '"';
763 }
764 *h = '\0';
765
766 svid = (tolog & LW_SVID) ?
767 (s->data_source != DATA_SRC_STATS) ?
768 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
769
770 send_log(prx_log, LOG_INFO,
771 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
772 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100773 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200774 pn,
775 (s->cli_addr.ss_family == AF_INET) ?
776 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
777 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200778 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
779 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200780 fe->id, be->id, svid,
781 s->logs.t_request,
782 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
783 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
784 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
785 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
786 txn->status,
787 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
788 txn->cli_cookie ? txn->cli_cookie : "-",
789 txn->srv_cookie ? txn->srv_cookie : "-",
790 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
791 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
792 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
793 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
794 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100795 (s->flags & SN_REDISP)?"+":"",
796 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200797 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
798
799 s->logs.logwait = 0;
800}
801
Willy Tarreau117f59e2007-03-04 18:17:17 +0100802
803/*
804 * Capture headers from message starting at <som> according to header list
805 * <cap_hdr>, and fill the <idx> structure appropriately.
806 */
807void capture_headers(char *som, struct hdr_idx *idx,
808 char **cap, struct cap_hdr *cap_hdr)
809{
810 char *eol, *sol, *col, *sov;
811 int cur_idx;
812 struct cap_hdr *h;
813 int len;
814
815 sol = som + hdr_idx_first_pos(idx);
816 cur_idx = hdr_idx_first_idx(idx);
817
818 while (cur_idx) {
819 eol = sol + idx->v[cur_idx].len;
820
821 col = sol;
822 while (col < eol && *col != ':')
823 col++;
824
825 sov = col + 1;
826 while (sov < eol && http_is_lws[(unsigned char)*sov])
827 sov++;
828
829 for (h = cap_hdr; h; h = h->next) {
830 if ((h->namelen == col - sol) &&
831 (strncasecmp(sol, h->name, h->namelen) == 0)) {
832 if (cap[h->index] == NULL)
833 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200834 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100835
836 if (cap[h->index] == NULL) {
837 Alert("HTTP capture : out of memory.\n");
838 continue;
839 }
840
841 len = eol - sov;
842 if (len > h->len)
843 len = h->len;
844
845 memcpy(cap[h->index], sov, len);
846 cap[h->index][len]=0;
847 }
848 }
849 sol = eol + idx->v[cur_idx].cr + 1;
850 cur_idx = idx->v[cur_idx].next;
851 }
852}
853
854
Willy Tarreau42250582007-04-01 01:30:43 +0200855/* either we find an LF at <ptr> or we jump to <bad>.
856 */
857#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
858
859/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
860 * otherwise to <http_msg_ood> with <state> set to <st>.
861 */
862#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
863 ptr++; \
864 if (likely(ptr < end)) \
865 goto good; \
866 else { \
867 state = (st); \
868 goto http_msg_ood; \
869 } \
870 } while (0)
871
872
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100874 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100875 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
876 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
877 * will give undefined results.
878 * Note that it is upon the caller's responsibility to ensure that ptr < end,
879 * and that msg->sol points to the beginning of the response.
880 * If a complete line is found (which implies that at least one CR or LF is
881 * found before <end>, the updated <ptr> is returned, otherwise NULL is
882 * returned indicating an incomplete line (which does not mean that parts have
883 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
884 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
885 * upon next call.
886 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200887 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100888 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
889 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200890 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100891 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100892const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100893 const char *ptr, const char *end,
894 char **ret_ptr, int *ret_state)
895{
896 __label__
897 http_msg_rpver,
898 http_msg_rpver_sp,
899 http_msg_rpcode,
900 http_msg_rpcode_sp,
901 http_msg_rpreason,
902 http_msg_rpline_eol,
903 http_msg_ood, /* out of data */
904 http_msg_invalid;
905
906 switch (state) {
907 http_msg_rpver:
908 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100909 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100910 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
911
912 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100913 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100914 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
915 }
916 goto http_msg_invalid;
917
918 http_msg_rpver_sp:
919 case HTTP_MSG_RPVER_SP:
920 if (likely(!HTTP_IS_LWS(*ptr))) {
921 msg->sl.st.c = ptr - msg_buf;
922 goto http_msg_rpcode;
923 }
924 if (likely(HTTP_IS_SPHT(*ptr)))
925 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
926 /* so it's a CR/LF, this is invalid */
927 goto http_msg_invalid;
928
929 http_msg_rpcode:
930 case HTTP_MSG_RPCODE:
931 if (likely(!HTTP_IS_LWS(*ptr)))
932 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
933
934 if (likely(HTTP_IS_SPHT(*ptr))) {
935 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
936 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
937 }
938
939 /* so it's a CR/LF, so there is no reason phrase */
940 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
941 http_msg_rsp_reason:
942 /* FIXME: should we support HTTP responses without any reason phrase ? */
943 msg->sl.st.r = ptr - msg_buf;
944 msg->sl.st.r_l = 0;
945 goto http_msg_rpline_eol;
946
947 http_msg_rpcode_sp:
948 case HTTP_MSG_RPCODE_SP:
949 if (likely(!HTTP_IS_LWS(*ptr))) {
950 msg->sl.st.r = ptr - msg_buf;
951 goto http_msg_rpreason;
952 }
953 if (likely(HTTP_IS_SPHT(*ptr)))
954 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
955 /* so it's a CR/LF, so there is no reason phrase */
956 goto http_msg_rsp_reason;
957
958 http_msg_rpreason:
959 case HTTP_MSG_RPREASON:
960 if (likely(!HTTP_IS_CRLF(*ptr)))
961 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
962 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
963 http_msg_rpline_eol:
964 /* We have seen the end of line. Note that we do not
965 * necessarily have the \n yet, but at least we know that we
966 * have EITHER \r OR \n, otherwise the response would not be
967 * complete. We can then record the response length and return
968 * to the caller which will be able to register it.
969 */
970 msg->sl.st.l = ptr - msg->sol;
971 return ptr;
972
973#ifdef DEBUG_FULL
974 default:
975 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
976 exit(1);
977#endif
978 }
979
980 http_msg_ood:
981 /* out of data */
982 if (ret_state)
983 *ret_state = state;
984 if (ret_ptr)
985 *ret_ptr = (char *)ptr;
986 return NULL;
987
988 http_msg_invalid:
989 /* invalid message */
990 if (ret_state)
991 *ret_state = HTTP_MSG_ERROR;
992 return NULL;
993}
994
995
996/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100997 * This function parses a request line between <ptr> and <end>, starting with
998 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
999 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1000 * will give undefined results.
1001 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1002 * and that msg->sol points to the beginning of the request.
1003 * If a complete line is found (which implies that at least one CR or LF is
1004 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1005 * returned indicating an incomplete line (which does not mean that parts have
1006 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1007 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1008 * upon next call.
1009 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001010 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001011 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1012 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001013 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001015const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +01001016 const char *ptr, const char *end,
1017 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001019 __label__
1020 http_msg_rqmeth,
1021 http_msg_rqmeth_sp,
1022 http_msg_rquri,
1023 http_msg_rquri_sp,
1024 http_msg_rqver,
1025 http_msg_rqline_eol,
1026 http_msg_ood, /* out of data */
1027 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001028
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029 switch (state) {
1030 http_msg_rqmeth:
1031 case HTTP_MSG_RQMETH:
1032 if (likely(HTTP_IS_TOKEN(*ptr)))
1033 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001034
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001035 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001036 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001037 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1038 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001039
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040 if (likely(HTTP_IS_CRLF(*ptr))) {
1041 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001042 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 http_msg_req09_uri:
1044 msg->sl.rq.u = ptr - msg_buf;
1045 http_msg_req09_uri_e:
1046 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1047 http_msg_req09_ver:
1048 msg->sl.rq.v = ptr - msg_buf;
1049 msg->sl.rq.v_l = 0;
1050 goto http_msg_rqline_eol;
1051 }
1052 goto http_msg_invalid;
1053
1054 http_msg_rqmeth_sp:
1055 case HTTP_MSG_RQMETH_SP:
1056 if (likely(!HTTP_IS_LWS(*ptr))) {
1057 msg->sl.rq.u = ptr - msg_buf;
1058 goto http_msg_rquri;
1059 }
1060 if (likely(HTTP_IS_SPHT(*ptr)))
1061 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1062 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1063 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001065 http_msg_rquri:
1066 case HTTP_MSG_RQURI:
1067 if (likely(!HTTP_IS_LWS(*ptr)))
1068 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001069
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001070 if (likely(HTTP_IS_SPHT(*ptr))) {
1071 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1072 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1073 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001074
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1076 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 http_msg_rquri_sp:
1079 case HTTP_MSG_RQURI_SP:
1080 if (likely(!HTTP_IS_LWS(*ptr))) {
1081 msg->sl.rq.v = ptr - msg_buf;
1082 goto http_msg_rqver;
1083 }
1084 if (likely(HTTP_IS_SPHT(*ptr)))
1085 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1086 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1087 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001089 http_msg_rqver:
1090 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001091 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001093
1094 if (likely(HTTP_IS_CRLF(*ptr))) {
1095 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1096 http_msg_rqline_eol:
1097 /* We have seen the end of line. Note that we do not
1098 * necessarily have the \n yet, but at least we know that we
1099 * have EITHER \r OR \n, otherwise the request would not be
1100 * complete. We can then record the request length and return
1101 * to the caller which will be able to register it.
1102 */
1103 msg->sl.rq.l = ptr - msg->sol;
1104 return ptr;
1105 }
1106
1107 /* neither an HTTP_VER token nor a CRLF */
1108 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110#ifdef DEBUG_FULL
1111 default:
1112 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1113 exit(1);
1114#endif
1115 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 http_msg_ood:
1118 /* out of data */
1119 if (ret_state)
1120 *ret_state = state;
1121 if (ret_ptr)
1122 *ret_ptr = (char *)ptr;
1123 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001125 http_msg_invalid:
1126 /* invalid message */
1127 if (ret_state)
1128 *ret_state = HTTP_MSG_ERROR;
1129 return NULL;
1130}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001131
1132
Willy Tarreau8973c702007-01-21 23:58:29 +01001133/*
1134 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001135 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001136 * when data are missing and recalled at the exact same location with no
1137 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001138 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1139 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001141void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1142{
1143 __label__
1144 http_msg_rqbefore,
1145 http_msg_rqbefore_cr,
1146 http_msg_rqmeth,
1147 http_msg_rqline_end,
1148 http_msg_hdr_first,
1149 http_msg_hdr_name,
1150 http_msg_hdr_l1_sp,
1151 http_msg_hdr_l1_lf,
1152 http_msg_hdr_l1_lws,
1153 http_msg_hdr_val,
1154 http_msg_hdr_l2_lf,
1155 http_msg_hdr_l2_lws,
1156 http_msg_complete_header,
1157 http_msg_last_lf,
1158 http_msg_ood, /* out of data */
1159 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001160
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 int state; /* updated only when leaving the FSM */
1162 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001163
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001164 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001165 ptr = buf->lr;
1166 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 if (unlikely(ptr >= end))
1169 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 /*
1173 * First, states that are specific to the response only.
1174 * We check them first so that request and headers are
1175 * closer to each other (accessed more often).
1176 */
1177 http_msg_rpbefore:
1178 case HTTP_MSG_RPBEFORE:
1179 if (likely(HTTP_IS_TOKEN(*ptr))) {
1180 if (likely(ptr == buf->data)) {
1181 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001182 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 } else {
1184#if PARSE_PRESERVE_EMPTY_LINES
1185 /* only skip empty leading lines, don't remove them */
1186 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001187 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001188#else
1189 /* Remove empty leading lines, as recommended by
1190 * RFC2616. This takes a lot of time because we
1191 * must move all the buffer backwards, but this
1192 * is rarely needed. The method above will be
1193 * cleaner when we'll be able to start sending
1194 * the request from any place in the buffer.
1195 */
1196 buf->lr = ptr;
1197 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001198 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001199 msg->sol = buf->data;
1200 ptr = buf->data;
1201 end = buf->r;
1202#endif
1203 }
1204 hdr_idx_init(idx);
1205 state = HTTP_MSG_RPVER;
1206 goto http_msg_rpver;
1207 }
1208
1209 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1210 goto http_msg_invalid;
1211
1212 if (unlikely(*ptr == '\n'))
1213 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1214 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1215 /* stop here */
1216
1217 http_msg_rpbefore_cr:
1218 case HTTP_MSG_RPBEFORE_CR:
1219 EXPECT_LF_HERE(ptr, http_msg_invalid);
1220 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1221 /* stop here */
1222
1223 http_msg_rpver:
1224 case HTTP_MSG_RPVER:
1225 case HTTP_MSG_RPVER_SP:
1226 case HTTP_MSG_RPCODE:
1227 case HTTP_MSG_RPCODE_SP:
1228 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001229 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001230 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001231 if (unlikely(!ptr))
1232 return;
1233
1234 /* we have a full response and we know that we have either a CR
1235 * or an LF at <ptr>.
1236 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001237 //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 +01001238 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1239
1240 msg->sol = ptr;
1241 if (likely(*ptr == '\r'))
1242 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1243 goto http_msg_rpline_end;
1244
1245 http_msg_rpline_end:
1246 case HTTP_MSG_RPLINE_END:
1247 /* msg->sol must point to the first of CR or LF. */
1248 EXPECT_LF_HERE(ptr, http_msg_invalid);
1249 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1250 /* stop here */
1251
1252 /*
1253 * Second, states that are specific to the request only
1254 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 http_msg_rqbefore:
1256 case HTTP_MSG_RQBEFORE:
1257 if (likely(HTTP_IS_TOKEN(*ptr))) {
1258 if (likely(ptr == buf->data)) {
1259 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001260 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 } else {
1262#if PARSE_PRESERVE_EMPTY_LINES
1263 /* only skip empty leading lines, don't remove them */
1264 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001265 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001266#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 /* Remove empty leading lines, as recommended by
1268 * RFC2616. This takes a lot of time because we
1269 * must move all the buffer backwards, but this
1270 * is rarely needed. The method above will be
1271 * cleaner when we'll be able to start sending
1272 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001273 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 buf->lr = ptr;
1275 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001276 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 msg->sol = buf->data;
1278 ptr = buf->data;
1279 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001280#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001282 /* we will need this when keep-alive will be supported
1283 hdr_idx_init(idx);
1284 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001285 state = HTTP_MSG_RQMETH;
1286 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1290 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 if (unlikely(*ptr == '\n'))
1293 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1294 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 http_msg_rqbefore_cr:
1298 case HTTP_MSG_RQBEFORE_CR:
1299 EXPECT_LF_HERE(ptr, http_msg_invalid);
1300 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001301 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 http_msg_rqmeth:
1304 case HTTP_MSG_RQMETH:
1305 case HTTP_MSG_RQMETH_SP:
1306 case HTTP_MSG_RQURI:
1307 case HTTP_MSG_RQURI_SP:
1308 case HTTP_MSG_RQVER:
1309 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001310 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (unlikely(!ptr))
1312 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001313
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 /* we have a full request and we know that we have either a CR
1315 * or an LF at <ptr>.
1316 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001317 //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 +01001318 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 msg->sol = ptr;
1321 if (likely(*ptr == '\r'))
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 http_msg_rqline_end:
1326 case HTTP_MSG_RQLINE_END:
1327 /* check for HTTP/0.9 request : no version information available.
1328 * msg->sol must point to the first of CR or LF.
1329 */
1330 if (unlikely(msg->sl.rq.v_l == 0))
1331 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 EXPECT_LF_HERE(ptr, http_msg_invalid);
1334 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001336
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 /*
1338 * Common states below
1339 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 http_msg_hdr_first:
1341 case HTTP_MSG_HDR_FIRST:
1342 msg->sol = ptr;
1343 if (likely(!HTTP_IS_CRLF(*ptr))) {
1344 goto http_msg_hdr_name;
1345 }
1346
1347 if (likely(*ptr == '\r'))
1348 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1349 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001350
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 http_msg_hdr_name:
1352 case HTTP_MSG_HDR_NAME:
1353 /* assumes msg->sol points to the first char */
1354 if (likely(HTTP_IS_TOKEN(*ptr)))
1355 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 if (likely(*ptr == ':')) {
1358 msg->col = ptr - buf->data;
1359 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1360 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001363
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 http_msg_hdr_l1_sp:
1365 case HTTP_MSG_HDR_L1_SP:
1366 /* assumes msg->sol points to the first char and msg->col to the colon */
1367 if (likely(HTTP_IS_SPHT(*ptr)))
1368 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 /* header value can be basically anything except CR/LF */
1371 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001372
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 if (likely(!HTTP_IS_CRLF(*ptr))) {
1374 goto http_msg_hdr_val;
1375 }
1376
1377 if (likely(*ptr == '\r'))
1378 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1379 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 http_msg_hdr_l1_lf:
1382 case HTTP_MSG_HDR_L1_LF:
1383 EXPECT_LF_HERE(ptr, http_msg_invalid);
1384 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 http_msg_hdr_l1_lws:
1387 case HTTP_MSG_HDR_L1_LWS:
1388 if (likely(HTTP_IS_SPHT(*ptr))) {
1389 /* replace HT,CR,LF with spaces */
1390 for (; buf->data+msg->sov < ptr; msg->sov++)
1391 buf->data[msg->sov] = ' ';
1392 goto http_msg_hdr_l1_sp;
1393 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001394 /* we had a header consisting only in spaces ! */
1395 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 goto http_msg_complete_header;
1397
1398 http_msg_hdr_val:
1399 case HTTP_MSG_HDR_VAL:
1400 /* assumes msg->sol points to the first char, msg->col to the
1401 * colon, and msg->sov points to the first character of the
1402 * value.
1403 */
1404 if (likely(!HTTP_IS_CRLF(*ptr)))
1405 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001406
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 msg->eol = ptr;
1408 /* Note: we could also copy eol into ->eoh so that we have the
1409 * real header end in case it ends with lots of LWS, but is this
1410 * really needed ?
1411 */
1412 if (likely(*ptr == '\r'))
1413 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1414 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 http_msg_hdr_l2_lf:
1417 case HTTP_MSG_HDR_L2_LF:
1418 EXPECT_LF_HERE(ptr, http_msg_invalid);
1419 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 http_msg_hdr_l2_lws:
1422 case HTTP_MSG_HDR_L2_LWS:
1423 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1424 /* LWS: replace HT,CR,LF with spaces */
1425 for (; msg->eol < ptr; msg->eol++)
1426 *msg->eol = ' ';
1427 goto http_msg_hdr_val;
1428 }
1429 http_msg_complete_header:
1430 /*
1431 * It was a new header, so the last one is finished.
1432 * Assumes msg->sol points to the first char, msg->col to the
1433 * colon, msg->sov points to the first character of the value
1434 * and msg->eol to the first CR or LF so we know how the line
1435 * ends. We insert last header into the index.
1436 */
1437 /*
1438 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1439 write(2, msg->sol, msg->eol-msg->sol);
1440 fprintf(stderr,"\n");
1441 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1444 idx, idx->tail) < 0))
1445 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 msg->sol = ptr;
1448 if (likely(!HTTP_IS_CRLF(*ptr))) {
1449 goto http_msg_hdr_name;
1450 }
1451
1452 if (likely(*ptr == '\r'))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1454 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 http_msg_last_lf:
1457 case HTTP_MSG_LAST_LF:
1458 /* Assumes msg->sol points to the first of either CR or LF */
1459 EXPECT_LF_HERE(ptr, http_msg_invalid);
1460 ptr++;
1461 buf->lr = ptr;
1462 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001463 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 return;
1465#ifdef DEBUG_FULL
1466 default:
1467 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1468 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001469#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 }
1471 http_msg_ood:
1472 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001473 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 buf->lr = ptr;
1475 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 http_msg_invalid:
1478 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001479 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 return;
1481}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483/*
1484 * manages the client FSM and its socket. BTW, it also tries to handle the
1485 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1486 * 0 else.
1487 */
1488int process_cli(struct session *t)
1489{
1490 int s = t->srv_state;
1491 int c = t->cli_state;
1492 struct buffer *req = t->req;
1493 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1496 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001497 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 req->rex.tv_sec, req->rex.tv_usec,
1499 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 if (c == CL_STHEADERS) {
1502 /*
1503 * Now parse the partial (or complete) lines.
1504 * We will check the request syntax, and also join multi-line
1505 * headers. An index of all the lines will be elaborated while
1506 * parsing.
1507 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001508 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001511 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 * req->data + req->eoh = end of processed headers / start of current one
1513 * req->data + req->eol = end of current header or line (LF or CRLF)
1514 * req->lr = first non-visited byte
1515 * req->r = end of data
1516 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001519 struct http_txn *txn = &t->txn;
1520 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001522
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001524 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 /* 1: we might have to print this header in debug mode */
1527 if (unlikely((global.mode & MODE_DEBUG) &&
1528 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001529 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001531
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001532 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 eol = sol + msg->sl.rq.l;
1534 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001535
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001536 sol += hdr_idx_first_pos(&txn->hdr_idx);
1537 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001538
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001540 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001542 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1543 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001544 }
1545 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001546
Willy Tarreau58f10d72006-12-04 02:26:12 +01001547
1548 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001550 * If not so, we check the FD and buffer states before leaving.
1551 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1553 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001554 *
1555 */
1556
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001557 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 /*
1559 * First, let's catch bad requests.
1560 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001561 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001563
1564 /* 1: Since we are in header mode, if there's no space
1565 * left for headers, we won't be able to free more
1566 * later, so the session will never terminate. We
1567 * must terminate it now.
1568 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 if (unlikely(req->l >= req->rlim - req->data)) {
1570 /* FIXME: check if URI is set and return Status
1571 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001572 */
Willy Tarreau06619262006-12-17 08:37:22 +01001573 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001574 }
1575
1576 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1578 /* read error, or last read : give up. */
Willy Tarreaufa645582007-06-03 15:59:52 +02001579 buffer_shutr(req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001580 fd_delete(t->cli_fd);
1581 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001582 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001583 if (!(t->flags & SN_ERR_MASK))
1584 t->flags |= SN_ERR_CLICL;
1585 if (!(t->flags & SN_FINST_MASK))
1586 t->flags |= SN_FINST_R;
1587 return 1;
1588 }
1589
1590 /* 3: has the read timeout expired ? */
Willy Tarreau036fae02008-01-06 13:24:40 +01001591 else if (unlikely(tv_isle(&req->rex, &now) ||
1592 tv_isle(&txn->exp, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001593 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001594 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001595 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001596 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001597 if (!(t->flags & SN_ERR_MASK))
1598 t->flags |= SN_ERR_CLITO;
1599 if (!(t->flags & SN_FINST_MASK))
1600 t->flags |= SN_FINST_R;
1601 return 1;
1602 }
1603
1604 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001605 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001606 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001607 * full. We cannot loop here since stream_sock_read will disable it only if
1608 * req->l == rlim-data
1609 */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01001610 if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001611 tv_eternity(&req->rex);
1612 }
1613 return t->cli_state != CL_STHEADERS;
1614 }
1615
1616
1617 /****************************************************************
1618 * More interesting part now : we know that we have a complete *
1619 * request which at least looks like HTTP. We have an indicator *
1620 * of each header's length, so we can parse them quickly. *
1621 ****************************************************************/
1622
Willy Tarreau9cdde232007-05-02 20:58:19 +02001623 /* ensure we keep this pointer to the beginning of the message */
1624 msg->sol = req->data + msg->som;
1625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 /*
1627 * 1: identify the method
1628 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001629 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630
1631 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001633 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001635 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 if (unlikely((t->fe->monitor_uri_len != 0) &&
1637 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1638 !memcmp(&req->data[msg->sl.rq.u],
1639 t->fe->monitor_uri,
1640 t->fe->monitor_uri_len))) {
1641 /*
1642 * We have found the monitor URI
1643 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001644 struct acl_cond *cond;
1645 cur_proxy = t->fe;
1646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001648
1649 /* Check if we want to fail this monitor request or not */
1650 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1651 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
1652 if (cond->pol == ACL_COND_UNLESS)
1653 ret = !ret;
1654
1655 if (ret) {
1656 /* we fail this request, let's return 503 service unavail */
1657 txn->status = 503;
1658 client_retnclose(t, error_message(t, HTTP_ERR_503));
1659 goto return_prx_cond;
1660 }
1661 }
1662
1663 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001664 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 client_retnclose(t, &http_200_chunk);
1666 goto return_prx_cond;
1667 }
1668
1669 /*
1670 * 3: Maybe we have to copy the original REQURI for the logs ?
1671 * Note: we cannot log anymore if the request has been
1672 * classified as invalid.
1673 */
1674 if (unlikely(t->logs.logwait & LW_REQ)) {
1675 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001676 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 int urilen = msg->sl.rq.l;
1678
1679 if (urilen >= REQURI_LEN)
1680 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001681 memcpy(txn->uri, &req->data[msg->som], urilen);
1682 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683
1684 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001685 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001686 } else {
1687 Alert("HTTP logging : out of memory.\n");
1688 }
1689 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690
Willy Tarreau06619262006-12-17 08:37:22 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1693 if (unlikely(msg->sl.rq.v_l == 0)) {
1694 int delta;
1695 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001696 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 cur_end = msg->sol + msg->sl.rq.l;
1698 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 if (msg->sl.rq.u_l == 0) {
1701 /* if no URI was set, add "/" */
1702 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1703 cur_end += delta;
1704 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001705 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 /* add HTTP version */
1707 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1708 msg->eoh += delta;
1709 cur_end += delta;
1710 cur_end = (char *)http_parse_reqline(msg, req->data,
1711 HTTP_MSG_RQMETH,
1712 msg->sol, cur_end + 1,
1713 NULL, NULL);
1714 if (unlikely(!cur_end))
1715 goto return_bad_req;
1716
1717 /* we have a full HTTP/1.0 request now and we know that
1718 * we have either a CR or an LF at <ptr>.
1719 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001720 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001721 }
1722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723
1724 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001725 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001726 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001727 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001728
1729 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001731 * As opposed to version 1.2, now they will be evaluated in the
1732 * filters order and not in the header order. This means that
1733 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001734 *
1735 * We can now check whether we want to switch to another
1736 * backend, in which case we will re-check the backend's
1737 * filters and various options. In order to support 3-level
1738 * switching, here's how we should proceed :
1739 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001740 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001741 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001742 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001743 * There cannot be any switch from there, so ->be cannot be
1744 * changed anymore.
1745 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001746 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001747 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001748 * The response path will be able to apply either ->be, or
1749 * ->be then ->fe filters in order to match the reverse of
1750 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001751 */
1752
Willy Tarreau06619262006-12-17 08:37:22 +01001753 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001754 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001755 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001756 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001757
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001758 /* first check whether we have some ACLs set to block this request */
1759 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001760 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001761 if (cond->pol == ACL_COND_UNLESS)
1762 ret = !ret;
1763
1764 if (ret) {
1765 txn->status = 403;
1766 /* let's log the request time */
1767 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1768 client_retnclose(t, error_message(t, HTTP_ERR_403));
1769 goto return_prx_cond;
1770 }
1771 }
1772
Willy Tarreau06619262006-12-17 08:37:22 +01001773 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001774 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1776 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001777 }
1778
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001779 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1780 /* to ensure correct connection accounting on
1781 * the backend, we count the connection for the
1782 * one managing the queue.
1783 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001784 t->be->beconn++;
1785 if (t->be->beconn > t->be->beconn_max)
1786 t->be->beconn_max = t->be->beconn;
1787 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001788 t->flags |= SN_BE_ASSIGNED;
1789 }
1790
Willy Tarreau06619262006-12-17 08:37:22 +01001791 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001792 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001793 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001794 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001795 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001796 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001797 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001798 goto return_prx_cond;
1799 }
1800
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001802 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 !(t->flags & SN_CONN_CLOSED)) {
1804 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001805 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001807
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001808 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 old_idx = 0;
1810
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001811 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1812 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001813 cur_ptr = cur_next;
1814 cur_end = cur_ptr + cur_hdr->len;
1815 cur_next = cur_end + cur_hdr->cr + 1;
1816
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001817 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1818 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 /* 3 possibilities :
1820 * - we have already set Connection: close,
1821 * so we remove this line.
1822 * - we have not yet set Connection: close,
1823 * but this line indicates close. We leave
1824 * it untouched and set the flag.
1825 * - we have not yet set Connection: close,
1826 * and this line indicates non-close. We
1827 * replace it.
1828 */
1829 if (t->flags & SN_CONN_CLOSED) {
1830 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001831 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001832 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001833 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1834 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 cur_hdr->len = 0;
1836 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001837 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1838 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1839 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 cur_next += delta;
1841 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001842 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 }
1844 t->flags |= SN_CONN_CLOSED;
1845 }
1846 }
1847 old_idx = cur_idx;
1848 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001849 }
1850 /* add request headers from the rule sets in the same order */
1851 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1852 if (unlikely(http_header_add_tail(req,
1853 &txn->req,
1854 &txn->hdr_idx,
1855 rule_set->req_add[cur_idx])) < 0)
1856 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001857 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001858
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001859 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001860 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001861 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001862 /* we have to check the URI and auth for this request */
1863 if (stats_check_uri_auth(t, rule_set))
1864 return 1;
1865 }
1866
Willy Tarreau55ea7572007-06-17 19:56:27 +02001867 /* now check whether we have some switching rules for this request */
1868 if (!(t->flags & SN_BE_ASSIGNED)) {
1869 struct switching_rule *rule;
1870
1871 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
1872 int ret;
1873
1874 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
1875 if (cond->pol == ACL_COND_UNLESS)
1876 ret = !ret;
1877
1878 if (ret) {
1879 t->be = rule->be.backend;
1880 t->be->beconn++;
1881 if (t->be->beconn > t->be->beconn_max)
1882 t->be->beconn_max = t->be->beconn;
1883 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001884
1885 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01001886 t->rep->rto = t->req->wto = t->be->timeout.server;
1887 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001888 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02001889 t->flags |= SN_BE_ASSIGNED;
1890 break;
1891 }
1892 }
1893 }
1894
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001895 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1896 /* No backend was set, but there was a default
1897 * backend set in the frontend, so we use it and
1898 * loop again.
1899 */
1900 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001901 t->be->beconn++;
1902 if (t->be->beconn > t->be->beconn_max)
1903 t->be->beconn_max = t->be->beconn;
1904 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001905
1906 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01001907 t->rep->rto = t->req->wto = t->be->timeout.server;
1908 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001909 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001910 t->flags |= SN_BE_ASSIGNED;
1911 }
1912 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001913
Willy Tarreau58f10d72006-12-04 02:26:12 +01001914
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001915 if (!(t->flags & SN_BE_ASSIGNED)) {
1916 /* To ensure correct connection accounting on
1917 * the backend, we count the connection for the
1918 * one managing the queue.
1919 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001920 t->be->beconn++;
1921 if (t->be->beconn > t->be->beconn_max)
1922 t->be->beconn_max = t->be->beconn;
1923 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001924 t->flags |= SN_BE_ASSIGNED;
1925 }
1926
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001927 /*
1928 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001929 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001930 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001931 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001932 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001933
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001934 /*
1935 * If HTTP PROXY is set we simply get remote server address
1936 * parsing incoming request.
1937 */
1938 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
1939 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
1940 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001941
Willy Tarreau2a324282006-12-05 00:05:46 +01001942 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001943 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001944 * so let's do the same now.
1945 */
1946
1947 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001948 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001949 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001950 }
1951
1952
1953 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001954 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001955 * Note that doing so might move headers in the request, but
1956 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001957 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001958 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01001959 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
1960 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001961 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001962
Willy Tarreau58f10d72006-12-04 02:26:12 +01001963
Willy Tarreau2a324282006-12-05 00:05:46 +01001964 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001965 * 9: add X-Forwarded-For if either the frontend or the backend
1966 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001967 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001968 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001969 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001970 /* Add an X-Forwarded-For header unless the source IP is
1971 * in the 'except' network range.
1972 */
1973 if ((!t->fe->except_mask.s_addr ||
1974 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1975 != t->fe->except_net.s_addr) &&
1976 (!t->be->except_mask.s_addr ||
1977 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1978 != t->be->except_net.s_addr)) {
1979 int len;
1980 unsigned char *pn;
1981 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001982
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001983 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1984 pn[0], pn[1], pn[2], pn[3]);
1985
1986 if (unlikely(http_header_add_tail2(req, &txn->req,
1987 &txn->hdr_idx, trash, len)) < 0)
1988 goto return_bad_req;
1989 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001990 }
1991 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001992 /* FIXME: for the sake of completeness, we should also support
1993 * 'except' here, although it is mostly useless in this case.
1994 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001995 int len;
1996 char pn[INET6_ADDRSTRLEN];
1997 inet_ntop(AF_INET6,
1998 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1999 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002000 len = sprintf(trash, "X-Forwarded-For: %s", pn);
2001 if (unlikely(http_header_add_tail2(req, &txn->req,
2002 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002003 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002004 }
2005 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002006
Willy Tarreau2a324282006-12-05 00:05:46 +01002007 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002008 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002009 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002010 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002011 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002012 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002013 if ((unlikely(msg->sl.rq.v_l != 8) ||
2014 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2015 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002016 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002017 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002018 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002019 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002020
Willy Tarreau2a324282006-12-05 00:05:46 +01002021 /*************************************************************
2022 * OK, that's finished for the headers. We have done what we *
2023 * could. Let's switch to the DATA state. *
2024 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002025
Willy Tarreau2a324282006-12-05 00:05:46 +01002026 t->cli_state = CL_STDATA;
2027 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002028
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002029 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002030
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002031 if (!tv_isset(&t->fe->timeout.client) ||
2032 (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server))) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002033 /* If the client has no timeout, or if the server is not ready yet,
2034 * and we know for sure that it can expire, then it's cleaner to
2035 * disable the timeout on the client side so that too low values
2036 * cannot make the sessions abort too early.
2037 *
2038 * FIXME-20050705: the server needs a way to re-enable this time-out
2039 * when it switches its state, otherwise a client can stay connected
2040 * indefinitely. This now seems to be OK.
2041 */
2042 tv_eternity(&req->rex);
2043 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002044
Willy Tarreau1fa31262007-12-03 00:36:16 +01002045 /* When a connection is tarpitted, we use the tarpit timeout,
2046 * which may be the same as the connect timeout if unspecified.
2047 * If unset, then set it to zero because we really want it to
2048 * eventually expire.
Willy Tarreau2a324282006-12-05 00:05:46 +01002049 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002050 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002051 t->req->l = 0;
2052 /* flush the request so that we can drop the connection early
2053 * if the client closes first.
2054 */
Willy Tarreau1fa31262007-12-03 00:36:16 +01002055 if (!tv_add_ifset(&req->cex, &now, &t->be->timeout.tarpit))
Willy Tarreaud825eef2007-05-12 22:35:00 +02002056 req->cex = now;
Willy Tarreau2a324282006-12-05 00:05:46 +01002057 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002058
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002059 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01002060 goto process_data;
2061
2062 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002063 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002064 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01002065 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002066 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002067 return_prx_cond:
2068 if (!(t->flags & SN_ERR_MASK))
2069 t->flags |= SN_ERR_PRXCOND;
2070 if (!(t->flags & SN_FINST_MASK))
2071 t->flags |= SN_FINST_R;
2072 return 1;
2073
Willy Tarreaubaaee002006-06-26 02:48:02 +02002074 }
2075 else if (c == CL_STDATA) {
2076 process_data:
2077 /* FIXME: this error handling is partly buggy because we always report
2078 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
2079 * or HEADER phase. BTW, it's not logical to expire the client while
2080 * we're waiting for the server to connect.
2081 */
2082 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002083 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002084 buffer_shutr(req);
2085 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002086 fd_delete(t->cli_fd);
2087 t->cli_state = CL_STCLOSE;
2088 if (!(t->flags & SN_ERR_MASK))
2089 t->flags |= SN_ERR_CLICL;
2090 if (!(t->flags & SN_FINST_MASK)) {
2091 if (t->pend_pos)
2092 t->flags |= SN_FINST_Q;
2093 else if (s == SV_STCONN)
2094 t->flags |= SN_FINST_C;
2095 else
2096 t->flags |= SN_FINST_D;
2097 }
2098 return 1;
2099 }
2100 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002101 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002102 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002103 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002104 t->cli_state = CL_STSHUTR;
2105 return 1;
2106 }
2107 /* last server read and buffer empty */
2108 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002109 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002110 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002111 shutdown(t->cli_fd, SHUT_WR);
2112 /* We must ensure that the read part is still alive when switching
2113 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002114 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002115 tv_add_ifset(&req->rex, &now, &t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002116 t->cli_state = CL_STSHUTW;
2117 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2118 return 1;
2119 }
2120 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002121 else if (tv_isle(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002122 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002123 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002124 t->cli_state = CL_STSHUTR;
2125 if (!(t->flags & SN_ERR_MASK))
2126 t->flags |= SN_ERR_CLITO;
2127 if (!(t->flags & SN_FINST_MASK)) {
2128 if (t->pend_pos)
2129 t->flags |= SN_FINST_Q;
2130 else if (s == SV_STCONN)
2131 t->flags |= SN_FINST_C;
2132 else
2133 t->flags |= SN_FINST_D;
2134 }
2135 return 1;
2136 }
2137 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002138 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002139 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002140 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002141 shutdown(t->cli_fd, SHUT_WR);
2142 /* We must ensure that the read part is still alive when switching
2143 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002144 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002145 tv_add_ifset(&req->rex, &now, &t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002146
2147 t->cli_state = CL_STSHUTW;
2148 if (!(t->flags & SN_ERR_MASK))
2149 t->flags |= SN_ERR_CLITO;
2150 if (!(t->flags & SN_FINST_MASK)) {
2151 if (t->pend_pos)
2152 t->flags |= SN_FINST_Q;
2153 else if (s == SV_STCONN)
2154 t->flags |= SN_FINST_C;
2155 else
2156 t->flags |= SN_FINST_D;
2157 }
2158 return 1;
2159 }
2160
2161 if (req->l >= req->rlim - req->data) {
2162 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002163 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002164 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002165 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002166 }
2167 } else {
2168 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002169 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002170 if (!tv_isset(&t->fe->timeout.client) ||
2171 (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server)))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002172 /* If the client has no timeout, or if the server not ready yet, and we
2173 * know for sure that it can expire, then it's cleaner to disable the
2174 * timeout on the client side so that too low values cannot make the
2175 * sessions abort too early.
2176 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002177 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002178 else
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002179 tv_add(&req->rex, &now, &t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002180 }
2181 }
2182
2183 if ((rep->l == 0) ||
2184 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002185 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2186 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002187 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002188 }
2189 } else {
2190 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002191 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2192 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002193 if (tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002194 /* FIXME: to prevent the client from expiring read timeouts during writes,
2195 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002196 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002197 }
2198 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002199 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002200 }
2201 }
2202 return 0; /* other cases change nothing */
2203 }
2204 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002205 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002206 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002207 fd_delete(t->cli_fd);
2208 t->cli_state = CL_STCLOSE;
2209 if (!(t->flags & SN_ERR_MASK))
2210 t->flags |= SN_ERR_CLICL;
2211 if (!(t->flags & SN_FINST_MASK)) {
2212 if (t->pend_pos)
2213 t->flags |= SN_FINST_Q;
2214 else if (s == SV_STCONN)
2215 t->flags |= SN_FINST_C;
2216 else
2217 t->flags |= SN_FINST_D;
2218 }
2219 return 1;
2220 }
2221 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2222 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002223 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002224 fd_delete(t->cli_fd);
2225 t->cli_state = CL_STCLOSE;
2226 return 1;
2227 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002228 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002229 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002230 fd_delete(t->cli_fd);
2231 t->cli_state = CL_STCLOSE;
2232 if (!(t->flags & SN_ERR_MASK))
2233 t->flags |= SN_ERR_CLITO;
2234 if (!(t->flags & SN_FINST_MASK)) {
2235 if (t->pend_pos)
2236 t->flags |= SN_FINST_Q;
2237 else if (s == SV_STCONN)
2238 t->flags |= SN_FINST_C;
2239 else
2240 t->flags |= SN_FINST_D;
2241 }
2242 return 1;
2243 }
2244
2245 if (t->flags & SN_SELF_GEN) {
2246 produce_content(t);
2247 if (rep->l == 0) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002248 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002249 fd_delete(t->cli_fd);
2250 t->cli_state = CL_STCLOSE;
2251 return 1;
2252 }
2253 }
2254
2255 if ((rep->l == 0)
2256 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002257 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2258 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002259 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002260 }
2261 } else {
2262 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002263 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2264 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002265 if (!tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client))
Willy Tarreaud7971282006-07-29 18:36:34 +02002266 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002267 }
2268 }
2269 return 0;
2270 }
2271 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002272 if (req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002273 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002274 fd_delete(t->cli_fd);
2275 t->cli_state = CL_STCLOSE;
2276 if (!(t->flags & SN_ERR_MASK))
2277 t->flags |= SN_ERR_CLICL;
2278 if (!(t->flags & SN_FINST_MASK)) {
2279 if (t->pend_pos)
2280 t->flags |= SN_FINST_Q;
2281 else if (s == SV_STCONN)
2282 t->flags |= SN_FINST_C;
2283 else
2284 t->flags |= SN_FINST_D;
2285 }
2286 return 1;
2287 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002288 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002289 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002290 fd_delete(t->cli_fd);
2291 t->cli_state = CL_STCLOSE;
2292 return 1;
2293 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002294 else if (tv_isle(&req->rex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002295 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002296 fd_delete(t->cli_fd);
2297 t->cli_state = CL_STCLOSE;
2298 if (!(t->flags & SN_ERR_MASK))
2299 t->flags |= SN_ERR_CLITO;
2300 if (!(t->flags & SN_FINST_MASK)) {
2301 if (t->pend_pos)
2302 t->flags |= SN_FINST_Q;
2303 else if (s == SV_STCONN)
2304 t->flags |= SN_FINST_C;
2305 else
2306 t->flags |= SN_FINST_D;
2307 }
2308 return 1;
2309 }
2310 else if (req->l >= req->rlim - req->data) {
2311 /* no room to read more data */
2312
2313 /* FIXME-20050705: is it possible for a client to maintain a session
2314 * after the timeout by sending more data after it receives a close ?
2315 */
2316
Willy Tarreau66319382007-04-08 17:17:37 +02002317 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002318 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002319 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002320 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2321 }
2322 } else {
2323 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002324 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002325 if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client))
Willy Tarreaud7971282006-07-29 18:36:34 +02002326 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002327 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2328 }
2329 }
2330 return 0;
2331 }
2332 else { /* CL_STCLOSE: nothing to do */
2333 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2334 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002335 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002336 write(1, trash, len);
2337 }
2338 return 0;
2339 }
2340 return 0;
2341}
2342
2343
2344/*
2345 * manages the server FSM and its socket. It returns 1 if a state has changed
2346 * (and a resync may be needed), 0 else.
2347 */
2348int process_srv(struct session *t)
2349{
2350 int s = t->srv_state;
2351 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002352 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002353 struct buffer *req = t->req;
2354 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002355 int conn_err;
2356
2357#ifdef DEBUG_FULL
2358 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2359#endif
Willy Tarreauee991362007-05-14 14:37:50 +02002360
2361#if 0
2362 fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n",
2363 __FUNCTION__, __LINE__,
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002364 t->fe->timeout.client.tv_sec, t->fe->timeout.client.tv_usec,
2365 t->fe->timeout.connect.tv_sec, t->fe->timeout.connect.tv_usec,
2366 t->fe->timeout.server.tv_sec, t->fe->timeout.server.tv_usec);
Willy Tarreauee991362007-05-14 14:37:50 +02002367 fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n",
2368 __FUNCTION__, __LINE__,
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002369 t->be->timeout.client.tv_sec, t->be->timeout.client.tv_usec,
2370 t->be->timeout.connect.tv_sec, t->be->timeout.connect.tv_usec,
2371 t->be->timeout.server.tv_sec, t->be->timeout.server.tv_usec);
Willy Tarreauee991362007-05-14 14:37:50 +02002372
2373 fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n",
2374 __FUNCTION__, __LINE__,
2375 req->cto.tv_sec, req->cto.tv_usec,
2376 req->rto.tv_sec, req->rto.tv_usec,
2377 req->wto.tv_sec, req->wto.tv_usec);
2378
2379 fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n",
2380 __FUNCTION__, __LINE__,
2381 rep->cto.tv_sec, rep->cto.tv_usec,
2382 rep->rto.tv_sec, rep->rto.tv_usec,
2383 rep->wto.tv_sec, rep->wto.tv_usec);
2384#endif
2385
Willy Tarreaubaaee002006-06-26 02:48:02 +02002386 //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s,
Willy Tarreauf161a342007-04-08 16:59:42 +02002387 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2388 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002389 //);
2390 if (s == SV_STIDLE) {
2391 if (c == CL_STHEADERS)
2392 return 0; /* stay in idle, waiting for data to reach the client side */
2393 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2394 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002395 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002396 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002397 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002398 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002399 /* note that this must not return any error because it would be able to
2400 * overwrite the client_retnclose() output.
2401 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002402 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002403 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002404 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002405 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002406
2407 return 1;
2408 }
2409 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002410 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002411 /* This connection is being tarpitted. The CLIENT side has
2412 * already set the connect expiration date to the right
2413 * timeout. We just have to check that it has not expired.
2414 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002415 if (!tv_isle(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002416 return 0;
2417
2418 /* We will set the queue timer to the time spent, just for
2419 * logging purposes. We fake a 500 server error, so that the
2420 * attacker will not suspect his connection has been tarpitted.
2421 * It will not cause trouble to the logs because we can exclude
2422 * the tarpitted connections by filtering on the 'PT' status flags.
2423 */
2424 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002425 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002426 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002427 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002428 return 1;
2429 }
2430
Willy Tarreaubaaee002006-06-26 02:48:02 +02002431 /* Right now, we will need to create a connection to the server.
2432 * We might already have tried, and got a connection pending, in
2433 * which case we will not do anything till it's pending. It's up
2434 * to any other session to release it and wake us up again.
2435 */
2436 if (t->pend_pos) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002437 if (!tv_isle(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002438 return 0;
2439 else {
2440 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002441 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002442 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002443 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002444 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002445 if (t->srv)
2446 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002447 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002448 return 1;
2449 }
2450 }
2451
2452 do {
2453 /* first, get a connection */
2454 if (srv_redispatch_connect(t))
2455 return t->srv_state != SV_STIDLE;
2456
2457 /* try to (re-)connect to the server, and fail if we expire the
2458 * number of retries.
2459 */
2460 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002461 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002462 return t->srv_state != SV_STIDLE;
2463 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002464 } while (1);
2465 }
2466 }
2467 else if (s == SV_STCONN) { /* connection in progress */
2468 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2469 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002470 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2471 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002472 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002473 fd_delete(t->srv_fd);
2474 if (t->srv)
2475 t->srv->cur_sess--;
2476
2477 /* note that this must not return any error because it would be able to
2478 * overwrite the client_retnclose() output.
2479 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002480 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002481 return 1;
2482 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002483 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002484 //fprintf(stderr,"1: c=%d, s=%d, now=%d.%06d, exp=%d.%06d\n", c, s, now.tv_sec, now.tv_usec, req->cex.tv_sec, req->cex.tv_usec);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002485 return 0; /* nothing changed */
2486 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002487 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002488 /* timeout, asynchronous connect error or first write error */
2489 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2490
2491 fd_delete(t->srv_fd);
2492 if (t->srv)
2493 t->srv->cur_sess--;
2494
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002495 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002496 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2497 else
2498 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2499
2500 /* ensure that we have enough retries left */
2501 if (srv_count_retry_down(t, conn_err))
2502 return 1;
2503
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002504 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002505 /* We're on our last chance, and the REDISP option was specified.
2506 * We will ignore cookie and force to balance or use the dispatcher.
2507 */
2508 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002509 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002510 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002511
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01002512 if (t->srv) {
Willy Tarreau98937b82007-12-10 15:05:42 +01002513 t->srv->cum_sess++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002514 t->srv->failed_conns++;
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01002515 t->srv->redispatches++;
2516 }
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002517 t->be->redispatches++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002518
2519 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01002520 t->flags |= SN_REDISP;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002521 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002522 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002523
2524 /* first, get a connection */
2525 if (srv_redispatch_connect(t))
2526 return t->srv_state != SV_STIDLE;
2527 }
2528
Willy Tarreaubaaee002006-06-26 02:48:02 +02002529 do {
2530 /* Now we will try to either reconnect to the same server or
2531 * connect to another server. If the connection gets queued
2532 * because all servers are saturated, then we will go back to
2533 * the SV_STIDLE state.
2534 */
2535 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002536 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002537 return t->srv_state != SV_STCONN;
2538 }
2539
2540 /* we need to redispatch the connection to another server */
2541 if (srv_redispatch_connect(t))
2542 return t->srv_state != SV_STCONN;
2543 } while (1);
2544 }
2545 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002546 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002547
2548 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2549 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002550 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002551 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002552 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002553 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002554 if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002555 /* FIXME: to prevent the server from expiring read timeouts during writes,
2556 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002557 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002558 }
2559 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002560 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002561 }
2562
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002563 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002564 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002565 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02002566 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002567
2568 t->srv_state = SV_STDATA;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002569 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2570
2571 /* if the user wants to log as soon as possible, without counting
2572 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002573 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002574 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002575 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002576 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002577#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002578 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002579 /* TCP splicing supported by both FE and BE */
2580 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2581 }
2582#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002583 }
2584 else {
2585 t->srv_state = SV_STHEADERS;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002586 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002587 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2588 /* reset hdr_idx which was already initialized by the request.
2589 * right now, the http parser does it.
2590 * hdr_idx_init(&t->txn.hdr_idx);
2591 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002592 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002593 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002594 return 1;
2595 }
2596 }
2597 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002598 /*
2599 * Now parse the partial (or complete) lines.
2600 * We will check the response syntax, and also join multi-line
2601 * headers. An index of all the lines will be elaborated while
2602 * parsing.
2603 *
2604 * For the parsing, we use a 28 states FSM.
2605 *
2606 * Here is the information we currently have :
2607 * rep->data + req->som = beginning of response
2608 * rep->data + req->eoh = end of processed headers / start of current one
2609 * rep->data + req->eol = end of current header or line (LF or CRLF)
2610 * rep->lr = first non-visited byte
2611 * rep->r = end of data
2612 */
2613
2614 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002615 struct http_msg *msg = &txn->rsp;
2616 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002617
Willy Tarreaua15645d2007-03-18 16:22:39 +01002618 if (likely(rep->lr < rep->r))
2619 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002620
Willy Tarreaua15645d2007-03-18 16:22:39 +01002621 /* 1: we might have to print this header in debug mode */
2622 if (unlikely((global.mode & MODE_DEBUG) &&
2623 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2624 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2625 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002626
Willy Tarreaua15645d2007-03-18 16:22:39 +01002627 sol = rep->data + msg->som;
2628 eol = sol + msg->sl.rq.l;
2629 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002630
Willy Tarreaua15645d2007-03-18 16:22:39 +01002631 sol += hdr_idx_first_pos(&txn->hdr_idx);
2632 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002633
Willy Tarreaua15645d2007-03-18 16:22:39 +01002634 while (cur_idx) {
2635 eol = sol + txn->hdr_idx.v[cur_idx].len;
2636 debug_hdr("srvhdr", t, sol, eol);
2637 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2638 cur_idx = txn->hdr_idx.v[cur_idx].next;
2639 }
2640 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002641
Willy Tarreaubaaee002006-06-26 02:48:02 +02002642
Willy Tarreau66319382007-04-08 17:17:37 +02002643 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002644 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002645 * full. We cannot loop here since stream_sock_read will disable it only if
2646 * rep->l == rlim-data
2647 */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002648 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002649 tv_eternity(&rep->rex);
2650 }
2651
2652
2653 /*
2654 * Now we quickly check if we have found a full valid response.
2655 * If not so, we check the FD and buffer states before leaving.
2656 * A full response is indicated by the fact that we have seen
2657 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2658 * responses are checked first.
2659 *
2660 * Depending on whether the client is still there or not, we
2661 * may send an error response back or not. Note that normally
2662 * we should only check for HTTP status there, and check I/O
2663 * errors somewhere else.
2664 */
2665
2666 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2667
2668 /* Invalid response, or read error or write error */
2669 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2670 (req->flags & BF_WRITE_ERROR) ||
2671 (rep->flags & BF_READ_ERROR))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002672 buffer_shutr(rep);
2673 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002674 fd_delete(t->srv_fd);
2675 if (t->srv) {
2676 t->srv->cur_sess--;
2677 t->srv->failed_resp++;
2678 }
2679 t->be->failed_resp++;
2680 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002681 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002682 client_return(t, error_message(t, HTTP_ERR_502));
2683 if (!(t->flags & SN_ERR_MASK))
2684 t->flags |= SN_ERR_SRVCL;
2685 if (!(t->flags & SN_FINST_MASK))
2686 t->flags |= SN_FINST_H;
2687 /* We used to have a free connection slot. Since we'll never use it,
2688 * we have to inform the server that it may be used by another session.
2689 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002690 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002691 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002692
Willy Tarreaua15645d2007-03-18 16:22:39 +01002693 return 1;
2694 }
2695
2696 /* end of client write or end of server read.
2697 * since we are in header mode, if there's no space left for headers, we
2698 * won't be able to free more later, so the session will never terminate.
2699 */
2700 else if (unlikely(rep->flags & BF_READ_NULL ||
2701 c == CL_STSHUTW || c == CL_STCLOSE ||
2702 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002703 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002704 buffer_shutr(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002705 t->srv_state = SV_STSHUTR;
2706 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2707 return 1;
2708 }
2709
2710 /* read timeout : return a 504 to the client.
2711 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002712 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002713 tv_isle(&rep->rex, &now))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002714 buffer_shutr(rep);
2715 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002716 fd_delete(t->srv_fd);
2717 if (t->srv) {
2718 t->srv->cur_sess--;
2719 t->srv->failed_resp++;
2720 }
2721 t->be->failed_resp++;
2722 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002723 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002724 client_return(t, error_message(t, HTTP_ERR_504));
2725 if (!(t->flags & SN_ERR_MASK))
2726 t->flags |= SN_ERR_SRVTO;
2727 if (!(t->flags & SN_FINST_MASK))
2728 t->flags |= SN_FINST_H;
2729 /* We used to have a free connection slot. Since we'll never use it,
2730 * we have to inform the server that it may be used by another session.
2731 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002732 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002733 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002734 return 1;
2735 }
2736
2737 /* last client read and buffer empty */
2738 /* FIXME!!! here, we don't want to switch to SHUTW if the
2739 * client shuts read too early, because we may still have
2740 * some work to do on the headers.
2741 * The side-effect is that if the client completely closes its
2742 * connection during SV_STHEADER, the connection to the server
2743 * is kept until a response comes back or the timeout is reached.
2744 */
2745 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2746 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002747 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002748 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002749
2750 /* We must ensure that the read part is still
2751 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002752 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002753 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002754
2755 shutdown(t->srv_fd, SHUT_WR);
2756 t->srv_state = SV_STSHUTW;
2757 return 1;
2758 }
2759
2760 /* write timeout */
2761 /* FIXME!!! here, we don't want to switch to SHUTW if the
2762 * client shuts read too early, because we may still have
2763 * some work to do on the headers.
2764 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002765 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002766 tv_isle(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002767 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002768 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002769 shutdown(t->srv_fd, SHUT_WR);
2770 /* We must ensure that the read part is still alive
2771 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002772 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002773 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002774
2775 t->srv_state = SV_STSHUTW;
2776 if (!(t->flags & SN_ERR_MASK))
2777 t->flags |= SN_ERR_SRVTO;
2778 if (!(t->flags & SN_FINST_MASK))
2779 t->flags |= SN_FINST_H;
2780 return 1;
2781 }
2782
2783 /*
2784 * And now the non-error cases.
2785 */
2786
2787 /* Data remaining in the request buffer.
2788 * This happens during the first pass here, and during
2789 * long posts.
2790 */
2791 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002792 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2793 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002794 if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002795 /* FIXME: to prevent the server from expiring read timeouts during writes,
2796 * we refresh it. */
2797 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002798 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002799 else
2800 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002801 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002802 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002803
Willy Tarreaua15645d2007-03-18 16:22:39 +01002804 /* nothing left in the request buffer */
2805 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002806 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2807 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002808 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002809 }
2810 }
2811
2812 return t->srv_state != SV_STHEADERS;
2813 }
2814
2815
2816 /*****************************************************************
2817 * More interesting part now : we know that we have a complete *
2818 * response which at least looks like HTTP. We have an indicator *
2819 * of each header's length, so we can parse them quickly. *
2820 ****************************************************************/
2821
Willy Tarreau9cdde232007-05-02 20:58:19 +02002822 /* ensure we keep this pointer to the beginning of the message */
2823 msg->sol = rep->data + msg->som;
2824
Willy Tarreaua15645d2007-03-18 16:22:39 +01002825 /*
2826 * 1: get the status code and check for cacheability.
2827 */
2828
2829 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002830 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002831
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002832 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002833 case 200:
2834 case 203:
2835 case 206:
2836 case 300:
2837 case 301:
2838 case 410:
2839 /* RFC2616 @13.4:
2840 * "A response received with a status code of
2841 * 200, 203, 206, 300, 301 or 410 MAY be stored
2842 * by a cache (...) unless a cache-control
2843 * directive prohibits caching."
2844 *
2845 * RFC2616 @9.5: POST method :
2846 * "Responses to this method are not cacheable,
2847 * unless the response includes appropriate
2848 * Cache-Control or Expires header fields."
2849 */
2850 if (likely(txn->meth != HTTP_METH_POST) &&
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002851 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
Willy Tarreau3d300592007-03-18 18:34:41 +01002852 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002853 break;
2854 default:
2855 break;
2856 }
2857
2858 /*
2859 * 2: we may need to capture headers
2860 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002861 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002862 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002863 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002864
2865 /*
2866 * 3: we will have to evaluate the filters.
2867 * As opposed to version 1.2, now they will be evaluated in the
2868 * filters order and not in the header order. This means that
2869 * each filter has to be validated among all headers.
2870 *
2871 * Filters are tried with ->be first, then with ->fe if it is
2872 * different from ->be.
2873 */
2874
2875 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2876
2877 cur_proxy = t->be;
2878 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002879 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002880
2881 /* try headers filters */
2882 if (rule_set->rsp_exp != NULL) {
2883 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2884 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002885 if (t->srv) {
2886 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002887 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002888 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002889 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002890 return_srv_prx_502:
Willy Tarreaufa645582007-06-03 15:59:52 +02002891 buffer_shutr(rep);
2892 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002893 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002894 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002895 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002896 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002897 if (!(t->flags & SN_ERR_MASK))
2898 t->flags |= SN_ERR_PRXCOND;
2899 if (!(t->flags & SN_FINST_MASK))
2900 t->flags |= SN_FINST_H;
2901 /* We used to have a free connection slot. Since we'll never use it,
2902 * we have to inform the server that it may be used by another session.
2903 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002904 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002905 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002906 return 1;
2907 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002908 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002909
Willy Tarreaua15645d2007-03-18 16:22:39 +01002910 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002911 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002912 if (t->srv) {
2913 t->srv->cur_sess--;
2914 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002915 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002916 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002917 goto return_srv_prx_502;
2918 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002919
Willy Tarreaua15645d2007-03-18 16:22:39 +01002920 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002921 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002922 !(t->flags & SN_CONN_CLOSED)) {
2923 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002924 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002925 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002926
Willy Tarreaua15645d2007-03-18 16:22:39 +01002927 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2928 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002929
Willy Tarreaua15645d2007-03-18 16:22:39 +01002930 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2931 cur_hdr = &txn->hdr_idx.v[cur_idx];
2932 cur_ptr = cur_next;
2933 cur_end = cur_ptr + cur_hdr->len;
2934 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002935
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002936 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2937 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002938 /* 3 possibilities :
2939 * - we have already set Connection: close,
2940 * so we remove this line.
2941 * - we have not yet set Connection: close,
2942 * but this line indicates close. We leave
2943 * it untouched and set the flag.
2944 * - we have not yet set Connection: close,
2945 * and this line indicates non-close. We
2946 * replace it.
2947 */
2948 if (t->flags & SN_CONN_CLOSED) {
2949 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2950 txn->rsp.eoh += delta;
2951 cur_next += delta;
2952 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2953 txn->hdr_idx.used--;
2954 cur_hdr->len = 0;
2955 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002956 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2957 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2958 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002959 cur_next += delta;
2960 cur_hdr->len += delta;
2961 txn->rsp.eoh += delta;
2962 }
2963 t->flags |= SN_CONN_CLOSED;
2964 }
2965 }
2966 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002967 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002968 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002969
Willy Tarreaua15645d2007-03-18 16:22:39 +01002970 /* add response headers from the rule sets in the same order */
2971 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002972 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2973 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002974 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002975 }
2976
Willy Tarreaua15645d2007-03-18 16:22:39 +01002977 /* check whether we're already working on the frontend */
2978 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002979 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002980 cur_proxy = t->fe;
2981 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002982
Willy Tarreaua15645d2007-03-18 16:22:39 +01002983 /*
2984 * 4: check for server cookie.
2985 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002986 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2987 || (t->be->options & PR_O_CHK_CACHE))
2988 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002989
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002990
Willy Tarreaua15645d2007-03-18 16:22:39 +01002991 /*
Willy Tarreau396d2c62007-11-04 19:30:00 +01002992 * 5: check for cache-control or pragma headers if required.
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002993 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002994 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2995 check_response_for_cacheability(t, rep);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002996
2997 /*
2998 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01002999 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003000 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3001 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003002 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003003
Willy Tarreaua15645d2007-03-18 16:22:39 +01003004 /* the server is known, it's not the one the client requested, we have to
3005 * insert a set-cookie here, except if we want to insert only on POST
3006 * requests and this one isn't. Note that servers which don't have cookies
3007 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003008 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003009 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003010 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003011 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003012
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003013 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3014 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003015 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01003016 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003017
Willy Tarreaua15645d2007-03-18 16:22:39 +01003018 /* Here, we will tell an eventual cache on the client side that we don't
3019 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3020 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3021 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3022 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003023 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
3024
3025 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3026
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003027 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3028 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003029 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003030 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003031 }
3032
3033
3034 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003035 * 7: check if result will be cacheable with a cookie.
3036 * We'll block the response if security checks have caught
3037 * nasty things such as a cacheable cookie.
3038 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003039 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3040 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003041 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003042
Willy Tarreaua15645d2007-03-18 16:22:39 +01003043 /* we're in presence of a cacheable response containing
3044 * a set-cookie header. We'll block it as requested by
3045 * the 'checkcache' option, and send an alert.
3046 */
3047 if (t->srv) {
3048 t->srv->cur_sess--;
3049 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003050 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003051 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003052
Willy Tarreaua15645d2007-03-18 16:22:39 +01003053 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003054 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003055 send_log(t->be, LOG_ALERT,
3056 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003057 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003058 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003059 }
3060
Willy Tarreaua15645d2007-03-18 16:22:39 +01003061 /*
3062 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003063 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003064 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003065 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003066 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003067 if ((unlikely(msg->sl.st.v_l != 8) ||
3068 unlikely(req->data[msg->som + 7] != '0')) &&
3069 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003070 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003071 goto return_bad_resp;
3072 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003073 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003074
Willy Tarreaubaaee002006-06-26 02:48:02 +02003075
Willy Tarreaua15645d2007-03-18 16:22:39 +01003076 /*************************************************************
3077 * OK, that's finished for the headers. We have done what we *
3078 * could. Let's switch to the DATA state. *
3079 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003080
Willy Tarreaua15645d2007-03-18 16:22:39 +01003081 t->srv_state = SV_STDATA;
3082 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003083 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003084
3085 /* client connection already closed or option 'forceclose' required :
3086 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003087 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003088 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003089 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003090 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003091 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003092
3093 /* We must ensure that the read part is still alive when switching
3094 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003095 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003096 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003097
Willy Tarreaua15645d2007-03-18 16:22:39 +01003098 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003099 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003100 }
3101
Willy Tarreaua15645d2007-03-18 16:22:39 +01003102#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003103 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003104 /* TCP splicing supported by both FE and BE */
3105 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003106 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003107#endif
3108 /* if the user wants to log as soon as possible, without counting
3109 bytes from the server, then this is the right moment. */
3110 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3111 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01003112 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003113 if (t->fe->to_log & LW_REQ)
3114 http_sess_log(t);
3115 else
3116 tcp_sess_log(t);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01003117 t->logs.bytes_in = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003118 }
3119
Willy Tarreaua15645d2007-03-18 16:22:39 +01003120 /* Note: we must not try to cheat by jumping directly to DATA,
3121 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003122 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003123
3124 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003125 }
3126 else if (s == SV_STDATA) {
3127 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003128 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02003129 buffer_shutr(rep);
3130 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003131 fd_delete(t->srv_fd);
3132 if (t->srv) {
3133 t->srv->cur_sess--;
3134 t->srv->failed_resp++;
3135 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003136 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003137 t->srv_state = SV_STCLOSE;
3138 if (!(t->flags & SN_ERR_MASK))
3139 t->flags |= SN_ERR_SRVCL;
3140 if (!(t->flags & SN_FINST_MASK))
3141 t->flags |= SN_FINST_D;
3142 /* We used to have a free connection slot. Since we'll never use it,
3143 * we have to inform the server that it may be used by another session.
3144 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003145 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003146 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003147
3148 return 1;
3149 }
3150 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003151 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003152 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003153 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003154 t->srv_state = SV_STSHUTR;
3155 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3156 return 1;
3157 }
3158 /* end of client read and no more data to send */
3159 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003160 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003161 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003162 shutdown(t->srv_fd, SHUT_WR);
3163 /* We must ensure that the read part is still alive when switching
3164 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003165 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003166 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003167
3168 t->srv_state = SV_STSHUTW;
3169 return 1;
3170 }
3171 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003172 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003173 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003174 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003175 t->srv_state = SV_STSHUTR;
3176 if (!(t->flags & SN_ERR_MASK))
3177 t->flags |= SN_ERR_SRVTO;
3178 if (!(t->flags & SN_FINST_MASK))
3179 t->flags |= SN_FINST_D;
3180 return 1;
3181 }
3182 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003183 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003184 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003185 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003186 shutdown(t->srv_fd, SHUT_WR);
3187 /* We must ensure that the read part is still alive when switching
3188 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003189 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003190 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003191 t->srv_state = SV_STSHUTW;
3192 if (!(t->flags & SN_ERR_MASK))
3193 t->flags |= SN_ERR_SRVTO;
3194 if (!(t->flags & SN_FINST_MASK))
3195 t->flags |= SN_FINST_D;
3196 return 1;
3197 }
3198
3199 /* recompute request time-outs */
3200 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003201 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3202 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003203 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003204 }
3205 }
3206 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003207 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3208 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003209 if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003210 /* FIXME: to prevent the server from expiring read timeouts during writes,
3211 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003212 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003213 }
3214 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003215 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003216 }
3217 }
3218
3219 /* recompute response time-outs */
3220 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003221 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003222 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003223 }
3224 }
3225 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003226 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003227 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02003228 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003229 }
3230 }
3231
3232 return 0; /* other cases change nothing */
3233 }
3234 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003235 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003236 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003237 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003238 fd_delete(t->srv_fd);
3239 if (t->srv) {
3240 t->srv->cur_sess--;
3241 t->srv->failed_resp++;
3242 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003243 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003244 //close(t->srv_fd);
3245 t->srv_state = SV_STCLOSE;
3246 if (!(t->flags & SN_ERR_MASK))
3247 t->flags |= SN_ERR_SRVCL;
3248 if (!(t->flags & SN_FINST_MASK))
3249 t->flags |= SN_FINST_D;
3250 /* We used to have a free connection slot. Since we'll never use it,
3251 * we have to inform the server that it may be used by another session.
3252 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003253 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003254 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003255
3256 return 1;
3257 }
3258 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003259 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003260 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003261 fd_delete(t->srv_fd);
3262 if (t->srv)
3263 t->srv->cur_sess--;
3264 //close(t->srv_fd);
3265 t->srv_state = SV_STCLOSE;
3266 /* We used to have a free connection slot. Since we'll never use it,
3267 * we have to inform the server that it may be used by another session.
3268 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003269 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003270 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003271
3272 return 1;
3273 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003274 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003275 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003276 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003277 fd_delete(t->srv_fd);
3278 if (t->srv)
3279 t->srv->cur_sess--;
3280 //close(t->srv_fd);
3281 t->srv_state = SV_STCLOSE;
3282 if (!(t->flags & SN_ERR_MASK))
3283 t->flags |= SN_ERR_SRVTO;
3284 if (!(t->flags & SN_FINST_MASK))
3285 t->flags |= SN_FINST_D;
3286 /* We used to have a free connection slot. Since we'll never use it,
3287 * we have to inform the server that it may be used by another session.
3288 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003289 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003290 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003291
3292 return 1;
3293 }
3294 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003295 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3296 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003297 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003298 }
3299 }
3300 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003301 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3302 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003303 if (!tv_add_ifset(&req->wex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02003304 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003305 }
3306 }
3307 return 0;
3308 }
3309 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003310 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003311 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003312 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003313 fd_delete(t->srv_fd);
3314 if (t->srv) {
3315 t->srv->cur_sess--;
3316 t->srv->failed_resp++;
3317 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003318 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003319 //close(t->srv_fd);
3320 t->srv_state = SV_STCLOSE;
3321 if (!(t->flags & SN_ERR_MASK))
3322 t->flags |= SN_ERR_SRVCL;
3323 if (!(t->flags & SN_FINST_MASK))
3324 t->flags |= SN_FINST_D;
3325 /* We used to have a free connection slot. Since we'll never use it,
3326 * we have to inform the server that it may be used by another session.
3327 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003328 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003329 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003330
3331 return 1;
3332 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003333 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003334 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003335 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003336 fd_delete(t->srv_fd);
3337 if (t->srv)
3338 t->srv->cur_sess--;
3339 //close(t->srv_fd);
3340 t->srv_state = SV_STCLOSE;
3341 /* We used to have a free connection slot. Since we'll never use it,
3342 * we have to inform the server that it may be used by another session.
3343 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003344 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003345 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003346
3347 return 1;
3348 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003349 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003350 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003351 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003352 fd_delete(t->srv_fd);
3353 if (t->srv)
3354 t->srv->cur_sess--;
3355 //close(t->srv_fd);
3356 t->srv_state = SV_STCLOSE;
3357 if (!(t->flags & SN_ERR_MASK))
3358 t->flags |= SN_ERR_SRVTO;
3359 if (!(t->flags & SN_FINST_MASK))
3360 t->flags |= SN_FINST_D;
3361 /* We used to have a free connection slot. Since we'll never use it,
3362 * we have to inform the server that it may be used by another session.
3363 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003364 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003365 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003366
3367 return 1;
3368 }
3369 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003370 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003371 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003372 }
3373 }
3374 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003375 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003376 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02003377 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003378 }
3379 }
3380 return 0;
3381 }
3382 else { /* SV_STCLOSE : nothing to do */
3383 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3384 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003385 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003386 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003387 write(1, trash, len);
3388 }
3389 return 0;
3390 }
3391 return 0;
3392}
3393
3394
3395/*
3396 * Produces data for the session <s> depending on its source. Expects to be
3397 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3398 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3399 * session, which it uses to keep on being called when there is free space in
3400 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3401 * if it changes the session state from CL_STSHUTR, otherwise 0.
3402 */
3403int produce_content(struct session *s)
3404{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003405 if (s->data_source == DATA_SRC_NONE) {
3406 s->flags &= ~SN_SELF_GEN;
3407 return 1;
3408 }
3409 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003410 /* dump server statistics */
Willy Tarreau55bb8452007-10-17 18:44:57 +02003411 int ret = stats_dump_http(s, s->be->uri_auth,
3412 (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML);
Willy Tarreau91861262007-10-17 17:06:05 +02003413 if (ret >= 0)
3414 return ret;
3415 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003416 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003417
Willy Tarreau91861262007-10-17 17:06:05 +02003418 /* unknown data source or internal error */
3419 s->txn.status = 500;
3420 client_retnclose(s, error_message(s, HTTP_ERR_500));
3421 if (!(s->flags & SN_ERR_MASK))
3422 s->flags |= SN_ERR_PRXCOND;
3423 if (!(s->flags & SN_FINST_MASK))
3424 s->flags |= SN_FINST_R;
3425 s->flags &= ~SN_SELF_GEN;
3426 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003427}
3428
3429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003430/* Iterate the same filter through all request headers.
3431 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003432 * Since it can manage the switch to another backend, it updates the per-proxy
3433 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003434 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003435int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003436{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003437 char term;
3438 char *cur_ptr, *cur_end, *cur_next;
3439 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003440 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003441 struct hdr_idx_elem *cur_hdr;
3442 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003444 last_hdr = 0;
3445
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003446 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003447 old_idx = 0;
3448
3449 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003450 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003451 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003452 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003453 (exp->action == ACT_ALLOW ||
3454 exp->action == ACT_DENY ||
3455 exp->action == ACT_TARPIT))
3456 return 0;
3457
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003458 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003459 if (!cur_idx)
3460 break;
3461
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003462 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003463 cur_ptr = cur_next;
3464 cur_end = cur_ptr + cur_hdr->len;
3465 cur_next = cur_end + cur_hdr->cr + 1;
3466
3467 /* Now we have one header between cur_ptr and cur_end,
3468 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003469 */
3470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003471 /* The annoying part is that pattern matching needs
3472 * that we modify the contents to null-terminate all
3473 * strings before testing them.
3474 */
3475
3476 term = *cur_end;
3477 *cur_end = '\0';
3478
3479 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3480 switch (exp->action) {
3481 case ACT_SETBE:
3482 /* It is not possible to jump a second time.
3483 * FIXME: should we return an HTTP/500 here so that
3484 * the admin knows there's a problem ?
3485 */
3486 if (t->be != t->fe)
3487 break;
3488
3489 /* Swithing Proxy */
3490 t->be = (struct proxy *) exp->replace;
3491
3492 /* right now, the backend switch is not too much complicated
3493 * because we have associated req_cap and rsp_cap to the
3494 * frontend, and the beconn will be updated later.
3495 */
3496
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003497 t->rep->rto = t->req->wto = t->be->timeout.server;
3498 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003499 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003500 last_hdr = 1;
3501 break;
3502
3503 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003504 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003505 last_hdr = 1;
3506 break;
3507
3508 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003509 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003510 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003511 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003512 break;
3513
3514 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003515 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003516 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003517 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003518 break;
3519
3520 case ACT_REPLACE:
3521 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3522 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3523 /* FIXME: if the user adds a newline in the replacement, the
3524 * index will not be recalculated for now, and the new line
3525 * will not be counted as a new header.
3526 */
3527
3528 cur_end += delta;
3529 cur_next += delta;
3530 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003531 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003532 break;
3533
3534 case ACT_REMOVE:
3535 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3536 cur_next += delta;
3537
3538 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003539 txn->req.eoh += delta;
3540 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3541 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003542 cur_hdr->len = 0;
3543 cur_end = NULL; /* null-term has been rewritten */
3544 break;
3545
3546 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003547 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003548 if (cur_end)
3549 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003551 /* keep the link from this header to next one in case of later
3552 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003553 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003554 old_idx = cur_idx;
3555 }
3556 return 0;
3557}
3558
3559
3560/* Apply the filter to the request line.
3561 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3562 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003563 * Since it can manage the switch to another backend, it updates the per-proxy
3564 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003565 */
3566int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3567{
3568 char term;
3569 char *cur_ptr, *cur_end;
3570 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003571 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003572 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003573
Willy Tarreau58f10d72006-12-04 02:26:12 +01003574
Willy Tarreau3d300592007-03-18 18:34:41 +01003575 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003576 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003577 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003578 (exp->action == ACT_ALLOW ||
3579 exp->action == ACT_DENY ||
3580 exp->action == ACT_TARPIT))
3581 return 0;
3582 else if (exp->action == ACT_REMOVE)
3583 return 0;
3584
3585 done = 0;
3586
Willy Tarreau9cdde232007-05-02 20:58:19 +02003587 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003588 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003589
3590 /* Now we have the request line between cur_ptr and cur_end */
3591
3592 /* The annoying part is that pattern matching needs
3593 * that we modify the contents to null-terminate all
3594 * strings before testing them.
3595 */
3596
3597 term = *cur_end;
3598 *cur_end = '\0';
3599
3600 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3601 switch (exp->action) {
3602 case ACT_SETBE:
3603 /* It is not possible to jump a second time.
3604 * FIXME: should we return an HTTP/500 here so that
3605 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003606 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003607 if (t->be != t->fe)
3608 break;
3609
3610 /* Swithing Proxy */
3611 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003612
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003613 /* right now, the backend switch is not too much complicated
3614 * because we have associated req_cap and rsp_cap to the
3615 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003616 */
3617
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003618 t->rep->rto = t->req->wto = t->be->timeout.server;
3619 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003620 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003621 done = 1;
3622 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003623
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003624 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003625 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003626 done = 1;
3627 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003629 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003630 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003631 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003632 done = 1;
3633 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003635 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003636 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003637 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003638 done = 1;
3639 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003641 case ACT_REPLACE:
3642 *cur_end = term; /* restore the string terminator */
3643 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3644 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3645 /* FIXME: if the user adds a newline in the replacement, the
3646 * index will not be recalculated for now, and the new line
3647 * will not be counted as a new header.
3648 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003649
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003650 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003651 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003652
Willy Tarreau9cdde232007-05-02 20:58:19 +02003653 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003654 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003655 HTTP_MSG_RQMETH,
3656 cur_ptr, cur_end + 1,
3657 NULL, NULL);
3658 if (unlikely(!cur_end))
3659 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003661 /* we have a full request and we know that we have either a CR
3662 * or an LF at <ptr>.
3663 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003664 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3665 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003666 /* there is no point trying this regex on headers */
3667 return 1;
3668 }
3669 }
3670 *cur_end = term; /* restore the string terminator */
3671 return done;
3672}
Willy Tarreau97de6242006-12-27 17:18:38 +01003673
Willy Tarreau58f10d72006-12-04 02:26:12 +01003674
Willy Tarreau58f10d72006-12-04 02:26:12 +01003675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003676/*
3677 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3678 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003679 * unparsable request. Since it can manage the switch to another backend, it
3680 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003681 */
3682int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3683{
Willy Tarreau3d300592007-03-18 18:34:41 +01003684 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003685 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003686 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003687 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003689 /*
3690 * The interleaving of transformations and verdicts
3691 * makes it difficult to decide to continue or stop
3692 * the evaluation.
3693 */
3694
Willy Tarreau3d300592007-03-18 18:34:41 +01003695 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003696 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3697 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3698 exp = exp->next;
3699 continue;
3700 }
3701
3702 /* Apply the filter to the request line. */
3703 ret = apply_filter_to_req_line(t, req, exp);
3704 if (unlikely(ret < 0))
3705 return -1;
3706
3707 if (likely(ret == 0)) {
3708 /* The filter did not match the request, it can be
3709 * iterated through all headers.
3710 */
3711 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003712 }
3713 exp = exp->next;
3714 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003715 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003716}
3717
3718
Willy Tarreaua15645d2007-03-18 16:22:39 +01003719
Willy Tarreau58f10d72006-12-04 02:26:12 +01003720/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003721 * Manage client-side cookie. It can impact performance by about 2% so it is
3722 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003723 */
3724void manage_client_side_cookies(struct session *t, struct buffer *req)
3725{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003726 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003727 char *p1, *p2, *p3, *p4;
3728 char *del_colon, *del_cookie, *colon;
3729 int app_cookies;
3730
3731 appsess *asession_temp = NULL;
3732 appsess local_asession;
3733
3734 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003735 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003736
Willy Tarreau2a324282006-12-05 00:05:46 +01003737 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003738 * we start with the start line.
3739 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003740 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003741 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003742
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003743 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003744 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003745 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003746
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003747 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003748 cur_ptr = cur_next;
3749 cur_end = cur_ptr + cur_hdr->len;
3750 cur_next = cur_end + cur_hdr->cr + 1;
3751
3752 /* We have one full header between cur_ptr and cur_end, and the
3753 * next header starts at cur_next. We're only interested in
3754 * "Cookie:" headers.
3755 */
3756
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003757 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3758 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003759 old_idx = cur_idx;
3760 continue;
3761 }
3762
3763 /* Now look for cookies. Conforming to RFC2109, we have to support
3764 * attributes whose name begin with a '$', and associate them with
3765 * the right cookie, if we want to delete this cookie.
3766 * So there are 3 cases for each cookie read :
3767 * 1) it's a special attribute, beginning with a '$' : ignore it.
3768 * 2) it's a server id cookie that we *MAY* want to delete : save
3769 * some pointers on it (last semi-colon, beginning of cookie...)
3770 * 3) it's an application cookie : we *MAY* have to delete a previous
3771 * "special" cookie.
3772 * At the end of loop, if a "special" cookie remains, we may have to
3773 * remove it. If no application cookie persists in the header, we
3774 * *MUST* delete it
3775 */
3776
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003777 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003778
Willy Tarreau58f10d72006-12-04 02:26:12 +01003779 /* del_cookie == NULL => nothing to be deleted */
3780 del_colon = del_cookie = NULL;
3781 app_cookies = 0;
3782
3783 while (p1 < cur_end) {
3784 /* skip spaces and colons, but keep an eye on these ones */
3785 while (p1 < cur_end) {
3786 if (*p1 == ';' || *p1 == ',')
3787 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003788 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003789 break;
3790 p1++;
3791 }
3792
3793 if (p1 == cur_end)
3794 break;
3795
3796 /* p1 is at the beginning of the cookie name */
3797 p2 = p1;
3798 while (p2 < cur_end && *p2 != '=')
3799 p2++;
3800
3801 if (p2 == cur_end)
3802 break;
3803
3804 p3 = p2 + 1; /* skips the '=' sign */
3805 if (p3 == cur_end)
3806 break;
3807
3808 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003809 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003810 p4++;
3811
3812 /* here, we have the cookie name between p1 and p2,
3813 * and its value between p3 and p4.
3814 * we can process it :
3815 *
3816 * Cookie: NAME=VALUE;
3817 * | || || |
3818 * | || || +--> p4
3819 * | || |+-------> p3
3820 * | || +--------> p2
3821 * | |+------------> p1
3822 * | +-------------> colon
3823 * +--------------------> cur_ptr
3824 */
3825
3826 if (*p1 == '$') {
3827 /* skip this one */
3828 }
3829 else {
3830 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003831 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003832 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003833 (p4 - p1 >= t->fe->capture_namelen) &&
3834 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003835 int log_len = p4 - p1;
3836
Willy Tarreau086b3b42007-05-13 21:45:51 +02003837 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003838 Alert("HTTP logging : out of memory.\n");
3839 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003840 if (log_len > t->fe->capture_len)
3841 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003842 memcpy(txn->cli_cookie, p1, log_len);
3843 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003844 }
3845 }
3846
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003847 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3848 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003849 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003850 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003851 char *delim;
3852
3853 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3854 * have the server ID betweek p3 and delim, and the original cookie between
3855 * delim+1 and p4. Otherwise, delim==p4 :
3856 *
3857 * Cookie: NAME=SRV~VALUE;
3858 * | || || | |
3859 * | || || | +--> p4
3860 * | || || +--------> delim
3861 * | || |+-----------> p3
3862 * | || +------------> p2
3863 * | |+----------------> p1
3864 * | +-----------------> colon
3865 * +------------------------> cur_ptr
3866 */
3867
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003868 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003869 for (delim = p3; delim < p4; delim++)
3870 if (*delim == COOKIE_DELIM)
3871 break;
3872 }
3873 else
3874 delim = p4;
3875
3876
3877 /* Here, we'll look for the first running server which supports the cookie.
3878 * This allows to share a same cookie between several servers, for example
3879 * to dedicate backup servers to specific servers only.
3880 * However, to prevent clients from sticking to cookie-less backup server
3881 * when they have incidentely learned an empty cookie, we simply ignore
3882 * empty cookies and mark them as invalid.
3883 */
3884 if (delim == p3)
3885 srv = NULL;
3886
3887 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003888 if (srv->cookie && (srv->cklen == delim - p3) &&
3889 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003890 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003891 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003892 txn->flags &= ~TX_CK_MASK;
3893 txn->flags |= TX_CK_VALID;
3894 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003895 t->srv = srv;
3896 break;
3897 } else {
3898 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003899 txn->flags &= ~TX_CK_MASK;
3900 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003901 }
3902 }
3903 srv = srv->next;
3904 }
3905
Willy Tarreau3d300592007-03-18 18:34:41 +01003906 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003907 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003908 txn->flags &= ~TX_CK_MASK;
3909 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003910 }
3911
3912 /* depending on the cookie mode, we may have to either :
3913 * - delete the complete cookie if we're in insert+indirect mode, so that
3914 * the server never sees it ;
3915 * - remove the server id from the cookie value, and tag the cookie as an
3916 * application cookie so that it does not get accidentely removed later,
3917 * if we're in cookie prefix mode
3918 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003919 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003920 int delta; /* negative */
3921
3922 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3923 p4 += delta;
3924 cur_end += delta;
3925 cur_next += delta;
3926 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003927 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003928
3929 del_cookie = del_colon = NULL;
3930 app_cookies++; /* protect the header from deletion */
3931 }
3932 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003933 (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 +01003934 del_cookie = p1;
3935 del_colon = colon;
3936 }
3937 } else {
3938 /* now we know that we must keep this cookie since it's
3939 * not ours. But if we wanted to delete our cookie
3940 * earlier, we cannot remove the complete header, but we
3941 * can remove the previous block itself.
3942 */
3943 app_cookies++;
3944
3945 if (del_cookie != NULL) {
3946 int delta; /* negative */
3947
3948 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3949 p4 += delta;
3950 cur_end += delta;
3951 cur_next += delta;
3952 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003953 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003954 del_cookie = del_colon = NULL;
3955 }
3956 }
3957
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003958 if ((t->be->appsession_name != NULL) &&
3959 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003960 /* first, let's see if the cookie is our appcookie*/
3961
3962 /* Cool... it's the right one */
3963
3964 asession_temp = &local_asession;
3965
Willy Tarreau63963c62007-05-13 21:29:55 +02003966 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003967 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3968 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3969 return;
3970 }
3971
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003972 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3973 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003974 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003975
Willy Tarreau58f10d72006-12-04 02:26:12 +01003976 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003977 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3978 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003979 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003980 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003981 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003982 Alert("Not enough memory process_cli():asession:calloc().\n");
3983 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3984 return;
3985 }
3986
3987 asession_temp->sessid = local_asession.sessid;
3988 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02003989 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003990 } else {
3991 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003992 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003993 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003994 if (asession_temp->serverid == NULL) {
3995 Alert("Found Application Session without matching server.\n");
3996 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003997 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003998 while (srv) {
3999 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004000 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004001 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004002 txn->flags &= ~TX_CK_MASK;
4003 txn->flags |= TX_CK_VALID;
4004 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004005 t->srv = srv;
4006 break;
4007 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004008 txn->flags &= ~TX_CK_MASK;
4009 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004010 }
4011 }
4012 srv = srv->next;
4013 }/* end while(srv) */
4014 }/* end else if server == NULL */
4015
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004016 tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004017 }/* end if ((t->proxy->appsession_name != NULL) ... */
4018 }
4019
4020 /* we'll have to look for another cookie ... */
4021 p1 = p4;
4022 } /* while (p1 < cur_end) */
4023
4024 /* There's no more cookie on this line.
4025 * We may have marked the last one(s) for deletion.
4026 * We must do this now in two ways :
4027 * - if there is no app cookie, we simply delete the header ;
4028 * - if there are app cookies, we must delete the end of the
4029 * string properly, including the colon/semi-colon before
4030 * the cookie name.
4031 */
4032 if (del_cookie != NULL) {
4033 int delta;
4034 if (app_cookies) {
4035 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4036 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004037 cur_hdr->len += delta;
4038 } else {
4039 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004040
4041 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004042 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4043 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004044 cur_hdr->len = 0;
4045 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004046 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004047 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004048 }
4049
4050 /* keep the link from this header to next one */
4051 old_idx = cur_idx;
4052 } /* end of cookie processing on this header */
4053}
4054
4055
Willy Tarreaua15645d2007-03-18 16:22:39 +01004056/* Iterate the same filter through all response headers contained in <rtr>.
4057 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4058 */
4059int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4060{
4061 char term;
4062 char *cur_ptr, *cur_end, *cur_next;
4063 int cur_idx, old_idx, last_hdr;
4064 struct http_txn *txn = &t->txn;
4065 struct hdr_idx_elem *cur_hdr;
4066 int len, delta;
4067
4068 last_hdr = 0;
4069
4070 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4071 old_idx = 0;
4072
4073 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004074 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004075 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004076 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004077 (exp->action == ACT_ALLOW ||
4078 exp->action == ACT_DENY))
4079 return 0;
4080
4081 cur_idx = txn->hdr_idx.v[old_idx].next;
4082 if (!cur_idx)
4083 break;
4084
4085 cur_hdr = &txn->hdr_idx.v[cur_idx];
4086 cur_ptr = cur_next;
4087 cur_end = cur_ptr + cur_hdr->len;
4088 cur_next = cur_end + cur_hdr->cr + 1;
4089
4090 /* Now we have one header between cur_ptr and cur_end,
4091 * and the next header starts at cur_next.
4092 */
4093
4094 /* The annoying part is that pattern matching needs
4095 * that we modify the contents to null-terminate all
4096 * strings before testing them.
4097 */
4098
4099 term = *cur_end;
4100 *cur_end = '\0';
4101
4102 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4103 switch (exp->action) {
4104 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004105 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004106 last_hdr = 1;
4107 break;
4108
4109 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004110 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004111 last_hdr = 1;
4112 break;
4113
4114 case ACT_REPLACE:
4115 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4116 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4117 /* FIXME: if the user adds a newline in the replacement, the
4118 * index will not be recalculated for now, and the new line
4119 * will not be counted as a new header.
4120 */
4121
4122 cur_end += delta;
4123 cur_next += delta;
4124 cur_hdr->len += delta;
4125 txn->rsp.eoh += delta;
4126 break;
4127
4128 case ACT_REMOVE:
4129 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4130 cur_next += delta;
4131
4132 /* FIXME: this should be a separate function */
4133 txn->rsp.eoh += delta;
4134 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4135 txn->hdr_idx.used--;
4136 cur_hdr->len = 0;
4137 cur_end = NULL; /* null-term has been rewritten */
4138 break;
4139
4140 }
4141 }
4142 if (cur_end)
4143 *cur_end = term; /* restore the string terminator */
4144
4145 /* keep the link from this header to next one in case of later
4146 * removal of next header.
4147 */
4148 old_idx = cur_idx;
4149 }
4150 return 0;
4151}
4152
4153
4154/* Apply the filter to the status line in the response buffer <rtr>.
4155 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4156 * or -1 if a replacement resulted in an invalid status line.
4157 */
4158int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4159{
4160 char term;
4161 char *cur_ptr, *cur_end;
4162 int done;
4163 struct http_txn *txn = &t->txn;
4164 int len, delta;
4165
4166
Willy Tarreau3d300592007-03-18 18:34:41 +01004167 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004168 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004169 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004170 (exp->action == ACT_ALLOW ||
4171 exp->action == ACT_DENY))
4172 return 0;
4173 else if (exp->action == ACT_REMOVE)
4174 return 0;
4175
4176 done = 0;
4177
Willy Tarreau9cdde232007-05-02 20:58:19 +02004178 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004179 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4180
4181 /* Now we have the status line between cur_ptr and cur_end */
4182
4183 /* The annoying part is that pattern matching needs
4184 * that we modify the contents to null-terminate all
4185 * strings before testing them.
4186 */
4187
4188 term = *cur_end;
4189 *cur_end = '\0';
4190
4191 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4192 switch (exp->action) {
4193 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004194 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004195 done = 1;
4196 break;
4197
4198 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004199 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004200 done = 1;
4201 break;
4202
4203 case ACT_REPLACE:
4204 *cur_end = term; /* restore the string terminator */
4205 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4206 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4207 /* FIXME: if the user adds a newline in the replacement, the
4208 * index will not be recalculated for now, and the new line
4209 * will not be counted as a new header.
4210 */
4211
4212 txn->rsp.eoh += delta;
4213 cur_end += delta;
4214
Willy Tarreau9cdde232007-05-02 20:58:19 +02004215 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004216 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004217 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004218 cur_ptr, cur_end + 1,
4219 NULL, NULL);
4220 if (unlikely(!cur_end))
4221 return -1;
4222
4223 /* we have a full respnse and we know that we have either a CR
4224 * or an LF at <ptr>.
4225 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004226 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004227 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4228 /* there is no point trying this regex on headers */
4229 return 1;
4230 }
4231 }
4232 *cur_end = term; /* restore the string terminator */
4233 return done;
4234}
4235
4236
4237
4238/*
4239 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4240 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4241 * unparsable response.
4242 */
4243int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4244{
Willy Tarreau3d300592007-03-18 18:34:41 +01004245 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004246 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004247 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004248 int ret;
4249
4250 /*
4251 * The interleaving of transformations and verdicts
4252 * makes it difficult to decide to continue or stop
4253 * the evaluation.
4254 */
4255
Willy Tarreau3d300592007-03-18 18:34:41 +01004256 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004257 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4258 exp->action == ACT_PASS)) {
4259 exp = exp->next;
4260 continue;
4261 }
4262
4263 /* Apply the filter to the status line. */
4264 ret = apply_filter_to_sts_line(t, rtr, exp);
4265 if (unlikely(ret < 0))
4266 return -1;
4267
4268 if (likely(ret == 0)) {
4269 /* The filter did not match the response, it can be
4270 * iterated through all headers.
4271 */
4272 apply_filter_to_resp_headers(t, rtr, exp);
4273 }
4274 exp = exp->next;
4275 }
4276 return 0;
4277}
4278
4279
4280
4281/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004282 * Manage server-side cookies. It can impact performance by about 2% so it is
4283 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004284 */
4285void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4286{
4287 struct http_txn *txn = &t->txn;
4288 char *p1, *p2, *p3, *p4;
4289
4290 appsess *asession_temp = NULL;
4291 appsess local_asession;
4292
4293 char *cur_ptr, *cur_end, *cur_next;
4294 int cur_idx, old_idx, delta;
4295
Willy Tarreaua15645d2007-03-18 16:22:39 +01004296 /* Iterate through the headers.
4297 * we start with the start line.
4298 */
4299 old_idx = 0;
4300 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4301
4302 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4303 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004304 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004305
4306 cur_hdr = &txn->hdr_idx.v[cur_idx];
4307 cur_ptr = cur_next;
4308 cur_end = cur_ptr + cur_hdr->len;
4309 cur_next = cur_end + cur_hdr->cr + 1;
4310
4311 /* We have one full header between cur_ptr and cur_end, and the
4312 * next header starts at cur_next. We're only interested in
4313 * "Cookie:" headers.
4314 */
4315
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004316 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4317 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004318 old_idx = cur_idx;
4319 continue;
4320 }
4321
4322 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004323 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004324
4325
4326 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004327 if (t->be->cookie_name == NULL &&
4328 t->be->appsession_name == NULL &&
4329 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004330 return;
4331
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004332 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004333
4334 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004335 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4336 break;
4337
4338 /* p1 is at the beginning of the cookie name */
4339 p2 = p1;
4340
4341 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4342 p2++;
4343
4344 if (p2 == cur_end || *p2 == ';') /* next cookie */
4345 break;
4346
4347 p3 = p2 + 1; /* skip the '=' sign */
4348 if (p3 == cur_end)
4349 break;
4350
4351 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004352 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004353 p4++;
4354
4355 /* here, we have the cookie name between p1 and p2,
4356 * and its value between p3 and p4.
4357 * we can process it.
4358 */
4359
4360 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004361 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004362 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004363 (p4 - p1 >= t->be->capture_namelen) &&
4364 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004365 int log_len = p4 - p1;
4366
Willy Tarreau086b3b42007-05-13 21:45:51 +02004367 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004368 Alert("HTTP logging : out of memory.\n");
4369 }
4370
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004371 if (log_len > t->be->capture_len)
4372 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004373 memcpy(txn->srv_cookie, p1, log_len);
4374 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004375 }
4376
4377 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004378 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4379 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004380 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004381 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004382
4383 /* If the cookie is in insert mode on a known server, we'll delete
4384 * this occurrence because we'll insert another one later.
4385 * We'll delete it too if the "indirect" option is set and we're in
4386 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004387 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4388 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004389 /* this header must be deleted */
4390 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4391 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4392 txn->hdr_idx.used--;
4393 cur_hdr->len = 0;
4394 cur_next += delta;
4395 txn->rsp.eoh += delta;
4396
Willy Tarreau3d300592007-03-18 18:34:41 +01004397 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004398 }
4399 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004400 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004401 /* replace bytes p3->p4 with the cookie name associated
4402 * with this server since we know it.
4403 */
4404 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4405 cur_hdr->len += delta;
4406 cur_next += delta;
4407 txn->rsp.eoh += delta;
4408
Willy Tarreau3d300592007-03-18 18:34:41 +01004409 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004410 }
4411 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004412 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004413 /* insert the cookie name associated with this server
4414 * before existing cookie, and insert a delimitor between them..
4415 */
4416 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4417 cur_hdr->len += delta;
4418 cur_next += delta;
4419 txn->rsp.eoh += delta;
4420
4421 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004422 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004423 }
4424 }
4425 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004426 else if ((t->be->appsession_name != NULL) &&
4427 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004428
4429 /* Cool... it's the right one */
4430
4431 size_t server_id_len = strlen(t->srv->id) + 1;
4432 asession_temp = &local_asession;
4433
Willy Tarreau63963c62007-05-13 21:29:55 +02004434 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004435 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4436 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4437 return;
4438 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004439 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4440 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004441 asession_temp->serverid = NULL;
4442
4443 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004444 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004445 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004446 Alert("Not enough Memory process_srv():asession:calloc().\n");
4447 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4448 return;
4449 }
4450 asession_temp->sessid = local_asession.sessid;
4451 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004452 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4453 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004454 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004455 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004456 }
4457
Willy Tarreaua15645d2007-03-18 16:22:39 +01004458 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004459 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004460 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4461 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4462 return;
4463 }
4464 asession_temp->serverid[0] = '\0';
4465 }
4466
4467 if (asession_temp->serverid[0] == '\0')
4468 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4469
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004470 tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004471
4472#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004473 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004474#endif
4475 }/* end if ((t->proxy->appsession_name != NULL) ... */
4476 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4477 } /* we're now at the end of the cookie value */
4478
4479 /* keep the link from this header to next one */
4480 old_idx = cur_idx;
4481 } /* end of cookie processing on this header */
4482}
4483
4484
4485
4486/*
4487 * Check if response is cacheable or not. Updates t->flags.
4488 */
4489void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4490{
4491 struct http_txn *txn = &t->txn;
4492 char *p1, *p2;
4493
4494 char *cur_ptr, *cur_end, *cur_next;
4495 int cur_idx;
4496
Willy Tarreau5df51872007-11-25 16:20:08 +01004497 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004498 return;
4499
4500 /* Iterate through the headers.
4501 * we start with the start line.
4502 */
4503 cur_idx = 0;
4504 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4505
4506 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4507 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004508 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004509
4510 cur_hdr = &txn->hdr_idx.v[cur_idx];
4511 cur_ptr = cur_next;
4512 cur_end = cur_ptr + cur_hdr->len;
4513 cur_next = cur_end + cur_hdr->cr + 1;
4514
4515 /* We have one full header between cur_ptr and cur_end, and the
4516 * next header starts at cur_next. We're only interested in
4517 * "Cookie:" headers.
4518 */
4519
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004520 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4521 if (val) {
4522 if ((cur_end - (cur_ptr + val) >= 8) &&
4523 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4524 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4525 return;
4526 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004527 }
4528
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004529 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4530 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004531 continue;
4532
4533 /* OK, right now we know we have a cache-control header at cur_ptr */
4534
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004535 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004536
4537 if (p1 >= cur_end) /* no more info */
4538 continue;
4539
4540 /* p1 is at the beginning of the value */
4541 p2 = p1;
4542
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004543 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004544 p2++;
4545
4546 /* we have a complete value between p1 and p2 */
4547 if (p2 < cur_end && *p2 == '=') {
4548 /* we have something of the form no-cache="set-cookie" */
4549 if ((cur_end - p1 >= 21) &&
4550 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4551 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004552 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004553 continue;
4554 }
4555
4556 /* OK, so we know that either p2 points to the end of string or to a comma */
4557 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4558 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4559 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4560 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004561 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004562 return;
4563 }
4564
4565 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004566 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004567 continue;
4568 }
4569 }
4570}
4571
4572
Willy Tarreau58f10d72006-12-04 02:26:12 +01004573/*
4574 * Try to retrieve a known appsession in the URI, then the associated server.
4575 * If the server is found, it's assigned to the session.
4576 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004577void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004578{
Willy Tarreau3d300592007-03-18 18:34:41 +01004579 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004580 appsess *asession_temp = NULL;
4581 appsess local_asession;
4582 char *request_line;
4583
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004584 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004585 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004586 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004587 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004588 return;
4589
4590 /* skip ';' */
4591 request_line++;
4592
4593 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004594 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004595 return;
4596
4597 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004598 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004599
4600 /* First try if we already have an appsession */
4601 asession_temp = &local_asession;
4602
Willy Tarreau63963c62007-05-13 21:29:55 +02004603 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004604 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4605 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4606 return;
4607 }
4608
4609 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004610 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4611 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004612 asession_temp->serverid = NULL;
4613
4614 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004615 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004616 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004617 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004618 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004619 Alert("Not enough memory process_cli():asession:calloc().\n");
4620 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4621 return;
4622 }
4623 asession_temp->sessid = local_asession.sessid;
4624 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004625 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004626 }
4627 else {
4628 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004629 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004630 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004631
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004632 tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004633 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004634
Willy Tarreau58f10d72006-12-04 02:26:12 +01004635#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004636 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004637#endif
4638 if (asession_temp->serverid == NULL) {
4639 Alert("Found Application Session without matching server.\n");
4640 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004641 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004642 while (srv) {
4643 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004644 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004645 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004646 txn->flags &= ~TX_CK_MASK;
4647 txn->flags |= TX_CK_VALID;
4648 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004649 t->srv = srv;
4650 break;
4651 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004652 txn->flags &= ~TX_CK_MASK;
4653 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004654 }
4655 }
4656 srv = srv->next;
4657 }
4658 }
4659}
4660
4661
Willy Tarreaub2513902006-12-17 14:52:38 +01004662/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004663 * In a GET or HEAD request, check if the requested URI matches the stats uri
4664 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004665 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004666 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004667 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004668 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004669 *
4670 * Returns 1 if the session's state changes, otherwise 0.
4671 */
4672int stats_check_uri_auth(struct session *t, struct proxy *backend)
4673{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004674 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004675 struct uri_auth *uri_auth = backend->uri_auth;
4676 struct user_auth *user;
4677 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004678 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004679
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004680 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004681 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004682 return 0;
4683
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004684 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004685
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004686 /* the URI is in h */
4687 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004688 return 0;
4689
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004690 h += uri_auth->uri_len;
4691 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4692 if (memcmp(h, ";up", 3) == 0) {
4693 t->flags |= SN_STAT_HIDEDWN;
4694 break;
4695 }
4696 h++;
4697 }
4698
4699 if (uri_auth->refresh) {
4700 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4701 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4702 if (memcmp(h, ";norefresh", 10) == 0) {
4703 t->flags |= SN_STAT_NORFRSH;
4704 break;
4705 }
4706 h++;
4707 }
4708 }
4709
Willy Tarreau55bb8452007-10-17 18:44:57 +02004710 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4711 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4712 if (memcmp(h, ";csv", 4) == 0) {
4713 t->flags |= SN_STAT_FMTCSV;
4714 break;
4715 }
4716 h++;
4717 }
4718
Willy Tarreaub2513902006-12-17 14:52:38 +01004719 /* we are in front of a interceptable URI. Let's check
4720 * if there's an authentication and if it's valid.
4721 */
4722 user = uri_auth->users;
4723 if (!user) {
4724 /* no user auth required, it's OK */
4725 authenticated = 1;
4726 } else {
4727 authenticated = 0;
4728
4729 /* a user list is defined, we have to check.
4730 * skip 21 chars for "Authorization: Basic ".
4731 */
4732
4733 /* FIXME: this should move to an earlier place */
4734 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004735 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4736 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4737 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004738 if (len > 14 &&
4739 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004740 txn->auth_hdr.str = h;
4741 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004742 break;
4743 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004744 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004745 }
4746
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004747 if (txn->auth_hdr.len < 21 ||
4748 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004749 user = NULL;
4750
4751 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004752 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4753 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004754 user->user_pwd, user->user_len)) {
4755 authenticated = 1;
4756 break;
4757 }
4758 user = user->next;
4759 }
4760 }
4761
4762 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004763 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004764
4765 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004766 msg.str = trash;
4767 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004768 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004769 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004770 if (!(t->flags & SN_ERR_MASK))
4771 t->flags |= SN_ERR_PRXCOND;
4772 if (!(t->flags & SN_FINST_MASK))
4773 t->flags |= SN_FINST_R;
4774 return 1;
4775 }
4776
4777 /* The request is valid, the user is authenticate. Let's start sending
4778 * data.
4779 */
4780 t->cli_state = CL_STSHUTR;
4781 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004782 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01004783 t->data_source = DATA_SRC_STATS;
4784 t->data_state = DATA_ST_INIT;
4785 produce_content(t);
4786 return 1;
4787}
4788
4789
Willy Tarreaubaaee002006-06-26 02:48:02 +02004790/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004791 * Print a debug line with a header
4792 */
4793void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4794{
4795 int len, max;
4796 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4797 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4798 max = end - start;
4799 UBOUND(max, sizeof(trash) - len - 1);
4800 len += strlcpy2(trash + len, start, max + 1);
4801 trash[len++] = '\n';
4802 write(1, trash, len);
4803}
4804
4805
Willy Tarreau8797c062007-05-07 00:55:35 +02004806/************************************************************************/
4807/* The code below is dedicated to ACL parsing and matching */
4808/************************************************************************/
4809
4810
4811
4812
4813/* 1. Check on METHOD
4814 * We use the pre-parsed method if it is known, and store its number as an
4815 * integer. If it is unknown, we use the pointer and the length.
4816 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004817static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004818{
4819 int len, meth;
4820
Willy Tarreauae8b7962007-06-09 23:10:04 +02004821 len = strlen(*text);
4822 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004823
4824 pattern->val.i = meth;
4825 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004826 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004827 if (!pattern->ptr.str)
4828 return 0;
4829 pattern->len = len;
4830 }
4831 return 1;
4832}
4833
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004834static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004835acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4836 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004837{
4838 int meth;
4839 struct http_txn *txn = l7;
4840
Willy Tarreauc11416f2007-06-17 16:58:38 +02004841 if (txn->req.msg_state != HTTP_MSG_BODY)
4842 return 0;
4843
Willy Tarreau8797c062007-05-07 00:55:35 +02004844 meth = txn->meth;
4845 test->i = meth;
4846 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004847 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4848 /* ensure the indexes are not affected */
4849 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004850 test->len = txn->req.sl.rq.m_l;
4851 test->ptr = txn->req.sol;
4852 }
4853 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4854 return 1;
4855}
4856
4857static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4858{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004859 int icase;
4860
Willy Tarreau8797c062007-05-07 00:55:35 +02004861 if (test->i != pattern->val.i)
4862 return 0;
4863
4864 if (test->i != HTTP_METH_OTHER)
4865 return 1;
4866
4867 /* Other method, we must compare the strings */
4868 if (pattern->len != test->len)
4869 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004870
4871 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4872 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4873 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau8797c062007-05-07 00:55:35 +02004874 return 0;
4875 return 1;
4876}
4877
4878/* 2. Check on Request/Status Version
4879 * We simply compare strings here.
4880 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004881static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004882{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004883 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004884 if (!pattern->ptr.str)
4885 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004886 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004887 return 1;
4888}
4889
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004890static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004891acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4892 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004893{
4894 struct http_txn *txn = l7;
4895 char *ptr;
4896 int len;
4897
Willy Tarreauc11416f2007-06-17 16:58:38 +02004898 if (txn->req.msg_state != HTTP_MSG_BODY)
4899 return 0;
4900
Willy Tarreau8797c062007-05-07 00:55:35 +02004901 len = txn->req.sl.rq.v_l;
4902 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4903
4904 while ((len-- > 0) && (*ptr++ != '/'));
4905 if (len <= 0)
4906 return 0;
4907
4908 test->ptr = ptr;
4909 test->len = len;
4910
4911 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4912 return 1;
4913}
4914
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004915static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004916acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4917 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004918{
4919 struct http_txn *txn = l7;
4920 char *ptr;
4921 int len;
4922
Willy Tarreauc11416f2007-06-17 16:58:38 +02004923 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4924 return 0;
4925
Willy Tarreau8797c062007-05-07 00:55:35 +02004926 len = txn->rsp.sl.st.v_l;
4927 ptr = txn->rsp.sol;
4928
4929 while ((len-- > 0) && (*ptr++ != '/'));
4930 if (len <= 0)
4931 return 0;
4932
4933 test->ptr = ptr;
4934 test->len = len;
4935
4936 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4937 return 1;
4938}
4939
4940/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004941static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004942acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4943 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004944{
4945 struct http_txn *txn = l7;
4946 char *ptr;
4947 int len;
4948
Willy Tarreauc11416f2007-06-17 16:58:38 +02004949 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4950 return 0;
4951
Willy Tarreau8797c062007-05-07 00:55:35 +02004952 len = txn->rsp.sl.st.c_l;
4953 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4954
4955 test->i = __strl2ui(ptr, len);
4956 test->flags = ACL_TEST_F_VOL_1ST;
4957 return 1;
4958}
4959
4960/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004961static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004962acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4963 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004964{
4965 struct http_txn *txn = l7;
4966
Willy Tarreauc11416f2007-06-17 16:58:38 +02004967 if (txn->req.msg_state != HTTP_MSG_BODY)
4968 return 0;
4969 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4970 /* ensure the indexes are not affected */
4971 return 0;
4972
Willy Tarreau8797c062007-05-07 00:55:35 +02004973 test->len = txn->req.sl.rq.u_l;
4974 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4975
Willy Tarreauf3d25982007-05-08 22:45:09 +02004976 /* we do not need to set READ_ONLY because the data is in a buffer */
4977 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004978 return 1;
4979}
4980
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004981static int
4982acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4983 struct acl_expr *expr, struct acl_test *test)
4984{
4985 struct http_txn *txn = l7;
4986
4987 if (txn->req.msg_state != HTTP_MSG_BODY)
4988 return 0;
4989 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4990 /* ensure the indexes are not affected */
4991 return 0;
4992
4993 /* Parse HTTP request */
4994 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4995 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4996 test->i = AF_INET;
4997
4998 /*
4999 * If we are parsing url in frontend space, we prepare backend stage
5000 * to not parse again the same url ! optimization lazyness...
5001 */
5002 if (px->options & PR_O_HTTP_PROXY)
5003 l4->flags |= SN_ADDR_SET;
5004
5005 test->flags = ACL_TEST_F_READ_ONLY;
5006 return 1;
5007}
5008
5009static int
5010acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5011 struct acl_expr *expr, struct acl_test *test)
5012{
5013 struct http_txn *txn = l7;
5014
5015 if (txn->req.msg_state != HTTP_MSG_BODY)
5016 return 0;
5017 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5018 /* ensure the indexes are not affected */
5019 return 0;
5020
5021 /* Same optimization as url_ip */
5022 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5023 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5024
5025 if (px->options & PR_O_HTTP_PROXY)
5026 l4->flags |= SN_ADDR_SET;
5027
5028 test->flags = ACL_TEST_F_READ_ONLY;
5029 return 1;
5030}
5031
Willy Tarreauc11416f2007-06-17 16:58:38 +02005032/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5033 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5034 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005035static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005036acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005037 struct acl_expr *expr, struct acl_test *test)
5038{
5039 struct http_txn *txn = l7;
5040 struct hdr_idx *idx = &txn->hdr_idx;
5041 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005042
5043 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5044 /* search for header from the beginning */
5045 ctx->idx = 0;
5046
Willy Tarreau33a7e692007-06-10 19:45:56 +02005047 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5048 test->flags |= ACL_TEST_F_FETCH_MORE;
5049 test->flags |= ACL_TEST_F_VOL_HDR;
5050 test->len = ctx->vlen;
5051 test->ptr = (char *)ctx->line + ctx->val;
5052 return 1;
5053 }
5054
5055 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5056 test->flags |= ACL_TEST_F_VOL_HDR;
5057 return 0;
5058}
5059
Willy Tarreau33a7e692007-06-10 19:45:56 +02005060static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005061acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5062 struct acl_expr *expr, struct acl_test *test)
5063{
5064 struct http_txn *txn = l7;
5065
5066 if (txn->req.msg_state != HTTP_MSG_BODY)
5067 return 0;
5068 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5069 /* ensure the indexes are not affected */
5070 return 0;
5071
5072 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5073}
5074
5075static int
5076acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5077 struct acl_expr *expr, struct acl_test *test)
5078{
5079 struct http_txn *txn = l7;
5080
5081 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5082 return 0;
5083
5084 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5085}
5086
5087/* 6. Check on HTTP header count. The number of occurrences is returned.
5088 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5089 */
5090static int
5091acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005092 struct acl_expr *expr, struct acl_test *test)
5093{
5094 struct http_txn *txn = l7;
5095 struct hdr_idx *idx = &txn->hdr_idx;
5096 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005097 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005098
Willy Tarreau33a7e692007-06-10 19:45:56 +02005099 ctx.idx = 0;
5100 cnt = 0;
5101 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5102 cnt++;
5103
5104 test->i = cnt;
5105 test->flags = ACL_TEST_F_VOL_HDR;
5106 return 1;
5107}
5108
Willy Tarreauc11416f2007-06-17 16:58:38 +02005109static int
5110acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5111 struct acl_expr *expr, struct acl_test *test)
5112{
5113 struct http_txn *txn = l7;
5114
5115 if (txn->req.msg_state != HTTP_MSG_BODY)
5116 return 0;
5117 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5118 /* ensure the indexes are not affected */
5119 return 0;
5120
5121 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5122}
5123
5124static int
5125acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5126 struct acl_expr *expr, struct acl_test *test)
5127{
5128 struct http_txn *txn = l7;
5129
5130 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5131 return 0;
5132
5133 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5134}
5135
Willy Tarreau33a7e692007-06-10 19:45:56 +02005136/* 7. Check on HTTP header's integer value. The integer value is returned.
5137 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005138 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005139 */
5140static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005141acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005142 struct acl_expr *expr, struct acl_test *test)
5143{
5144 struct http_txn *txn = l7;
5145 struct hdr_idx *idx = &txn->hdr_idx;
5146 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005147
5148 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5149 /* search for header from the beginning */
5150 ctx->idx = 0;
5151
Willy Tarreau33a7e692007-06-10 19:45:56 +02005152 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5153 test->flags |= ACL_TEST_F_FETCH_MORE;
5154 test->flags |= ACL_TEST_F_VOL_HDR;
5155 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5156 return 1;
5157 }
5158
5159 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5160 test->flags |= ACL_TEST_F_VOL_HDR;
5161 return 0;
5162}
5163
Willy Tarreauc11416f2007-06-17 16:58:38 +02005164static int
5165acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5166 struct acl_expr *expr, struct acl_test *test)
5167{
5168 struct http_txn *txn = l7;
5169
5170 if (txn->req.msg_state != HTTP_MSG_BODY)
5171 return 0;
5172 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5173 /* ensure the indexes are not affected */
5174 return 0;
5175
5176 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5177}
5178
5179static int
5180acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5181 struct acl_expr *expr, struct acl_test *test)
5182{
5183 struct http_txn *txn = l7;
5184
5185 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5186 return 0;
5187
5188 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5189}
5190
Willy Tarreau737b0c12007-06-10 21:28:46 +02005191/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5192 * the first '/' after the possible hostname, and ends before the possible '?'.
5193 */
5194static int
5195acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5196 struct acl_expr *expr, struct acl_test *test)
5197{
5198 struct http_txn *txn = l7;
5199 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005200
Willy Tarreauc11416f2007-06-17 16:58:38 +02005201 if (txn->req.msg_state != HTTP_MSG_BODY)
5202 return 0;
5203 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5204 /* ensure the indexes are not affected */
5205 return 0;
5206
Willy Tarreau737b0c12007-06-10 21:28:46 +02005207 ptr = txn->req.sol + txn->req.sl.rq.u;
5208 end = ptr + txn->req.sl.rq.u_l;
5209
5210 if (ptr >= end)
5211 return 0;
5212
5213 /* RFC2616, par. 5.1.2 :
5214 * Request-URI = "*" | absuri | abspath | authority
5215 */
5216
5217 if (*ptr == '*')
5218 return 0;
5219
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005220 if (isalpha((unsigned char)*ptr)) {
Willy Tarreau737b0c12007-06-10 21:28:46 +02005221 /* this is a scheme as described by RFC3986, par. 3.1 */
5222 ptr++;
5223 while (ptr < end &&
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005224 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
Willy Tarreau737b0c12007-06-10 21:28:46 +02005225 ptr++;
5226 /* skip '://' */
5227 if (ptr == end || *ptr++ != ':')
5228 return 0;
5229 if (ptr == end || *ptr++ != '/')
5230 return 0;
5231 if (ptr == end || *ptr++ != '/')
5232 return 0;
5233 }
5234 /* skip [user[:passwd]@]host[:[port]] */
5235
5236 while (ptr < end && *ptr != '/')
5237 ptr++;
5238
5239 if (ptr == end)
5240 return 0;
5241
5242 /* OK, we got the '/' ! */
5243 test->ptr = ptr;
5244
5245 while (ptr < end && *ptr != '?')
5246 ptr++;
5247
5248 test->len = ptr - test->ptr;
5249
5250 /* we do not need to set READ_ONLY because the data is in a buffer */
5251 test->flags = ACL_TEST_F_VOL_1ST;
5252 return 1;
5253}
5254
5255
Willy Tarreau8797c062007-05-07 00:55:35 +02005256
5257/************************************************************************/
5258/* All supported keywords must be declared here. */
5259/************************************************************************/
5260
5261/* Note: must not be declared <const> as its list will be overwritten */
5262static struct acl_kw_list acl_kws = {{ },{
5263 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5264 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5265 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
Willy Tarreauae8b7962007-06-09 23:10:04 +02005266 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005267
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005268 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5269 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5270 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5271 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5272 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5273 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
5274 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
5275 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip },
5276 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005277
Willy Tarreauc11416f2007-06-17 16:58:38 +02005278 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str },
5279 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg },
5280 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg },
5281 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end },
5282 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub },
5283 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir },
5284 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom },
5285 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int },
5286 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int },
5287
5288 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str },
5289 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg },
5290 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg },
5291 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end },
5292 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub },
5293 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir },
5294 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom },
5295 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int },
5296 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005297
5298 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5299 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5300 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5301 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5302 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5303 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5304 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5305
Willy Tarreauf3d25982007-05-08 22:45:09 +02005306 { NULL, NULL, NULL, NULL },
5307
5308#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005309 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5310 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5311 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5312 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5313 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5314 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5315 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5316
Willy Tarreau8797c062007-05-07 00:55:35 +02005317 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5318 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5319 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5320 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5321 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5322 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5323 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5324 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5325
5326 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5327 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5328 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5329 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5330 { NULL, NULL, NULL, NULL },
5331#endif
5332}};
5333
5334
5335__attribute__((constructor))
5336static void __http_protocol_init(void)
5337{
5338 acl_register_keywords(&acl_kws);
5339}
5340
5341
Willy Tarreau58f10d72006-12-04 02:26:12 +01005342/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005343 * Local variables:
5344 * c-indent-level: 8
5345 * c-basic-offset: 8
5346 * End:
5347 */