blob: 9fe10392aca8d52964996e82e37da81105117c0d [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>
49#include <proto/fd.h>
50#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/proto_http.h>
53#include <proto/queue.h>
54#include <proto/session.h>
55#include <proto/task.h>
56
Willy Tarreau6d1a9882007-01-07 02:03:04 +010057#ifdef CONFIG_HAP_TCPSPLICE
58#include <libtcpsplice.h>
59#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
Willy Tarreau58f10d72006-12-04 02:26:12 +010061#define DEBUG_PARSE_NO_SPEEDUP
62#undef DEBUG_PARSE_NO_SPEEDUP
63
Willy Tarreau976f1ee2006-12-17 10:06:03 +010064/* This is used to perform a quick jump as an alternative to a break/continue
65 * instruction. The first argument is the label for normal operation, and the
66 * second one is the break/continue instruction in the no_speedup mode.
67 */
68
69#ifdef DEBUG_PARSE_NO_SPEEDUP
70#define QUICK_JUMP(x,y) y
71#else
72#define QUICK_JUMP(x,y) goto x
73#endif
74
Willy Tarreau1c47f852006-07-09 08:22:27 +020075/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010076const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020077 "HTTP/1.0 200 OK\r\n"
78 "Cache-Control: no-cache\r\n"
79 "Connection: close\r\n"
80 "Content-Type: text/html\r\n"
81 "\r\n"
82 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
83
Willy Tarreau0f772532006-12-23 20:51:41 +010084const struct chunk http_200_chunk = {
85 .str = (char *)&HTTP_200,
86 .len = sizeof(HTTP_200)-1
87};
88
89const char *HTTP_302 =
90 "HTTP/1.0 302 Found\r\n"
91 "Cache-Control: no-cache\r\n"
92 "Connection: close\r\n"
93 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
97 "HTTP/1.0 303 See Other\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Connection: close\r\n"
100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
103const char *HTTP_401_fmt =
104 "HTTP/1.0 401 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200107 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
114 [HTTP_ERR_400] = 400,
115 [HTTP_ERR_403] = 403,
116 [HTTP_ERR_408] = 408,
117 [HTTP_ERR_500] = 500,
118 [HTTP_ERR_502] = 502,
119 [HTTP_ERR_503] = 503,
120 [HTTP_ERR_504] = 504,
121};
122
Willy Tarreau80587432006-12-24 17:47:20 +0100123static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100124 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100125 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 "Cache-Control: no-cache\r\n"
127 "Connection: close\r\n"
128 "Content-Type: text/html\r\n"
129 "\r\n"
130 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
131
132 [HTTP_ERR_403] =
133 "HTTP/1.0 403 Forbidden\r\n"
134 "Cache-Control: no-cache\r\n"
135 "Connection: close\r\n"
136 "Content-Type: text/html\r\n"
137 "\r\n"
138 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
139
140 [HTTP_ERR_408] =
141 "HTTP/1.0 408 Request Time-out\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
147
148 [HTTP_ERR_500] =
149 "HTTP/1.0 500 Server Error\r\n"
150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
155
156 [HTTP_ERR_502] =
157 "HTTP/1.0 502 Bad Gateway\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
163
164 [HTTP_ERR_503] =
165 "HTTP/1.0 503 Service Unavailable\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
171
172 [HTTP_ERR_504] =
173 "HTTP/1.0 504 Gateway Time-out\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
179
180};
181
Willy Tarreau80587432006-12-24 17:47:20 +0100182/* We must put the messages here since GCC cannot initialize consts depending
183 * on strlen().
184 */
185struct chunk http_err_chunks[HTTP_ERR_SIZE];
186
Willy Tarreau42250582007-04-01 01:30:43 +0200187#define FD_SETS_ARE_BITFIELDS
188#ifdef FD_SETS_ARE_BITFIELDS
189/*
190 * This map is used with all the FD_* macros to check whether a particular bit
191 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
192 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
193 * byte should be encoded. Be careful to always pass bytes from 0 to 255
194 * exclusively to the macros.
195 */
196fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
197fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
198
199#else
200#error "Check if your OS uses bitfields for fd_sets"
201#endif
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203void init_proto_http()
204{
Willy Tarreau42250582007-04-01 01:30:43 +0200205 int i;
206 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100207 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200208
Willy Tarreau80587432006-12-24 17:47:20 +0100209 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
210 if (!http_err_msgs[msg]) {
211 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
212 abort();
213 }
214
215 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
216 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
217 }
Willy Tarreau42250582007-04-01 01:30:43 +0200218
219 /* initialize the log header encoding map : '{|}"#' should be encoded with
220 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
221 * URL encoding only requires '"', '#' to be encoded as well as non-
222 * printable characters above.
223 */
224 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
225 memset(url_encode_map, 0, sizeof(url_encode_map));
226 for (i = 0; i < 32; i++) {
227 FD_SET(i, hdr_encode_map);
228 FD_SET(i, url_encode_map);
229 }
230 for (i = 127; i < 256; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234
235 tmp = "\"#{|}";
236 while (*tmp) {
237 FD_SET(*tmp, hdr_encode_map);
238 tmp++;
239 }
240
241 tmp = "\"#";
242 while (*tmp) {
243 FD_SET(*tmp, url_encode_map);
244 tmp++;
245 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200246
247 /* memory allocations */
248 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200249 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100250}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251
Willy Tarreau53b6c742006-12-17 13:37:46 +0100252/*
253 * We have 26 list of methods (1 per first letter), each of which can have
254 * up to 3 entries (2 valid, 1 null).
255 */
256struct http_method_desc {
257 http_meth_t meth;
258 int len;
259 const char text[8];
260};
261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100262const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100263 ['C' - 'A'] = {
264 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
265 },
266 ['D' - 'A'] = {
267 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
268 },
269 ['G' - 'A'] = {
270 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
271 },
272 ['H' - 'A'] = {
273 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
274 },
275 ['P' - 'A'] = {
276 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
277 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
278 },
279 ['T' - 'A'] = {
280 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
281 },
282 /* rest is empty like this :
283 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
284 */
285};
286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100287/* It is about twice as fast on recent architectures to lookup a byte in a
288 * table than two perform a boolean AND or OR between two tests. Refer to
289 * RFC2616 for those chars.
290 */
291
292const char http_is_spht[256] = {
293 [' '] = 1, ['\t'] = 1,
294};
295
296const char http_is_crlf[256] = {
297 ['\r'] = 1, ['\n'] = 1,
298};
299
300const char http_is_lws[256] = {
301 [' '] = 1, ['\t'] = 1,
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_sep[256] = {
306 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
307 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
308 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
309 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
310 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
311};
312
313const char http_is_ctl[256] = {
314 [0 ... 31] = 1,
315 [127] = 1,
316};
317
318/*
319 * A token is any ASCII char that is neither a separator nor a CTL char.
320 * Do not overwrite values in assignment since gcc-2.95 will not handle
321 * them correctly. Instead, define every non-CTL char's status.
322 */
323const char http_is_token[256] = {
324 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
325 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
326 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
327 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
328 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
329 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
330 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
331 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
332 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
333 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
334 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
335 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
336 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
337 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
338 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
339 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
340 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
341 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
342 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
343 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
344 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
345 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
346 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
347 ['|'] = 1, ['}'] = 0, ['~'] = 1,
348};
349
350
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100351/*
352 * An http ver_token is any ASCII which can be found in an HTTP version,
353 * which includes 'H', 'T', 'P', '/', '.' and any digit.
354 */
355const char http_is_ver_token[256] = {
356 ['.'] = 1, ['/'] = 1,
357 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
358 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
359 ['H'] = 1, ['P'] = 1, ['T'] = 1,
360};
361
362
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363#ifdef DEBUG_FULL
364static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
365static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
366#endif
367
Willy Tarreau42250582007-04-01 01:30:43 +0200368static void http_sess_log(struct session *s);
369
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100370/*
371 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
372 * CRLF. Text length is measured first, so it cannot be NULL.
373 * The header is also automatically added to the index <hdr_idx>, and the end
374 * of headers is automatically adjusted. The number of bytes added is returned
375 * on success, otherwise <0 is returned indicating an error.
376 */
377int http_header_add_tail(struct buffer *b, struct http_msg *msg,
378 struct hdr_idx *hdr_idx, const char *text)
379{
380 int bytes, len;
381
382 len = strlen(text);
383 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
384 if (!bytes)
385 return -1;
386 msg->eoh += bytes;
387 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
388}
389
390/*
391 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
392 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
393 * the buffer is only opened and the space reserved, but nothing is copied.
394 * The header is also automatically added to the index <hdr_idx>, and the end
395 * of headers is automatically adjusted. The number of bytes added is returned
396 * on success, otherwise <0 is returned indicating an error.
397 */
398int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
399 struct hdr_idx *hdr_idx, const char *text, int len)
400{
401 int bytes;
402
403 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
404 if (!bytes)
405 return -1;
406 msg->eoh += bytes;
407 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
408}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409
410/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100411 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
412 * If so, returns the position of the first non-space character relative to
413 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
414 * to return a pointer to the place after the first space. Returns 0 if the
415 * header name does not match. Checks are case-insensitive.
416 */
417int http_header_match2(const char *hdr, const char *end,
418 const char *name, int len)
419{
420 const char *val;
421
422 if (hdr + len >= end)
423 return 0;
424 if (hdr[len] != ':')
425 return 0;
426 if (strncasecmp(hdr, name, len) != 0)
427 return 0;
428 val = hdr + len + 1;
429 while (val < end && HTTP_IS_SPHT(*val))
430 val++;
431 if ((val >= end) && (len + 2 <= end - hdr))
432 return len + 2; /* we may replace starting from second space */
433 return val - hdr;
434}
435
Willy Tarreau33a7e692007-06-10 19:45:56 +0200436/* Find the end of the header value contained between <s> and <e>.
437 * See RFC2616, par 2.2 for more information. Note that it requires
438 * a valid header to return a valid result.
439 */
440const char *find_hdr_value_end(const char *s, const char *e)
441{
442 int quoted, qdpair;
443
444 quoted = qdpair = 0;
445 for (; s < e; s++) {
446 if (qdpair) qdpair = 0;
447 else if (quoted && *s == '\\') qdpair = 1;
448 else if (quoted && *s == '"') quoted = 0;
449 else if (*s == '"') quoted = 1;
450 else if (*s == ',') return s;
451 }
452 return s;
453}
454
455/* Find the first or next occurrence of header <name> in message buffer <sol>
456 * using headers index <idx>, and return it in the <ctx> structure. This
457 * structure holds everything necessary to use the header and find next
458 * occurrence. If its <idx> member is 0, the header is searched from the
459 * beginning. Otherwise, the next occurrence is returned. The function returns
460 * 1 when it finds a value, and 0 when there is no more.
461 */
462int http_find_header2(const char *name, int len,
463 const char *sol, struct hdr_idx *idx,
464 struct hdr_ctx *ctx)
465{
466 __label__ return_hdr, next_hdr;
467 const char *eol, *sov;
468 int cur_idx;
469
470 if (ctx->idx) {
471 /* We have previously returned a value, let's search
472 * another one on the same line.
473 */
474 cur_idx = ctx->idx;
475 sol = ctx->line;
476 sov = sol + ctx->val + ctx->vlen;
477 eol = sol + idx->v[cur_idx].len;
478
479 if (sov >= eol)
480 /* no more values in this header */
481 goto next_hdr;
482
483 /* values remaining for this header, skip the comma */
484 sov++;
485 while (sov < eol && http_is_lws[(unsigned char)*sov])
486 sov++;
487
488 goto return_hdr;
489 }
490
491 /* first request for this header */
492 sol += hdr_idx_first_pos(idx);
493 cur_idx = hdr_idx_first_idx(idx);
494
495 while (cur_idx) {
496 eol = sol + idx->v[cur_idx].len;
497
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200498 if (len == 0) {
499 /* No argument was passed, we want any header.
500 * To achieve this, we simply build a fake request. */
501 while (sol + len < eol && sol[len] != ':')
502 len++;
503 name = sol;
504 }
505
Willy Tarreau33a7e692007-06-10 19:45:56 +0200506 if ((len < eol - sol) &&
507 (sol[len] == ':') &&
508 (strncasecmp(sol, name, len) == 0)) {
509
510 sov = sol + len + 1;
511 while (sov < eol && http_is_lws[(unsigned char)*sov])
512 sov++;
513 return_hdr:
514 ctx->line = sol;
515 ctx->idx = cur_idx;
516 ctx->val = sov - sol;
517
518 eol = find_hdr_value_end(sov, eol);
519 ctx->vlen = eol - sov;
520 return 1;
521 }
522 next_hdr:
523 sol = eol + idx->v[cur_idx].cr + 1;
524 cur_idx = idx->v[cur_idx].next;
525 }
526 return 0;
527}
528
529int http_find_header(const char *name,
530 const char *sol, struct hdr_idx *idx,
531 struct hdr_ctx *ctx)
532{
533 return http_find_header2(name, strlen(name), sol, idx, ctx);
534}
535
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100536/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537 * returns a message to the client ; the connection is shut down for read,
538 * and the request is cleared so that no server connection can be initiated.
539 * The client must be in a valid state for this (HEADER, DATA ...).
Willy Tarreau0f772532006-12-23 20:51:41 +0100540 * Nothing is performed on the server side. The message is contained in a
541 * "chunk". If it is null, then an empty message is used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 * The reply buffer doesn't need to be empty before this.
543 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100544void client_retnclose(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545{
Willy Tarreauf161a342007-04-08 16:59:42 +0200546 EV_FD_CLR(s->cli_fd, DIR_RD);
547 EV_FD_SET(s->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +0200548 buffer_shutr(s->req);
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200549 if (!tv_add_ifset(&s->rep->wex, &now, &s->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +0200550 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200551 s->cli_state = CL_STSHUTR;
552 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100553 if (msg->len)
554 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 s->req->l = 0;
556}
557
558
559/*
560 * returns a message into the rep buffer, and flushes the req buffer.
Willy Tarreau0f772532006-12-23 20:51:41 +0100561 * The reply buffer doesn't need to be empty before this. The message
562 * is contained in a "chunk". If it is null, then an empty message is
563 * used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200564 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100565void client_return(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566{
567 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100568 if (msg->len)
569 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 s->req->l = 0;
571}
572
573
574/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100575 * indicators accordingly. Note that if <status> is 0, or if the message
576 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200577 */
578void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100579 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580{
581 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100582 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100583 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100584 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100585 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200586 }
587 if (!(t->flags & SN_ERR_MASK))
588 t->flags |= err;
589 if (!(t->flags & SN_FINST_MASK))
590 t->flags |= finst;
591}
592
Willy Tarreau80587432006-12-24 17:47:20 +0100593/* This function returns the appropriate error location for the given session
594 * and message.
595 */
596
597struct chunk *error_message(struct session *s, int msgnum)
598{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200599 if (s->be->errmsg[msgnum].str)
600 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100601 else if (s->fe->errmsg[msgnum].str)
602 return &s->fe->errmsg[msgnum];
603 else
604 return &http_err_chunks[msgnum];
605}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606
Willy Tarreau53b6c742006-12-17 13:37:46 +0100607/*
608 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
609 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
610 */
611static http_meth_t find_http_meth(const char *str, const int len)
612{
613 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100614 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100615
616 m = ((unsigned)*str - 'A');
617
618 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100619 for (h = http_methods[m]; h->len > 0; h++) {
620 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100621 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100622 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100623 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100624 };
625 return HTTP_METH_OTHER;
626 }
627 return HTTP_METH_NONE;
628
629}
630
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631/* Processes the client and server jobs of a session task, then
632 * puts it back to the wait queue in a clean state, or
633 * cleans up its resources if it must be deleted. Returns
634 * the time the task accepts to wait, or TIME_ETERNITY for
635 * infinity.
636 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200637void process_session(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200638{
639 struct session *s = t->context;
640 int fsm_resync = 0;
641
642 do {
643 fsm_resync = 0;
644 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
645 fsm_resync |= process_cli(s);
646 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
647 fsm_resync |= process_srv(s);
648 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
649 } while (fsm_resync);
650
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200651 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200652 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
653 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200655 t->expire = s->req->rex;
656 tv_min(&t->expire, &s->req->rex, &s->req->wex);
657 tv_bound(&t->expire, &s->req->cex);
658 tv_bound(&t->expire, &s->rep->rex);
659 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660
661 /* restore t to its place in the task list */
662 task_queue(t);
663
664#ifdef DEBUG_FULL
665 /* DEBUG code : this should never ever happen, otherwise it indicates
666 * that a task still has something to do and will provoke a quick loop.
667 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200668 if (tv_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669 exit(100);
670#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200671 *next = t->expire;
672 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673 }
674
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100675 s->fe->feconn--;
676 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200677 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200678 actconn--;
679
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200680 if (unlikely((global.mode & MODE_DEBUG) &&
681 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100683 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200684 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100685 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686 write(1, trash, len);
687 }
688
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200689 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100690 if (s->req != NULL)
691 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200692 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100693 s->logs.bytes_out = s->rep->total;
694
695 s->fe->bytes_in += s->logs.bytes_in;
696 s->fe->bytes_out += s->logs.bytes_out;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200697 if (s->be != s->fe) {
698 s->be->bytes_in += s->logs.bytes_in;
699 s->be->bytes_out += s->logs.bytes_out;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100700 }
701 if (s->srv) {
702 s->srv->bytes_in += s->logs.bytes_in;
703 s->srv->bytes_out += s->logs.bytes_out;
704 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200705
706 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200707 if (s->logs.logwait &&
708 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200709 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
710 if (s->fe->to_log & LW_REQ)
711 http_sess_log(s);
712 else
713 tcp_sess_log(s);
714 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200715
716 /* the task MUST not be in the run queue anymore */
717 task_delete(t);
718 session_free(s);
719 task_free(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200720 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721}
722
723
Willy Tarreau42250582007-04-01 01:30:43 +0200724extern const char sess_term_cond[8];
725extern const char sess_fin_state[8];
726extern const char *monthname[12];
727const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
728const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
729 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
730 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200731struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200732struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100733
Willy Tarreau42250582007-04-01 01:30:43 +0200734/*
735 * send a log for the session when we have enough info about it.
736 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100737 */
Willy Tarreau42250582007-04-01 01:30:43 +0200738static void http_sess_log(struct session *s)
739{
740 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
741 struct proxy *fe = s->fe;
742 struct proxy *be = s->be;
743 struct proxy *prx_log;
744 struct http_txn *txn = &s->txn;
745 int tolog;
746 char *uri, *h;
747 char *svid;
748 struct tm *tm;
749 static char tmpline[MAX_SYSLOG_LEN];
750 int hdr;
751
752 if (fe->logfac1 < 0 && fe->logfac2 < 0)
753 return;
754 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100755
Willy Tarreau42250582007-04-01 01:30:43 +0200756 if (s->cli_addr.ss_family == AF_INET)
757 inet_ntop(AF_INET,
758 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
759 pn, sizeof(pn));
760 else
761 inet_ntop(AF_INET6,
762 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
763 pn, sizeof(pn));
764
765 tm = localtime((time_t *)&s->logs.tv_accept.tv_sec);
766
767
768 /* FIXME: let's limit ourselves to frontend logging for now. */
769 tolog = fe->to_log;
770
771 h = tmpline;
772 if (fe->to_log & LW_REQHDR &&
773 txn->req.cap &&
774 (h < tmpline + sizeof(tmpline) - 10)) {
775 *(h++) = ' ';
776 *(h++) = '{';
777 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
778 if (hdr)
779 *(h++) = '|';
780 if (txn->req.cap[hdr] != NULL)
781 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
782 '#', hdr_encode_map, txn->req.cap[hdr]);
783 }
784 *(h++) = '}';
785 }
786
787 if (fe->to_log & LW_RSPHDR &&
788 txn->rsp.cap &&
789 (h < tmpline + sizeof(tmpline) - 7)) {
790 *(h++) = ' ';
791 *(h++) = '{';
792 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
793 if (hdr)
794 *(h++) = '|';
795 if (txn->rsp.cap[hdr] != NULL)
796 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
797 '#', hdr_encode_map, txn->rsp.cap[hdr]);
798 }
799 *(h++) = '}';
800 }
801
802 if (h < tmpline + sizeof(tmpline) - 4) {
803 *(h++) = ' ';
804 *(h++) = '"';
805 uri = txn->uri ? txn->uri : "<BADREQ>";
806 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
807 '#', url_encode_map, uri);
808 *(h++) = '"';
809 }
810 *h = '\0';
811
812 svid = (tolog & LW_SVID) ?
813 (s->data_source != DATA_SRC_STATS) ?
814 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
815
816 send_log(prx_log, LOG_INFO,
817 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
818 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
819 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
820 pn,
821 (s->cli_addr.ss_family == AF_INET) ?
822 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
823 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
824 tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900,
825 tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000,
826 fe->id, be->id, svid,
827 s->logs.t_request,
828 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
829 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
830 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
831 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
832 txn->status,
833 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
834 txn->cli_cookie ? txn->cli_cookie : "-",
835 txn->srv_cookie ? txn->srv_cookie : "-",
836 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
837 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
838 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
839 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
840 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
841 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
842
843 s->logs.logwait = 0;
844}
845
Willy Tarreau117f59e2007-03-04 18:17:17 +0100846
847/*
848 * Capture headers from message starting at <som> according to header list
849 * <cap_hdr>, and fill the <idx> structure appropriately.
850 */
851void capture_headers(char *som, struct hdr_idx *idx,
852 char **cap, struct cap_hdr *cap_hdr)
853{
854 char *eol, *sol, *col, *sov;
855 int cur_idx;
856 struct cap_hdr *h;
857 int len;
858
859 sol = som + hdr_idx_first_pos(idx);
860 cur_idx = hdr_idx_first_idx(idx);
861
862 while (cur_idx) {
863 eol = sol + idx->v[cur_idx].len;
864
865 col = sol;
866 while (col < eol && *col != ':')
867 col++;
868
869 sov = col + 1;
870 while (sov < eol && http_is_lws[(unsigned char)*sov])
871 sov++;
872
873 for (h = cap_hdr; h; h = h->next) {
874 if ((h->namelen == col - sol) &&
875 (strncasecmp(sol, h->name, h->namelen) == 0)) {
876 if (cap[h->index] == NULL)
877 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200878 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100879
880 if (cap[h->index] == NULL) {
881 Alert("HTTP capture : out of memory.\n");
882 continue;
883 }
884
885 len = eol - sov;
886 if (len > h->len)
887 len = h->len;
888
889 memcpy(cap[h->index], sov, len);
890 cap[h->index][len]=0;
891 }
892 }
893 sol = eol + idx->v[cur_idx].cr + 1;
894 cur_idx = idx->v[cur_idx].next;
895 }
896}
897
898
Willy Tarreau42250582007-04-01 01:30:43 +0200899/* either we find an LF at <ptr> or we jump to <bad>.
900 */
901#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
902
903/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
904 * otherwise to <http_msg_ood> with <state> set to <st>.
905 */
906#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
907 ptr++; \
908 if (likely(ptr < end)) \
909 goto good; \
910 else { \
911 state = (st); \
912 goto http_msg_ood; \
913 } \
914 } while (0)
915
916
Willy Tarreaubaaee002006-06-26 02:48:02 +0200917/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100918 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100919 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
920 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
921 * will give undefined results.
922 * Note that it is upon the caller's responsibility to ensure that ptr < end,
923 * and that msg->sol points to the beginning of the response.
924 * If a complete line is found (which implies that at least one CR or LF is
925 * found before <end>, the updated <ptr> is returned, otherwise NULL is
926 * returned indicating an incomplete line (which does not mean that parts have
927 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
928 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
929 * upon next call.
930 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200931 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100932 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
933 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200934 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100935 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100936const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100937 const char *ptr, const char *end,
938 char **ret_ptr, int *ret_state)
939{
940 __label__
941 http_msg_rpver,
942 http_msg_rpver_sp,
943 http_msg_rpcode,
944 http_msg_rpcode_sp,
945 http_msg_rpreason,
946 http_msg_rpline_eol,
947 http_msg_ood, /* out of data */
948 http_msg_invalid;
949
950 switch (state) {
951 http_msg_rpver:
952 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100953 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100954 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
955
956 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100957 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
959 }
960 goto http_msg_invalid;
961
962 http_msg_rpver_sp:
963 case HTTP_MSG_RPVER_SP:
964 if (likely(!HTTP_IS_LWS(*ptr))) {
965 msg->sl.st.c = ptr - msg_buf;
966 goto http_msg_rpcode;
967 }
968 if (likely(HTTP_IS_SPHT(*ptr)))
969 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
970 /* so it's a CR/LF, this is invalid */
971 goto http_msg_invalid;
972
973 http_msg_rpcode:
974 case HTTP_MSG_RPCODE:
975 if (likely(!HTTP_IS_LWS(*ptr)))
976 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
977
978 if (likely(HTTP_IS_SPHT(*ptr))) {
979 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
980 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
981 }
982
983 /* so it's a CR/LF, so there is no reason phrase */
984 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
985 http_msg_rsp_reason:
986 /* FIXME: should we support HTTP responses without any reason phrase ? */
987 msg->sl.st.r = ptr - msg_buf;
988 msg->sl.st.r_l = 0;
989 goto http_msg_rpline_eol;
990
991 http_msg_rpcode_sp:
992 case HTTP_MSG_RPCODE_SP:
993 if (likely(!HTTP_IS_LWS(*ptr))) {
994 msg->sl.st.r = ptr - msg_buf;
995 goto http_msg_rpreason;
996 }
997 if (likely(HTTP_IS_SPHT(*ptr)))
998 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
999 /* so it's a CR/LF, so there is no reason phrase */
1000 goto http_msg_rsp_reason;
1001
1002 http_msg_rpreason:
1003 case HTTP_MSG_RPREASON:
1004 if (likely(!HTTP_IS_CRLF(*ptr)))
1005 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1006 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1007 http_msg_rpline_eol:
1008 /* We have seen the end of line. Note that we do not
1009 * necessarily have the \n yet, but at least we know that we
1010 * have EITHER \r OR \n, otherwise the response would not be
1011 * complete. We can then record the response length and return
1012 * to the caller which will be able to register it.
1013 */
1014 msg->sl.st.l = ptr - msg->sol;
1015 return ptr;
1016
1017#ifdef DEBUG_FULL
1018 default:
1019 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1020 exit(1);
1021#endif
1022 }
1023
1024 http_msg_ood:
1025 /* out of data */
1026 if (ret_state)
1027 *ret_state = state;
1028 if (ret_ptr)
1029 *ret_ptr = (char *)ptr;
1030 return NULL;
1031
1032 http_msg_invalid:
1033 /* invalid message */
1034 if (ret_state)
1035 *ret_state = HTTP_MSG_ERROR;
1036 return NULL;
1037}
1038
1039
1040/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001041 * This function parses a request line between <ptr> and <end>, starting with
1042 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1043 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1044 * will give undefined results.
1045 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1046 * and that msg->sol points to the beginning of the request.
1047 * If a complete line is found (which implies that at least one CR or LF is
1048 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1049 * returned indicating an incomplete line (which does not mean that parts have
1050 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1051 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1052 * upon next call.
1053 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001054 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001055 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1056 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001057 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001058 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001059const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +01001060 const char *ptr, const char *end,
1061 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001062{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001063 __label__
1064 http_msg_rqmeth,
1065 http_msg_rqmeth_sp,
1066 http_msg_rquri,
1067 http_msg_rquri_sp,
1068 http_msg_rqver,
1069 http_msg_rqline_eol,
1070 http_msg_ood, /* out of data */
1071 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 switch (state) {
1074 http_msg_rqmeth:
1075 case HTTP_MSG_RQMETH:
1076 if (likely(HTTP_IS_TOKEN(*ptr)))
1077 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001080 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1082 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 if (likely(HTTP_IS_CRLF(*ptr))) {
1085 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001086 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 http_msg_req09_uri:
1088 msg->sl.rq.u = ptr - msg_buf;
1089 http_msg_req09_uri_e:
1090 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1091 http_msg_req09_ver:
1092 msg->sl.rq.v = ptr - msg_buf;
1093 msg->sl.rq.v_l = 0;
1094 goto http_msg_rqline_eol;
1095 }
1096 goto http_msg_invalid;
1097
1098 http_msg_rqmeth_sp:
1099 case HTTP_MSG_RQMETH_SP:
1100 if (likely(!HTTP_IS_LWS(*ptr))) {
1101 msg->sl.rq.u = ptr - msg_buf;
1102 goto http_msg_rquri;
1103 }
1104 if (likely(HTTP_IS_SPHT(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1106 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1107 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 http_msg_rquri:
1110 case HTTP_MSG_RQURI:
1111 if (likely(!HTTP_IS_LWS(*ptr)))
1112 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 if (likely(HTTP_IS_SPHT(*ptr))) {
1115 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1116 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001119 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1120 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001122 http_msg_rquri_sp:
1123 case HTTP_MSG_RQURI_SP:
1124 if (likely(!HTTP_IS_LWS(*ptr))) {
1125 msg->sl.rq.v = ptr - msg_buf;
1126 goto http_msg_rqver;
1127 }
1128 if (likely(HTTP_IS_SPHT(*ptr)))
1129 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1130 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1131 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001132
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001133 http_msg_rqver:
1134 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001135 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001136 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001137
1138 if (likely(HTTP_IS_CRLF(*ptr))) {
1139 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1140 http_msg_rqline_eol:
1141 /* We have seen the end of line. Note that we do not
1142 * necessarily have the \n yet, but at least we know that we
1143 * have EITHER \r OR \n, otherwise the request would not be
1144 * complete. We can then record the request length and return
1145 * to the caller which will be able to register it.
1146 */
1147 msg->sl.rq.l = ptr - msg->sol;
1148 return ptr;
1149 }
1150
1151 /* neither an HTTP_VER token nor a CRLF */
1152 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001153
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001154#ifdef DEBUG_FULL
1155 default:
1156 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1157 exit(1);
1158#endif
1159 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001160
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 http_msg_ood:
1162 /* out of data */
1163 if (ret_state)
1164 *ret_state = state;
1165 if (ret_ptr)
1166 *ret_ptr = (char *)ptr;
1167 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001168
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001169 http_msg_invalid:
1170 /* invalid message */
1171 if (ret_state)
1172 *ret_state = HTTP_MSG_ERROR;
1173 return NULL;
1174}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001175
1176
Willy Tarreau8973c702007-01-21 23:58:29 +01001177/*
1178 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001179 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001180 * when data are missing and recalled at the exact same location with no
1181 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001182 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1183 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001184 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1186{
1187 __label__
1188 http_msg_rqbefore,
1189 http_msg_rqbefore_cr,
1190 http_msg_rqmeth,
1191 http_msg_rqline_end,
1192 http_msg_hdr_first,
1193 http_msg_hdr_name,
1194 http_msg_hdr_l1_sp,
1195 http_msg_hdr_l1_lf,
1196 http_msg_hdr_l1_lws,
1197 http_msg_hdr_val,
1198 http_msg_hdr_l2_lf,
1199 http_msg_hdr_l2_lws,
1200 http_msg_complete_header,
1201 http_msg_last_lf,
1202 http_msg_ood, /* out of data */
1203 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001204
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001205 int state; /* updated only when leaving the FSM */
1206 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001207
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001208 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001209 ptr = buf->lr;
1210 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001211
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 if (unlikely(ptr >= end))
1213 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001214
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001215 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001216 /*
1217 * First, states that are specific to the response only.
1218 * We check them first so that request and headers are
1219 * closer to each other (accessed more often).
1220 */
1221 http_msg_rpbefore:
1222 case HTTP_MSG_RPBEFORE:
1223 if (likely(HTTP_IS_TOKEN(*ptr))) {
1224 if (likely(ptr == buf->data)) {
1225 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001226 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001227 } else {
1228#if PARSE_PRESERVE_EMPTY_LINES
1229 /* only skip empty leading lines, don't remove them */
1230 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001231 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001232#else
1233 /* Remove empty leading lines, as recommended by
1234 * RFC2616. This takes a lot of time because we
1235 * must move all the buffer backwards, but this
1236 * is rarely needed. The method above will be
1237 * cleaner when we'll be able to start sending
1238 * the request from any place in the buffer.
1239 */
1240 buf->lr = ptr;
1241 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001242 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001243 msg->sol = buf->data;
1244 ptr = buf->data;
1245 end = buf->r;
1246#endif
1247 }
1248 hdr_idx_init(idx);
1249 state = HTTP_MSG_RPVER;
1250 goto http_msg_rpver;
1251 }
1252
1253 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1254 goto http_msg_invalid;
1255
1256 if (unlikely(*ptr == '\n'))
1257 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1258 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1259 /* stop here */
1260
1261 http_msg_rpbefore_cr:
1262 case HTTP_MSG_RPBEFORE_CR:
1263 EXPECT_LF_HERE(ptr, http_msg_invalid);
1264 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1265 /* stop here */
1266
1267 http_msg_rpver:
1268 case HTTP_MSG_RPVER:
1269 case HTTP_MSG_RPVER_SP:
1270 case HTTP_MSG_RPCODE:
1271 case HTTP_MSG_RPCODE_SP:
1272 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001273 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001274 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001275 if (unlikely(!ptr))
1276 return;
1277
1278 /* we have a full response and we know that we have either a CR
1279 * or an LF at <ptr>.
1280 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001281 //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 +01001282 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1283
1284 msg->sol = ptr;
1285 if (likely(*ptr == '\r'))
1286 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1287 goto http_msg_rpline_end;
1288
1289 http_msg_rpline_end:
1290 case HTTP_MSG_RPLINE_END:
1291 /* msg->sol must point to the first of CR or LF. */
1292 EXPECT_LF_HERE(ptr, http_msg_invalid);
1293 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1294 /* stop here */
1295
1296 /*
1297 * Second, states that are specific to the request only
1298 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 http_msg_rqbefore:
1300 case HTTP_MSG_RQBEFORE:
1301 if (likely(HTTP_IS_TOKEN(*ptr))) {
1302 if (likely(ptr == buf->data)) {
1303 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001304 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 } else {
1306#if PARSE_PRESERVE_EMPTY_LINES
1307 /* only skip empty leading lines, don't remove them */
1308 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001309 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001310#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 /* Remove empty leading lines, as recommended by
1312 * RFC2616. This takes a lot of time because we
1313 * must move all the buffer backwards, but this
1314 * is rarely needed. The method above will be
1315 * cleaner when we'll be able to start sending
1316 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 buf->lr = ptr;
1319 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001320 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 msg->sol = buf->data;
1322 ptr = buf->data;
1323 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001324#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001326 /* we will need this when keep-alive will be supported
1327 hdr_idx_init(idx);
1328 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 state = HTTP_MSG_RQMETH;
1330 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1334 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 if (unlikely(*ptr == '\n'))
1337 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1338 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 http_msg_rqbefore_cr:
1342 case HTTP_MSG_RQBEFORE_CR:
1343 EXPECT_LF_HERE(ptr, http_msg_invalid);
1344 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_rqmeth:
1348 case HTTP_MSG_RQMETH:
1349 case HTTP_MSG_RQMETH_SP:
1350 case HTTP_MSG_RQURI:
1351 case HTTP_MSG_RQURI_SP:
1352 case HTTP_MSG_RQVER:
1353 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001354 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001355 if (unlikely(!ptr))
1356 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 /* we have a full request and we know that we have either a CR
1359 * or an LF at <ptr>.
1360 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001361 //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 +01001362 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001363
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 msg->sol = ptr;
1365 if (likely(*ptr == '\r'))
1366 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 http_msg_rqline_end:
1370 case HTTP_MSG_RQLINE_END:
1371 /* check for HTTP/0.9 request : no version information available.
1372 * msg->sol must point to the first of CR or LF.
1373 */
1374 if (unlikely(msg->sl.rq.v_l == 0))
1375 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001376
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 EXPECT_LF_HERE(ptr, http_msg_invalid);
1378 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001379 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001380
Willy Tarreau8973c702007-01-21 23:58:29 +01001381 /*
1382 * Common states below
1383 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 http_msg_hdr_first:
1385 case HTTP_MSG_HDR_FIRST:
1386 msg->sol = ptr;
1387 if (likely(!HTTP_IS_CRLF(*ptr))) {
1388 goto http_msg_hdr_name;
1389 }
1390
1391 if (likely(*ptr == '\r'))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1393 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 http_msg_hdr_name:
1396 case HTTP_MSG_HDR_NAME:
1397 /* assumes msg->sol points to the first char */
1398 if (likely(HTTP_IS_TOKEN(*ptr)))
1399 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 if (likely(*ptr == ':')) {
1402 msg->col = ptr - buf->data;
1403 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1404 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001405
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 http_msg_hdr_l1_sp:
1409 case HTTP_MSG_HDR_L1_SP:
1410 /* assumes msg->sol points to the first char and msg->col to the colon */
1411 if (likely(HTTP_IS_SPHT(*ptr)))
1412 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 /* header value can be basically anything except CR/LF */
1415 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(!HTTP_IS_CRLF(*ptr))) {
1418 goto http_msg_hdr_val;
1419 }
1420
1421 if (likely(*ptr == '\r'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1423 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 http_msg_hdr_l1_lf:
1426 case HTTP_MSG_HDR_L1_LF:
1427 EXPECT_LF_HERE(ptr, http_msg_invalid);
1428 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 http_msg_hdr_l1_lws:
1431 case HTTP_MSG_HDR_L1_LWS:
1432 if (likely(HTTP_IS_SPHT(*ptr))) {
1433 /* replace HT,CR,LF with spaces */
1434 for (; buf->data+msg->sov < ptr; msg->sov++)
1435 buf->data[msg->sov] = ' ';
1436 goto http_msg_hdr_l1_sp;
1437 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001438 /* we had a header consisting only in spaces ! */
1439 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 goto http_msg_complete_header;
1441
1442 http_msg_hdr_val:
1443 case HTTP_MSG_HDR_VAL:
1444 /* assumes msg->sol points to the first char, msg->col to the
1445 * colon, and msg->sov points to the first character of the
1446 * value.
1447 */
1448 if (likely(!HTTP_IS_CRLF(*ptr)))
1449 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 msg->eol = ptr;
1452 /* Note: we could also copy eol into ->eoh so that we have the
1453 * real header end in case it ends with lots of LWS, but is this
1454 * really needed ?
1455 */
1456 if (likely(*ptr == '\r'))
1457 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1458 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001459
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 http_msg_hdr_l2_lf:
1461 case HTTP_MSG_HDR_L2_LF:
1462 EXPECT_LF_HERE(ptr, http_msg_invalid);
1463 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 http_msg_hdr_l2_lws:
1466 case HTTP_MSG_HDR_L2_LWS:
1467 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1468 /* LWS: replace HT,CR,LF with spaces */
1469 for (; msg->eol < ptr; msg->eol++)
1470 *msg->eol = ' ';
1471 goto http_msg_hdr_val;
1472 }
1473 http_msg_complete_header:
1474 /*
1475 * It was a new header, so the last one is finished.
1476 * Assumes msg->sol points to the first char, msg->col to the
1477 * colon, msg->sov points to the first character of the value
1478 * and msg->eol to the first CR or LF so we know how the line
1479 * ends. We insert last header into the index.
1480 */
1481 /*
1482 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1483 write(2, msg->sol, msg->eol-msg->sol);
1484 fprintf(stderr,"\n");
1485 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1488 idx, idx->tail) < 0))
1489 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 msg->sol = ptr;
1492 if (likely(!HTTP_IS_CRLF(*ptr))) {
1493 goto http_msg_hdr_name;
1494 }
1495
1496 if (likely(*ptr == '\r'))
1497 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1498 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 http_msg_last_lf:
1501 case HTTP_MSG_LAST_LF:
1502 /* Assumes msg->sol points to the first of either CR or LF */
1503 EXPECT_LF_HERE(ptr, http_msg_invalid);
1504 ptr++;
1505 buf->lr = ptr;
1506 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001507 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 return;
1509#ifdef DEBUG_FULL
1510 default:
1511 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1512 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001513#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 }
1515 http_msg_ood:
1516 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001517 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 buf->lr = ptr;
1519 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 http_msg_invalid:
1522 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001523 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 return;
1525}
1526
1527/*
1528 * manages the client FSM and its socket. BTW, it also tries to handle the
1529 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1530 * 0 else.
1531 */
1532int process_cli(struct session *t)
1533{
1534 int s = t->srv_state;
1535 int c = t->cli_state;
1536 struct buffer *req = t->req;
1537 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001538
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1540 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001541 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 req->rex.tv_sec, req->rex.tv_usec,
1543 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001544
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 if (c == CL_STHEADERS) {
1546 /*
1547 * Now parse the partial (or complete) lines.
1548 * We will check the request syntax, and also join multi-line
1549 * headers. An index of all the lines will be elaborated while
1550 * parsing.
1551 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001552 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001555 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 * req->data + req->eoh = end of processed headers / start of current one
1557 * req->data + req->eol = end of current header or line (LF or CRLF)
1558 * req->lr = first non-visited byte
1559 * req->r = end of data
1560 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001563 struct http_txn *txn = &t->txn;
1564 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001568 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 /* 1: we might have to print this header in debug mode */
1571 if (unlikely((global.mode & MODE_DEBUG) &&
1572 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001573 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001575
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001576 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 eol = sol + msg->sl.rq.l;
1578 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001579
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001580 sol += hdr_idx_first_pos(&txn->hdr_idx);
1581 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001582
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001584 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001586 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1587 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 }
1589 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001590
Willy Tarreau58f10d72006-12-04 02:26:12 +01001591
1592 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001594 * If not so, we check the FD and buffer states before leaving.
1595 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1597 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598 *
1599 */
1600
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001601 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 /*
1603 * First, let's catch bad requests.
1604 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001605 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001607
1608 /* 1: Since we are in header mode, if there's no space
1609 * left for headers, we won't be able to free more
1610 * later, so the session will never terminate. We
1611 * must terminate it now.
1612 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 if (unlikely(req->l >= req->rlim - req->data)) {
1614 /* FIXME: check if URI is set and return Status
1615 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001616 */
Willy Tarreau06619262006-12-17 08:37:22 +01001617 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001618 }
1619
1620 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1622 /* read error, or last read : give up. */
Willy Tarreaufa645582007-06-03 15:59:52 +02001623 buffer_shutr(req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624 fd_delete(t->cli_fd);
1625 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001626 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627 if (!(t->flags & SN_ERR_MASK))
1628 t->flags |= SN_ERR_CLICL;
1629 if (!(t->flags & SN_FINST_MASK))
1630 t->flags |= SN_FINST_R;
1631 return 1;
1632 }
1633
1634 /* 3: has the read timeout expired ? */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001635 else if (unlikely(tv_isle(&req->rex, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001636 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001637 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001638 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001639 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001640 if (!(t->flags & SN_ERR_MASK))
1641 t->flags |= SN_ERR_CLITO;
1642 if (!(t->flags & SN_FINST_MASK))
1643 t->flags |= SN_FINST_R;
1644 return 1;
1645 }
1646
1647 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001648 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001649 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001650 * full. We cannot loop here since stream_sock_read will disable it only if
1651 * req->l == rlim-data
1652 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001653 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001654 tv_eternity(&req->rex);
1655 }
1656 return t->cli_state != CL_STHEADERS;
1657 }
1658
1659
1660 /****************************************************************
1661 * More interesting part now : we know that we have a complete *
1662 * request which at least looks like HTTP. We have an indicator *
1663 * of each header's length, so we can parse them quickly. *
1664 ****************************************************************/
1665
Willy Tarreau9cdde232007-05-02 20:58:19 +02001666 /* ensure we keep this pointer to the beginning of the message */
1667 msg->sol = req->data + msg->som;
1668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 /*
1670 * 1: identify the method
1671 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001672 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001673
1674 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001676 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001678 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 if (unlikely((t->fe->monitor_uri_len != 0) &&
1680 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1681 !memcmp(&req->data[msg->sl.rq.u],
1682 t->fe->monitor_uri,
1683 t->fe->monitor_uri_len))) {
1684 /*
1685 * We have found the monitor URI
1686 */
1687 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001688 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 client_retnclose(t, &http_200_chunk);
1690 goto return_prx_cond;
1691 }
1692
1693 /*
1694 * 3: Maybe we have to copy the original REQURI for the logs ?
1695 * Note: we cannot log anymore if the request has been
1696 * classified as invalid.
1697 */
1698 if (unlikely(t->logs.logwait & LW_REQ)) {
1699 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001700 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 int urilen = msg->sl.rq.l;
1702
1703 if (urilen >= REQURI_LEN)
1704 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001705 memcpy(txn->uri, &req->data[msg->som], urilen);
1706 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707
1708 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001709 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 } else {
1711 Alert("HTTP logging : out of memory.\n");
1712 }
1713 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001714
Willy Tarreau06619262006-12-17 08:37:22 +01001715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1717 if (unlikely(msg->sl.rq.v_l == 0)) {
1718 int delta;
1719 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001720 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 cur_end = msg->sol + msg->sl.rq.l;
1722 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001723
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (msg->sl.rq.u_l == 0) {
1725 /* if no URI was set, add "/" */
1726 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1727 cur_end += delta;
1728 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001729 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 /* add HTTP version */
1731 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1732 msg->eoh += delta;
1733 cur_end += delta;
1734 cur_end = (char *)http_parse_reqline(msg, req->data,
1735 HTTP_MSG_RQMETH,
1736 msg->sol, cur_end + 1,
1737 NULL, NULL);
1738 if (unlikely(!cur_end))
1739 goto return_bad_req;
1740
1741 /* we have a full HTTP/1.0 request now and we know that
1742 * we have either a CR or an LF at <ptr>.
1743 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001744 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001745 }
1746
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747
1748 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001749 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001750 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001751 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001752
1753 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001755 * As opposed to version 1.2, now they will be evaluated in the
1756 * filters order and not in the header order. This means that
1757 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001758 *
1759 * We can now check whether we want to switch to another
1760 * backend, in which case we will re-check the backend's
1761 * filters and various options. In order to support 3-level
1762 * switching, here's how we should proceed :
1763 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001764 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001765 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001766 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001767 * There cannot be any switch from there, so ->be cannot be
1768 * changed anymore.
1769 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001770 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001771 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001772 * The response path will be able to apply either ->be, or
1773 * ->be then ->fe filters in order to match the reverse of
1774 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001775 */
1776
Willy Tarreau06619262006-12-17 08:37:22 +01001777 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001778 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001779 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001780 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001781
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001782 /* first check whether we have some ACLs set to block this request */
1783 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001784 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001785 if (cond->pol == ACL_COND_UNLESS)
1786 ret = !ret;
1787
1788 if (ret) {
1789 txn->status = 403;
1790 /* let's log the request time */
1791 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1792 client_retnclose(t, error_message(t, HTTP_ERR_403));
1793 goto return_prx_cond;
1794 }
1795 }
1796
Willy Tarreau06619262006-12-17 08:37:22 +01001797 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001798 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1800 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001801 }
1802
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001803 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1804 /* to ensure correct connection accounting on
1805 * the backend, we count the connection for the
1806 * one managing the queue.
1807 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001808 t->be->beconn++;
1809 if (t->be->beconn > t->be->beconn_max)
1810 t->be->beconn_max = t->be->beconn;
1811 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001812 t->flags |= SN_BE_ASSIGNED;
1813 }
1814
Willy Tarreau06619262006-12-17 08:37:22 +01001815 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001816 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001817 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001818 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001819 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001820 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001821 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001822 goto return_prx_cond;
1823 }
1824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001826 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 !(t->flags & SN_CONN_CLOSED)) {
1828 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001829 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001831
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001832 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 old_idx = 0;
1834
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001835 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1836 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 cur_ptr = cur_next;
1838 cur_end = cur_ptr + cur_hdr->len;
1839 cur_next = cur_end + cur_hdr->cr + 1;
1840
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001841 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1842 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 /* 3 possibilities :
1844 * - we have already set Connection: close,
1845 * so we remove this line.
1846 * - we have not yet set Connection: close,
1847 * but this line indicates close. We leave
1848 * it untouched and set the flag.
1849 * - we have not yet set Connection: close,
1850 * and this line indicates non-close. We
1851 * replace it.
1852 */
1853 if (t->flags & SN_CONN_CLOSED) {
1854 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001855 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001857 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1858 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001859 cur_hdr->len = 0;
1860 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001861 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1862 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1863 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 cur_next += delta;
1865 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001866 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 }
1868 t->flags |= SN_CONN_CLOSED;
1869 }
1870 }
1871 old_idx = cur_idx;
1872 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001873 }
1874 /* add request headers from the rule sets in the same order */
1875 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1876 if (unlikely(http_header_add_tail(req,
1877 &txn->req,
1878 &txn->hdr_idx,
1879 rule_set->req_add[cur_idx])) < 0)
1880 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001881 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001882
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001884 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001885 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001886 /* we have to check the URI and auth for this request */
1887 if (stats_check_uri_auth(t, rule_set))
1888 return 1;
1889 }
1890
Willy Tarreau55ea7572007-06-17 19:56:27 +02001891 /* now check whether we have some switching rules for this request */
1892 if (!(t->flags & SN_BE_ASSIGNED)) {
1893 struct switching_rule *rule;
1894
1895 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
1896 int ret;
1897
1898 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
1899 if (cond->pol == ACL_COND_UNLESS)
1900 ret = !ret;
1901
1902 if (ret) {
1903 t->be = rule->be.backend;
1904 t->be->beconn++;
1905 if (t->be->beconn > t->be->beconn_max)
1906 t->be->beconn_max = t->be->beconn;
1907 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001908
1909 /* assign new parameters to the session from the new backend */
1910 t->rep->rto = t->req->wto = t->be->srvtimeout;
1911 t->req->cto = t->be->contimeout;
1912 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02001913 t->flags |= SN_BE_ASSIGNED;
1914 break;
1915 }
1916 }
1917 }
1918
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001919 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1920 /* No backend was set, but there was a default
1921 * backend set in the frontend, so we use it and
1922 * loop again.
1923 */
1924 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001925 t->be->beconn++;
1926 if (t->be->beconn > t->be->beconn_max)
1927 t->be->beconn_max = t->be->beconn;
1928 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001929
1930 /* assign new parameters to the session from the new backend */
1931 t->rep->rto = t->req->wto = t->be->srvtimeout;
1932 t->req->cto = t->be->contimeout;
1933 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001934 t->flags |= SN_BE_ASSIGNED;
1935 }
1936 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001937
Willy Tarreau58f10d72006-12-04 02:26:12 +01001938
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001939 if (!(t->flags & SN_BE_ASSIGNED)) {
1940 /* To ensure correct connection accounting on
1941 * the backend, we count the connection for the
1942 * one managing the queue.
1943 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001944 t->be->beconn++;
1945 if (t->be->beconn > t->be->beconn_max)
1946 t->be->beconn_max = t->be->beconn;
1947 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001948 t->flags |= SN_BE_ASSIGNED;
1949 }
1950
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001951 /*
1952 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001953 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001954 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001955 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001956 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001957
Willy Tarreau58f10d72006-12-04 02:26:12 +01001958
Willy Tarreau58f10d72006-12-04 02:26:12 +01001959
Willy Tarreau58f10d72006-12-04 02:26:12 +01001960
Willy Tarreau2a324282006-12-05 00:05:46 +01001961 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001962 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001963 * so let's do the same now.
1964 */
1965
1966 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001967 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001968 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001969 }
1970
1971
1972 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001973 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001974 * Note that doing so might move headers in the request, but
1975 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001976 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001977 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001978 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001979 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001980
Willy Tarreau58f10d72006-12-04 02:26:12 +01001981
Willy Tarreau2a324282006-12-05 00:05:46 +01001982 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001983 * 9: add X-Forwarded-For if either the frontend or the backend
1984 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001985 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001986 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001987 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001988 /* Add an X-Forwarded-For header unless the source IP is
1989 * in the 'except' network range.
1990 */
1991 if ((!t->fe->except_mask.s_addr ||
1992 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1993 != t->fe->except_net.s_addr) &&
1994 (!t->be->except_mask.s_addr ||
1995 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1996 != t->be->except_net.s_addr)) {
1997 int len;
1998 unsigned char *pn;
1999 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002000
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002001 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
2002 pn[0], pn[1], pn[2], pn[3]);
2003
2004 if (unlikely(http_header_add_tail2(req, &txn->req,
2005 &txn->hdr_idx, trash, len)) < 0)
2006 goto return_bad_req;
2007 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002008 }
2009 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002010 /* FIXME: for the sake of completeness, we should also support
2011 * 'except' here, although it is mostly useless in this case.
2012 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002013 int len;
2014 char pn[INET6_ADDRSTRLEN];
2015 inet_ntop(AF_INET6,
2016 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2017 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002018 len = sprintf(trash, "X-Forwarded-For: %s", pn);
2019 if (unlikely(http_header_add_tail2(req, &txn->req,
2020 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002021 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002022 }
2023 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002024
Willy Tarreau2a324282006-12-05 00:05:46 +01002025 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002026 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002027 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002028 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002029 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002030 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002031 if ((unlikely(msg->sl.rq.v_l != 8) ||
2032 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2033 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002034 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002035 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002036 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002037 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002038
Willy Tarreau2a324282006-12-05 00:05:46 +01002039 /*************************************************************
2040 * OK, that's finished for the headers. We have done what we *
2041 * could. Let's switch to the DATA state. *
2042 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002043
Willy Tarreau2a324282006-12-05 00:05:46 +01002044 t->cli_state = CL_STDATA;
2045 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002046
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002047 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002048
Willy Tarreaud825eef2007-05-12 22:35:00 +02002049 if (!tv_isset(&t->fe->clitimeout) ||
2050 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002051 /* If the client has no timeout, or if the server is not ready yet,
2052 * and we know for sure that it can expire, then it's cleaner to
2053 * disable the timeout on the client side so that too low values
2054 * cannot make the sessions abort too early.
2055 *
2056 * FIXME-20050705: the server needs a way to re-enable this time-out
2057 * when it switches its state, otherwise a client can stay connected
2058 * indefinitely. This now seems to be OK.
2059 */
2060 tv_eternity(&req->rex);
2061 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002062
Willy Tarreau2a324282006-12-05 00:05:46 +01002063 /* When a connection is tarpitted, we use the queue timeout for the
2064 * tarpit delay, which currently happens to be the server's connect
2065 * timeout. If unset, then set it to zero because we really want it
2066 * to expire at one moment.
2067 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002068 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002069 t->req->l = 0;
2070 /* flush the request so that we can drop the connection early
2071 * if the client closes first.
2072 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002073 if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout))
Willy Tarreaud825eef2007-05-12 22:35:00 +02002074 req->cex = now;
Willy Tarreau2a324282006-12-05 00:05:46 +01002075 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002077 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01002078 goto process_data;
2079
2080 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002081 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002082 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01002083 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002084 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002085 return_prx_cond:
2086 if (!(t->flags & SN_ERR_MASK))
2087 t->flags |= SN_ERR_PRXCOND;
2088 if (!(t->flags & SN_FINST_MASK))
2089 t->flags |= SN_FINST_R;
2090 return 1;
2091
Willy Tarreaubaaee002006-06-26 02:48:02 +02002092 }
2093 else if (c == CL_STDATA) {
2094 process_data:
2095 /* FIXME: this error handling is partly buggy because we always report
2096 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
2097 * or HEADER phase. BTW, it's not logical to expire the client while
2098 * we're waiting for the server to connect.
2099 */
2100 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002101 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002102 buffer_shutr(req);
2103 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002104 fd_delete(t->cli_fd);
2105 t->cli_state = CL_STCLOSE;
2106 if (!(t->flags & SN_ERR_MASK))
2107 t->flags |= SN_ERR_CLICL;
2108 if (!(t->flags & SN_FINST_MASK)) {
2109 if (t->pend_pos)
2110 t->flags |= SN_FINST_Q;
2111 else if (s == SV_STCONN)
2112 t->flags |= SN_FINST_C;
2113 else
2114 t->flags |= SN_FINST_D;
2115 }
2116 return 1;
2117 }
2118 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002119 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002120 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002121 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002122 t->cli_state = CL_STSHUTR;
2123 return 1;
2124 }
2125 /* last server read and buffer empty */
2126 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002127 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002128 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002129 shutdown(t->cli_fd, SHUT_WR);
2130 /* We must ensure that the read part is still alive when switching
2131 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002132 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002133 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002134 t->cli_state = CL_STSHUTW;
2135 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2136 return 1;
2137 }
2138 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002139 else if (tv_isle(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002140 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002141 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002142 t->cli_state = CL_STSHUTR;
2143 if (!(t->flags & SN_ERR_MASK))
2144 t->flags |= SN_ERR_CLITO;
2145 if (!(t->flags & SN_FINST_MASK)) {
2146 if (t->pend_pos)
2147 t->flags |= SN_FINST_Q;
2148 else if (s == SV_STCONN)
2149 t->flags |= SN_FINST_C;
2150 else
2151 t->flags |= SN_FINST_D;
2152 }
2153 return 1;
2154 }
2155 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002156 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002157 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002158 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002159 shutdown(t->cli_fd, SHUT_WR);
2160 /* We must ensure that the read part is still alive when switching
2161 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002162 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002163 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002164
2165 t->cli_state = CL_STSHUTW;
2166 if (!(t->flags & SN_ERR_MASK))
2167 t->flags |= SN_ERR_CLITO;
2168 if (!(t->flags & SN_FINST_MASK)) {
2169 if (t->pend_pos)
2170 t->flags |= SN_FINST_Q;
2171 else if (s == SV_STCONN)
2172 t->flags |= SN_FINST_C;
2173 else
2174 t->flags |= SN_FINST_D;
2175 }
2176 return 1;
2177 }
2178
2179 if (req->l >= req->rlim - req->data) {
2180 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002181 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002182 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002183 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002184 }
2185 } else {
2186 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002187 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud825eef2007-05-12 22:35:00 +02002188 if (!tv_isset(&t->fe->clitimeout) ||
2189 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout)))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002190 /* If the client has no timeout, or if the server not ready yet, and we
2191 * know for sure that it can expire, then it's cleaner to disable the
2192 * timeout on the client side so that too low values cannot make the
2193 * sessions abort too early.
2194 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002195 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002196 else
Willy Tarreaud825eef2007-05-12 22:35:00 +02002197 tv_add(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 }
2199 }
2200
2201 if ((rep->l == 0) ||
2202 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002203 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2204 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002205 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002206 }
2207 } else {
2208 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002209 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2210 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002211 if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002212 /* FIXME: to prevent the client from expiring read timeouts during writes,
2213 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002214 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002215 }
2216 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002217 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002218 }
2219 }
2220 return 0; /* other cases change nothing */
2221 }
2222 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002223 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002224 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002225 fd_delete(t->cli_fd);
2226 t->cli_state = CL_STCLOSE;
2227 if (!(t->flags & SN_ERR_MASK))
2228 t->flags |= SN_ERR_CLICL;
2229 if (!(t->flags & SN_FINST_MASK)) {
2230 if (t->pend_pos)
2231 t->flags |= SN_FINST_Q;
2232 else if (s == SV_STCONN)
2233 t->flags |= SN_FINST_C;
2234 else
2235 t->flags |= SN_FINST_D;
2236 }
2237 return 1;
2238 }
2239 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2240 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002241 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002242 fd_delete(t->cli_fd);
2243 t->cli_state = CL_STCLOSE;
2244 return 1;
2245 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002246 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002247 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002248 fd_delete(t->cli_fd);
2249 t->cli_state = CL_STCLOSE;
2250 if (!(t->flags & SN_ERR_MASK))
2251 t->flags |= SN_ERR_CLITO;
2252 if (!(t->flags & SN_FINST_MASK)) {
2253 if (t->pend_pos)
2254 t->flags |= SN_FINST_Q;
2255 else if (s == SV_STCONN)
2256 t->flags |= SN_FINST_C;
2257 else
2258 t->flags |= SN_FINST_D;
2259 }
2260 return 1;
2261 }
2262
2263 if (t->flags & SN_SELF_GEN) {
2264 produce_content(t);
2265 if (rep->l == 0) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002266 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002267 fd_delete(t->cli_fd);
2268 t->cli_state = CL_STCLOSE;
2269 return 1;
2270 }
2271 }
2272
2273 if ((rep->l == 0)
2274 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002275 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2276 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002277 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002278 }
2279 } else {
2280 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002281 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2282 /* restart writing */
Willy Tarreau33014d02007-06-03 15:25:37 +02002283 if (!tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002284 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002285 }
2286 }
2287 return 0;
2288 }
2289 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002290 if (req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002291 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002292 fd_delete(t->cli_fd);
2293 t->cli_state = CL_STCLOSE;
2294 if (!(t->flags & SN_ERR_MASK))
2295 t->flags |= SN_ERR_CLICL;
2296 if (!(t->flags & SN_FINST_MASK)) {
2297 if (t->pend_pos)
2298 t->flags |= SN_FINST_Q;
2299 else if (s == SV_STCONN)
2300 t->flags |= SN_FINST_C;
2301 else
2302 t->flags |= SN_FINST_D;
2303 }
2304 return 1;
2305 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002306 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002307 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002308 fd_delete(t->cli_fd);
2309 t->cli_state = CL_STCLOSE;
2310 return 1;
2311 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002312 else if (tv_isle(&req->rex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002313 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002314 fd_delete(t->cli_fd);
2315 t->cli_state = CL_STCLOSE;
2316 if (!(t->flags & SN_ERR_MASK))
2317 t->flags |= SN_ERR_CLITO;
2318 if (!(t->flags & SN_FINST_MASK)) {
2319 if (t->pend_pos)
2320 t->flags |= SN_FINST_Q;
2321 else if (s == SV_STCONN)
2322 t->flags |= SN_FINST_C;
2323 else
2324 t->flags |= SN_FINST_D;
2325 }
2326 return 1;
2327 }
2328 else if (req->l >= req->rlim - req->data) {
2329 /* no room to read more data */
2330
2331 /* FIXME-20050705: is it possible for a client to maintain a session
2332 * after the timeout by sending more data after it receives a close ?
2333 */
2334
Willy Tarreau66319382007-04-08 17:17:37 +02002335 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002336 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002337 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002338 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2339 }
2340 } else {
2341 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002342 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002343 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002344 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002345 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2346 }
2347 }
2348 return 0;
2349 }
2350 else { /* CL_STCLOSE: nothing to do */
2351 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2352 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002353 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 +02002354 write(1, trash, len);
2355 }
2356 return 0;
2357 }
2358 return 0;
2359}
2360
2361
2362/*
2363 * manages the server FSM and its socket. It returns 1 if a state has changed
2364 * (and a resync may be needed), 0 else.
2365 */
2366int process_srv(struct session *t)
2367{
2368 int s = t->srv_state;
2369 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002370 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002371 struct buffer *req = t->req;
2372 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002373 int conn_err;
2374
2375#ifdef DEBUG_FULL
2376 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2377#endif
Willy Tarreauee991362007-05-14 14:37:50 +02002378
2379#if 0
2380 fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n",
2381 __FUNCTION__, __LINE__,
2382 t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec,
2383 t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec,
2384 t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec);
2385 fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n",
2386 __FUNCTION__, __LINE__,
2387 t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec,
2388 t->be->contimeout.tv_sec, t->be->contimeout.tv_usec,
2389 t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec);
2390
2391 fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n",
2392 __FUNCTION__, __LINE__,
2393 req->cto.tv_sec, req->cto.tv_usec,
2394 req->rto.tv_sec, req->rto.tv_usec,
2395 req->wto.tv_sec, req->wto.tv_usec);
2396
2397 fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n",
2398 __FUNCTION__, __LINE__,
2399 rep->cto.tv_sec, rep->cto.tv_usec,
2400 rep->rto.tv_sec, rep->rto.tv_usec,
2401 rep->wto.tv_sec, rep->wto.tv_usec);
2402#endif
2403
Willy Tarreaubaaee002006-06-26 02:48:02 +02002404 //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 +02002405 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2406 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002407 //);
2408 if (s == SV_STIDLE) {
2409 if (c == CL_STHEADERS)
2410 return 0; /* stay in idle, waiting for data to reach the client side */
2411 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2412 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002413 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002414 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002415 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002416 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002417 /* note that this must not return any error because it would be able to
2418 * overwrite the client_retnclose() output.
2419 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002420 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002421 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002422 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002423 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 +02002424
2425 return 1;
2426 }
2427 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002428 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002429 /* This connection is being tarpitted. The CLIENT side has
2430 * already set the connect expiration date to the right
2431 * timeout. We just have to check that it has not expired.
2432 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002433 if (!tv_isle(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002434 return 0;
2435
2436 /* We will set the queue timer to the time spent, just for
2437 * logging purposes. We fake a 500 server error, so that the
2438 * attacker will not suspect his connection has been tarpitted.
2439 * It will not cause trouble to the logs because we can exclude
2440 * the tarpitted connections by filtering on the 'PT' status flags.
2441 */
2442 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002443 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002444 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002445 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002446 return 1;
2447 }
2448
Willy Tarreaubaaee002006-06-26 02:48:02 +02002449 /* Right now, we will need to create a connection to the server.
2450 * We might already have tried, and got a connection pending, in
2451 * which case we will not do anything till it's pending. It's up
2452 * to any other session to release it and wake us up again.
2453 */
2454 if (t->pend_pos) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002455 if (!tv_isle(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002456 return 0;
2457 else {
2458 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002459 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002460 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002461 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002462 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002463 if (t->srv)
2464 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002465 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002466 return 1;
2467 }
2468 }
2469
2470 do {
2471 /* first, get a connection */
2472 if (srv_redispatch_connect(t))
2473 return t->srv_state != SV_STIDLE;
2474
2475 /* try to (re-)connect to the server, and fail if we expire the
2476 * number of retries.
2477 */
2478 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002479 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002480 return t->srv_state != SV_STIDLE;
2481 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002482 } while (1);
2483 }
2484 }
2485 else if (s == SV_STCONN) { /* connection in progress */
2486 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2487 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002488 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2489 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002490 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002491 fd_delete(t->srv_fd);
2492 if (t->srv)
2493 t->srv->cur_sess--;
2494
2495 /* note that this must not return any error because it would be able to
2496 * overwrite the client_retnclose() output.
2497 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002498 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002499 return 1;
2500 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002501 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002502 //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 +02002503 return 0; /* nothing changed */
2504 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002505 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002506 /* timeout, asynchronous connect error or first write error */
2507 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2508
2509 fd_delete(t->srv_fd);
2510 if (t->srv)
2511 t->srv->cur_sess--;
2512
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002513 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002514 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2515 else
2516 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2517
2518 /* ensure that we have enough retries left */
2519 if (srv_count_retry_down(t, conn_err))
2520 return 1;
2521
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002522 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002523 /* We're on our last chance, and the REDISP option was specified.
2524 * We will ignore cookie and force to balance or use the dispatcher.
2525 */
2526 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002527 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002528 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002529
2530 if (t->srv)
2531 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002532 t->be->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002533
2534 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2535 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002536 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002537
2538 /* first, get a connection */
2539 if (srv_redispatch_connect(t))
2540 return t->srv_state != SV_STIDLE;
2541 }
2542
Willy Tarreaubaaee002006-06-26 02:48:02 +02002543 do {
2544 /* Now we will try to either reconnect to the same server or
2545 * connect to another server. If the connection gets queued
2546 * because all servers are saturated, then we will go back to
2547 * the SV_STIDLE state.
2548 */
2549 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002550 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002551 return t->srv_state != SV_STCONN;
2552 }
2553
2554 /* we need to redispatch the connection to another server */
2555 if (srv_redispatch_connect(t))
2556 return t->srv_state != SV_STCONN;
2557 } while (1);
2558 }
2559 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002560 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002561
2562 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2563 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002564 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002565 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002566 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002567 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002568 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002569 /* FIXME: to prevent the server from expiring read timeouts during writes,
2570 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002571 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002572 }
2573 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002574 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002575 }
2576
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002577 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002578 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002579 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002580 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002581
2582 t->srv_state = SV_STDATA;
2583 if (t->srv)
2584 t->srv->cum_sess++;
2585 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2586
2587 /* if the user wants to log as soon as possible, without counting
2588 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002589 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002590 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002591 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002592 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002593#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002594 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002595 /* TCP splicing supported by both FE and BE */
2596 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2597 }
2598#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002599 }
2600 else {
2601 t->srv_state = SV_STHEADERS;
2602 if (t->srv)
2603 t->srv->cum_sess++;
2604 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002605 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2606 /* reset hdr_idx which was already initialized by the request.
2607 * right now, the http parser does it.
2608 * hdr_idx_init(&t->txn.hdr_idx);
2609 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002610 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002611 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002612 return 1;
2613 }
2614 }
2615 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002616 /*
2617 * Now parse the partial (or complete) lines.
2618 * We will check the response syntax, and also join multi-line
2619 * headers. An index of all the lines will be elaborated while
2620 * parsing.
2621 *
2622 * For the parsing, we use a 28 states FSM.
2623 *
2624 * Here is the information we currently have :
2625 * rep->data + req->som = beginning of response
2626 * rep->data + req->eoh = end of processed headers / start of current one
2627 * rep->data + req->eol = end of current header or line (LF or CRLF)
2628 * rep->lr = first non-visited byte
2629 * rep->r = end of data
2630 */
2631
2632 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002633 struct http_msg *msg = &txn->rsp;
2634 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002635
Willy Tarreaua15645d2007-03-18 16:22:39 +01002636 if (likely(rep->lr < rep->r))
2637 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002638
Willy Tarreaua15645d2007-03-18 16:22:39 +01002639 /* 1: we might have to print this header in debug mode */
2640 if (unlikely((global.mode & MODE_DEBUG) &&
2641 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2642 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2643 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002644
Willy Tarreaua15645d2007-03-18 16:22:39 +01002645 sol = rep->data + msg->som;
2646 eol = sol + msg->sl.rq.l;
2647 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002648
Willy Tarreaua15645d2007-03-18 16:22:39 +01002649 sol += hdr_idx_first_pos(&txn->hdr_idx);
2650 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002651
Willy Tarreaua15645d2007-03-18 16:22:39 +01002652 while (cur_idx) {
2653 eol = sol + txn->hdr_idx.v[cur_idx].len;
2654 debug_hdr("srvhdr", t, sol, eol);
2655 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2656 cur_idx = txn->hdr_idx.v[cur_idx].next;
2657 }
2658 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002659
Willy Tarreaubaaee002006-06-26 02:48:02 +02002660
Willy Tarreau66319382007-04-08 17:17:37 +02002661 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002662 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002663 * full. We cannot loop here since stream_sock_read will disable it only if
2664 * rep->l == rlim-data
2665 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002666 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002667 tv_eternity(&rep->rex);
2668 }
2669
2670
2671 /*
2672 * Now we quickly check if we have found a full valid response.
2673 * If not so, we check the FD and buffer states before leaving.
2674 * A full response is indicated by the fact that we have seen
2675 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2676 * responses are checked first.
2677 *
2678 * Depending on whether the client is still there or not, we
2679 * may send an error response back or not. Note that normally
2680 * we should only check for HTTP status there, and check I/O
2681 * errors somewhere else.
2682 */
2683
2684 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2685
2686 /* Invalid response, or read error or write error */
2687 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2688 (req->flags & BF_WRITE_ERROR) ||
2689 (rep->flags & BF_READ_ERROR))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002690 buffer_shutr(rep);
2691 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002692 fd_delete(t->srv_fd);
2693 if (t->srv) {
2694 t->srv->cur_sess--;
2695 t->srv->failed_resp++;
2696 }
2697 t->be->failed_resp++;
2698 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002699 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002700 client_return(t, error_message(t, HTTP_ERR_502));
2701 if (!(t->flags & SN_ERR_MASK))
2702 t->flags |= SN_ERR_SRVCL;
2703 if (!(t->flags & SN_FINST_MASK))
2704 t->flags |= SN_FINST_H;
2705 /* We used to have a free connection slot. Since we'll never use it,
2706 * we have to inform the server that it may be used by another session.
2707 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002708 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002709 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002710
Willy Tarreaua15645d2007-03-18 16:22:39 +01002711 return 1;
2712 }
2713
2714 /* end of client write or end of server read.
2715 * since we are in header mode, if there's no space left for headers, we
2716 * won't be able to free more later, so the session will never terminate.
2717 */
2718 else if (unlikely(rep->flags & BF_READ_NULL ||
2719 c == CL_STSHUTW || c == CL_STCLOSE ||
2720 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002721 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002722 buffer_shutr(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002723 t->srv_state = SV_STSHUTR;
2724 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2725 return 1;
2726 }
2727
2728 /* read timeout : return a 504 to the client.
2729 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002730 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002731 tv_isle(&rep->rex, &now))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002732 buffer_shutr(rep);
2733 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002734 fd_delete(t->srv_fd);
2735 if (t->srv) {
2736 t->srv->cur_sess--;
2737 t->srv->failed_resp++;
2738 }
2739 t->be->failed_resp++;
2740 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002741 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002742 client_return(t, error_message(t, HTTP_ERR_504));
2743 if (!(t->flags & SN_ERR_MASK))
2744 t->flags |= SN_ERR_SRVTO;
2745 if (!(t->flags & SN_FINST_MASK))
2746 t->flags |= SN_FINST_H;
2747 /* We used to have a free connection slot. Since we'll never use it,
2748 * we have to inform the server that it may be used by another session.
2749 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002750 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002751 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002752 return 1;
2753 }
2754
2755 /* last client read and buffer empty */
2756 /* FIXME!!! here, we don't want to switch to SHUTW if the
2757 * client shuts read too early, because we may still have
2758 * some work to do on the headers.
2759 * The side-effect is that if the client completely closes its
2760 * connection during SV_STHEADER, the connection to the server
2761 * is kept until a response comes back or the timeout is reached.
2762 */
2763 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2764 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002765 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002766 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002767
2768 /* We must ensure that the read part is still
2769 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002770 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002771 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002772
2773 shutdown(t->srv_fd, SHUT_WR);
2774 t->srv_state = SV_STSHUTW;
2775 return 1;
2776 }
2777
2778 /* write timeout */
2779 /* FIXME!!! here, we don't want to switch to SHUTW if the
2780 * client shuts read too early, because we may still have
2781 * some work to do on the headers.
2782 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002783 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002784 tv_isle(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002785 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002786 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002787 shutdown(t->srv_fd, SHUT_WR);
2788 /* We must ensure that the read part is still alive
2789 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002790 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002791 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002792
2793 t->srv_state = SV_STSHUTW;
2794 if (!(t->flags & SN_ERR_MASK))
2795 t->flags |= SN_ERR_SRVTO;
2796 if (!(t->flags & SN_FINST_MASK))
2797 t->flags |= SN_FINST_H;
2798 return 1;
2799 }
2800
2801 /*
2802 * And now the non-error cases.
2803 */
2804
2805 /* Data remaining in the request buffer.
2806 * This happens during the first pass here, and during
2807 * long posts.
2808 */
2809 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002810 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2811 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002812 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002813 /* FIXME: to prevent the server from expiring read timeouts during writes,
2814 * we refresh it. */
2815 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002816 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002817 else
2818 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002819 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002820 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002821
Willy Tarreaua15645d2007-03-18 16:22:39 +01002822 /* nothing left in the request buffer */
2823 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002824 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2825 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002826 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002827 }
2828 }
2829
2830 return t->srv_state != SV_STHEADERS;
2831 }
2832
2833
2834 /*****************************************************************
2835 * More interesting part now : we know that we have a complete *
2836 * response which at least looks like HTTP. We have an indicator *
2837 * of each header's length, so we can parse them quickly. *
2838 ****************************************************************/
2839
Willy Tarreau9cdde232007-05-02 20:58:19 +02002840 /* ensure we keep this pointer to the beginning of the message */
2841 msg->sol = rep->data + msg->som;
2842
Willy Tarreaua15645d2007-03-18 16:22:39 +01002843 /*
2844 * 1: get the status code and check for cacheability.
2845 */
2846
2847 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002848 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002849
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002850 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002851 case 200:
2852 case 203:
2853 case 206:
2854 case 300:
2855 case 301:
2856 case 410:
2857 /* RFC2616 @13.4:
2858 * "A response received with a status code of
2859 * 200, 203, 206, 300, 301 or 410 MAY be stored
2860 * by a cache (...) unless a cache-control
2861 * directive prohibits caching."
2862 *
2863 * RFC2616 @9.5: POST method :
2864 * "Responses to this method are not cacheable,
2865 * unless the response includes appropriate
2866 * Cache-Control or Expires header fields."
2867 */
2868 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002869 unlikely(t->be->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002870 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002871 break;
2872 default:
2873 break;
2874 }
2875
2876 /*
2877 * 2: we may need to capture headers
2878 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002879 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002880 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002881 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002882
2883 /*
2884 * 3: we will have to evaluate the filters.
2885 * As opposed to version 1.2, now they will be evaluated in the
2886 * filters order and not in the header order. This means that
2887 * each filter has to be validated among all headers.
2888 *
2889 * Filters are tried with ->be first, then with ->fe if it is
2890 * different from ->be.
2891 */
2892
2893 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2894
2895 cur_proxy = t->be;
2896 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002897 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002898
2899 /* try headers filters */
2900 if (rule_set->rsp_exp != NULL) {
2901 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2902 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002903 if (t->srv) {
2904 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002905 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002906 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002907 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002908 return_srv_prx_502:
Willy Tarreaufa645582007-06-03 15:59:52 +02002909 buffer_shutr(rep);
2910 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002911 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002912 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002913 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002914 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002915 if (!(t->flags & SN_ERR_MASK))
2916 t->flags |= SN_ERR_PRXCOND;
2917 if (!(t->flags & SN_FINST_MASK))
2918 t->flags |= SN_FINST_H;
2919 /* We used to have a free connection slot. Since we'll never use it,
2920 * we have to inform the server that it may be used by another session.
2921 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002922 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002923 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002924 return 1;
2925 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002926 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002927
Willy Tarreaua15645d2007-03-18 16:22:39 +01002928 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002929 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002930 if (t->srv) {
2931 t->srv->cur_sess--;
2932 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002933 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002934 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002935 goto return_srv_prx_502;
2936 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002937
Willy Tarreaua15645d2007-03-18 16:22:39 +01002938 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002939 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002940 !(t->flags & SN_CONN_CLOSED)) {
2941 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002942 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002943 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002944
Willy Tarreaua15645d2007-03-18 16:22:39 +01002945 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2946 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002947
Willy Tarreaua15645d2007-03-18 16:22:39 +01002948 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2949 cur_hdr = &txn->hdr_idx.v[cur_idx];
2950 cur_ptr = cur_next;
2951 cur_end = cur_ptr + cur_hdr->len;
2952 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002953
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002954 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2955 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002956 /* 3 possibilities :
2957 * - we have already set Connection: close,
2958 * so we remove this line.
2959 * - we have not yet set Connection: close,
2960 * but this line indicates close. We leave
2961 * it untouched and set the flag.
2962 * - we have not yet set Connection: close,
2963 * and this line indicates non-close. We
2964 * replace it.
2965 */
2966 if (t->flags & SN_CONN_CLOSED) {
2967 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2968 txn->rsp.eoh += delta;
2969 cur_next += delta;
2970 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2971 txn->hdr_idx.used--;
2972 cur_hdr->len = 0;
2973 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002974 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2975 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2976 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002977 cur_next += delta;
2978 cur_hdr->len += delta;
2979 txn->rsp.eoh += delta;
2980 }
2981 t->flags |= SN_CONN_CLOSED;
2982 }
2983 }
2984 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002986 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002987
Willy Tarreaua15645d2007-03-18 16:22:39 +01002988 /* add response headers from the rule sets in the same order */
2989 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002990 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2991 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002992 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002993 }
2994
Willy Tarreaua15645d2007-03-18 16:22:39 +01002995 /* check whether we're already working on the frontend */
2996 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002997 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002998 cur_proxy = t->fe;
2999 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003000
Willy Tarreaua15645d2007-03-18 16:22:39 +01003001 /*
3002 * 4: check for server cookie.
3003 */
3004 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003005
Willy Tarreaua15645d2007-03-18 16:22:39 +01003006 /*
3007 * 5: add server cookie in the response if needed
3008 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003009 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3010 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003011 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003012
Willy Tarreaua15645d2007-03-18 16:22:39 +01003013 /* the server is known, it's not the one the client requested, we have to
3014 * insert a set-cookie here, except if we want to insert only on POST
3015 * requests and this one isn't. Note that servers which don't have cookies
3016 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003017 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003018 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003019 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003020 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003021
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003022 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3023 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003024 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01003025 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003026
Willy Tarreaua15645d2007-03-18 16:22:39 +01003027 /* Here, we will tell an eventual cache on the client side that we don't
3028 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3029 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3030 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3031 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003032 if (t->be->options & PR_O_COOK_NOC) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003033 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3034 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003035 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003036 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003037 }
3038
3039
3040 /*
3041 * 6: check for cache-control or pragma headers.
3042 */
3043 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003044
Willy Tarreaubaaee002006-06-26 02:48:02 +02003045
Willy Tarreaua15645d2007-03-18 16:22:39 +01003046 /*
3047 * 7: check if result will be cacheable with a cookie.
3048 * We'll block the response if security checks have caught
3049 * nasty things such as a cacheable cookie.
3050 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003051 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3052 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003053 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003054
Willy Tarreaua15645d2007-03-18 16:22:39 +01003055 /* we're in presence of a cacheable response containing
3056 * a set-cookie header. We'll block it as requested by
3057 * the 'checkcache' option, and send an alert.
3058 */
3059 if (t->srv) {
3060 t->srv->cur_sess--;
3061 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003062 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003063 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003064
Willy Tarreaua15645d2007-03-18 16:22:39 +01003065 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003066 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003067 send_log(t->be, LOG_ALERT,
3068 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003069 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003070 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003071 }
3072
Willy Tarreaua15645d2007-03-18 16:22:39 +01003073 /*
3074 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003075 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003076 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003077 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003078 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003079 if ((unlikely(msg->sl.st.v_l != 8) ||
3080 unlikely(req->data[msg->som + 7] != '0')) &&
3081 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003082 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003083 goto return_bad_resp;
3084 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003085 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003086
Willy Tarreaubaaee002006-06-26 02:48:02 +02003087
Willy Tarreaua15645d2007-03-18 16:22:39 +01003088 /*************************************************************
3089 * OK, that's finished for the headers. We have done what we *
3090 * could. Let's switch to the DATA state. *
3091 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003092
Willy Tarreaua15645d2007-03-18 16:22:39 +01003093 t->srv_state = SV_STDATA;
3094 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003095 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003096
3097 /* client connection already closed or option 'forceclose' required :
3098 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003099 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003100 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003101 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003102 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003103 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003104
3105 /* We must ensure that the read part is still alive when switching
3106 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003107 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003108 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003109
Willy Tarreaua15645d2007-03-18 16:22:39 +01003110 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003111 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003112 }
3113
Willy Tarreaua15645d2007-03-18 16:22:39 +01003114#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003115 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003116 /* TCP splicing supported by both FE and BE */
3117 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003118 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003119#endif
3120 /* if the user wants to log as soon as possible, without counting
3121 bytes from the server, then this is the right moment. */
3122 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3123 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01003124 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003125 if (t->fe->to_log & LW_REQ)
3126 http_sess_log(t);
3127 else
3128 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003129 }
3130
Willy Tarreaua15645d2007-03-18 16:22:39 +01003131 /* Note: we must not try to cheat by jumping directly to DATA,
3132 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003133 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003134
3135 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003136 }
3137 else if (s == SV_STDATA) {
3138 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003139 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02003140 buffer_shutr(rep);
3141 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003142 fd_delete(t->srv_fd);
3143 if (t->srv) {
3144 t->srv->cur_sess--;
3145 t->srv->failed_resp++;
3146 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003147 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003148 t->srv_state = SV_STCLOSE;
3149 if (!(t->flags & SN_ERR_MASK))
3150 t->flags |= SN_ERR_SRVCL;
3151 if (!(t->flags & SN_FINST_MASK))
3152 t->flags |= SN_FINST_D;
3153 /* We used to have a free connection slot. Since we'll never use it,
3154 * we have to inform the server that it may be used by another session.
3155 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003156 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003157 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003158
3159 return 1;
3160 }
3161 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003162 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003163 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003164 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003165 t->srv_state = SV_STSHUTR;
3166 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3167 return 1;
3168 }
3169 /* end of client read and no more data to send */
3170 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003171 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003172 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003173 shutdown(t->srv_fd, SHUT_WR);
3174 /* We must ensure that the read part is still alive when switching
3175 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003176 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003177 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003178
3179 t->srv_state = SV_STSHUTW;
3180 return 1;
3181 }
3182 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003183 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003184 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003185 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003186 t->srv_state = SV_STSHUTR;
3187 if (!(t->flags & SN_ERR_MASK))
3188 t->flags |= SN_ERR_SRVTO;
3189 if (!(t->flags & SN_FINST_MASK))
3190 t->flags |= SN_FINST_D;
3191 return 1;
3192 }
3193 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003194 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003195 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003196 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003197 shutdown(t->srv_fd, SHUT_WR);
3198 /* We must ensure that the read part is still alive when switching
3199 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003200 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003201 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003202 t->srv_state = SV_STSHUTW;
3203 if (!(t->flags & SN_ERR_MASK))
3204 t->flags |= SN_ERR_SRVTO;
3205 if (!(t->flags & SN_FINST_MASK))
3206 t->flags |= SN_FINST_D;
3207 return 1;
3208 }
3209
3210 /* recompute request time-outs */
3211 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003212 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3213 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003214 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003215 }
3216 }
3217 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003218 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3219 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003220 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003221 /* FIXME: to prevent the server from expiring read timeouts during writes,
3222 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003223 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003224 }
3225 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003226 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003227 }
3228 }
3229
3230 /* recompute response time-outs */
3231 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003232 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003233 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234 }
3235 }
3236 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003237 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003238 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003239 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003240 }
3241 }
3242
3243 return 0; /* other cases change nothing */
3244 }
3245 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003246 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003247 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003248 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003249 fd_delete(t->srv_fd);
3250 if (t->srv) {
3251 t->srv->cur_sess--;
3252 t->srv->failed_resp++;
3253 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003254 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003255 //close(t->srv_fd);
3256 t->srv_state = SV_STCLOSE;
3257 if (!(t->flags & SN_ERR_MASK))
3258 t->flags |= SN_ERR_SRVCL;
3259 if (!(t->flags & SN_FINST_MASK))
3260 t->flags |= SN_FINST_D;
3261 /* We used to have a free connection slot. Since we'll never use it,
3262 * we have to inform the server that it may be used by another session.
3263 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003264 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003265 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003266
3267 return 1;
3268 }
3269 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003270 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003271 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003272 fd_delete(t->srv_fd);
3273 if (t->srv)
3274 t->srv->cur_sess--;
3275 //close(t->srv_fd);
3276 t->srv_state = SV_STCLOSE;
3277 /* We used to have a free connection slot. Since we'll never use it,
3278 * we have to inform the server that it may be used by another session.
3279 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003280 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003281 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003282
3283 return 1;
3284 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003285 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003286 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003287 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003288 fd_delete(t->srv_fd);
3289 if (t->srv)
3290 t->srv->cur_sess--;
3291 //close(t->srv_fd);
3292 t->srv_state = SV_STCLOSE;
3293 if (!(t->flags & SN_ERR_MASK))
3294 t->flags |= SN_ERR_SRVTO;
3295 if (!(t->flags & SN_FINST_MASK))
3296 t->flags |= SN_FINST_D;
3297 /* We used to have a free connection slot. Since we'll never use it,
3298 * we have to inform the server that it may be used by another session.
3299 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003300 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003301 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003302
3303 return 1;
3304 }
3305 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003306 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3307 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003308 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003309 }
3310 }
3311 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003312 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3313 /* restart writing */
Willy Tarreau33014d02007-06-03 15:25:37 +02003314 if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003315 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003316 }
3317 }
3318 return 0;
3319 }
3320 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003321 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003322 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003323 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003324 fd_delete(t->srv_fd);
3325 if (t->srv) {
3326 t->srv->cur_sess--;
3327 t->srv->failed_resp++;
3328 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003329 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003330 //close(t->srv_fd);
3331 t->srv_state = SV_STCLOSE;
3332 if (!(t->flags & SN_ERR_MASK))
3333 t->flags |= SN_ERR_SRVCL;
3334 if (!(t->flags & SN_FINST_MASK))
3335 t->flags |= SN_FINST_D;
3336 /* We used to have a free connection slot. Since we'll never use it,
3337 * we have to inform the server that it may be used by another session.
3338 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003339 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003340 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003341
3342 return 1;
3343 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003344 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003345 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003346 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003347 fd_delete(t->srv_fd);
3348 if (t->srv)
3349 t->srv->cur_sess--;
3350 //close(t->srv_fd);
3351 t->srv_state = SV_STCLOSE;
3352 /* We used to have a free connection slot. Since we'll never use it,
3353 * we have to inform the server that it may be used by another session.
3354 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003355 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003356 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003357
3358 return 1;
3359 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003360 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003361 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003362 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003363 fd_delete(t->srv_fd);
3364 if (t->srv)
3365 t->srv->cur_sess--;
3366 //close(t->srv_fd);
3367 t->srv_state = SV_STCLOSE;
3368 if (!(t->flags & SN_ERR_MASK))
3369 t->flags |= SN_ERR_SRVTO;
3370 if (!(t->flags & SN_FINST_MASK))
3371 t->flags |= SN_FINST_D;
3372 /* We used to have a free connection slot. Since we'll never use it,
3373 * we have to inform the server that it may be used by another session.
3374 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003375 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003376 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003377
3378 return 1;
3379 }
3380 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003381 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003382 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003383 }
3384 }
3385 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003386 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003387 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003388 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003389 }
3390 }
3391 return 0;
3392 }
3393 else { /* SV_STCLOSE : nothing to do */
3394 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3395 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003396 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003397 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003398 write(1, trash, len);
3399 }
3400 return 0;
3401 }
3402 return 0;
3403}
3404
3405
3406/*
3407 * Produces data for the session <s> depending on its source. Expects to be
3408 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3409 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3410 * session, which it uses to keep on being called when there is free space in
3411 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3412 * if it changes the session state from CL_STSHUTR, otherwise 0.
3413 */
3414int produce_content(struct session *s)
3415{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003416 if (s->data_source == DATA_SRC_NONE) {
3417 s->flags &= ~SN_SELF_GEN;
3418 return 1;
3419 }
3420 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003421 /* dump server statistics */
3422 return produce_content_stats(s);
3423 }
3424 else {
3425 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003426 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003427 client_retnclose(s, error_message(s, HTTP_ERR_500));
3428 if (!(s->flags & SN_ERR_MASK))
3429 s->flags |= SN_ERR_PRXCOND;
3430 if (!(s->flags & SN_FINST_MASK))
3431 s->flags |= SN_FINST_R;
3432 s->flags &= ~SN_SELF_GEN;
3433 return 1;
3434 }
3435}
3436
3437
3438/*
3439 * Produces statistics data for the session <s>. Expects to be called with
3440 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3441 * flag from the session, which it uses to keep on being called when there is
3442 * free space in the buffer, of simply by letting an empty buffer upon return.
3443 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3444 */
3445int produce_content_stats(struct session *s)
3446{
3447 struct buffer *rep = s->rep;
3448 struct proxy *px;
3449 struct chunk msg;
3450 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003451
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003452 msg.len = 0;
3453 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003454
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003455 switch (s->data_state) {
3456 case DATA_ST_INIT:
3457 /* the function had not been called yet */
3458 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003459
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003460 chunk_printf(&msg, sizeof(trash),
3461 "HTTP/1.0 200 OK\r\n"
3462 "Cache-Control: no-cache\r\n"
3463 "Connection: close\r\n"
Willy Tarreaubbd42122007-07-25 07:26:38 +02003464 "Content-Type: text/html\r\n");
3465
Willy Tarreaue7150cd2007-07-25 14:43:32 +02003466 if (s->be->uri_auth->refresh > 0 && !(s->flags & SN_STAT_NORFRSH))
Willy Tarreaubbd42122007-07-25 07:26:38 +02003467 chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n",
3468 s->be->uri_auth->refresh);
3469
3470 chunk_printf(&msg, sizeof(trash), "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003471
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003472 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003473 client_retnclose(s, &msg); // send the start of the response.
3474 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003475
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003476 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3477 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3478 if (!(s->flags & SN_FINST_MASK))
3479 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003480
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003481 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003482 /* that's all we return in case of HEAD request */
3483 s->data_state = DATA_ST_FIN;
3484 s->flags &= ~SN_SELF_GEN;
3485 return 1;
3486 }
3487
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003488 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3489 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003490
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003491 case DATA_ST_HEAD:
3492 /* WARNING! This must fit in the first buffer !!! */
3493 chunk_printf(&msg, sizeof(trash),
3494 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3495 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3496 "<style type=\"text/css\"><!--\n"
3497 "body {"
3498 " font-family: helvetica, arial;"
3499 " font-size: 12px;"
3500 " font-weight: normal;"
3501 " color: black;"
3502 " background: white;"
3503 "}\n"
3504 "th,td {"
3505 " font-size: 0.8em;"
3506 " align: center;"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003507 "}\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003508 "h1 {"
3509 " font-size: xx-large;"
3510 " margin-bottom: 0.5em;"
3511 "}\n"
3512 "h2 {"
3513 " font-family: helvetica, arial;"
3514 " font-size: x-large;"
3515 " font-weight: bold;"
3516 " font-style: italic;"
3517 " color: #6020a0;"
3518 " margin-top: 0em;"
3519 " margin-bottom: 0em;"
3520 "}\n"
3521 "h3 {"
3522 " font-family: helvetica, arial;"
3523 " font-size: 16px;"
3524 " font-weight: bold;"
3525 " color: #b00040;"
3526 " background: #e8e8d0;"
3527 " margin-top: 0em;"
3528 " margin-bottom: 0em;"
3529 "}\n"
3530 "li {"
3531 " margin-top: 0.25em;"
3532 " margin-right: 2em;"
3533 "}\n"
3534 ".hr {margin-top: 0.25em;"
3535 " border-color: black;"
3536 " border-bottom-style: solid;"
3537 "}\n"
3538 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3539 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3540 ".total {background: #20D0D0;color: #ffff80;}\n"
3541 ".frontend {background: #e8e8d0;}\n"
3542 ".backend {background: #e8e8d0;}\n"
3543 ".active0 {background: #ff9090;}\n"
3544 ".active1 {background: #ffd020;}\n"
3545 ".active2 {background: #ffffa0;}\n"
3546 ".active3 {background: #c0ffc0;}\n"
3547 ".active4 {background: #e0e0e0;}\n"
3548 ".backup0 {background: #ff9090;}\n"
3549 ".backup1 {background: #ff80ff;}\n"
3550 ".backup2 {background: #c060ff;}\n"
3551 ".backup3 {background: #b0d0ff;}\n"
3552 ".backup4 {background: #e0e0e0;}\n"
3553 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003554 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003555 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3556 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3557 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3558 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3559 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003560 "-->\n"
3561 "</style></head>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003562
3563 if (buffer_write_chunk(rep, &msg) != 0)
3564 return 0;
3565
3566 s->data_state = DATA_ST_INFO;
3567 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003568
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003569 case DATA_ST_INFO:
3570 up = (now.tv_sec - start_date.tv_sec);
3571
3572 /* WARNING! this has to fit the first packet too.
3573 * We are around 3.5 kB, add adding entries will
3574 * become tricky if we want to support 4kB buffers !
3575 */
3576 chunk_printf(&msg, sizeof(trash),
3577 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3578 PRODUCT_NAME "</a></h1>\n"
3579 "<h2>Statistics Report for pid %d</h2>\n"
3580 "<hr width=\"100%%\" class=\"hr\">\n"
3581 "<h3>&gt; General process information</h3>\n"
Willy Tarreaue7150cd2007-07-25 14:43:32 +02003582 "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003583 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3584 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3585 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3586 "<b>maxsock = </b> %d<br>\n"
3587 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3588 "</td><td align=\"center\" nowrap>\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003589 "<table class=\"lgd\"><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003590 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3591 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003592 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003593 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3594 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003595 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003596 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3597 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003598 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003599 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3600 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3601 "</tr></table>\n"
3602 "</td>"
Willy Tarreaue7150cd2007-07-25 14:43:32 +02003603 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3604 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003605 "",
3606 pid, pid, global.nbproc,
3607 up / 86400, (up % 86400) / 3600,
3608 (up % 3600) / 60, (up % 60),
3609 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3610 global.rlimit_memmax ? " MB" : "",
3611 global.rlimit_nofile,
3612 global.maxsock,
3613 global.maxconn,
3614 actconn
3615 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003616
Willy Tarreaue7150cd2007-07-25 14:43:32 +02003617 if (s->flags & SN_STAT_HIDEDWN)
3618 chunk_printf(&msg, sizeof(trash),
3619 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
3620 s->be->uri_auth->uri_prefix,
3621 "",
3622 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
3623 else
3624 chunk_printf(&msg, sizeof(trash),
3625 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3626 s->be->uri_auth->uri_prefix,
3627 ";up",
3628 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
3629
3630 if (s->be->uri_auth->refresh > 0) {
3631 if (s->flags & SN_STAT_NORFRSH)
3632 chunk_printf(&msg, sizeof(trash),
3633 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
3634 s->be->uri_auth->uri_prefix,
3635 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
3636 "");
3637 else
3638 chunk_printf(&msg, sizeof(trash),
3639 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
3640 s->be->uri_auth->uri_prefix,
3641 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
3642 ";norefresh");
3643 }
3644
3645 chunk_printf(&msg, sizeof(trash),
3646 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
3647 s->be->uri_auth->uri_prefix,
3648 (s->flags & SN_STAT_HIDEDWN) ? ";up" : "",
3649 (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : "");
3650
3651 chunk_printf(&msg, sizeof(trash),
3652 "</td>"
3653 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3654 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
3655 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3656 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3657 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3658 "</ul>"
3659 "</td>"
3660 "</tr></table>\n"
3661 ""
3662 );
3663
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003664 if (buffer_write_chunk(rep, &msg) != 0)
3665 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003666
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003667 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003668
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003669 s->data_ctx.stats.px = proxy;
3670 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3671 s->data_state = DATA_ST_LIST;
3672 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003673
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003674 case DATA_ST_LIST:
3675 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003676 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003677 px = s->data_ctx.stats.px;
3678 /* skip the disabled proxies and non-networked ones */
3679 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3680 if (produce_content_stats_proxy(s, px) == 0)
3681 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003682
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003683 s->data_ctx.stats.px = px->next;
3684 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3685 }
3686 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003687
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003688 s->data_state = DATA_ST_END;
3689 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003690
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003691 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003692 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003693 if (buffer_write_chunk(rep, &msg) != 0)
3694 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003695
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003696 s->data_state = DATA_ST_FIN;
3697 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003698
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003699 case DATA_ST_FIN:
3700 s->flags &= ~SN_SELF_GEN;
3701 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003702
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003703 default:
3704 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003705 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003706 client_retnclose(s, error_message(s, HTTP_ERR_500));
3707 if (!(s->flags & SN_ERR_MASK))
3708 s->flags |= SN_ERR_PRXCOND;
3709 if (!(s->flags & SN_FINST_MASK))
3710 s->flags |= SN_FINST_R;
3711 s->flags &= ~SN_SELF_GEN;
3712 return 1;
3713 }
3714}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003715
Willy Tarreaubaaee002006-06-26 02:48:02 +02003716
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003717/*
3718 * Dumps statistics for a proxy.
3719 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3720 * ot non-zero if everything completed.
3721 */
3722int produce_content_stats_proxy(struct session *s, struct proxy *px)
3723{
3724 struct buffer *rep = s->rep;
3725 struct server *sv;
3726 struct chunk msg;
3727
3728 msg.len = 0;
3729 msg.str = trash;
3730
3731 switch (s->data_ctx.stats.px_st) {
3732 case DATA_ST_PX_INIT:
3733 /* we are on a new proxy */
3734
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003735 if (s->be->uri_auth && s->be->uri_auth->scope) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003736 /* we have a limited scope, we have to check the proxy name */
3737 struct stat_scope *scope;
3738 int len;
3739
3740 len = strlen(px->id);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003741 scope = s->be->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003742
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003743 while (scope) {
3744 /* match exact proxy name */
3745 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3746 break;
3747
3748 /* match '.' which means 'self' proxy */
3749 if (!strcmp(scope->px_id, ".") && px == s->fe)
3750 break;
3751 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003752 }
3753
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003754 /* proxy name not found : don't dump anything */
3755 if (scope == NULL)
3756 return 1;
3757 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003758
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003759 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3760 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003761
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003762 case DATA_ST_PX_TH:
3763 /* print a new table */
3764 chunk_printf(&msg, sizeof(trash),
3765 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3766 "<tr align=\"center\" class=\"titre\">"
3767 "<th colspan=2 class=\"pxname\">%s</th>"
3768 "<th colspan=18 class=\"empty\"></th>"
3769 "</tr>\n"
3770 "<tr align=\"center\" class=\"titre\">"
3771 "<th rowspan=2></th>"
3772 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3773 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3774 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3775 "</tr>\n"
3776 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003777 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3778 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003779 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3780 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3781 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3782 "",
3783 px->id);
3784
3785 if (buffer_write_chunk(rep, &msg) != 0)
3786 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003787
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003788 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3789 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003790
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003791 case DATA_ST_PX_FE:
3792 /* print the frontend */
3793 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003794 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003795 /* name, queue */
3796 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3797 /* sessions : current, max, limit, cumul. */
3798 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3799 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003800 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003801 /* denied: req, resp */
3802 "<td align=right>%d</td><td align=right>%d</td>"
3803 /* errors : request, connect, response */
3804 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3805 /* server status : reflect backend status */
3806 "<td align=center>%s</td>"
3807 /* rest of server: nothing */
3808 "<td align=center colspan=5></td></tr>"
3809 "",
3810 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003811 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003812 px->denied_req, px->denied_resp,
3813 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003814 px->state == PR_STRUN ? "OPEN" :
3815 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003816
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003817 if (buffer_write_chunk(rep, &msg) != 0)
3818 return 0;
3819 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003820
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003821 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3822 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3823 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003824
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003825 case DATA_ST_PX_SV:
3826 /* stats.sv has been initialized above */
3827 while (s->data_ctx.stats.sv != NULL) {
3828 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3829 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003830
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003831 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003832
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003833 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3834 if (!(sv->state & SRV_CHECKED))
3835 sv_state = 4;
3836 else if (sv->state & SRV_RUNNING)
3837 if (sv->health == sv->rise + sv->fall - 1)
3838 sv_state = 3; /* UP */
3839 else
3840 sv_state = 2; /* going down */
3841 else
3842 if (sv->health)
3843 sv_state = 1; /* going up */
3844 else
3845 sv_state = 0; /* DOWN */
3846
Willy Tarreaue7150cd2007-07-25 14:43:32 +02003847 if ((sv_state == 0) && (s->flags & SN_STAT_HIDEDWN)) {
3848 /* do not report servers which are DOWN */
3849 s->data_ctx.stats.sv = sv->next;
3850 continue;
3851 }
3852
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003853 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003854 /* name */
3855 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3856 /* queue : current, max */
3857 "<td align=right>%d</td><td align=right>%d</td>"
3858 /* sessions : current, max, limit, cumul */
3859 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3860 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003861 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003862 /* denied: req, resp */
3863 "<td align=right></td><td align=right>%d</td>"
3864 /* errors : request, connect, response */
3865 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3866 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003867 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003868 sv_state, sv->id,
3869 sv->nbpend, sv->nbpend_max,
3870 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003871 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003872 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003873 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003874
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003875 /* status */
3876 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3877 chunk_printf(&msg, sizeof(trash),
3878 srv_hlt_st[sv_state],
3879 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3880 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3881
Willy Tarreau128e9542007-01-01 22:01:43 +01003882 chunk_printf(&msg, sizeof(trash),
3883 /* weight */
3884 "</td><td>%d</td>"
3885 /* act, bck */
3886 "<td>%s</td><td>%s</td>"
3887 "",
Willy Tarreau417fae02007-03-25 21:16:40 +02003888 sv->uweight,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003889 (sv->state & SRV_BACKUP) ? "-" : "Y",
3890 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003891
3892 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003893 if (sv->state & SRV_CHECKED)
3894 chunk_printf(&msg, sizeof(trash),
3895 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3896 sv->failed_checks, sv->down_trans);
3897 else
3898 chunk_printf(&msg, sizeof(trash),
3899 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003900
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003901 if (buffer_write_chunk(rep, &msg) != 0)
3902 return 0;
3903
3904 s->data_ctx.stats.sv = sv->next;
3905 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003906
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003907 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3908 /* fall through */
3909
3910 case DATA_ST_PX_BE:
3911 /* print the backend */
3912 if (px->cap & PR_CAP_BE) {
Willy Tarreau4b946c82007-07-25 00:28:06 +02003913 int gcd = 1;
3914
Willy Tarreau5af3a692007-07-24 23:32:33 +02003915 if (px->map_state & PR_MAP_RECALC)
3916 recalc_server_map(px);
3917
Willy Tarreau4b946c82007-07-25 00:28:06 +02003918 /* The GCD which was computed causes the total effective
3919 * weight to appear lower than all weights. Let's
3920 * recompute it.
3921 */
3922 if (px->srv && px->srv->eweight)
3923 gcd = px->srv->uweight / px->srv->eweight;
3924
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003925 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003926 /* name */
3927 "<tr align=center class=\"backend\"><td>Backend</td>"
3928 /* queue : current, max */
3929 "<td align=right>%d</td><td align=right>%d</td>"
3930 /* sessions : current, max, limit, cumul. */
3931 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3932 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003933 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003934 /* denied: req, resp */
3935 "<td align=right>%d</td><td align=right>%d</td>"
3936 /* errors : request, connect, response */
3937 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3938 /* server status : reflect backend status (up/down) : we display UP
3939 * if the backend has known working servers or if it has no server at
3940 * all (eg: for stats). Tthen we display the total weight, number of
3941 * active and backups. */
3942 "<td align=center>%s</td><td align=center>%d</td>"
3943 "<td align=center>%d</td><td align=center>%d</td>"
3944 /* rest of server: nothing */
3945 "<td align=center colspan=2></td></tr>"
3946 "",
3947 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3948 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003949 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003950 px->denied_req, px->denied_resp,
3951 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003952 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau4b946c82007-07-25 00:28:06 +02003953 px->srv_map_sz * gcd, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003954
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003955 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003956 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003957 }
3958
3959 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3960 /* fall through */
3961
3962 case DATA_ST_PX_END:
3963 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3964
3965 if (buffer_write_chunk(rep, &msg) != 0)
3966 return 0;
3967
3968 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3969 /* fall through */
3970
3971 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003972 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003973
3974 default:
3975 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003976 return 1;
3977 }
3978}
3979
3980
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003981/* Iterate the same filter through all request headers.
3982 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003983 * Since it can manage the switch to another backend, it updates the per-proxy
3984 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003985 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003986int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003987{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003988 char term;
3989 char *cur_ptr, *cur_end, *cur_next;
3990 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003991 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003992 struct hdr_idx_elem *cur_hdr;
3993 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003994
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003995 last_hdr = 0;
3996
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003997 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003998 old_idx = 0;
3999
4000 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004001 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004002 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004003 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004004 (exp->action == ACT_ALLOW ||
4005 exp->action == ACT_DENY ||
4006 exp->action == ACT_TARPIT))
4007 return 0;
4008
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004009 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004010 if (!cur_idx)
4011 break;
4012
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004013 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004014 cur_ptr = cur_next;
4015 cur_end = cur_ptr + cur_hdr->len;
4016 cur_next = cur_end + cur_hdr->cr + 1;
4017
4018 /* Now we have one header between cur_ptr and cur_end,
4019 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004020 */
4021
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004022 /* The annoying part is that pattern matching needs
4023 * that we modify the contents to null-terminate all
4024 * strings before testing them.
4025 */
4026
4027 term = *cur_end;
4028 *cur_end = '\0';
4029
4030 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4031 switch (exp->action) {
4032 case ACT_SETBE:
4033 /* It is not possible to jump a second time.
4034 * FIXME: should we return an HTTP/500 here so that
4035 * the admin knows there's a problem ?
4036 */
4037 if (t->be != t->fe)
4038 break;
4039
4040 /* Swithing Proxy */
4041 t->be = (struct proxy *) exp->replace;
4042
4043 /* right now, the backend switch is not too much complicated
4044 * because we have associated req_cap and rsp_cap to the
4045 * frontend, and the beconn will be updated later.
4046 */
4047
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004048 t->rep->rto = t->req->wto = t->be->srvtimeout;
4049 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004050 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004051 last_hdr = 1;
4052 break;
4053
4054 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004055 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004056 last_hdr = 1;
4057 break;
4058
4059 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004060 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004061 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004062 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004063 break;
4064
4065 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004066 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004067 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004068 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004069 break;
4070
4071 case ACT_REPLACE:
4072 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4073 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4074 /* FIXME: if the user adds a newline in the replacement, the
4075 * index will not be recalculated for now, and the new line
4076 * will not be counted as a new header.
4077 */
4078
4079 cur_end += delta;
4080 cur_next += delta;
4081 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004082 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004083 break;
4084
4085 case ACT_REMOVE:
4086 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4087 cur_next += delta;
4088
4089 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004090 txn->req.eoh += delta;
4091 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4092 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004093 cur_hdr->len = 0;
4094 cur_end = NULL; /* null-term has been rewritten */
4095 break;
4096
4097 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004098 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004099 if (cur_end)
4100 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004102 /* keep the link from this header to next one in case of later
4103 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004104 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004105 old_idx = cur_idx;
4106 }
4107 return 0;
4108}
4109
4110
4111/* Apply the filter to the request line.
4112 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4113 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004114 * Since it can manage the switch to another backend, it updates the per-proxy
4115 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004116 */
4117int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4118{
4119 char term;
4120 char *cur_ptr, *cur_end;
4121 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004122 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004123 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004124
Willy Tarreau58f10d72006-12-04 02:26:12 +01004125
Willy Tarreau3d300592007-03-18 18:34:41 +01004126 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004127 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004128 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004129 (exp->action == ACT_ALLOW ||
4130 exp->action == ACT_DENY ||
4131 exp->action == ACT_TARPIT))
4132 return 0;
4133 else if (exp->action == ACT_REMOVE)
4134 return 0;
4135
4136 done = 0;
4137
Willy Tarreau9cdde232007-05-02 20:58:19 +02004138 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004139 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004140
4141 /* Now we have the request line between cur_ptr and cur_end */
4142
4143 /* The annoying part is that pattern matching needs
4144 * that we modify the contents to null-terminate all
4145 * strings before testing them.
4146 */
4147
4148 term = *cur_end;
4149 *cur_end = '\0';
4150
4151 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4152 switch (exp->action) {
4153 case ACT_SETBE:
4154 /* It is not possible to jump a second time.
4155 * FIXME: should we return an HTTP/500 here so that
4156 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004157 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004158 if (t->be != t->fe)
4159 break;
4160
4161 /* Swithing Proxy */
4162 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004163
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004164 /* right now, the backend switch is not too much complicated
4165 * because we have associated req_cap and rsp_cap to the
4166 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004167 */
4168
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004169 t->rep->rto = t->req->wto = t->be->srvtimeout;
4170 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004171 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004172 done = 1;
4173 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004175 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004176 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004177 done = 1;
4178 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004179
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004180 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004181 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004182 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004183 done = 1;
4184 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004186 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004187 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004188 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004189 done = 1;
4190 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004191
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004192 case ACT_REPLACE:
4193 *cur_end = term; /* restore the string terminator */
4194 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4195 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4196 /* FIXME: if the user adds a newline in the replacement, the
4197 * index will not be recalculated for now, and the new line
4198 * will not be counted as a new header.
4199 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004200
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004201 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004202 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004203
Willy Tarreau9cdde232007-05-02 20:58:19 +02004204 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004205 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004206 HTTP_MSG_RQMETH,
4207 cur_ptr, cur_end + 1,
4208 NULL, NULL);
4209 if (unlikely(!cur_end))
4210 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004211
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004212 /* we have a full request and we know that we have either a CR
4213 * or an LF at <ptr>.
4214 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004215 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4216 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004217 /* there is no point trying this regex on headers */
4218 return 1;
4219 }
4220 }
4221 *cur_end = term; /* restore the string terminator */
4222 return done;
4223}
Willy Tarreau97de6242006-12-27 17:18:38 +01004224
Willy Tarreau58f10d72006-12-04 02:26:12 +01004225
Willy Tarreau58f10d72006-12-04 02:26:12 +01004226
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004227/*
4228 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4229 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004230 * unparsable request. Since it can manage the switch to another backend, it
4231 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004232 */
4233int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4234{
Willy Tarreau3d300592007-03-18 18:34:41 +01004235 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004236 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004237 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004238 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004240 /*
4241 * The interleaving of transformations and verdicts
4242 * makes it difficult to decide to continue or stop
4243 * the evaluation.
4244 */
4245
Willy Tarreau3d300592007-03-18 18:34:41 +01004246 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004247 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4248 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4249 exp = exp->next;
4250 continue;
4251 }
4252
4253 /* Apply the filter to the request line. */
4254 ret = apply_filter_to_req_line(t, req, exp);
4255 if (unlikely(ret < 0))
4256 return -1;
4257
4258 if (likely(ret == 0)) {
4259 /* The filter did not match the request, it can be
4260 * iterated through all headers.
4261 */
4262 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004263 }
4264 exp = exp->next;
4265 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004266 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004267}
4268
4269
Willy Tarreaua15645d2007-03-18 16:22:39 +01004270
Willy Tarreau58f10d72006-12-04 02:26:12 +01004271/*
4272 * Manager client-side cookie
4273 */
4274void manage_client_side_cookies(struct session *t, struct buffer *req)
4275{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004276 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004277 char *p1, *p2, *p3, *p4;
4278 char *del_colon, *del_cookie, *colon;
4279 int app_cookies;
4280
4281 appsess *asession_temp = NULL;
4282 appsess local_asession;
4283
4284 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004285 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004286
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004287 if (t->be->cookie_name == NULL &&
4288 t->be->appsession_name == NULL &&
4289 t->be->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004290 return;
4291
Willy Tarreau2a324282006-12-05 00:05:46 +01004292 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004293 * we start with the start line.
4294 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004295 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004296 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004297
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004298 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004299 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004300 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004301
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004302 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004303 cur_ptr = cur_next;
4304 cur_end = cur_ptr + cur_hdr->len;
4305 cur_next = cur_end + cur_hdr->cr + 1;
4306
4307 /* We have one full header between cur_ptr and cur_end, and the
4308 * next header starts at cur_next. We're only interested in
4309 * "Cookie:" headers.
4310 */
4311
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004312 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4313 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004314 old_idx = cur_idx;
4315 continue;
4316 }
4317
4318 /* Now look for cookies. Conforming to RFC2109, we have to support
4319 * attributes whose name begin with a '$', and associate them with
4320 * the right cookie, if we want to delete this cookie.
4321 * So there are 3 cases for each cookie read :
4322 * 1) it's a special attribute, beginning with a '$' : ignore it.
4323 * 2) it's a server id cookie that we *MAY* want to delete : save
4324 * some pointers on it (last semi-colon, beginning of cookie...)
4325 * 3) it's an application cookie : we *MAY* have to delete a previous
4326 * "special" cookie.
4327 * At the end of loop, if a "special" cookie remains, we may have to
4328 * remove it. If no application cookie persists in the header, we
4329 * *MUST* delete it
4330 */
4331
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004332 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004333
Willy Tarreau58f10d72006-12-04 02:26:12 +01004334 /* del_cookie == NULL => nothing to be deleted */
4335 del_colon = del_cookie = NULL;
4336 app_cookies = 0;
4337
4338 while (p1 < cur_end) {
4339 /* skip spaces and colons, but keep an eye on these ones */
4340 while (p1 < cur_end) {
4341 if (*p1 == ';' || *p1 == ',')
4342 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004343 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004344 break;
4345 p1++;
4346 }
4347
4348 if (p1 == cur_end)
4349 break;
4350
4351 /* p1 is at the beginning of the cookie name */
4352 p2 = p1;
4353 while (p2 < cur_end && *p2 != '=')
4354 p2++;
4355
4356 if (p2 == cur_end)
4357 break;
4358
4359 p3 = p2 + 1; /* skips the '=' sign */
4360 if (p3 == cur_end)
4361 break;
4362
4363 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004364 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004365 p4++;
4366
4367 /* here, we have the cookie name between p1 and p2,
4368 * and its value between p3 and p4.
4369 * we can process it :
4370 *
4371 * Cookie: NAME=VALUE;
4372 * | || || |
4373 * | || || +--> p4
4374 * | || |+-------> p3
4375 * | || +--------> p2
4376 * | |+------------> p1
4377 * | +-------------> colon
4378 * +--------------------> cur_ptr
4379 */
4380
4381 if (*p1 == '$') {
4382 /* skip this one */
4383 }
4384 else {
4385 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004386 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004387 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004388 (p4 - p1 >= t->fe->capture_namelen) &&
4389 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004390 int log_len = p4 - p1;
4391
Willy Tarreau086b3b42007-05-13 21:45:51 +02004392 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004393 Alert("HTTP logging : out of memory.\n");
4394 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004395 if (log_len > t->fe->capture_len)
4396 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004397 memcpy(txn->cli_cookie, p1, log_len);
4398 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004399 }
4400 }
4401
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004402 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4403 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004404 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004405 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004406 char *delim;
4407
4408 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4409 * have the server ID betweek p3 and delim, and the original cookie between
4410 * delim+1 and p4. Otherwise, delim==p4 :
4411 *
4412 * Cookie: NAME=SRV~VALUE;
4413 * | || || | |
4414 * | || || | +--> p4
4415 * | || || +--------> delim
4416 * | || |+-----------> p3
4417 * | || +------------> p2
4418 * | |+----------------> p1
4419 * | +-----------------> colon
4420 * +------------------------> cur_ptr
4421 */
4422
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004423 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004424 for (delim = p3; delim < p4; delim++)
4425 if (*delim == COOKIE_DELIM)
4426 break;
4427 }
4428 else
4429 delim = p4;
4430
4431
4432 /* Here, we'll look for the first running server which supports the cookie.
4433 * This allows to share a same cookie between several servers, for example
4434 * to dedicate backup servers to specific servers only.
4435 * However, to prevent clients from sticking to cookie-less backup server
4436 * when they have incidentely learned an empty cookie, we simply ignore
4437 * empty cookies and mark them as invalid.
4438 */
4439 if (delim == p3)
4440 srv = NULL;
4441
4442 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004443 if (srv->cookie && (srv->cklen == delim - p3) &&
4444 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004445 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004446 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004447 txn->flags &= ~TX_CK_MASK;
4448 txn->flags |= TX_CK_VALID;
4449 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004450 t->srv = srv;
4451 break;
4452 } else {
4453 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004454 txn->flags &= ~TX_CK_MASK;
4455 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004456 }
4457 }
4458 srv = srv->next;
4459 }
4460
Willy Tarreau3d300592007-03-18 18:34:41 +01004461 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004462 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004463 txn->flags &= ~TX_CK_MASK;
4464 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004465 }
4466
4467 /* depending on the cookie mode, we may have to either :
4468 * - delete the complete cookie if we're in insert+indirect mode, so that
4469 * the server never sees it ;
4470 * - remove the server id from the cookie value, and tag the cookie as an
4471 * application cookie so that it does not get accidentely removed later,
4472 * if we're in cookie prefix mode
4473 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004474 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004475 int delta; /* negative */
4476
4477 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4478 p4 += delta;
4479 cur_end += delta;
4480 cur_next += delta;
4481 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004482 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004483
4484 del_cookie = del_colon = NULL;
4485 app_cookies++; /* protect the header from deletion */
4486 }
4487 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004488 (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 +01004489 del_cookie = p1;
4490 del_colon = colon;
4491 }
4492 } else {
4493 /* now we know that we must keep this cookie since it's
4494 * not ours. But if we wanted to delete our cookie
4495 * earlier, we cannot remove the complete header, but we
4496 * can remove the previous block itself.
4497 */
4498 app_cookies++;
4499
4500 if (del_cookie != NULL) {
4501 int delta; /* negative */
4502
4503 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4504 p4 += delta;
4505 cur_end += delta;
4506 cur_next += delta;
4507 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004508 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004509 del_cookie = del_colon = NULL;
4510 }
4511 }
4512
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004513 if ((t->be->appsession_name != NULL) &&
4514 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004515 /* first, let's see if the cookie is our appcookie*/
4516
4517 /* Cool... it's the right one */
4518
4519 asession_temp = &local_asession;
4520
Willy Tarreau63963c62007-05-13 21:29:55 +02004521 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004522 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4523 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4524 return;
4525 }
4526
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004527 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4528 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004529 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004530
Willy Tarreau58f10d72006-12-04 02:26:12 +01004531 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004532 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4533 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004534 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004535 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004536 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004537 Alert("Not enough memory process_cli():asession:calloc().\n");
4538 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4539 return;
4540 }
4541
4542 asession_temp->sessid = local_asession.sessid;
4543 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004544 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004545 } else {
4546 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004547 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004548 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004549 if (asession_temp->serverid == NULL) {
4550 Alert("Found Application Session without matching server.\n");
4551 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004552 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004553 while (srv) {
4554 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004555 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004556 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004557 txn->flags &= ~TX_CK_MASK;
4558 txn->flags |= TX_CK_VALID;
4559 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004560 t->srv = srv;
4561 break;
4562 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004563 txn->flags &= ~TX_CK_MASK;
4564 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004565 }
4566 }
4567 srv = srv->next;
4568 }/* end while(srv) */
4569 }/* end else if server == NULL */
4570
Willy Tarreaud825eef2007-05-12 22:35:00 +02004571 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004572 }/* end if ((t->proxy->appsession_name != NULL) ... */
4573 }
4574
4575 /* we'll have to look for another cookie ... */
4576 p1 = p4;
4577 } /* while (p1 < cur_end) */
4578
4579 /* There's no more cookie on this line.
4580 * We may have marked the last one(s) for deletion.
4581 * We must do this now in two ways :
4582 * - if there is no app cookie, we simply delete the header ;
4583 * - if there are app cookies, we must delete the end of the
4584 * string properly, including the colon/semi-colon before
4585 * the cookie name.
4586 */
4587 if (del_cookie != NULL) {
4588 int delta;
4589 if (app_cookies) {
4590 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4591 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004592 cur_hdr->len += delta;
4593 } else {
4594 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004595
4596 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004597 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4598 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004599 cur_hdr->len = 0;
4600 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004601 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004602 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004603 }
4604
4605 /* keep the link from this header to next one */
4606 old_idx = cur_idx;
4607 } /* end of cookie processing on this header */
4608}
4609
4610
Willy Tarreaua15645d2007-03-18 16:22:39 +01004611/* Iterate the same filter through all response headers contained in <rtr>.
4612 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4613 */
4614int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4615{
4616 char term;
4617 char *cur_ptr, *cur_end, *cur_next;
4618 int cur_idx, old_idx, last_hdr;
4619 struct http_txn *txn = &t->txn;
4620 struct hdr_idx_elem *cur_hdr;
4621 int len, delta;
4622
4623 last_hdr = 0;
4624
4625 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4626 old_idx = 0;
4627
4628 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004629 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004630 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004631 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004632 (exp->action == ACT_ALLOW ||
4633 exp->action == ACT_DENY))
4634 return 0;
4635
4636 cur_idx = txn->hdr_idx.v[old_idx].next;
4637 if (!cur_idx)
4638 break;
4639
4640 cur_hdr = &txn->hdr_idx.v[cur_idx];
4641 cur_ptr = cur_next;
4642 cur_end = cur_ptr + cur_hdr->len;
4643 cur_next = cur_end + cur_hdr->cr + 1;
4644
4645 /* Now we have one header between cur_ptr and cur_end,
4646 * and the next header starts at cur_next.
4647 */
4648
4649 /* The annoying part is that pattern matching needs
4650 * that we modify the contents to null-terminate all
4651 * strings before testing them.
4652 */
4653
4654 term = *cur_end;
4655 *cur_end = '\0';
4656
4657 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4658 switch (exp->action) {
4659 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004660 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004661 last_hdr = 1;
4662 break;
4663
4664 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004665 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004666 last_hdr = 1;
4667 break;
4668
4669 case ACT_REPLACE:
4670 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4671 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4672 /* FIXME: if the user adds a newline in the replacement, the
4673 * index will not be recalculated for now, and the new line
4674 * will not be counted as a new header.
4675 */
4676
4677 cur_end += delta;
4678 cur_next += delta;
4679 cur_hdr->len += delta;
4680 txn->rsp.eoh += delta;
4681 break;
4682
4683 case ACT_REMOVE:
4684 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4685 cur_next += delta;
4686
4687 /* FIXME: this should be a separate function */
4688 txn->rsp.eoh += delta;
4689 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4690 txn->hdr_idx.used--;
4691 cur_hdr->len = 0;
4692 cur_end = NULL; /* null-term has been rewritten */
4693 break;
4694
4695 }
4696 }
4697 if (cur_end)
4698 *cur_end = term; /* restore the string terminator */
4699
4700 /* keep the link from this header to next one in case of later
4701 * removal of next header.
4702 */
4703 old_idx = cur_idx;
4704 }
4705 return 0;
4706}
4707
4708
4709/* Apply the filter to the status line in the response buffer <rtr>.
4710 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4711 * or -1 if a replacement resulted in an invalid status line.
4712 */
4713int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4714{
4715 char term;
4716 char *cur_ptr, *cur_end;
4717 int done;
4718 struct http_txn *txn = &t->txn;
4719 int len, delta;
4720
4721
Willy Tarreau3d300592007-03-18 18:34:41 +01004722 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004723 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004724 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004725 (exp->action == ACT_ALLOW ||
4726 exp->action == ACT_DENY))
4727 return 0;
4728 else if (exp->action == ACT_REMOVE)
4729 return 0;
4730
4731 done = 0;
4732
Willy Tarreau9cdde232007-05-02 20:58:19 +02004733 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004734 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4735
4736 /* Now we have the status line between cur_ptr and cur_end */
4737
4738 /* The annoying part is that pattern matching needs
4739 * that we modify the contents to null-terminate all
4740 * strings before testing them.
4741 */
4742
4743 term = *cur_end;
4744 *cur_end = '\0';
4745
4746 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4747 switch (exp->action) {
4748 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004749 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004750 done = 1;
4751 break;
4752
4753 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004754 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004755 done = 1;
4756 break;
4757
4758 case ACT_REPLACE:
4759 *cur_end = term; /* restore the string terminator */
4760 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4761 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4762 /* FIXME: if the user adds a newline in the replacement, the
4763 * index will not be recalculated for now, and the new line
4764 * will not be counted as a new header.
4765 */
4766
4767 txn->rsp.eoh += delta;
4768 cur_end += delta;
4769
Willy Tarreau9cdde232007-05-02 20:58:19 +02004770 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004771 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004772 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004773 cur_ptr, cur_end + 1,
4774 NULL, NULL);
4775 if (unlikely(!cur_end))
4776 return -1;
4777
4778 /* we have a full respnse and we know that we have either a CR
4779 * or an LF at <ptr>.
4780 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004781 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004782 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4783 /* there is no point trying this regex on headers */
4784 return 1;
4785 }
4786 }
4787 *cur_end = term; /* restore the string terminator */
4788 return done;
4789}
4790
4791
4792
4793/*
4794 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4795 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4796 * unparsable response.
4797 */
4798int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4799{
Willy Tarreau3d300592007-03-18 18:34:41 +01004800 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004801 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004802 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004803 int ret;
4804
4805 /*
4806 * The interleaving of transformations and verdicts
4807 * makes it difficult to decide to continue or stop
4808 * the evaluation.
4809 */
4810
Willy Tarreau3d300592007-03-18 18:34:41 +01004811 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004812 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4813 exp->action == ACT_PASS)) {
4814 exp = exp->next;
4815 continue;
4816 }
4817
4818 /* Apply the filter to the status line. */
4819 ret = apply_filter_to_sts_line(t, rtr, exp);
4820 if (unlikely(ret < 0))
4821 return -1;
4822
4823 if (likely(ret == 0)) {
4824 /* The filter did not match the response, it can be
4825 * iterated through all headers.
4826 */
4827 apply_filter_to_resp_headers(t, rtr, exp);
4828 }
4829 exp = exp->next;
4830 }
4831 return 0;
4832}
4833
4834
4835
4836/*
4837 * Manager server-side cookies
4838 */
4839void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4840{
4841 struct http_txn *txn = &t->txn;
4842 char *p1, *p2, *p3, *p4;
4843
4844 appsess *asession_temp = NULL;
4845 appsess local_asession;
4846
4847 char *cur_ptr, *cur_end, *cur_next;
4848 int cur_idx, old_idx, delta;
4849
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004850 if (t->be->cookie_name == NULL &&
4851 t->be->appsession_name == NULL &&
4852 t->be->capture_name == NULL &&
4853 !(t->be->options & PR_O_CHK_CACHE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004854 return;
4855
4856 /* Iterate through the headers.
4857 * we start with the start line.
4858 */
4859 old_idx = 0;
4860 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4861
4862 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4863 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004864 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004865
4866 cur_hdr = &txn->hdr_idx.v[cur_idx];
4867 cur_ptr = cur_next;
4868 cur_end = cur_ptr + cur_hdr->len;
4869 cur_next = cur_end + cur_hdr->cr + 1;
4870
4871 /* We have one full header between cur_ptr and cur_end, and the
4872 * next header starts at cur_next. We're only interested in
4873 * "Cookie:" headers.
4874 */
4875
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004876 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4877 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004878 old_idx = cur_idx;
4879 continue;
4880 }
4881
4882 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004883 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004884
4885
4886 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004887 if (t->be->cookie_name == NULL &&
4888 t->be->appsession_name == NULL &&
4889 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004890 return;
4891
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004892 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004893
4894 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004895 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4896 break;
4897
4898 /* p1 is at the beginning of the cookie name */
4899 p2 = p1;
4900
4901 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4902 p2++;
4903
4904 if (p2 == cur_end || *p2 == ';') /* next cookie */
4905 break;
4906
4907 p3 = p2 + 1; /* skip the '=' sign */
4908 if (p3 == cur_end)
4909 break;
4910
4911 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004912 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004913 p4++;
4914
4915 /* here, we have the cookie name between p1 and p2,
4916 * and its value between p3 and p4.
4917 * we can process it.
4918 */
4919
4920 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004921 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004922 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004923 (p4 - p1 >= t->be->capture_namelen) &&
4924 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004925 int log_len = p4 - p1;
4926
Willy Tarreau086b3b42007-05-13 21:45:51 +02004927 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004928 Alert("HTTP logging : out of memory.\n");
4929 }
4930
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004931 if (log_len > t->be->capture_len)
4932 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004933 memcpy(txn->srv_cookie, p1, log_len);
4934 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004935 }
4936
4937 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004938 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4939 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004940 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004941 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004942
4943 /* If the cookie is in insert mode on a known server, we'll delete
4944 * this occurrence because we'll insert another one later.
4945 * We'll delete it too if the "indirect" option is set and we're in
4946 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004947 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4948 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004949 /* this header must be deleted */
4950 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4951 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4952 txn->hdr_idx.used--;
4953 cur_hdr->len = 0;
4954 cur_next += delta;
4955 txn->rsp.eoh += delta;
4956
Willy Tarreau3d300592007-03-18 18:34:41 +01004957 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004958 }
4959 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004960 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004961 /* replace bytes p3->p4 with the cookie name associated
4962 * with this server since we know it.
4963 */
4964 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4965 cur_hdr->len += delta;
4966 cur_next += delta;
4967 txn->rsp.eoh += delta;
4968
Willy Tarreau3d300592007-03-18 18:34:41 +01004969 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004970 }
4971 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004972 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004973 /* insert the cookie name associated with this server
4974 * before existing cookie, and insert a delimitor between them..
4975 */
4976 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4977 cur_hdr->len += delta;
4978 cur_next += delta;
4979 txn->rsp.eoh += delta;
4980
4981 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004982 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004983 }
4984 }
4985 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004986 else if ((t->be->appsession_name != NULL) &&
4987 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004988
4989 /* Cool... it's the right one */
4990
4991 size_t server_id_len = strlen(t->srv->id) + 1;
4992 asession_temp = &local_asession;
4993
Willy Tarreau63963c62007-05-13 21:29:55 +02004994 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004995 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4996 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4997 return;
4998 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004999 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
5000 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005001 asession_temp->serverid = NULL;
5002
5003 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02005004 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005005 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005006 Alert("Not enough Memory process_srv():asession:calloc().\n");
5007 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5008 return;
5009 }
5010 asession_temp->sessid = local_asession.sessid;
5011 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02005012 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
5013 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005014 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005015 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02005016 }
5017
Willy Tarreaua15645d2007-03-18 16:22:39 +01005018 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005019 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005020 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5021 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5022 return;
5023 }
5024 asession_temp->serverid[0] = '\0';
5025 }
5026
5027 if (asession_temp->serverid[0] == '\0')
5028 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5029
Willy Tarreaud825eef2007-05-12 22:35:00 +02005030 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005031
5032#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02005033 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005034#endif
5035 }/* end if ((t->proxy->appsession_name != NULL) ... */
5036 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5037 } /* we're now at the end of the cookie value */
5038
5039 /* keep the link from this header to next one */
5040 old_idx = cur_idx;
5041 } /* end of cookie processing on this header */
5042}
5043
5044
5045
5046/*
5047 * Check if response is cacheable or not. Updates t->flags.
5048 */
5049void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5050{
5051 struct http_txn *txn = &t->txn;
5052 char *p1, *p2;
5053
5054 char *cur_ptr, *cur_end, *cur_next;
5055 int cur_idx;
5056
Willy Tarreau3d300592007-03-18 18:34:41 +01005057 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005058 return;
5059
5060 /* Iterate through the headers.
5061 * we start with the start line.
5062 */
5063 cur_idx = 0;
5064 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5065
5066 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5067 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005068 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005069
5070 cur_hdr = &txn->hdr_idx.v[cur_idx];
5071 cur_ptr = cur_next;
5072 cur_end = cur_ptr + cur_hdr->len;
5073 cur_next = cur_end + cur_hdr->cr + 1;
5074
5075 /* We have one full header between cur_ptr and cur_end, and the
5076 * next header starts at cur_next. We're only interested in
5077 * "Cookie:" headers.
5078 */
5079
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005080 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5081 if (val) {
5082 if ((cur_end - (cur_ptr + val) >= 8) &&
5083 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5084 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5085 return;
5086 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005087 }
5088
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005089 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5090 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005091 continue;
5092
5093 /* OK, right now we know we have a cache-control header at cur_ptr */
5094
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005095 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005096
5097 if (p1 >= cur_end) /* no more info */
5098 continue;
5099
5100 /* p1 is at the beginning of the value */
5101 p2 = p1;
5102
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005103 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005104 p2++;
5105
5106 /* we have a complete value between p1 and p2 */
5107 if (p2 < cur_end && *p2 == '=') {
5108 /* we have something of the form no-cache="set-cookie" */
5109 if ((cur_end - p1 >= 21) &&
5110 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5111 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005112 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005113 continue;
5114 }
5115
5116 /* OK, so we know that either p2 points to the end of string or to a comma */
5117 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5118 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5119 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5120 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005121 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005122 return;
5123 }
5124
5125 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005126 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005127 continue;
5128 }
5129 }
5130}
5131
5132
Willy Tarreau58f10d72006-12-04 02:26:12 +01005133/*
5134 * Try to retrieve a known appsession in the URI, then the associated server.
5135 * If the server is found, it's assigned to the session.
5136 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005137void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005138{
Willy Tarreau3d300592007-03-18 18:34:41 +01005139 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005140 appsess *asession_temp = NULL;
5141 appsess local_asession;
5142 char *request_line;
5143
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005144 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005145 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005146 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005147 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005148 return;
5149
5150 /* skip ';' */
5151 request_line++;
5152
5153 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005154 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005155 return;
5156
5157 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005158 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005159
5160 /* First try if we already have an appsession */
5161 asession_temp = &local_asession;
5162
Willy Tarreau63963c62007-05-13 21:29:55 +02005163 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005164 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5165 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5166 return;
5167 }
5168
5169 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005170 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5171 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005172 asession_temp->serverid = NULL;
5173
5174 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02005175 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005176 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005177 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005178 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005179 Alert("Not enough memory process_cli():asession:calloc().\n");
5180 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5181 return;
5182 }
5183 asession_temp->sessid = local_asession.sessid;
5184 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02005185 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005186 }
5187 else {
5188 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005189 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005190 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005191
Willy Tarreaud825eef2007-05-12 22:35:00 +02005192 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005193 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005194
Willy Tarreau58f10d72006-12-04 02:26:12 +01005195#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02005196 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005197#endif
5198 if (asession_temp->serverid == NULL) {
5199 Alert("Found Application Session without matching server.\n");
5200 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005201 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005202 while (srv) {
5203 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005204 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005205 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005206 txn->flags &= ~TX_CK_MASK;
5207 txn->flags |= TX_CK_VALID;
5208 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005209 t->srv = srv;
5210 break;
5211 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005212 txn->flags &= ~TX_CK_MASK;
5213 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005214 }
5215 }
5216 srv = srv->next;
5217 }
5218 }
5219}
5220
5221
Willy Tarreaub2513902006-12-17 14:52:38 +01005222/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005223 * In a GET or HEAD request, check if the requested URI matches the stats uri
5224 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005225 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005226 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005227 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005228 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005229 *
5230 * Returns 1 if the session's state changes, otherwise 0.
5231 */
5232int stats_check_uri_auth(struct session *t, struct proxy *backend)
5233{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005234 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005235 struct uri_auth *uri_auth = backend->uri_auth;
5236 struct user_auth *user;
5237 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005238 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005240 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005241 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005242 return 0;
5243
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005244 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005245
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005246 /* the URI is in h */
5247 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005248 return 0;
5249
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005250 h += uri_auth->uri_len;
5251 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5252 if (memcmp(h, ";up", 3) == 0) {
5253 t->flags |= SN_STAT_HIDEDWN;
5254 break;
5255 }
5256 h++;
5257 }
5258
5259 if (uri_auth->refresh) {
5260 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5261 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5262 if (memcmp(h, ";norefresh", 10) == 0) {
5263 t->flags |= SN_STAT_NORFRSH;
5264 break;
5265 }
5266 h++;
5267 }
5268 }
5269
Willy Tarreaub2513902006-12-17 14:52:38 +01005270 /* we are in front of a interceptable URI. Let's check
5271 * if there's an authentication and if it's valid.
5272 */
5273 user = uri_auth->users;
5274 if (!user) {
5275 /* no user auth required, it's OK */
5276 authenticated = 1;
5277 } else {
5278 authenticated = 0;
5279
5280 /* a user list is defined, we have to check.
5281 * skip 21 chars for "Authorization: Basic ".
5282 */
5283
5284 /* FIXME: this should move to an earlier place */
5285 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005286 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5287 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5288 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005289 if (len > 14 &&
5290 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005291 txn->auth_hdr.str = h;
5292 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005293 break;
5294 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005295 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005296 }
5297
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005298 if (txn->auth_hdr.len < 21 ||
5299 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005300 user = NULL;
5301
5302 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005303 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5304 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005305 user->user_pwd, user->user_len)) {
5306 authenticated = 1;
5307 break;
5308 }
5309 user = user->next;
5310 }
5311 }
5312
5313 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005314 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005315
5316 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005317 msg.str = trash;
5318 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005319 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005320 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01005321 if (!(t->flags & SN_ERR_MASK))
5322 t->flags |= SN_ERR_PRXCOND;
5323 if (!(t->flags & SN_FINST_MASK))
5324 t->flags |= SN_FINST_R;
5325 return 1;
5326 }
5327
5328 /* The request is valid, the user is authenticate. Let's start sending
5329 * data.
5330 */
5331 t->cli_state = CL_STSHUTR;
5332 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02005333 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01005334 t->data_source = DATA_SRC_STATS;
5335 t->data_state = DATA_ST_INIT;
5336 produce_content(t);
5337 return 1;
5338}
5339
5340
Willy Tarreaubaaee002006-06-26 02:48:02 +02005341/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005342 * Print a debug line with a header
5343 */
5344void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5345{
5346 int len, max;
5347 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5348 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5349 max = end - start;
5350 UBOUND(max, sizeof(trash) - len - 1);
5351 len += strlcpy2(trash + len, start, max + 1);
5352 trash[len++] = '\n';
5353 write(1, trash, len);
5354}
5355
5356
Willy Tarreau8797c062007-05-07 00:55:35 +02005357/************************************************************************/
5358/* The code below is dedicated to ACL parsing and matching */
5359/************************************************************************/
5360
5361
5362
5363
5364/* 1. Check on METHOD
5365 * We use the pre-parsed method if it is known, and store its number as an
5366 * integer. If it is unknown, we use the pointer and the length.
5367 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005368static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005369{
5370 int len, meth;
5371
Willy Tarreauae8b7962007-06-09 23:10:04 +02005372 len = strlen(*text);
5373 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005374
5375 pattern->val.i = meth;
5376 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005377 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005378 if (!pattern->ptr.str)
5379 return 0;
5380 pattern->len = len;
5381 }
5382 return 1;
5383}
5384
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005385static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005386acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5387 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005388{
5389 int meth;
5390 struct http_txn *txn = l7;
5391
Willy Tarreauc11416f2007-06-17 16:58:38 +02005392 if (txn->req.msg_state != HTTP_MSG_BODY)
5393 return 0;
5394
Willy Tarreau8797c062007-05-07 00:55:35 +02005395 meth = txn->meth;
5396 test->i = meth;
5397 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005398 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5399 /* ensure the indexes are not affected */
5400 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005401 test->len = txn->req.sl.rq.m_l;
5402 test->ptr = txn->req.sol;
5403 }
5404 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5405 return 1;
5406}
5407
5408static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5409{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005410 int icase;
5411
Willy Tarreau8797c062007-05-07 00:55:35 +02005412 if (test->i != pattern->val.i)
5413 return 0;
5414
5415 if (test->i != HTTP_METH_OTHER)
5416 return 1;
5417
5418 /* Other method, we must compare the strings */
5419 if (pattern->len != test->len)
5420 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005421
5422 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5423 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5424 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau8797c062007-05-07 00:55:35 +02005425 return 0;
5426 return 1;
5427}
5428
5429/* 2. Check on Request/Status Version
5430 * We simply compare strings here.
5431 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005432static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005433{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005434 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005435 if (!pattern->ptr.str)
5436 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005437 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005438 return 1;
5439}
5440
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005441static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005442acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5443 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005444{
5445 struct http_txn *txn = l7;
5446 char *ptr;
5447 int len;
5448
Willy Tarreauc11416f2007-06-17 16:58:38 +02005449 if (txn->req.msg_state != HTTP_MSG_BODY)
5450 return 0;
5451
Willy Tarreau8797c062007-05-07 00:55:35 +02005452 len = txn->req.sl.rq.v_l;
5453 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5454
5455 while ((len-- > 0) && (*ptr++ != '/'));
5456 if (len <= 0)
5457 return 0;
5458
5459 test->ptr = ptr;
5460 test->len = len;
5461
5462 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5463 return 1;
5464}
5465
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005466static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005467acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5468 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005469{
5470 struct http_txn *txn = l7;
5471 char *ptr;
5472 int len;
5473
Willy Tarreauc11416f2007-06-17 16:58:38 +02005474 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5475 return 0;
5476
Willy Tarreau8797c062007-05-07 00:55:35 +02005477 len = txn->rsp.sl.st.v_l;
5478 ptr = txn->rsp.sol;
5479
5480 while ((len-- > 0) && (*ptr++ != '/'));
5481 if (len <= 0)
5482 return 0;
5483
5484 test->ptr = ptr;
5485 test->len = len;
5486
5487 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5488 return 1;
5489}
5490
5491/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005492static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005493acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5494 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005495{
5496 struct http_txn *txn = l7;
5497 char *ptr;
5498 int len;
5499
Willy Tarreauc11416f2007-06-17 16:58:38 +02005500 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5501 return 0;
5502
Willy Tarreau8797c062007-05-07 00:55:35 +02005503 len = txn->rsp.sl.st.c_l;
5504 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5505
5506 test->i = __strl2ui(ptr, len);
5507 test->flags = ACL_TEST_F_VOL_1ST;
5508 return 1;
5509}
5510
5511/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005512static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005513acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5514 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005515{
5516 struct http_txn *txn = l7;
5517
Willy Tarreauc11416f2007-06-17 16:58:38 +02005518 if (txn->req.msg_state != HTTP_MSG_BODY)
5519 return 0;
5520 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5521 /* ensure the indexes are not affected */
5522 return 0;
5523
Willy Tarreau8797c062007-05-07 00:55:35 +02005524 test->len = txn->req.sl.rq.u_l;
5525 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5526
Willy Tarreauf3d25982007-05-08 22:45:09 +02005527 /* we do not need to set READ_ONLY because the data is in a buffer */
5528 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005529 return 1;
5530}
5531
Willy Tarreauc11416f2007-06-17 16:58:38 +02005532/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5533 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5534 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005535static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005536acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005537 struct acl_expr *expr, struct acl_test *test)
5538{
5539 struct http_txn *txn = l7;
5540 struct hdr_idx *idx = &txn->hdr_idx;
5541 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005542
5543 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5544 /* search for header from the beginning */
5545 ctx->idx = 0;
5546
Willy Tarreau33a7e692007-06-10 19:45:56 +02005547 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5548 test->flags |= ACL_TEST_F_FETCH_MORE;
5549 test->flags |= ACL_TEST_F_VOL_HDR;
5550 test->len = ctx->vlen;
5551 test->ptr = (char *)ctx->line + ctx->val;
5552 return 1;
5553 }
5554
5555 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5556 test->flags |= ACL_TEST_F_VOL_HDR;
5557 return 0;
5558}
5559
Willy Tarreau33a7e692007-06-10 19:45:56 +02005560static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005561acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5562 struct acl_expr *expr, struct acl_test *test)
5563{
5564 struct http_txn *txn = l7;
5565
5566 if (txn->req.msg_state != HTTP_MSG_BODY)
5567 return 0;
5568 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5569 /* ensure the indexes are not affected */
5570 return 0;
5571
5572 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5573}
5574
5575static int
5576acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5577 struct acl_expr *expr, struct acl_test *test)
5578{
5579 struct http_txn *txn = l7;
5580
5581 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5582 return 0;
5583
5584 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5585}
5586
5587/* 6. Check on HTTP header count. The number of occurrences is returned.
5588 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5589 */
5590static int
5591acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005592 struct acl_expr *expr, struct acl_test *test)
5593{
5594 struct http_txn *txn = l7;
5595 struct hdr_idx *idx = &txn->hdr_idx;
5596 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005597 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005598
Willy Tarreau33a7e692007-06-10 19:45:56 +02005599 ctx.idx = 0;
5600 cnt = 0;
5601 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5602 cnt++;
5603
5604 test->i = cnt;
5605 test->flags = ACL_TEST_F_VOL_HDR;
5606 return 1;
5607}
5608
Willy Tarreauc11416f2007-06-17 16:58:38 +02005609static int
5610acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5611 struct acl_expr *expr, struct acl_test *test)
5612{
5613 struct http_txn *txn = l7;
5614
5615 if (txn->req.msg_state != HTTP_MSG_BODY)
5616 return 0;
5617 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5618 /* ensure the indexes are not affected */
5619 return 0;
5620
5621 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5622}
5623
5624static int
5625acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5626 struct acl_expr *expr, struct acl_test *test)
5627{
5628 struct http_txn *txn = l7;
5629
5630 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5631 return 0;
5632
5633 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5634}
5635
Willy Tarreau33a7e692007-06-10 19:45:56 +02005636/* 7. Check on HTTP header's integer value. The integer value is returned.
5637 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005638 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005639 */
5640static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005641acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005642 struct acl_expr *expr, struct acl_test *test)
5643{
5644 struct http_txn *txn = l7;
5645 struct hdr_idx *idx = &txn->hdr_idx;
5646 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005647
5648 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5649 /* search for header from the beginning */
5650 ctx->idx = 0;
5651
Willy Tarreau33a7e692007-06-10 19:45:56 +02005652 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5653 test->flags |= ACL_TEST_F_FETCH_MORE;
5654 test->flags |= ACL_TEST_F_VOL_HDR;
5655 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5656 return 1;
5657 }
5658
5659 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5660 test->flags |= ACL_TEST_F_VOL_HDR;
5661 return 0;
5662}
5663
Willy Tarreauc11416f2007-06-17 16:58:38 +02005664static int
5665acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5666 struct acl_expr *expr, struct acl_test *test)
5667{
5668 struct http_txn *txn = l7;
5669
5670 if (txn->req.msg_state != HTTP_MSG_BODY)
5671 return 0;
5672 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5673 /* ensure the indexes are not affected */
5674 return 0;
5675
5676 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5677}
5678
5679static int
5680acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5681 struct acl_expr *expr, struct acl_test *test)
5682{
5683 struct http_txn *txn = l7;
5684
5685 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5686 return 0;
5687
5688 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5689}
5690
Willy Tarreau737b0c12007-06-10 21:28:46 +02005691/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5692 * the first '/' after the possible hostname, and ends before the possible '?'.
5693 */
5694static int
5695acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5696 struct acl_expr *expr, struct acl_test *test)
5697{
5698 struct http_txn *txn = l7;
5699 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005700
Willy Tarreauc11416f2007-06-17 16:58:38 +02005701 if (txn->req.msg_state != HTTP_MSG_BODY)
5702 return 0;
5703 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5704 /* ensure the indexes are not affected */
5705 return 0;
5706
Willy Tarreau737b0c12007-06-10 21:28:46 +02005707 ptr = txn->req.sol + txn->req.sl.rq.u;
5708 end = ptr + txn->req.sl.rq.u_l;
5709
5710 if (ptr >= end)
5711 return 0;
5712
5713 /* RFC2616, par. 5.1.2 :
5714 * Request-URI = "*" | absuri | abspath | authority
5715 */
5716
5717 if (*ptr == '*')
5718 return 0;
5719
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005720 if (isalpha((unsigned char)*ptr)) {
Willy Tarreau737b0c12007-06-10 21:28:46 +02005721 /* this is a scheme as described by RFC3986, par. 3.1 */
5722 ptr++;
5723 while (ptr < end &&
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005724 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
Willy Tarreau737b0c12007-06-10 21:28:46 +02005725 ptr++;
5726 /* skip '://' */
5727 if (ptr == end || *ptr++ != ':')
5728 return 0;
5729 if (ptr == end || *ptr++ != '/')
5730 return 0;
5731 if (ptr == end || *ptr++ != '/')
5732 return 0;
5733 }
5734 /* skip [user[:passwd]@]host[:[port]] */
5735
5736 while (ptr < end && *ptr != '/')
5737 ptr++;
5738
5739 if (ptr == end)
5740 return 0;
5741
5742 /* OK, we got the '/' ! */
5743 test->ptr = ptr;
5744
5745 while (ptr < end && *ptr != '?')
5746 ptr++;
5747
5748 test->len = ptr - test->ptr;
5749
5750 /* we do not need to set READ_ONLY because the data is in a buffer */
5751 test->flags = ACL_TEST_F_VOL_1ST;
5752 return 1;
5753}
5754
5755
Willy Tarreau8797c062007-05-07 00:55:35 +02005756
5757/************************************************************************/
5758/* All supported keywords must be declared here. */
5759/************************************************************************/
5760
5761/* Note: must not be declared <const> as its list will be overwritten */
5762static struct acl_kw_list acl_kws = {{ },{
5763 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5764 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5765 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
Willy Tarreauae8b7962007-06-09 23:10:04 +02005766 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005767
Willy Tarreauae8b7962007-06-09 23:10:04 +02005768 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5769 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5770 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5771 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5772 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5773 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
5774 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
Willy Tarreau8797c062007-05-07 00:55:35 +02005775
Willy Tarreauc11416f2007-06-17 16:58:38 +02005776 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str },
5777 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg },
5778 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg },
5779 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end },
5780 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub },
5781 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir },
5782 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom },
5783 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int },
5784 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int },
5785
5786 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str },
5787 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg },
5788 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg },
5789 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end },
5790 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub },
5791 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir },
5792 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom },
5793 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int },
5794 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005795
5796 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5797 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5798 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5799 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5800 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5801 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5802 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5803
Willy Tarreauf3d25982007-05-08 22:45:09 +02005804 { NULL, NULL, NULL, NULL },
5805
5806#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005807 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5808 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5809 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5810 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5811 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5812 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5813 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5814
Willy Tarreau8797c062007-05-07 00:55:35 +02005815 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5816 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5817 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5818 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5819 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5820 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5821 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5822 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5823
5824 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5825 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5826 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5827 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5828 { NULL, NULL, NULL, NULL },
5829#endif
5830}};
5831
5832
5833__attribute__((constructor))
5834static void __http_protocol_init(void)
5835{
5836 acl_register_keywords(&acl_kws);
5837}
5838
5839
Willy Tarreau58f10d72006-12-04 02:26:12 +01005840/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005841 * Local variables:
5842 * c-indent-level: 8
5843 * c-basic-offset: 8
5844 * End:
5845 */