blob: d1bfdcf22f22a7c9372f08c760767d0f898acd1f [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 Tarreaua8b55e32007-05-13 16:08:19 +0200445 if (!tv_add_ifset(&s->rep->wex, &now, &s->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +0200446 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200447 s->cli_state = CL_STSHUTR;
448 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100449 if (msg->len)
450 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451 s->req->l = 0;
452}
453
454
455/*
456 * returns a message into the rep buffer, and flushes the req buffer.
Willy Tarreau0f772532006-12-23 20:51:41 +0100457 * The reply buffer doesn't need to be empty before this. The message
458 * is contained in a "chunk". If it is null, then an empty message is
459 * used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100461void client_return(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462{
463 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100464 if (msg->len)
465 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466 s->req->l = 0;
467}
468
469
470/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100471 * indicators accordingly. Note that if <status> is 0, or if the message
472 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473 */
474void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100475 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476{
477 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100478 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100479 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100480 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100481 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200482 }
483 if (!(t->flags & SN_ERR_MASK))
484 t->flags |= err;
485 if (!(t->flags & SN_FINST_MASK))
486 t->flags |= finst;
487}
488
Willy Tarreau80587432006-12-24 17:47:20 +0100489/* This function returns the appropriate error location for the given session
490 * and message.
491 */
492
493struct chunk *error_message(struct session *s, int msgnum)
494{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200495 if (s->be->errmsg[msgnum].str)
496 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100497 else if (s->fe->errmsg[msgnum].str)
498 return &s->fe->errmsg[msgnum];
499 else
500 return &http_err_chunks[msgnum];
501}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200502
Willy Tarreau53b6c742006-12-17 13:37:46 +0100503/*
504 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
505 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
506 */
507static http_meth_t find_http_meth(const char *str, const int len)
508{
509 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100510 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100511
512 m = ((unsigned)*str - 'A');
513
514 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100515 for (h = http_methods[m]; h->len > 0; h++) {
516 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100517 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100518 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100519 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100520 };
521 return HTTP_METH_OTHER;
522 }
523 return HTTP_METH_NONE;
524
525}
526
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527/* Processes the client and server jobs of a session task, then
528 * puts it back to the wait queue in a clean state, or
529 * cleans up its resources if it must be deleted. Returns
530 * the time the task accepts to wait, or TIME_ETERNITY for
531 * infinity.
532 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200533void process_session(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200534{
535 struct session *s = t->context;
536 int fsm_resync = 0;
537
538 do {
539 fsm_resync = 0;
540 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
541 fsm_resync |= process_cli(s);
542 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
543 fsm_resync |= process_srv(s);
544 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
545 } while (fsm_resync);
546
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200547 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200548 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
549 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200551 t->expire = s->req->rex;
552 tv_min(&t->expire, &s->req->rex, &s->req->wex);
553 tv_bound(&t->expire, &s->req->cex);
554 tv_bound(&t->expire, &s->rep->rex);
555 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200556
557 /* restore t to its place in the task list */
558 task_queue(t);
559
560#ifdef DEBUG_FULL
561 /* DEBUG code : this should never ever happen, otherwise it indicates
562 * that a task still has something to do and will provoke a quick loop.
563 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200564 if (tv_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200565 exit(100);
566#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200567 *next = t->expire;
568 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200569 }
570
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100571 s->fe->feconn--;
572 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200573 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574 actconn--;
575
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200576 if (unlikely((global.mode & MODE_DEBUG) &&
577 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100579 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200580 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100581 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200582 write(1, trash, len);
583 }
584
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200585 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100586 if (s->req != NULL)
587 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200588 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100589 s->logs.bytes_out = s->rep->total;
590
591 s->fe->bytes_in += s->logs.bytes_in;
592 s->fe->bytes_out += s->logs.bytes_out;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200593 if (s->be != s->fe) {
594 s->be->bytes_in += s->logs.bytes_in;
595 s->be->bytes_out += s->logs.bytes_out;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100596 }
597 if (s->srv) {
598 s->srv->bytes_in += s->logs.bytes_in;
599 s->srv->bytes_out += s->logs.bytes_out;
600 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601
602 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200603 if (s->logs.logwait &&
604 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200605 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
606 if (s->fe->to_log & LW_REQ)
607 http_sess_log(s);
608 else
609 tcp_sess_log(s);
610 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611
612 /* the task MUST not be in the run queue anymore */
613 task_delete(t);
614 session_free(s);
615 task_free(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200616 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200617}
618
619
Willy Tarreau42250582007-04-01 01:30:43 +0200620extern const char sess_term_cond[8];
621extern const char sess_fin_state[8];
622extern const char *monthname[12];
623const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
624const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
625 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
626 unknown, Set-cookie Rewritten */
627void **pool_requri = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100628
Willy Tarreau42250582007-04-01 01:30:43 +0200629/*
630 * send a log for the session when we have enough info about it.
631 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100632 */
Willy Tarreau42250582007-04-01 01:30:43 +0200633static void http_sess_log(struct session *s)
634{
635 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
636 struct proxy *fe = s->fe;
637 struct proxy *be = s->be;
638 struct proxy *prx_log;
639 struct http_txn *txn = &s->txn;
640 int tolog;
641 char *uri, *h;
642 char *svid;
643 struct tm *tm;
644 static char tmpline[MAX_SYSLOG_LEN];
645 int hdr;
646
647 if (fe->logfac1 < 0 && fe->logfac2 < 0)
648 return;
649 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100650
Willy Tarreau42250582007-04-01 01:30:43 +0200651 if (s->cli_addr.ss_family == AF_INET)
652 inet_ntop(AF_INET,
653 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
654 pn, sizeof(pn));
655 else
656 inet_ntop(AF_INET6,
657 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
658 pn, sizeof(pn));
659
660 tm = localtime((time_t *)&s->logs.tv_accept.tv_sec);
661
662
663 /* FIXME: let's limit ourselves to frontend logging for now. */
664 tolog = fe->to_log;
665
666 h = tmpline;
667 if (fe->to_log & LW_REQHDR &&
668 txn->req.cap &&
669 (h < tmpline + sizeof(tmpline) - 10)) {
670 *(h++) = ' ';
671 *(h++) = '{';
672 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
673 if (hdr)
674 *(h++) = '|';
675 if (txn->req.cap[hdr] != NULL)
676 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
677 '#', hdr_encode_map, txn->req.cap[hdr]);
678 }
679 *(h++) = '}';
680 }
681
682 if (fe->to_log & LW_RSPHDR &&
683 txn->rsp.cap &&
684 (h < tmpline + sizeof(tmpline) - 7)) {
685 *(h++) = ' ';
686 *(h++) = '{';
687 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
688 if (hdr)
689 *(h++) = '|';
690 if (txn->rsp.cap[hdr] != NULL)
691 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
692 '#', hdr_encode_map, txn->rsp.cap[hdr]);
693 }
694 *(h++) = '}';
695 }
696
697 if (h < tmpline + sizeof(tmpline) - 4) {
698 *(h++) = ' ';
699 *(h++) = '"';
700 uri = txn->uri ? txn->uri : "<BADREQ>";
701 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
702 '#', url_encode_map, uri);
703 *(h++) = '"';
704 }
705 *h = '\0';
706
707 svid = (tolog & LW_SVID) ?
708 (s->data_source != DATA_SRC_STATS) ?
709 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
710
711 send_log(prx_log, LOG_INFO,
712 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
713 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
714 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
715 pn,
716 (s->cli_addr.ss_family == AF_INET) ?
717 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
718 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
719 tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900,
720 tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000,
721 fe->id, be->id, svid,
722 s->logs.t_request,
723 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
724 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
725 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
726 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
727 txn->status,
728 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
729 txn->cli_cookie ? txn->cli_cookie : "-",
730 txn->srv_cookie ? txn->srv_cookie : "-",
731 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
732 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
733 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
734 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
735 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
736 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
737
738 s->logs.logwait = 0;
739}
740
Willy Tarreau117f59e2007-03-04 18:17:17 +0100741
742/*
743 * Capture headers from message starting at <som> according to header list
744 * <cap_hdr>, and fill the <idx> structure appropriately.
745 */
746void capture_headers(char *som, struct hdr_idx *idx,
747 char **cap, struct cap_hdr *cap_hdr)
748{
749 char *eol, *sol, *col, *sov;
750 int cur_idx;
751 struct cap_hdr *h;
752 int len;
753
754 sol = som + hdr_idx_first_pos(idx);
755 cur_idx = hdr_idx_first_idx(idx);
756
757 while (cur_idx) {
758 eol = sol + idx->v[cur_idx].len;
759
760 col = sol;
761 while (col < eol && *col != ':')
762 col++;
763
764 sov = col + 1;
765 while (sov < eol && http_is_lws[(unsigned char)*sov])
766 sov++;
767
768 for (h = cap_hdr; h; h = h->next) {
769 if ((h->namelen == col - sol) &&
770 (strncasecmp(sol, h->name, h->namelen) == 0)) {
771 if (cap[h->index] == NULL)
772 cap[h->index] =
773 pool_alloc_from(h->pool, h->len + 1);
774
775 if (cap[h->index] == NULL) {
776 Alert("HTTP capture : out of memory.\n");
777 continue;
778 }
779
780 len = eol - sov;
781 if (len > h->len)
782 len = h->len;
783
784 memcpy(cap[h->index], sov, len);
785 cap[h->index][len]=0;
786 }
787 }
788 sol = eol + idx->v[cur_idx].cr + 1;
789 cur_idx = idx->v[cur_idx].next;
790 }
791}
792
793
Willy Tarreau42250582007-04-01 01:30:43 +0200794/* either we find an LF at <ptr> or we jump to <bad>.
795 */
796#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
797
798/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
799 * otherwise to <http_msg_ood> with <state> set to <st>.
800 */
801#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
802 ptr++; \
803 if (likely(ptr < end)) \
804 goto good; \
805 else { \
806 state = (st); \
807 goto http_msg_ood; \
808 } \
809 } while (0)
810
811
Willy Tarreaubaaee002006-06-26 02:48:02 +0200812/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100813 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100814 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
815 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
816 * will give undefined results.
817 * Note that it is upon the caller's responsibility to ensure that ptr < end,
818 * and that msg->sol points to the beginning of the response.
819 * If a complete line is found (which implies that at least one CR or LF is
820 * found before <end>, the updated <ptr> is returned, otherwise NULL is
821 * returned indicating an incomplete line (which does not mean that parts have
822 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
823 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
824 * upon next call.
825 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200826 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100827 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
828 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200829 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100830 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100831const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100832 const char *ptr, const char *end,
833 char **ret_ptr, int *ret_state)
834{
835 __label__
836 http_msg_rpver,
837 http_msg_rpver_sp,
838 http_msg_rpcode,
839 http_msg_rpcode_sp,
840 http_msg_rpreason,
841 http_msg_rpline_eol,
842 http_msg_ood, /* out of data */
843 http_msg_invalid;
844
845 switch (state) {
846 http_msg_rpver:
847 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100848 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100849 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
850
851 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100852 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100853 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
854 }
855 goto http_msg_invalid;
856
857 http_msg_rpver_sp:
858 case HTTP_MSG_RPVER_SP:
859 if (likely(!HTTP_IS_LWS(*ptr))) {
860 msg->sl.st.c = ptr - msg_buf;
861 goto http_msg_rpcode;
862 }
863 if (likely(HTTP_IS_SPHT(*ptr)))
864 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
865 /* so it's a CR/LF, this is invalid */
866 goto http_msg_invalid;
867
868 http_msg_rpcode:
869 case HTTP_MSG_RPCODE:
870 if (likely(!HTTP_IS_LWS(*ptr)))
871 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
872
873 if (likely(HTTP_IS_SPHT(*ptr))) {
874 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
875 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
876 }
877
878 /* so it's a CR/LF, so there is no reason phrase */
879 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
880 http_msg_rsp_reason:
881 /* FIXME: should we support HTTP responses without any reason phrase ? */
882 msg->sl.st.r = ptr - msg_buf;
883 msg->sl.st.r_l = 0;
884 goto http_msg_rpline_eol;
885
886 http_msg_rpcode_sp:
887 case HTTP_MSG_RPCODE_SP:
888 if (likely(!HTTP_IS_LWS(*ptr))) {
889 msg->sl.st.r = ptr - msg_buf;
890 goto http_msg_rpreason;
891 }
892 if (likely(HTTP_IS_SPHT(*ptr)))
893 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
894 /* so it's a CR/LF, so there is no reason phrase */
895 goto http_msg_rsp_reason;
896
897 http_msg_rpreason:
898 case HTTP_MSG_RPREASON:
899 if (likely(!HTTP_IS_CRLF(*ptr)))
900 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
901 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
902 http_msg_rpline_eol:
903 /* We have seen the end of line. Note that we do not
904 * necessarily have the \n yet, but at least we know that we
905 * have EITHER \r OR \n, otherwise the response would not be
906 * complete. We can then record the response length and return
907 * to the caller which will be able to register it.
908 */
909 msg->sl.st.l = ptr - msg->sol;
910 return ptr;
911
912#ifdef DEBUG_FULL
913 default:
914 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
915 exit(1);
916#endif
917 }
918
919 http_msg_ood:
920 /* out of data */
921 if (ret_state)
922 *ret_state = state;
923 if (ret_ptr)
924 *ret_ptr = (char *)ptr;
925 return NULL;
926
927 http_msg_invalid:
928 /* invalid message */
929 if (ret_state)
930 *ret_state = HTTP_MSG_ERROR;
931 return NULL;
932}
933
934
935/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100936 * This function parses a request line between <ptr> and <end>, starting with
937 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
938 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
939 * will give undefined results.
940 * Note that it is upon the caller's responsibility to ensure that ptr < end,
941 * and that msg->sol points to the beginning of the request.
942 * If a complete line is found (which implies that at least one CR or LF is
943 * found before <end>, the updated <ptr> is returned, otherwise NULL is
944 * returned indicating an incomplete line (which does not mean that parts have
945 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
946 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
947 * upon next call.
948 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200949 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100950 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
951 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200952 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200953 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100954const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100955 const char *ptr, const char *end,
956 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200957{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100958 __label__
959 http_msg_rqmeth,
960 http_msg_rqmeth_sp,
961 http_msg_rquri,
962 http_msg_rquri_sp,
963 http_msg_rqver,
964 http_msg_rqline_eol,
965 http_msg_ood, /* out of data */
966 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100967
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100968 switch (state) {
969 http_msg_rqmeth:
970 case HTTP_MSG_RQMETH:
971 if (likely(HTTP_IS_TOKEN(*ptr)))
972 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100973
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100974 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100975 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100976 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
977 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100978
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100979 if (likely(HTTP_IS_CRLF(*ptr))) {
980 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100981 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100982 http_msg_req09_uri:
983 msg->sl.rq.u = ptr - msg_buf;
984 http_msg_req09_uri_e:
985 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
986 http_msg_req09_ver:
987 msg->sl.rq.v = ptr - msg_buf;
988 msg->sl.rq.v_l = 0;
989 goto http_msg_rqline_eol;
990 }
991 goto http_msg_invalid;
992
993 http_msg_rqmeth_sp:
994 case HTTP_MSG_RQMETH_SP:
995 if (likely(!HTTP_IS_LWS(*ptr))) {
996 msg->sl.rq.u = ptr - msg_buf;
997 goto http_msg_rquri;
998 }
999 if (likely(HTTP_IS_SPHT(*ptr)))
1000 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1001 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1002 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001003
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001004 http_msg_rquri:
1005 case HTTP_MSG_RQURI:
1006 if (likely(!HTTP_IS_LWS(*ptr)))
1007 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001008
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001009 if (likely(HTTP_IS_SPHT(*ptr))) {
1010 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1011 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1012 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001013
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001014 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1015 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001016
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001017 http_msg_rquri_sp:
1018 case HTTP_MSG_RQURI_SP:
1019 if (likely(!HTTP_IS_LWS(*ptr))) {
1020 msg->sl.rq.v = ptr - msg_buf;
1021 goto http_msg_rqver;
1022 }
1023 if (likely(HTTP_IS_SPHT(*ptr)))
1024 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1025 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1026 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001027
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001028 http_msg_rqver:
1029 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001030 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001031 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001032
1033 if (likely(HTTP_IS_CRLF(*ptr))) {
1034 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1035 http_msg_rqline_eol:
1036 /* We have seen the end of line. Note that we do not
1037 * necessarily have the \n yet, but at least we know that we
1038 * have EITHER \r OR \n, otherwise the request would not be
1039 * complete. We can then record the request length and return
1040 * to the caller which will be able to register it.
1041 */
1042 msg->sl.rq.l = ptr - msg->sol;
1043 return ptr;
1044 }
1045
1046 /* neither an HTTP_VER token nor a CRLF */
1047 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001049#ifdef DEBUG_FULL
1050 default:
1051 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1052 exit(1);
1053#endif
1054 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001055
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001056 http_msg_ood:
1057 /* out of data */
1058 if (ret_state)
1059 *ret_state = state;
1060 if (ret_ptr)
1061 *ret_ptr = (char *)ptr;
1062 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001063
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001064 http_msg_invalid:
1065 /* invalid message */
1066 if (ret_state)
1067 *ret_state = HTTP_MSG_ERROR;
1068 return NULL;
1069}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001070
1071
Willy Tarreau8973c702007-01-21 23:58:29 +01001072/*
1073 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001074 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001075 * when data are missing and recalled at the exact same location with no
1076 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001077 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1078 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001079 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1081{
1082 __label__
1083 http_msg_rqbefore,
1084 http_msg_rqbefore_cr,
1085 http_msg_rqmeth,
1086 http_msg_rqline_end,
1087 http_msg_hdr_first,
1088 http_msg_hdr_name,
1089 http_msg_hdr_l1_sp,
1090 http_msg_hdr_l1_lf,
1091 http_msg_hdr_l1_lws,
1092 http_msg_hdr_val,
1093 http_msg_hdr_l2_lf,
1094 http_msg_hdr_l2_lws,
1095 http_msg_complete_header,
1096 http_msg_last_lf,
1097 http_msg_ood, /* out of data */
1098 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001099
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 int state; /* updated only when leaving the FSM */
1101 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001102
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001103 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 ptr = buf->lr;
1105 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 if (unlikely(ptr >= end))
1108 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001111 /*
1112 * First, states that are specific to the response only.
1113 * We check them first so that request and headers are
1114 * closer to each other (accessed more often).
1115 */
1116 http_msg_rpbefore:
1117 case HTTP_MSG_RPBEFORE:
1118 if (likely(HTTP_IS_TOKEN(*ptr))) {
1119 if (likely(ptr == buf->data)) {
1120 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001121 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001122 } else {
1123#if PARSE_PRESERVE_EMPTY_LINES
1124 /* only skip empty leading lines, don't remove them */
1125 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001126 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001127#else
1128 /* Remove empty leading lines, as recommended by
1129 * RFC2616. This takes a lot of time because we
1130 * must move all the buffer backwards, but this
1131 * is rarely needed. The method above will be
1132 * cleaner when we'll be able to start sending
1133 * the request from any place in the buffer.
1134 */
1135 buf->lr = ptr;
1136 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001137 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 msg->sol = buf->data;
1139 ptr = buf->data;
1140 end = buf->r;
1141#endif
1142 }
1143 hdr_idx_init(idx);
1144 state = HTTP_MSG_RPVER;
1145 goto http_msg_rpver;
1146 }
1147
1148 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1149 goto http_msg_invalid;
1150
1151 if (unlikely(*ptr == '\n'))
1152 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1154 /* stop here */
1155
1156 http_msg_rpbefore_cr:
1157 case HTTP_MSG_RPBEFORE_CR:
1158 EXPECT_LF_HERE(ptr, http_msg_invalid);
1159 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1160 /* stop here */
1161
1162 http_msg_rpver:
1163 case HTTP_MSG_RPVER:
1164 case HTTP_MSG_RPVER_SP:
1165 case HTTP_MSG_RPCODE:
1166 case HTTP_MSG_RPCODE_SP:
1167 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001168 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001169 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 if (unlikely(!ptr))
1171 return;
1172
1173 /* we have a full response and we know that we have either a CR
1174 * or an LF at <ptr>.
1175 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001176 //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 +01001177 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1178
1179 msg->sol = ptr;
1180 if (likely(*ptr == '\r'))
1181 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1182 goto http_msg_rpline_end;
1183
1184 http_msg_rpline_end:
1185 case HTTP_MSG_RPLINE_END:
1186 /* msg->sol must point to the first of CR or LF. */
1187 EXPECT_LF_HERE(ptr, http_msg_invalid);
1188 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1189 /* stop here */
1190
1191 /*
1192 * Second, states that are specific to the request only
1193 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 http_msg_rqbefore:
1195 case HTTP_MSG_RQBEFORE:
1196 if (likely(HTTP_IS_TOKEN(*ptr))) {
1197 if (likely(ptr == buf->data)) {
1198 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001199 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 } else {
1201#if PARSE_PRESERVE_EMPTY_LINES
1202 /* only skip empty leading lines, don't remove them */
1203 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001204 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001205#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001206 /* Remove empty leading lines, as recommended by
1207 * RFC2616. This takes a lot of time because we
1208 * must move all the buffer backwards, but this
1209 * is rarely needed. The method above will be
1210 * cleaner when we'll be able to start sending
1211 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001212 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001213 buf->lr = ptr;
1214 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001215 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001216 msg->sol = buf->data;
1217 ptr = buf->data;
1218 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001219#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001220 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001221 /* we will need this when keep-alive will be supported
1222 hdr_idx_init(idx);
1223 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001224 state = HTTP_MSG_RQMETH;
1225 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001227
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001228 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1229 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001230
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 if (unlikely(*ptr == '\n'))
1232 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1233 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 http_msg_rqbefore_cr:
1237 case HTTP_MSG_RQBEFORE_CR:
1238 EXPECT_LF_HERE(ptr, http_msg_invalid);
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001240 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 http_msg_rqmeth:
1243 case HTTP_MSG_RQMETH:
1244 case HTTP_MSG_RQMETH_SP:
1245 case HTTP_MSG_RQURI:
1246 case HTTP_MSG_RQURI_SP:
1247 case HTTP_MSG_RQVER:
1248 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001249 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 if (unlikely(!ptr))
1251 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001252
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 /* we have a full request and we know that we have either a CR
1254 * or an LF at <ptr>.
1255 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001256 //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 +01001257 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259 msg->sol = ptr;
1260 if (likely(*ptr == '\r'))
1261 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 http_msg_rqline_end:
1265 case HTTP_MSG_RQLINE_END:
1266 /* check for HTTP/0.9 request : no version information available.
1267 * msg->sol must point to the first of CR or LF.
1268 */
1269 if (unlikely(msg->sl.rq.v_l == 0))
1270 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 EXPECT_LF_HERE(ptr, http_msg_invalid);
1273 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001275
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 /*
1277 * Common states below
1278 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 http_msg_hdr_first:
1280 case HTTP_MSG_HDR_FIRST:
1281 msg->sol = ptr;
1282 if (likely(!HTTP_IS_CRLF(*ptr))) {
1283 goto http_msg_hdr_name;
1284 }
1285
1286 if (likely(*ptr == '\r'))
1287 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1288 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 http_msg_hdr_name:
1291 case HTTP_MSG_HDR_NAME:
1292 /* assumes msg->sol points to the first char */
1293 if (likely(HTTP_IS_TOKEN(*ptr)))
1294 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 if (likely(*ptr == ':')) {
1297 msg->col = ptr - buf->data;
1298 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1299 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001300
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 http_msg_hdr_l1_sp:
1304 case HTTP_MSG_HDR_L1_SP:
1305 /* assumes msg->sol points to the first char and msg->col to the colon */
1306 if (likely(HTTP_IS_SPHT(*ptr)))
1307 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 /* header value can be basically anything except CR/LF */
1310 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001311
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 if (likely(!HTTP_IS_CRLF(*ptr))) {
1313 goto http_msg_hdr_val;
1314 }
1315
1316 if (likely(*ptr == '\r'))
1317 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1318 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 http_msg_hdr_l1_lf:
1321 case HTTP_MSG_HDR_L1_LF:
1322 EXPECT_LF_HERE(ptr, http_msg_invalid);
1323 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 http_msg_hdr_l1_lws:
1326 case HTTP_MSG_HDR_L1_LWS:
1327 if (likely(HTTP_IS_SPHT(*ptr))) {
1328 /* replace HT,CR,LF with spaces */
1329 for (; buf->data+msg->sov < ptr; msg->sov++)
1330 buf->data[msg->sov] = ' ';
1331 goto http_msg_hdr_l1_sp;
1332 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001333 /* we had a header consisting only in spaces ! */
1334 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 goto http_msg_complete_header;
1336
1337 http_msg_hdr_val:
1338 case HTTP_MSG_HDR_VAL:
1339 /* assumes msg->sol points to the first char, msg->col to the
1340 * colon, and msg->sov points to the first character of the
1341 * value.
1342 */
1343 if (likely(!HTTP_IS_CRLF(*ptr)))
1344 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 msg->eol = ptr;
1347 /* Note: we could also copy eol into ->eoh so that we have the
1348 * real header end in case it ends with lots of LWS, but is this
1349 * really needed ?
1350 */
1351 if (likely(*ptr == '\r'))
1352 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1353 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001355 http_msg_hdr_l2_lf:
1356 case HTTP_MSG_HDR_L2_LF:
1357 EXPECT_LF_HERE(ptr, http_msg_invalid);
1358 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001359
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 http_msg_hdr_l2_lws:
1361 case HTTP_MSG_HDR_L2_LWS:
1362 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1363 /* LWS: replace HT,CR,LF with spaces */
1364 for (; msg->eol < ptr; msg->eol++)
1365 *msg->eol = ' ';
1366 goto http_msg_hdr_val;
1367 }
1368 http_msg_complete_header:
1369 /*
1370 * It was a new header, so the last one is finished.
1371 * Assumes msg->sol points to the first char, msg->col to the
1372 * colon, msg->sov points to the first character of the value
1373 * and msg->eol to the first CR or LF so we know how the line
1374 * ends. We insert last header into the index.
1375 */
1376 /*
1377 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1378 write(2, msg->sol, msg->eol-msg->sol);
1379 fprintf(stderr,"\n");
1380 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1383 idx, idx->tail) < 0))
1384 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 msg->sol = ptr;
1387 if (likely(!HTTP_IS_CRLF(*ptr))) {
1388 goto http_msg_hdr_name;
1389 }
1390
1391 if (likely(*ptr == '\r'))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1393 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 http_msg_last_lf:
1396 case HTTP_MSG_LAST_LF:
1397 /* Assumes msg->sol points to the first of either CR or LF */
1398 EXPECT_LF_HERE(ptr, http_msg_invalid);
1399 ptr++;
1400 buf->lr = ptr;
1401 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001402 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 return;
1404#ifdef DEBUG_FULL
1405 default:
1406 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1407 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001408#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 }
1410 http_msg_ood:
1411 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001412 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 buf->lr = ptr;
1414 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 http_msg_invalid:
1417 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001418 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 return;
1420}
1421
1422/*
1423 * manages the client FSM and its socket. BTW, it also tries to handle the
1424 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1425 * 0 else.
1426 */
1427int process_cli(struct session *t)
1428{
1429 int s = t->srv_state;
1430 int c = t->cli_state;
1431 struct buffer *req = t->req;
1432 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1435 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001436 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 req->rex.tv_sec, req->rex.tv_usec,
1438 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (c == CL_STHEADERS) {
1441 /*
1442 * Now parse the partial (or complete) lines.
1443 * We will check the request syntax, and also join multi-line
1444 * headers. An index of all the lines will be elaborated while
1445 * parsing.
1446 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001450 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 * req->data + req->eoh = end of processed headers / start of current one
1452 * req->data + req->eol = end of current header or line (LF or CRLF)
1453 * req->lr = first non-visited byte
1454 * req->r = end of data
1455 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001456
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001458 struct http_txn *txn = &t->txn;
1459 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001463 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 /* 1: we might have to print this header in debug mode */
1466 if (unlikely((global.mode & MODE_DEBUG) &&
1467 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001470
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001471 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 eol = sol + msg->sl.rq.l;
1473 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001474
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001475 sol += hdr_idx_first_pos(&txn->hdr_idx);
1476 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001479 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001481 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1482 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 }
1484 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001485
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
1487 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001489 * If not so, we check the FD and buffer states before leaving.
1490 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1492 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493 *
1494 */
1495
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001496 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 /*
1498 * First, let's catch bad requests.
1499 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001500 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
1503 /* 1: Since we are in header mode, if there's no space
1504 * left for headers, we won't be able to free more
1505 * later, so the session will never terminate. We
1506 * must terminate it now.
1507 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (unlikely(req->l >= req->rlim - req->data)) {
1509 /* FIXME: check if URI is set and return Status
1510 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001511 */
Willy Tarreau06619262006-12-17 08:37:22 +01001512 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001513 }
1514
1515 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1517 /* read error, or last read : give up. */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001518 tv_eternity(&req->rex);
1519 fd_delete(t->cli_fd);
1520 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001521 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522 if (!(t->flags & SN_ERR_MASK))
1523 t->flags |= SN_ERR_CLICL;
1524 if (!(t->flags & SN_FINST_MASK))
1525 t->flags |= SN_FINST_R;
1526 return 1;
1527 }
1528
1529 /* 3: has the read timeout expired ? */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001530 else if (unlikely(tv_isle(&req->rex, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001531 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001532 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001533 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001534 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001535 if (!(t->flags & SN_ERR_MASK))
1536 t->flags |= SN_ERR_CLITO;
1537 if (!(t->flags & SN_FINST_MASK))
1538 t->flags |= SN_FINST_R;
1539 return 1;
1540 }
1541
1542 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001543 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001544 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545 * full. We cannot loop here since stream_sock_read will disable it only if
1546 * req->l == rlim-data
1547 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001548 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001549 tv_eternity(&req->rex);
1550 }
1551 return t->cli_state != CL_STHEADERS;
1552 }
1553
1554
1555 /****************************************************************
1556 * More interesting part now : we know that we have a complete *
1557 * request which at least looks like HTTP. We have an indicator *
1558 * of each header's length, so we can parse them quickly. *
1559 ****************************************************************/
1560
Willy Tarreau9cdde232007-05-02 20:58:19 +02001561 /* ensure we keep this pointer to the beginning of the message */
1562 msg->sol = req->data + msg->som;
1563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 /*
1565 * 1: identify the method
1566 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001567 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001568
1569 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001571 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001573 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 if (unlikely((t->fe->monitor_uri_len != 0) &&
1575 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1576 !memcmp(&req->data[msg->sl.rq.u],
1577 t->fe->monitor_uri,
1578 t->fe->monitor_uri_len))) {
1579 /*
1580 * We have found the monitor URI
1581 */
1582 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001583 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 client_retnclose(t, &http_200_chunk);
1585 goto return_prx_cond;
1586 }
1587
1588 /*
1589 * 3: Maybe we have to copy the original REQURI for the logs ?
1590 * Note: we cannot log anymore if the request has been
1591 * classified as invalid.
1592 */
1593 if (unlikely(t->logs.logwait & LW_REQ)) {
1594 /* we have a complete HTTP request that we must log */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001595 if ((txn->uri = pool_alloc(requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 int urilen = msg->sl.rq.l;
1597
1598 if (urilen >= REQURI_LEN)
1599 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001600 memcpy(txn->uri, &req->data[msg->som], urilen);
1601 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602
1603 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001604 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 } else {
1606 Alert("HTTP logging : out of memory.\n");
1607 }
1608 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001609
Willy Tarreau06619262006-12-17 08:37:22 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1612 if (unlikely(msg->sl.rq.v_l == 0)) {
1613 int delta;
1614 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001615 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 cur_end = msg->sol + msg->sl.rq.l;
1617 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 if (msg->sl.rq.u_l == 0) {
1620 /* if no URI was set, add "/" */
1621 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1622 cur_end += delta;
1623 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001624 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 /* add HTTP version */
1626 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1627 msg->eoh += delta;
1628 cur_end += delta;
1629 cur_end = (char *)http_parse_reqline(msg, req->data,
1630 HTTP_MSG_RQMETH,
1631 msg->sol, cur_end + 1,
1632 NULL, NULL);
1633 if (unlikely(!cur_end))
1634 goto return_bad_req;
1635
1636 /* we have a full HTTP/1.0 request now and we know that
1637 * we have either a CR or an LF at <ptr>.
1638 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001639 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001640 }
1641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642
1643 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001644 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001645 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001646 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001647
1648 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001650 * As opposed to version 1.2, now they will be evaluated in the
1651 * filters order and not in the header order. This means that
1652 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001653 *
1654 * We can now check whether we want to switch to another
1655 * backend, in which case we will re-check the backend's
1656 * filters and various options. In order to support 3-level
1657 * switching, here's how we should proceed :
1658 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001659 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001660 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001661 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001662 * There cannot be any switch from there, so ->be cannot be
1663 * changed anymore.
1664 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001665 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001666 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001667 * The response path will be able to apply either ->be, or
1668 * ->be then ->fe filters in order to match the reverse of
1669 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670 */
1671
Willy Tarreau06619262006-12-17 08:37:22 +01001672 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001673 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001674 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001675 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001676
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001677 /* first check whether we have some ACLs set to block this request */
1678 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
1679 int ret = acl_exec_cond(cond, cur_proxy, t, txn);
1680 if (cond->pol == ACL_COND_UNLESS)
1681 ret = !ret;
1682
1683 if (ret) {
1684 txn->status = 403;
1685 /* let's log the request time */
1686 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1687 client_retnclose(t, error_message(t, HTTP_ERR_403));
1688 goto return_prx_cond;
1689 }
1690 }
1691
Willy Tarreau06619262006-12-17 08:37:22 +01001692 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001693 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1695 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001696 }
1697
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001698 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1699 /* to ensure correct connection accounting on
1700 * the backend, we count the connection for the
1701 * one managing the queue.
1702 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001703 t->be->beconn++;
1704 if (t->be->beconn > t->be->beconn_max)
1705 t->be->beconn_max = t->be->beconn;
1706 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001707 t->flags |= SN_BE_ASSIGNED;
1708 }
1709
Willy Tarreau06619262006-12-17 08:37:22 +01001710 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001711 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001712 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001713 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001714 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001715 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001716 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001717 goto return_prx_cond;
1718 }
1719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001721 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 !(t->flags & SN_CONN_CLOSED)) {
1723 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001724 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001726
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001727 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 old_idx = 0;
1729
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001730 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1731 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 cur_ptr = cur_next;
1733 cur_end = cur_ptr + cur_hdr->len;
1734 cur_next = cur_end + cur_hdr->cr + 1;
1735
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001736 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1737 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 /* 3 possibilities :
1739 * - we have already set Connection: close,
1740 * so we remove this line.
1741 * - we have not yet set Connection: close,
1742 * but this line indicates close. We leave
1743 * it untouched and set the flag.
1744 * - we have not yet set Connection: close,
1745 * and this line indicates non-close. We
1746 * replace it.
1747 */
1748 if (t->flags & SN_CONN_CLOSED) {
1749 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001750 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001752 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1753 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 cur_hdr->len = 0;
1755 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001756 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1757 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1758 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 cur_next += delta;
1760 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001761 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 }
1763 t->flags |= SN_CONN_CLOSED;
1764 }
1765 }
1766 old_idx = cur_idx;
1767 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001768 }
1769 /* add request headers from the rule sets in the same order */
1770 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1771 if (unlikely(http_header_add_tail(req,
1772 &txn->req,
1773 &txn->hdr_idx,
1774 rule_set->req_add[cur_idx])) < 0)
1775 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001776 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001777
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001779 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001780 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001781 /* we have to check the URI and auth for this request */
1782 if (stats_check_uri_auth(t, rule_set))
1783 return 1;
1784 }
1785
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001786 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1787 /* No backend was set, but there was a default
1788 * backend set in the frontend, so we use it and
1789 * loop again.
1790 */
1791 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001792 t->be->beconn++;
1793 if (t->be->beconn > t->be->beconn_max)
1794 t->be->beconn_max = t->be->beconn;
1795 t->be->cum_beconn++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001796 t->flags |= SN_BE_ASSIGNED;
1797 }
1798 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001799
Willy Tarreau58f10d72006-12-04 02:26:12 +01001800
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001801 if (!(t->flags & SN_BE_ASSIGNED)) {
1802 /* To ensure correct connection accounting on
1803 * the backend, we count the connection for the
1804 * one managing the queue.
1805 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001806 t->be->beconn++;
1807 if (t->be->beconn > t->be->beconn_max)
1808 t->be->beconn_max = t->be->beconn;
1809 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001810 t->flags |= SN_BE_ASSIGNED;
1811 }
1812
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001813 /*
1814 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001815 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001816 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001817 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001818 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001819
Willy Tarreau58f10d72006-12-04 02:26:12 +01001820
Willy Tarreau58f10d72006-12-04 02:26:12 +01001821
Willy Tarreau58f10d72006-12-04 02:26:12 +01001822
Willy Tarreau2a324282006-12-05 00:05:46 +01001823 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001825 * so let's do the same now.
1826 */
1827
1828 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001829 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001830 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001831 }
1832
1833
1834 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001836 * Note that doing so might move headers in the request, but
1837 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001838 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001839 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001840 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001841 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001842
Willy Tarreau58f10d72006-12-04 02:26:12 +01001843
Willy Tarreau2a324282006-12-05 00:05:46 +01001844 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001845 * 9: add X-Forwarded-For if either the frontend or the backend
1846 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001847 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001848 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001849 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001850 /* Add an X-Forwarded-For header unless the source IP is
1851 * in the 'except' network range.
1852 */
1853 if ((!t->fe->except_mask.s_addr ||
1854 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1855 != t->fe->except_net.s_addr) &&
1856 (!t->be->except_mask.s_addr ||
1857 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1858 != t->be->except_net.s_addr)) {
1859 int len;
1860 unsigned char *pn;
1861 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001862
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001863 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1864 pn[0], pn[1], pn[2], pn[3]);
1865
1866 if (unlikely(http_header_add_tail2(req, &txn->req,
1867 &txn->hdr_idx, trash, len)) < 0)
1868 goto return_bad_req;
1869 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001870 }
1871 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001872 /* FIXME: for the sake of completeness, we should also support
1873 * 'except' here, although it is mostly useless in this case.
1874 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001875 int len;
1876 char pn[INET6_ADDRSTRLEN];
1877 inet_ntop(AF_INET6,
1878 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1879 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001880 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1881 if (unlikely(http_header_add_tail2(req, &txn->req,
1882 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001883 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001884 }
1885 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001886
Willy Tarreau2a324282006-12-05 00:05:46 +01001887 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02001889 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01001890 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02001891 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001892 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02001893 if ((unlikely(msg->sl.rq.v_l != 8) ||
1894 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
1895 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001896 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001897 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001898 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001899 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001900
Willy Tarreau2a324282006-12-05 00:05:46 +01001901 /*************************************************************
1902 * OK, that's finished for the headers. We have done what we *
1903 * could. Let's switch to the DATA state. *
1904 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001905
Willy Tarreau2a324282006-12-05 00:05:46 +01001906 t->cli_state = CL_STDATA;
1907 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001908
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001909 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001910
Willy Tarreaud825eef2007-05-12 22:35:00 +02001911 if (!tv_isset(&t->fe->clitimeout) ||
1912 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001913 /* If the client has no timeout, or if the server is not ready yet,
1914 * and we know for sure that it can expire, then it's cleaner to
1915 * disable the timeout on the client side so that too low values
1916 * cannot make the sessions abort too early.
1917 *
1918 * FIXME-20050705: the server needs a way to re-enable this time-out
1919 * when it switches its state, otherwise a client can stay connected
1920 * indefinitely. This now seems to be OK.
1921 */
1922 tv_eternity(&req->rex);
1923 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001924
Willy Tarreau2a324282006-12-05 00:05:46 +01001925 /* When a connection is tarpitted, we use the queue timeout for the
1926 * tarpit delay, which currently happens to be the server's connect
1927 * timeout. If unset, then set it to zero because we really want it
1928 * to expire at one moment.
1929 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001930 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001931 t->req->l = 0;
1932 /* flush the request so that we can drop the connection early
1933 * if the client closes first.
1934 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001935 if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout))
Willy Tarreaud825eef2007-05-12 22:35:00 +02001936 req->cex = now;
Willy Tarreau2a324282006-12-05 00:05:46 +01001937 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001939 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01001940 goto process_data;
1941
1942 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001943 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001944 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01001945 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001946 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01001947 return_prx_cond:
1948 if (!(t->flags & SN_ERR_MASK))
1949 t->flags |= SN_ERR_PRXCOND;
1950 if (!(t->flags & SN_FINST_MASK))
1951 t->flags |= SN_FINST_R;
1952 return 1;
1953
Willy Tarreaubaaee002006-06-26 02:48:02 +02001954 }
1955 else if (c == CL_STDATA) {
1956 process_data:
1957 /* FIXME: this error handling is partly buggy because we always report
1958 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1959 * or HEADER phase. BTW, it's not logical to expire the client while
1960 * we're waiting for the server to connect.
1961 */
1962 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001963 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001964 tv_eternity(&req->rex);
1965 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001966 fd_delete(t->cli_fd);
1967 t->cli_state = CL_STCLOSE;
1968 if (!(t->flags & SN_ERR_MASK))
1969 t->flags |= SN_ERR_CLICL;
1970 if (!(t->flags & SN_FINST_MASK)) {
1971 if (t->pend_pos)
1972 t->flags |= SN_FINST_Q;
1973 else if (s == SV_STCONN)
1974 t->flags |= SN_FINST_C;
1975 else
1976 t->flags |= SN_FINST_D;
1977 }
1978 return 1;
1979 }
1980 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001981 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001982 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02001983 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001984 t->cli_state = CL_STSHUTR;
1985 return 1;
1986 }
1987 /* last server read and buffer empty */
1988 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001989 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02001990 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001991 shutdown(t->cli_fd, SHUT_WR);
1992 /* We must ensure that the read part is still alive when switching
1993 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02001994 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001995 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001996 t->cli_state = CL_STSHUTW;
1997 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1998 return 1;
1999 }
2000 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002001 else if (tv_isle(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002002 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02002003 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002004 t->cli_state = CL_STSHUTR;
2005 if (!(t->flags & SN_ERR_MASK))
2006 t->flags |= SN_ERR_CLITO;
2007 if (!(t->flags & SN_FINST_MASK)) {
2008 if (t->pend_pos)
2009 t->flags |= SN_FINST_Q;
2010 else if (s == SV_STCONN)
2011 t->flags |= SN_FINST_C;
2012 else
2013 t->flags |= SN_FINST_D;
2014 }
2015 return 1;
2016 }
2017 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002018 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002019 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002020 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002021 shutdown(t->cli_fd, SHUT_WR);
2022 /* We must ensure that the read part is still alive when switching
2023 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002024 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002025 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002026
2027 t->cli_state = CL_STSHUTW;
2028 if (!(t->flags & SN_ERR_MASK))
2029 t->flags |= SN_ERR_CLITO;
2030 if (!(t->flags & SN_FINST_MASK)) {
2031 if (t->pend_pos)
2032 t->flags |= SN_FINST_Q;
2033 else if (s == SV_STCONN)
2034 t->flags |= SN_FINST_C;
2035 else
2036 t->flags |= SN_FINST_D;
2037 }
2038 return 1;
2039 }
2040
2041 if (req->l >= req->rlim - req->data) {
2042 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002043 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002044 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002045 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002046 }
2047 } else {
2048 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002049 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud825eef2007-05-12 22:35:00 +02002050 if (!tv_isset(&t->fe->clitimeout) ||
2051 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout)))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002052 /* If the client has no timeout, or if the server not ready yet, and we
2053 * know for sure that it can expire, then it's cleaner to disable the
2054 * timeout on the client side so that too low values cannot make the
2055 * sessions abort too early.
2056 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002057 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002058 else
Willy Tarreaud825eef2007-05-12 22:35:00 +02002059 tv_add(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002060 }
2061 }
2062
2063 if ((rep->l == 0) ||
2064 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002065 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2066 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002067 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002068 }
2069 } else {
2070 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002071 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2072 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002073 if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002074 /* FIXME: to prevent the client from expiring read timeouts during writes,
2075 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002076 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002077 }
2078 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002079 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002080 }
2081 }
2082 return 0; /* other cases change nothing */
2083 }
2084 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002085 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002086 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002087 fd_delete(t->cli_fd);
2088 t->cli_state = CL_STCLOSE;
2089 if (!(t->flags & SN_ERR_MASK))
2090 t->flags |= SN_ERR_CLICL;
2091 if (!(t->flags & SN_FINST_MASK)) {
2092 if (t->pend_pos)
2093 t->flags |= SN_FINST_Q;
2094 else if (s == SV_STCONN)
2095 t->flags |= SN_FINST_C;
2096 else
2097 t->flags |= SN_FINST_D;
2098 }
2099 return 1;
2100 }
2101 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2102 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002103 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002104 fd_delete(t->cli_fd);
2105 t->cli_state = CL_STCLOSE;
2106 return 1;
2107 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002108 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002109 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002110 fd_delete(t->cli_fd);
2111 t->cli_state = CL_STCLOSE;
2112 if (!(t->flags & SN_ERR_MASK))
2113 t->flags |= SN_ERR_CLITO;
2114 if (!(t->flags & SN_FINST_MASK)) {
2115 if (t->pend_pos)
2116 t->flags |= SN_FINST_Q;
2117 else if (s == SV_STCONN)
2118 t->flags |= SN_FINST_C;
2119 else
2120 t->flags |= SN_FINST_D;
2121 }
2122 return 1;
2123 }
2124
2125 if (t->flags & SN_SELF_GEN) {
2126 produce_content(t);
2127 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002128 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002129 fd_delete(t->cli_fd);
2130 t->cli_state = CL_STCLOSE;
2131 return 1;
2132 }
2133 }
2134
2135 if ((rep->l == 0)
2136 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002137 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2138 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002139 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002140 }
2141 } else {
2142 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002143 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2144 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002145 if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002146 /* FIXME: to prevent the client from expiring read timeouts during writes,
2147 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002148 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002149 }
2150 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002151 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002152 }
2153 }
2154 return 0;
2155 }
2156 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002157 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002158 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002159 fd_delete(t->cli_fd);
2160 t->cli_state = CL_STCLOSE;
2161 if (!(t->flags & SN_ERR_MASK))
2162 t->flags |= SN_ERR_CLICL;
2163 if (!(t->flags & SN_FINST_MASK)) {
2164 if (t->pend_pos)
2165 t->flags |= SN_FINST_Q;
2166 else if (s == SV_STCONN)
2167 t->flags |= SN_FINST_C;
2168 else
2169 t->flags |= SN_FINST_D;
2170 }
2171 return 1;
2172 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002173 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002174 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002175 fd_delete(t->cli_fd);
2176 t->cli_state = CL_STCLOSE;
2177 return 1;
2178 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002179 else if (tv_isle(&req->rex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002180 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002181 fd_delete(t->cli_fd);
2182 t->cli_state = CL_STCLOSE;
2183 if (!(t->flags & SN_ERR_MASK))
2184 t->flags |= SN_ERR_CLITO;
2185 if (!(t->flags & SN_FINST_MASK)) {
2186 if (t->pend_pos)
2187 t->flags |= SN_FINST_Q;
2188 else if (s == SV_STCONN)
2189 t->flags |= SN_FINST_C;
2190 else
2191 t->flags |= SN_FINST_D;
2192 }
2193 return 1;
2194 }
2195 else if (req->l >= req->rlim - req->data) {
2196 /* no room to read more data */
2197
2198 /* FIXME-20050705: is it possible for a client to maintain a session
2199 * after the timeout by sending more data after it receives a close ?
2200 */
2201
Willy Tarreau66319382007-04-08 17:17:37 +02002202 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002203 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002204 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002205 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2206 }
2207 } else {
2208 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002209 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002210 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002211 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002212 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2213 }
2214 }
2215 return 0;
2216 }
2217 else { /* CL_STCLOSE: nothing to do */
2218 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2219 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002220 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 +02002221 write(1, trash, len);
2222 }
2223 return 0;
2224 }
2225 return 0;
2226}
2227
2228
2229/*
2230 * manages the server FSM and its socket. It returns 1 if a state has changed
2231 * (and a resync may be needed), 0 else.
2232 */
2233int process_srv(struct session *t)
2234{
2235 int s = t->srv_state;
2236 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002237 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002238 struct buffer *req = t->req;
2239 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002240 int conn_err;
2241
2242#ifdef DEBUG_FULL
2243 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2244#endif
2245 //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 +02002246 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2247 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002248 //);
2249 if (s == SV_STIDLE) {
2250 if (c == CL_STHEADERS)
2251 return 0; /* stay in idle, waiting for data to reach the client side */
2252 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2253 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002254 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002255 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002256 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002257 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002258 /* note that this must not return any error because it would be able to
2259 * overwrite the client_retnclose() output.
2260 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002261 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002262 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002263 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002264 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 +02002265
2266 return 1;
2267 }
2268 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002269 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002270 /* This connection is being tarpitted. The CLIENT side has
2271 * already set the connect expiration date to the right
2272 * timeout. We just have to check that it has not expired.
2273 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002274 if (!tv_isle(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002275 return 0;
2276
2277 /* We will set the queue timer to the time spent, just for
2278 * logging purposes. We fake a 500 server error, so that the
2279 * attacker will not suspect his connection has been tarpitted.
2280 * It will not cause trouble to the logs because we can exclude
2281 * the tarpitted connections by filtering on the 'PT' status flags.
2282 */
2283 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002284 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002285 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002286 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002287 return 1;
2288 }
2289
Willy Tarreaubaaee002006-06-26 02:48:02 +02002290 /* Right now, we will need to create a connection to the server.
2291 * We might already have tried, and got a connection pending, in
2292 * which case we will not do anything till it's pending. It's up
2293 * to any other session to release it and wake us up again.
2294 */
2295 if (t->pend_pos) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002296 if (!tv_isle(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002297 return 0;
2298 else {
2299 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002300 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002301 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002302 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002303 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002304 if (t->srv)
2305 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002306 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002307 return 1;
2308 }
2309 }
2310
2311 do {
2312 /* first, get a connection */
2313 if (srv_redispatch_connect(t))
2314 return t->srv_state != SV_STIDLE;
2315
2316 /* try to (re-)connect to the server, and fail if we expire the
2317 * number of retries.
2318 */
2319 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002320 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002321 return t->srv_state != SV_STIDLE;
2322 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002323 } while (1);
2324 }
2325 }
2326 else if (s == SV_STCONN) { /* connection in progress */
2327 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2328 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002329 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2330 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002331 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002332 fd_delete(t->srv_fd);
2333 if (t->srv)
2334 t->srv->cur_sess--;
2335
2336 /* note that this must not return any error because it would be able to
2337 * overwrite the client_retnclose() output.
2338 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002339 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002340 return 1;
2341 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002342 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002343 //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 +02002344 return 0; /* nothing changed */
2345 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002346 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002347 /* timeout, asynchronous connect error or first write error */
2348 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2349
2350 fd_delete(t->srv_fd);
2351 if (t->srv)
2352 t->srv->cur_sess--;
2353
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002354 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002355 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2356 else
2357 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2358
2359 /* ensure that we have enough retries left */
2360 if (srv_count_retry_down(t, conn_err))
2361 return 1;
2362
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002363 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002364 /* We're on our last chance, and the REDISP option was specified.
2365 * We will ignore cookie and force to balance or use the dispatcher.
2366 */
2367 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002368 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002369 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002370
2371 if (t->srv)
2372 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002373 t->be->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002374
2375 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2376 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002377 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002378
2379 /* first, get a connection */
2380 if (srv_redispatch_connect(t))
2381 return t->srv_state != SV_STIDLE;
2382 }
2383
Willy Tarreaubaaee002006-06-26 02:48:02 +02002384 do {
2385 /* Now we will try to either reconnect to the same server or
2386 * connect to another server. If the connection gets queued
2387 * because all servers are saturated, then we will go back to
2388 * the SV_STIDLE state.
2389 */
2390 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002391 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002392 return t->srv_state != SV_STCONN;
2393 }
2394
2395 /* we need to redispatch the connection to another server */
2396 if (srv_redispatch_connect(t))
2397 return t->srv_state != SV_STCONN;
2398 } while (1);
2399 }
2400 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002401 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402
2403 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2404 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002405 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002406 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002407 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002408 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002409 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002410 /* FIXME: to prevent the server from expiring read timeouts during writes,
2411 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002412 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002413 }
2414 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002415 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002416 }
2417
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002418 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002419 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002420 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002421 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002422
2423 t->srv_state = SV_STDATA;
2424 if (t->srv)
2425 t->srv->cum_sess++;
2426 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2427
2428 /* if the user wants to log as soon as possible, without counting
2429 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002430 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002431 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002432 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002433 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002434#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002435 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002436 /* TCP splicing supported by both FE and BE */
2437 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2438 }
2439#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002440 }
2441 else {
2442 t->srv_state = SV_STHEADERS;
2443 if (t->srv)
2444 t->srv->cum_sess++;
2445 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002446 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2447 /* reset hdr_idx which was already initialized by the request.
2448 * right now, the http parser does it.
2449 * hdr_idx_init(&t->txn.hdr_idx);
2450 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002451 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002452 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002453 return 1;
2454 }
2455 }
2456 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002457 /*
2458 * Now parse the partial (or complete) lines.
2459 * We will check the response syntax, and also join multi-line
2460 * headers. An index of all the lines will be elaborated while
2461 * parsing.
2462 *
2463 * For the parsing, we use a 28 states FSM.
2464 *
2465 * Here is the information we currently have :
2466 * rep->data + req->som = beginning of response
2467 * rep->data + req->eoh = end of processed headers / start of current one
2468 * rep->data + req->eol = end of current header or line (LF or CRLF)
2469 * rep->lr = first non-visited byte
2470 * rep->r = end of data
2471 */
2472
2473 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002474 struct http_msg *msg = &txn->rsp;
2475 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002476
Willy Tarreaua15645d2007-03-18 16:22:39 +01002477 if (likely(rep->lr < rep->r))
2478 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002479
Willy Tarreaua15645d2007-03-18 16:22:39 +01002480 /* 1: we might have to print this header in debug mode */
2481 if (unlikely((global.mode & MODE_DEBUG) &&
2482 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2483 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2484 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002485
Willy Tarreaua15645d2007-03-18 16:22:39 +01002486 sol = rep->data + msg->som;
2487 eol = sol + msg->sl.rq.l;
2488 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002489
Willy Tarreaua15645d2007-03-18 16:22:39 +01002490 sol += hdr_idx_first_pos(&txn->hdr_idx);
2491 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002492
Willy Tarreaua15645d2007-03-18 16:22:39 +01002493 while (cur_idx) {
2494 eol = sol + txn->hdr_idx.v[cur_idx].len;
2495 debug_hdr("srvhdr", t, sol, eol);
2496 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2497 cur_idx = txn->hdr_idx.v[cur_idx].next;
2498 }
2499 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002500
Willy Tarreaubaaee002006-06-26 02:48:02 +02002501
Willy Tarreau66319382007-04-08 17:17:37 +02002502 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002503 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002504 * full. We cannot loop here since stream_sock_read will disable it only if
2505 * rep->l == rlim-data
2506 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002507 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002508 tv_eternity(&rep->rex);
2509 }
2510
2511
2512 /*
2513 * Now we quickly check if we have found a full valid response.
2514 * If not so, we check the FD and buffer states before leaving.
2515 * A full response is indicated by the fact that we have seen
2516 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2517 * responses are checked first.
2518 *
2519 * Depending on whether the client is still there or not, we
2520 * may send an error response back or not. Note that normally
2521 * we should only check for HTTP status there, and check I/O
2522 * errors somewhere else.
2523 */
2524
2525 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2526
2527 /* Invalid response, or read error or write error */
2528 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2529 (req->flags & BF_WRITE_ERROR) ||
2530 (rep->flags & BF_READ_ERROR))) {
2531 tv_eternity(&rep->rex);
2532 tv_eternity(&req->wex);
2533 fd_delete(t->srv_fd);
2534 if (t->srv) {
2535 t->srv->cur_sess--;
2536 t->srv->failed_resp++;
2537 }
2538 t->be->failed_resp++;
2539 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002540 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002541 client_return(t, error_message(t, HTTP_ERR_502));
2542 if (!(t->flags & SN_ERR_MASK))
2543 t->flags |= SN_ERR_SRVCL;
2544 if (!(t->flags & SN_FINST_MASK))
2545 t->flags |= SN_FINST_H;
2546 /* We used to have a free connection slot. Since we'll never use it,
2547 * we have to inform the server that it may be used by another session.
2548 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002549 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002550 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002551
Willy Tarreaua15645d2007-03-18 16:22:39 +01002552 return 1;
2553 }
2554
2555 /* end of client write or end of server read.
2556 * since we are in header mode, if there's no space left for headers, we
2557 * won't be able to free more later, so the session will never terminate.
2558 */
2559 else if (unlikely(rep->flags & BF_READ_NULL ||
2560 c == CL_STSHUTW || c == CL_STCLOSE ||
2561 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002562 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002563 tv_eternity(&rep->rex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002564 t->srv_state = SV_STSHUTR;
2565 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2566 return 1;
2567 }
2568
2569 /* read timeout : return a 504 to the client.
2570 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002571 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002572 tv_isle(&rep->rex, &now))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002573 tv_eternity(&rep->rex);
2574 tv_eternity(&req->wex);
2575 fd_delete(t->srv_fd);
2576 if (t->srv) {
2577 t->srv->cur_sess--;
2578 t->srv->failed_resp++;
2579 }
2580 t->be->failed_resp++;
2581 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002582 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002583 client_return(t, error_message(t, HTTP_ERR_504));
2584 if (!(t->flags & SN_ERR_MASK))
2585 t->flags |= SN_ERR_SRVTO;
2586 if (!(t->flags & SN_FINST_MASK))
2587 t->flags |= SN_FINST_H;
2588 /* We used to have a free connection slot. Since we'll never use it,
2589 * we have to inform the server that it may be used by another session.
2590 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002591 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002592 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002593 return 1;
2594 }
2595
2596 /* last client read and buffer empty */
2597 /* FIXME!!! here, we don't want to switch to SHUTW if the
2598 * client shuts read too early, because we may still have
2599 * some work to do on the headers.
2600 * The side-effect is that if the client completely closes its
2601 * connection during SV_STHEADER, the connection to the server
2602 * is kept until a response comes back or the timeout is reached.
2603 */
2604 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2605 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002606 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002607 tv_eternity(&req->wex);
2608
2609 /* We must ensure that the read part is still
2610 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002611 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002612 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002613
2614 shutdown(t->srv_fd, SHUT_WR);
2615 t->srv_state = SV_STSHUTW;
2616 return 1;
2617 }
2618
2619 /* write timeout */
2620 /* FIXME!!! here, we don't want to switch to SHUTW if the
2621 * client shuts read too early, because we may still have
2622 * some work to do on the headers.
2623 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002624 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002625 tv_isle(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002626 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002627 tv_eternity(&req->wex);
2628 shutdown(t->srv_fd, SHUT_WR);
2629 /* We must ensure that the read part is still alive
2630 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002631 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002632 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002633
2634 t->srv_state = SV_STSHUTW;
2635 if (!(t->flags & SN_ERR_MASK))
2636 t->flags |= SN_ERR_SRVTO;
2637 if (!(t->flags & SN_FINST_MASK))
2638 t->flags |= SN_FINST_H;
2639 return 1;
2640 }
2641
2642 /*
2643 * And now the non-error cases.
2644 */
2645
2646 /* Data remaining in the request buffer.
2647 * This happens during the first pass here, and during
2648 * long posts.
2649 */
2650 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002651 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2652 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002653 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002654 /* FIXME: to prevent the server from expiring read timeouts during writes,
2655 * we refresh it. */
2656 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002657 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002658 else
2659 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002660 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002661 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002662
Willy Tarreaua15645d2007-03-18 16:22:39 +01002663 /* nothing left in the request buffer */
2664 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002665 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2666 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002667 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002668 }
2669 }
2670
2671 return t->srv_state != SV_STHEADERS;
2672 }
2673
2674
2675 /*****************************************************************
2676 * More interesting part now : we know that we have a complete *
2677 * response which at least looks like HTTP. We have an indicator *
2678 * of each header's length, so we can parse them quickly. *
2679 ****************************************************************/
2680
Willy Tarreau9cdde232007-05-02 20:58:19 +02002681 /* ensure we keep this pointer to the beginning of the message */
2682 msg->sol = rep->data + msg->som;
2683
Willy Tarreaua15645d2007-03-18 16:22:39 +01002684 /*
2685 * 1: get the status code and check for cacheability.
2686 */
2687
2688 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002689 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002690
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002691 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002692 case 200:
2693 case 203:
2694 case 206:
2695 case 300:
2696 case 301:
2697 case 410:
2698 /* RFC2616 @13.4:
2699 * "A response received with a status code of
2700 * 200, 203, 206, 300, 301 or 410 MAY be stored
2701 * by a cache (...) unless a cache-control
2702 * directive prohibits caching."
2703 *
2704 * RFC2616 @9.5: POST method :
2705 * "Responses to this method are not cacheable,
2706 * unless the response includes appropriate
2707 * Cache-Control or Expires header fields."
2708 */
2709 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002710 unlikely(t->be->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002711 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002712 break;
2713 default:
2714 break;
2715 }
2716
2717 /*
2718 * 2: we may need to capture headers
2719 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002720 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002721 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002722 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002723
2724 /*
2725 * 3: we will have to evaluate the filters.
2726 * As opposed to version 1.2, now they will be evaluated in the
2727 * filters order and not in the header order. This means that
2728 * each filter has to be validated among all headers.
2729 *
2730 * Filters are tried with ->be first, then with ->fe if it is
2731 * different from ->be.
2732 */
2733
2734 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2735
2736 cur_proxy = t->be;
2737 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002738 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002739
2740 /* try headers filters */
2741 if (rule_set->rsp_exp != NULL) {
2742 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2743 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002744 if (t->srv) {
2745 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002746 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002747 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002748 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002749 return_srv_prx_502:
2750 tv_eternity(&rep->rex);
2751 tv_eternity(&req->wex);
2752 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002753 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002754 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002755 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002756 if (!(t->flags & SN_ERR_MASK))
2757 t->flags |= SN_ERR_PRXCOND;
2758 if (!(t->flags & SN_FINST_MASK))
2759 t->flags |= SN_FINST_H;
2760 /* We used to have a free connection slot. Since we'll never use it,
2761 * we have to inform the server that it may be used by another session.
2762 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002763 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002764 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002765 return 1;
2766 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002767 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002768
Willy Tarreaua15645d2007-03-18 16:22:39 +01002769 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002770 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002771 if (t->srv) {
2772 t->srv->cur_sess--;
2773 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002774 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002775 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002776 goto return_srv_prx_502;
2777 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002778
Willy Tarreaua15645d2007-03-18 16:22:39 +01002779 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002780 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002781 !(t->flags & SN_CONN_CLOSED)) {
2782 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002783 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002784 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002785
Willy Tarreaua15645d2007-03-18 16:22:39 +01002786 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2787 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002788
Willy Tarreaua15645d2007-03-18 16:22:39 +01002789 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2790 cur_hdr = &txn->hdr_idx.v[cur_idx];
2791 cur_ptr = cur_next;
2792 cur_end = cur_ptr + cur_hdr->len;
2793 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002794
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002795 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2796 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002797 /* 3 possibilities :
2798 * - we have already set Connection: close,
2799 * so we remove this line.
2800 * - we have not yet set Connection: close,
2801 * but this line indicates close. We leave
2802 * it untouched and set the flag.
2803 * - we have not yet set Connection: close,
2804 * and this line indicates non-close. We
2805 * replace it.
2806 */
2807 if (t->flags & SN_CONN_CLOSED) {
2808 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2809 txn->rsp.eoh += delta;
2810 cur_next += delta;
2811 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2812 txn->hdr_idx.used--;
2813 cur_hdr->len = 0;
2814 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002815 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2816 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2817 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002818 cur_next += delta;
2819 cur_hdr->len += delta;
2820 txn->rsp.eoh += delta;
2821 }
2822 t->flags |= SN_CONN_CLOSED;
2823 }
2824 }
2825 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002826 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002827 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002828
Willy Tarreaua15645d2007-03-18 16:22:39 +01002829 /* add response headers from the rule sets in the same order */
2830 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002831 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2832 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002833 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002834 }
2835
Willy Tarreaua15645d2007-03-18 16:22:39 +01002836 /* check whether we're already working on the frontend */
2837 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002838 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002839 cur_proxy = t->fe;
2840 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002841
Willy Tarreaua15645d2007-03-18 16:22:39 +01002842 /*
2843 * 4: check for server cookie.
2844 */
2845 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002846
Willy Tarreaua15645d2007-03-18 16:22:39 +01002847 /*
2848 * 5: add server cookie in the response if needed
2849 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002850 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2851 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002852 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002853
Willy Tarreaua15645d2007-03-18 16:22:39 +01002854 /* the server is known, it's not the one the client requested, we have to
2855 * insert a set-cookie here, except if we want to insert only on POST
2856 * requests and this one isn't. Note that servers which don't have cookies
2857 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002858 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002859 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002860 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01002861 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002862
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002863 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2864 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002865 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002866 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002867
Willy Tarreaua15645d2007-03-18 16:22:39 +01002868 /* Here, we will tell an eventual cache on the client side that we don't
2869 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2870 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2871 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2872 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002873 if (t->be->options & PR_O_COOK_NOC) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002874 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2875 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002876 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002877 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002878 }
2879
2880
2881 /*
2882 * 6: check for cache-control or pragma headers.
2883 */
2884 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002885
Willy Tarreaubaaee002006-06-26 02:48:02 +02002886
Willy Tarreaua15645d2007-03-18 16:22:39 +01002887 /*
2888 * 7: check if result will be cacheable with a cookie.
2889 * We'll block the response if security checks have caught
2890 * nasty things such as a cacheable cookie.
2891 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002892 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2893 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002894 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895
Willy Tarreaua15645d2007-03-18 16:22:39 +01002896 /* we're in presence of a cacheable response containing
2897 * a set-cookie header. We'll block it as requested by
2898 * the 'checkcache' option, and send an alert.
2899 */
2900 if (t->srv) {
2901 t->srv->cur_sess--;
2902 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002903 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002904 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002905
Willy Tarreaua15645d2007-03-18 16:22:39 +01002906 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002907 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002908 send_log(t->be, LOG_ALERT,
2909 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002910 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002911 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002912 }
2913
Willy Tarreaua15645d2007-03-18 16:22:39 +01002914 /*
2915 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002916 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002917 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002918 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002919 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002920 if ((unlikely(msg->sl.st.v_l != 8) ||
2921 unlikely(req->data[msg->som + 7] != '0')) &&
2922 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002923 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002924 goto return_bad_resp;
2925 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002926 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002927
Willy Tarreaubaaee002006-06-26 02:48:02 +02002928
Willy Tarreaua15645d2007-03-18 16:22:39 +01002929 /*************************************************************
2930 * OK, that's finished for the headers. We have done what we *
2931 * could. Let's switch to the DATA state. *
2932 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002933
Willy Tarreaua15645d2007-03-18 16:22:39 +01002934 t->srv_state = SV_STDATA;
2935 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002936 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002937
2938 /* client connection already closed or option 'forceclose' required :
2939 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002940 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002941 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002942 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002943 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002944 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002945
2946 /* We must ensure that the read part is still alive when switching
2947 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002948 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002949 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002950
Willy Tarreaua15645d2007-03-18 16:22:39 +01002951 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002952 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002953 }
2954
Willy Tarreaua15645d2007-03-18 16:22:39 +01002955#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002956 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002957 /* TCP splicing supported by both FE and BE */
2958 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002959 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002960#endif
2961 /* if the user wants to log as soon as possible, without counting
2962 bytes from the server, then this is the right moment. */
2963 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2964 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002965 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02002966 if (t->fe->to_log & LW_REQ)
2967 http_sess_log(t);
2968 else
2969 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002970 }
2971
Willy Tarreaua15645d2007-03-18 16:22:39 +01002972 /* Note: we must not try to cheat by jumping directly to DATA,
2973 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002974 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002975
2976 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002977 }
2978 else if (s == SV_STDATA) {
2979 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002980 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002981 tv_eternity(&rep->rex);
2982 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002983 fd_delete(t->srv_fd);
2984 if (t->srv) {
2985 t->srv->cur_sess--;
2986 t->srv->failed_resp++;
2987 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002988 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002989 t->srv_state = SV_STCLOSE;
2990 if (!(t->flags & SN_ERR_MASK))
2991 t->flags |= SN_ERR_SRVCL;
2992 if (!(t->flags & SN_FINST_MASK))
2993 t->flags |= SN_FINST_D;
2994 /* We used to have a free connection slot. Since we'll never use it,
2995 * we have to inform the server that it may be used by another session.
2996 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002997 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002998 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002999
3000 return 1;
3001 }
3002 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003003 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003004 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003005 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003006 t->srv_state = SV_STSHUTR;
3007 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3008 return 1;
3009 }
3010 /* end of client read and no more data to send */
3011 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003012 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003013 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003014 shutdown(t->srv_fd, SHUT_WR);
3015 /* We must ensure that the read part is still alive when switching
3016 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003017 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003018 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003019
3020 t->srv_state = SV_STSHUTW;
3021 return 1;
3022 }
3023 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003024 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003025 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003026 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003027 t->srv_state = SV_STSHUTR;
3028 if (!(t->flags & SN_ERR_MASK))
3029 t->flags |= SN_ERR_SRVTO;
3030 if (!(t->flags & SN_FINST_MASK))
3031 t->flags |= SN_FINST_D;
3032 return 1;
3033 }
3034 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003035 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003036 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003037 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003038 shutdown(t->srv_fd, SHUT_WR);
3039 /* We must ensure that the read part is still alive when switching
3040 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003041 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003042 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003043 t->srv_state = SV_STSHUTW;
3044 if (!(t->flags & SN_ERR_MASK))
3045 t->flags |= SN_ERR_SRVTO;
3046 if (!(t->flags & SN_FINST_MASK))
3047 t->flags |= SN_FINST_D;
3048 return 1;
3049 }
3050
3051 /* recompute request time-outs */
3052 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003053 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3054 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003055 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003056 }
3057 }
3058 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003059 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3060 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003061 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003062 /* FIXME: to prevent the server from expiring read timeouts during writes,
3063 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003064 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003065 }
3066 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003067 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003068 }
3069 }
3070
3071 /* recompute response time-outs */
3072 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003073 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003074 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003075 }
3076 }
3077 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003078 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003079 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003080 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003081 }
3082 }
3083
3084 return 0; /* other cases change nothing */
3085 }
3086 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003087 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003088 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003089 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003090 fd_delete(t->srv_fd);
3091 if (t->srv) {
3092 t->srv->cur_sess--;
3093 t->srv->failed_resp++;
3094 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003095 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003096 //close(t->srv_fd);
3097 t->srv_state = SV_STCLOSE;
3098 if (!(t->flags & SN_ERR_MASK))
3099 t->flags |= SN_ERR_SRVCL;
3100 if (!(t->flags & SN_FINST_MASK))
3101 t->flags |= SN_FINST_D;
3102 /* We used to have a free connection slot. Since we'll never use it,
3103 * we have to inform the server that it may be used by another session.
3104 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003105 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003106 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003107
3108 return 1;
3109 }
3110 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003111 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003112 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003113 fd_delete(t->srv_fd);
3114 if (t->srv)
3115 t->srv->cur_sess--;
3116 //close(t->srv_fd);
3117 t->srv_state = SV_STCLOSE;
3118 /* We used to have a free connection slot. Since we'll never use it,
3119 * we have to inform the server that it may be used by another session.
3120 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003121 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003122 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003123
3124 return 1;
3125 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003126 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003127 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003128 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003129 fd_delete(t->srv_fd);
3130 if (t->srv)
3131 t->srv->cur_sess--;
3132 //close(t->srv_fd);
3133 t->srv_state = SV_STCLOSE;
3134 if (!(t->flags & SN_ERR_MASK))
3135 t->flags |= SN_ERR_SRVTO;
3136 if (!(t->flags & SN_FINST_MASK))
3137 t->flags |= SN_FINST_D;
3138 /* We used to have a free connection slot. Since we'll never use it,
3139 * we have to inform the server that it may be used by another session.
3140 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003141 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003142 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003143
3144 return 1;
3145 }
3146 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003147 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3148 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003149 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003150 }
3151 }
3152 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003153 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3154 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003155 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003156 /* FIXME: to prevent the server from expiring read timeouts during writes,
3157 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003158 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003159 }
3160 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003161 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003162 }
3163 }
3164 return 0;
3165 }
3166 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003167 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003168 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003169 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003170 fd_delete(t->srv_fd);
3171 if (t->srv) {
3172 t->srv->cur_sess--;
3173 t->srv->failed_resp++;
3174 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003175 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003176 //close(t->srv_fd);
3177 t->srv_state = SV_STCLOSE;
3178 if (!(t->flags & SN_ERR_MASK))
3179 t->flags |= SN_ERR_SRVCL;
3180 if (!(t->flags & SN_FINST_MASK))
3181 t->flags |= SN_FINST_D;
3182 /* We used to have a free connection slot. Since we'll never use it,
3183 * we have to inform the server that it may be used by another session.
3184 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003185 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003186 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003187
3188 return 1;
3189 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003190 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003191 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003192 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003193 fd_delete(t->srv_fd);
3194 if (t->srv)
3195 t->srv->cur_sess--;
3196 //close(t->srv_fd);
3197 t->srv_state = SV_STCLOSE;
3198 /* We used to have a free connection slot. Since we'll never use it,
3199 * we have to inform the server that it may be used by another session.
3200 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003201 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003202 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003203
3204 return 1;
3205 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003206 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003207 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003208 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003209 fd_delete(t->srv_fd);
3210 if (t->srv)
3211 t->srv->cur_sess--;
3212 //close(t->srv_fd);
3213 t->srv_state = SV_STCLOSE;
3214 if (!(t->flags & SN_ERR_MASK))
3215 t->flags |= SN_ERR_SRVTO;
3216 if (!(t->flags & SN_FINST_MASK))
3217 t->flags |= SN_FINST_D;
3218 /* We used to have a free connection slot. Since we'll never use it,
3219 * we have to inform the server that it may be used by another session.
3220 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003221 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003222 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003223
3224 return 1;
3225 }
3226 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003227 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003228 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003229 }
3230 }
3231 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003232 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003233 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003234 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003235 }
3236 }
3237 return 0;
3238 }
3239 else { /* SV_STCLOSE : nothing to do */
3240 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3241 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003242 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003243 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003244 write(1, trash, len);
3245 }
3246 return 0;
3247 }
3248 return 0;
3249}
3250
3251
3252/*
3253 * Produces data for the session <s> depending on its source. Expects to be
3254 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3255 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3256 * session, which it uses to keep on being called when there is free space in
3257 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3258 * if it changes the session state from CL_STSHUTR, otherwise 0.
3259 */
3260int produce_content(struct session *s)
3261{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003262 if (s->data_source == DATA_SRC_NONE) {
3263 s->flags &= ~SN_SELF_GEN;
3264 return 1;
3265 }
3266 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003267 /* dump server statistics */
3268 return produce_content_stats(s);
3269 }
3270 else {
3271 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003272 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003273 client_retnclose(s, error_message(s, HTTP_ERR_500));
3274 if (!(s->flags & SN_ERR_MASK))
3275 s->flags |= SN_ERR_PRXCOND;
3276 if (!(s->flags & SN_FINST_MASK))
3277 s->flags |= SN_FINST_R;
3278 s->flags &= ~SN_SELF_GEN;
3279 return 1;
3280 }
3281}
3282
3283
3284/*
3285 * Produces statistics data for the session <s>. Expects to be called with
3286 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3287 * flag from the session, which it uses to keep on being called when there is
3288 * free space in the buffer, of simply by letting an empty buffer upon return.
3289 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3290 */
3291int produce_content_stats(struct session *s)
3292{
3293 struct buffer *rep = s->rep;
3294 struct proxy *px;
3295 struct chunk msg;
3296 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003297
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003298 msg.len = 0;
3299 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003300
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003301 switch (s->data_state) {
3302 case DATA_ST_INIT:
3303 /* the function had not been called yet */
3304 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003305
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003306 chunk_printf(&msg, sizeof(trash),
3307 "HTTP/1.0 200 OK\r\n"
3308 "Cache-Control: no-cache\r\n"
3309 "Connection: close\r\n"
3310 "Content-Type: text/html\r\n"
3311 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003312
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003313 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003314 client_retnclose(s, &msg); // send the start of the response.
3315 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003316
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003317 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3318 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3319 if (!(s->flags & SN_FINST_MASK))
3320 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003321
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003322 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003323 /* that's all we return in case of HEAD request */
3324 s->data_state = DATA_ST_FIN;
3325 s->flags &= ~SN_SELF_GEN;
3326 return 1;
3327 }
3328
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003329 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3330 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003331
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003332 case DATA_ST_HEAD:
3333 /* WARNING! This must fit in the first buffer !!! */
3334 chunk_printf(&msg, sizeof(trash),
3335 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3336 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3337 "<style type=\"text/css\"><!--\n"
3338 "body {"
3339 " font-family: helvetica, arial;"
3340 " font-size: 12px;"
3341 " font-weight: normal;"
3342 " color: black;"
3343 " background: white;"
3344 "}\n"
3345 "th,td {"
3346 " font-size: 0.8em;"
3347 " align: center;"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003348 "}\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003349 "h1 {"
3350 " font-size: xx-large;"
3351 " margin-bottom: 0.5em;"
3352 "}\n"
3353 "h2 {"
3354 " font-family: helvetica, arial;"
3355 " font-size: x-large;"
3356 " font-weight: bold;"
3357 " font-style: italic;"
3358 " color: #6020a0;"
3359 " margin-top: 0em;"
3360 " margin-bottom: 0em;"
3361 "}\n"
3362 "h3 {"
3363 " font-family: helvetica, arial;"
3364 " font-size: 16px;"
3365 " font-weight: bold;"
3366 " color: #b00040;"
3367 " background: #e8e8d0;"
3368 " margin-top: 0em;"
3369 " margin-bottom: 0em;"
3370 "}\n"
3371 "li {"
3372 " margin-top: 0.25em;"
3373 " margin-right: 2em;"
3374 "}\n"
3375 ".hr {margin-top: 0.25em;"
3376 " border-color: black;"
3377 " border-bottom-style: solid;"
3378 "}\n"
3379 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3380 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3381 ".total {background: #20D0D0;color: #ffff80;}\n"
3382 ".frontend {background: #e8e8d0;}\n"
3383 ".backend {background: #e8e8d0;}\n"
3384 ".active0 {background: #ff9090;}\n"
3385 ".active1 {background: #ffd020;}\n"
3386 ".active2 {background: #ffffa0;}\n"
3387 ".active3 {background: #c0ffc0;}\n"
3388 ".active4 {background: #e0e0e0;}\n"
3389 ".backup0 {background: #ff9090;}\n"
3390 ".backup1 {background: #ff80ff;}\n"
3391 ".backup2 {background: #c060ff;}\n"
3392 ".backup3 {background: #b0d0ff;}\n"
3393 ".backup4 {background: #e0e0e0;}\n"
3394 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003395 "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 +01003396 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3397 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3398 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3399 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3400 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003401 "-->\n"
3402 "</style></head>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003403
3404 if (buffer_write_chunk(rep, &msg) != 0)
3405 return 0;
3406
3407 s->data_state = DATA_ST_INFO;
3408 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003409
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003410 case DATA_ST_INFO:
3411 up = (now.tv_sec - start_date.tv_sec);
3412
3413 /* WARNING! this has to fit the first packet too.
3414 * We are around 3.5 kB, add adding entries will
3415 * become tricky if we want to support 4kB buffers !
3416 */
3417 chunk_printf(&msg, sizeof(trash),
3418 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3419 PRODUCT_NAME "</a></h1>\n"
3420 "<h2>Statistics Report for pid %d</h2>\n"
3421 "<hr width=\"100%%\" class=\"hr\">\n"
3422 "<h3>&gt; General process information</h3>\n"
3423 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3424 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3425 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3426 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3427 "<b>maxsock = </b> %d<br>\n"
3428 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3429 "</td><td align=\"center\" nowrap>\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003430 "<table class=\"lgd\"><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003431 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3432 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003433 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003434 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3435 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003436 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003437 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3438 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003439 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003440 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3441 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3442 "</tr></table>\n"
3443 "</td>"
3444 "<td align=\"left\" nowrap width=\"1%%\">"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003445 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
3446 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3447 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3448 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003449 "</ul>"
3450 "</td>"
3451 "</tr></table>\n"
3452 "",
3453 pid, pid, global.nbproc,
3454 up / 86400, (up % 86400) / 3600,
3455 (up % 3600) / 60, (up % 60),
3456 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3457 global.rlimit_memmax ? " MB" : "",
3458 global.rlimit_nofile,
3459 global.maxsock,
3460 global.maxconn,
3461 actconn
3462 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003463
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003464 if (buffer_write_chunk(rep, &msg) != 0)
3465 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003466
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003467 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003468
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003469 s->data_ctx.stats.px = proxy;
3470 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3471 s->data_state = DATA_ST_LIST;
3472 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003473
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003474 case DATA_ST_LIST:
3475 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003476 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003477 px = s->data_ctx.stats.px;
3478 /* skip the disabled proxies and non-networked ones */
3479 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3480 if (produce_content_stats_proxy(s, px) == 0)
3481 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003482
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003483 s->data_ctx.stats.px = px->next;
3484 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3485 }
3486 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003487
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003488 s->data_state = DATA_ST_END;
3489 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003490
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003491 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003492 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003493 if (buffer_write_chunk(rep, &msg) != 0)
3494 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003495
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003496 s->data_state = DATA_ST_FIN;
3497 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003498
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003499 case DATA_ST_FIN:
3500 s->flags &= ~SN_SELF_GEN;
3501 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003502
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003503 default:
3504 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003505 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003506 client_retnclose(s, error_message(s, HTTP_ERR_500));
3507 if (!(s->flags & SN_ERR_MASK))
3508 s->flags |= SN_ERR_PRXCOND;
3509 if (!(s->flags & SN_FINST_MASK))
3510 s->flags |= SN_FINST_R;
3511 s->flags &= ~SN_SELF_GEN;
3512 return 1;
3513 }
3514}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003515
Willy Tarreaubaaee002006-06-26 02:48:02 +02003516
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003517/*
3518 * Dumps statistics for a proxy.
3519 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3520 * ot non-zero if everything completed.
3521 */
3522int produce_content_stats_proxy(struct session *s, struct proxy *px)
3523{
3524 struct buffer *rep = s->rep;
3525 struct server *sv;
3526 struct chunk msg;
3527
3528 msg.len = 0;
3529 msg.str = trash;
3530
3531 switch (s->data_ctx.stats.px_st) {
3532 case DATA_ST_PX_INIT:
3533 /* we are on a new proxy */
3534
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003535 if (s->be->uri_auth && s->be->uri_auth->scope) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003536 /* we have a limited scope, we have to check the proxy name */
3537 struct stat_scope *scope;
3538 int len;
3539
3540 len = strlen(px->id);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003541 scope = s->be->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003542
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003543 while (scope) {
3544 /* match exact proxy name */
3545 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3546 break;
3547
3548 /* match '.' which means 'self' proxy */
3549 if (!strcmp(scope->px_id, ".") && px == s->fe)
3550 break;
3551 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003552 }
3553
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003554 /* proxy name not found : don't dump anything */
3555 if (scope == NULL)
3556 return 1;
3557 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003558
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003559 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3560 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003561
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003562 case DATA_ST_PX_TH:
3563 /* print a new table */
3564 chunk_printf(&msg, sizeof(trash),
3565 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3566 "<tr align=\"center\" class=\"titre\">"
3567 "<th colspan=2 class=\"pxname\">%s</th>"
3568 "<th colspan=18 class=\"empty\"></th>"
3569 "</tr>\n"
3570 "<tr align=\"center\" class=\"titre\">"
3571 "<th rowspan=2></th>"
3572 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3573 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3574 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3575 "</tr>\n"
3576 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003577 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3578 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003579 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3580 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3581 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3582 "",
3583 px->id);
3584
3585 if (buffer_write_chunk(rep, &msg) != 0)
3586 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003587
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003588 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3589 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003590
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003591 case DATA_ST_PX_FE:
3592 /* print the frontend */
3593 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003594 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003595 /* name, queue */
3596 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3597 /* sessions : current, max, limit, cumul. */
3598 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3599 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003600 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003601 /* denied: req, resp */
3602 "<td align=right>%d</td><td align=right>%d</td>"
3603 /* errors : request, connect, response */
3604 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3605 /* server status : reflect backend status */
3606 "<td align=center>%s</td>"
3607 /* rest of server: nothing */
3608 "<td align=center colspan=5></td></tr>"
3609 "",
3610 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003611 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003612 px->denied_req, px->denied_resp,
3613 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003614 px->state == PR_STRUN ? "OPEN" :
3615 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003616
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003617 if (buffer_write_chunk(rep, &msg) != 0)
3618 return 0;
3619 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003620
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003621 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3622 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3623 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003624
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003625 case DATA_ST_PX_SV:
3626 /* stats.sv has been initialized above */
3627 while (s->data_ctx.stats.sv != NULL) {
3628 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3629 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003630
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003631 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003632
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003633 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3634 if (!(sv->state & SRV_CHECKED))
3635 sv_state = 4;
3636 else if (sv->state & SRV_RUNNING)
3637 if (sv->health == sv->rise + sv->fall - 1)
3638 sv_state = 3; /* UP */
3639 else
3640 sv_state = 2; /* going down */
3641 else
3642 if (sv->health)
3643 sv_state = 1; /* going up */
3644 else
3645 sv_state = 0; /* DOWN */
3646
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003647 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003648 /* name */
3649 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3650 /* queue : current, max */
3651 "<td align=right>%d</td><td align=right>%d</td>"
3652 /* sessions : current, max, limit, cumul */
3653 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3654 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003655 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003656 /* denied: req, resp */
3657 "<td align=right></td><td align=right>%d</td>"
3658 /* errors : request, connect, response */
3659 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3660 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003661 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003662 sv_state, sv->id,
3663 sv->nbpend, sv->nbpend_max,
3664 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003665 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003666 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003667 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003668
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003669 /* status */
3670 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3671 chunk_printf(&msg, sizeof(trash),
3672 srv_hlt_st[sv_state],
3673 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3674 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3675
Willy Tarreau128e9542007-01-01 22:01:43 +01003676 chunk_printf(&msg, sizeof(trash),
3677 /* weight */
3678 "</td><td>%d</td>"
3679 /* act, bck */
3680 "<td>%s</td><td>%s</td>"
3681 "",
Willy Tarreau417fae02007-03-25 21:16:40 +02003682 sv->uweight,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003683 (sv->state & SRV_BACKUP) ? "-" : "Y",
3684 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003685
3686 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003687 if (sv->state & SRV_CHECKED)
3688 chunk_printf(&msg, sizeof(trash),
3689 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3690 sv->failed_checks, sv->down_trans);
3691 else
3692 chunk_printf(&msg, sizeof(trash),
3693 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003694
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003695 if (buffer_write_chunk(rep, &msg) != 0)
3696 return 0;
3697
3698 s->data_ctx.stats.sv = sv->next;
3699 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003700
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003701 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3702 /* fall through */
3703
3704 case DATA_ST_PX_BE:
3705 /* print the backend */
3706 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003707 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003708 /* name */
3709 "<tr align=center class=\"backend\"><td>Backend</td>"
3710 /* queue : current, max */
3711 "<td align=right>%d</td><td align=right>%d</td>"
3712 /* sessions : current, max, limit, cumul. */
3713 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3714 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003715 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003716 /* denied: req, resp */
3717 "<td align=right>%d</td><td align=right>%d</td>"
3718 /* errors : request, connect, response */
3719 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3720 /* server status : reflect backend status (up/down) : we display UP
3721 * if the backend has known working servers or if it has no server at
3722 * all (eg: for stats). Tthen we display the total weight, number of
3723 * active and backups. */
3724 "<td align=center>%s</td><td align=center>%d</td>"
3725 "<td align=center>%d</td><td align=center>%d</td>"
3726 /* rest of server: nothing */
3727 "<td align=center colspan=2></td></tr>"
3728 "",
3729 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3730 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003731 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003732 px->denied_req, px->denied_resp,
3733 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003734 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3735 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003736
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003737 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003738 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003739 }
3740
3741 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3742 /* fall through */
3743
3744 case DATA_ST_PX_END:
3745 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3746
3747 if (buffer_write_chunk(rep, &msg) != 0)
3748 return 0;
3749
3750 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3751 /* fall through */
3752
3753 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003754 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003755
3756 default:
3757 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003758 return 1;
3759 }
3760}
3761
3762
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003763/* Iterate the same filter through all request headers.
3764 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003765 * Since it can manage the switch to another backend, it updates the per-proxy
3766 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003767 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003768int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003769{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003770 char term;
3771 char *cur_ptr, *cur_end, *cur_next;
3772 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003773 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003774 struct hdr_idx_elem *cur_hdr;
3775 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003777 last_hdr = 0;
3778
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003779 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003780 old_idx = 0;
3781
3782 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003783 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003784 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003785 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003786 (exp->action == ACT_ALLOW ||
3787 exp->action == ACT_DENY ||
3788 exp->action == ACT_TARPIT))
3789 return 0;
3790
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003791 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003792 if (!cur_idx)
3793 break;
3794
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003795 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003796 cur_ptr = cur_next;
3797 cur_end = cur_ptr + cur_hdr->len;
3798 cur_next = cur_end + cur_hdr->cr + 1;
3799
3800 /* Now we have one header between cur_ptr and cur_end,
3801 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003802 */
3803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003804 /* The annoying part is that pattern matching needs
3805 * that we modify the contents to null-terminate all
3806 * strings before testing them.
3807 */
3808
3809 term = *cur_end;
3810 *cur_end = '\0';
3811
3812 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3813 switch (exp->action) {
3814 case ACT_SETBE:
3815 /* It is not possible to jump a second time.
3816 * FIXME: should we return an HTTP/500 here so that
3817 * the admin knows there's a problem ?
3818 */
3819 if (t->be != t->fe)
3820 break;
3821
3822 /* Swithing Proxy */
3823 t->be = (struct proxy *) exp->replace;
3824
3825 /* right now, the backend switch is not too much complicated
3826 * because we have associated req_cap and rsp_cap to the
3827 * frontend, and the beconn will be updated later.
3828 */
3829
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003830 t->rep->rto = t->req->wto = t->be->srvtimeout;
3831 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003832 last_hdr = 1;
3833 break;
3834
3835 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003836 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003837 last_hdr = 1;
3838 break;
3839
3840 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003841 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003842 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003843 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003844 break;
3845
3846 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003847 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003848 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003849 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003850 break;
3851
3852 case ACT_REPLACE:
3853 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3854 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3855 /* FIXME: if the user adds a newline in the replacement, the
3856 * index will not be recalculated for now, and the new line
3857 * will not be counted as a new header.
3858 */
3859
3860 cur_end += delta;
3861 cur_next += delta;
3862 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003863 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003864 break;
3865
3866 case ACT_REMOVE:
3867 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3868 cur_next += delta;
3869
3870 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003871 txn->req.eoh += delta;
3872 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3873 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003874 cur_hdr->len = 0;
3875 cur_end = NULL; /* null-term has been rewritten */
3876 break;
3877
3878 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003879 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003880 if (cur_end)
3881 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003882
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003883 /* keep the link from this header to next one in case of later
3884 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003885 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003886 old_idx = cur_idx;
3887 }
3888 return 0;
3889}
3890
3891
3892/* Apply the filter to the request line.
3893 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3894 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003895 * Since it can manage the switch to another backend, it updates the per-proxy
3896 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003897 */
3898int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3899{
3900 char term;
3901 char *cur_ptr, *cur_end;
3902 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003903 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003904 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003905
Willy Tarreau58f10d72006-12-04 02:26:12 +01003906
Willy Tarreau3d300592007-03-18 18:34:41 +01003907 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003908 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003909 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003910 (exp->action == ACT_ALLOW ||
3911 exp->action == ACT_DENY ||
3912 exp->action == ACT_TARPIT))
3913 return 0;
3914 else if (exp->action == ACT_REMOVE)
3915 return 0;
3916
3917 done = 0;
3918
Willy Tarreau9cdde232007-05-02 20:58:19 +02003919 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003920 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003921
3922 /* Now we have the request line between cur_ptr and cur_end */
3923
3924 /* The annoying part is that pattern matching needs
3925 * that we modify the contents to null-terminate all
3926 * strings before testing them.
3927 */
3928
3929 term = *cur_end;
3930 *cur_end = '\0';
3931
3932 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3933 switch (exp->action) {
3934 case ACT_SETBE:
3935 /* It is not possible to jump a second time.
3936 * FIXME: should we return an HTTP/500 here so that
3937 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003938 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003939 if (t->be != t->fe)
3940 break;
3941
3942 /* Swithing Proxy */
3943 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003944
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003945 /* right now, the backend switch is not too much complicated
3946 * because we have associated req_cap and rsp_cap to the
3947 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003948 */
3949
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003950 t->rep->rto = t->req->wto = t->be->srvtimeout;
3951 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003952 done = 1;
3953 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003954
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003955 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003956 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003957 done = 1;
3958 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003959
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003960 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003961 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003962 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003963 done = 1;
3964 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003965
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003966 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003967 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003968 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003969 done = 1;
3970 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003971
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003972 case ACT_REPLACE:
3973 *cur_end = term; /* restore the string terminator */
3974 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3975 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3976 /* FIXME: if the user adds a newline in the replacement, the
3977 * index will not be recalculated for now, and the new line
3978 * will not be counted as a new header.
3979 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003980
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003981 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003982 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003983
Willy Tarreau9cdde232007-05-02 20:58:19 +02003984 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003985 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003986 HTTP_MSG_RQMETH,
3987 cur_ptr, cur_end + 1,
3988 NULL, NULL);
3989 if (unlikely(!cur_end))
3990 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003991
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003992 /* we have a full request and we know that we have either a CR
3993 * or an LF at <ptr>.
3994 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003995 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3996 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003997 /* there is no point trying this regex on headers */
3998 return 1;
3999 }
4000 }
4001 *cur_end = term; /* restore the string terminator */
4002 return done;
4003}
Willy Tarreau97de6242006-12-27 17:18:38 +01004004
Willy Tarreau58f10d72006-12-04 02:26:12 +01004005
Willy Tarreau58f10d72006-12-04 02:26:12 +01004006
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004007/*
4008 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4009 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004010 * unparsable request. Since it can manage the switch to another backend, it
4011 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004012 */
4013int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4014{
Willy Tarreau3d300592007-03-18 18:34:41 +01004015 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004016 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004017 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004018 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004019
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004020 /*
4021 * The interleaving of transformations and verdicts
4022 * makes it difficult to decide to continue or stop
4023 * the evaluation.
4024 */
4025
Willy Tarreau3d300592007-03-18 18:34:41 +01004026 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004027 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4028 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4029 exp = exp->next;
4030 continue;
4031 }
4032
4033 /* Apply the filter to the request line. */
4034 ret = apply_filter_to_req_line(t, req, exp);
4035 if (unlikely(ret < 0))
4036 return -1;
4037
4038 if (likely(ret == 0)) {
4039 /* The filter did not match the request, it can be
4040 * iterated through all headers.
4041 */
4042 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004043 }
4044 exp = exp->next;
4045 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004046 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004047}
4048
4049
Willy Tarreaua15645d2007-03-18 16:22:39 +01004050
Willy Tarreau58f10d72006-12-04 02:26:12 +01004051/*
4052 * Manager client-side cookie
4053 */
4054void manage_client_side_cookies(struct session *t, struct buffer *req)
4055{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004056 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004057 char *p1, *p2, *p3, *p4;
4058 char *del_colon, *del_cookie, *colon;
4059 int app_cookies;
4060
4061 appsess *asession_temp = NULL;
4062 appsess local_asession;
4063
4064 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004065 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004066
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004067 if (t->be->cookie_name == NULL &&
4068 t->be->appsession_name == NULL &&
4069 t->be->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004070 return;
4071
Willy Tarreau2a324282006-12-05 00:05:46 +01004072 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004073 * we start with the start line.
4074 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004075 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004076 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004077
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004078 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004079 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004080 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004081
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004082 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004083 cur_ptr = cur_next;
4084 cur_end = cur_ptr + cur_hdr->len;
4085 cur_next = cur_end + cur_hdr->cr + 1;
4086
4087 /* We have one full header between cur_ptr and cur_end, and the
4088 * next header starts at cur_next. We're only interested in
4089 * "Cookie:" headers.
4090 */
4091
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004092 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4093 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004094 old_idx = cur_idx;
4095 continue;
4096 }
4097
4098 /* Now look for cookies. Conforming to RFC2109, we have to support
4099 * attributes whose name begin with a '$', and associate them with
4100 * the right cookie, if we want to delete this cookie.
4101 * So there are 3 cases for each cookie read :
4102 * 1) it's a special attribute, beginning with a '$' : ignore it.
4103 * 2) it's a server id cookie that we *MAY* want to delete : save
4104 * some pointers on it (last semi-colon, beginning of cookie...)
4105 * 3) it's an application cookie : we *MAY* have to delete a previous
4106 * "special" cookie.
4107 * At the end of loop, if a "special" cookie remains, we may have to
4108 * remove it. If no application cookie persists in the header, we
4109 * *MUST* delete it
4110 */
4111
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004112 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004113
Willy Tarreau58f10d72006-12-04 02:26:12 +01004114 /* del_cookie == NULL => nothing to be deleted */
4115 del_colon = del_cookie = NULL;
4116 app_cookies = 0;
4117
4118 while (p1 < cur_end) {
4119 /* skip spaces and colons, but keep an eye on these ones */
4120 while (p1 < cur_end) {
4121 if (*p1 == ';' || *p1 == ',')
4122 colon = p1;
4123 else if (!isspace((int)*p1))
4124 break;
4125 p1++;
4126 }
4127
4128 if (p1 == cur_end)
4129 break;
4130
4131 /* p1 is at the beginning of the cookie name */
4132 p2 = p1;
4133 while (p2 < cur_end && *p2 != '=')
4134 p2++;
4135
4136 if (p2 == cur_end)
4137 break;
4138
4139 p3 = p2 + 1; /* skips the '=' sign */
4140 if (p3 == cur_end)
4141 break;
4142
4143 p4 = p3;
4144 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
4145 p4++;
4146
4147 /* here, we have the cookie name between p1 and p2,
4148 * and its value between p3 and p4.
4149 * we can process it :
4150 *
4151 * Cookie: NAME=VALUE;
4152 * | || || |
4153 * | || || +--> p4
4154 * | || |+-------> p3
4155 * | || +--------> p2
4156 * | |+------------> p1
4157 * | +-------------> colon
4158 * +--------------------> cur_ptr
4159 */
4160
4161 if (*p1 == '$') {
4162 /* skip this one */
4163 }
4164 else {
4165 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004166 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004167 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004168 (p4 - p1 >= t->fe->capture_namelen) &&
4169 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004170 int log_len = p4 - p1;
4171
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004172 if ((txn->cli_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004173 Alert("HTTP logging : out of memory.\n");
4174 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004175 if (log_len > t->fe->capture_len)
4176 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004177 memcpy(txn->cli_cookie, p1, log_len);
4178 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004179 }
4180 }
4181
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004182 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4183 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004184 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004185 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004186 char *delim;
4187
4188 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4189 * have the server ID betweek p3 and delim, and the original cookie between
4190 * delim+1 and p4. Otherwise, delim==p4 :
4191 *
4192 * Cookie: NAME=SRV~VALUE;
4193 * | || || | |
4194 * | || || | +--> p4
4195 * | || || +--------> delim
4196 * | || |+-----------> p3
4197 * | || +------------> p2
4198 * | |+----------------> p1
4199 * | +-----------------> colon
4200 * +------------------------> cur_ptr
4201 */
4202
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004203 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004204 for (delim = p3; delim < p4; delim++)
4205 if (*delim == COOKIE_DELIM)
4206 break;
4207 }
4208 else
4209 delim = p4;
4210
4211
4212 /* Here, we'll look for the first running server which supports the cookie.
4213 * This allows to share a same cookie between several servers, for example
4214 * to dedicate backup servers to specific servers only.
4215 * However, to prevent clients from sticking to cookie-less backup server
4216 * when they have incidentely learned an empty cookie, we simply ignore
4217 * empty cookies and mark them as invalid.
4218 */
4219 if (delim == p3)
4220 srv = NULL;
4221
4222 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004223 if (srv->cookie && (srv->cklen == delim - p3) &&
4224 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004225 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004226 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004227 txn->flags &= ~TX_CK_MASK;
4228 txn->flags |= TX_CK_VALID;
4229 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004230 t->srv = srv;
4231 break;
4232 } else {
4233 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004234 txn->flags &= ~TX_CK_MASK;
4235 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004236 }
4237 }
4238 srv = srv->next;
4239 }
4240
Willy Tarreau3d300592007-03-18 18:34:41 +01004241 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004242 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004243 txn->flags &= ~TX_CK_MASK;
4244 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004245 }
4246
4247 /* depending on the cookie mode, we may have to either :
4248 * - delete the complete cookie if we're in insert+indirect mode, so that
4249 * the server never sees it ;
4250 * - remove the server id from the cookie value, and tag the cookie as an
4251 * application cookie so that it does not get accidentely removed later,
4252 * if we're in cookie prefix mode
4253 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004254 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004255 int delta; /* negative */
4256
4257 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4258 p4 += delta;
4259 cur_end += delta;
4260 cur_next += delta;
4261 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004262 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004263
4264 del_cookie = del_colon = NULL;
4265 app_cookies++; /* protect the header from deletion */
4266 }
4267 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004268 (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 +01004269 del_cookie = p1;
4270 del_colon = colon;
4271 }
4272 } else {
4273 /* now we know that we must keep this cookie since it's
4274 * not ours. But if we wanted to delete our cookie
4275 * earlier, we cannot remove the complete header, but we
4276 * can remove the previous block itself.
4277 */
4278 app_cookies++;
4279
4280 if (del_cookie != NULL) {
4281 int delta; /* negative */
4282
4283 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4284 p4 += delta;
4285 cur_end += delta;
4286 cur_next += delta;
4287 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004288 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004289 del_cookie = del_colon = NULL;
4290 }
4291 }
4292
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004293 if ((t->be->appsession_name != NULL) &&
4294 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004295 /* first, let's see if the cookie is our appcookie*/
4296
4297 /* Cool... it's the right one */
4298
4299 asession_temp = &local_asession;
4300
4301 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4302 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4303 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4304 return;
4305 }
4306
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004307 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4308 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004309 asession_temp->serverid = NULL;
4310
4311 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004312 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004313 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4314 /* free previously allocated memory */
4315 pool_free_to(apools.sessid, local_asession.sessid);
4316 Alert("Not enough memory process_cli():asession:calloc().\n");
4317 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4318 return;
4319 }
4320
4321 asession_temp->sessid = local_asession.sessid;
4322 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004323 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004324 } else {
4325 /* free previously allocated memory */
4326 pool_free_to(apools.sessid, local_asession.sessid);
4327 }
4328
4329 if (asession_temp->serverid == NULL) {
4330 Alert("Found Application Session without matching server.\n");
4331 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004332 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004333 while (srv) {
4334 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004335 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004336 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004337 txn->flags &= ~TX_CK_MASK;
4338 txn->flags |= TX_CK_VALID;
4339 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004340 t->srv = srv;
4341 break;
4342 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004343 txn->flags &= ~TX_CK_MASK;
4344 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004345 }
4346 }
4347 srv = srv->next;
4348 }/* end while(srv) */
4349 }/* end else if server == NULL */
4350
Willy Tarreaud825eef2007-05-12 22:35:00 +02004351 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004352 }/* end if ((t->proxy->appsession_name != NULL) ... */
4353 }
4354
4355 /* we'll have to look for another cookie ... */
4356 p1 = p4;
4357 } /* while (p1 < cur_end) */
4358
4359 /* There's no more cookie on this line.
4360 * We may have marked the last one(s) for deletion.
4361 * We must do this now in two ways :
4362 * - if there is no app cookie, we simply delete the header ;
4363 * - if there are app cookies, we must delete the end of the
4364 * string properly, including the colon/semi-colon before
4365 * the cookie name.
4366 */
4367 if (del_cookie != NULL) {
4368 int delta;
4369 if (app_cookies) {
4370 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4371 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004372 cur_hdr->len += delta;
4373 } else {
4374 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004375
4376 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004377 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4378 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004379 cur_hdr->len = 0;
4380 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004381 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004382 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004383 }
4384
4385 /* keep the link from this header to next one */
4386 old_idx = cur_idx;
4387 } /* end of cookie processing on this header */
4388}
4389
4390
Willy Tarreaua15645d2007-03-18 16:22:39 +01004391/* Iterate the same filter through all response headers contained in <rtr>.
4392 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4393 */
4394int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4395{
4396 char term;
4397 char *cur_ptr, *cur_end, *cur_next;
4398 int cur_idx, old_idx, last_hdr;
4399 struct http_txn *txn = &t->txn;
4400 struct hdr_idx_elem *cur_hdr;
4401 int len, delta;
4402
4403 last_hdr = 0;
4404
4405 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4406 old_idx = 0;
4407
4408 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004409 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004410 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004411 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004412 (exp->action == ACT_ALLOW ||
4413 exp->action == ACT_DENY))
4414 return 0;
4415
4416 cur_idx = txn->hdr_idx.v[old_idx].next;
4417 if (!cur_idx)
4418 break;
4419
4420 cur_hdr = &txn->hdr_idx.v[cur_idx];
4421 cur_ptr = cur_next;
4422 cur_end = cur_ptr + cur_hdr->len;
4423 cur_next = cur_end + cur_hdr->cr + 1;
4424
4425 /* Now we have one header between cur_ptr and cur_end,
4426 * and the next header starts at cur_next.
4427 */
4428
4429 /* The annoying part is that pattern matching needs
4430 * that we modify the contents to null-terminate all
4431 * strings before testing them.
4432 */
4433
4434 term = *cur_end;
4435 *cur_end = '\0';
4436
4437 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4438 switch (exp->action) {
4439 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004440 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004441 last_hdr = 1;
4442 break;
4443
4444 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004445 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004446 last_hdr = 1;
4447 break;
4448
4449 case ACT_REPLACE:
4450 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4451 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4452 /* FIXME: if the user adds a newline in the replacement, the
4453 * index will not be recalculated for now, and the new line
4454 * will not be counted as a new header.
4455 */
4456
4457 cur_end += delta;
4458 cur_next += delta;
4459 cur_hdr->len += delta;
4460 txn->rsp.eoh += delta;
4461 break;
4462
4463 case ACT_REMOVE:
4464 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4465 cur_next += delta;
4466
4467 /* FIXME: this should be a separate function */
4468 txn->rsp.eoh += delta;
4469 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4470 txn->hdr_idx.used--;
4471 cur_hdr->len = 0;
4472 cur_end = NULL; /* null-term has been rewritten */
4473 break;
4474
4475 }
4476 }
4477 if (cur_end)
4478 *cur_end = term; /* restore the string terminator */
4479
4480 /* keep the link from this header to next one in case of later
4481 * removal of next header.
4482 */
4483 old_idx = cur_idx;
4484 }
4485 return 0;
4486}
4487
4488
4489/* Apply the filter to the status line in the response buffer <rtr>.
4490 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4491 * or -1 if a replacement resulted in an invalid status line.
4492 */
4493int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4494{
4495 char term;
4496 char *cur_ptr, *cur_end;
4497 int done;
4498 struct http_txn *txn = &t->txn;
4499 int len, delta;
4500
4501
Willy Tarreau3d300592007-03-18 18:34:41 +01004502 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004503 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004504 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004505 (exp->action == ACT_ALLOW ||
4506 exp->action == ACT_DENY))
4507 return 0;
4508 else if (exp->action == ACT_REMOVE)
4509 return 0;
4510
4511 done = 0;
4512
Willy Tarreau9cdde232007-05-02 20:58:19 +02004513 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004514 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4515
4516 /* Now we have the status line between cur_ptr and cur_end */
4517
4518 /* The annoying part is that pattern matching needs
4519 * that we modify the contents to null-terminate all
4520 * strings before testing them.
4521 */
4522
4523 term = *cur_end;
4524 *cur_end = '\0';
4525
4526 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4527 switch (exp->action) {
4528 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004529 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004530 done = 1;
4531 break;
4532
4533 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004534 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004535 done = 1;
4536 break;
4537
4538 case ACT_REPLACE:
4539 *cur_end = term; /* restore the string terminator */
4540 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4541 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4542 /* FIXME: if the user adds a newline in the replacement, the
4543 * index will not be recalculated for now, and the new line
4544 * will not be counted as a new header.
4545 */
4546
4547 txn->rsp.eoh += delta;
4548 cur_end += delta;
4549
Willy Tarreau9cdde232007-05-02 20:58:19 +02004550 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004551 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004552 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004553 cur_ptr, cur_end + 1,
4554 NULL, NULL);
4555 if (unlikely(!cur_end))
4556 return -1;
4557
4558 /* we have a full respnse and we know that we have either a CR
4559 * or an LF at <ptr>.
4560 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004561 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004562 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4563 /* there is no point trying this regex on headers */
4564 return 1;
4565 }
4566 }
4567 *cur_end = term; /* restore the string terminator */
4568 return done;
4569}
4570
4571
4572
4573/*
4574 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4575 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4576 * unparsable response.
4577 */
4578int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4579{
Willy Tarreau3d300592007-03-18 18:34:41 +01004580 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004581 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004582 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004583 int ret;
4584
4585 /*
4586 * The interleaving of transformations and verdicts
4587 * makes it difficult to decide to continue or stop
4588 * the evaluation.
4589 */
4590
Willy Tarreau3d300592007-03-18 18:34:41 +01004591 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004592 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4593 exp->action == ACT_PASS)) {
4594 exp = exp->next;
4595 continue;
4596 }
4597
4598 /* Apply the filter to the status line. */
4599 ret = apply_filter_to_sts_line(t, rtr, exp);
4600 if (unlikely(ret < 0))
4601 return -1;
4602
4603 if (likely(ret == 0)) {
4604 /* The filter did not match the response, it can be
4605 * iterated through all headers.
4606 */
4607 apply_filter_to_resp_headers(t, rtr, exp);
4608 }
4609 exp = exp->next;
4610 }
4611 return 0;
4612}
4613
4614
4615
4616/*
4617 * Manager server-side cookies
4618 */
4619void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4620{
4621 struct http_txn *txn = &t->txn;
4622 char *p1, *p2, *p3, *p4;
4623
4624 appsess *asession_temp = NULL;
4625 appsess local_asession;
4626
4627 char *cur_ptr, *cur_end, *cur_next;
4628 int cur_idx, old_idx, delta;
4629
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004630 if (t->be->cookie_name == NULL &&
4631 t->be->appsession_name == NULL &&
4632 t->be->capture_name == NULL &&
4633 !(t->be->options & PR_O_CHK_CACHE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004634 return;
4635
4636 /* Iterate through the headers.
4637 * we start with the start line.
4638 */
4639 old_idx = 0;
4640 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4641
4642 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4643 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004644 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004645
4646 cur_hdr = &txn->hdr_idx.v[cur_idx];
4647 cur_ptr = cur_next;
4648 cur_end = cur_ptr + cur_hdr->len;
4649 cur_next = cur_end + cur_hdr->cr + 1;
4650
4651 /* We have one full header between cur_ptr and cur_end, and the
4652 * next header starts at cur_next. We're only interested in
4653 * "Cookie:" headers.
4654 */
4655
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004656 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4657 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004658 old_idx = cur_idx;
4659 continue;
4660 }
4661
4662 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004663 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004664
4665
4666 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004667 if (t->be->cookie_name == NULL &&
4668 t->be->appsession_name == NULL &&
4669 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004670 return;
4671
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004672 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004673
4674 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004675 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4676 break;
4677
4678 /* p1 is at the beginning of the cookie name */
4679 p2 = p1;
4680
4681 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4682 p2++;
4683
4684 if (p2 == cur_end || *p2 == ';') /* next cookie */
4685 break;
4686
4687 p3 = p2 + 1; /* skip the '=' sign */
4688 if (p3 == cur_end)
4689 break;
4690
4691 p4 = p3;
4692 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4693 p4++;
4694
4695 /* here, we have the cookie name between p1 and p2,
4696 * and its value between p3 and p4.
4697 * we can process it.
4698 */
4699
4700 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004701 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004702 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004703 (p4 - p1 >= t->be->capture_namelen) &&
4704 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004705 int log_len = p4 - p1;
4706
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004707 if ((txn->srv_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004708 Alert("HTTP logging : out of memory.\n");
4709 }
4710
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004711 if (log_len > t->be->capture_len)
4712 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004713 memcpy(txn->srv_cookie, p1, log_len);
4714 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004715 }
4716
4717 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004718 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4719 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004720 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004721 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004722
4723 /* If the cookie is in insert mode on a known server, we'll delete
4724 * this occurrence because we'll insert another one later.
4725 * We'll delete it too if the "indirect" option is set and we're in
4726 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004727 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4728 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004729 /* this header must be deleted */
4730 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4731 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4732 txn->hdr_idx.used--;
4733 cur_hdr->len = 0;
4734 cur_next += delta;
4735 txn->rsp.eoh += delta;
4736
Willy Tarreau3d300592007-03-18 18:34:41 +01004737 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004738 }
4739 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004740 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004741 /* replace bytes p3->p4 with the cookie name associated
4742 * with this server since we know it.
4743 */
4744 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4745 cur_hdr->len += delta;
4746 cur_next += delta;
4747 txn->rsp.eoh += delta;
4748
Willy Tarreau3d300592007-03-18 18:34:41 +01004749 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004750 }
4751 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004752 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004753 /* insert the cookie name associated with this server
4754 * before existing cookie, and insert a delimitor between them..
4755 */
4756 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4757 cur_hdr->len += delta;
4758 cur_next += delta;
4759 txn->rsp.eoh += delta;
4760
4761 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004762 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004763 }
4764 }
4765 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004766 else if ((t->be->appsession_name != NULL) &&
4767 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004768
4769 /* Cool... it's the right one */
4770
4771 size_t server_id_len = strlen(t->srv->id) + 1;
4772 asession_temp = &local_asession;
4773
4774 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4775 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4776 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4777 return;
4778 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004779 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4780 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004781 asession_temp->serverid = NULL;
4782
4783 /* only do insert, if lookup fails */
4784 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
4785 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4786 Alert("Not enough Memory process_srv():asession:calloc().\n");
4787 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4788 return;
4789 }
4790 asession_temp->sessid = local_asession.sessid;
4791 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004792 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004793 }/* end if (chtbl_lookup()) */
4794 else {
4795 /* free wasted memory */
4796 pool_free_to(apools.sessid, local_asession.sessid);
4797 } /* end else from if (chtbl_lookup()) */
4798
4799 if (asession_temp->serverid == NULL) {
4800 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
4801 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4802 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4803 return;
4804 }
4805 asession_temp->serverid[0] = '\0';
4806 }
4807
4808 if (asession_temp->serverid[0] == '\0')
4809 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4810
Willy Tarreaud825eef2007-05-12 22:35:00 +02004811 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004812
4813#if defined(DEBUG_HASH)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004814 print_table(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004815#endif
4816 }/* end if ((t->proxy->appsession_name != NULL) ... */
4817 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4818 } /* we're now at the end of the cookie value */
4819
4820 /* keep the link from this header to next one */
4821 old_idx = cur_idx;
4822 } /* end of cookie processing on this header */
4823}
4824
4825
4826
4827/*
4828 * Check if response is cacheable or not. Updates t->flags.
4829 */
4830void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4831{
4832 struct http_txn *txn = &t->txn;
4833 char *p1, *p2;
4834
4835 char *cur_ptr, *cur_end, *cur_next;
4836 int cur_idx;
4837
Willy Tarreau3d300592007-03-18 18:34:41 +01004838 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004839 return;
4840
4841 /* Iterate through the headers.
4842 * we start with the start line.
4843 */
4844 cur_idx = 0;
4845 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4846
4847 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4848 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004849 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004850
4851 cur_hdr = &txn->hdr_idx.v[cur_idx];
4852 cur_ptr = cur_next;
4853 cur_end = cur_ptr + cur_hdr->len;
4854 cur_next = cur_end + cur_hdr->cr + 1;
4855
4856 /* We have one full header between cur_ptr and cur_end, and the
4857 * next header starts at cur_next. We're only interested in
4858 * "Cookie:" headers.
4859 */
4860
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004861 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4862 if (val) {
4863 if ((cur_end - (cur_ptr + val) >= 8) &&
4864 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4865 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4866 return;
4867 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004868 }
4869
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004870 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4871 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004872 continue;
4873
4874 /* OK, right now we know we have a cache-control header at cur_ptr */
4875
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004876 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004877
4878 if (p1 >= cur_end) /* no more info */
4879 continue;
4880
4881 /* p1 is at the beginning of the value */
4882 p2 = p1;
4883
4884 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4885 p2++;
4886
4887 /* we have a complete value between p1 and p2 */
4888 if (p2 < cur_end && *p2 == '=') {
4889 /* we have something of the form no-cache="set-cookie" */
4890 if ((cur_end - p1 >= 21) &&
4891 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4892 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004893 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004894 continue;
4895 }
4896
4897 /* OK, so we know that either p2 points to the end of string or to a comma */
4898 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4899 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4900 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4901 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004902 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004903 return;
4904 }
4905
4906 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004907 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004908 continue;
4909 }
4910 }
4911}
4912
4913
Willy Tarreau58f10d72006-12-04 02:26:12 +01004914/*
4915 * Try to retrieve a known appsession in the URI, then the associated server.
4916 * If the server is found, it's assigned to the session.
4917 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004918void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004919{
Willy Tarreau3d300592007-03-18 18:34:41 +01004920 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004921 appsess *asession_temp = NULL;
4922 appsess local_asession;
4923 char *request_line;
4924
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004925 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004926 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004927 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004928 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004929 return;
4930
4931 /* skip ';' */
4932 request_line++;
4933
4934 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004935 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004936 return;
4937
4938 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004939 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004940
4941 /* First try if we already have an appsession */
4942 asession_temp = &local_asession;
4943
4944 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4945 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4946 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4947 return;
4948 }
4949
4950 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004951 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4952 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004953 asession_temp->serverid = NULL;
4954
4955 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004956 if (chtbl_lookup(&(t->be->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004957 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4958 /* free previously allocated memory */
4959 pool_free_to(apools.sessid, local_asession.sessid);
4960 Alert("Not enough memory process_cli():asession:calloc().\n");
4961 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4962 return;
4963 }
4964 asession_temp->sessid = local_asession.sessid;
4965 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004966 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004967 }
4968 else {
4969 /* free previously allocated memory */
4970 pool_free_to(apools.sessid, local_asession.sessid);
4971 }
4972
Willy Tarreaud825eef2007-05-12 22:35:00 +02004973 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004974 asession_temp->request_count++;
4975
4976#if defined(DEBUG_HASH)
4977 print_table(&(t->proxy->htbl_proxy));
4978#endif
4979 if (asession_temp->serverid == NULL) {
4980 Alert("Found Application Session without matching server.\n");
4981 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004982 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004983 while (srv) {
4984 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004985 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004986 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004987 txn->flags &= ~TX_CK_MASK;
4988 txn->flags |= TX_CK_VALID;
4989 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004990 t->srv = srv;
4991 break;
4992 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004993 txn->flags &= ~TX_CK_MASK;
4994 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004995 }
4996 }
4997 srv = srv->next;
4998 }
4999 }
5000}
5001
5002
Willy Tarreaub2513902006-12-17 14:52:38 +01005003/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005004 * In a GET or HEAD request, check if the requested URI matches the stats uri
5005 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005006 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005007 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005008 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005009 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005010 *
5011 * Returns 1 if the session's state changes, otherwise 0.
5012 */
5013int stats_check_uri_auth(struct session *t, struct proxy *backend)
5014{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005015 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005016 struct uri_auth *uri_auth = backend->uri_auth;
5017 struct user_auth *user;
5018 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005019 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005020
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005021 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005022 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005023 return 0;
5024
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005025 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005026
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005027 /* the URI is in h */
5028 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005029 return 0;
5030
5031 /* we are in front of a interceptable URI. Let's check
5032 * if there's an authentication and if it's valid.
5033 */
5034 user = uri_auth->users;
5035 if (!user) {
5036 /* no user auth required, it's OK */
5037 authenticated = 1;
5038 } else {
5039 authenticated = 0;
5040
5041 /* a user list is defined, we have to check.
5042 * skip 21 chars for "Authorization: Basic ".
5043 */
5044
5045 /* FIXME: this should move to an earlier place */
5046 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005047 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5048 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5049 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005050 if (len > 14 &&
5051 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005052 txn->auth_hdr.str = h;
5053 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005054 break;
5055 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005056 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005057 }
5058
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005059 if (txn->auth_hdr.len < 21 ||
5060 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005061 user = NULL;
5062
5063 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005064 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5065 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005066 user->user_pwd, user->user_len)) {
5067 authenticated = 1;
5068 break;
5069 }
5070 user = user->next;
5071 }
5072 }
5073
5074 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005075 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005076
5077 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005078 msg.str = trash;
5079 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005080 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005081 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01005082 if (!(t->flags & SN_ERR_MASK))
5083 t->flags |= SN_ERR_PRXCOND;
5084 if (!(t->flags & SN_FINST_MASK))
5085 t->flags |= SN_FINST_R;
5086 return 1;
5087 }
5088
5089 /* The request is valid, the user is authenticate. Let's start sending
5090 * data.
5091 */
5092 t->cli_state = CL_STSHUTR;
5093 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02005094 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01005095 t->data_source = DATA_SRC_STATS;
5096 t->data_state = DATA_ST_INIT;
5097 produce_content(t);
5098 return 1;
5099}
5100
5101
Willy Tarreaubaaee002006-06-26 02:48:02 +02005102/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005103 * Print a debug line with a header
5104 */
5105void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5106{
5107 int len, max;
5108 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5109 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5110 max = end - start;
5111 UBOUND(max, sizeof(trash) - len - 1);
5112 len += strlcpy2(trash + len, start, max + 1);
5113 trash[len++] = '\n';
5114 write(1, trash, len);
5115}
5116
5117
Willy Tarreau8797c062007-05-07 00:55:35 +02005118/************************************************************************/
5119/* The code below is dedicated to ACL parsing and matching */
5120/************************************************************************/
5121
5122
5123
5124
5125/* 1. Check on METHOD
5126 * We use the pre-parsed method if it is known, and store its number as an
5127 * integer. If it is unknown, we use the pointer and the length.
5128 */
5129static int acl_parse_meth(const char *text, struct acl_pattern *pattern)
5130{
5131 int len, meth;
5132
5133 len = strlen(text);
5134 meth = find_http_meth(text, len);
5135
5136 pattern->val.i = meth;
5137 if (meth == HTTP_METH_OTHER) {
5138 pattern->ptr.str = strdup(text);
5139 if (!pattern->ptr.str)
5140 return 0;
5141 pattern->len = len;
5142 }
5143 return 1;
5144}
5145
5146static int acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5147{
5148 int meth;
5149 struct http_txn *txn = l7;
5150
5151 meth = txn->meth;
5152 test->i = meth;
5153 if (meth == HTTP_METH_OTHER) {
5154 test->len = txn->req.sl.rq.m_l;
5155 test->ptr = txn->req.sol;
5156 }
5157 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5158 return 1;
5159}
5160
5161static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5162{
5163 if (test->i != pattern->val.i)
5164 return 0;
5165
5166 if (test->i != HTTP_METH_OTHER)
5167 return 1;
5168
5169 /* Other method, we must compare the strings */
5170 if (pattern->len != test->len)
5171 return 0;
5172 if (strncmp(pattern->ptr.str, test->ptr, test->len) != 0)
5173 return 0;
5174 return 1;
5175}
5176
5177/* 2. Check on Request/Status Version
5178 * We simply compare strings here.
5179 */
5180static int acl_parse_ver(const char *text, struct acl_pattern *pattern)
5181{
5182 pattern->ptr.str = strdup(text);
5183 if (!pattern->ptr.str)
5184 return 0;
5185 pattern->len = strlen(text);
5186 return 1;
5187}
5188
5189static int acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5190{
5191 struct http_txn *txn = l7;
5192 char *ptr;
5193 int len;
5194
5195 len = txn->req.sl.rq.v_l;
5196 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5197
5198 while ((len-- > 0) && (*ptr++ != '/'));
5199 if (len <= 0)
5200 return 0;
5201
5202 test->ptr = ptr;
5203 test->len = len;
5204
5205 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5206 return 1;
5207}
5208
5209static int acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5210{
5211 struct http_txn *txn = l7;
5212 char *ptr;
5213 int len;
5214
5215 len = txn->rsp.sl.st.v_l;
5216 ptr = txn->rsp.sol;
5217
5218 while ((len-- > 0) && (*ptr++ != '/'));
5219 if (len <= 0)
5220 return 0;
5221
5222 test->ptr = ptr;
5223 test->len = len;
5224
5225 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5226 return 1;
5227}
5228
5229/* 3. Check on Status Code. We manipulate integers here. */
5230static int acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5231{
5232 struct http_txn *txn = l7;
5233 char *ptr;
5234 int len;
5235
5236 len = txn->rsp.sl.st.c_l;
5237 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5238
5239 test->i = __strl2ui(ptr, len);
5240 test->flags = ACL_TEST_F_VOL_1ST;
5241 return 1;
5242}
5243
5244/* 4. Check on URL/URI. A pointer to the URI is stored. */
5245static int acl_fetch_url(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test)
5246{
5247 struct http_txn *txn = l7;
5248
5249 test->len = txn->req.sl.rq.u_l;
5250 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5251
Willy Tarreauf3d25982007-05-08 22:45:09 +02005252 /* we do not need to set READ_ONLY because the data is in a buffer */
5253 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005254 return 1;
5255}
5256
5257
5258
5259/************************************************************************/
5260/* All supported keywords must be declared here. */
5261/************************************************************************/
5262
5263/* Note: must not be declared <const> as its list will be overwritten */
5264static struct acl_kw_list acl_kws = {{ },{
5265 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5266 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5267 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
5268 { "status", acl_parse_range, acl_fetch_stcode, acl_match_range },
5269
5270 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5271 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5272 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5273 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5274 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5275 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
Willy Tarreau8797c062007-05-07 00:55:35 +02005276 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
5277
Willy Tarreauf3d25982007-05-08 22:45:09 +02005278 { NULL, NULL, NULL, NULL },
5279
5280#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005281 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5282 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5283 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5284 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5285 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5286 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5287 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5288
5289 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5290 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5291 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5292 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5293 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5294 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5295 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5296
5297 { "hdr", acl_parse_str, acl_fetch_hdr, acl_match_str },
5298 { "hdr_reg", acl_parse_reg, acl_fetch_hdr, acl_match_reg },
5299 { "hdr_beg", acl_parse_str, acl_fetch_hdr, acl_match_beg },
5300 { "hdr_end", acl_parse_str, acl_fetch_hdr, acl_match_end },
5301 { "hdr_sub", acl_parse_str, acl_fetch_hdr, acl_match_sub },
5302 { "hdr_dir", acl_parse_str, acl_fetch_hdr, acl_match_dir },
5303 { "hdr_dom", acl_parse_str, acl_fetch_hdr, acl_match_dom },
5304 { "hdr_pst", acl_parse_none, acl_fetch_hdr, acl_match_pst },
5305
5306 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5307 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5308 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5309 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5310 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5311 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5312 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5313 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5314
5315 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5316 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5317 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5318 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5319 { NULL, NULL, NULL, NULL },
5320#endif
5321}};
5322
5323
5324__attribute__((constructor))
5325static void __http_protocol_init(void)
5326{
5327 acl_register_keywords(&acl_kws);
5328}
5329
5330
Willy Tarreau58f10d72006-12-04 02:26:12 +01005331/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005332 * Local variables:
5333 * c-indent-level: 8
5334 * c-basic-offset: 8
5335 * End:
5336 */