blob: 6d4c152801c063c5af2d109f11a88f621a548093 [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 Tarreau80587432006-12-24 17:47:20 +0100246}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200247
Willy Tarreau53b6c742006-12-17 13:37:46 +0100248/*
249 * We have 26 list of methods (1 per first letter), each of which can have
250 * up to 3 entries (2 valid, 1 null).
251 */
252struct http_method_desc {
253 http_meth_t meth;
254 int len;
255 const char text[8];
256};
257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100258const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100259 ['C' - 'A'] = {
260 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
261 },
262 ['D' - 'A'] = {
263 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
264 },
265 ['G' - 'A'] = {
266 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
267 },
268 ['H' - 'A'] = {
269 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
270 },
271 ['P' - 'A'] = {
272 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
273 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
274 },
275 ['T' - 'A'] = {
276 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
277 },
278 /* rest is empty like this :
279 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
280 */
281};
282
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100283/* It is about twice as fast on recent architectures to lookup a byte in a
284 * table than two perform a boolean AND or OR between two tests. Refer to
285 * RFC2616 for those chars.
286 */
287
288const char http_is_spht[256] = {
289 [' '] = 1, ['\t'] = 1,
290};
291
292const char http_is_crlf[256] = {
293 ['\r'] = 1, ['\n'] = 1,
294};
295
296const char http_is_lws[256] = {
297 [' '] = 1, ['\t'] = 1,
298 ['\r'] = 1, ['\n'] = 1,
299};
300
301const char http_is_sep[256] = {
302 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
303 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
304 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
305 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
306 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
307};
308
309const char http_is_ctl[256] = {
310 [0 ... 31] = 1,
311 [127] = 1,
312};
313
314/*
315 * A token is any ASCII char that is neither a separator nor a CTL char.
316 * Do not overwrite values in assignment since gcc-2.95 will not handle
317 * them correctly. Instead, define every non-CTL char's status.
318 */
319const char http_is_token[256] = {
320 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
321 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
322 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
323 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
324 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
325 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
326 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
327 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
328 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
329 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
330 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
331 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
332 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
333 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
334 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
335 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
336 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
337 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
338 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
339 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
340 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
341 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
342 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
343 ['|'] = 1, ['}'] = 0, ['~'] = 1,
344};
345
346
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100347/*
348 * An http ver_token is any ASCII which can be found in an HTTP version,
349 * which includes 'H', 'T', 'P', '/', '.' and any digit.
350 */
351const char http_is_ver_token[256] = {
352 ['.'] = 1, ['/'] = 1,
353 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
354 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
355 ['H'] = 1, ['P'] = 1, ['T'] = 1,
356};
357
358
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359#ifdef DEBUG_FULL
360static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
361static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
362#endif
363
Willy Tarreau42250582007-04-01 01:30:43 +0200364static void http_sess_log(struct session *s);
365
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100366/*
367 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
368 * CRLF. Text length is measured first, so it cannot be NULL.
369 * The header is also automatically added to the index <hdr_idx>, and the end
370 * of headers is automatically adjusted. The number of bytes added is returned
371 * on success, otherwise <0 is returned indicating an error.
372 */
373int http_header_add_tail(struct buffer *b, struct http_msg *msg,
374 struct hdr_idx *hdr_idx, const char *text)
375{
376 int bytes, len;
377
378 len = strlen(text);
379 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
380 if (!bytes)
381 return -1;
382 msg->eoh += bytes;
383 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
384}
385
386/*
387 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
388 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
389 * the buffer is only opened and the space reserved, but nothing is copied.
390 * The header is also automatically added to the index <hdr_idx>, and the end
391 * of headers is automatically adjusted. The number of bytes added is returned
392 * on success, otherwise <0 is returned indicating an error.
393 */
394int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
395 struct hdr_idx *hdr_idx, const char *text, int len)
396{
397 int bytes;
398
399 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
400 if (!bytes)
401 return -1;
402 msg->eoh += bytes;
403 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
404}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405
406/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100407 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
408 * If so, returns the position of the first non-space character relative to
409 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
410 * to return a pointer to the place after the first space. Returns 0 if the
411 * header name does not match. Checks are case-insensitive.
412 */
413int http_header_match2(const char *hdr, const char *end,
414 const char *name, int len)
415{
416 const char *val;
417
418 if (hdr + len >= end)
419 return 0;
420 if (hdr[len] != ':')
421 return 0;
422 if (strncasecmp(hdr, name, len) != 0)
423 return 0;
424 val = hdr + len + 1;
425 while (val < end && HTTP_IS_SPHT(*val))
426 val++;
427 if ((val >= end) && (len + 2 <= end - hdr))
428 return len + 2; /* we may replace starting from second space */
429 return val - hdr;
430}
431
432/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433 * returns a message to the client ; the connection is shut down for read,
434 * and the request is cleared so that no server connection can be initiated.
435 * The client must be in a valid state for this (HEADER, DATA ...).
Willy Tarreau0f772532006-12-23 20:51:41 +0100436 * Nothing is performed on the server side. The message is contained in a
437 * "chunk". If it is null, then an empty message is used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438 * The reply buffer doesn't need to be empty before this.
439 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100440void client_retnclose(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441{
Willy Tarreauf161a342007-04-08 16:59:42 +0200442 EV_FD_CLR(s->cli_fd, DIR_RD);
443 EV_FD_SET(s->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +0200444 tv_eternity(&s->req->rex);
Willy Tarreau73de9892006-11-30 11:40:23 +0100445 if (s->fe->clitimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200446 tv_ms_add(&s->rep->wex, &now, s->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200447 else
Willy Tarreaud7971282006-07-29 18:36:34 +0200448 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449 s->cli_state = CL_STSHUTR;
450 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100451 if (msg->len)
452 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453 s->req->l = 0;
454}
455
456
457/*
458 * returns a message into the rep buffer, and flushes the req buffer.
Willy Tarreau0f772532006-12-23 20:51:41 +0100459 * The reply buffer doesn't need to be empty before this. The message
460 * is contained in a "chunk". If it is null, then an empty message is
461 * used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100463void client_return(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464{
465 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100466 if (msg->len)
467 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468 s->req->l = 0;
469}
470
471
472/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100473 * indicators accordingly. Note that if <status> is 0, or if the message
474 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475 */
476void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100477 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200478{
479 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100480 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100481 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100482 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100483 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484 }
485 if (!(t->flags & SN_ERR_MASK))
486 t->flags |= err;
487 if (!(t->flags & SN_FINST_MASK))
488 t->flags |= finst;
489}
490
Willy Tarreau80587432006-12-24 17:47:20 +0100491/* This function returns the appropriate error location for the given session
492 * and message.
493 */
494
495struct chunk *error_message(struct session *s, int msgnum)
496{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200497 if (s->be->errmsg[msgnum].str)
498 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100499 else if (s->fe->errmsg[msgnum].str)
500 return &s->fe->errmsg[msgnum];
501 else
502 return &http_err_chunks[msgnum];
503}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200504
Willy Tarreau53b6c742006-12-17 13:37:46 +0100505/*
506 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
507 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
508 */
509static http_meth_t find_http_meth(const char *str, const int len)
510{
511 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100512 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100513
514 m = ((unsigned)*str - 'A');
515
516 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100517 for (h = http_methods[m]; h->len > 0; h++) {
518 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100519 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100520 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100521 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100522 };
523 return HTTP_METH_OTHER;
524 }
525 return HTTP_METH_NONE;
526
527}
528
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529/* Processes the client and server jobs of a session task, then
530 * puts it back to the wait queue in a clean state, or
531 * cleans up its resources if it must be deleted. Returns
532 * the time the task accepts to wait, or TIME_ETERNITY for
533 * infinity.
534 */
535int process_session(struct task *t)
536{
537 struct session *s = t->context;
538 int fsm_resync = 0;
539
540 do {
541 fsm_resync = 0;
542 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
543 fsm_resync |= process_cli(s);
544 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
545 fsm_resync |= process_srv(s);
546 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
547 } while (fsm_resync);
548
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200549 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200550 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
551 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200553 t->expire = s->req->rex;
554 tv_min(&t->expire, &s->req->rex, &s->req->wex);
555 tv_bound(&t->expire, &s->req->cex);
556 tv_bound(&t->expire, &s->rep->rex);
557 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558
559 /* restore t to its place in the task list */
560 task_queue(t);
561
562#ifdef DEBUG_FULL
563 /* DEBUG code : this should never ever happen, otherwise it indicates
564 * that a task still has something to do and will provoke a quick loop.
565 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200566 if (tv_ms_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200567 exit(100);
568#endif
569
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200570 return tv_ms_remain2(&now, &t->expire); /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571 }
572
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100573 s->fe->feconn--;
574 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200575 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576 actconn--;
577
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200578 if (unlikely((global.mode & MODE_DEBUG) &&
579 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200580 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100581 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200582 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100583 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200584 write(1, trash, len);
585 }
586
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200587 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100588 if (s->req != NULL)
589 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200590 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100591 s->logs.bytes_out = s->rep->total;
592
593 s->fe->bytes_in += s->logs.bytes_in;
594 s->fe->bytes_out += s->logs.bytes_out;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200595 if (s->be != s->fe) {
596 s->be->bytes_in += s->logs.bytes_in;
597 s->be->bytes_out += s->logs.bytes_out;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100598 }
599 if (s->srv) {
600 s->srv->bytes_in += s->logs.bytes_in;
601 s->srv->bytes_out += s->logs.bytes_out;
602 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200603
604 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200605 if (s->logs.logwait &&
606 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200607 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
608 if (s->fe->to_log & LW_REQ)
609 http_sess_log(s);
610 else
611 tcp_sess_log(s);
612 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200613
614 /* the task MUST not be in the run queue anymore */
615 task_delete(t);
616 session_free(s);
617 task_free(t);
618 return TIME_ETERNITY; /* rest in peace for eternity */
619}
620
621
Willy Tarreau42250582007-04-01 01:30:43 +0200622extern const char sess_term_cond[8];
623extern const char sess_fin_state[8];
624extern const char *monthname[12];
625const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
626const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
627 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
628 unknown, Set-cookie Rewritten */
629void **pool_requri = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100630
Willy Tarreau42250582007-04-01 01:30:43 +0200631/*
632 * send a log for the session when we have enough info about it.
633 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100634 */
Willy Tarreau42250582007-04-01 01:30:43 +0200635static void http_sess_log(struct session *s)
636{
637 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
638 struct proxy *fe = s->fe;
639 struct proxy *be = s->be;
640 struct proxy *prx_log;
641 struct http_txn *txn = &s->txn;
642 int tolog;
643 char *uri, *h;
644 char *svid;
645 struct tm *tm;
646 static char tmpline[MAX_SYSLOG_LEN];
647 int hdr;
648
649 if (fe->logfac1 < 0 && fe->logfac2 < 0)
650 return;
651 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100652
Willy Tarreau42250582007-04-01 01:30:43 +0200653 if (s->cli_addr.ss_family == AF_INET)
654 inet_ntop(AF_INET,
655 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
656 pn, sizeof(pn));
657 else
658 inet_ntop(AF_INET6,
659 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
660 pn, sizeof(pn));
661
662 tm = localtime((time_t *)&s->logs.tv_accept.tv_sec);
663
664
665 /* FIXME: let's limit ourselves to frontend logging for now. */
666 tolog = fe->to_log;
667
668 h = tmpline;
669 if (fe->to_log & LW_REQHDR &&
670 txn->req.cap &&
671 (h < tmpline + sizeof(tmpline) - 10)) {
672 *(h++) = ' ';
673 *(h++) = '{';
674 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
675 if (hdr)
676 *(h++) = '|';
677 if (txn->req.cap[hdr] != NULL)
678 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
679 '#', hdr_encode_map, txn->req.cap[hdr]);
680 }
681 *(h++) = '}';
682 }
683
684 if (fe->to_log & LW_RSPHDR &&
685 txn->rsp.cap &&
686 (h < tmpline + sizeof(tmpline) - 7)) {
687 *(h++) = ' ';
688 *(h++) = '{';
689 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
690 if (hdr)
691 *(h++) = '|';
692 if (txn->rsp.cap[hdr] != NULL)
693 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
694 '#', hdr_encode_map, txn->rsp.cap[hdr]);
695 }
696 *(h++) = '}';
697 }
698
699 if (h < tmpline + sizeof(tmpline) - 4) {
700 *(h++) = ' ';
701 *(h++) = '"';
702 uri = txn->uri ? txn->uri : "<BADREQ>";
703 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
704 '#', url_encode_map, uri);
705 *(h++) = '"';
706 }
707 *h = '\0';
708
709 svid = (tolog & LW_SVID) ?
710 (s->data_source != DATA_SRC_STATS) ?
711 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
712
713 send_log(prx_log, LOG_INFO,
714 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
715 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
716 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
717 pn,
718 (s->cli_addr.ss_family == AF_INET) ?
719 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
720 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
721 tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900,
722 tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000,
723 fe->id, be->id, svid,
724 s->logs.t_request,
725 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
726 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
727 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
728 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
729 txn->status,
730 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
731 txn->cli_cookie ? txn->cli_cookie : "-",
732 txn->srv_cookie ? txn->srv_cookie : "-",
733 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
734 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
735 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
736 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
737 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
738 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
739
740 s->logs.logwait = 0;
741}
742
Willy Tarreau117f59e2007-03-04 18:17:17 +0100743
744/*
745 * Capture headers from message starting at <som> according to header list
746 * <cap_hdr>, and fill the <idx> structure appropriately.
747 */
748void capture_headers(char *som, struct hdr_idx *idx,
749 char **cap, struct cap_hdr *cap_hdr)
750{
751 char *eol, *sol, *col, *sov;
752 int cur_idx;
753 struct cap_hdr *h;
754 int len;
755
756 sol = som + hdr_idx_first_pos(idx);
757 cur_idx = hdr_idx_first_idx(idx);
758
759 while (cur_idx) {
760 eol = sol + idx->v[cur_idx].len;
761
762 col = sol;
763 while (col < eol && *col != ':')
764 col++;
765
766 sov = col + 1;
767 while (sov < eol && http_is_lws[(unsigned char)*sov])
768 sov++;
769
770 for (h = cap_hdr; h; h = h->next) {
771 if ((h->namelen == col - sol) &&
772 (strncasecmp(sol, h->name, h->namelen) == 0)) {
773 if (cap[h->index] == NULL)
774 cap[h->index] =
775 pool_alloc_from(h->pool, h->len + 1);
776
777 if (cap[h->index] == NULL) {
778 Alert("HTTP capture : out of memory.\n");
779 continue;
780 }
781
782 len = eol - sov;
783 if (len > h->len)
784 len = h->len;
785
786 memcpy(cap[h->index], sov, len);
787 cap[h->index][len]=0;
788 }
789 }
790 sol = eol + idx->v[cur_idx].cr + 1;
791 cur_idx = idx->v[cur_idx].next;
792 }
793}
794
795
Willy Tarreau42250582007-04-01 01:30:43 +0200796/* either we find an LF at <ptr> or we jump to <bad>.
797 */
798#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
799
800/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
801 * otherwise to <http_msg_ood> with <state> set to <st>.
802 */
803#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
804 ptr++; \
805 if (likely(ptr < end)) \
806 goto good; \
807 else { \
808 state = (st); \
809 goto http_msg_ood; \
810 } \
811 } while (0)
812
813
Willy Tarreaubaaee002006-06-26 02:48:02 +0200814/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100815 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100816 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
817 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
818 * will give undefined results.
819 * Note that it is upon the caller's responsibility to ensure that ptr < end,
820 * and that msg->sol points to the beginning of the response.
821 * If a complete line is found (which implies that at least one CR or LF is
822 * found before <end>, the updated <ptr> is returned, otherwise NULL is
823 * returned indicating an incomplete line (which does not mean that parts have
824 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
825 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
826 * upon next call.
827 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200828 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100829 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
830 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200831 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100832 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100833const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100834 const char *ptr, const char *end,
835 char **ret_ptr, int *ret_state)
836{
837 __label__
838 http_msg_rpver,
839 http_msg_rpver_sp,
840 http_msg_rpcode,
841 http_msg_rpcode_sp,
842 http_msg_rpreason,
843 http_msg_rpline_eol,
844 http_msg_ood, /* out of data */
845 http_msg_invalid;
846
847 switch (state) {
848 http_msg_rpver:
849 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100850 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100851 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
852
853 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100854 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100855 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
856 }
857 goto http_msg_invalid;
858
859 http_msg_rpver_sp:
860 case HTTP_MSG_RPVER_SP:
861 if (likely(!HTTP_IS_LWS(*ptr))) {
862 msg->sl.st.c = ptr - msg_buf;
863 goto http_msg_rpcode;
864 }
865 if (likely(HTTP_IS_SPHT(*ptr)))
866 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
867 /* so it's a CR/LF, this is invalid */
868 goto http_msg_invalid;
869
870 http_msg_rpcode:
871 case HTTP_MSG_RPCODE:
872 if (likely(!HTTP_IS_LWS(*ptr)))
873 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
874
875 if (likely(HTTP_IS_SPHT(*ptr))) {
876 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
877 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
878 }
879
880 /* so it's a CR/LF, so there is no reason phrase */
881 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
882 http_msg_rsp_reason:
883 /* FIXME: should we support HTTP responses without any reason phrase ? */
884 msg->sl.st.r = ptr - msg_buf;
885 msg->sl.st.r_l = 0;
886 goto http_msg_rpline_eol;
887
888 http_msg_rpcode_sp:
889 case HTTP_MSG_RPCODE_SP:
890 if (likely(!HTTP_IS_LWS(*ptr))) {
891 msg->sl.st.r = ptr - msg_buf;
892 goto http_msg_rpreason;
893 }
894 if (likely(HTTP_IS_SPHT(*ptr)))
895 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
896 /* so it's a CR/LF, so there is no reason phrase */
897 goto http_msg_rsp_reason;
898
899 http_msg_rpreason:
900 case HTTP_MSG_RPREASON:
901 if (likely(!HTTP_IS_CRLF(*ptr)))
902 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
903 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
904 http_msg_rpline_eol:
905 /* We have seen the end of line. Note that we do not
906 * necessarily have the \n yet, but at least we know that we
907 * have EITHER \r OR \n, otherwise the response would not be
908 * complete. We can then record the response length and return
909 * to the caller which will be able to register it.
910 */
911 msg->sl.st.l = ptr - msg->sol;
912 return ptr;
913
914#ifdef DEBUG_FULL
915 default:
916 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
917 exit(1);
918#endif
919 }
920
921 http_msg_ood:
922 /* out of data */
923 if (ret_state)
924 *ret_state = state;
925 if (ret_ptr)
926 *ret_ptr = (char *)ptr;
927 return NULL;
928
929 http_msg_invalid:
930 /* invalid message */
931 if (ret_state)
932 *ret_state = HTTP_MSG_ERROR;
933 return NULL;
934}
935
936
937/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100938 * This function parses a request line between <ptr> and <end>, starting with
939 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
940 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
941 * will give undefined results.
942 * Note that it is upon the caller's responsibility to ensure that ptr < end,
943 * and that msg->sol points to the beginning of the request.
944 * If a complete line is found (which implies that at least one CR or LF is
945 * found before <end>, the updated <ptr> is returned, otherwise NULL is
946 * returned indicating an incomplete line (which does not mean that parts have
947 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
948 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
949 * upon next call.
950 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200951 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100952 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
953 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200954 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100956const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100957 const char *ptr, const char *end,
958 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200959{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100960 __label__
961 http_msg_rqmeth,
962 http_msg_rqmeth_sp,
963 http_msg_rquri,
964 http_msg_rquri_sp,
965 http_msg_rqver,
966 http_msg_rqline_eol,
967 http_msg_ood, /* out of data */
968 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100969
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100970 switch (state) {
971 http_msg_rqmeth:
972 case HTTP_MSG_RQMETH:
973 if (likely(HTTP_IS_TOKEN(*ptr)))
974 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100975
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100976 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100977 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100978 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
979 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100980
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100981 if (likely(HTTP_IS_CRLF(*ptr))) {
982 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100983 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100984 http_msg_req09_uri:
985 msg->sl.rq.u = ptr - msg_buf;
986 http_msg_req09_uri_e:
987 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
988 http_msg_req09_ver:
989 msg->sl.rq.v = ptr - msg_buf;
990 msg->sl.rq.v_l = 0;
991 goto http_msg_rqline_eol;
992 }
993 goto http_msg_invalid;
994
995 http_msg_rqmeth_sp:
996 case HTTP_MSG_RQMETH_SP:
997 if (likely(!HTTP_IS_LWS(*ptr))) {
998 msg->sl.rq.u = ptr - msg_buf;
999 goto http_msg_rquri;
1000 }
1001 if (likely(HTTP_IS_SPHT(*ptr)))
1002 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1003 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1004 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001005
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001006 http_msg_rquri:
1007 case HTTP_MSG_RQURI:
1008 if (likely(!HTTP_IS_LWS(*ptr)))
1009 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001010
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001011 if (likely(HTTP_IS_SPHT(*ptr))) {
1012 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1013 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1014 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001015
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001016 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1017 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001019 http_msg_rquri_sp:
1020 case HTTP_MSG_RQURI_SP:
1021 if (likely(!HTTP_IS_LWS(*ptr))) {
1022 msg->sl.rq.v = ptr - msg_buf;
1023 goto http_msg_rqver;
1024 }
1025 if (likely(HTTP_IS_SPHT(*ptr)))
1026 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1027 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1028 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001029
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001030 http_msg_rqver:
1031 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001032 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001033 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001034
1035 if (likely(HTTP_IS_CRLF(*ptr))) {
1036 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1037 http_msg_rqline_eol:
1038 /* We have seen the end of line. Note that we do not
1039 * necessarily have the \n yet, but at least we know that we
1040 * have EITHER \r OR \n, otherwise the request would not be
1041 * complete. We can then record the request length and return
1042 * to the caller which will be able to register it.
1043 */
1044 msg->sl.rq.l = ptr - msg->sol;
1045 return ptr;
1046 }
1047
1048 /* neither an HTTP_VER token nor a CRLF */
1049 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001050
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001051#ifdef DEBUG_FULL
1052 default:
1053 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1054 exit(1);
1055#endif
1056 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001057
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001058 http_msg_ood:
1059 /* out of data */
1060 if (ret_state)
1061 *ret_state = state;
1062 if (ret_ptr)
1063 *ret_ptr = (char *)ptr;
1064 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001065
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066 http_msg_invalid:
1067 /* invalid message */
1068 if (ret_state)
1069 *ret_state = HTTP_MSG_ERROR;
1070 return NULL;
1071}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072
1073
Willy Tarreau8973c702007-01-21 23:58:29 +01001074/*
1075 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001076 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001077 * when data are missing and recalled at the exact same location with no
1078 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001079 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1080 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001081 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1083{
1084 __label__
1085 http_msg_rqbefore,
1086 http_msg_rqbefore_cr,
1087 http_msg_rqmeth,
1088 http_msg_rqline_end,
1089 http_msg_hdr_first,
1090 http_msg_hdr_name,
1091 http_msg_hdr_l1_sp,
1092 http_msg_hdr_l1_lf,
1093 http_msg_hdr_l1_lws,
1094 http_msg_hdr_val,
1095 http_msg_hdr_l2_lf,
1096 http_msg_hdr_l2_lws,
1097 http_msg_complete_header,
1098 http_msg_last_lf,
1099 http_msg_ood, /* out of data */
1100 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 int state; /* updated only when leaving the FSM */
1103 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001104
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001105 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001106 ptr = buf->lr;
1107 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 if (unlikely(ptr >= end))
1110 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 /*
1114 * First, states that are specific to the response only.
1115 * We check them first so that request and headers are
1116 * closer to each other (accessed more often).
1117 */
1118 http_msg_rpbefore:
1119 case HTTP_MSG_RPBEFORE:
1120 if (likely(HTTP_IS_TOKEN(*ptr))) {
1121 if (likely(ptr == buf->data)) {
1122 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001123 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 } else {
1125#if PARSE_PRESERVE_EMPTY_LINES
1126 /* only skip empty leading lines, don't remove them */
1127 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001128 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001129#else
1130 /* Remove empty leading lines, as recommended by
1131 * RFC2616. This takes a lot of time because we
1132 * must move all the buffer backwards, but this
1133 * is rarely needed. The method above will be
1134 * cleaner when we'll be able to start sending
1135 * the request from any place in the buffer.
1136 */
1137 buf->lr = ptr;
1138 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001139 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 msg->sol = buf->data;
1141 ptr = buf->data;
1142 end = buf->r;
1143#endif
1144 }
1145 hdr_idx_init(idx);
1146 state = HTTP_MSG_RPVER;
1147 goto http_msg_rpver;
1148 }
1149
1150 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1151 goto http_msg_invalid;
1152
1153 if (unlikely(*ptr == '\n'))
1154 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1155 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1156 /* stop here */
1157
1158 http_msg_rpbefore_cr:
1159 case HTTP_MSG_RPBEFORE_CR:
1160 EXPECT_LF_HERE(ptr, http_msg_invalid);
1161 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1162 /* stop here */
1163
1164 http_msg_rpver:
1165 case HTTP_MSG_RPVER:
1166 case HTTP_MSG_RPVER_SP:
1167 case HTTP_MSG_RPCODE:
1168 case HTTP_MSG_RPCODE_SP:
1169 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001170 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001171 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 if (unlikely(!ptr))
1173 return;
1174
1175 /* we have a full response and we know that we have either a CR
1176 * or an LF at <ptr>.
1177 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001178 //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 +01001179 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1180
1181 msg->sol = ptr;
1182 if (likely(*ptr == '\r'))
1183 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1184 goto http_msg_rpline_end;
1185
1186 http_msg_rpline_end:
1187 case HTTP_MSG_RPLINE_END:
1188 /* msg->sol must point to the first of CR or LF. */
1189 EXPECT_LF_HERE(ptr, http_msg_invalid);
1190 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1191 /* stop here */
1192
1193 /*
1194 * Second, states that are specific to the request only
1195 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001196 http_msg_rqbefore:
1197 case HTTP_MSG_RQBEFORE:
1198 if (likely(HTTP_IS_TOKEN(*ptr))) {
1199 if (likely(ptr == buf->data)) {
1200 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001201 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001202 } else {
1203#if PARSE_PRESERVE_EMPTY_LINES
1204 /* only skip empty leading lines, don't remove them */
1205 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001206 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001207#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001208 /* Remove empty leading lines, as recommended by
1209 * RFC2616. This takes a lot of time because we
1210 * must move all the buffer backwards, but this
1211 * is rarely needed. The method above will be
1212 * cleaner when we'll be able to start sending
1213 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001214 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001215 buf->lr = ptr;
1216 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001217 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001218 msg->sol = buf->data;
1219 ptr = buf->data;
1220 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001221#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001222 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001223 /* we will need this when keep-alive will be supported
1224 hdr_idx_init(idx);
1225 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 state = HTTP_MSG_RQMETH;
1227 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001228 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1231 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233 if (unlikely(*ptr == '\n'))
1234 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1235 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001236 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001237
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001238 http_msg_rqbefore_cr:
1239 case HTTP_MSG_RQBEFORE_CR:
1240 EXPECT_LF_HERE(ptr, http_msg_invalid);
1241 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001242 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001243
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 http_msg_rqmeth:
1245 case HTTP_MSG_RQMETH:
1246 case HTTP_MSG_RQMETH_SP:
1247 case HTTP_MSG_RQURI:
1248 case HTTP_MSG_RQURI_SP:
1249 case HTTP_MSG_RQVER:
1250 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001251 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 if (unlikely(!ptr))
1253 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001254
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 /* we have a full request and we know that we have either a CR
1256 * or an LF at <ptr>.
1257 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001258 //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 +01001259 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 msg->sol = ptr;
1262 if (likely(*ptr == '\r'))
1263 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 http_msg_rqline_end:
1267 case HTTP_MSG_RQLINE_END:
1268 /* check for HTTP/0.9 request : no version information available.
1269 * msg->sol must point to the first of CR or LF.
1270 */
1271 if (unlikely(msg->sl.rq.v_l == 0))
1272 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 EXPECT_LF_HERE(ptr, http_msg_invalid);
1275 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001277
Willy Tarreau8973c702007-01-21 23:58:29 +01001278 /*
1279 * Common states below
1280 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 http_msg_hdr_first:
1282 case HTTP_MSG_HDR_FIRST:
1283 msg->sol = ptr;
1284 if (likely(!HTTP_IS_CRLF(*ptr))) {
1285 goto http_msg_hdr_name;
1286 }
1287
1288 if (likely(*ptr == '\r'))
1289 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1290 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 http_msg_hdr_name:
1293 case HTTP_MSG_HDR_NAME:
1294 /* assumes msg->sol points to the first char */
1295 if (likely(HTTP_IS_TOKEN(*ptr)))
1296 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001298 if (likely(*ptr == ':')) {
1299 msg->col = ptr - buf->data;
1300 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1301 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001304
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 http_msg_hdr_l1_sp:
1306 case HTTP_MSG_HDR_L1_SP:
1307 /* assumes msg->sol points to the first char and msg->col to the colon */
1308 if (likely(HTTP_IS_SPHT(*ptr)))
1309 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 /* header value can be basically anything except CR/LF */
1312 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001313
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 if (likely(!HTTP_IS_CRLF(*ptr))) {
1315 goto http_msg_hdr_val;
1316 }
1317
1318 if (likely(*ptr == '\r'))
1319 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1320 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001321
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 http_msg_hdr_l1_lf:
1323 case HTTP_MSG_HDR_L1_LF:
1324 EXPECT_LF_HERE(ptr, http_msg_invalid);
1325 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001326
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001327 http_msg_hdr_l1_lws:
1328 case HTTP_MSG_HDR_L1_LWS:
1329 if (likely(HTTP_IS_SPHT(*ptr))) {
1330 /* replace HT,CR,LF with spaces */
1331 for (; buf->data+msg->sov < ptr; msg->sov++)
1332 buf->data[msg->sov] = ' ';
1333 goto http_msg_hdr_l1_sp;
1334 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001335 /* we had a header consisting only in spaces ! */
1336 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 goto http_msg_complete_header;
1338
1339 http_msg_hdr_val:
1340 case HTTP_MSG_HDR_VAL:
1341 /* assumes msg->sol points to the first char, msg->col to the
1342 * colon, and msg->sov points to the first character of the
1343 * value.
1344 */
1345 if (likely(!HTTP_IS_CRLF(*ptr)))
1346 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 msg->eol = ptr;
1349 /* Note: we could also copy eol into ->eoh so that we have the
1350 * real header end in case it ends with lots of LWS, but is this
1351 * really needed ?
1352 */
1353 if (likely(*ptr == '\r'))
1354 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1355 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 http_msg_hdr_l2_lf:
1358 case HTTP_MSG_HDR_L2_LF:
1359 EXPECT_LF_HERE(ptr, http_msg_invalid);
1360 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 http_msg_hdr_l2_lws:
1363 case HTTP_MSG_HDR_L2_LWS:
1364 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1365 /* LWS: replace HT,CR,LF with spaces */
1366 for (; msg->eol < ptr; msg->eol++)
1367 *msg->eol = ' ';
1368 goto http_msg_hdr_val;
1369 }
1370 http_msg_complete_header:
1371 /*
1372 * It was a new header, so the last one is finished.
1373 * Assumes msg->sol points to the first char, msg->col to the
1374 * colon, msg->sov points to the first character of the value
1375 * and msg->eol to the first CR or LF so we know how the line
1376 * ends. We insert last header into the index.
1377 */
1378 /*
1379 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1380 write(2, msg->sol, msg->eol-msg->sol);
1381 fprintf(stderr,"\n");
1382 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1385 idx, idx->tail) < 0))
1386 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 msg->sol = ptr;
1389 if (likely(!HTTP_IS_CRLF(*ptr))) {
1390 goto http_msg_hdr_name;
1391 }
1392
1393 if (likely(*ptr == '\r'))
1394 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1395 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 http_msg_last_lf:
1398 case HTTP_MSG_LAST_LF:
1399 /* Assumes msg->sol points to the first of either CR or LF */
1400 EXPECT_LF_HERE(ptr, http_msg_invalid);
1401 ptr++;
1402 buf->lr = ptr;
1403 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001404 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 return;
1406#ifdef DEBUG_FULL
1407 default:
1408 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1409 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001410#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 }
1412 http_msg_ood:
1413 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001414 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 buf->lr = ptr;
1416 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_invalid:
1419 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001420 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 return;
1422}
1423
1424/*
1425 * manages the client FSM and its socket. BTW, it also tries to handle the
1426 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1427 * 0 else.
1428 */
1429int process_cli(struct session *t)
1430{
1431 int s = t->srv_state;
1432 int c = t->cli_state;
1433 struct buffer *req = t->req;
1434 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001435
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1437 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001438 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 req->rex.tv_sec, req->rex.tv_usec,
1440 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 if (c == CL_STHEADERS) {
1443 /*
1444 * Now parse the partial (or complete) lines.
1445 * We will check the request syntax, and also join multi-line
1446 * headers. An index of all the lines will be elaborated while
1447 * parsing.
1448 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001452 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 * req->data + req->eoh = end of processed headers / start of current one
1454 * req->data + req->eol = end of current header or line (LF or CRLF)
1455 * req->lr = first non-visited byte
1456 * req->r = end of data
1457 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001460 struct http_txn *txn = &t->txn;
1461 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001465 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 /* 1: we might have to print this header in debug mode */
1468 if (unlikely((global.mode & MODE_DEBUG) &&
1469 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001470 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001472
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001473 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 eol = sol + msg->sl.rq.l;
1475 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001476
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001477 sol += hdr_idx_first_pos(&txn->hdr_idx);
1478 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001481 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001483 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1484 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 }
1486 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001487
Willy Tarreau58f10d72006-12-04 02:26:12 +01001488
1489 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001491 * If not so, we check the FD and buffer states before leaving.
1492 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1494 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001495 *
1496 */
1497
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001498 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 /*
1500 * First, let's catch bad requests.
1501 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001502 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001504
1505 /* 1: Since we are in header mode, if there's no space
1506 * left for headers, we won't be able to free more
1507 * later, so the session will never terminate. We
1508 * must terminate it now.
1509 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 if (unlikely(req->l >= req->rlim - req->data)) {
1511 /* FIXME: check if URI is set and return Status
1512 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001513 */
Willy Tarreau06619262006-12-17 08:37:22 +01001514 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001515 }
1516
1517 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1519 /* read error, or last read : give up. */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001520 tv_eternity(&req->rex);
1521 fd_delete(t->cli_fd);
1522 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001523 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001524 if (!(t->flags & SN_ERR_MASK))
1525 t->flags |= SN_ERR_CLICL;
1526 if (!(t->flags & SN_FINST_MASK))
1527 t->flags |= SN_FINST_R;
1528 return 1;
1529 }
1530
1531 /* 3: has the read timeout expired ? */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001532 else if (unlikely(tv_ms_le2(&req->rex, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001533 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001534 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001535 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001536 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001537 if (!(t->flags & SN_ERR_MASK))
1538 t->flags |= SN_ERR_CLITO;
1539 if (!(t->flags & SN_FINST_MASK))
1540 t->flags |= SN_FINST_R;
1541 return 1;
1542 }
1543
1544 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001545 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001546 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001547 * full. We cannot loop here since stream_sock_read will disable it only if
1548 * req->l == rlim-data
1549 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001550 if (t->fe->clitimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001551 tv_ms_add(&req->rex, &now, t->fe->clitimeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001552 else
1553 tv_eternity(&req->rex);
1554 }
1555 return t->cli_state != CL_STHEADERS;
1556 }
1557
1558
1559 /****************************************************************
1560 * More interesting part now : we know that we have a complete *
1561 * request which at least looks like HTTP. We have an indicator *
1562 * of each header's length, so we can parse them quickly. *
1563 ****************************************************************/
1564
Willy Tarreau9cdde232007-05-02 20:58:19 +02001565 /* ensure we keep this pointer to the beginning of the message */
1566 msg->sol = req->data + msg->som;
1567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 /*
1569 * 1: identify the method
1570 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001571 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001572
1573 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001575 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 if (unlikely((t->fe->monitor_uri_len != 0) &&
1579 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1580 !memcmp(&req->data[msg->sl.rq.u],
1581 t->fe->monitor_uri,
1582 t->fe->monitor_uri_len))) {
1583 /*
1584 * We have found the monitor URI
1585 */
1586 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001587 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 client_retnclose(t, &http_200_chunk);
1589 goto return_prx_cond;
1590 }
1591
1592 /*
1593 * 3: Maybe we have to copy the original REQURI for the logs ?
1594 * Note: we cannot log anymore if the request has been
1595 * classified as invalid.
1596 */
1597 if (unlikely(t->logs.logwait & LW_REQ)) {
1598 /* we have a complete HTTP request that we must log */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001599 if ((txn->uri = pool_alloc(requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 int urilen = msg->sl.rq.l;
1601
1602 if (urilen >= REQURI_LEN)
1603 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001604 memcpy(txn->uri, &req->data[msg->som], urilen);
1605 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606
1607 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001608 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 } else {
1610 Alert("HTTP logging : out of memory.\n");
1611 }
1612 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001613
Willy Tarreau06619262006-12-17 08:37:22 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1616 if (unlikely(msg->sl.rq.v_l == 0)) {
1617 int delta;
1618 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001619 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 cur_end = msg->sol + msg->sl.rq.l;
1621 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (msg->sl.rq.u_l == 0) {
1624 /* if no URI was set, add "/" */
1625 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1626 cur_end += delta;
1627 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001628 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 /* add HTTP version */
1630 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1631 msg->eoh += delta;
1632 cur_end += delta;
1633 cur_end = (char *)http_parse_reqline(msg, req->data,
1634 HTTP_MSG_RQMETH,
1635 msg->sol, cur_end + 1,
1636 NULL, NULL);
1637 if (unlikely(!cur_end))
1638 goto return_bad_req;
1639
1640 /* we have a full HTTP/1.0 request now and we know that
1641 * we have either a CR or an LF at <ptr>.
1642 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001643 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001644 }
1645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646
1647 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001648 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001649 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001650 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001651
1652 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001654 * As opposed to version 1.2, now they will be evaluated in the
1655 * filters order and not in the header order. This means that
1656 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001657 *
1658 * We can now check whether we want to switch to another
1659 * backend, in which case we will re-check the backend's
1660 * filters and various options. In order to support 3-level
1661 * switching, here's how we should proceed :
1662 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001663 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001664 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001665 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001666 * There cannot be any switch from there, so ->be cannot be
1667 * changed anymore.
1668 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001669 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001670 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001671 * The response path will be able to apply either ->be, or
1672 * ->be then ->fe filters in order to match the reverse of
1673 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001674 */
1675
Willy Tarreau06619262006-12-17 08:37:22 +01001676 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001677 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001678 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001679 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001680
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001681 /* first check whether we have some ACLs set to block this request */
1682 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
1683 int ret = acl_exec_cond(cond, cur_proxy, t, txn);
1684 if (cond->pol == ACL_COND_UNLESS)
1685 ret = !ret;
1686
1687 if (ret) {
1688 txn->status = 403;
1689 /* let's log the request time */
1690 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1691 client_retnclose(t, error_message(t, HTTP_ERR_403));
1692 goto return_prx_cond;
1693 }
1694 }
1695
Willy Tarreau06619262006-12-17 08:37:22 +01001696 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001697 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1699 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001700 }
1701
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001702 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1703 /* to ensure correct connection accounting on
1704 * the backend, we count the connection for the
1705 * one managing the queue.
1706 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001707 t->be->beconn++;
1708 if (t->be->beconn > t->be->beconn_max)
1709 t->be->beconn_max = t->be->beconn;
1710 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001711 t->flags |= SN_BE_ASSIGNED;
1712 }
1713
Willy Tarreau06619262006-12-17 08:37:22 +01001714 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001715 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001716 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001717 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001718 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001719 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001720 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001721 goto return_prx_cond;
1722 }
1723
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001725 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 !(t->flags & SN_CONN_CLOSED)) {
1727 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001728 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001730
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001731 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 old_idx = 0;
1733
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001734 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1735 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 cur_ptr = cur_next;
1737 cur_end = cur_ptr + cur_hdr->len;
1738 cur_next = cur_end + cur_hdr->cr + 1;
1739
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001740 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1741 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 /* 3 possibilities :
1743 * - we have already set Connection: close,
1744 * so we remove this line.
1745 * - we have not yet set Connection: close,
1746 * but this line indicates close. We leave
1747 * it untouched and set the flag.
1748 * - we have not yet set Connection: close,
1749 * and this line indicates non-close. We
1750 * replace it.
1751 */
1752 if (t->flags & SN_CONN_CLOSED) {
1753 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001754 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001756 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1757 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 cur_hdr->len = 0;
1759 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001760 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1761 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1762 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 cur_next += delta;
1764 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001765 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 }
1767 t->flags |= SN_CONN_CLOSED;
1768 }
1769 }
1770 old_idx = cur_idx;
1771 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001772 }
1773 /* add request headers from the rule sets in the same order */
1774 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1775 if (unlikely(http_header_add_tail(req,
1776 &txn->req,
1777 &txn->hdr_idx,
1778 rule_set->req_add[cur_idx])) < 0)
1779 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001780 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001781
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001783 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001784 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001785 /* we have to check the URI and auth for this request */
1786 if (stats_check_uri_auth(t, rule_set))
1787 return 1;
1788 }
1789
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001790 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1791 /* No backend was set, but there was a default
1792 * backend set in the frontend, so we use it and
1793 * loop again.
1794 */
1795 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001796 t->be->beconn++;
1797 if (t->be->beconn > t->be->beconn_max)
1798 t->be->beconn_max = t->be->beconn;
1799 t->be->cum_beconn++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001800 t->flags |= SN_BE_ASSIGNED;
1801 }
1802 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001803
Willy Tarreau58f10d72006-12-04 02:26:12 +01001804
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001805 if (!(t->flags & SN_BE_ASSIGNED)) {
1806 /* To ensure correct connection accounting on
1807 * the backend, we count the connection for the
1808 * one managing the queue.
1809 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001810 t->be->beconn++;
1811 if (t->be->beconn > t->be->beconn_max)
1812 t->be->beconn_max = t->be->beconn;
1813 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001814 t->flags |= SN_BE_ASSIGNED;
1815 }
1816
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001817 /*
1818 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001819 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001820 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001821 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001822 */
Willy Tarreau58f10d72006-12-04 02:26:12 +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 Tarreau2a324282006-12-05 00:05:46 +01001827 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001829 * so let's do the same now.
1830 */
1831
1832 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001833 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001834 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001835 }
1836
1837
1838 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001840 * Note that doing so might move headers in the request, but
1841 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001842 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001843 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001844 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001845 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001846
Willy Tarreau58f10d72006-12-04 02:26:12 +01001847
Willy Tarreau2a324282006-12-05 00:05:46 +01001848 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001849 * 9: add X-Forwarded-For if either the frontend or the backend
1850 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001851 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001852 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001853 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001854 /* Add an X-Forwarded-For header unless the source IP is
1855 * in the 'except' network range.
1856 */
1857 if ((!t->fe->except_mask.s_addr ||
1858 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1859 != t->fe->except_net.s_addr) &&
1860 (!t->be->except_mask.s_addr ||
1861 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1862 != t->be->except_net.s_addr)) {
1863 int len;
1864 unsigned char *pn;
1865 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001866
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001867 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1868 pn[0], pn[1], pn[2], pn[3]);
1869
1870 if (unlikely(http_header_add_tail2(req, &txn->req,
1871 &txn->hdr_idx, trash, len)) < 0)
1872 goto return_bad_req;
1873 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001874 }
1875 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001876 /* FIXME: for the sake of completeness, we should also support
1877 * 'except' here, although it is mostly useless in this case.
1878 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001879 int len;
1880 char pn[INET6_ADDRSTRLEN];
1881 inet_ntop(AF_INET6,
1882 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1883 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001884 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1885 if (unlikely(http_header_add_tail2(req, &txn->req,
1886 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001887 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001888 }
1889 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001890
Willy Tarreau2a324282006-12-05 00:05:46 +01001891 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02001893 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01001894 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02001895 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001896 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02001897 if ((unlikely(msg->sl.rq.v_l != 8) ||
1898 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
1899 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001900 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001901 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001902 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001903 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001904
Willy Tarreau2a324282006-12-05 00:05:46 +01001905 /*************************************************************
1906 * OK, that's finished for the headers. We have done what we *
1907 * could. Let's switch to the DATA state. *
1908 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001909
Willy Tarreau2a324282006-12-05 00:05:46 +01001910 t->cli_state = CL_STDATA;
1911 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001912
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001913 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001914
Willy Tarreau2a324282006-12-05 00:05:46 +01001915 if (!t->fe->clitimeout ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001916 (t->srv_state < SV_STDATA && t->be->srvtimeout)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001917 /* If the client has no timeout, or if the server is not ready yet,
1918 * and we know for sure that it can expire, then it's cleaner to
1919 * disable the timeout on the client side so that too low values
1920 * cannot make the sessions abort too early.
1921 *
1922 * FIXME-20050705: the server needs a way to re-enable this time-out
1923 * when it switches its state, otherwise a client can stay connected
1924 * indefinitely. This now seems to be OK.
1925 */
1926 tv_eternity(&req->rex);
1927 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001928
Willy Tarreau2a324282006-12-05 00:05:46 +01001929 /* When a connection is tarpitted, we use the queue timeout for the
1930 * tarpit delay, which currently happens to be the server's connect
1931 * timeout. If unset, then set it to zero because we really want it
1932 * to expire at one moment.
1933 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001934 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001935 t->req->l = 0;
1936 /* flush the request so that we can drop the connection early
1937 * if the client closes first.
1938 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001939 tv_ms_add(&req->cex, &now,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001940 t->be->contimeout ? t->be->contimeout : 0);
Willy Tarreau2a324282006-12-05 00:05:46 +01001941 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001942
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001943 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01001944 goto process_data;
1945
1946 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001947 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001948 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01001949 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001950 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01001951 return_prx_cond:
1952 if (!(t->flags & SN_ERR_MASK))
1953 t->flags |= SN_ERR_PRXCOND;
1954 if (!(t->flags & SN_FINST_MASK))
1955 t->flags |= SN_FINST_R;
1956 return 1;
1957
Willy Tarreaubaaee002006-06-26 02:48:02 +02001958 }
1959 else if (c == CL_STDATA) {
1960 process_data:
1961 /* FIXME: this error handling is partly buggy because we always report
1962 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1963 * or HEADER phase. BTW, it's not logical to expire the client while
1964 * we're waiting for the server to connect.
1965 */
1966 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001967 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001968 tv_eternity(&req->rex);
1969 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001970 fd_delete(t->cli_fd);
1971 t->cli_state = CL_STCLOSE;
1972 if (!(t->flags & SN_ERR_MASK))
1973 t->flags |= SN_ERR_CLICL;
1974 if (!(t->flags & SN_FINST_MASK)) {
1975 if (t->pend_pos)
1976 t->flags |= SN_FINST_Q;
1977 else if (s == SV_STCONN)
1978 t->flags |= SN_FINST_C;
1979 else
1980 t->flags |= SN_FINST_D;
1981 }
1982 return 1;
1983 }
1984 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001985 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001986 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02001987 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001988 t->cli_state = CL_STSHUTR;
1989 return 1;
1990 }
1991 /* last server read and buffer empty */
1992 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001993 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02001994 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001995 shutdown(t->cli_fd, SHUT_WR);
1996 /* We must ensure that the read part is still alive when switching
1997 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02001998 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau73de9892006-11-30 11:40:23 +01001999 if (t->fe->clitimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002000 tv_ms_add(&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 Tarreau42aae5c2007-04-29 17:43:56 +02002006 else if (tv_ms_le2(&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 Tarreau42aae5c2007-04-29 17:43:56 +02002023 else if (tv_ms_le2(&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 Tarreau73de9892006-11-30 11:40:23 +01002030 if (t->fe->clitimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002031 tv_ms_add(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002032
2033 t->cli_state = CL_STSHUTW;
2034 if (!(t->flags & SN_ERR_MASK))
2035 t->flags |= SN_ERR_CLITO;
2036 if (!(t->flags & SN_FINST_MASK)) {
2037 if (t->pend_pos)
2038 t->flags |= SN_FINST_Q;
2039 else if (s == SV_STCONN)
2040 t->flags |= SN_FINST_C;
2041 else
2042 t->flags |= SN_FINST_D;
2043 }
2044 return 1;
2045 }
2046
2047 if (req->l >= req->rlim - req->data) {
2048 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002049 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002050 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002051 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002052 }
2053 } else {
2054 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002055 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreau73de9892006-11-30 11:40:23 +01002056 if (!t->fe->clitimeout ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002057 (t->srv_state < SV_STDATA && t->be->srvtimeout))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002058 /* If the client has no timeout, or if the server not ready yet, and we
2059 * know for sure that it can expire, then it's cleaner to disable the
2060 * timeout on the client side so that too low values cannot make the
2061 * sessions abort too early.
2062 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002063 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002064 else
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002065 tv_ms_add(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002066 }
2067 }
2068
2069 if ((rep->l == 0) ||
2070 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002071 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2072 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002073 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002074 }
2075 } else {
2076 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002077 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2078 /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01002079 if (t->fe->clitimeout) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002080 tv_ms_add(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002081 /* FIXME: to prevent the client from expiring read timeouts during writes,
2082 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002083 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002084 }
2085 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002086 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002087 }
2088 }
2089 return 0; /* other cases change nothing */
2090 }
2091 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002092 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002093 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002094 fd_delete(t->cli_fd);
2095 t->cli_state = CL_STCLOSE;
2096 if (!(t->flags & SN_ERR_MASK))
2097 t->flags |= SN_ERR_CLICL;
2098 if (!(t->flags & SN_FINST_MASK)) {
2099 if (t->pend_pos)
2100 t->flags |= SN_FINST_Q;
2101 else if (s == SV_STCONN)
2102 t->flags |= SN_FINST_C;
2103 else
2104 t->flags |= SN_FINST_D;
2105 }
2106 return 1;
2107 }
2108 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2109 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002110 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002111 fd_delete(t->cli_fd);
2112 t->cli_state = CL_STCLOSE;
2113 return 1;
2114 }
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002115 else if (tv_ms_le2(&rep->wex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002116 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002117 fd_delete(t->cli_fd);
2118 t->cli_state = CL_STCLOSE;
2119 if (!(t->flags & SN_ERR_MASK))
2120 t->flags |= SN_ERR_CLITO;
2121 if (!(t->flags & SN_FINST_MASK)) {
2122 if (t->pend_pos)
2123 t->flags |= SN_FINST_Q;
2124 else if (s == SV_STCONN)
2125 t->flags |= SN_FINST_C;
2126 else
2127 t->flags |= SN_FINST_D;
2128 }
2129 return 1;
2130 }
2131
2132 if (t->flags & SN_SELF_GEN) {
2133 produce_content(t);
2134 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002135 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002136 fd_delete(t->cli_fd);
2137 t->cli_state = CL_STCLOSE;
2138 return 1;
2139 }
2140 }
2141
2142 if ((rep->l == 0)
2143 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002144 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2145 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002146 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002147 }
2148 } else {
2149 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002150 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2151 /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01002152 if (t->fe->clitimeout) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002153 tv_ms_add(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002154 /* FIXME: to prevent the client from expiring read timeouts during writes,
2155 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002156 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002157 }
2158 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002159 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002160 }
2161 }
2162 return 0;
2163 }
2164 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002165 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002166 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002167 fd_delete(t->cli_fd);
2168 t->cli_state = CL_STCLOSE;
2169 if (!(t->flags & SN_ERR_MASK))
2170 t->flags |= SN_ERR_CLICL;
2171 if (!(t->flags & SN_FINST_MASK)) {
2172 if (t->pend_pos)
2173 t->flags |= SN_FINST_Q;
2174 else if (s == SV_STCONN)
2175 t->flags |= SN_FINST_C;
2176 else
2177 t->flags |= SN_FINST_D;
2178 }
2179 return 1;
2180 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002181 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002182 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002183 fd_delete(t->cli_fd);
2184 t->cli_state = CL_STCLOSE;
2185 return 1;
2186 }
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002187 else if (tv_ms_le2(&req->rex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002188 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002189 fd_delete(t->cli_fd);
2190 t->cli_state = CL_STCLOSE;
2191 if (!(t->flags & SN_ERR_MASK))
2192 t->flags |= SN_ERR_CLITO;
2193 if (!(t->flags & SN_FINST_MASK)) {
2194 if (t->pend_pos)
2195 t->flags |= SN_FINST_Q;
2196 else if (s == SV_STCONN)
2197 t->flags |= SN_FINST_C;
2198 else
2199 t->flags |= SN_FINST_D;
2200 }
2201 return 1;
2202 }
2203 else if (req->l >= req->rlim - req->data) {
2204 /* no room to read more data */
2205
2206 /* FIXME-20050705: is it possible for a client to maintain a session
2207 * after the timeout by sending more data after it receives a close ?
2208 */
2209
Willy Tarreau66319382007-04-08 17:17:37 +02002210 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002211 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002212 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002213 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2214 }
2215 } else {
2216 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002217 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreau73de9892006-11-30 11:40:23 +01002218 if (t->fe->clitimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002219 tv_ms_add(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002220 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002221 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002222 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2223 }
2224 }
2225 return 0;
2226 }
2227 else { /* CL_STCLOSE: nothing to do */
2228 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2229 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002230 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 +02002231 write(1, trash, len);
2232 }
2233 return 0;
2234 }
2235 return 0;
2236}
2237
2238
2239/*
2240 * manages the server FSM and its socket. It returns 1 if a state has changed
2241 * (and a resync may be needed), 0 else.
2242 */
2243int process_srv(struct session *t)
2244{
2245 int s = t->srv_state;
2246 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002247 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002248 struct buffer *req = t->req;
2249 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002250 int conn_err;
2251
2252#ifdef DEBUG_FULL
2253 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2254#endif
2255 //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 +02002256 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2257 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002258 //);
2259 if (s == SV_STIDLE) {
2260 if (c == CL_STHEADERS)
2261 return 0; /* stay in idle, waiting for data to reach the client side */
2262 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2263 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002264 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002265 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002266 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002267 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268 /* note that this must not return any error because it would be able to
2269 * overwrite the client_retnclose() output.
2270 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002271 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002272 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002273 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002274 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 +02002275
2276 return 1;
2277 }
2278 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002279 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002280 /* This connection is being tarpitted. The CLIENT side has
2281 * already set the connect expiration date to the right
2282 * timeout. We just have to check that it has not expired.
2283 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002284 if (!tv_ms_le2(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002285 return 0;
2286
2287 /* We will set the queue timer to the time spent, just for
2288 * logging purposes. We fake a 500 server error, so that the
2289 * attacker will not suspect his connection has been tarpitted.
2290 * It will not cause trouble to the logs because we can exclude
2291 * the tarpitted connections by filtering on the 'PT' status flags.
2292 */
2293 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002294 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002295 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002296 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002297 return 1;
2298 }
2299
Willy Tarreaubaaee002006-06-26 02:48:02 +02002300 /* Right now, we will need to create a connection to the server.
2301 * We might already have tried, and got a connection pending, in
2302 * which case we will not do anything till it's pending. It's up
2303 * to any other session to release it and wake us up again.
2304 */
2305 if (t->pend_pos) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002306 if (!tv_ms_le2(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002307 return 0;
2308 else {
2309 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002310 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002311 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002312 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002313 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002314 if (t->srv)
2315 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002316 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002317 return 1;
2318 }
2319 }
2320
2321 do {
2322 /* first, get a connection */
2323 if (srv_redispatch_connect(t))
2324 return t->srv_state != SV_STIDLE;
2325
2326 /* try to (re-)connect to the server, and fail if we expire the
2327 * number of retries.
2328 */
2329 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002330 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002331 return t->srv_state != SV_STIDLE;
2332 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002333 } while (1);
2334 }
2335 }
2336 else if (s == SV_STCONN) { /* connection in progress */
2337 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2338 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002339 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2340 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002341 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002342 fd_delete(t->srv_fd);
2343 if (t->srv)
2344 t->srv->cur_sess--;
2345
2346 /* note that this must not return any error because it would be able to
2347 * overwrite the client_retnclose() output.
2348 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002349 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002350 return 1;
2351 }
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002352 if (!(req->flags & BF_WRITE_STATUS) && !tv_ms_le2(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002353 //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 +02002354 return 0; /* nothing changed */
2355 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002356 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002357 /* timeout, asynchronous connect error or first write error */
2358 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2359
2360 fd_delete(t->srv_fd);
2361 if (t->srv)
2362 t->srv->cur_sess--;
2363
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002364 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002365 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2366 else
2367 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2368
2369 /* ensure that we have enough retries left */
2370 if (srv_count_retry_down(t, conn_err))
2371 return 1;
2372
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002373 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002374 /* We're on our last chance, and the REDISP option was specified.
2375 * We will ignore cookie and force to balance or use the dispatcher.
2376 */
2377 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002378 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002379 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002380
2381 if (t->srv)
2382 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002383 t->be->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002384
2385 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2386 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002387 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002388
2389 /* first, get a connection */
2390 if (srv_redispatch_connect(t))
2391 return t->srv_state != SV_STIDLE;
2392 }
2393
Willy Tarreaubaaee002006-06-26 02:48:02 +02002394 do {
2395 /* Now we will try to either reconnect to the same server or
2396 * connect to another server. If the connection gets queued
2397 * because all servers are saturated, then we will go back to
2398 * the SV_STIDLE state.
2399 */
2400 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002401 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402 return t->srv_state != SV_STCONN;
2403 }
2404
2405 /* we need to redispatch the connection to another server */
2406 if (srv_redispatch_connect(t))
2407 return t->srv_state != SV_STCONN;
2408 } while (1);
2409 }
2410 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002411 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002412
2413 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2414 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002415 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002416 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002417 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002418 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002419 if (t->be->srvtimeout) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002420 tv_ms_add(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002421 /* FIXME: to prevent the server from expiring read timeouts during writes,
2422 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002423 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002424 }
2425 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002426 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002427 }
2428
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002429 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002430 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002431 if (t->be->srvtimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002432 tv_ms_add(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002433 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002434 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002435
2436 t->srv_state = SV_STDATA;
2437 if (t->srv)
2438 t->srv->cum_sess++;
2439 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2440
2441 /* if the user wants to log as soon as possible, without counting
2442 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002443 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002444 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002445 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002446 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002447#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002448 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002449 /* TCP splicing supported by both FE and BE */
2450 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2451 }
2452#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002453 }
2454 else {
2455 t->srv_state = SV_STHEADERS;
2456 if (t->srv)
2457 t->srv->cum_sess++;
2458 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002459 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2460 /* reset hdr_idx which was already initialized by the request.
2461 * right now, the http parser does it.
2462 * hdr_idx_init(&t->txn.hdr_idx);
2463 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002464 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002465 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002466 return 1;
2467 }
2468 }
2469 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002470 /*
2471 * Now parse the partial (or complete) lines.
2472 * We will check the response syntax, and also join multi-line
2473 * headers. An index of all the lines will be elaborated while
2474 * parsing.
2475 *
2476 * For the parsing, we use a 28 states FSM.
2477 *
2478 * Here is the information we currently have :
2479 * rep->data + req->som = beginning of response
2480 * rep->data + req->eoh = end of processed headers / start of current one
2481 * rep->data + req->eol = end of current header or line (LF or CRLF)
2482 * rep->lr = first non-visited byte
2483 * rep->r = end of data
2484 */
2485
2486 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002487 struct http_msg *msg = &txn->rsp;
2488 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002489
Willy Tarreaua15645d2007-03-18 16:22:39 +01002490 if (likely(rep->lr < rep->r))
2491 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002492
Willy Tarreaua15645d2007-03-18 16:22:39 +01002493 /* 1: we might have to print this header in debug mode */
2494 if (unlikely((global.mode & MODE_DEBUG) &&
2495 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2496 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2497 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002498
Willy Tarreaua15645d2007-03-18 16:22:39 +01002499 sol = rep->data + msg->som;
2500 eol = sol + msg->sl.rq.l;
2501 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002502
Willy Tarreaua15645d2007-03-18 16:22:39 +01002503 sol += hdr_idx_first_pos(&txn->hdr_idx);
2504 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002505
Willy Tarreaua15645d2007-03-18 16:22:39 +01002506 while (cur_idx) {
2507 eol = sol + txn->hdr_idx.v[cur_idx].len;
2508 debug_hdr("srvhdr", t, sol, eol);
2509 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2510 cur_idx = txn->hdr_idx.v[cur_idx].next;
2511 }
2512 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002513
Willy Tarreaubaaee002006-06-26 02:48:02 +02002514
Willy Tarreau66319382007-04-08 17:17:37 +02002515 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002516 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002517 * full. We cannot loop here since stream_sock_read will disable it only if
2518 * rep->l == rlim-data
2519 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002520 if (t->be->srvtimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002521 tv_ms_add(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002522 else
2523 tv_eternity(&rep->rex);
2524 }
2525
2526
2527 /*
2528 * Now we quickly check if we have found a full valid response.
2529 * If not so, we check the FD and buffer states before leaving.
2530 * A full response is indicated by the fact that we have seen
2531 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2532 * responses are checked first.
2533 *
2534 * Depending on whether the client is still there or not, we
2535 * may send an error response back or not. Note that normally
2536 * we should only check for HTTP status there, and check I/O
2537 * errors somewhere else.
2538 */
2539
2540 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2541
2542 /* Invalid response, or read error or write error */
2543 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2544 (req->flags & BF_WRITE_ERROR) ||
2545 (rep->flags & BF_READ_ERROR))) {
2546 tv_eternity(&rep->rex);
2547 tv_eternity(&req->wex);
2548 fd_delete(t->srv_fd);
2549 if (t->srv) {
2550 t->srv->cur_sess--;
2551 t->srv->failed_resp++;
2552 }
2553 t->be->failed_resp++;
2554 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002555 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002556 client_return(t, error_message(t, HTTP_ERR_502));
2557 if (!(t->flags & SN_ERR_MASK))
2558 t->flags |= SN_ERR_SRVCL;
2559 if (!(t->flags & SN_FINST_MASK))
2560 t->flags |= SN_FINST_H;
2561 /* We used to have a free connection slot. Since we'll never use it,
2562 * we have to inform the server that it may be used by another session.
2563 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002564 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002565 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002566
Willy Tarreaua15645d2007-03-18 16:22:39 +01002567 return 1;
2568 }
2569
2570 /* end of client write or end of server read.
2571 * since we are in header mode, if there's no space left for headers, we
2572 * won't be able to free more later, so the session will never terminate.
2573 */
2574 else if (unlikely(rep->flags & BF_READ_NULL ||
2575 c == CL_STSHUTW || c == CL_STCLOSE ||
2576 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002577 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002578 tv_eternity(&rep->rex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002579 t->srv_state = SV_STSHUTR;
2580 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2581 return 1;
2582 }
2583
2584 /* read timeout : return a 504 to the client.
2585 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002586 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002587 tv_ms_le2(&rep->rex, &now))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002588 tv_eternity(&rep->rex);
2589 tv_eternity(&req->wex);
2590 fd_delete(t->srv_fd);
2591 if (t->srv) {
2592 t->srv->cur_sess--;
2593 t->srv->failed_resp++;
2594 }
2595 t->be->failed_resp++;
2596 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002597 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002598 client_return(t, error_message(t, HTTP_ERR_504));
2599 if (!(t->flags & SN_ERR_MASK))
2600 t->flags |= SN_ERR_SRVTO;
2601 if (!(t->flags & SN_FINST_MASK))
2602 t->flags |= SN_FINST_H;
2603 /* We used to have a free connection slot. Since we'll never use it,
2604 * we have to inform the server that it may be used by another session.
2605 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002606 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002607 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002608 return 1;
2609 }
2610
2611 /* last client read and buffer empty */
2612 /* FIXME!!! here, we don't want to switch to SHUTW if the
2613 * client shuts read too early, because we may still have
2614 * some work to do on the headers.
2615 * The side-effect is that if the client completely closes its
2616 * connection during SV_STHEADER, the connection to the server
2617 * is kept until a response comes back or the timeout is reached.
2618 */
2619 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2620 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002621 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002622 tv_eternity(&req->wex);
2623
2624 /* We must ensure that the read part is still
2625 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002626 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002627 if (t->be->srvtimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002628 tv_ms_add(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002629
2630 shutdown(t->srv_fd, SHUT_WR);
2631 t->srv_state = SV_STSHUTW;
2632 return 1;
2633 }
2634
2635 /* write timeout */
2636 /* FIXME!!! here, we don't want to switch to SHUTW if the
2637 * client shuts read too early, because we may still have
2638 * some work to do on the headers.
2639 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002640 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002641 tv_ms_le2(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002642 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002643 tv_eternity(&req->wex);
2644 shutdown(t->srv_fd, SHUT_WR);
2645 /* We must ensure that the read part is still alive
2646 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002647 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002648 if (t->be->srvtimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002649 tv_ms_add(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002650
2651 t->srv_state = SV_STSHUTW;
2652 if (!(t->flags & SN_ERR_MASK))
2653 t->flags |= SN_ERR_SRVTO;
2654 if (!(t->flags & SN_FINST_MASK))
2655 t->flags |= SN_FINST_H;
2656 return 1;
2657 }
2658
2659 /*
2660 * And now the non-error cases.
2661 */
2662
2663 /* Data remaining in the request buffer.
2664 * This happens during the first pass here, and during
2665 * long posts.
2666 */
2667 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002668 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2669 /* restart writing */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002670 if (t->be->srvtimeout) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002671 tv_ms_add(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002672 /* FIXME: to prevent the server from expiring read timeouts during writes,
2673 * we refresh it. */
2674 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002675 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002676 else
2677 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002678 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002679 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002680
Willy Tarreaua15645d2007-03-18 16:22:39 +01002681 /* nothing left in the request buffer */
2682 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002683 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2684 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002685 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002686 }
2687 }
2688
2689 return t->srv_state != SV_STHEADERS;
2690 }
2691
2692
2693 /*****************************************************************
2694 * More interesting part now : we know that we have a complete *
2695 * response which at least looks like HTTP. We have an indicator *
2696 * of each header's length, so we can parse them quickly. *
2697 ****************************************************************/
2698
Willy Tarreau9cdde232007-05-02 20:58:19 +02002699 /* ensure we keep this pointer to the beginning of the message */
2700 msg->sol = rep->data + msg->som;
2701
Willy Tarreaua15645d2007-03-18 16:22:39 +01002702 /*
2703 * 1: get the status code and check for cacheability.
2704 */
2705
2706 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002707 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002708
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002709 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002710 case 200:
2711 case 203:
2712 case 206:
2713 case 300:
2714 case 301:
2715 case 410:
2716 /* RFC2616 @13.4:
2717 * "A response received with a status code of
2718 * 200, 203, 206, 300, 301 or 410 MAY be stored
2719 * by a cache (...) unless a cache-control
2720 * directive prohibits caching."
2721 *
2722 * RFC2616 @9.5: POST method :
2723 * "Responses to this method are not cacheable,
2724 * unless the response includes appropriate
2725 * Cache-Control or Expires header fields."
2726 */
2727 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002728 unlikely(t->be->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002729 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002730 break;
2731 default:
2732 break;
2733 }
2734
2735 /*
2736 * 2: we may need to capture headers
2737 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002738 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002739 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002740 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002741
2742 /*
2743 * 3: we will have to evaluate the filters.
2744 * As opposed to version 1.2, now they will be evaluated in the
2745 * filters order and not in the header order. This means that
2746 * each filter has to be validated among all headers.
2747 *
2748 * Filters are tried with ->be first, then with ->fe if it is
2749 * different from ->be.
2750 */
2751
2752 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2753
2754 cur_proxy = t->be;
2755 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002756 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002757
2758 /* try headers filters */
2759 if (rule_set->rsp_exp != NULL) {
2760 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2761 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002762 if (t->srv) {
2763 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002764 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002765 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002766 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002767 return_srv_prx_502:
2768 tv_eternity(&rep->rex);
2769 tv_eternity(&req->wex);
2770 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002771 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002772 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002773 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002774 if (!(t->flags & SN_ERR_MASK))
2775 t->flags |= SN_ERR_PRXCOND;
2776 if (!(t->flags & SN_FINST_MASK))
2777 t->flags |= SN_FINST_H;
2778 /* We used to have a free connection slot. Since we'll never use it,
2779 * we have to inform the server that it may be used by another session.
2780 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002781 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002782 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002783 return 1;
2784 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002785 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002786
Willy Tarreaua15645d2007-03-18 16:22:39 +01002787 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002788 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002789 if (t->srv) {
2790 t->srv->cur_sess--;
2791 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002792 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002793 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002794 goto return_srv_prx_502;
2795 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002796
Willy Tarreaua15645d2007-03-18 16:22:39 +01002797 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002798 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002799 !(t->flags & SN_CONN_CLOSED)) {
2800 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002801 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002802 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002803
Willy Tarreaua15645d2007-03-18 16:22:39 +01002804 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2805 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002806
Willy Tarreaua15645d2007-03-18 16:22:39 +01002807 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2808 cur_hdr = &txn->hdr_idx.v[cur_idx];
2809 cur_ptr = cur_next;
2810 cur_end = cur_ptr + cur_hdr->len;
2811 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002812
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002813 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2814 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002815 /* 3 possibilities :
2816 * - we have already set Connection: close,
2817 * so we remove this line.
2818 * - we have not yet set Connection: close,
2819 * but this line indicates close. We leave
2820 * it untouched and set the flag.
2821 * - we have not yet set Connection: close,
2822 * and this line indicates non-close. We
2823 * replace it.
2824 */
2825 if (t->flags & SN_CONN_CLOSED) {
2826 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2827 txn->rsp.eoh += delta;
2828 cur_next += delta;
2829 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2830 txn->hdr_idx.used--;
2831 cur_hdr->len = 0;
2832 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002833 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2834 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2835 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002836 cur_next += delta;
2837 cur_hdr->len += delta;
2838 txn->rsp.eoh += delta;
2839 }
2840 t->flags |= SN_CONN_CLOSED;
2841 }
2842 }
2843 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002844 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002845 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002846
Willy Tarreaua15645d2007-03-18 16:22:39 +01002847 /* add response headers from the rule sets in the same order */
2848 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002849 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2850 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002851 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002852 }
2853
Willy Tarreaua15645d2007-03-18 16:22:39 +01002854 /* check whether we're already working on the frontend */
2855 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002856 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002857 cur_proxy = t->fe;
2858 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002859
Willy Tarreaua15645d2007-03-18 16:22:39 +01002860 /*
2861 * 4: check for server cookie.
2862 */
2863 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002864
Willy Tarreaua15645d2007-03-18 16:22:39 +01002865 /*
2866 * 5: add server cookie in the response if needed
2867 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002868 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2869 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002870 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002871
Willy Tarreaua15645d2007-03-18 16:22:39 +01002872 /* the server is known, it's not the one the client requested, we have to
2873 * insert a set-cookie here, except if we want to insert only on POST
2874 * requests and this one isn't. Note that servers which don't have cookies
2875 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002876 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002877 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002878 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01002879 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002880
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002881 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2882 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002883 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002884 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002885
Willy Tarreaua15645d2007-03-18 16:22:39 +01002886 /* Here, we will tell an eventual cache on the client side that we don't
2887 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2888 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2889 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2890 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002891 if (t->be->options & PR_O_COOK_NOC) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002892 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2893 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002894 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002896 }
2897
2898
2899 /*
2900 * 6: check for cache-control or pragma headers.
2901 */
2902 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002903
Willy Tarreaubaaee002006-06-26 02:48:02 +02002904
Willy Tarreaua15645d2007-03-18 16:22:39 +01002905 /*
2906 * 7: check if result will be cacheable with a cookie.
2907 * We'll block the response if security checks have caught
2908 * nasty things such as a cacheable cookie.
2909 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002910 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2911 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002912 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002913
Willy Tarreaua15645d2007-03-18 16:22:39 +01002914 /* we're in presence of a cacheable response containing
2915 * a set-cookie header. We'll block it as requested by
2916 * the 'checkcache' option, and send an alert.
2917 */
2918 if (t->srv) {
2919 t->srv->cur_sess--;
2920 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002921 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002922 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002923
Willy Tarreaua15645d2007-03-18 16:22:39 +01002924 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002925 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002926 send_log(t->be, LOG_ALERT,
2927 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002928 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002929 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002930 }
2931
Willy Tarreaua15645d2007-03-18 16:22:39 +01002932 /*
2933 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002934 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002935 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002936 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002937 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002938 if ((unlikely(msg->sl.st.v_l != 8) ||
2939 unlikely(req->data[msg->som + 7] != '0')) &&
2940 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002941 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002942 goto return_bad_resp;
2943 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002944 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002945
Willy Tarreaubaaee002006-06-26 02:48:02 +02002946
Willy Tarreaua15645d2007-03-18 16:22:39 +01002947 /*************************************************************
2948 * OK, that's finished for the headers. We have done what we *
2949 * could. Let's switch to the DATA state. *
2950 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002951
Willy Tarreaua15645d2007-03-18 16:22:39 +01002952 t->srv_state = SV_STDATA;
2953 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002954 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002955
2956 /* client connection already closed or option 'forceclose' required :
2957 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002958 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002959 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002960 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002961 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002962 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002963
2964 /* We must ensure that the read part is still alive when switching
2965 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002966 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002967 if (t->be->srvtimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002968 tv_ms_add(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002969
Willy Tarreaua15645d2007-03-18 16:22:39 +01002970 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002971 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002972 }
2973
Willy Tarreaua15645d2007-03-18 16:22:39 +01002974#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002975 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002976 /* TCP splicing supported by both FE and BE */
2977 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002978 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002979#endif
2980 /* if the user wants to log as soon as possible, without counting
2981 bytes from the server, then this is the right moment. */
2982 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2983 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002984 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02002985 if (t->fe->to_log & LW_REQ)
2986 http_sess_log(t);
2987 else
2988 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002989 }
2990
Willy Tarreaua15645d2007-03-18 16:22:39 +01002991 /* Note: we must not try to cheat by jumping directly to DATA,
2992 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002993 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002994
2995 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002996 }
2997 else if (s == SV_STDATA) {
2998 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002999 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003000 tv_eternity(&rep->rex);
3001 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003002 fd_delete(t->srv_fd);
3003 if (t->srv) {
3004 t->srv->cur_sess--;
3005 t->srv->failed_resp++;
3006 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003007 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003008 t->srv_state = SV_STCLOSE;
3009 if (!(t->flags & SN_ERR_MASK))
3010 t->flags |= SN_ERR_SRVCL;
3011 if (!(t->flags & SN_FINST_MASK))
3012 t->flags |= SN_FINST_D;
3013 /* We used to have a free connection slot. Since we'll never use it,
3014 * we have to inform the server that it may be used by another session.
3015 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003016 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003017 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003018
3019 return 1;
3020 }
3021 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003022 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003023 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003024 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003025 t->srv_state = SV_STSHUTR;
3026 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3027 return 1;
3028 }
3029 /* end of client read and no more data to send */
3030 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003031 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003032 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003033 shutdown(t->srv_fd, SHUT_WR);
3034 /* We must ensure that the read part is still alive when switching
3035 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003036 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003037 if (t->be->srvtimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003038 tv_ms_add(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003039
3040 t->srv_state = SV_STSHUTW;
3041 return 1;
3042 }
3043 /* read timeout */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003044 else if (tv_ms_le2(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003045 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003046 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003047 t->srv_state = SV_STSHUTR;
3048 if (!(t->flags & SN_ERR_MASK))
3049 t->flags |= SN_ERR_SRVTO;
3050 if (!(t->flags & SN_FINST_MASK))
3051 t->flags |= SN_FINST_D;
3052 return 1;
3053 }
3054 /* write timeout */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003055 else if (tv_ms_le2(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003056 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003057 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003058 shutdown(t->srv_fd, SHUT_WR);
3059 /* We must ensure that the read part is still alive when switching
3060 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003061 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003062 if (t->be->srvtimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003063 tv_ms_add(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003064 t->srv_state = SV_STSHUTW;
3065 if (!(t->flags & SN_ERR_MASK))
3066 t->flags |= SN_ERR_SRVTO;
3067 if (!(t->flags & SN_FINST_MASK))
3068 t->flags |= SN_FINST_D;
3069 return 1;
3070 }
3071
3072 /* recompute request time-outs */
3073 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003074 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3075 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003076 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003077 }
3078 }
3079 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003080 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3081 /* restart writing */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003082 if (t->be->srvtimeout) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003083 tv_ms_add(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003084 /* FIXME: to prevent the server from expiring read timeouts during writes,
3085 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003086 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003087 }
3088 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003089 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003090 }
3091 }
3092
3093 /* recompute response time-outs */
3094 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003095 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003096 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003097 }
3098 }
3099 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003100 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003101 if (t->be->srvtimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003102 tv_ms_add(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003103 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003104 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003105 }
3106 }
3107
3108 return 0; /* other cases change nothing */
3109 }
3110 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003111 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003112 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003113 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003114 fd_delete(t->srv_fd);
3115 if (t->srv) {
3116 t->srv->cur_sess--;
3117 t->srv->failed_resp++;
3118 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003119 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003120 //close(t->srv_fd);
3121 t->srv_state = SV_STCLOSE;
3122 if (!(t->flags & SN_ERR_MASK))
3123 t->flags |= SN_ERR_SRVCL;
3124 if (!(t->flags & SN_FINST_MASK))
3125 t->flags |= SN_FINST_D;
3126 /* We used to have a free connection slot. Since we'll never use it,
3127 * we have to inform the server that it may be used by another session.
3128 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003129 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003130 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003131
3132 return 1;
3133 }
3134 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003135 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003136 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003137 fd_delete(t->srv_fd);
3138 if (t->srv)
3139 t->srv->cur_sess--;
3140 //close(t->srv_fd);
3141 t->srv_state = SV_STCLOSE;
3142 /* We used to have a free connection slot. Since we'll never use it,
3143 * we have to inform the server that it may be used by another session.
3144 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003145 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003146 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003147
3148 return 1;
3149 }
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003150 else if (tv_ms_le2(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003151 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003152 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003153 fd_delete(t->srv_fd);
3154 if (t->srv)
3155 t->srv->cur_sess--;
3156 //close(t->srv_fd);
3157 t->srv_state = SV_STCLOSE;
3158 if (!(t->flags & SN_ERR_MASK))
3159 t->flags |= SN_ERR_SRVTO;
3160 if (!(t->flags & SN_FINST_MASK))
3161 t->flags |= SN_FINST_D;
3162 /* We used to have a free connection slot. Since we'll never use it,
3163 * we have to inform the server that it may be used by another session.
3164 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003165 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003166 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003167
3168 return 1;
3169 }
3170 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003171 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3172 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003173 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003174 }
3175 }
3176 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003177 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3178 /* restart writing */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003179 if (t->be->srvtimeout) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003180 tv_ms_add(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003181 /* FIXME: to prevent the server from expiring read timeouts during writes,
3182 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003183 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003184 }
3185 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003186 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003187 }
3188 }
3189 return 0;
3190 }
3191 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003192 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003193 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003194 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003195 fd_delete(t->srv_fd);
3196 if (t->srv) {
3197 t->srv->cur_sess--;
3198 t->srv->failed_resp++;
3199 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003200 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003201 //close(t->srv_fd);
3202 t->srv_state = SV_STCLOSE;
3203 if (!(t->flags & SN_ERR_MASK))
3204 t->flags |= SN_ERR_SRVCL;
3205 if (!(t->flags & SN_FINST_MASK))
3206 t->flags |= SN_FINST_D;
3207 /* We used to have a free connection slot. Since we'll never use it,
3208 * we have to inform the server that it may be used by another session.
3209 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003210 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003211 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003212
3213 return 1;
3214 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003215 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003216 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003217 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003218 fd_delete(t->srv_fd);
3219 if (t->srv)
3220 t->srv->cur_sess--;
3221 //close(t->srv_fd);
3222 t->srv_state = SV_STCLOSE;
3223 /* We used to have a free connection slot. Since we'll never use it,
3224 * we have to inform the server that it may be used by another session.
3225 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003226 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003227 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003228
3229 return 1;
3230 }
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003231 else if (tv_ms_le2(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003232 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003233 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234 fd_delete(t->srv_fd);
3235 if (t->srv)
3236 t->srv->cur_sess--;
3237 //close(t->srv_fd);
3238 t->srv_state = SV_STCLOSE;
3239 if (!(t->flags & SN_ERR_MASK))
3240 t->flags |= SN_ERR_SRVTO;
3241 if (!(t->flags & SN_FINST_MASK))
3242 t->flags |= SN_FINST_D;
3243 /* We used to have a free connection slot. Since we'll never use it,
3244 * we have to inform the server that it may be used by another session.
3245 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003246 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003247 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003248
3249 return 1;
3250 }
3251 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003252 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003253 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003254 }
3255 }
3256 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003257 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003258 if (t->be->srvtimeout)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003259 tv_ms_add(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003260 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003261 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003262 }
3263 }
3264 return 0;
3265 }
3266 else { /* SV_STCLOSE : nothing to do */
3267 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3268 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003269 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003270 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003271 write(1, trash, len);
3272 }
3273 return 0;
3274 }
3275 return 0;
3276}
3277
3278
3279/*
3280 * Produces data for the session <s> depending on its source. Expects to be
3281 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3282 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3283 * session, which it uses to keep on being called when there is free space in
3284 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3285 * if it changes the session state from CL_STSHUTR, otherwise 0.
3286 */
3287int produce_content(struct session *s)
3288{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003289 if (s->data_source == DATA_SRC_NONE) {
3290 s->flags &= ~SN_SELF_GEN;
3291 return 1;
3292 }
3293 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003294 /* dump server statistics */
3295 return produce_content_stats(s);
3296 }
3297 else {
3298 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003299 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003300 client_retnclose(s, error_message(s, HTTP_ERR_500));
3301 if (!(s->flags & SN_ERR_MASK))
3302 s->flags |= SN_ERR_PRXCOND;
3303 if (!(s->flags & SN_FINST_MASK))
3304 s->flags |= SN_FINST_R;
3305 s->flags &= ~SN_SELF_GEN;
3306 return 1;
3307 }
3308}
3309
3310
3311/*
3312 * Produces statistics data for the session <s>. Expects to be called with
3313 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3314 * flag from the session, which it uses to keep on being called when there is
3315 * free space in the buffer, of simply by letting an empty buffer upon return.
3316 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3317 */
3318int produce_content_stats(struct session *s)
3319{
3320 struct buffer *rep = s->rep;
3321 struct proxy *px;
3322 struct chunk msg;
3323 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003324
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003325 msg.len = 0;
3326 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003327
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003328 switch (s->data_state) {
3329 case DATA_ST_INIT:
3330 /* the function had not been called yet */
3331 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003332
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003333 chunk_printf(&msg, sizeof(trash),
3334 "HTTP/1.0 200 OK\r\n"
3335 "Cache-Control: no-cache\r\n"
3336 "Connection: close\r\n"
3337 "Content-Type: text/html\r\n"
3338 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003339
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003340 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003341 client_retnclose(s, &msg); // send the start of the response.
3342 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003343
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003344 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3345 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3346 if (!(s->flags & SN_FINST_MASK))
3347 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003348
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003349 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003350 /* that's all we return in case of HEAD request */
3351 s->data_state = DATA_ST_FIN;
3352 s->flags &= ~SN_SELF_GEN;
3353 return 1;
3354 }
3355
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003356 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3357 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003358
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003359 case DATA_ST_HEAD:
3360 /* WARNING! This must fit in the first buffer !!! */
3361 chunk_printf(&msg, sizeof(trash),
3362 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3363 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3364 "<style type=\"text/css\"><!--\n"
3365 "body {"
3366 " font-family: helvetica, arial;"
3367 " font-size: 12px;"
3368 " font-weight: normal;"
3369 " color: black;"
3370 " background: white;"
3371 "}\n"
3372 "th,td {"
3373 " font-size: 0.8em;"
3374 " align: center;"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003375 "}\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003376 "h1 {"
3377 " font-size: xx-large;"
3378 " margin-bottom: 0.5em;"
3379 "}\n"
3380 "h2 {"
3381 " font-family: helvetica, arial;"
3382 " font-size: x-large;"
3383 " font-weight: bold;"
3384 " font-style: italic;"
3385 " color: #6020a0;"
3386 " margin-top: 0em;"
3387 " margin-bottom: 0em;"
3388 "}\n"
3389 "h3 {"
3390 " font-family: helvetica, arial;"
3391 " font-size: 16px;"
3392 " font-weight: bold;"
3393 " color: #b00040;"
3394 " background: #e8e8d0;"
3395 " margin-top: 0em;"
3396 " margin-bottom: 0em;"
3397 "}\n"
3398 "li {"
3399 " margin-top: 0.25em;"
3400 " margin-right: 2em;"
3401 "}\n"
3402 ".hr {margin-top: 0.25em;"
3403 " border-color: black;"
3404 " border-bottom-style: solid;"
3405 "}\n"
3406 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3407 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3408 ".total {background: #20D0D0;color: #ffff80;}\n"
3409 ".frontend {background: #e8e8d0;}\n"
3410 ".backend {background: #e8e8d0;}\n"
3411 ".active0 {background: #ff9090;}\n"
3412 ".active1 {background: #ffd020;}\n"
3413 ".active2 {background: #ffffa0;}\n"
3414 ".active3 {background: #c0ffc0;}\n"
3415 ".active4 {background: #e0e0e0;}\n"
3416 ".backup0 {background: #ff9090;}\n"
3417 ".backup1 {background: #ff80ff;}\n"
3418 ".backup2 {background: #c060ff;}\n"
3419 ".backup3 {background: #b0d0ff;}\n"
3420 ".backup4 {background: #e0e0e0;}\n"
3421 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003422 "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 +01003423 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3424 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3425 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3426 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3427 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003428 "-->\n"
3429 "</style></head>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003430
3431 if (buffer_write_chunk(rep, &msg) != 0)
3432 return 0;
3433
3434 s->data_state = DATA_ST_INFO;
3435 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003436
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003437 case DATA_ST_INFO:
3438 up = (now.tv_sec - start_date.tv_sec);
3439
3440 /* WARNING! this has to fit the first packet too.
3441 * We are around 3.5 kB, add adding entries will
3442 * become tricky if we want to support 4kB buffers !
3443 */
3444 chunk_printf(&msg, sizeof(trash),
3445 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3446 PRODUCT_NAME "</a></h1>\n"
3447 "<h2>Statistics Report for pid %d</h2>\n"
3448 "<hr width=\"100%%\" class=\"hr\">\n"
3449 "<h3>&gt; General process information</h3>\n"
3450 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3451 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3452 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3453 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3454 "<b>maxsock = </b> %d<br>\n"
3455 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3456 "</td><td align=\"center\" nowrap>\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003457 "<table class=\"lgd\"><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003458 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3459 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003460 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003461 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3462 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003463 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003464 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3465 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003466 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003467 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3468 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3469 "</tr></table>\n"
3470 "</td>"
3471 "<td align=\"left\" nowrap width=\"1%%\">"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003472 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
3473 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3474 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3475 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003476 "</ul>"
3477 "</td>"
3478 "</tr></table>\n"
3479 "",
3480 pid, pid, global.nbproc,
3481 up / 86400, (up % 86400) / 3600,
3482 (up % 3600) / 60, (up % 60),
3483 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3484 global.rlimit_memmax ? " MB" : "",
3485 global.rlimit_nofile,
3486 global.maxsock,
3487 global.maxconn,
3488 actconn
3489 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003490
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003491 if (buffer_write_chunk(rep, &msg) != 0)
3492 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003493
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003494 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003495
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003496 s->data_ctx.stats.px = proxy;
3497 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3498 s->data_state = DATA_ST_LIST;
3499 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003500
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003501 case DATA_ST_LIST:
3502 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003503 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003504 px = s->data_ctx.stats.px;
3505 /* skip the disabled proxies and non-networked ones */
3506 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3507 if (produce_content_stats_proxy(s, px) == 0)
3508 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003509
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003510 s->data_ctx.stats.px = px->next;
3511 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3512 }
3513 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003514
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003515 s->data_state = DATA_ST_END;
3516 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003517
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003518 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003519 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003520 if (buffer_write_chunk(rep, &msg) != 0)
3521 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003522
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003523 s->data_state = DATA_ST_FIN;
3524 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003525
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003526 case DATA_ST_FIN:
3527 s->flags &= ~SN_SELF_GEN;
3528 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003529
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003530 default:
3531 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003532 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003533 client_retnclose(s, error_message(s, HTTP_ERR_500));
3534 if (!(s->flags & SN_ERR_MASK))
3535 s->flags |= SN_ERR_PRXCOND;
3536 if (!(s->flags & SN_FINST_MASK))
3537 s->flags |= SN_FINST_R;
3538 s->flags &= ~SN_SELF_GEN;
3539 return 1;
3540 }
3541}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003542
Willy Tarreaubaaee002006-06-26 02:48:02 +02003543
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003544/*
3545 * Dumps statistics for a proxy.
3546 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3547 * ot non-zero if everything completed.
3548 */
3549int produce_content_stats_proxy(struct session *s, struct proxy *px)
3550{
3551 struct buffer *rep = s->rep;
3552 struct server *sv;
3553 struct chunk msg;
3554
3555 msg.len = 0;
3556 msg.str = trash;
3557
3558 switch (s->data_ctx.stats.px_st) {
3559 case DATA_ST_PX_INIT:
3560 /* we are on a new proxy */
3561
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003562 if (s->be->uri_auth && s->be->uri_auth->scope) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003563 /* we have a limited scope, we have to check the proxy name */
3564 struct stat_scope *scope;
3565 int len;
3566
3567 len = strlen(px->id);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003568 scope = s->be->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003569
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003570 while (scope) {
3571 /* match exact proxy name */
3572 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3573 break;
3574
3575 /* match '.' which means 'self' proxy */
3576 if (!strcmp(scope->px_id, ".") && px == s->fe)
3577 break;
3578 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003579 }
3580
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003581 /* proxy name not found : don't dump anything */
3582 if (scope == NULL)
3583 return 1;
3584 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003585
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003586 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3587 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003588
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003589 case DATA_ST_PX_TH:
3590 /* print a new table */
3591 chunk_printf(&msg, sizeof(trash),
3592 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3593 "<tr align=\"center\" class=\"titre\">"
3594 "<th colspan=2 class=\"pxname\">%s</th>"
3595 "<th colspan=18 class=\"empty\"></th>"
3596 "</tr>\n"
3597 "<tr align=\"center\" class=\"titre\">"
3598 "<th rowspan=2></th>"
3599 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3600 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3601 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3602 "</tr>\n"
3603 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003604 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3605 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003606 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3607 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3608 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3609 "",
3610 px->id);
3611
3612 if (buffer_write_chunk(rep, &msg) != 0)
3613 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003614
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003615 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3616 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003617
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003618 case DATA_ST_PX_FE:
3619 /* print the frontend */
3620 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003621 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003622 /* name, queue */
3623 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3624 /* sessions : current, max, limit, cumul. */
3625 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3626 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003627 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003628 /* denied: req, resp */
3629 "<td align=right>%d</td><td align=right>%d</td>"
3630 /* errors : request, connect, response */
3631 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3632 /* server status : reflect backend status */
3633 "<td align=center>%s</td>"
3634 /* rest of server: nothing */
3635 "<td align=center colspan=5></td></tr>"
3636 "",
3637 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003638 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003639 px->denied_req, px->denied_resp,
3640 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003641 px->state == PR_STRUN ? "OPEN" :
3642 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003643
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003644 if (buffer_write_chunk(rep, &msg) != 0)
3645 return 0;
3646 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003647
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003648 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3649 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3650 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003651
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003652 case DATA_ST_PX_SV:
3653 /* stats.sv has been initialized above */
3654 while (s->data_ctx.stats.sv != NULL) {
3655 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3656 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003657
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003658 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003659
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003660 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3661 if (!(sv->state & SRV_CHECKED))
3662 sv_state = 4;
3663 else if (sv->state & SRV_RUNNING)
3664 if (sv->health == sv->rise + sv->fall - 1)
3665 sv_state = 3; /* UP */
3666 else
3667 sv_state = 2; /* going down */
3668 else
3669 if (sv->health)
3670 sv_state = 1; /* going up */
3671 else
3672 sv_state = 0; /* DOWN */
3673
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003674 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003675 /* name */
3676 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3677 /* queue : current, max */
3678 "<td align=right>%d</td><td align=right>%d</td>"
3679 /* sessions : current, max, limit, cumul */
3680 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3681 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003682 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003683 /* denied: req, resp */
3684 "<td align=right></td><td align=right>%d</td>"
3685 /* errors : request, connect, response */
3686 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3687 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003688 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003689 sv_state, sv->id,
3690 sv->nbpend, sv->nbpend_max,
3691 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003692 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003693 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003694 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003695
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003696 /* status */
3697 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3698 chunk_printf(&msg, sizeof(trash),
3699 srv_hlt_st[sv_state],
3700 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3701 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3702
Willy Tarreau128e9542007-01-01 22:01:43 +01003703 chunk_printf(&msg, sizeof(trash),
3704 /* weight */
3705 "</td><td>%d</td>"
3706 /* act, bck */
3707 "<td>%s</td><td>%s</td>"
3708 "",
Willy Tarreau417fae02007-03-25 21:16:40 +02003709 sv->uweight,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003710 (sv->state & SRV_BACKUP) ? "-" : "Y",
3711 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003712
3713 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003714 if (sv->state & SRV_CHECKED)
3715 chunk_printf(&msg, sizeof(trash),
3716 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3717 sv->failed_checks, sv->down_trans);
3718 else
3719 chunk_printf(&msg, sizeof(trash),
3720 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003721
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003722 if (buffer_write_chunk(rep, &msg) != 0)
3723 return 0;
3724
3725 s->data_ctx.stats.sv = sv->next;
3726 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003727
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003728 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3729 /* fall through */
3730
3731 case DATA_ST_PX_BE:
3732 /* print the backend */
3733 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003734 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003735 /* name */
3736 "<tr align=center class=\"backend\"><td>Backend</td>"
3737 /* queue : current, max */
3738 "<td align=right>%d</td><td align=right>%d</td>"
3739 /* sessions : current, max, limit, cumul. */
3740 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3741 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003742 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003743 /* denied: req, resp */
3744 "<td align=right>%d</td><td align=right>%d</td>"
3745 /* errors : request, connect, response */
3746 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3747 /* server status : reflect backend status (up/down) : we display UP
3748 * if the backend has known working servers or if it has no server at
3749 * all (eg: for stats). Tthen we display the total weight, number of
3750 * active and backups. */
3751 "<td align=center>%s</td><td align=center>%d</td>"
3752 "<td align=center>%d</td><td align=center>%d</td>"
3753 /* rest of server: nothing */
3754 "<td align=center colspan=2></td></tr>"
3755 "",
3756 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3757 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003758 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003759 px->denied_req, px->denied_resp,
3760 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003761 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3762 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003763
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003764 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003765 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003766 }
3767
3768 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3769 /* fall through */
3770
3771 case DATA_ST_PX_END:
3772 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3773
3774 if (buffer_write_chunk(rep, &msg) != 0)
3775 return 0;
3776
3777 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3778 /* fall through */
3779
3780 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003781 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003782
3783 default:
3784 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003785 return 1;
3786 }
3787}
3788
3789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003790/* Iterate the same filter through all request headers.
3791 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003792 * Since it can manage the switch to another backend, it updates the per-proxy
3793 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003794 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003795int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003796{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003797 char term;
3798 char *cur_ptr, *cur_end, *cur_next;
3799 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003800 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003801 struct hdr_idx_elem *cur_hdr;
3802 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003804 last_hdr = 0;
3805
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003806 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003807 old_idx = 0;
3808
3809 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003810 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003811 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003812 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003813 (exp->action == ACT_ALLOW ||
3814 exp->action == ACT_DENY ||
3815 exp->action == ACT_TARPIT))
3816 return 0;
3817
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003818 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003819 if (!cur_idx)
3820 break;
3821
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003822 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003823 cur_ptr = cur_next;
3824 cur_end = cur_ptr + cur_hdr->len;
3825 cur_next = cur_end + cur_hdr->cr + 1;
3826
3827 /* Now we have one header between cur_ptr and cur_end,
3828 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003829 */
3830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003831 /* The annoying part is that pattern matching needs
3832 * that we modify the contents to null-terminate all
3833 * strings before testing them.
3834 */
3835
3836 term = *cur_end;
3837 *cur_end = '\0';
3838
3839 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3840 switch (exp->action) {
3841 case ACT_SETBE:
3842 /* It is not possible to jump a second time.
3843 * FIXME: should we return an HTTP/500 here so that
3844 * the admin knows there's a problem ?
3845 */
3846 if (t->be != t->fe)
3847 break;
3848
3849 /* Swithing Proxy */
3850 t->be = (struct proxy *) exp->replace;
3851
3852 /* right now, the backend switch is not too much complicated
3853 * because we have associated req_cap and rsp_cap to the
3854 * frontend, and the beconn will be updated later.
3855 */
3856
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003857 t->rep->rto = t->req->wto = t->be->srvtimeout;
3858 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003859 last_hdr = 1;
3860 break;
3861
3862 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003863 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003864 last_hdr = 1;
3865 break;
3866
3867 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003868 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003869 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003870 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003871 break;
3872
3873 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003874 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003875 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003876 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003877 break;
3878
3879 case ACT_REPLACE:
3880 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3881 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3882 /* FIXME: if the user adds a newline in the replacement, the
3883 * index will not be recalculated for now, and the new line
3884 * will not be counted as a new header.
3885 */
3886
3887 cur_end += delta;
3888 cur_next += delta;
3889 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003890 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003891 break;
3892
3893 case ACT_REMOVE:
3894 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3895 cur_next += delta;
3896
3897 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003898 txn->req.eoh += delta;
3899 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3900 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003901 cur_hdr->len = 0;
3902 cur_end = NULL; /* null-term has been rewritten */
3903 break;
3904
3905 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003906 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003907 if (cur_end)
3908 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003909
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003910 /* keep the link from this header to next one in case of later
3911 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003912 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003913 old_idx = cur_idx;
3914 }
3915 return 0;
3916}
3917
3918
3919/* Apply the filter to the request line.
3920 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3921 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003922 * Since it can manage the switch to another backend, it updates the per-proxy
3923 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003924 */
3925int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3926{
3927 char term;
3928 char *cur_ptr, *cur_end;
3929 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003930 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003931 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003932
Willy Tarreau58f10d72006-12-04 02:26:12 +01003933
Willy Tarreau3d300592007-03-18 18:34:41 +01003934 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003935 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003936 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003937 (exp->action == ACT_ALLOW ||
3938 exp->action == ACT_DENY ||
3939 exp->action == ACT_TARPIT))
3940 return 0;
3941 else if (exp->action == ACT_REMOVE)
3942 return 0;
3943
3944 done = 0;
3945
Willy Tarreau9cdde232007-05-02 20:58:19 +02003946 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003947 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003948
3949 /* Now we have the request line between cur_ptr and cur_end */
3950
3951 /* The annoying part is that pattern matching needs
3952 * that we modify the contents to null-terminate all
3953 * strings before testing them.
3954 */
3955
3956 term = *cur_end;
3957 *cur_end = '\0';
3958
3959 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3960 switch (exp->action) {
3961 case ACT_SETBE:
3962 /* It is not possible to jump a second time.
3963 * FIXME: should we return an HTTP/500 here so that
3964 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003965 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003966 if (t->be != t->fe)
3967 break;
3968
3969 /* Swithing Proxy */
3970 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003971
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003972 /* right now, the backend switch is not too much complicated
3973 * because we have associated req_cap and rsp_cap to the
3974 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003975 */
3976
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003977 t->rep->rto = t->req->wto = t->be->srvtimeout;
3978 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003979 done = 1;
3980 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003981
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003982 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003983 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003984 done = 1;
3985 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003987 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003988 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003989 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003990 done = 1;
3991 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003992
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003993 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003994 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003995 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003996 done = 1;
3997 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003998
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003999 case ACT_REPLACE:
4000 *cur_end = term; /* restore the string terminator */
4001 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4002 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4003 /* FIXME: if the user adds a newline in the replacement, the
4004 * index will not be recalculated for now, and the new line
4005 * will not be counted as a new header.
4006 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004007
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004008 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004009 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004010
Willy Tarreau9cdde232007-05-02 20:58:19 +02004011 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004012 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004013 HTTP_MSG_RQMETH,
4014 cur_ptr, cur_end + 1,
4015 NULL, NULL);
4016 if (unlikely(!cur_end))
4017 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004019 /* we have a full request and we know that we have either a CR
4020 * or an LF at <ptr>.
4021 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004022 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4023 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004024 /* there is no point trying this regex on headers */
4025 return 1;
4026 }
4027 }
4028 *cur_end = term; /* restore the string terminator */
4029 return done;
4030}
Willy Tarreau97de6242006-12-27 17:18:38 +01004031
Willy Tarreau58f10d72006-12-04 02:26:12 +01004032
Willy Tarreau58f10d72006-12-04 02:26:12 +01004033
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004034/*
4035 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4036 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004037 * unparsable request. Since it can manage the switch to another backend, it
4038 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004039 */
4040int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4041{
Willy Tarreau3d300592007-03-18 18:34:41 +01004042 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004043 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004044 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004045 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004046
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004047 /*
4048 * The interleaving of transformations and verdicts
4049 * makes it difficult to decide to continue or stop
4050 * the evaluation.
4051 */
4052
Willy Tarreau3d300592007-03-18 18:34:41 +01004053 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004054 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4055 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4056 exp = exp->next;
4057 continue;
4058 }
4059
4060 /* Apply the filter to the request line. */
4061 ret = apply_filter_to_req_line(t, req, exp);
4062 if (unlikely(ret < 0))
4063 return -1;
4064
4065 if (likely(ret == 0)) {
4066 /* The filter did not match the request, it can be
4067 * iterated through all headers.
4068 */
4069 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004070 }
4071 exp = exp->next;
4072 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004073 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004074}
4075
4076
Willy Tarreaua15645d2007-03-18 16:22:39 +01004077
Willy Tarreau58f10d72006-12-04 02:26:12 +01004078/*
4079 * Manager client-side cookie
4080 */
4081void manage_client_side_cookies(struct session *t, struct buffer *req)
4082{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004083 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004084 char *p1, *p2, *p3, *p4;
4085 char *del_colon, *del_cookie, *colon;
4086 int app_cookies;
4087
4088 appsess *asession_temp = NULL;
4089 appsess local_asession;
4090
4091 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004092 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004093
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004094 if (t->be->cookie_name == NULL &&
4095 t->be->appsession_name == NULL &&
4096 t->be->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004097 return;
4098
Willy Tarreau2a324282006-12-05 00:05:46 +01004099 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004100 * we start with the start line.
4101 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004102 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004103 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004104
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004105 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004106 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004107 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004108
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004109 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004110 cur_ptr = cur_next;
4111 cur_end = cur_ptr + cur_hdr->len;
4112 cur_next = cur_end + cur_hdr->cr + 1;
4113
4114 /* We have one full header between cur_ptr and cur_end, and the
4115 * next header starts at cur_next. We're only interested in
4116 * "Cookie:" headers.
4117 */
4118
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004119 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4120 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004121 old_idx = cur_idx;
4122 continue;
4123 }
4124
4125 /* Now look for cookies. Conforming to RFC2109, we have to support
4126 * attributes whose name begin with a '$', and associate them with
4127 * the right cookie, if we want to delete this cookie.
4128 * So there are 3 cases for each cookie read :
4129 * 1) it's a special attribute, beginning with a '$' : ignore it.
4130 * 2) it's a server id cookie that we *MAY* want to delete : save
4131 * some pointers on it (last semi-colon, beginning of cookie...)
4132 * 3) it's an application cookie : we *MAY* have to delete a previous
4133 * "special" cookie.
4134 * At the end of loop, if a "special" cookie remains, we may have to
4135 * remove it. If no application cookie persists in the header, we
4136 * *MUST* delete it
4137 */
4138
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004139 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004140
Willy Tarreau58f10d72006-12-04 02:26:12 +01004141 /* del_cookie == NULL => nothing to be deleted */
4142 del_colon = del_cookie = NULL;
4143 app_cookies = 0;
4144
4145 while (p1 < cur_end) {
4146 /* skip spaces and colons, but keep an eye on these ones */
4147 while (p1 < cur_end) {
4148 if (*p1 == ';' || *p1 == ',')
4149 colon = p1;
4150 else if (!isspace((int)*p1))
4151 break;
4152 p1++;
4153 }
4154
4155 if (p1 == cur_end)
4156 break;
4157
4158 /* p1 is at the beginning of the cookie name */
4159 p2 = p1;
4160 while (p2 < cur_end && *p2 != '=')
4161 p2++;
4162
4163 if (p2 == cur_end)
4164 break;
4165
4166 p3 = p2 + 1; /* skips the '=' sign */
4167 if (p3 == cur_end)
4168 break;
4169
4170 p4 = p3;
4171 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
4172 p4++;
4173
4174 /* here, we have the cookie name between p1 and p2,
4175 * and its value between p3 and p4.
4176 * we can process it :
4177 *
4178 * Cookie: NAME=VALUE;
4179 * | || || |
4180 * | || || +--> p4
4181 * | || |+-------> p3
4182 * | || +--------> p2
4183 * | |+------------> p1
4184 * | +-------------> colon
4185 * +--------------------> cur_ptr
4186 */
4187
4188 if (*p1 == '$') {
4189 /* skip this one */
4190 }
4191 else {
4192 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004193 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004194 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004195 (p4 - p1 >= t->fe->capture_namelen) &&
4196 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004197 int log_len = p4 - p1;
4198
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004199 if ((txn->cli_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004200 Alert("HTTP logging : out of memory.\n");
4201 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004202 if (log_len > t->fe->capture_len)
4203 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004204 memcpy(txn->cli_cookie, p1, log_len);
4205 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004206 }
4207 }
4208
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004209 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4210 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004211 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004212 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004213 char *delim;
4214
4215 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4216 * have the server ID betweek p3 and delim, and the original cookie between
4217 * delim+1 and p4. Otherwise, delim==p4 :
4218 *
4219 * Cookie: NAME=SRV~VALUE;
4220 * | || || | |
4221 * | || || | +--> p4
4222 * | || || +--------> delim
4223 * | || |+-----------> p3
4224 * | || +------------> p2
4225 * | |+----------------> p1
4226 * | +-----------------> colon
4227 * +------------------------> cur_ptr
4228 */
4229
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004230 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004231 for (delim = p3; delim < p4; delim++)
4232 if (*delim == COOKIE_DELIM)
4233 break;
4234 }
4235 else
4236 delim = p4;
4237
4238
4239 /* Here, we'll look for the first running server which supports the cookie.
4240 * This allows to share a same cookie between several servers, for example
4241 * to dedicate backup servers to specific servers only.
4242 * However, to prevent clients from sticking to cookie-less backup server
4243 * when they have incidentely learned an empty cookie, we simply ignore
4244 * empty cookies and mark them as invalid.
4245 */
4246 if (delim == p3)
4247 srv = NULL;
4248
4249 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004250 if (srv->cookie && (srv->cklen == delim - p3) &&
4251 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004252 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004253 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004254 txn->flags &= ~TX_CK_MASK;
4255 txn->flags |= TX_CK_VALID;
4256 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004257 t->srv = srv;
4258 break;
4259 } else {
4260 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004261 txn->flags &= ~TX_CK_MASK;
4262 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004263 }
4264 }
4265 srv = srv->next;
4266 }
4267
Willy Tarreau3d300592007-03-18 18:34:41 +01004268 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004269 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004270 txn->flags &= ~TX_CK_MASK;
4271 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004272 }
4273
4274 /* depending on the cookie mode, we may have to either :
4275 * - delete the complete cookie if we're in insert+indirect mode, so that
4276 * the server never sees it ;
4277 * - remove the server id from the cookie value, and tag the cookie as an
4278 * application cookie so that it does not get accidentely removed later,
4279 * if we're in cookie prefix mode
4280 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004281 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004282 int delta; /* negative */
4283
4284 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4285 p4 += delta;
4286 cur_end += delta;
4287 cur_next += delta;
4288 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004289 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004290
4291 del_cookie = del_colon = NULL;
4292 app_cookies++; /* protect the header from deletion */
4293 }
4294 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004295 (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 +01004296 del_cookie = p1;
4297 del_colon = colon;
4298 }
4299 } else {
4300 /* now we know that we must keep this cookie since it's
4301 * not ours. But if we wanted to delete our cookie
4302 * earlier, we cannot remove the complete header, but we
4303 * can remove the previous block itself.
4304 */
4305 app_cookies++;
4306
4307 if (del_cookie != NULL) {
4308 int delta; /* negative */
4309
4310 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4311 p4 += delta;
4312 cur_end += delta;
4313 cur_next += delta;
4314 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004315 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004316 del_cookie = del_colon = NULL;
4317 }
4318 }
4319
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004320 if ((t->be->appsession_name != NULL) &&
4321 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004322 /* first, let's see if the cookie is our appcookie*/
4323
4324 /* Cool... it's the right one */
4325
4326 asession_temp = &local_asession;
4327
4328 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4329 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4330 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4331 return;
4332 }
4333
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004334 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4335 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004336 asession_temp->serverid = NULL;
4337
4338 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004339 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004340 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4341 /* free previously allocated memory */
4342 pool_free_to(apools.sessid, local_asession.sessid);
4343 Alert("Not enough memory process_cli():asession:calloc().\n");
4344 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4345 return;
4346 }
4347
4348 asession_temp->sessid = local_asession.sessid;
4349 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004350 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004351 } else {
4352 /* free previously allocated memory */
4353 pool_free_to(apools.sessid, local_asession.sessid);
4354 }
4355
4356 if (asession_temp->serverid == NULL) {
4357 Alert("Found Application Session without matching server.\n");
4358 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004359 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004360 while (srv) {
4361 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004362 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004363 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004364 txn->flags &= ~TX_CK_MASK;
4365 txn->flags |= TX_CK_VALID;
4366 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004367 t->srv = srv;
4368 break;
4369 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004370 txn->flags &= ~TX_CK_MASK;
4371 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004372 }
4373 }
4374 srv = srv->next;
4375 }/* end while(srv) */
4376 }/* end else if server == NULL */
4377
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004378 tv_ms_add(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004379 }/* end if ((t->proxy->appsession_name != NULL) ... */
4380 }
4381
4382 /* we'll have to look for another cookie ... */
4383 p1 = p4;
4384 } /* while (p1 < cur_end) */
4385
4386 /* There's no more cookie on this line.
4387 * We may have marked the last one(s) for deletion.
4388 * We must do this now in two ways :
4389 * - if there is no app cookie, we simply delete the header ;
4390 * - if there are app cookies, we must delete the end of the
4391 * string properly, including the colon/semi-colon before
4392 * the cookie name.
4393 */
4394 if (del_cookie != NULL) {
4395 int delta;
4396 if (app_cookies) {
4397 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4398 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004399 cur_hdr->len += delta;
4400 } else {
4401 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004402
4403 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004404 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4405 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004406 cur_hdr->len = 0;
4407 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004408 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004409 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004410 }
4411
4412 /* keep the link from this header to next one */
4413 old_idx = cur_idx;
4414 } /* end of cookie processing on this header */
4415}
4416
4417
Willy Tarreaua15645d2007-03-18 16:22:39 +01004418/* Iterate the same filter through all response headers contained in <rtr>.
4419 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4420 */
4421int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4422{
4423 char term;
4424 char *cur_ptr, *cur_end, *cur_next;
4425 int cur_idx, old_idx, last_hdr;
4426 struct http_txn *txn = &t->txn;
4427 struct hdr_idx_elem *cur_hdr;
4428 int len, delta;
4429
4430 last_hdr = 0;
4431
4432 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4433 old_idx = 0;
4434
4435 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004436 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004437 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004438 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004439 (exp->action == ACT_ALLOW ||
4440 exp->action == ACT_DENY))
4441 return 0;
4442
4443 cur_idx = txn->hdr_idx.v[old_idx].next;
4444 if (!cur_idx)
4445 break;
4446
4447 cur_hdr = &txn->hdr_idx.v[cur_idx];
4448 cur_ptr = cur_next;
4449 cur_end = cur_ptr + cur_hdr->len;
4450 cur_next = cur_end + cur_hdr->cr + 1;
4451
4452 /* Now we have one header between cur_ptr and cur_end,
4453 * and the next header starts at cur_next.
4454 */
4455
4456 /* The annoying part is that pattern matching needs
4457 * that we modify the contents to null-terminate all
4458 * strings before testing them.
4459 */
4460
4461 term = *cur_end;
4462 *cur_end = '\0';
4463
4464 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4465 switch (exp->action) {
4466 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004467 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004468 last_hdr = 1;
4469 break;
4470
4471 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004472 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004473 last_hdr = 1;
4474 break;
4475
4476 case ACT_REPLACE:
4477 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4478 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4479 /* FIXME: if the user adds a newline in the replacement, the
4480 * index will not be recalculated for now, and the new line
4481 * will not be counted as a new header.
4482 */
4483
4484 cur_end += delta;
4485 cur_next += delta;
4486 cur_hdr->len += delta;
4487 txn->rsp.eoh += delta;
4488 break;
4489
4490 case ACT_REMOVE:
4491 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4492 cur_next += delta;
4493
4494 /* FIXME: this should be a separate function */
4495 txn->rsp.eoh += delta;
4496 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4497 txn->hdr_idx.used--;
4498 cur_hdr->len = 0;
4499 cur_end = NULL; /* null-term has been rewritten */
4500 break;
4501
4502 }
4503 }
4504 if (cur_end)
4505 *cur_end = term; /* restore the string terminator */
4506
4507 /* keep the link from this header to next one in case of later
4508 * removal of next header.
4509 */
4510 old_idx = cur_idx;
4511 }
4512 return 0;
4513}
4514
4515
4516/* Apply the filter to the status line in the response buffer <rtr>.
4517 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4518 * or -1 if a replacement resulted in an invalid status line.
4519 */
4520int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4521{
4522 char term;
4523 char *cur_ptr, *cur_end;
4524 int done;
4525 struct http_txn *txn = &t->txn;
4526 int len, delta;
4527
4528
Willy Tarreau3d300592007-03-18 18:34:41 +01004529 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004530 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004531 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004532 (exp->action == ACT_ALLOW ||
4533 exp->action == ACT_DENY))
4534 return 0;
4535 else if (exp->action == ACT_REMOVE)
4536 return 0;
4537
4538 done = 0;
4539
Willy Tarreau9cdde232007-05-02 20:58:19 +02004540 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004541 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4542
4543 /* Now we have the status line between cur_ptr and cur_end */
4544
4545 /* The annoying part is that pattern matching needs
4546 * that we modify the contents to null-terminate all
4547 * strings before testing them.
4548 */
4549
4550 term = *cur_end;
4551 *cur_end = '\0';
4552
4553 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4554 switch (exp->action) {
4555 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004556 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004557 done = 1;
4558 break;
4559
4560 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004561 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004562 done = 1;
4563 break;
4564
4565 case ACT_REPLACE:
4566 *cur_end = term; /* restore the string terminator */
4567 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4568 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4569 /* FIXME: if the user adds a newline in the replacement, the
4570 * index will not be recalculated for now, and the new line
4571 * will not be counted as a new header.
4572 */
4573
4574 txn->rsp.eoh += delta;
4575 cur_end += delta;
4576
Willy Tarreau9cdde232007-05-02 20:58:19 +02004577 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004578 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004579 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004580 cur_ptr, cur_end + 1,
4581 NULL, NULL);
4582 if (unlikely(!cur_end))
4583 return -1;
4584
4585 /* we have a full respnse and we know that we have either a CR
4586 * or an LF at <ptr>.
4587 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004588 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004589 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4590 /* there is no point trying this regex on headers */
4591 return 1;
4592 }
4593 }
4594 *cur_end = term; /* restore the string terminator */
4595 return done;
4596}
4597
4598
4599
4600/*
4601 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4602 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4603 * unparsable response.
4604 */
4605int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4606{
Willy Tarreau3d300592007-03-18 18:34:41 +01004607 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004608 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004609 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004610 int ret;
4611
4612 /*
4613 * The interleaving of transformations and verdicts
4614 * makes it difficult to decide to continue or stop
4615 * the evaluation.
4616 */
4617
Willy Tarreau3d300592007-03-18 18:34:41 +01004618 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004619 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4620 exp->action == ACT_PASS)) {
4621 exp = exp->next;
4622 continue;
4623 }
4624
4625 /* Apply the filter to the status line. */
4626 ret = apply_filter_to_sts_line(t, rtr, exp);
4627 if (unlikely(ret < 0))
4628 return -1;
4629
4630 if (likely(ret == 0)) {
4631 /* The filter did not match the response, it can be
4632 * iterated through all headers.
4633 */
4634 apply_filter_to_resp_headers(t, rtr, exp);
4635 }
4636 exp = exp->next;
4637 }
4638 return 0;
4639}
4640
4641
4642
4643/*
4644 * Manager server-side cookies
4645 */
4646void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4647{
4648 struct http_txn *txn = &t->txn;
4649 char *p1, *p2, *p3, *p4;
4650
4651 appsess *asession_temp = NULL;
4652 appsess local_asession;
4653
4654 char *cur_ptr, *cur_end, *cur_next;
4655 int cur_idx, old_idx, delta;
4656
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004657 if (t->be->cookie_name == NULL &&
4658 t->be->appsession_name == NULL &&
4659 t->be->capture_name == NULL &&
4660 !(t->be->options & PR_O_CHK_CACHE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004661 return;
4662
4663 /* Iterate through the headers.
4664 * we start with the start line.
4665 */
4666 old_idx = 0;
4667 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4668
4669 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4670 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004671 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004672
4673 cur_hdr = &txn->hdr_idx.v[cur_idx];
4674 cur_ptr = cur_next;
4675 cur_end = cur_ptr + cur_hdr->len;
4676 cur_next = cur_end + cur_hdr->cr + 1;
4677
4678 /* We have one full header between cur_ptr and cur_end, and the
4679 * next header starts at cur_next. We're only interested in
4680 * "Cookie:" headers.
4681 */
4682
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004683 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4684 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004685 old_idx = cur_idx;
4686 continue;
4687 }
4688
4689 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004690 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004691
4692
4693 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004694 if (t->be->cookie_name == NULL &&
4695 t->be->appsession_name == NULL &&
4696 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004697 return;
4698
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004699 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004700
4701 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004702 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4703 break;
4704
4705 /* p1 is at the beginning of the cookie name */
4706 p2 = p1;
4707
4708 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4709 p2++;
4710
4711 if (p2 == cur_end || *p2 == ';') /* next cookie */
4712 break;
4713
4714 p3 = p2 + 1; /* skip the '=' sign */
4715 if (p3 == cur_end)
4716 break;
4717
4718 p4 = p3;
4719 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4720 p4++;
4721
4722 /* here, we have the cookie name between p1 and p2,
4723 * and its value between p3 and p4.
4724 * we can process it.
4725 */
4726
4727 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004728 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004729 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004730 (p4 - p1 >= t->be->capture_namelen) &&
4731 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004732 int log_len = p4 - p1;
4733
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004734 if ((txn->srv_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004735 Alert("HTTP logging : out of memory.\n");
4736 }
4737
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004738 if (log_len > t->be->capture_len)
4739 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004740 memcpy(txn->srv_cookie, p1, log_len);
4741 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004742 }
4743
4744 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004745 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4746 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004747 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004748 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004749
4750 /* If the cookie is in insert mode on a known server, we'll delete
4751 * this occurrence because we'll insert another one later.
4752 * We'll delete it too if the "indirect" option is set and we're in
4753 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004754 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4755 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004756 /* this header must be deleted */
4757 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4758 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4759 txn->hdr_idx.used--;
4760 cur_hdr->len = 0;
4761 cur_next += delta;
4762 txn->rsp.eoh += delta;
4763
Willy Tarreau3d300592007-03-18 18:34:41 +01004764 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004765 }
4766 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004767 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004768 /* replace bytes p3->p4 with the cookie name associated
4769 * with this server since we know it.
4770 */
4771 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4772 cur_hdr->len += delta;
4773 cur_next += delta;
4774 txn->rsp.eoh += delta;
4775
Willy Tarreau3d300592007-03-18 18:34:41 +01004776 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004777 }
4778 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004779 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004780 /* insert the cookie name associated with this server
4781 * before existing cookie, and insert a delimitor between them..
4782 */
4783 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4784 cur_hdr->len += delta;
4785 cur_next += delta;
4786 txn->rsp.eoh += delta;
4787
4788 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004789 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004790 }
4791 }
4792 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004793 else if ((t->be->appsession_name != NULL) &&
4794 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004795
4796 /* Cool... it's the right one */
4797
4798 size_t server_id_len = strlen(t->srv->id) + 1;
4799 asession_temp = &local_asession;
4800
4801 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4802 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4803 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4804 return;
4805 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004806 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4807 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004808 asession_temp->serverid = NULL;
4809
4810 /* only do insert, if lookup fails */
4811 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
4812 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4813 Alert("Not enough Memory process_srv():asession:calloc().\n");
4814 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4815 return;
4816 }
4817 asession_temp->sessid = local_asession.sessid;
4818 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004819 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004820 }/* end if (chtbl_lookup()) */
4821 else {
4822 /* free wasted memory */
4823 pool_free_to(apools.sessid, local_asession.sessid);
4824 } /* end else from if (chtbl_lookup()) */
4825
4826 if (asession_temp->serverid == NULL) {
4827 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
4828 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4829 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4830 return;
4831 }
4832 asession_temp->serverid[0] = '\0';
4833 }
4834
4835 if (asession_temp->serverid[0] == '\0')
4836 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4837
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004838 tv_ms_add(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004839
4840#if defined(DEBUG_HASH)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004841 print_table(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004842#endif
4843 }/* end if ((t->proxy->appsession_name != NULL) ... */
4844 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4845 } /* we're now at the end of the cookie value */
4846
4847 /* keep the link from this header to next one */
4848 old_idx = cur_idx;
4849 } /* end of cookie processing on this header */
4850}
4851
4852
4853
4854/*
4855 * Check if response is cacheable or not. Updates t->flags.
4856 */
4857void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4858{
4859 struct http_txn *txn = &t->txn;
4860 char *p1, *p2;
4861
4862 char *cur_ptr, *cur_end, *cur_next;
4863 int cur_idx;
4864
Willy Tarreau3d300592007-03-18 18:34:41 +01004865 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004866 return;
4867
4868 /* Iterate through the headers.
4869 * we start with the start line.
4870 */
4871 cur_idx = 0;
4872 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4873
4874 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4875 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004876 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004877
4878 cur_hdr = &txn->hdr_idx.v[cur_idx];
4879 cur_ptr = cur_next;
4880 cur_end = cur_ptr + cur_hdr->len;
4881 cur_next = cur_end + cur_hdr->cr + 1;
4882
4883 /* We have one full header between cur_ptr and cur_end, and the
4884 * next header starts at cur_next. We're only interested in
4885 * "Cookie:" headers.
4886 */
4887
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004888 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4889 if (val) {
4890 if ((cur_end - (cur_ptr + val) >= 8) &&
4891 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4892 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4893 return;
4894 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004895 }
4896
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004897 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4898 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004899 continue;
4900
4901 /* OK, right now we know we have a cache-control header at cur_ptr */
4902
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004903 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004904
4905 if (p1 >= cur_end) /* no more info */
4906 continue;
4907
4908 /* p1 is at the beginning of the value */
4909 p2 = p1;
4910
4911 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4912 p2++;
4913
4914 /* we have a complete value between p1 and p2 */
4915 if (p2 < cur_end && *p2 == '=') {
4916 /* we have something of the form no-cache="set-cookie" */
4917 if ((cur_end - p1 >= 21) &&
4918 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4919 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004920 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004921 continue;
4922 }
4923
4924 /* OK, so we know that either p2 points to the end of string or to a comma */
4925 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4926 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4927 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4928 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004929 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004930 return;
4931 }
4932
4933 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004934 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004935 continue;
4936 }
4937 }
4938}
4939
4940
Willy Tarreau58f10d72006-12-04 02:26:12 +01004941/*
4942 * Try to retrieve a known appsession in the URI, then the associated server.
4943 * If the server is found, it's assigned to the session.
4944 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004945void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004946{
Willy Tarreau3d300592007-03-18 18:34:41 +01004947 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004948 appsess *asession_temp = NULL;
4949 appsess local_asession;
4950 char *request_line;
4951
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004952 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004953 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004954 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004955 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004956 return;
4957
4958 /* skip ';' */
4959 request_line++;
4960
4961 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004962 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004963 return;
4964
4965 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004966 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004967
4968 /* First try if we already have an appsession */
4969 asession_temp = &local_asession;
4970
4971 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4972 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4973 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4974 return;
4975 }
4976
4977 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004978 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4979 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004980 asession_temp->serverid = NULL;
4981
4982 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004983 if (chtbl_lookup(&(t->be->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004984 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4985 /* free previously allocated memory */
4986 pool_free_to(apools.sessid, local_asession.sessid);
4987 Alert("Not enough memory process_cli():asession:calloc().\n");
4988 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4989 return;
4990 }
4991 asession_temp->sessid = local_asession.sessid;
4992 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004993 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004994 }
4995 else {
4996 /* free previously allocated memory */
4997 pool_free_to(apools.sessid, local_asession.sessid);
4998 }
4999
Willy Tarreau42aae5c2007-04-29 17:43:56 +02005000 tv_ms_add(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005001 asession_temp->request_count++;
5002
5003#if defined(DEBUG_HASH)
5004 print_table(&(t->proxy->htbl_proxy));
5005#endif
5006 if (asession_temp->serverid == NULL) {
5007 Alert("Found Application Session without matching server.\n");
5008 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005009 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005010 while (srv) {
5011 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005012 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005013 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005014 txn->flags &= ~TX_CK_MASK;
5015 txn->flags |= TX_CK_VALID;
5016 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005017 t->srv = srv;
5018 break;
5019 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005020 txn->flags &= ~TX_CK_MASK;
5021 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005022 }
5023 }
5024 srv = srv->next;
5025 }
5026 }
5027}
5028
5029
Willy Tarreaub2513902006-12-17 14:52:38 +01005030/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005031 * In a GET or HEAD request, check if the requested URI matches the stats uri
5032 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005033 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005034 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005035 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005036 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005037 *
5038 * Returns 1 if the session's state changes, otherwise 0.
5039 */
5040int stats_check_uri_auth(struct session *t, struct proxy *backend)
5041{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005042 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005043 struct uri_auth *uri_auth = backend->uri_auth;
5044 struct user_auth *user;
5045 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005046 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005047
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005048 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005049 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005050 return 0;
5051
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005052 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005053
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005054 /* the URI is in h */
5055 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005056 return 0;
5057
5058 /* we are in front of a interceptable URI. Let's check
5059 * if there's an authentication and if it's valid.
5060 */
5061 user = uri_auth->users;
5062 if (!user) {
5063 /* no user auth required, it's OK */
5064 authenticated = 1;
5065 } else {
5066 authenticated = 0;
5067
5068 /* a user list is defined, we have to check.
5069 * skip 21 chars for "Authorization: Basic ".
5070 */
5071
5072 /* FIXME: this should move to an earlier place */
5073 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005074 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5075 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5076 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005077 if (len > 14 &&
5078 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005079 txn->auth_hdr.str = h;
5080 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005081 break;
5082 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005083 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005084 }
5085
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005086 if (txn->auth_hdr.len < 21 ||
5087 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005088 user = NULL;
5089
5090 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005091 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5092 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005093 user->user_pwd, user->user_len)) {
5094 authenticated = 1;
5095 break;
5096 }
5097 user = user->next;
5098 }
5099 }
5100
5101 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005102 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005103
5104 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005105 msg.str = trash;
5106 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005107 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005108 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01005109 if (!(t->flags & SN_ERR_MASK))
5110 t->flags |= SN_ERR_PRXCOND;
5111 if (!(t->flags & SN_FINST_MASK))
5112 t->flags |= SN_FINST_R;
5113 return 1;
5114 }
5115
5116 /* The request is valid, the user is authenticate. Let's start sending
5117 * data.
5118 */
5119 t->cli_state = CL_STSHUTR;
5120 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02005121 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01005122 t->data_source = DATA_SRC_STATS;
5123 t->data_state = DATA_ST_INIT;
5124 produce_content(t);
5125 return 1;
5126}
5127
5128
Willy Tarreaubaaee002006-06-26 02:48:02 +02005129/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005130 * Print a debug line with a header
5131 */
5132void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5133{
5134 int len, max;
5135 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5136 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5137 max = end - start;
5138 UBOUND(max, sizeof(trash) - len - 1);
5139 len += strlcpy2(trash + len, start, max + 1);
5140 trash[len++] = '\n';
5141 write(1, trash, len);
5142}
5143
5144
Willy Tarreau8797c062007-05-07 00:55:35 +02005145/************************************************************************/
5146/* The code below is dedicated to ACL parsing and matching */
5147/************************************************************************/
5148
5149
5150
5151
5152/* 1. Check on METHOD
5153 * We use the pre-parsed method if it is known, and store its number as an
5154 * integer. If it is unknown, we use the pointer and the length.
5155 */
5156static int acl_parse_meth(const char *text, struct acl_pattern *pattern)
5157{
5158 int len, meth;
5159
5160 len = strlen(text);
5161 meth = find_http_meth(text, len);
5162
5163 pattern->val.i = meth;
5164 if (meth == HTTP_METH_OTHER) {
5165 pattern->ptr.str = strdup(text);
5166 if (!pattern->ptr.str)
5167 return 0;
5168 pattern->len = len;
5169 }
5170 return 1;
5171}
5172
5173static int acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5174{
5175 int meth;
5176 struct http_txn *txn = l7;
5177
5178 meth = txn->meth;
5179 test->i = meth;
5180 if (meth == HTTP_METH_OTHER) {
5181 test->len = txn->req.sl.rq.m_l;
5182 test->ptr = txn->req.sol;
5183 }
5184 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5185 return 1;
5186}
5187
5188static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5189{
5190 if (test->i != pattern->val.i)
5191 return 0;
5192
5193 if (test->i != HTTP_METH_OTHER)
5194 return 1;
5195
5196 /* Other method, we must compare the strings */
5197 if (pattern->len != test->len)
5198 return 0;
5199 if (strncmp(pattern->ptr.str, test->ptr, test->len) != 0)
5200 return 0;
5201 return 1;
5202}
5203
5204/* 2. Check on Request/Status Version
5205 * We simply compare strings here.
5206 */
5207static int acl_parse_ver(const char *text, struct acl_pattern *pattern)
5208{
5209 pattern->ptr.str = strdup(text);
5210 if (!pattern->ptr.str)
5211 return 0;
5212 pattern->len = strlen(text);
5213 return 1;
5214}
5215
5216static int acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5217{
5218 struct http_txn *txn = l7;
5219 char *ptr;
5220 int len;
5221
5222 len = txn->req.sl.rq.v_l;
5223 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5224
5225 while ((len-- > 0) && (*ptr++ != '/'));
5226 if (len <= 0)
5227 return 0;
5228
5229 test->ptr = ptr;
5230 test->len = len;
5231
5232 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5233 return 1;
5234}
5235
5236static int acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5237{
5238 struct http_txn *txn = l7;
5239 char *ptr;
5240 int len;
5241
5242 len = txn->rsp.sl.st.v_l;
5243 ptr = txn->rsp.sol;
5244
5245 while ((len-- > 0) && (*ptr++ != '/'));
5246 if (len <= 0)
5247 return 0;
5248
5249 test->ptr = ptr;
5250 test->len = len;
5251
5252 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5253 return 1;
5254}
5255
5256/* 3. Check on Status Code. We manipulate integers here. */
5257static int acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5258{
5259 struct http_txn *txn = l7;
5260 char *ptr;
5261 int len;
5262
5263 len = txn->rsp.sl.st.c_l;
5264 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5265
5266 test->i = __strl2ui(ptr, len);
5267 test->flags = ACL_TEST_F_VOL_1ST;
5268 return 1;
5269}
5270
5271/* 4. Check on URL/URI. A pointer to the URI is stored. */
5272static int acl_fetch_url(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5273{
5274 struct http_txn *txn = l7;
5275
5276 test->len = txn->req.sl.rq.u_l;
5277 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5278
5279 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5280 return 1;
5281}
5282
5283
5284
5285/************************************************************************/
5286/* All supported keywords must be declared here. */
5287/************************************************************************/
5288
5289/* Note: must not be declared <const> as its list will be overwritten */
5290static struct acl_kw_list acl_kws = {{ },{
5291 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5292 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5293 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
5294 { "status", acl_parse_range, acl_fetch_stcode, acl_match_range },
5295
5296 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5297 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5298 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5299 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5300 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5301 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
5302 { NULL, NULL, NULL, NULL },
5303#if 0
5304 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
5305
5306 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5307 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5308 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5309 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5310 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5311 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5312 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5313
5314 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5315 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5316 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5317 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5318 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5319 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5320 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5321
5322 { "hdr", acl_parse_str, acl_fetch_hdr, acl_match_str },
5323 { "hdr_reg", acl_parse_reg, acl_fetch_hdr, acl_match_reg },
5324 { "hdr_beg", acl_parse_str, acl_fetch_hdr, acl_match_beg },
5325 { "hdr_end", acl_parse_str, acl_fetch_hdr, acl_match_end },
5326 { "hdr_sub", acl_parse_str, acl_fetch_hdr, acl_match_sub },
5327 { "hdr_dir", acl_parse_str, acl_fetch_hdr, acl_match_dir },
5328 { "hdr_dom", acl_parse_str, acl_fetch_hdr, acl_match_dom },
5329 { "hdr_pst", acl_parse_none, acl_fetch_hdr, acl_match_pst },
5330
5331 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5332 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5333 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5334 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5335 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5336 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5337 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5338 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5339
5340 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5341 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5342 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5343 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5344 { NULL, NULL, NULL, NULL },
5345#endif
5346}};
5347
5348
5349__attribute__((constructor))
5350static void __http_protocol_init(void)
5351{
5352 acl_register_keywords(&acl_kws);
5353}
5354
5355
Willy Tarreau58f10d72006-12-04 02:26:12 +01005356/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005357 * Local variables:
5358 * c-indent-level: 8
5359 * c-basic-offset: 8
5360 * End:
5361 */