blob: 8655372d725a25ec4a46609db8f5b112a1f9a819 [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
436/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437 * returns a message to the client ; the connection is shut down for read,
438 * and the request is cleared so that no server connection can be initiated.
439 * The client must be in a valid state for this (HEADER, DATA ...).
Willy Tarreau0f772532006-12-23 20:51:41 +0100440 * Nothing is performed on the server side. The message is contained in a
441 * "chunk". If it is null, then an empty message is used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442 * The reply buffer doesn't need to be empty before this.
443 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100444void client_retnclose(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445{
Willy Tarreauf161a342007-04-08 16:59:42 +0200446 EV_FD_CLR(s->cli_fd, DIR_RD);
447 EV_FD_SET(s->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +0200448 tv_eternity(&s->req->rex);
Willy Tarreaua8b55e32007-05-13 16:08:19 +0200449 if (!tv_add_ifset(&s->rep->wex, &now, &s->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +0200450 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451 s->cli_state = CL_STSHUTR;
452 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100453 if (msg->len)
454 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 s->req->l = 0;
456}
457
458
459/*
460 * returns a message into the rep buffer, and flushes the req buffer.
Willy Tarreau0f772532006-12-23 20:51:41 +0100461 * The reply buffer doesn't need to be empty before this. The message
462 * is contained in a "chunk". If it is null, then an empty message is
463 * used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100465void client_return(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466{
467 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100468 if (msg->len)
469 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470 s->req->l = 0;
471}
472
473
474/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100475 * indicators accordingly. Note that if <status> is 0, or if the message
476 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477 */
478void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100479 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480{
481 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100482 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100483 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100484 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100485 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200486 }
487 if (!(t->flags & SN_ERR_MASK))
488 t->flags |= err;
489 if (!(t->flags & SN_FINST_MASK))
490 t->flags |= finst;
491}
492
Willy Tarreau80587432006-12-24 17:47:20 +0100493/* This function returns the appropriate error location for the given session
494 * and message.
495 */
496
497struct chunk *error_message(struct session *s, int msgnum)
498{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200499 if (s->be->errmsg[msgnum].str)
500 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100501 else if (s->fe->errmsg[msgnum].str)
502 return &s->fe->errmsg[msgnum];
503 else
504 return &http_err_chunks[msgnum];
505}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200506
Willy Tarreau53b6c742006-12-17 13:37:46 +0100507/*
508 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
509 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
510 */
511static http_meth_t find_http_meth(const char *str, const int len)
512{
513 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100514 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100515
516 m = ((unsigned)*str - 'A');
517
518 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100519 for (h = http_methods[m]; h->len > 0; h++) {
520 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100521 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100522 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100523 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100524 };
525 return HTTP_METH_OTHER;
526 }
527 return HTTP_METH_NONE;
528
529}
530
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531/* Processes the client and server jobs of a session task, then
532 * puts it back to the wait queue in a clean state, or
533 * cleans up its resources if it must be deleted. Returns
534 * the time the task accepts to wait, or TIME_ETERNITY for
535 * infinity.
536 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200537void process_session(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538{
539 struct session *s = t->context;
540 int fsm_resync = 0;
541
542 do {
543 fsm_resync = 0;
544 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
545 fsm_resync |= process_cli(s);
546 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
547 fsm_resync |= process_srv(s);
548 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
549 } while (fsm_resync);
550
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200551 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200552 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
553 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200555 t->expire = s->req->rex;
556 tv_min(&t->expire, &s->req->rex, &s->req->wex);
557 tv_bound(&t->expire, &s->req->cex);
558 tv_bound(&t->expire, &s->rep->rex);
559 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200560
561 /* restore t to its place in the task list */
562 task_queue(t);
563
564#ifdef DEBUG_FULL
565 /* DEBUG code : this should never ever happen, otherwise it indicates
566 * that a task still has something to do and will provoke a quick loop.
567 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200568 if (tv_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569 exit(100);
570#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200571 *next = t->expire;
572 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 }
574
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100575 s->fe->feconn--;
576 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200577 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578 actconn--;
579
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200580 if (unlikely((global.mode & MODE_DEBUG) &&
581 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200582 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100583 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200584 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100585 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200586 write(1, trash, len);
587 }
588
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200589 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100590 if (s->req != NULL)
591 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200592 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100593 s->logs.bytes_out = s->rep->total;
594
595 s->fe->bytes_in += s->logs.bytes_in;
596 s->fe->bytes_out += s->logs.bytes_out;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200597 if (s->be != s->fe) {
598 s->be->bytes_in += s->logs.bytes_in;
599 s->be->bytes_out += s->logs.bytes_out;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100600 }
601 if (s->srv) {
602 s->srv->bytes_in += s->logs.bytes_in;
603 s->srv->bytes_out += s->logs.bytes_out;
604 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200605
606 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200607 if (s->logs.logwait &&
608 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200609 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
610 if (s->fe->to_log & LW_REQ)
611 http_sess_log(s);
612 else
613 tcp_sess_log(s);
614 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200615
616 /* the task MUST not be in the run queue anymore */
617 task_delete(t);
618 session_free(s);
619 task_free(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200620 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621}
622
623
Willy Tarreau42250582007-04-01 01:30:43 +0200624extern const char sess_term_cond[8];
625extern const char sess_fin_state[8];
626extern const char *monthname[12];
627const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
628const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
629 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
630 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200631struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200632struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100633
Willy Tarreau42250582007-04-01 01:30:43 +0200634/*
635 * send a log for the session when we have enough info about it.
636 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100637 */
Willy Tarreau42250582007-04-01 01:30:43 +0200638static void http_sess_log(struct session *s)
639{
640 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
641 struct proxy *fe = s->fe;
642 struct proxy *be = s->be;
643 struct proxy *prx_log;
644 struct http_txn *txn = &s->txn;
645 int tolog;
646 char *uri, *h;
647 char *svid;
648 struct tm *tm;
649 static char tmpline[MAX_SYSLOG_LEN];
650 int hdr;
651
652 if (fe->logfac1 < 0 && fe->logfac2 < 0)
653 return;
654 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100655
Willy Tarreau42250582007-04-01 01:30:43 +0200656 if (s->cli_addr.ss_family == AF_INET)
657 inet_ntop(AF_INET,
658 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
659 pn, sizeof(pn));
660 else
661 inet_ntop(AF_INET6,
662 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
663 pn, sizeof(pn));
664
665 tm = localtime((time_t *)&s->logs.tv_accept.tv_sec);
666
667
668 /* FIXME: let's limit ourselves to frontend logging for now. */
669 tolog = fe->to_log;
670
671 h = tmpline;
672 if (fe->to_log & LW_REQHDR &&
673 txn->req.cap &&
674 (h < tmpline + sizeof(tmpline) - 10)) {
675 *(h++) = ' ';
676 *(h++) = '{';
677 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
678 if (hdr)
679 *(h++) = '|';
680 if (txn->req.cap[hdr] != NULL)
681 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
682 '#', hdr_encode_map, txn->req.cap[hdr]);
683 }
684 *(h++) = '}';
685 }
686
687 if (fe->to_log & LW_RSPHDR &&
688 txn->rsp.cap &&
689 (h < tmpline + sizeof(tmpline) - 7)) {
690 *(h++) = ' ';
691 *(h++) = '{';
692 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
693 if (hdr)
694 *(h++) = '|';
695 if (txn->rsp.cap[hdr] != NULL)
696 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
697 '#', hdr_encode_map, txn->rsp.cap[hdr]);
698 }
699 *(h++) = '}';
700 }
701
702 if (h < tmpline + sizeof(tmpline) - 4) {
703 *(h++) = ' ';
704 *(h++) = '"';
705 uri = txn->uri ? txn->uri : "<BADREQ>";
706 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
707 '#', url_encode_map, uri);
708 *(h++) = '"';
709 }
710 *h = '\0';
711
712 svid = (tolog & LW_SVID) ?
713 (s->data_source != DATA_SRC_STATS) ?
714 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
715
716 send_log(prx_log, LOG_INFO,
717 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
718 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
719 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
720 pn,
721 (s->cli_addr.ss_family == AF_INET) ?
722 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
723 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
724 tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900,
725 tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000,
726 fe->id, be->id, svid,
727 s->logs.t_request,
728 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
729 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
730 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
731 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
732 txn->status,
733 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
734 txn->cli_cookie ? txn->cli_cookie : "-",
735 txn->srv_cookie ? txn->srv_cookie : "-",
736 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
737 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
738 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
739 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
740 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
741 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
742
743 s->logs.logwait = 0;
744}
745
Willy Tarreau117f59e2007-03-04 18:17:17 +0100746
747/*
748 * Capture headers from message starting at <som> according to header list
749 * <cap_hdr>, and fill the <idx> structure appropriately.
750 */
751void capture_headers(char *som, struct hdr_idx *idx,
752 char **cap, struct cap_hdr *cap_hdr)
753{
754 char *eol, *sol, *col, *sov;
755 int cur_idx;
756 struct cap_hdr *h;
757 int len;
758
759 sol = som + hdr_idx_first_pos(idx);
760 cur_idx = hdr_idx_first_idx(idx);
761
762 while (cur_idx) {
763 eol = sol + idx->v[cur_idx].len;
764
765 col = sol;
766 while (col < eol && *col != ':')
767 col++;
768
769 sov = col + 1;
770 while (sov < eol && http_is_lws[(unsigned char)*sov])
771 sov++;
772
773 for (h = cap_hdr; h; h = h->next) {
774 if ((h->namelen == col - sol) &&
775 (strncasecmp(sol, h->name, h->namelen) == 0)) {
776 if (cap[h->index] == NULL)
777 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200778 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100779
780 if (cap[h->index] == NULL) {
781 Alert("HTTP capture : out of memory.\n");
782 continue;
783 }
784
785 len = eol - sov;
786 if (len > h->len)
787 len = h->len;
788
789 memcpy(cap[h->index], sov, len);
790 cap[h->index][len]=0;
791 }
792 }
793 sol = eol + idx->v[cur_idx].cr + 1;
794 cur_idx = idx->v[cur_idx].next;
795 }
796}
797
798
Willy Tarreau42250582007-04-01 01:30:43 +0200799/* either we find an LF at <ptr> or we jump to <bad>.
800 */
801#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
802
803/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
804 * otherwise to <http_msg_ood> with <state> set to <st>.
805 */
806#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
807 ptr++; \
808 if (likely(ptr < end)) \
809 goto good; \
810 else { \
811 state = (st); \
812 goto http_msg_ood; \
813 } \
814 } while (0)
815
816
Willy Tarreaubaaee002006-06-26 02:48:02 +0200817/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100818 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100819 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
820 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
821 * will give undefined results.
822 * Note that it is upon the caller's responsibility to ensure that ptr < end,
823 * and that msg->sol points to the beginning of the response.
824 * If a complete line is found (which implies that at least one CR or LF is
825 * found before <end>, the updated <ptr> is returned, otherwise NULL is
826 * returned indicating an incomplete line (which does not mean that parts have
827 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
828 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
829 * upon next call.
830 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200831 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100832 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
833 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200834 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100835 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100836const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100837 const char *ptr, const char *end,
838 char **ret_ptr, int *ret_state)
839{
840 __label__
841 http_msg_rpver,
842 http_msg_rpver_sp,
843 http_msg_rpcode,
844 http_msg_rpcode_sp,
845 http_msg_rpreason,
846 http_msg_rpline_eol,
847 http_msg_ood, /* out of data */
848 http_msg_invalid;
849
850 switch (state) {
851 http_msg_rpver:
852 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100853 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100854 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
855
856 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100857 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100858 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
859 }
860 goto http_msg_invalid;
861
862 http_msg_rpver_sp:
863 case HTTP_MSG_RPVER_SP:
864 if (likely(!HTTP_IS_LWS(*ptr))) {
865 msg->sl.st.c = ptr - msg_buf;
866 goto http_msg_rpcode;
867 }
868 if (likely(HTTP_IS_SPHT(*ptr)))
869 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
870 /* so it's a CR/LF, this is invalid */
871 goto http_msg_invalid;
872
873 http_msg_rpcode:
874 case HTTP_MSG_RPCODE:
875 if (likely(!HTTP_IS_LWS(*ptr)))
876 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
877
878 if (likely(HTTP_IS_SPHT(*ptr))) {
879 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
880 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
881 }
882
883 /* so it's a CR/LF, so there is no reason phrase */
884 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
885 http_msg_rsp_reason:
886 /* FIXME: should we support HTTP responses without any reason phrase ? */
887 msg->sl.st.r = ptr - msg_buf;
888 msg->sl.st.r_l = 0;
889 goto http_msg_rpline_eol;
890
891 http_msg_rpcode_sp:
892 case HTTP_MSG_RPCODE_SP:
893 if (likely(!HTTP_IS_LWS(*ptr))) {
894 msg->sl.st.r = ptr - msg_buf;
895 goto http_msg_rpreason;
896 }
897 if (likely(HTTP_IS_SPHT(*ptr)))
898 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
899 /* so it's a CR/LF, so there is no reason phrase */
900 goto http_msg_rsp_reason;
901
902 http_msg_rpreason:
903 case HTTP_MSG_RPREASON:
904 if (likely(!HTTP_IS_CRLF(*ptr)))
905 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
906 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
907 http_msg_rpline_eol:
908 /* We have seen the end of line. Note that we do not
909 * necessarily have the \n yet, but at least we know that we
910 * have EITHER \r OR \n, otherwise the response would not be
911 * complete. We can then record the response length and return
912 * to the caller which will be able to register it.
913 */
914 msg->sl.st.l = ptr - msg->sol;
915 return ptr;
916
917#ifdef DEBUG_FULL
918 default:
919 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
920 exit(1);
921#endif
922 }
923
924 http_msg_ood:
925 /* out of data */
926 if (ret_state)
927 *ret_state = state;
928 if (ret_ptr)
929 *ret_ptr = (char *)ptr;
930 return NULL;
931
932 http_msg_invalid:
933 /* invalid message */
934 if (ret_state)
935 *ret_state = HTTP_MSG_ERROR;
936 return NULL;
937}
938
939
940/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100941 * This function parses a request line between <ptr> and <end>, starting with
942 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
943 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
944 * will give undefined results.
945 * Note that it is upon the caller's responsibility to ensure that ptr < end,
946 * and that msg->sol points to the beginning of the request.
947 * If a complete line is found (which implies that at least one CR or LF is
948 * found before <end>, the updated <ptr> is returned, otherwise NULL is
949 * returned indicating an incomplete line (which does not mean that parts have
950 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
951 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
952 * upon next call.
953 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200954 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100955 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
956 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200957 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100959const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 const char *ptr, const char *end,
961 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100963 __label__
964 http_msg_rqmeth,
965 http_msg_rqmeth_sp,
966 http_msg_rquri,
967 http_msg_rquri_sp,
968 http_msg_rqver,
969 http_msg_rqline_eol,
970 http_msg_ood, /* out of data */
971 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100972
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100973 switch (state) {
974 http_msg_rqmeth:
975 case HTTP_MSG_RQMETH:
976 if (likely(HTTP_IS_TOKEN(*ptr)))
977 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100978
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100979 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100980 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100981 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
982 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100983
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100984 if (likely(HTTP_IS_CRLF(*ptr))) {
985 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100986 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100987 http_msg_req09_uri:
988 msg->sl.rq.u = ptr - msg_buf;
989 http_msg_req09_uri_e:
990 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
991 http_msg_req09_ver:
992 msg->sl.rq.v = ptr - msg_buf;
993 msg->sl.rq.v_l = 0;
994 goto http_msg_rqline_eol;
995 }
996 goto http_msg_invalid;
997
998 http_msg_rqmeth_sp:
999 case HTTP_MSG_RQMETH_SP:
1000 if (likely(!HTTP_IS_LWS(*ptr))) {
1001 msg->sl.rq.u = ptr - msg_buf;
1002 goto http_msg_rquri;
1003 }
1004 if (likely(HTTP_IS_SPHT(*ptr)))
1005 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1006 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1007 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001008
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001009 http_msg_rquri:
1010 case HTTP_MSG_RQURI:
1011 if (likely(!HTTP_IS_LWS(*ptr)))
1012 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001013
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001014 if (likely(HTTP_IS_SPHT(*ptr))) {
1015 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1016 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1017 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001019 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1020 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001021
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001022 http_msg_rquri_sp:
1023 case HTTP_MSG_RQURI_SP:
1024 if (likely(!HTTP_IS_LWS(*ptr))) {
1025 msg->sl.rq.v = ptr - msg_buf;
1026 goto http_msg_rqver;
1027 }
1028 if (likely(HTTP_IS_SPHT(*ptr)))
1029 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1030 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1031 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001032
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001033 http_msg_rqver:
1034 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001035 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001036 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001037
1038 if (likely(HTTP_IS_CRLF(*ptr))) {
1039 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1040 http_msg_rqline_eol:
1041 /* We have seen the end of line. Note that we do not
1042 * necessarily have the \n yet, but at least we know that we
1043 * have EITHER \r OR \n, otherwise the request would not be
1044 * complete. We can then record the request length and return
1045 * to the caller which will be able to register it.
1046 */
1047 msg->sl.rq.l = ptr - msg->sol;
1048 return ptr;
1049 }
1050
1051 /* neither an HTTP_VER token nor a CRLF */
1052 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001053
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001054#ifdef DEBUG_FULL
1055 default:
1056 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1057 exit(1);
1058#endif
1059 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001061 http_msg_ood:
1062 /* out of data */
1063 if (ret_state)
1064 *ret_state = state;
1065 if (ret_ptr)
1066 *ret_ptr = (char *)ptr;
1067 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001068
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001069 http_msg_invalid:
1070 /* invalid message */
1071 if (ret_state)
1072 *ret_state = HTTP_MSG_ERROR;
1073 return NULL;
1074}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001075
1076
Willy Tarreau8973c702007-01-21 23:58:29 +01001077/*
1078 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001079 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001080 * when data are missing and recalled at the exact same location with no
1081 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001082 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1083 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001084 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1086{
1087 __label__
1088 http_msg_rqbefore,
1089 http_msg_rqbefore_cr,
1090 http_msg_rqmeth,
1091 http_msg_rqline_end,
1092 http_msg_hdr_first,
1093 http_msg_hdr_name,
1094 http_msg_hdr_l1_sp,
1095 http_msg_hdr_l1_lf,
1096 http_msg_hdr_l1_lws,
1097 http_msg_hdr_val,
1098 http_msg_hdr_l2_lf,
1099 http_msg_hdr_l2_lws,
1100 http_msg_complete_header,
1101 http_msg_last_lf,
1102 http_msg_ood, /* out of data */
1103 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 int state; /* updated only when leaving the FSM */
1106 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001107
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001108 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 ptr = buf->lr;
1110 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 if (unlikely(ptr >= end))
1113 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001116 /*
1117 * First, states that are specific to the response only.
1118 * We check them first so that request and headers are
1119 * closer to each other (accessed more often).
1120 */
1121 http_msg_rpbefore:
1122 case HTTP_MSG_RPBEFORE:
1123 if (likely(HTTP_IS_TOKEN(*ptr))) {
1124 if (likely(ptr == buf->data)) {
1125 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001126 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001127 } else {
1128#if PARSE_PRESERVE_EMPTY_LINES
1129 /* only skip empty leading lines, don't remove them */
1130 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001131 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001132#else
1133 /* Remove empty leading lines, as recommended by
1134 * RFC2616. This takes a lot of time because we
1135 * must move all the buffer backwards, but this
1136 * is rarely needed. The method above will be
1137 * cleaner when we'll be able to start sending
1138 * the request from any place in the buffer.
1139 */
1140 buf->lr = ptr;
1141 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001142 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001143 msg->sol = buf->data;
1144 ptr = buf->data;
1145 end = buf->r;
1146#endif
1147 }
1148 hdr_idx_init(idx);
1149 state = HTTP_MSG_RPVER;
1150 goto http_msg_rpver;
1151 }
1152
1153 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1154 goto http_msg_invalid;
1155
1156 if (unlikely(*ptr == '\n'))
1157 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1158 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1159 /* stop here */
1160
1161 http_msg_rpbefore_cr:
1162 case HTTP_MSG_RPBEFORE_CR:
1163 EXPECT_LF_HERE(ptr, http_msg_invalid);
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1165 /* stop here */
1166
1167 http_msg_rpver:
1168 case HTTP_MSG_RPVER:
1169 case HTTP_MSG_RPVER_SP:
1170 case HTTP_MSG_RPCODE:
1171 case HTTP_MSG_RPCODE_SP:
1172 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001173 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001174 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001175 if (unlikely(!ptr))
1176 return;
1177
1178 /* we have a full response and we know that we have either a CR
1179 * or an LF at <ptr>.
1180 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001181 //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 +01001182 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1183
1184 msg->sol = ptr;
1185 if (likely(*ptr == '\r'))
1186 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1187 goto http_msg_rpline_end;
1188
1189 http_msg_rpline_end:
1190 case HTTP_MSG_RPLINE_END:
1191 /* msg->sol must point to the first of CR or LF. */
1192 EXPECT_LF_HERE(ptr, http_msg_invalid);
1193 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1194 /* stop here */
1195
1196 /*
1197 * Second, states that are specific to the request only
1198 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199 http_msg_rqbefore:
1200 case HTTP_MSG_RQBEFORE:
1201 if (likely(HTTP_IS_TOKEN(*ptr))) {
1202 if (likely(ptr == buf->data)) {
1203 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001204 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001205 } else {
1206#if PARSE_PRESERVE_EMPTY_LINES
1207 /* only skip empty leading lines, don't remove them */
1208 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001209 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001210#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001211 /* Remove empty leading lines, as recommended by
1212 * RFC2616. This takes a lot of time because we
1213 * must move all the buffer backwards, but this
1214 * is rarely needed. The method above will be
1215 * cleaner when we'll be able to start sending
1216 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001217 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001218 buf->lr = ptr;
1219 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001220 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001221 msg->sol = buf->data;
1222 ptr = buf->data;
1223 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001224#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001226 /* we will need this when keep-alive will be supported
1227 hdr_idx_init(idx);
1228 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001229 state = HTTP_MSG_RQMETH;
1230 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1234 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 if (unlikely(*ptr == '\n'))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1238 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001239 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 http_msg_rqbefore_cr:
1242 case HTTP_MSG_RQBEFORE_CR:
1243 EXPECT_LF_HERE(ptr, http_msg_invalid);
1244 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 http_msg_rqmeth:
1248 case HTTP_MSG_RQMETH:
1249 case HTTP_MSG_RQMETH_SP:
1250 case HTTP_MSG_RQURI:
1251 case HTTP_MSG_RQURI_SP:
1252 case HTTP_MSG_RQVER:
1253 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001254 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 if (unlikely(!ptr))
1256 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258 /* we have a full request and we know that we have either a CR
1259 * or an LF at <ptr>.
1260 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001261 //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 +01001262 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 msg->sol = ptr;
1265 if (likely(*ptr == '\r'))
1266 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 http_msg_rqline_end:
1270 case HTTP_MSG_RQLINE_END:
1271 /* check for HTTP/0.9 request : no version information available.
1272 * msg->sol must point to the first of CR or LF.
1273 */
1274 if (unlikely(msg->sl.rq.v_l == 0))
1275 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 EXPECT_LF_HERE(ptr, http_msg_invalid);
1278 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001280
Willy Tarreau8973c702007-01-21 23:58:29 +01001281 /*
1282 * Common states below
1283 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 http_msg_hdr_first:
1285 case HTTP_MSG_HDR_FIRST:
1286 msg->sol = ptr;
1287 if (likely(!HTTP_IS_CRLF(*ptr))) {
1288 goto http_msg_hdr_name;
1289 }
1290
1291 if (likely(*ptr == '\r'))
1292 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1293 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 http_msg_hdr_name:
1296 case HTTP_MSG_HDR_NAME:
1297 /* assumes msg->sol points to the first char */
1298 if (likely(HTTP_IS_TOKEN(*ptr)))
1299 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001300
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 if (likely(*ptr == ':')) {
1302 msg->col = ptr - buf->data;
1303 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1304 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001307
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 http_msg_hdr_l1_sp:
1309 case HTTP_MSG_HDR_L1_SP:
1310 /* assumes msg->sol points to the first char and msg->col to the colon */
1311 if (likely(HTTP_IS_SPHT(*ptr)))
1312 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001313
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 /* header value can be basically anything except CR/LF */
1315 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 if (likely(!HTTP_IS_CRLF(*ptr))) {
1318 goto http_msg_hdr_val;
1319 }
1320
1321 if (likely(*ptr == '\r'))
1322 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1323 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 http_msg_hdr_l1_lf:
1326 case HTTP_MSG_HDR_L1_LF:
1327 EXPECT_LF_HERE(ptr, http_msg_invalid);
1328 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 http_msg_hdr_l1_lws:
1331 case HTTP_MSG_HDR_L1_LWS:
1332 if (likely(HTTP_IS_SPHT(*ptr))) {
1333 /* replace HT,CR,LF with spaces */
1334 for (; buf->data+msg->sov < ptr; msg->sov++)
1335 buf->data[msg->sov] = ' ';
1336 goto http_msg_hdr_l1_sp;
1337 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001338 /* we had a header consisting only in spaces ! */
1339 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 goto http_msg_complete_header;
1341
1342 http_msg_hdr_val:
1343 case HTTP_MSG_HDR_VAL:
1344 /* assumes msg->sol points to the first char, msg->col to the
1345 * colon, and msg->sov points to the first character of the
1346 * value.
1347 */
1348 if (likely(!HTTP_IS_CRLF(*ptr)))
1349 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001350
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 msg->eol = ptr;
1352 /* Note: we could also copy eol into ->eoh so that we have the
1353 * real header end in case it ends with lots of LWS, but is this
1354 * really needed ?
1355 */
1356 if (likely(*ptr == '\r'))
1357 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1358 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001359
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 http_msg_hdr_l2_lf:
1361 case HTTP_MSG_HDR_L2_LF:
1362 EXPECT_LF_HERE(ptr, http_msg_invalid);
1363 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001364
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 http_msg_hdr_l2_lws:
1366 case HTTP_MSG_HDR_L2_LWS:
1367 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1368 /* LWS: replace HT,CR,LF with spaces */
1369 for (; msg->eol < ptr; msg->eol++)
1370 *msg->eol = ' ';
1371 goto http_msg_hdr_val;
1372 }
1373 http_msg_complete_header:
1374 /*
1375 * It was a new header, so the last one is finished.
1376 * Assumes msg->sol points to the first char, msg->col to the
1377 * colon, msg->sov points to the first character of the value
1378 * and msg->eol to the first CR or LF so we know how the line
1379 * ends. We insert last header into the index.
1380 */
1381 /*
1382 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1383 write(2, msg->sol, msg->eol-msg->sol);
1384 fprintf(stderr,"\n");
1385 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1388 idx, idx->tail) < 0))
1389 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 msg->sol = ptr;
1392 if (likely(!HTTP_IS_CRLF(*ptr))) {
1393 goto http_msg_hdr_name;
1394 }
1395
1396 if (likely(*ptr == '\r'))
1397 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1398 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 http_msg_last_lf:
1401 case HTTP_MSG_LAST_LF:
1402 /* Assumes msg->sol points to the first of either CR or LF */
1403 EXPECT_LF_HERE(ptr, http_msg_invalid);
1404 ptr++;
1405 buf->lr = ptr;
1406 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001407 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 return;
1409#ifdef DEBUG_FULL
1410 default:
1411 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1412 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001413#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 }
1415 http_msg_ood:
1416 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001417 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 buf->lr = ptr;
1419 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 http_msg_invalid:
1422 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001423 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 return;
1425}
1426
1427/*
1428 * manages the client FSM and its socket. BTW, it also tries to handle the
1429 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1430 * 0 else.
1431 */
1432int process_cli(struct session *t)
1433{
1434 int s = t->srv_state;
1435 int c = t->cli_state;
1436 struct buffer *req = t->req;
1437 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1440 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001441 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 req->rex.tv_sec, req->rex.tv_usec,
1443 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001444
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 if (c == CL_STHEADERS) {
1446 /*
1447 * Now parse the partial (or complete) lines.
1448 * We will check the request syntax, and also join multi-line
1449 * headers. An index of all the lines will be elaborated while
1450 * parsing.
1451 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001455 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 * req->data + req->eoh = end of processed headers / start of current one
1457 * req->data + req->eol = end of current header or line (LF or CRLF)
1458 * req->lr = first non-visited byte
1459 * req->r = end of data
1460 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001463 struct http_txn *txn = &t->txn;
1464 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001468 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 /* 1: we might have to print this header in debug mode */
1471 if (unlikely((global.mode & MODE_DEBUG) &&
1472 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001473 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001476 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 eol = sol + msg->sl.rq.l;
1478 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001479
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001480 sol += hdr_idx_first_pos(&txn->hdr_idx);
1481 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001484 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001486 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1487 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 }
1489 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001490
Willy Tarreau58f10d72006-12-04 02:26:12 +01001491
1492 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001494 * If not so, we check the FD and buffer states before leaving.
1495 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1497 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001498 *
1499 */
1500
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001501 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 /*
1503 * First, let's catch bad requests.
1504 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001505 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001507
1508 /* 1: Since we are in header mode, if there's no space
1509 * left for headers, we won't be able to free more
1510 * later, so the session will never terminate. We
1511 * must terminate it now.
1512 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 if (unlikely(req->l >= req->rlim - req->data)) {
1514 /* FIXME: check if URI is set and return Status
1515 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001516 */
Willy Tarreau06619262006-12-17 08:37:22 +01001517 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001518 }
1519
1520 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1522 /* read error, or last read : give up. */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001523 tv_eternity(&req->rex);
1524 fd_delete(t->cli_fd);
1525 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001526 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001527 if (!(t->flags & SN_ERR_MASK))
1528 t->flags |= SN_ERR_CLICL;
1529 if (!(t->flags & SN_FINST_MASK))
1530 t->flags |= SN_FINST_R;
1531 return 1;
1532 }
1533
1534 /* 3: has the read timeout expired ? */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001535 else if (unlikely(tv_isle(&req->rex, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001536 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001537 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001538 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001539 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001540 if (!(t->flags & SN_ERR_MASK))
1541 t->flags |= SN_ERR_CLITO;
1542 if (!(t->flags & SN_FINST_MASK))
1543 t->flags |= SN_FINST_R;
1544 return 1;
1545 }
1546
1547 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001548 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001549 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001550 * full. We cannot loop here since stream_sock_read will disable it only if
1551 * req->l == rlim-data
1552 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001553 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001554 tv_eternity(&req->rex);
1555 }
1556 return t->cli_state != CL_STHEADERS;
1557 }
1558
1559
1560 /****************************************************************
1561 * More interesting part now : we know that we have a complete *
1562 * request which at least looks like HTTP. We have an indicator *
1563 * of each header's length, so we can parse them quickly. *
1564 ****************************************************************/
1565
Willy Tarreau9cdde232007-05-02 20:58:19 +02001566 /* ensure we keep this pointer to the beginning of the message */
1567 msg->sol = req->data + msg->som;
1568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 /*
1570 * 1: identify the method
1571 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001572 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001573
1574 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001576 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001578 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 if (unlikely((t->fe->monitor_uri_len != 0) &&
1580 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1581 !memcmp(&req->data[msg->sl.rq.u],
1582 t->fe->monitor_uri,
1583 t->fe->monitor_uri_len))) {
1584 /*
1585 * We have found the monitor URI
1586 */
1587 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001588 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 client_retnclose(t, &http_200_chunk);
1590 goto return_prx_cond;
1591 }
1592
1593 /*
1594 * 3: Maybe we have to copy the original REQURI for the logs ?
1595 * Note: we cannot log anymore if the request has been
1596 * classified as invalid.
1597 */
1598 if (unlikely(t->logs.logwait & LW_REQ)) {
1599 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001600 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 int urilen = msg->sl.rq.l;
1602
1603 if (urilen >= REQURI_LEN)
1604 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001605 memcpy(txn->uri, &req->data[msg->som], urilen);
1606 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607
1608 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001609 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 } else {
1611 Alert("HTTP logging : out of memory.\n");
1612 }
1613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001614
Willy Tarreau06619262006-12-17 08:37:22 +01001615
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1617 if (unlikely(msg->sl.rq.v_l == 0)) {
1618 int delta;
1619 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001620 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 cur_end = msg->sol + msg->sl.rq.l;
1622 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001623
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 if (msg->sl.rq.u_l == 0) {
1625 /* if no URI was set, add "/" */
1626 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1627 cur_end += delta;
1628 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001629 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 /* add HTTP version */
1631 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1632 msg->eoh += delta;
1633 cur_end += delta;
1634 cur_end = (char *)http_parse_reqline(msg, req->data,
1635 HTTP_MSG_RQMETH,
1636 msg->sol, cur_end + 1,
1637 NULL, NULL);
1638 if (unlikely(!cur_end))
1639 goto return_bad_req;
1640
1641 /* we have a full HTTP/1.0 request now and we know that
1642 * we have either a CR or an LF at <ptr>.
1643 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001644 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001645 }
1646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647
1648 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001649 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001650 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001651 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001652
1653 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001655 * As opposed to version 1.2, now they will be evaluated in the
1656 * filters order and not in the header order. This means that
1657 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001658 *
1659 * We can now check whether we want to switch to another
1660 * backend, in which case we will re-check the backend's
1661 * filters and various options. In order to support 3-level
1662 * switching, here's how we should proceed :
1663 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001664 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001665 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001666 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001667 * There cannot be any switch from there, so ->be cannot be
1668 * changed anymore.
1669 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001670 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001671 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001672 * The response path will be able to apply either ->be, or
1673 * ->be then ->fe filters in order to match the reverse of
1674 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001675 */
1676
Willy Tarreau06619262006-12-17 08:37:22 +01001677 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001678 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001679 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001680 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001681
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001682 /* first check whether we have some ACLs set to block this request */
1683 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
1684 int ret = acl_exec_cond(cond, cur_proxy, t, txn);
1685 if (cond->pol == ACL_COND_UNLESS)
1686 ret = !ret;
1687
1688 if (ret) {
1689 txn->status = 403;
1690 /* let's log the request time */
1691 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1692 client_retnclose(t, error_message(t, HTTP_ERR_403));
1693 goto return_prx_cond;
1694 }
1695 }
1696
Willy Tarreau06619262006-12-17 08:37:22 +01001697 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001698 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1700 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001701 }
1702
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001703 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1704 /* to ensure correct connection accounting on
1705 * the backend, we count the connection for the
1706 * one managing the queue.
1707 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001708 t->be->beconn++;
1709 if (t->be->beconn > t->be->beconn_max)
1710 t->be->beconn_max = t->be->beconn;
1711 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001712 t->flags |= SN_BE_ASSIGNED;
1713 }
1714
Willy Tarreau06619262006-12-17 08:37:22 +01001715 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001716 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001717 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001718 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001719 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001720 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001721 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001722 goto return_prx_cond;
1723 }
1724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001726 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 !(t->flags & SN_CONN_CLOSED)) {
1728 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001729 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001731
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001732 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 old_idx = 0;
1734
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001735 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1736 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 cur_ptr = cur_next;
1738 cur_end = cur_ptr + cur_hdr->len;
1739 cur_next = cur_end + cur_hdr->cr + 1;
1740
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001741 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1742 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 /* 3 possibilities :
1744 * - we have already set Connection: close,
1745 * so we remove this line.
1746 * - we have not yet set Connection: close,
1747 * but this line indicates close. We leave
1748 * it untouched and set the flag.
1749 * - we have not yet set Connection: close,
1750 * and this line indicates non-close. We
1751 * replace it.
1752 */
1753 if (t->flags & SN_CONN_CLOSED) {
1754 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001755 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001757 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1758 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 cur_hdr->len = 0;
1760 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001761 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1762 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1763 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 cur_next += delta;
1765 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001766 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 }
1768 t->flags |= SN_CONN_CLOSED;
1769 }
1770 }
1771 old_idx = cur_idx;
1772 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001773 }
1774 /* add request headers from the rule sets in the same order */
1775 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1776 if (unlikely(http_header_add_tail(req,
1777 &txn->req,
1778 &txn->hdr_idx,
1779 rule_set->req_add[cur_idx])) < 0)
1780 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001781 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001782
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001783 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001784 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001785 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001786 /* we have to check the URI and auth for this request */
1787 if (stats_check_uri_auth(t, rule_set))
1788 return 1;
1789 }
1790
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001791 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1792 /* No backend was set, but there was a default
1793 * backend set in the frontend, so we use it and
1794 * loop again.
1795 */
1796 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001797 t->be->beconn++;
1798 if (t->be->beconn > t->be->beconn_max)
1799 t->be->beconn_max = t->be->beconn;
1800 t->be->cum_beconn++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001801 t->flags |= SN_BE_ASSIGNED;
1802 }
1803 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001804
Willy Tarreau58f10d72006-12-04 02:26:12 +01001805
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001806 if (!(t->flags & SN_BE_ASSIGNED)) {
1807 /* To ensure correct connection accounting on
1808 * the backend, we count the connection for the
1809 * one managing the queue.
1810 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001811 t->be->beconn++;
1812 if (t->be->beconn > t->be->beconn_max)
1813 t->be->beconn_max = t->be->beconn;
1814 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001815 t->flags |= SN_BE_ASSIGNED;
1816 }
1817
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001818 /*
1819 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001820 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001821 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001822 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001823 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001824
Willy Tarreau58f10d72006-12-04 02:26:12 +01001825
Willy Tarreau58f10d72006-12-04 02:26:12 +01001826
Willy Tarreau58f10d72006-12-04 02:26:12 +01001827
Willy Tarreau2a324282006-12-05 00:05:46 +01001828 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001829 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001830 * so let's do the same now.
1831 */
1832
1833 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001834 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001835 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001836 }
1837
1838
1839 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001841 * Note that doing so might move headers in the request, but
1842 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001843 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001844 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001845 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001846 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001847
Willy Tarreau58f10d72006-12-04 02:26:12 +01001848
Willy Tarreau2a324282006-12-05 00:05:46 +01001849 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001850 * 9: add X-Forwarded-For if either the frontend or the backend
1851 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001852 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001853 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001854 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001855 /* Add an X-Forwarded-For header unless the source IP is
1856 * in the 'except' network range.
1857 */
1858 if ((!t->fe->except_mask.s_addr ||
1859 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1860 != t->fe->except_net.s_addr) &&
1861 (!t->be->except_mask.s_addr ||
1862 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1863 != t->be->except_net.s_addr)) {
1864 int len;
1865 unsigned char *pn;
1866 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001867
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001868 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1869 pn[0], pn[1], pn[2], pn[3]);
1870
1871 if (unlikely(http_header_add_tail2(req, &txn->req,
1872 &txn->hdr_idx, trash, len)) < 0)
1873 goto return_bad_req;
1874 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001875 }
1876 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001877 /* FIXME: for the sake of completeness, we should also support
1878 * 'except' here, although it is mostly useless in this case.
1879 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001880 int len;
1881 char pn[INET6_ADDRSTRLEN];
1882 inet_ntop(AF_INET6,
1883 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1884 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001885 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1886 if (unlikely(http_header_add_tail2(req, &txn->req,
1887 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001888 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001889 }
1890 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001891
Willy Tarreau2a324282006-12-05 00:05:46 +01001892 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02001894 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01001895 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02001896 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001897 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02001898 if ((unlikely(msg->sl.rq.v_l != 8) ||
1899 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
1900 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001901 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001902 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001903 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001904 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001905
Willy Tarreau2a324282006-12-05 00:05:46 +01001906 /*************************************************************
1907 * OK, that's finished for the headers. We have done what we *
1908 * could. Let's switch to the DATA state. *
1909 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001910
Willy Tarreau2a324282006-12-05 00:05:46 +01001911 t->cli_state = CL_STDATA;
1912 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001913
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001914 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001915
Willy Tarreaud825eef2007-05-12 22:35:00 +02001916 if (!tv_isset(&t->fe->clitimeout) ||
1917 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001918 /* If the client has no timeout, or if the server is not ready yet,
1919 * and we know for sure that it can expire, then it's cleaner to
1920 * disable the timeout on the client side so that too low values
1921 * cannot make the sessions abort too early.
1922 *
1923 * FIXME-20050705: the server needs a way to re-enable this time-out
1924 * when it switches its state, otherwise a client can stay connected
1925 * indefinitely. This now seems to be OK.
1926 */
1927 tv_eternity(&req->rex);
1928 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001929
Willy Tarreau2a324282006-12-05 00:05:46 +01001930 /* When a connection is tarpitted, we use the queue timeout for the
1931 * tarpit delay, which currently happens to be the server's connect
1932 * timeout. If unset, then set it to zero because we really want it
1933 * to expire at one moment.
1934 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001935 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001936 t->req->l = 0;
1937 /* flush the request so that we can drop the connection early
1938 * if the client closes first.
1939 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001940 if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout))
Willy Tarreaud825eef2007-05-12 22:35:00 +02001941 req->cex = now;
Willy Tarreau2a324282006-12-05 00:05:46 +01001942 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001943
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001944 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01001945 goto process_data;
1946
1947 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001948 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001949 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01001950 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001951 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01001952 return_prx_cond:
1953 if (!(t->flags & SN_ERR_MASK))
1954 t->flags |= SN_ERR_PRXCOND;
1955 if (!(t->flags & SN_FINST_MASK))
1956 t->flags |= SN_FINST_R;
1957 return 1;
1958
Willy Tarreaubaaee002006-06-26 02:48:02 +02001959 }
1960 else if (c == CL_STDATA) {
1961 process_data:
1962 /* FIXME: this error handling is partly buggy because we always report
1963 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1964 * or HEADER phase. BTW, it's not logical to expire the client while
1965 * we're waiting for the server to connect.
1966 */
1967 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001968 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001969 tv_eternity(&req->rex);
1970 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001971 fd_delete(t->cli_fd);
1972 t->cli_state = CL_STCLOSE;
1973 if (!(t->flags & SN_ERR_MASK))
1974 t->flags |= SN_ERR_CLICL;
1975 if (!(t->flags & SN_FINST_MASK)) {
1976 if (t->pend_pos)
1977 t->flags |= SN_FINST_Q;
1978 else if (s == SV_STCONN)
1979 t->flags |= SN_FINST_C;
1980 else
1981 t->flags |= SN_FINST_D;
1982 }
1983 return 1;
1984 }
1985 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001986 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001987 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02001988 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001989 t->cli_state = CL_STSHUTR;
1990 return 1;
1991 }
1992 /* last server read and buffer empty */
1993 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001994 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02001995 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001996 shutdown(t->cli_fd, SHUT_WR);
1997 /* We must ensure that the read part is still alive when switching
1998 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02001999 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002000 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002001 t->cli_state = CL_STSHUTW;
2002 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2003 return 1;
2004 }
2005 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002006 else if (tv_isle(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002007 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02002008 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002009 t->cli_state = CL_STSHUTR;
2010 if (!(t->flags & SN_ERR_MASK))
2011 t->flags |= SN_ERR_CLITO;
2012 if (!(t->flags & SN_FINST_MASK)) {
2013 if (t->pend_pos)
2014 t->flags |= SN_FINST_Q;
2015 else if (s == SV_STCONN)
2016 t->flags |= SN_FINST_C;
2017 else
2018 t->flags |= SN_FINST_D;
2019 }
2020 return 1;
2021 }
2022 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002023 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002024 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002025 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002026 shutdown(t->cli_fd, SHUT_WR);
2027 /* We must ensure that the read part is still alive when switching
2028 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002029 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002030 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002031
2032 t->cli_state = CL_STSHUTW;
2033 if (!(t->flags & SN_ERR_MASK))
2034 t->flags |= SN_ERR_CLITO;
2035 if (!(t->flags & SN_FINST_MASK)) {
2036 if (t->pend_pos)
2037 t->flags |= SN_FINST_Q;
2038 else if (s == SV_STCONN)
2039 t->flags |= SN_FINST_C;
2040 else
2041 t->flags |= SN_FINST_D;
2042 }
2043 return 1;
2044 }
2045
2046 if (req->l >= req->rlim - req->data) {
2047 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002048 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002049 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002050 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002051 }
2052 } else {
2053 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002054 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud825eef2007-05-12 22:35:00 +02002055 if (!tv_isset(&t->fe->clitimeout) ||
2056 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout)))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002057 /* If the client has no timeout, or if the server not ready yet, and we
2058 * know for sure that it can expire, then it's cleaner to disable the
2059 * timeout on the client side so that too low values cannot make the
2060 * sessions abort too early.
2061 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002062 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002063 else
Willy Tarreaud825eef2007-05-12 22:35:00 +02002064 tv_add(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002065 }
2066 }
2067
2068 if ((rep->l == 0) ||
2069 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002070 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2071 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002072 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002073 }
2074 } else {
2075 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002076 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2077 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002078 if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002079 /* FIXME: to prevent the client from expiring read timeouts during writes,
2080 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002081 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002082 }
2083 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002084 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002085 }
2086 }
2087 return 0; /* other cases change nothing */
2088 }
2089 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002090 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002091 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002092 fd_delete(t->cli_fd);
2093 t->cli_state = CL_STCLOSE;
2094 if (!(t->flags & SN_ERR_MASK))
2095 t->flags |= SN_ERR_CLICL;
2096 if (!(t->flags & SN_FINST_MASK)) {
2097 if (t->pend_pos)
2098 t->flags |= SN_FINST_Q;
2099 else if (s == SV_STCONN)
2100 t->flags |= SN_FINST_C;
2101 else
2102 t->flags |= SN_FINST_D;
2103 }
2104 return 1;
2105 }
2106 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2107 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002108 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002109 fd_delete(t->cli_fd);
2110 t->cli_state = CL_STCLOSE;
2111 return 1;
2112 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002113 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002114 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002115 fd_delete(t->cli_fd);
2116 t->cli_state = CL_STCLOSE;
2117 if (!(t->flags & SN_ERR_MASK))
2118 t->flags |= SN_ERR_CLITO;
2119 if (!(t->flags & SN_FINST_MASK)) {
2120 if (t->pend_pos)
2121 t->flags |= SN_FINST_Q;
2122 else if (s == SV_STCONN)
2123 t->flags |= SN_FINST_C;
2124 else
2125 t->flags |= SN_FINST_D;
2126 }
2127 return 1;
2128 }
2129
2130 if (t->flags & SN_SELF_GEN) {
2131 produce_content(t);
2132 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002133 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002134 fd_delete(t->cli_fd);
2135 t->cli_state = CL_STCLOSE;
2136 return 1;
2137 }
2138 }
2139
2140 if ((rep->l == 0)
2141 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002142 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2143 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002144 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002145 }
2146 } else {
2147 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002148 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2149 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002150 if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002151 /* FIXME: to prevent the client from expiring read timeouts during writes,
2152 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002153 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002154 }
2155 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002156 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002157 }
2158 }
2159 return 0;
2160 }
2161 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002162 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002163 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002164 fd_delete(t->cli_fd);
2165 t->cli_state = CL_STCLOSE;
2166 if (!(t->flags & SN_ERR_MASK))
2167 t->flags |= SN_ERR_CLICL;
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 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002178 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002179 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002180 fd_delete(t->cli_fd);
2181 t->cli_state = CL_STCLOSE;
2182 return 1;
2183 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002184 else if (tv_isle(&req->rex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002185 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002186 fd_delete(t->cli_fd);
2187 t->cli_state = CL_STCLOSE;
2188 if (!(t->flags & SN_ERR_MASK))
2189 t->flags |= SN_ERR_CLITO;
2190 if (!(t->flags & SN_FINST_MASK)) {
2191 if (t->pend_pos)
2192 t->flags |= SN_FINST_Q;
2193 else if (s == SV_STCONN)
2194 t->flags |= SN_FINST_C;
2195 else
2196 t->flags |= SN_FINST_D;
2197 }
2198 return 1;
2199 }
2200 else if (req->l >= req->rlim - req->data) {
2201 /* no room to read more data */
2202
2203 /* FIXME-20050705: is it possible for a client to maintain a session
2204 * after the timeout by sending more data after it receives a close ?
2205 */
2206
Willy Tarreau66319382007-04-08 17:17:37 +02002207 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002208 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002209 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002210 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2211 }
2212 } else {
2213 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002214 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002215 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002216 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002217 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2218 }
2219 }
2220 return 0;
2221 }
2222 else { /* CL_STCLOSE: nothing to do */
2223 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2224 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002225 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 +02002226 write(1, trash, len);
2227 }
2228 return 0;
2229 }
2230 return 0;
2231}
2232
2233
2234/*
2235 * manages the server FSM and its socket. It returns 1 if a state has changed
2236 * (and a resync may be needed), 0 else.
2237 */
2238int process_srv(struct session *t)
2239{
2240 int s = t->srv_state;
2241 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002242 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002243 struct buffer *req = t->req;
2244 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002245 int conn_err;
2246
2247#ifdef DEBUG_FULL
2248 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2249#endif
Willy Tarreauee991362007-05-14 14:37:50 +02002250
2251#if 0
2252 fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n",
2253 __FUNCTION__, __LINE__,
2254 t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec,
2255 t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec,
2256 t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec);
2257 fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n",
2258 __FUNCTION__, __LINE__,
2259 t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec,
2260 t->be->contimeout.tv_sec, t->be->contimeout.tv_usec,
2261 t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec);
2262
2263 fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n",
2264 __FUNCTION__, __LINE__,
2265 req->cto.tv_sec, req->cto.tv_usec,
2266 req->rto.tv_sec, req->rto.tv_usec,
2267 req->wto.tv_sec, req->wto.tv_usec);
2268
2269 fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n",
2270 __FUNCTION__, __LINE__,
2271 rep->cto.tv_sec, rep->cto.tv_usec,
2272 rep->rto.tv_sec, rep->rto.tv_usec,
2273 rep->wto.tv_sec, rep->wto.tv_usec);
2274#endif
2275
Willy Tarreaubaaee002006-06-26 02:48:02 +02002276 //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 +02002277 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2278 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002279 //);
2280 if (s == SV_STIDLE) {
2281 if (c == CL_STHEADERS)
2282 return 0; /* stay in idle, waiting for data to reach the client side */
2283 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2284 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002285 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002286 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002287 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002288 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002289 /* note that this must not return any error because it would be able to
2290 * overwrite the client_retnclose() output.
2291 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002292 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002293 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002294 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002295 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 +02002296
2297 return 1;
2298 }
2299 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002300 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002301 /* This connection is being tarpitted. The CLIENT side has
2302 * already set the connect expiration date to the right
2303 * timeout. We just have to check that it has not expired.
2304 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002305 if (!tv_isle(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002306 return 0;
2307
2308 /* We will set the queue timer to the time spent, just for
2309 * logging purposes. We fake a 500 server error, so that the
2310 * attacker will not suspect his connection has been tarpitted.
2311 * It will not cause trouble to the logs because we can exclude
2312 * the tarpitted connections by filtering on the 'PT' status flags.
2313 */
2314 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002315 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002316 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002317 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002318 return 1;
2319 }
2320
Willy Tarreaubaaee002006-06-26 02:48:02 +02002321 /* Right now, we will need to create a connection to the server.
2322 * We might already have tried, and got a connection pending, in
2323 * which case we will not do anything till it's pending. It's up
2324 * to any other session to release it and wake us up again.
2325 */
2326 if (t->pend_pos) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002327 if (!tv_isle(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002328 return 0;
2329 else {
2330 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002331 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002332 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002333 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002334 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002335 if (t->srv)
2336 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002337 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002338 return 1;
2339 }
2340 }
2341
2342 do {
2343 /* first, get a connection */
2344 if (srv_redispatch_connect(t))
2345 return t->srv_state != SV_STIDLE;
2346
2347 /* try to (re-)connect to the server, and fail if we expire the
2348 * number of retries.
2349 */
2350 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002351 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002352 return t->srv_state != SV_STIDLE;
2353 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002354 } while (1);
2355 }
2356 }
2357 else if (s == SV_STCONN) { /* connection in progress */
2358 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2359 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002360 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2361 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002362 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002363 fd_delete(t->srv_fd);
2364 if (t->srv)
2365 t->srv->cur_sess--;
2366
2367 /* note that this must not return any error because it would be able to
2368 * overwrite the client_retnclose() output.
2369 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002370 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002371 return 1;
2372 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002373 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002374 //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 +02002375 return 0; /* nothing changed */
2376 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002377 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002378 /* timeout, asynchronous connect error or first write error */
2379 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2380
2381 fd_delete(t->srv_fd);
2382 if (t->srv)
2383 t->srv->cur_sess--;
2384
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002385 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002386 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2387 else
2388 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2389
2390 /* ensure that we have enough retries left */
2391 if (srv_count_retry_down(t, conn_err))
2392 return 1;
2393
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002394 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002395 /* We're on our last chance, and the REDISP option was specified.
2396 * We will ignore cookie and force to balance or use the dispatcher.
2397 */
2398 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002399 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002400 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002401
2402 if (t->srv)
2403 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002404 t->be->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002405
2406 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2407 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002408 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002409
2410 /* first, get a connection */
2411 if (srv_redispatch_connect(t))
2412 return t->srv_state != SV_STIDLE;
2413 }
2414
Willy Tarreaubaaee002006-06-26 02:48:02 +02002415 do {
2416 /* Now we will try to either reconnect to the same server or
2417 * connect to another server. If the connection gets queued
2418 * because all servers are saturated, then we will go back to
2419 * the SV_STIDLE state.
2420 */
2421 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002422 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002423 return t->srv_state != SV_STCONN;
2424 }
2425
2426 /* we need to redispatch the connection to another server */
2427 if (srv_redispatch_connect(t))
2428 return t->srv_state != SV_STCONN;
2429 } while (1);
2430 }
2431 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002432 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002433
2434 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2435 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002436 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002437 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002438 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002439 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002440 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002441 /* FIXME: to prevent the server from expiring read timeouts during writes,
2442 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002443 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002444 }
2445 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002446 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002447 }
2448
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002449 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002450 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002451 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002452 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002453
2454 t->srv_state = SV_STDATA;
2455 if (t->srv)
2456 t->srv->cum_sess++;
2457 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2458
2459 /* if the user wants to log as soon as possible, without counting
2460 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002461 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002462 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002463 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002464 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002465#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002466 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002467 /* TCP splicing supported by both FE and BE */
2468 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2469 }
2470#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002471 }
2472 else {
2473 t->srv_state = SV_STHEADERS;
2474 if (t->srv)
2475 t->srv->cum_sess++;
2476 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002477 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2478 /* reset hdr_idx which was already initialized by the request.
2479 * right now, the http parser does it.
2480 * hdr_idx_init(&t->txn.hdr_idx);
2481 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002482 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002483 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002484 return 1;
2485 }
2486 }
2487 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002488 /*
2489 * Now parse the partial (or complete) lines.
2490 * We will check the response syntax, and also join multi-line
2491 * headers. An index of all the lines will be elaborated while
2492 * parsing.
2493 *
2494 * For the parsing, we use a 28 states FSM.
2495 *
2496 * Here is the information we currently have :
2497 * rep->data + req->som = beginning of response
2498 * rep->data + req->eoh = end of processed headers / start of current one
2499 * rep->data + req->eol = end of current header or line (LF or CRLF)
2500 * rep->lr = first non-visited byte
2501 * rep->r = end of data
2502 */
2503
2504 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002505 struct http_msg *msg = &txn->rsp;
2506 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002507
Willy Tarreaua15645d2007-03-18 16:22:39 +01002508 if (likely(rep->lr < rep->r))
2509 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002510
Willy Tarreaua15645d2007-03-18 16:22:39 +01002511 /* 1: we might have to print this header in debug mode */
2512 if (unlikely((global.mode & MODE_DEBUG) &&
2513 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2514 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2515 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002516
Willy Tarreaua15645d2007-03-18 16:22:39 +01002517 sol = rep->data + msg->som;
2518 eol = sol + msg->sl.rq.l;
2519 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002520
Willy Tarreaua15645d2007-03-18 16:22:39 +01002521 sol += hdr_idx_first_pos(&txn->hdr_idx);
2522 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002523
Willy Tarreaua15645d2007-03-18 16:22:39 +01002524 while (cur_idx) {
2525 eol = sol + txn->hdr_idx.v[cur_idx].len;
2526 debug_hdr("srvhdr", t, sol, eol);
2527 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2528 cur_idx = txn->hdr_idx.v[cur_idx].next;
2529 }
2530 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002531
Willy Tarreaubaaee002006-06-26 02:48:02 +02002532
Willy Tarreau66319382007-04-08 17:17:37 +02002533 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002534 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002535 * full. We cannot loop here since stream_sock_read will disable it only if
2536 * rep->l == rlim-data
2537 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002538 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002539 tv_eternity(&rep->rex);
2540 }
2541
2542
2543 /*
2544 * Now we quickly check if we have found a full valid response.
2545 * If not so, we check the FD and buffer states before leaving.
2546 * A full response is indicated by the fact that we have seen
2547 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2548 * responses are checked first.
2549 *
2550 * Depending on whether the client is still there or not, we
2551 * may send an error response back or not. Note that normally
2552 * we should only check for HTTP status there, and check I/O
2553 * errors somewhere else.
2554 */
2555
2556 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2557
2558 /* Invalid response, or read error or write error */
2559 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2560 (req->flags & BF_WRITE_ERROR) ||
2561 (rep->flags & BF_READ_ERROR))) {
2562 tv_eternity(&rep->rex);
2563 tv_eternity(&req->wex);
2564 fd_delete(t->srv_fd);
2565 if (t->srv) {
2566 t->srv->cur_sess--;
2567 t->srv->failed_resp++;
2568 }
2569 t->be->failed_resp++;
2570 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002571 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002572 client_return(t, error_message(t, HTTP_ERR_502));
2573 if (!(t->flags & SN_ERR_MASK))
2574 t->flags |= SN_ERR_SRVCL;
2575 if (!(t->flags & SN_FINST_MASK))
2576 t->flags |= SN_FINST_H;
2577 /* We used to have a free connection slot. Since we'll never use it,
2578 * we have to inform the server that it may be used by another session.
2579 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002580 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002581 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002582
Willy Tarreaua15645d2007-03-18 16:22:39 +01002583 return 1;
2584 }
2585
2586 /* end of client write or end of server read.
2587 * since we are in header mode, if there's no space left for headers, we
2588 * won't be able to free more later, so the session will never terminate.
2589 */
2590 else if (unlikely(rep->flags & BF_READ_NULL ||
2591 c == CL_STSHUTW || c == CL_STCLOSE ||
2592 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002593 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002594 tv_eternity(&rep->rex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002595 t->srv_state = SV_STSHUTR;
2596 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2597 return 1;
2598 }
2599
2600 /* read timeout : return a 504 to the client.
2601 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002602 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002603 tv_isle(&rep->rex, &now))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002604 tv_eternity(&rep->rex);
2605 tv_eternity(&req->wex);
2606 fd_delete(t->srv_fd);
2607 if (t->srv) {
2608 t->srv->cur_sess--;
2609 t->srv->failed_resp++;
2610 }
2611 t->be->failed_resp++;
2612 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002613 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002614 client_return(t, error_message(t, HTTP_ERR_504));
2615 if (!(t->flags & SN_ERR_MASK))
2616 t->flags |= SN_ERR_SRVTO;
2617 if (!(t->flags & SN_FINST_MASK))
2618 t->flags |= SN_FINST_H;
2619 /* We used to have a free connection slot. Since we'll never use it,
2620 * we have to inform the server that it may be used by another session.
2621 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002622 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002623 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002624 return 1;
2625 }
2626
2627 /* last client read and buffer empty */
2628 /* FIXME!!! here, we don't want to switch to SHUTW if the
2629 * client shuts read too early, because we may still have
2630 * some work to do on the headers.
2631 * The side-effect is that if the client completely closes its
2632 * connection during SV_STHEADER, the connection to the server
2633 * is kept until a response comes back or the timeout is reached.
2634 */
2635 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2636 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002637 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002638 tv_eternity(&req->wex);
2639
2640 /* We must ensure that the read part is still
2641 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002642 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002643 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002644
2645 shutdown(t->srv_fd, SHUT_WR);
2646 t->srv_state = SV_STSHUTW;
2647 return 1;
2648 }
2649
2650 /* write timeout */
2651 /* FIXME!!! here, we don't want to switch to SHUTW if the
2652 * client shuts read too early, because we may still have
2653 * some work to do on the headers.
2654 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002655 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002656 tv_isle(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002657 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002658 tv_eternity(&req->wex);
2659 shutdown(t->srv_fd, SHUT_WR);
2660 /* We must ensure that the read part is still alive
2661 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002662 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002663 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002664
2665 t->srv_state = SV_STSHUTW;
2666 if (!(t->flags & SN_ERR_MASK))
2667 t->flags |= SN_ERR_SRVTO;
2668 if (!(t->flags & SN_FINST_MASK))
2669 t->flags |= SN_FINST_H;
2670 return 1;
2671 }
2672
2673 /*
2674 * And now the non-error cases.
2675 */
2676
2677 /* Data remaining in the request buffer.
2678 * This happens during the first pass here, and during
2679 * long posts.
2680 */
2681 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002682 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2683 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002684 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002685 /* FIXME: to prevent the server from expiring read timeouts during writes,
2686 * we refresh it. */
2687 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002688 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002689 else
2690 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002691 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002692 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002693
Willy Tarreaua15645d2007-03-18 16:22:39 +01002694 /* nothing left in the request buffer */
2695 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002696 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2697 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002698 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002699 }
2700 }
2701
2702 return t->srv_state != SV_STHEADERS;
2703 }
2704
2705
2706 /*****************************************************************
2707 * More interesting part now : we know that we have a complete *
2708 * response which at least looks like HTTP. We have an indicator *
2709 * of each header's length, so we can parse them quickly. *
2710 ****************************************************************/
2711
Willy Tarreau9cdde232007-05-02 20:58:19 +02002712 /* ensure we keep this pointer to the beginning of the message */
2713 msg->sol = rep->data + msg->som;
2714
Willy Tarreaua15645d2007-03-18 16:22:39 +01002715 /*
2716 * 1: get the status code and check for cacheability.
2717 */
2718
2719 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002720 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002721
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002722 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002723 case 200:
2724 case 203:
2725 case 206:
2726 case 300:
2727 case 301:
2728 case 410:
2729 /* RFC2616 @13.4:
2730 * "A response received with a status code of
2731 * 200, 203, 206, 300, 301 or 410 MAY be stored
2732 * by a cache (...) unless a cache-control
2733 * directive prohibits caching."
2734 *
2735 * RFC2616 @9.5: POST method :
2736 * "Responses to this method are not cacheable,
2737 * unless the response includes appropriate
2738 * Cache-Control or Expires header fields."
2739 */
2740 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002741 unlikely(t->be->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002742 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002743 break;
2744 default:
2745 break;
2746 }
2747
2748 /*
2749 * 2: we may need to capture headers
2750 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002751 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002752 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002753 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002754
2755 /*
2756 * 3: we will have to evaluate the filters.
2757 * As opposed to version 1.2, now they will be evaluated in the
2758 * filters order and not in the header order. This means that
2759 * each filter has to be validated among all headers.
2760 *
2761 * Filters are tried with ->be first, then with ->fe if it is
2762 * different from ->be.
2763 */
2764
2765 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2766
2767 cur_proxy = t->be;
2768 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002769 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002770
2771 /* try headers filters */
2772 if (rule_set->rsp_exp != NULL) {
2773 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2774 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002775 if (t->srv) {
2776 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002777 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002778 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002779 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002780 return_srv_prx_502:
2781 tv_eternity(&rep->rex);
2782 tv_eternity(&req->wex);
2783 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002784 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002785 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002786 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002787 if (!(t->flags & SN_ERR_MASK))
2788 t->flags |= SN_ERR_PRXCOND;
2789 if (!(t->flags & SN_FINST_MASK))
2790 t->flags |= SN_FINST_H;
2791 /* We used to have a free connection slot. Since we'll never use it,
2792 * we have to inform the server that it may be used by another session.
2793 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002794 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002795 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002796 return 1;
2797 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002798 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002799
Willy Tarreaua15645d2007-03-18 16:22:39 +01002800 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002801 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002802 if (t->srv) {
2803 t->srv->cur_sess--;
2804 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002805 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002806 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002807 goto return_srv_prx_502;
2808 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002809
Willy Tarreaua15645d2007-03-18 16:22:39 +01002810 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002811 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002812 !(t->flags & SN_CONN_CLOSED)) {
2813 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002814 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002815 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002816
Willy Tarreaua15645d2007-03-18 16:22:39 +01002817 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2818 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002819
Willy Tarreaua15645d2007-03-18 16:22:39 +01002820 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2821 cur_hdr = &txn->hdr_idx.v[cur_idx];
2822 cur_ptr = cur_next;
2823 cur_end = cur_ptr + cur_hdr->len;
2824 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002825
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002826 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2827 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002828 /* 3 possibilities :
2829 * - we have already set Connection: close,
2830 * so we remove this line.
2831 * - we have not yet set Connection: close,
2832 * but this line indicates close. We leave
2833 * it untouched and set the flag.
2834 * - we have not yet set Connection: close,
2835 * and this line indicates non-close. We
2836 * replace it.
2837 */
2838 if (t->flags & SN_CONN_CLOSED) {
2839 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2840 txn->rsp.eoh += delta;
2841 cur_next += delta;
2842 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2843 txn->hdr_idx.used--;
2844 cur_hdr->len = 0;
2845 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002846 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2847 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2848 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002849 cur_next += delta;
2850 cur_hdr->len += delta;
2851 txn->rsp.eoh += delta;
2852 }
2853 t->flags |= SN_CONN_CLOSED;
2854 }
2855 }
2856 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002857 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002858 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002859
Willy Tarreaua15645d2007-03-18 16:22:39 +01002860 /* add response headers from the rule sets in the same order */
2861 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002862 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2863 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002864 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002865 }
2866
Willy Tarreaua15645d2007-03-18 16:22:39 +01002867 /* check whether we're already working on the frontend */
2868 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002869 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002870 cur_proxy = t->fe;
2871 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002872
Willy Tarreaua15645d2007-03-18 16:22:39 +01002873 /*
2874 * 4: check for server cookie.
2875 */
2876 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002877
Willy Tarreaua15645d2007-03-18 16:22:39 +01002878 /*
2879 * 5: add server cookie in the response if needed
2880 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002881 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2882 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002883 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002884
Willy Tarreaua15645d2007-03-18 16:22:39 +01002885 /* the server is known, it's not the one the client requested, we have to
2886 * insert a set-cookie here, except if we want to insert only on POST
2887 * requests and this one isn't. Note that servers which don't have cookies
2888 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002889 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002890 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002891 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01002892 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002893
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002894 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2895 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002896 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002897 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002898
Willy Tarreaua15645d2007-03-18 16:22:39 +01002899 /* Here, we will tell an eventual cache on the client side that we don't
2900 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2901 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2902 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2903 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002904 if (t->be->options & PR_O_COOK_NOC) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002905 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2906 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002907 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002908 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002909 }
2910
2911
2912 /*
2913 * 6: check for cache-control or pragma headers.
2914 */
2915 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002916
Willy Tarreaubaaee002006-06-26 02:48:02 +02002917
Willy Tarreaua15645d2007-03-18 16:22:39 +01002918 /*
2919 * 7: check if result will be cacheable with a cookie.
2920 * We'll block the response if security checks have caught
2921 * nasty things such as a cacheable cookie.
2922 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002923 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2924 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002925 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002926
Willy Tarreaua15645d2007-03-18 16:22:39 +01002927 /* we're in presence of a cacheable response containing
2928 * a set-cookie header. We'll block it as requested by
2929 * the 'checkcache' option, and send an alert.
2930 */
2931 if (t->srv) {
2932 t->srv->cur_sess--;
2933 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002934 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002935 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002936
Willy Tarreaua15645d2007-03-18 16:22:39 +01002937 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002938 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002939 send_log(t->be, LOG_ALERT,
2940 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002941 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002942 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002943 }
2944
Willy Tarreaua15645d2007-03-18 16:22:39 +01002945 /*
2946 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002947 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002948 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002949 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002950 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002951 if ((unlikely(msg->sl.st.v_l != 8) ||
2952 unlikely(req->data[msg->som + 7] != '0')) &&
2953 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002954 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002955 goto return_bad_resp;
2956 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002957 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002958
Willy Tarreaubaaee002006-06-26 02:48:02 +02002959
Willy Tarreaua15645d2007-03-18 16:22:39 +01002960 /*************************************************************
2961 * OK, that's finished for the headers. We have done what we *
2962 * could. Let's switch to the DATA state. *
2963 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002964
Willy Tarreaua15645d2007-03-18 16:22:39 +01002965 t->srv_state = SV_STDATA;
2966 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002967 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002968
2969 /* client connection already closed or option 'forceclose' required :
2970 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002971 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002972 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002973 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002974 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002975 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002976
2977 /* We must ensure that the read part is still alive when switching
2978 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002979 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002980 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002981
Willy Tarreaua15645d2007-03-18 16:22:39 +01002982 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002983 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002984 }
2985
Willy Tarreaua15645d2007-03-18 16:22:39 +01002986#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002987 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002988 /* TCP splicing supported by both FE and BE */
2989 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002990 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002991#endif
2992 /* if the user wants to log as soon as possible, without counting
2993 bytes from the server, then this is the right moment. */
2994 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2995 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002996 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02002997 if (t->fe->to_log & LW_REQ)
2998 http_sess_log(t);
2999 else
3000 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003001 }
3002
Willy Tarreaua15645d2007-03-18 16:22:39 +01003003 /* Note: we must not try to cheat by jumping directly to DATA,
3004 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003005 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003006
3007 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003008 }
3009 else if (s == SV_STDATA) {
3010 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003011 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003012 tv_eternity(&rep->rex);
3013 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003014 fd_delete(t->srv_fd);
3015 if (t->srv) {
3016 t->srv->cur_sess--;
3017 t->srv->failed_resp++;
3018 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003019 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003020 t->srv_state = SV_STCLOSE;
3021 if (!(t->flags & SN_ERR_MASK))
3022 t->flags |= SN_ERR_SRVCL;
3023 if (!(t->flags & SN_FINST_MASK))
3024 t->flags |= SN_FINST_D;
3025 /* We used to have a free connection slot. Since we'll never use it,
3026 * we have to inform the server that it may be used by another session.
3027 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003028 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003029 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003030
3031 return 1;
3032 }
3033 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003034 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003035 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003036 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003037 t->srv_state = SV_STSHUTR;
3038 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3039 return 1;
3040 }
3041 /* end of client read and no more data to send */
3042 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003043 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003044 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003045 shutdown(t->srv_fd, SHUT_WR);
3046 /* We must ensure that the read part is still alive when switching
3047 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003048 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003049 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003050
3051 t->srv_state = SV_STSHUTW;
3052 return 1;
3053 }
3054 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003055 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003056 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003057 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003058 t->srv_state = SV_STSHUTR;
3059 if (!(t->flags & SN_ERR_MASK))
3060 t->flags |= SN_ERR_SRVTO;
3061 if (!(t->flags & SN_FINST_MASK))
3062 t->flags |= SN_FINST_D;
3063 return 1;
3064 }
3065 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003066 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003067 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003068 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003069 shutdown(t->srv_fd, SHUT_WR);
3070 /* We must ensure that the read part is still alive when switching
3071 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003072 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003073 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003074 t->srv_state = SV_STSHUTW;
3075 if (!(t->flags & SN_ERR_MASK))
3076 t->flags |= SN_ERR_SRVTO;
3077 if (!(t->flags & SN_FINST_MASK))
3078 t->flags |= SN_FINST_D;
3079 return 1;
3080 }
3081
3082 /* recompute request time-outs */
3083 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003084 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3085 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003086 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003087 }
3088 }
3089 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003090 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3091 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003092 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003093 /* FIXME: to prevent the server from expiring read timeouts during writes,
3094 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003095 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003096 }
3097 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003098 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003099 }
3100 }
3101
3102 /* recompute response time-outs */
3103 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003104 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003105 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003106 }
3107 }
3108 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003109 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003110 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003111 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003112 }
3113 }
3114
3115 return 0; /* other cases change nothing */
3116 }
3117 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003118 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003119 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003120 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003121 fd_delete(t->srv_fd);
3122 if (t->srv) {
3123 t->srv->cur_sess--;
3124 t->srv->failed_resp++;
3125 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003126 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003127 //close(t->srv_fd);
3128 t->srv_state = SV_STCLOSE;
3129 if (!(t->flags & SN_ERR_MASK))
3130 t->flags |= SN_ERR_SRVCL;
3131 if (!(t->flags & SN_FINST_MASK))
3132 t->flags |= SN_FINST_D;
3133 /* We used to have a free connection slot. Since we'll never use it,
3134 * we have to inform the server that it may be used by another session.
3135 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003136 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003137 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003138
3139 return 1;
3140 }
3141 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003142 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003143 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003144 fd_delete(t->srv_fd);
3145 if (t->srv)
3146 t->srv->cur_sess--;
3147 //close(t->srv_fd);
3148 t->srv_state = SV_STCLOSE;
3149 /* We used to have a free connection slot. Since we'll never use it,
3150 * we have to inform the server that it may be used by another session.
3151 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003152 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003153 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003154
3155 return 1;
3156 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003157 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003158 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003159 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003160 fd_delete(t->srv_fd);
3161 if (t->srv)
3162 t->srv->cur_sess--;
3163 //close(t->srv_fd);
3164 t->srv_state = SV_STCLOSE;
3165 if (!(t->flags & SN_ERR_MASK))
3166 t->flags |= SN_ERR_SRVTO;
3167 if (!(t->flags & SN_FINST_MASK))
3168 t->flags |= SN_FINST_D;
3169 /* We used to have a free connection slot. Since we'll never use it,
3170 * we have to inform the server that it may be used by another session.
3171 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003172 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003173 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003174
3175 return 1;
3176 }
3177 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003178 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3179 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003180 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003181 }
3182 }
3183 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003184 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3185 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003186 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003187 /* FIXME: to prevent the server from expiring read timeouts during writes,
3188 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003189 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003190 }
3191 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003192 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003193 }
3194 }
3195 return 0;
3196 }
3197 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003198 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003199 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003200 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003201 fd_delete(t->srv_fd);
3202 if (t->srv) {
3203 t->srv->cur_sess--;
3204 t->srv->failed_resp++;
3205 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003206 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003207 //close(t->srv_fd);
3208 t->srv_state = SV_STCLOSE;
3209 if (!(t->flags & SN_ERR_MASK))
3210 t->flags |= SN_ERR_SRVCL;
3211 if (!(t->flags & SN_FINST_MASK))
3212 t->flags |= SN_FINST_D;
3213 /* We used to have a free connection slot. Since we'll never use it,
3214 * we have to inform the server that it may be used by another session.
3215 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003216 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003217 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003218
3219 return 1;
3220 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003221 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003222 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003223 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003224 fd_delete(t->srv_fd);
3225 if (t->srv)
3226 t->srv->cur_sess--;
3227 //close(t->srv_fd);
3228 t->srv_state = SV_STCLOSE;
3229 /* We used to have a free connection slot. Since we'll never use it,
3230 * we have to inform the server that it may be used by another session.
3231 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003232 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003233 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234
3235 return 1;
3236 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003237 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003238 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003239 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003240 fd_delete(t->srv_fd);
3241 if (t->srv)
3242 t->srv->cur_sess--;
3243 //close(t->srv_fd);
3244 t->srv_state = SV_STCLOSE;
3245 if (!(t->flags & SN_ERR_MASK))
3246 t->flags |= SN_ERR_SRVTO;
3247 if (!(t->flags & SN_FINST_MASK))
3248 t->flags |= SN_FINST_D;
3249 /* We used to have a free connection slot. Since we'll never use it,
3250 * we have to inform the server that it may be used by another session.
3251 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003252 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003253 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003254
3255 return 1;
3256 }
3257 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003258 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003259 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003260 }
3261 }
3262 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003263 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003264 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003265 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003266 }
3267 }
3268 return 0;
3269 }
3270 else { /* SV_STCLOSE : nothing to do */
3271 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3272 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003273 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003274 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003275 write(1, trash, len);
3276 }
3277 return 0;
3278 }
3279 return 0;
3280}
3281
3282
3283/*
3284 * Produces data for the session <s> depending on its source. Expects to be
3285 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3286 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3287 * session, which it uses to keep on being called when there is free space in
3288 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3289 * if it changes the session state from CL_STSHUTR, otherwise 0.
3290 */
3291int produce_content(struct session *s)
3292{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003293 if (s->data_source == DATA_SRC_NONE) {
3294 s->flags &= ~SN_SELF_GEN;
3295 return 1;
3296 }
3297 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003298 /* dump server statistics */
3299 return produce_content_stats(s);
3300 }
3301 else {
3302 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003303 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003304 client_retnclose(s, error_message(s, HTTP_ERR_500));
3305 if (!(s->flags & SN_ERR_MASK))
3306 s->flags |= SN_ERR_PRXCOND;
3307 if (!(s->flags & SN_FINST_MASK))
3308 s->flags |= SN_FINST_R;
3309 s->flags &= ~SN_SELF_GEN;
3310 return 1;
3311 }
3312}
3313
3314
3315/*
3316 * Produces statistics data for the session <s>. Expects to be called with
3317 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3318 * flag from the session, which it uses to keep on being called when there is
3319 * free space in the buffer, of simply by letting an empty buffer upon return.
3320 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3321 */
3322int produce_content_stats(struct session *s)
3323{
3324 struct buffer *rep = s->rep;
3325 struct proxy *px;
3326 struct chunk msg;
3327 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003328
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003329 msg.len = 0;
3330 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003331
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003332 switch (s->data_state) {
3333 case DATA_ST_INIT:
3334 /* the function had not been called yet */
3335 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003336
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003337 chunk_printf(&msg, sizeof(trash),
3338 "HTTP/1.0 200 OK\r\n"
3339 "Cache-Control: no-cache\r\n"
3340 "Connection: close\r\n"
3341 "Content-Type: text/html\r\n"
3342 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003343
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003344 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003345 client_retnclose(s, &msg); // send the start of the response.
3346 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003347
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003348 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3349 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3350 if (!(s->flags & SN_FINST_MASK))
3351 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003352
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003353 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003354 /* that's all we return in case of HEAD request */
3355 s->data_state = DATA_ST_FIN;
3356 s->flags &= ~SN_SELF_GEN;
3357 return 1;
3358 }
3359
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003360 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3361 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003362
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003363 case DATA_ST_HEAD:
3364 /* WARNING! This must fit in the first buffer !!! */
3365 chunk_printf(&msg, sizeof(trash),
3366 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3367 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3368 "<style type=\"text/css\"><!--\n"
3369 "body {"
3370 " font-family: helvetica, arial;"
3371 " font-size: 12px;"
3372 " font-weight: normal;"
3373 " color: black;"
3374 " background: white;"
3375 "}\n"
3376 "th,td {"
3377 " font-size: 0.8em;"
3378 " align: center;"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003379 "}\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003380 "h1 {"
3381 " font-size: xx-large;"
3382 " margin-bottom: 0.5em;"
3383 "}\n"
3384 "h2 {"
3385 " font-family: helvetica, arial;"
3386 " font-size: x-large;"
3387 " font-weight: bold;"
3388 " font-style: italic;"
3389 " color: #6020a0;"
3390 " margin-top: 0em;"
3391 " margin-bottom: 0em;"
3392 "}\n"
3393 "h3 {"
3394 " font-family: helvetica, arial;"
3395 " font-size: 16px;"
3396 " font-weight: bold;"
3397 " color: #b00040;"
3398 " background: #e8e8d0;"
3399 " margin-top: 0em;"
3400 " margin-bottom: 0em;"
3401 "}\n"
3402 "li {"
3403 " margin-top: 0.25em;"
3404 " margin-right: 2em;"
3405 "}\n"
3406 ".hr {margin-top: 0.25em;"
3407 " border-color: black;"
3408 " border-bottom-style: solid;"
3409 "}\n"
3410 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3411 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3412 ".total {background: #20D0D0;color: #ffff80;}\n"
3413 ".frontend {background: #e8e8d0;}\n"
3414 ".backend {background: #e8e8d0;}\n"
3415 ".active0 {background: #ff9090;}\n"
3416 ".active1 {background: #ffd020;}\n"
3417 ".active2 {background: #ffffa0;}\n"
3418 ".active3 {background: #c0ffc0;}\n"
3419 ".active4 {background: #e0e0e0;}\n"
3420 ".backup0 {background: #ff9090;}\n"
3421 ".backup1 {background: #ff80ff;}\n"
3422 ".backup2 {background: #c060ff;}\n"
3423 ".backup3 {background: #b0d0ff;}\n"
3424 ".backup4 {background: #e0e0e0;}\n"
3425 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003426 "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 +01003427 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3428 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3429 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3430 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3431 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003432 "-->\n"
3433 "</style></head>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003434
3435 if (buffer_write_chunk(rep, &msg) != 0)
3436 return 0;
3437
3438 s->data_state = DATA_ST_INFO;
3439 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003440
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003441 case DATA_ST_INFO:
3442 up = (now.tv_sec - start_date.tv_sec);
3443
3444 /* WARNING! this has to fit the first packet too.
3445 * We are around 3.5 kB, add adding entries will
3446 * become tricky if we want to support 4kB buffers !
3447 */
3448 chunk_printf(&msg, sizeof(trash),
3449 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3450 PRODUCT_NAME "</a></h1>\n"
3451 "<h2>Statistics Report for pid %d</h2>\n"
3452 "<hr width=\"100%%\" class=\"hr\">\n"
3453 "<h3>&gt; General process information</h3>\n"
3454 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3455 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3456 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3457 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3458 "<b>maxsock = </b> %d<br>\n"
3459 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3460 "</td><td align=\"center\" nowrap>\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003461 "<table class=\"lgd\"><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003462 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3463 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003464 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003465 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3466 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003467 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003468 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3469 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003470 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003471 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3472 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3473 "</tr></table>\n"
3474 "</td>"
3475 "<td align=\"left\" nowrap width=\"1%%\">"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003476 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
3477 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3478 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3479 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003480 "</ul>"
3481 "</td>"
3482 "</tr></table>\n"
3483 "",
3484 pid, pid, global.nbproc,
3485 up / 86400, (up % 86400) / 3600,
3486 (up % 3600) / 60, (up % 60),
3487 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3488 global.rlimit_memmax ? " MB" : "",
3489 global.rlimit_nofile,
3490 global.maxsock,
3491 global.maxconn,
3492 actconn
3493 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003494
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003495 if (buffer_write_chunk(rep, &msg) != 0)
3496 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003497
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003498 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003499
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003500 s->data_ctx.stats.px = proxy;
3501 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3502 s->data_state = DATA_ST_LIST;
3503 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003504
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003505 case DATA_ST_LIST:
3506 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003507 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003508 px = s->data_ctx.stats.px;
3509 /* skip the disabled proxies and non-networked ones */
3510 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3511 if (produce_content_stats_proxy(s, px) == 0)
3512 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003513
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003514 s->data_ctx.stats.px = px->next;
3515 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3516 }
3517 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003518
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003519 s->data_state = DATA_ST_END;
3520 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003521
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003522 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003523 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003524 if (buffer_write_chunk(rep, &msg) != 0)
3525 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003526
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003527 s->data_state = DATA_ST_FIN;
3528 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003529
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003530 case DATA_ST_FIN:
3531 s->flags &= ~SN_SELF_GEN;
3532 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003533
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003534 default:
3535 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003536 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003537 client_retnclose(s, error_message(s, HTTP_ERR_500));
3538 if (!(s->flags & SN_ERR_MASK))
3539 s->flags |= SN_ERR_PRXCOND;
3540 if (!(s->flags & SN_FINST_MASK))
3541 s->flags |= SN_FINST_R;
3542 s->flags &= ~SN_SELF_GEN;
3543 return 1;
3544 }
3545}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003546
Willy Tarreaubaaee002006-06-26 02:48:02 +02003547
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003548/*
3549 * Dumps statistics for a proxy.
3550 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3551 * ot non-zero if everything completed.
3552 */
3553int produce_content_stats_proxy(struct session *s, struct proxy *px)
3554{
3555 struct buffer *rep = s->rep;
3556 struct server *sv;
3557 struct chunk msg;
3558
3559 msg.len = 0;
3560 msg.str = trash;
3561
3562 switch (s->data_ctx.stats.px_st) {
3563 case DATA_ST_PX_INIT:
3564 /* we are on a new proxy */
3565
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003566 if (s->be->uri_auth && s->be->uri_auth->scope) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003567 /* we have a limited scope, we have to check the proxy name */
3568 struct stat_scope *scope;
3569 int len;
3570
3571 len = strlen(px->id);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003572 scope = s->be->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003573
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003574 while (scope) {
3575 /* match exact proxy name */
3576 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3577 break;
3578
3579 /* match '.' which means 'self' proxy */
3580 if (!strcmp(scope->px_id, ".") && px == s->fe)
3581 break;
3582 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003583 }
3584
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003585 /* proxy name not found : don't dump anything */
3586 if (scope == NULL)
3587 return 1;
3588 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003589
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003590 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3591 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003592
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003593 case DATA_ST_PX_TH:
3594 /* print a new table */
3595 chunk_printf(&msg, sizeof(trash),
3596 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3597 "<tr align=\"center\" class=\"titre\">"
3598 "<th colspan=2 class=\"pxname\">%s</th>"
3599 "<th colspan=18 class=\"empty\"></th>"
3600 "</tr>\n"
3601 "<tr align=\"center\" class=\"titre\">"
3602 "<th rowspan=2></th>"
3603 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3604 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3605 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3606 "</tr>\n"
3607 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003608 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3609 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003610 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3611 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3612 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3613 "",
3614 px->id);
3615
3616 if (buffer_write_chunk(rep, &msg) != 0)
3617 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003618
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003619 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3620 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003621
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003622 case DATA_ST_PX_FE:
3623 /* print the frontend */
3624 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003625 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003626 /* name, queue */
3627 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3628 /* sessions : current, max, limit, cumul. */
3629 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3630 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003631 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003632 /* denied: req, resp */
3633 "<td align=right>%d</td><td align=right>%d</td>"
3634 /* errors : request, connect, response */
3635 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3636 /* server status : reflect backend status */
3637 "<td align=center>%s</td>"
3638 /* rest of server: nothing */
3639 "<td align=center colspan=5></td></tr>"
3640 "",
3641 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003642 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003643 px->denied_req, px->denied_resp,
3644 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003645 px->state == PR_STRUN ? "OPEN" :
3646 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003647
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003648 if (buffer_write_chunk(rep, &msg) != 0)
3649 return 0;
3650 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003651
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003652 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3653 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3654 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003655
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003656 case DATA_ST_PX_SV:
3657 /* stats.sv has been initialized above */
3658 while (s->data_ctx.stats.sv != NULL) {
3659 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3660 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003661
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003662 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003663
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003664 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3665 if (!(sv->state & SRV_CHECKED))
3666 sv_state = 4;
3667 else if (sv->state & SRV_RUNNING)
3668 if (sv->health == sv->rise + sv->fall - 1)
3669 sv_state = 3; /* UP */
3670 else
3671 sv_state = 2; /* going down */
3672 else
3673 if (sv->health)
3674 sv_state = 1; /* going up */
3675 else
3676 sv_state = 0; /* DOWN */
3677
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003678 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003679 /* name */
3680 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3681 /* queue : current, max */
3682 "<td align=right>%d</td><td align=right>%d</td>"
3683 /* sessions : current, max, limit, cumul */
3684 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3685 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003686 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003687 /* denied: req, resp */
3688 "<td align=right></td><td align=right>%d</td>"
3689 /* errors : request, connect, response */
3690 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3691 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003692 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003693 sv_state, sv->id,
3694 sv->nbpend, sv->nbpend_max,
3695 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003696 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003697 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003698 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003699
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003700 /* status */
3701 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3702 chunk_printf(&msg, sizeof(trash),
3703 srv_hlt_st[sv_state],
3704 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3705 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3706
Willy Tarreau128e9542007-01-01 22:01:43 +01003707 chunk_printf(&msg, sizeof(trash),
3708 /* weight */
3709 "</td><td>%d</td>"
3710 /* act, bck */
3711 "<td>%s</td><td>%s</td>"
3712 "",
Willy Tarreau417fae02007-03-25 21:16:40 +02003713 sv->uweight,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003714 (sv->state & SRV_BACKUP) ? "-" : "Y",
3715 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003716
3717 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003718 if (sv->state & SRV_CHECKED)
3719 chunk_printf(&msg, sizeof(trash),
3720 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3721 sv->failed_checks, sv->down_trans);
3722 else
3723 chunk_printf(&msg, sizeof(trash),
3724 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003725
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003726 if (buffer_write_chunk(rep, &msg) != 0)
3727 return 0;
3728
3729 s->data_ctx.stats.sv = sv->next;
3730 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003731
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003732 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3733 /* fall through */
3734
3735 case DATA_ST_PX_BE:
3736 /* print the backend */
3737 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003738 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003739 /* name */
3740 "<tr align=center class=\"backend\"><td>Backend</td>"
3741 /* queue : current, max */
3742 "<td align=right>%d</td><td align=right>%d</td>"
3743 /* sessions : current, max, limit, cumul. */
3744 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3745 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003746 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003747 /* denied: req, resp */
3748 "<td align=right>%d</td><td align=right>%d</td>"
3749 /* errors : request, connect, response */
3750 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3751 /* server status : reflect backend status (up/down) : we display UP
3752 * if the backend has known working servers or if it has no server at
3753 * all (eg: for stats). Tthen we display the total weight, number of
3754 * active and backups. */
3755 "<td align=center>%s</td><td align=center>%d</td>"
3756 "<td align=center>%d</td><td align=center>%d</td>"
3757 /* rest of server: nothing */
3758 "<td align=center colspan=2></td></tr>"
3759 "",
3760 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3761 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003762 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003763 px->denied_req, px->denied_resp,
3764 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003765 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3766 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003767
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003768 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003769 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003770 }
3771
3772 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3773 /* fall through */
3774
3775 case DATA_ST_PX_END:
3776 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3777
3778 if (buffer_write_chunk(rep, &msg) != 0)
3779 return 0;
3780
3781 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3782 /* fall through */
3783
3784 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003785 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003786
3787 default:
3788 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003789 return 1;
3790 }
3791}
3792
3793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003794/* Iterate the same filter through all request headers.
3795 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003796 * Since it can manage the switch to another backend, it updates the per-proxy
3797 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003798 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003799int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003800{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003801 char term;
3802 char *cur_ptr, *cur_end, *cur_next;
3803 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003804 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003805 struct hdr_idx_elem *cur_hdr;
3806 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003807
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003808 last_hdr = 0;
3809
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003810 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003811 old_idx = 0;
3812
3813 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003814 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003815 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003816 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003817 (exp->action == ACT_ALLOW ||
3818 exp->action == ACT_DENY ||
3819 exp->action == ACT_TARPIT))
3820 return 0;
3821
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003822 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003823 if (!cur_idx)
3824 break;
3825
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003826 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003827 cur_ptr = cur_next;
3828 cur_end = cur_ptr + cur_hdr->len;
3829 cur_next = cur_end + cur_hdr->cr + 1;
3830
3831 /* Now we have one header between cur_ptr and cur_end,
3832 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003833 */
3834
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003835 /* The annoying part is that pattern matching needs
3836 * that we modify the contents to null-terminate all
3837 * strings before testing them.
3838 */
3839
3840 term = *cur_end;
3841 *cur_end = '\0';
3842
3843 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3844 switch (exp->action) {
3845 case ACT_SETBE:
3846 /* It is not possible to jump a second time.
3847 * FIXME: should we return an HTTP/500 here so that
3848 * the admin knows there's a problem ?
3849 */
3850 if (t->be != t->fe)
3851 break;
3852
3853 /* Swithing Proxy */
3854 t->be = (struct proxy *) exp->replace;
3855
3856 /* right now, the backend switch is not too much complicated
3857 * because we have associated req_cap and rsp_cap to the
3858 * frontend, and the beconn will be updated later.
3859 */
3860
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003861 t->rep->rto = t->req->wto = t->be->srvtimeout;
3862 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003863 last_hdr = 1;
3864 break;
3865
3866 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003867 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003868 last_hdr = 1;
3869 break;
3870
3871 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003872 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003873 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003874 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003875 break;
3876
3877 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003878 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003879 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003880 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003881 break;
3882
3883 case ACT_REPLACE:
3884 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3885 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3886 /* FIXME: if the user adds a newline in the replacement, the
3887 * index will not be recalculated for now, and the new line
3888 * will not be counted as a new header.
3889 */
3890
3891 cur_end += delta;
3892 cur_next += delta;
3893 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003894 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003895 break;
3896
3897 case ACT_REMOVE:
3898 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3899 cur_next += delta;
3900
3901 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003902 txn->req.eoh += delta;
3903 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3904 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003905 cur_hdr->len = 0;
3906 cur_end = NULL; /* null-term has been rewritten */
3907 break;
3908
3909 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003910 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003911 if (cur_end)
3912 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003913
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003914 /* keep the link from this header to next one in case of later
3915 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003916 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003917 old_idx = cur_idx;
3918 }
3919 return 0;
3920}
3921
3922
3923/* Apply the filter to the request line.
3924 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3925 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003926 * Since it can manage the switch to another backend, it updates the per-proxy
3927 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003928 */
3929int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3930{
3931 char term;
3932 char *cur_ptr, *cur_end;
3933 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003934 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003935 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003936
Willy Tarreau58f10d72006-12-04 02:26:12 +01003937
Willy Tarreau3d300592007-03-18 18:34:41 +01003938 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003939 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003940 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003941 (exp->action == ACT_ALLOW ||
3942 exp->action == ACT_DENY ||
3943 exp->action == ACT_TARPIT))
3944 return 0;
3945 else if (exp->action == ACT_REMOVE)
3946 return 0;
3947
3948 done = 0;
3949
Willy Tarreau9cdde232007-05-02 20:58:19 +02003950 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003951 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003952
3953 /* Now we have the request line between cur_ptr and cur_end */
3954
3955 /* The annoying part is that pattern matching needs
3956 * that we modify the contents to null-terminate all
3957 * strings before testing them.
3958 */
3959
3960 term = *cur_end;
3961 *cur_end = '\0';
3962
3963 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3964 switch (exp->action) {
3965 case ACT_SETBE:
3966 /* It is not possible to jump a second time.
3967 * FIXME: should we return an HTTP/500 here so that
3968 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003969 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003970 if (t->be != t->fe)
3971 break;
3972
3973 /* Swithing Proxy */
3974 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003975
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003976 /* right now, the backend switch is not too much complicated
3977 * because we have associated req_cap and rsp_cap to the
3978 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003979 */
3980
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003981 t->rep->rto = t->req->wto = t->be->srvtimeout;
3982 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003983 done = 1;
3984 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003985
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003986 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003987 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003988 done = 1;
3989 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003990
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003991 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003992 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003993 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003994 done = 1;
3995 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003996
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003997 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003998 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003999 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004000 done = 1;
4001 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004002
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004003 case ACT_REPLACE:
4004 *cur_end = term; /* restore the string terminator */
4005 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4006 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4007 /* FIXME: if the user adds a newline in the replacement, the
4008 * index will not be recalculated for now, and the new line
4009 * will not be counted as a new header.
4010 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004011
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004012 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004013 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004014
Willy Tarreau9cdde232007-05-02 20:58:19 +02004015 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004016 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004017 HTTP_MSG_RQMETH,
4018 cur_ptr, cur_end + 1,
4019 NULL, NULL);
4020 if (unlikely(!cur_end))
4021 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004022
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004023 /* we have a full request and we know that we have either a CR
4024 * or an LF at <ptr>.
4025 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004026 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4027 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004028 /* there is no point trying this regex on headers */
4029 return 1;
4030 }
4031 }
4032 *cur_end = term; /* restore the string terminator */
4033 return done;
4034}
Willy Tarreau97de6242006-12-27 17:18:38 +01004035
Willy Tarreau58f10d72006-12-04 02:26:12 +01004036
Willy Tarreau58f10d72006-12-04 02:26:12 +01004037
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004038/*
4039 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4040 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004041 * unparsable request. Since it can manage the switch to another backend, it
4042 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004043 */
4044int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4045{
Willy Tarreau3d300592007-03-18 18:34:41 +01004046 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004047 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004048 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004049 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004050
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004051 /*
4052 * The interleaving of transformations and verdicts
4053 * makes it difficult to decide to continue or stop
4054 * the evaluation.
4055 */
4056
Willy Tarreau3d300592007-03-18 18:34:41 +01004057 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004058 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4059 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4060 exp = exp->next;
4061 continue;
4062 }
4063
4064 /* Apply the filter to the request line. */
4065 ret = apply_filter_to_req_line(t, req, exp);
4066 if (unlikely(ret < 0))
4067 return -1;
4068
4069 if (likely(ret == 0)) {
4070 /* The filter did not match the request, it can be
4071 * iterated through all headers.
4072 */
4073 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004074 }
4075 exp = exp->next;
4076 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004077 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004078}
4079
4080
Willy Tarreaua15645d2007-03-18 16:22:39 +01004081
Willy Tarreau58f10d72006-12-04 02:26:12 +01004082/*
4083 * Manager client-side cookie
4084 */
4085void manage_client_side_cookies(struct session *t, struct buffer *req)
4086{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004087 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004088 char *p1, *p2, *p3, *p4;
4089 char *del_colon, *del_cookie, *colon;
4090 int app_cookies;
4091
4092 appsess *asession_temp = NULL;
4093 appsess local_asession;
4094
4095 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004096 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004097
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004098 if (t->be->cookie_name == NULL &&
4099 t->be->appsession_name == NULL &&
4100 t->be->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004101 return;
4102
Willy Tarreau2a324282006-12-05 00:05:46 +01004103 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004104 * we start with the start line.
4105 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004106 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004107 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004108
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004109 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004110 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004111 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004112
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004113 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004114 cur_ptr = cur_next;
4115 cur_end = cur_ptr + cur_hdr->len;
4116 cur_next = cur_end + cur_hdr->cr + 1;
4117
4118 /* We have one full header between cur_ptr and cur_end, and the
4119 * next header starts at cur_next. We're only interested in
4120 * "Cookie:" headers.
4121 */
4122
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004123 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4124 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004125 old_idx = cur_idx;
4126 continue;
4127 }
4128
4129 /* Now look for cookies. Conforming to RFC2109, we have to support
4130 * attributes whose name begin with a '$', and associate them with
4131 * the right cookie, if we want to delete this cookie.
4132 * So there are 3 cases for each cookie read :
4133 * 1) it's a special attribute, beginning with a '$' : ignore it.
4134 * 2) it's a server id cookie that we *MAY* want to delete : save
4135 * some pointers on it (last semi-colon, beginning of cookie...)
4136 * 3) it's an application cookie : we *MAY* have to delete a previous
4137 * "special" cookie.
4138 * At the end of loop, if a "special" cookie remains, we may have to
4139 * remove it. If no application cookie persists in the header, we
4140 * *MUST* delete it
4141 */
4142
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004143 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004144
Willy Tarreau58f10d72006-12-04 02:26:12 +01004145 /* del_cookie == NULL => nothing to be deleted */
4146 del_colon = del_cookie = NULL;
4147 app_cookies = 0;
4148
4149 while (p1 < cur_end) {
4150 /* skip spaces and colons, but keep an eye on these ones */
4151 while (p1 < cur_end) {
4152 if (*p1 == ';' || *p1 == ',')
4153 colon = p1;
4154 else if (!isspace((int)*p1))
4155 break;
4156 p1++;
4157 }
4158
4159 if (p1 == cur_end)
4160 break;
4161
4162 /* p1 is at the beginning of the cookie name */
4163 p2 = p1;
4164 while (p2 < cur_end && *p2 != '=')
4165 p2++;
4166
4167 if (p2 == cur_end)
4168 break;
4169
4170 p3 = p2 + 1; /* skips the '=' sign */
4171 if (p3 == cur_end)
4172 break;
4173
4174 p4 = p3;
4175 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
4176 p4++;
4177
4178 /* here, we have the cookie name between p1 and p2,
4179 * and its value between p3 and p4.
4180 * we can process it :
4181 *
4182 * Cookie: NAME=VALUE;
4183 * | || || |
4184 * | || || +--> p4
4185 * | || |+-------> p3
4186 * | || +--------> p2
4187 * | |+------------> p1
4188 * | +-------------> colon
4189 * +--------------------> cur_ptr
4190 */
4191
4192 if (*p1 == '$') {
4193 /* skip this one */
4194 }
4195 else {
4196 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004197 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004198 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004199 (p4 - p1 >= t->fe->capture_namelen) &&
4200 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004201 int log_len = p4 - p1;
4202
Willy Tarreau086b3b42007-05-13 21:45:51 +02004203 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004204 Alert("HTTP logging : out of memory.\n");
4205 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004206 if (log_len > t->fe->capture_len)
4207 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004208 memcpy(txn->cli_cookie, p1, log_len);
4209 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004210 }
4211 }
4212
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004213 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4214 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004215 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004216 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004217 char *delim;
4218
4219 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4220 * have the server ID betweek p3 and delim, and the original cookie between
4221 * delim+1 and p4. Otherwise, delim==p4 :
4222 *
4223 * Cookie: NAME=SRV~VALUE;
4224 * | || || | |
4225 * | || || | +--> p4
4226 * | || || +--------> delim
4227 * | || |+-----------> p3
4228 * | || +------------> p2
4229 * | |+----------------> p1
4230 * | +-----------------> colon
4231 * +------------------------> cur_ptr
4232 */
4233
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004234 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004235 for (delim = p3; delim < p4; delim++)
4236 if (*delim == COOKIE_DELIM)
4237 break;
4238 }
4239 else
4240 delim = p4;
4241
4242
4243 /* Here, we'll look for the first running server which supports the cookie.
4244 * This allows to share a same cookie between several servers, for example
4245 * to dedicate backup servers to specific servers only.
4246 * However, to prevent clients from sticking to cookie-less backup server
4247 * when they have incidentely learned an empty cookie, we simply ignore
4248 * empty cookies and mark them as invalid.
4249 */
4250 if (delim == p3)
4251 srv = NULL;
4252
4253 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004254 if (srv->cookie && (srv->cklen == delim - p3) &&
4255 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004256 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004257 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004258 txn->flags &= ~TX_CK_MASK;
4259 txn->flags |= TX_CK_VALID;
4260 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004261 t->srv = srv;
4262 break;
4263 } else {
4264 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004265 txn->flags &= ~TX_CK_MASK;
4266 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004267 }
4268 }
4269 srv = srv->next;
4270 }
4271
Willy Tarreau3d300592007-03-18 18:34:41 +01004272 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004273 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004274 txn->flags &= ~TX_CK_MASK;
4275 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004276 }
4277
4278 /* depending on the cookie mode, we may have to either :
4279 * - delete the complete cookie if we're in insert+indirect mode, so that
4280 * the server never sees it ;
4281 * - remove the server id from the cookie value, and tag the cookie as an
4282 * application cookie so that it does not get accidentely removed later,
4283 * if we're in cookie prefix mode
4284 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004285 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004286 int delta; /* negative */
4287
4288 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4289 p4 += delta;
4290 cur_end += delta;
4291 cur_next += delta;
4292 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004293 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004294
4295 del_cookie = del_colon = NULL;
4296 app_cookies++; /* protect the header from deletion */
4297 }
4298 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004299 (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 +01004300 del_cookie = p1;
4301 del_colon = colon;
4302 }
4303 } else {
4304 /* now we know that we must keep this cookie since it's
4305 * not ours. But if we wanted to delete our cookie
4306 * earlier, we cannot remove the complete header, but we
4307 * can remove the previous block itself.
4308 */
4309 app_cookies++;
4310
4311 if (del_cookie != NULL) {
4312 int delta; /* negative */
4313
4314 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4315 p4 += delta;
4316 cur_end += delta;
4317 cur_next += delta;
4318 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004319 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004320 del_cookie = del_colon = NULL;
4321 }
4322 }
4323
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004324 if ((t->be->appsession_name != NULL) &&
4325 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004326 /* first, let's see if the cookie is our appcookie*/
4327
4328 /* Cool... it's the right one */
4329
4330 asession_temp = &local_asession;
4331
Willy Tarreau63963c62007-05-13 21:29:55 +02004332 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004333 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4334 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4335 return;
4336 }
4337
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004338 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4339 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004340 asession_temp->serverid = NULL;
4341
4342 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004343 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004344 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004345 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004346 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004347 Alert("Not enough memory process_cli():asession:calloc().\n");
4348 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4349 return;
4350 }
4351
4352 asession_temp->sessid = local_asession.sessid;
4353 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004354 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004355 } else {
4356 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004357 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004358 }
4359
4360 if (asession_temp->serverid == NULL) {
4361 Alert("Found Application Session without matching server.\n");
4362 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004363 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004364 while (srv) {
4365 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004366 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004367 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004368 txn->flags &= ~TX_CK_MASK;
4369 txn->flags |= TX_CK_VALID;
4370 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004371 t->srv = srv;
4372 break;
4373 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004374 txn->flags &= ~TX_CK_MASK;
4375 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004376 }
4377 }
4378 srv = srv->next;
4379 }/* end while(srv) */
4380 }/* end else if server == NULL */
4381
Willy Tarreaud825eef2007-05-12 22:35:00 +02004382 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004383 }/* end if ((t->proxy->appsession_name != NULL) ... */
4384 }
4385
4386 /* we'll have to look for another cookie ... */
4387 p1 = p4;
4388 } /* while (p1 < cur_end) */
4389
4390 /* There's no more cookie on this line.
4391 * We may have marked the last one(s) for deletion.
4392 * We must do this now in two ways :
4393 * - if there is no app cookie, we simply delete the header ;
4394 * - if there are app cookies, we must delete the end of the
4395 * string properly, including the colon/semi-colon before
4396 * the cookie name.
4397 */
4398 if (del_cookie != NULL) {
4399 int delta;
4400 if (app_cookies) {
4401 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4402 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004403 cur_hdr->len += delta;
4404 } else {
4405 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004406
4407 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004408 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4409 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004410 cur_hdr->len = 0;
4411 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004412 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004413 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004414 }
4415
4416 /* keep the link from this header to next one */
4417 old_idx = cur_idx;
4418 } /* end of cookie processing on this header */
4419}
4420
4421
Willy Tarreaua15645d2007-03-18 16:22:39 +01004422/* Iterate the same filter through all response headers contained in <rtr>.
4423 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4424 */
4425int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4426{
4427 char term;
4428 char *cur_ptr, *cur_end, *cur_next;
4429 int cur_idx, old_idx, last_hdr;
4430 struct http_txn *txn = &t->txn;
4431 struct hdr_idx_elem *cur_hdr;
4432 int len, delta;
4433
4434 last_hdr = 0;
4435
4436 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4437 old_idx = 0;
4438
4439 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004440 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004441 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004442 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004443 (exp->action == ACT_ALLOW ||
4444 exp->action == ACT_DENY))
4445 return 0;
4446
4447 cur_idx = txn->hdr_idx.v[old_idx].next;
4448 if (!cur_idx)
4449 break;
4450
4451 cur_hdr = &txn->hdr_idx.v[cur_idx];
4452 cur_ptr = cur_next;
4453 cur_end = cur_ptr + cur_hdr->len;
4454 cur_next = cur_end + cur_hdr->cr + 1;
4455
4456 /* Now we have one header between cur_ptr and cur_end,
4457 * and the next header starts at cur_next.
4458 */
4459
4460 /* The annoying part is that pattern matching needs
4461 * that we modify the contents to null-terminate all
4462 * strings before testing them.
4463 */
4464
4465 term = *cur_end;
4466 *cur_end = '\0';
4467
4468 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4469 switch (exp->action) {
4470 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004471 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004472 last_hdr = 1;
4473 break;
4474
4475 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004476 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004477 last_hdr = 1;
4478 break;
4479
4480 case ACT_REPLACE:
4481 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4482 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4483 /* FIXME: if the user adds a newline in the replacement, the
4484 * index will not be recalculated for now, and the new line
4485 * will not be counted as a new header.
4486 */
4487
4488 cur_end += delta;
4489 cur_next += delta;
4490 cur_hdr->len += delta;
4491 txn->rsp.eoh += delta;
4492 break;
4493
4494 case ACT_REMOVE:
4495 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4496 cur_next += delta;
4497
4498 /* FIXME: this should be a separate function */
4499 txn->rsp.eoh += delta;
4500 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4501 txn->hdr_idx.used--;
4502 cur_hdr->len = 0;
4503 cur_end = NULL; /* null-term has been rewritten */
4504 break;
4505
4506 }
4507 }
4508 if (cur_end)
4509 *cur_end = term; /* restore the string terminator */
4510
4511 /* keep the link from this header to next one in case of later
4512 * removal of next header.
4513 */
4514 old_idx = cur_idx;
4515 }
4516 return 0;
4517}
4518
4519
4520/* Apply the filter to the status line in the response buffer <rtr>.
4521 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4522 * or -1 if a replacement resulted in an invalid status line.
4523 */
4524int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4525{
4526 char term;
4527 char *cur_ptr, *cur_end;
4528 int done;
4529 struct http_txn *txn = &t->txn;
4530 int len, delta;
4531
4532
Willy Tarreau3d300592007-03-18 18:34:41 +01004533 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004534 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004535 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004536 (exp->action == ACT_ALLOW ||
4537 exp->action == ACT_DENY))
4538 return 0;
4539 else if (exp->action == ACT_REMOVE)
4540 return 0;
4541
4542 done = 0;
4543
Willy Tarreau9cdde232007-05-02 20:58:19 +02004544 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004545 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4546
4547 /* Now we have the status line between cur_ptr and cur_end */
4548
4549 /* The annoying part is that pattern matching needs
4550 * that we modify the contents to null-terminate all
4551 * strings before testing them.
4552 */
4553
4554 term = *cur_end;
4555 *cur_end = '\0';
4556
4557 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4558 switch (exp->action) {
4559 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004560 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004561 done = 1;
4562 break;
4563
4564 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004565 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004566 done = 1;
4567 break;
4568
4569 case ACT_REPLACE:
4570 *cur_end = term; /* restore the string terminator */
4571 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4572 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4573 /* FIXME: if the user adds a newline in the replacement, the
4574 * index will not be recalculated for now, and the new line
4575 * will not be counted as a new header.
4576 */
4577
4578 txn->rsp.eoh += delta;
4579 cur_end += delta;
4580
Willy Tarreau9cdde232007-05-02 20:58:19 +02004581 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004582 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004583 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004584 cur_ptr, cur_end + 1,
4585 NULL, NULL);
4586 if (unlikely(!cur_end))
4587 return -1;
4588
4589 /* we have a full respnse and we know that we have either a CR
4590 * or an LF at <ptr>.
4591 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004592 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004593 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4594 /* there is no point trying this regex on headers */
4595 return 1;
4596 }
4597 }
4598 *cur_end = term; /* restore the string terminator */
4599 return done;
4600}
4601
4602
4603
4604/*
4605 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4606 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4607 * unparsable response.
4608 */
4609int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4610{
Willy Tarreau3d300592007-03-18 18:34:41 +01004611 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004612 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004613 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004614 int ret;
4615
4616 /*
4617 * The interleaving of transformations and verdicts
4618 * makes it difficult to decide to continue or stop
4619 * the evaluation.
4620 */
4621
Willy Tarreau3d300592007-03-18 18:34:41 +01004622 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004623 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4624 exp->action == ACT_PASS)) {
4625 exp = exp->next;
4626 continue;
4627 }
4628
4629 /* Apply the filter to the status line. */
4630 ret = apply_filter_to_sts_line(t, rtr, exp);
4631 if (unlikely(ret < 0))
4632 return -1;
4633
4634 if (likely(ret == 0)) {
4635 /* The filter did not match the response, it can be
4636 * iterated through all headers.
4637 */
4638 apply_filter_to_resp_headers(t, rtr, exp);
4639 }
4640 exp = exp->next;
4641 }
4642 return 0;
4643}
4644
4645
4646
4647/*
4648 * Manager server-side cookies
4649 */
4650void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4651{
4652 struct http_txn *txn = &t->txn;
4653 char *p1, *p2, *p3, *p4;
4654
4655 appsess *asession_temp = NULL;
4656 appsess local_asession;
4657
4658 char *cur_ptr, *cur_end, *cur_next;
4659 int cur_idx, old_idx, delta;
4660
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004661 if (t->be->cookie_name == NULL &&
4662 t->be->appsession_name == NULL &&
4663 t->be->capture_name == NULL &&
4664 !(t->be->options & PR_O_CHK_CACHE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004665 return;
4666
4667 /* Iterate through the headers.
4668 * we start with the start line.
4669 */
4670 old_idx = 0;
4671 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4672
4673 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4674 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004675 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004676
4677 cur_hdr = &txn->hdr_idx.v[cur_idx];
4678 cur_ptr = cur_next;
4679 cur_end = cur_ptr + cur_hdr->len;
4680 cur_next = cur_end + cur_hdr->cr + 1;
4681
4682 /* We have one full header between cur_ptr and cur_end, and the
4683 * next header starts at cur_next. We're only interested in
4684 * "Cookie:" headers.
4685 */
4686
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004687 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4688 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004689 old_idx = cur_idx;
4690 continue;
4691 }
4692
4693 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004694 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004695
4696
4697 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004698 if (t->be->cookie_name == NULL &&
4699 t->be->appsession_name == NULL &&
4700 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004701 return;
4702
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004703 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004704
4705 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004706 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4707 break;
4708
4709 /* p1 is at the beginning of the cookie name */
4710 p2 = p1;
4711
4712 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4713 p2++;
4714
4715 if (p2 == cur_end || *p2 == ';') /* next cookie */
4716 break;
4717
4718 p3 = p2 + 1; /* skip the '=' sign */
4719 if (p3 == cur_end)
4720 break;
4721
4722 p4 = p3;
4723 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4724 p4++;
4725
4726 /* here, we have the cookie name between p1 and p2,
4727 * and its value between p3 and p4.
4728 * we can process it.
4729 */
4730
4731 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004732 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004733 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004734 (p4 - p1 >= t->be->capture_namelen) &&
4735 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004736 int log_len = p4 - p1;
4737
Willy Tarreau086b3b42007-05-13 21:45:51 +02004738 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004739 Alert("HTTP logging : out of memory.\n");
4740 }
4741
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004742 if (log_len > t->be->capture_len)
4743 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004744 memcpy(txn->srv_cookie, p1, log_len);
4745 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004746 }
4747
4748 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004749 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4750 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004751 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004752 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004753
4754 /* If the cookie is in insert mode on a known server, we'll delete
4755 * this occurrence because we'll insert another one later.
4756 * We'll delete it too if the "indirect" option is set and we're in
4757 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004758 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4759 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004760 /* this header must be deleted */
4761 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4762 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4763 txn->hdr_idx.used--;
4764 cur_hdr->len = 0;
4765 cur_next += delta;
4766 txn->rsp.eoh += delta;
4767
Willy Tarreau3d300592007-03-18 18:34:41 +01004768 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004769 }
4770 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004771 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004772 /* replace bytes p3->p4 with the cookie name associated
4773 * with this server since we know it.
4774 */
4775 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4776 cur_hdr->len += delta;
4777 cur_next += delta;
4778 txn->rsp.eoh += delta;
4779
Willy Tarreau3d300592007-03-18 18:34:41 +01004780 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004781 }
4782 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004783 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004784 /* insert the cookie name associated with this server
4785 * before existing cookie, and insert a delimitor between them..
4786 */
4787 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4788 cur_hdr->len += delta;
4789 cur_next += delta;
4790 txn->rsp.eoh += delta;
4791
4792 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004793 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004794 }
4795 }
4796 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004797 else if ((t->be->appsession_name != NULL) &&
4798 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004799
4800 /* Cool... it's the right one */
4801
4802 size_t server_id_len = strlen(t->srv->id) + 1;
4803 asession_temp = &local_asession;
4804
Willy Tarreau63963c62007-05-13 21:29:55 +02004805 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004806 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4807 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4808 return;
4809 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004810 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4811 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004812 asession_temp->serverid = NULL;
4813
4814 /* only do insert, if lookup fails */
4815 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004816 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004817 Alert("Not enough Memory process_srv():asession:calloc().\n");
4818 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4819 return;
4820 }
4821 asession_temp->sessid = local_asession.sessid;
4822 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004823 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004824 }/* end if (chtbl_lookup()) */
4825 else {
4826 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004827 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004828 } /* end else from if (chtbl_lookup()) */
4829
4830 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004831 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004832 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4833 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4834 return;
4835 }
4836 asession_temp->serverid[0] = '\0';
4837 }
4838
4839 if (asession_temp->serverid[0] == '\0')
4840 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4841
Willy Tarreaud825eef2007-05-12 22:35:00 +02004842 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004843
4844#if defined(DEBUG_HASH)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004845 print_table(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004846#endif
4847 }/* end if ((t->proxy->appsession_name != NULL) ... */
4848 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4849 } /* we're now at the end of the cookie value */
4850
4851 /* keep the link from this header to next one */
4852 old_idx = cur_idx;
4853 } /* end of cookie processing on this header */
4854}
4855
4856
4857
4858/*
4859 * Check if response is cacheable or not. Updates t->flags.
4860 */
4861void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4862{
4863 struct http_txn *txn = &t->txn;
4864 char *p1, *p2;
4865
4866 char *cur_ptr, *cur_end, *cur_next;
4867 int cur_idx;
4868
Willy Tarreau3d300592007-03-18 18:34:41 +01004869 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004870 return;
4871
4872 /* Iterate through the headers.
4873 * we start with the start line.
4874 */
4875 cur_idx = 0;
4876 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4877
4878 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4879 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004880 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004881
4882 cur_hdr = &txn->hdr_idx.v[cur_idx];
4883 cur_ptr = cur_next;
4884 cur_end = cur_ptr + cur_hdr->len;
4885 cur_next = cur_end + cur_hdr->cr + 1;
4886
4887 /* We have one full header between cur_ptr and cur_end, and the
4888 * next header starts at cur_next. We're only interested in
4889 * "Cookie:" headers.
4890 */
4891
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004892 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4893 if (val) {
4894 if ((cur_end - (cur_ptr + val) >= 8) &&
4895 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4896 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4897 return;
4898 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004899 }
4900
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004901 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4902 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004903 continue;
4904
4905 /* OK, right now we know we have a cache-control header at cur_ptr */
4906
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004907 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004908
4909 if (p1 >= cur_end) /* no more info */
4910 continue;
4911
4912 /* p1 is at the beginning of the value */
4913 p2 = p1;
4914
4915 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4916 p2++;
4917
4918 /* we have a complete value between p1 and p2 */
4919 if (p2 < cur_end && *p2 == '=') {
4920 /* we have something of the form no-cache="set-cookie" */
4921 if ((cur_end - p1 >= 21) &&
4922 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4923 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004924 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004925 continue;
4926 }
4927
4928 /* OK, so we know that either p2 points to the end of string or to a comma */
4929 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4930 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4931 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4932 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004933 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004934 return;
4935 }
4936
4937 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004938 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004939 continue;
4940 }
4941 }
4942}
4943
4944
Willy Tarreau58f10d72006-12-04 02:26:12 +01004945/*
4946 * Try to retrieve a known appsession in the URI, then the associated server.
4947 * If the server is found, it's assigned to the session.
4948 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004949void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004950{
Willy Tarreau3d300592007-03-18 18:34:41 +01004951 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004952 appsess *asession_temp = NULL;
4953 appsess local_asession;
4954 char *request_line;
4955
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004956 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004957 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004958 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004959 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004960 return;
4961
4962 /* skip ';' */
4963 request_line++;
4964
4965 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004966 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004967 return;
4968
4969 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004970 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004971
4972 /* First try if we already have an appsession */
4973 asession_temp = &local_asession;
4974
Willy Tarreau63963c62007-05-13 21:29:55 +02004975 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004976 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4977 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4978 return;
4979 }
4980
4981 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004982 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4983 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004984 asession_temp->serverid = NULL;
4985
4986 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004987 if (chtbl_lookup(&(t->be->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004988 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004989 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004990 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991 Alert("Not enough memory process_cli():asession:calloc().\n");
4992 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4993 return;
4994 }
4995 asession_temp->sessid = local_asession.sessid;
4996 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004997 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004998 }
4999 else {
5000 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005001 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005002 }
5003
Willy Tarreaud825eef2007-05-12 22:35:00 +02005004 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005005 asession_temp->request_count++;
5006
5007#if defined(DEBUG_HASH)
5008 print_table(&(t->proxy->htbl_proxy));
5009#endif
5010 if (asession_temp->serverid == NULL) {
5011 Alert("Found Application Session without matching server.\n");
5012 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005013 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005014 while (srv) {
5015 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005016 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005017 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005018 txn->flags &= ~TX_CK_MASK;
5019 txn->flags |= TX_CK_VALID;
5020 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005021 t->srv = srv;
5022 break;
5023 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005024 txn->flags &= ~TX_CK_MASK;
5025 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005026 }
5027 }
5028 srv = srv->next;
5029 }
5030 }
5031}
5032
5033
Willy Tarreaub2513902006-12-17 14:52:38 +01005034/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005035 * In a GET or HEAD request, check if the requested URI matches the stats uri
5036 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005037 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005038 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005039 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005040 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005041 *
5042 * Returns 1 if the session's state changes, otherwise 0.
5043 */
5044int stats_check_uri_auth(struct session *t, struct proxy *backend)
5045{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005046 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005047 struct uri_auth *uri_auth = backend->uri_auth;
5048 struct user_auth *user;
5049 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005050 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005052 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005053 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005054 return 0;
5055
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005056 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005057
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005058 /* the URI is in h */
5059 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005060 return 0;
5061
5062 /* we are in front of a interceptable URI. Let's check
5063 * if there's an authentication and if it's valid.
5064 */
5065 user = uri_auth->users;
5066 if (!user) {
5067 /* no user auth required, it's OK */
5068 authenticated = 1;
5069 } else {
5070 authenticated = 0;
5071
5072 /* a user list is defined, we have to check.
5073 * skip 21 chars for "Authorization: Basic ".
5074 */
5075
5076 /* FIXME: this should move to an earlier place */
5077 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005078 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5079 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5080 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005081 if (len > 14 &&
5082 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005083 txn->auth_hdr.str = h;
5084 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005085 break;
5086 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005087 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005088 }
5089
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005090 if (txn->auth_hdr.len < 21 ||
5091 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005092 user = NULL;
5093
5094 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005095 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5096 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005097 user->user_pwd, user->user_len)) {
5098 authenticated = 1;
5099 break;
5100 }
5101 user = user->next;
5102 }
5103 }
5104
5105 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005106 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005107
5108 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005109 msg.str = trash;
5110 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005111 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005112 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01005113 if (!(t->flags & SN_ERR_MASK))
5114 t->flags |= SN_ERR_PRXCOND;
5115 if (!(t->flags & SN_FINST_MASK))
5116 t->flags |= SN_FINST_R;
5117 return 1;
5118 }
5119
5120 /* The request is valid, the user is authenticate. Let's start sending
5121 * data.
5122 */
5123 t->cli_state = CL_STSHUTR;
5124 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02005125 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01005126 t->data_source = DATA_SRC_STATS;
5127 t->data_state = DATA_ST_INIT;
5128 produce_content(t);
5129 return 1;
5130}
5131
5132
Willy Tarreaubaaee002006-06-26 02:48:02 +02005133/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005134 * Print a debug line with a header
5135 */
5136void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5137{
5138 int len, max;
5139 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5140 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5141 max = end - start;
5142 UBOUND(max, sizeof(trash) - len - 1);
5143 len += strlcpy2(trash + len, start, max + 1);
5144 trash[len++] = '\n';
5145 write(1, trash, len);
5146}
5147
5148
Willy Tarreau8797c062007-05-07 00:55:35 +02005149/************************************************************************/
5150/* The code below is dedicated to ACL parsing and matching */
5151/************************************************************************/
5152
5153
5154
5155
5156/* 1. Check on METHOD
5157 * We use the pre-parsed method if it is known, and store its number as an
5158 * integer. If it is unknown, we use the pointer and the length.
5159 */
5160static int acl_parse_meth(const char *text, struct acl_pattern *pattern)
5161{
5162 int len, meth;
5163
5164 len = strlen(text);
5165 meth = find_http_meth(text, len);
5166
5167 pattern->val.i = meth;
5168 if (meth == HTTP_METH_OTHER) {
5169 pattern->ptr.str = strdup(text);
5170 if (!pattern->ptr.str)
5171 return 0;
5172 pattern->len = len;
5173 }
5174 return 1;
5175}
5176
5177static int acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5178{
5179 int meth;
5180 struct http_txn *txn = l7;
5181
5182 meth = txn->meth;
5183 test->i = meth;
5184 if (meth == HTTP_METH_OTHER) {
5185 test->len = txn->req.sl.rq.m_l;
5186 test->ptr = txn->req.sol;
5187 }
5188 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5189 return 1;
5190}
5191
5192static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5193{
5194 if (test->i != pattern->val.i)
5195 return 0;
5196
5197 if (test->i != HTTP_METH_OTHER)
5198 return 1;
5199
5200 /* Other method, we must compare the strings */
5201 if (pattern->len != test->len)
5202 return 0;
5203 if (strncmp(pattern->ptr.str, test->ptr, test->len) != 0)
5204 return 0;
5205 return 1;
5206}
5207
5208/* 2. Check on Request/Status Version
5209 * We simply compare strings here.
5210 */
5211static int acl_parse_ver(const char *text, struct acl_pattern *pattern)
5212{
5213 pattern->ptr.str = strdup(text);
5214 if (!pattern->ptr.str)
5215 return 0;
5216 pattern->len = strlen(text);
5217 return 1;
5218}
5219
5220static int acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5221{
5222 struct http_txn *txn = l7;
5223 char *ptr;
5224 int len;
5225
5226 len = txn->req.sl.rq.v_l;
5227 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5228
5229 while ((len-- > 0) && (*ptr++ != '/'));
5230 if (len <= 0)
5231 return 0;
5232
5233 test->ptr = ptr;
5234 test->len = len;
5235
5236 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5237 return 1;
5238}
5239
5240static int acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5241{
5242 struct http_txn *txn = l7;
5243 char *ptr;
5244 int len;
5245
5246 len = txn->rsp.sl.st.v_l;
5247 ptr = txn->rsp.sol;
5248
5249 while ((len-- > 0) && (*ptr++ != '/'));
5250 if (len <= 0)
5251 return 0;
5252
5253 test->ptr = ptr;
5254 test->len = len;
5255
5256 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5257 return 1;
5258}
5259
5260/* 3. Check on Status Code. We manipulate integers here. */
5261static int acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5262{
5263 struct http_txn *txn = l7;
5264 char *ptr;
5265 int len;
5266
5267 len = txn->rsp.sl.st.c_l;
5268 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5269
5270 test->i = __strl2ui(ptr, len);
5271 test->flags = ACL_TEST_F_VOL_1ST;
5272 return 1;
5273}
5274
5275/* 4. Check on URL/URI. A pointer to the URI is stored. */
5276static int acl_fetch_url(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5277{
5278 struct http_txn *txn = l7;
5279
5280 test->len = txn->req.sl.rq.u_l;
5281 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5282
Willy Tarreauf3d25982007-05-08 22:45:09 +02005283 /* we do not need to set READ_ONLY because the data is in a buffer */
5284 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005285 return 1;
5286}
5287
5288
5289
5290/************************************************************************/
5291/* All supported keywords must be declared here. */
5292/************************************************************************/
5293
5294/* Note: must not be declared <const> as its list will be overwritten */
5295static struct acl_kw_list acl_kws = {{ },{
5296 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5297 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5298 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
5299 { "status", acl_parse_range, acl_fetch_stcode, acl_match_range },
5300
5301 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5302 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5303 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5304 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5305 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5306 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
Willy Tarreau8797c062007-05-07 00:55:35 +02005307 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
5308
Willy Tarreauf3d25982007-05-08 22:45:09 +02005309 { NULL, NULL, NULL, NULL },
5310
5311#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005312 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5313 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5314 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5315 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5316 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5317 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5318 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5319
5320 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5321 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5322 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5323 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5324 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5325 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5326 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5327
5328 { "hdr", acl_parse_str, acl_fetch_hdr, acl_match_str },
5329 { "hdr_reg", acl_parse_reg, acl_fetch_hdr, acl_match_reg },
5330 { "hdr_beg", acl_parse_str, acl_fetch_hdr, acl_match_beg },
5331 { "hdr_end", acl_parse_str, acl_fetch_hdr, acl_match_end },
5332 { "hdr_sub", acl_parse_str, acl_fetch_hdr, acl_match_sub },
5333 { "hdr_dir", acl_parse_str, acl_fetch_hdr, acl_match_dir },
5334 { "hdr_dom", acl_parse_str, acl_fetch_hdr, acl_match_dom },
5335 { "hdr_pst", acl_parse_none, acl_fetch_hdr, acl_match_pst },
5336
5337 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5338 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5339 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5340 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5341 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5342 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5343 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5344 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5345
5346 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5347 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5348 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5349 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5350 { NULL, NULL, NULL, NULL },
5351#endif
5352}};
5353
5354
5355__attribute__((constructor))
5356static void __http_protocol_init(void)
5357{
5358 acl_register_keywords(&acl_kws);
5359}
5360
5361
Willy Tarreau58f10d72006-12-04 02:26:12 +01005362/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005363 * Local variables:
5364 * c-indent-level: 8
5365 * c-basic-offset: 8
5366 * End:
5367 */