blob: 4c58e5e216c14fb23475a16363fe608809905ddd [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
37#include <types/capture.h>
38#include <types/client.h>
39#include <types/global.h>
40#include <types/httperr.h>
41#include <types/polling.h>
42#include <types/proxy.h>
43#include <types/server.h>
44
45#include <proto/backend.h>
46#include <proto/buffers.h>
47#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010049#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/proto_http.h>
51#include <proto/queue.h>
52#include <proto/session.h>
53#include <proto/task.h>
54
Willy Tarreau6d1a9882007-01-07 02:03:04 +010055#ifdef CONFIG_HAP_TCPSPLICE
56#include <libtcpsplice.h>
57#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau58f10d72006-12-04 02:26:12 +010059#define DEBUG_PARSE_NO_SPEEDUP
60#undef DEBUG_PARSE_NO_SPEEDUP
61
Willy Tarreau976f1ee2006-12-17 10:06:03 +010062/* This is used to perform a quick jump as an alternative to a break/continue
63 * instruction. The first argument is the label for normal operation, and the
64 * second one is the break/continue instruction in the no_speedup mode.
65 */
66
67#ifdef DEBUG_PARSE_NO_SPEEDUP
68#define QUICK_JUMP(x,y) y
69#else
70#define QUICK_JUMP(x,y) goto x
71#endif
72
Willy Tarreau1c47f852006-07-09 08:22:27 +020073/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010074const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020075 "HTTP/1.0 200 OK\r\n"
76 "Cache-Control: no-cache\r\n"
77 "Connection: close\r\n"
78 "Content-Type: text/html\r\n"
79 "\r\n"
80 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const struct chunk http_200_chunk = {
83 .str = (char *)&HTTP_200,
84 .len = sizeof(HTTP_200)-1
85};
86
87const char *HTTP_302 =
88 "HTTP/1.0 302 Found\r\n"
89 "Cache-Control: no-cache\r\n"
90 "Connection: close\r\n"
91 "Location: "; /* not terminated since it will be concatenated with the URL */
92
93/* same as 302 except that the browser MUST retry with the GET method */
94const char *HTTP_303 =
95 "HTTP/1.0 303 See Other\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
98 "Location: "; /* not terminated since it will be concatenated with the URL */
99
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
101const char *HTTP_401_fmt =
102 "HTTP/1.0 401 Unauthorized\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200105 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
107 "\r\n"
108 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
109
Willy Tarreau0f772532006-12-23 20:51:41 +0100110
111const int http_err_codes[HTTP_ERR_SIZE] = {
112 [HTTP_ERR_400] = 400,
113 [HTTP_ERR_403] = 403,
114 [HTTP_ERR_408] = 408,
115 [HTTP_ERR_500] = 500,
116 [HTTP_ERR_502] = 502,
117 [HTTP_ERR_503] = 503,
118 [HTTP_ERR_504] = 504,
119};
120
Willy Tarreau80587432006-12-24 17:47:20 +0100121static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100122 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100123 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100124 "Cache-Control: no-cache\r\n"
125 "Connection: close\r\n"
126 "Content-Type: text/html\r\n"
127 "\r\n"
128 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
129
130 [HTTP_ERR_403] =
131 "HTTP/1.0 403 Forbidden\r\n"
132 "Cache-Control: no-cache\r\n"
133 "Connection: close\r\n"
134 "Content-Type: text/html\r\n"
135 "\r\n"
136 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
137
138 [HTTP_ERR_408] =
139 "HTTP/1.0 408 Request Time-out\r\n"
140 "Cache-Control: no-cache\r\n"
141 "Connection: close\r\n"
142 "Content-Type: text/html\r\n"
143 "\r\n"
144 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
145
146 [HTTP_ERR_500] =
147 "HTTP/1.0 500 Server Error\r\n"
148 "Cache-Control: no-cache\r\n"
149 "Connection: close\r\n"
150 "Content-Type: text/html\r\n"
151 "\r\n"
152 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
153
154 [HTTP_ERR_502] =
155 "HTTP/1.0 502 Bad Gateway\r\n"
156 "Cache-Control: no-cache\r\n"
157 "Connection: close\r\n"
158 "Content-Type: text/html\r\n"
159 "\r\n"
160 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
161
162 [HTTP_ERR_503] =
163 "HTTP/1.0 503 Service Unavailable\r\n"
164 "Cache-Control: no-cache\r\n"
165 "Connection: close\r\n"
166 "Content-Type: text/html\r\n"
167 "\r\n"
168 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
169
170 [HTTP_ERR_504] =
171 "HTTP/1.0 504 Gateway Time-out\r\n"
172 "Cache-Control: no-cache\r\n"
173 "Connection: close\r\n"
174 "Content-Type: text/html\r\n"
175 "\r\n"
176 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
177
178};
179
Willy Tarreau80587432006-12-24 17:47:20 +0100180/* We must put the messages here since GCC cannot initialize consts depending
181 * on strlen().
182 */
183struct chunk http_err_chunks[HTTP_ERR_SIZE];
184
Willy Tarreau42250582007-04-01 01:30:43 +0200185#define FD_SETS_ARE_BITFIELDS
186#ifdef FD_SETS_ARE_BITFIELDS
187/*
188 * This map is used with all the FD_* macros to check whether a particular bit
189 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
190 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
191 * byte should be encoded. Be careful to always pass bytes from 0 to 255
192 * exclusively to the macros.
193 */
194fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
195fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
196
197#else
198#error "Check if your OS uses bitfields for fd_sets"
199#endif
200
Willy Tarreau80587432006-12-24 17:47:20 +0100201void init_proto_http()
202{
Willy Tarreau42250582007-04-01 01:30:43 +0200203 int i;
204 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100205 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200206
Willy Tarreau80587432006-12-24 17:47:20 +0100207 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
208 if (!http_err_msgs[msg]) {
209 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
210 abort();
211 }
212
213 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
214 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
215 }
Willy Tarreau42250582007-04-01 01:30:43 +0200216
217 /* initialize the log header encoding map : '{|}"#' should be encoded with
218 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
219 * URL encoding only requires '"', '#' to be encoded as well as non-
220 * printable characters above.
221 */
222 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
223 memset(url_encode_map, 0, sizeof(url_encode_map));
224 for (i = 0; i < 32; i++) {
225 FD_SET(i, hdr_encode_map);
226 FD_SET(i, url_encode_map);
227 }
228 for (i = 127; i < 256; i++) {
229 FD_SET(i, hdr_encode_map);
230 FD_SET(i, url_encode_map);
231 }
232
233 tmp = "\"#{|}";
234 while (*tmp) {
235 FD_SET(*tmp, hdr_encode_map);
236 tmp++;
237 }
238
239 tmp = "\"#";
240 while (*tmp) {
241 FD_SET(*tmp, url_encode_map);
242 tmp++;
243 }
Willy Tarreau80587432006-12-24 17:47:20 +0100244}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245
Willy Tarreau53b6c742006-12-17 13:37:46 +0100246/*
247 * We have 26 list of methods (1 per first letter), each of which can have
248 * up to 3 entries (2 valid, 1 null).
249 */
250struct http_method_desc {
251 http_meth_t meth;
252 int len;
253 const char text[8];
254};
255
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100256const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100257 ['C' - 'A'] = {
258 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
259 },
260 ['D' - 'A'] = {
261 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
262 },
263 ['G' - 'A'] = {
264 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
265 },
266 ['H' - 'A'] = {
267 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
268 },
269 ['P' - 'A'] = {
270 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
271 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
272 },
273 ['T' - 'A'] = {
274 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
275 },
276 /* rest is empty like this :
277 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
278 */
279};
280
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100281/* It is about twice as fast on recent architectures to lookup a byte in a
282 * table than two perform a boolean AND or OR between two tests. Refer to
283 * RFC2616 for those chars.
284 */
285
286const char http_is_spht[256] = {
287 [' '] = 1, ['\t'] = 1,
288};
289
290const char http_is_crlf[256] = {
291 ['\r'] = 1, ['\n'] = 1,
292};
293
294const char http_is_lws[256] = {
295 [' '] = 1, ['\t'] = 1,
296 ['\r'] = 1, ['\n'] = 1,
297};
298
299const char http_is_sep[256] = {
300 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
301 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
302 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
303 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
304 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
305};
306
307const char http_is_ctl[256] = {
308 [0 ... 31] = 1,
309 [127] = 1,
310};
311
312/*
313 * A token is any ASCII char that is neither a separator nor a CTL char.
314 * Do not overwrite values in assignment since gcc-2.95 will not handle
315 * them correctly. Instead, define every non-CTL char's status.
316 */
317const char http_is_token[256] = {
318 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
319 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
320 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
321 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
322 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
323 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
324 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
325 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
326 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
327 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
328 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
329 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
330 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
331 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
332 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
333 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
334 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
335 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
336 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
337 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
338 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
339 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
340 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
341 ['|'] = 1, ['}'] = 0, ['~'] = 1,
342};
343
344
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100345/*
346 * An http ver_token is any ASCII which can be found in an HTTP version,
347 * which includes 'H', 'T', 'P', '/', '.' and any digit.
348 */
349const char http_is_ver_token[256] = {
350 ['.'] = 1, ['/'] = 1,
351 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
352 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
353 ['H'] = 1, ['P'] = 1, ['T'] = 1,
354};
355
356
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357#ifdef DEBUG_FULL
358static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
359static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
360#endif
361
Willy Tarreau42250582007-04-01 01:30:43 +0200362static void http_sess_log(struct session *s);
363
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100364/*
365 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
366 * CRLF. Text length is measured first, so it cannot be NULL.
367 * The header is also automatically added to the index <hdr_idx>, and the end
368 * of headers is automatically adjusted. The number of bytes added is returned
369 * on success, otherwise <0 is returned indicating an error.
370 */
371int http_header_add_tail(struct buffer *b, struct http_msg *msg,
372 struct hdr_idx *hdr_idx, const char *text)
373{
374 int bytes, len;
375
376 len = strlen(text);
377 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
378 if (!bytes)
379 return -1;
380 msg->eoh += bytes;
381 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
382}
383
384/*
385 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
386 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
387 * the buffer is only opened and the space reserved, but nothing is copied.
388 * The header is also automatically added to the index <hdr_idx>, and the end
389 * of headers is automatically adjusted. The number of bytes added is returned
390 * on success, otherwise <0 is returned indicating an error.
391 */
392int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
393 struct hdr_idx *hdr_idx, const char *text, int len)
394{
395 int bytes;
396
397 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
398 if (!bytes)
399 return -1;
400 msg->eoh += bytes;
401 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
402}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403
404/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100405 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
406 * If so, returns the position of the first non-space character relative to
407 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
408 * to return a pointer to the place after the first space. Returns 0 if the
409 * header name does not match. Checks are case-insensitive.
410 */
411int http_header_match2(const char *hdr, const char *end,
412 const char *name, int len)
413{
414 const char *val;
415
416 if (hdr + len >= end)
417 return 0;
418 if (hdr[len] != ':')
419 return 0;
420 if (strncasecmp(hdr, name, len) != 0)
421 return 0;
422 val = hdr + len + 1;
423 while (val < end && HTTP_IS_SPHT(*val))
424 val++;
425 if ((val >= end) && (len + 2 <= end - hdr))
426 return len + 2; /* we may replace starting from second space */
427 return val - hdr;
428}
429
430/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200431 * returns a message to the client ; the connection is shut down for read,
432 * and the request is cleared so that no server connection can be initiated.
433 * The client must be in a valid state for this (HEADER, DATA ...).
Willy Tarreau0f772532006-12-23 20:51:41 +0100434 * Nothing is performed on the server side. The message is contained in a
435 * "chunk". If it is null, then an empty message is used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436 * The reply buffer doesn't need to be empty before this.
437 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100438void client_retnclose(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439{
Willy Tarreauf161a342007-04-08 16:59:42 +0200440 EV_FD_CLR(s->cli_fd, DIR_RD);
441 EV_FD_SET(s->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +0200442 tv_eternity(&s->req->rex);
Willy Tarreau73de9892006-11-30 11:40:23 +0100443 if (s->fe->clitimeout)
444 tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445 else
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 */
533int process_session(struct task *t)
534{
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 */
564 if (tv_remain2(&now, &t->expire) <= 0)
565 exit(100);
566#endif
567
568 return tv_remain2(&now, &t->expire); /* nothing more to do */
569 }
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
585 s->logs.t_close = tv_diff(&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);
616 return TIME_ETERNITY; /* rest in peace for eternity */
617}
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 *
826 * This function was intentionnally designed to be called from
827 * 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
829 * labels and variable names.
830 */
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 *
949 * This function was intentionnally designed to be called from
950 * 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
952 * labels and variable names.
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
1077 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH.
1078 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1080{
1081 __label__
1082 http_msg_rqbefore,
1083 http_msg_rqbefore_cr,
1084 http_msg_rqmeth,
1085 http_msg_rqline_end,
1086 http_msg_hdr_first,
1087 http_msg_hdr_name,
1088 http_msg_hdr_l1_sp,
1089 http_msg_hdr_l1_lf,
1090 http_msg_hdr_l1_lws,
1091 http_msg_hdr_val,
1092 http_msg_hdr_l2_lf,
1093 http_msg_hdr_l2_lws,
1094 http_msg_complete_header,
1095 http_msg_last_lf,
1096 http_msg_ood, /* out of data */
1097 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001099 int state; /* updated only when leaving the FSM */
1100 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001101
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001102 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 ptr = buf->lr;
1104 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001105
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001106 if (unlikely(ptr >= end))
1107 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001110 /*
1111 * First, states that are specific to the response only.
1112 * We check them first so that request and headers are
1113 * closer to each other (accessed more often).
1114 */
1115 http_msg_rpbefore:
1116 case HTTP_MSG_RPBEFORE:
1117 if (likely(HTTP_IS_TOKEN(*ptr))) {
1118 if (likely(ptr == buf->data)) {
1119 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001120 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001121 } else {
1122#if PARSE_PRESERVE_EMPTY_LINES
1123 /* only skip empty leading lines, don't remove them */
1124 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001125 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001126#else
1127 /* Remove empty leading lines, as recommended by
1128 * RFC2616. This takes a lot of time because we
1129 * must move all the buffer backwards, but this
1130 * is rarely needed. The method above will be
1131 * cleaner when we'll be able to start sending
1132 * the request from any place in the buffer.
1133 */
1134 buf->lr = ptr;
1135 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001136 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 msg->sol = buf->data;
1138 ptr = buf->data;
1139 end = buf->r;
1140#endif
1141 }
1142 hdr_idx_init(idx);
1143 state = HTTP_MSG_RPVER;
1144 goto http_msg_rpver;
1145 }
1146
1147 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1148 goto http_msg_invalid;
1149
1150 if (unlikely(*ptr == '\n'))
1151 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1152 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1153 /* stop here */
1154
1155 http_msg_rpbefore_cr:
1156 case HTTP_MSG_RPBEFORE_CR:
1157 EXPECT_LF_HERE(ptr, http_msg_invalid);
1158 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1159 /* stop here */
1160
1161 http_msg_rpver:
1162 case HTTP_MSG_RPVER:
1163 case HTTP_MSG_RPVER_SP:
1164 case HTTP_MSG_RPCODE:
1165 case HTTP_MSG_RPCODE_SP:
1166 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001167 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001168 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001169 if (unlikely(!ptr))
1170 return;
1171
1172 /* we have a full response and we know that we have either a CR
1173 * or an LF at <ptr>.
1174 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001175 //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 +01001176 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1177
1178 msg->sol = ptr;
1179 if (likely(*ptr == '\r'))
1180 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1181 goto http_msg_rpline_end;
1182
1183 http_msg_rpline_end:
1184 case HTTP_MSG_RPLINE_END:
1185 /* msg->sol must point to the first of CR or LF. */
1186 EXPECT_LF_HERE(ptr, http_msg_invalid);
1187 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1188 /* stop here */
1189
1190 /*
1191 * Second, states that are specific to the request only
1192 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001193 http_msg_rqbefore:
1194 case HTTP_MSG_RQBEFORE:
1195 if (likely(HTTP_IS_TOKEN(*ptr))) {
1196 if (likely(ptr == buf->data)) {
1197 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001198 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199 } else {
1200#if PARSE_PRESERVE_EMPTY_LINES
1201 /* only skip empty leading lines, don't remove them */
1202 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001203 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001204#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001205 /* Remove empty leading lines, as recommended by
1206 * RFC2616. This takes a lot of time because we
1207 * must move all the buffer backwards, but this
1208 * is rarely needed. The method above will be
1209 * cleaner when we'll be able to start sending
1210 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001211 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 buf->lr = ptr;
1213 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001214 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001215 msg->sol = buf->data;
1216 ptr = buf->data;
1217 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001218#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001219 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001220 /* we will need this when keep-alive will be supported
1221 hdr_idx_init(idx);
1222 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001223 state = HTTP_MSG_RQMETH;
1224 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001226
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001227 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1228 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230 if (unlikely(*ptr == '\n'))
1231 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1232 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001233 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001234
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235 http_msg_rqbefore_cr:
1236 case HTTP_MSG_RQBEFORE_CR:
1237 EXPECT_LF_HERE(ptr, http_msg_invalid);
1238 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001239 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001240
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 http_msg_rqmeth:
1242 case HTTP_MSG_RQMETH:
1243 case HTTP_MSG_RQMETH_SP:
1244 case HTTP_MSG_RQURI:
1245 case HTTP_MSG_RQURI_SP:
1246 case HTTP_MSG_RQVER:
1247 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001248 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001249 if (unlikely(!ptr))
1250 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001251
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 /* we have a full request and we know that we have either a CR
1253 * or an LF at <ptr>.
1254 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001255 //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 +01001256 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258 msg->sol = ptr;
1259 if (likely(*ptr == '\r'))
1260 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001262
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 http_msg_rqline_end:
1264 case HTTP_MSG_RQLINE_END:
1265 /* check for HTTP/0.9 request : no version information available.
1266 * msg->sol must point to the first of CR or LF.
1267 */
1268 if (unlikely(msg->sl.rq.v_l == 0))
1269 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 EXPECT_LF_HERE(ptr, http_msg_invalid);
1272 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001273 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001274
Willy Tarreau8973c702007-01-21 23:58:29 +01001275 /*
1276 * Common states below
1277 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 http_msg_hdr_first:
1279 case HTTP_MSG_HDR_FIRST:
1280 msg->sol = ptr;
1281 if (likely(!HTTP_IS_CRLF(*ptr))) {
1282 goto http_msg_hdr_name;
1283 }
1284
1285 if (likely(*ptr == '\r'))
1286 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1287 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 http_msg_hdr_name:
1290 case HTTP_MSG_HDR_NAME:
1291 /* assumes msg->sol points to the first char */
1292 if (likely(HTTP_IS_TOKEN(*ptr)))
1293 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 if (likely(*ptr == ':')) {
1296 msg->col = ptr - buf->data;
1297 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1298 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 http_msg_hdr_l1_sp:
1303 case HTTP_MSG_HDR_L1_SP:
1304 /* assumes msg->sol points to the first char and msg->col to the colon */
1305 if (likely(HTTP_IS_SPHT(*ptr)))
1306 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001307
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 /* header value can be basically anything except CR/LF */
1309 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(!HTTP_IS_CRLF(*ptr))) {
1312 goto http_msg_hdr_val;
1313 }
1314
1315 if (likely(*ptr == '\r'))
1316 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1317 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 http_msg_hdr_l1_lf:
1320 case HTTP_MSG_HDR_L1_LF:
1321 EXPECT_LF_HERE(ptr, http_msg_invalid);
1322 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001323
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324 http_msg_hdr_l1_lws:
1325 case HTTP_MSG_HDR_L1_LWS:
1326 if (likely(HTTP_IS_SPHT(*ptr))) {
1327 /* replace HT,CR,LF with spaces */
1328 for (; buf->data+msg->sov < ptr; msg->sov++)
1329 buf->data[msg->sov] = ' ';
1330 goto http_msg_hdr_l1_sp;
1331 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001332 /* we had a header consisting only in spaces ! */
1333 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 goto http_msg_complete_header;
1335
1336 http_msg_hdr_val:
1337 case HTTP_MSG_HDR_VAL:
1338 /* assumes msg->sol points to the first char, msg->col to the
1339 * colon, and msg->sov points to the first character of the
1340 * value.
1341 */
1342 if (likely(!HTTP_IS_CRLF(*ptr)))
1343 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 msg->eol = ptr;
1346 /* Note: we could also copy eol into ->eoh so that we have the
1347 * real header end in case it ends with lots of LWS, but is this
1348 * really needed ?
1349 */
1350 if (likely(*ptr == '\r'))
1351 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1352 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001353
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 http_msg_hdr_l2_lf:
1355 case HTTP_MSG_HDR_L2_LF:
1356 EXPECT_LF_HERE(ptr, http_msg_invalid);
1357 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001358
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001359 http_msg_hdr_l2_lws:
1360 case HTTP_MSG_HDR_L2_LWS:
1361 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1362 /* LWS: replace HT,CR,LF with spaces */
1363 for (; msg->eol < ptr; msg->eol++)
1364 *msg->eol = ' ';
1365 goto http_msg_hdr_val;
1366 }
1367 http_msg_complete_header:
1368 /*
1369 * It was a new header, so the last one is finished.
1370 * Assumes msg->sol points to the first char, msg->col to the
1371 * colon, msg->sov points to the first character of the value
1372 * and msg->eol to the first CR or LF so we know how the line
1373 * ends. We insert last header into the index.
1374 */
1375 /*
1376 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1377 write(2, msg->sol, msg->eol-msg->sol);
1378 fprintf(stderr,"\n");
1379 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1382 idx, idx->tail) < 0))
1383 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001384
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 msg->sol = ptr;
1386 if (likely(!HTTP_IS_CRLF(*ptr))) {
1387 goto http_msg_hdr_name;
1388 }
1389
1390 if (likely(*ptr == '\r'))
1391 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1392 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 http_msg_last_lf:
1395 case HTTP_MSG_LAST_LF:
1396 /* Assumes msg->sol points to the first of either CR or LF */
1397 EXPECT_LF_HERE(ptr, http_msg_invalid);
1398 ptr++;
1399 buf->lr = ptr;
1400 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001401 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 return;
1403#ifdef DEBUG_FULL
1404 default:
1405 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1406 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001407#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 }
1409 http_msg_ood:
1410 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001411 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 buf->lr = ptr;
1413 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 http_msg_invalid:
1416 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001417 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 return;
1419}
1420
1421/*
1422 * manages the client FSM and its socket. BTW, it also tries to handle the
1423 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1424 * 0 else.
1425 */
1426int process_cli(struct session *t)
1427{
1428 int s = t->srv_state;
1429 int c = t->cli_state;
1430 struct buffer *req = t->req;
1431 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1434 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001435 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 req->rex.tv_sec, req->rex.tv_usec,
1437 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (c == CL_STHEADERS) {
1440 /*
1441 * Now parse the partial (or complete) lines.
1442 * We will check the request syntax, and also join multi-line
1443 * headers. An index of all the lines will be elaborated while
1444 * parsing.
1445 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001446 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001449 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 * req->data + req->eoh = end of processed headers / start of current one
1451 * req->data + req->eol = end of current header or line (LF or CRLF)
1452 * req->lr = first non-visited byte
1453 * req->r = end of data
1454 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001457 struct http_txn *txn = &t->txn;
1458 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001462 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 /* 1: we might have to print this header in debug mode */
1465 if (unlikely((global.mode & MODE_DEBUG) &&
1466 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001467 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001469
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001470 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 eol = sol + msg->sl.rq.l;
1472 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001473
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001474 sol += hdr_idx_first_pos(&txn->hdr_idx);
1475 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001478 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001480 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1481 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 }
1483 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001484
Willy Tarreau58f10d72006-12-04 02:26:12 +01001485
1486 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001488 * If not so, we check the FD and buffer states before leaving.
1489 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1491 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001492 *
1493 */
1494
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001495 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 /*
1497 * First, let's catch bad requests.
1498 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001499 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001501
1502 /* 1: Since we are in header mode, if there's no space
1503 * left for headers, we won't be able to free more
1504 * later, so the session will never terminate. We
1505 * must terminate it now.
1506 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 if (unlikely(req->l >= req->rlim - req->data)) {
1508 /* FIXME: check if URI is set and return Status
1509 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001510 */
Willy Tarreau06619262006-12-17 08:37:22 +01001511 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001512 }
1513
1514 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1516 /* read error, or last read : give up. */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001517 tv_eternity(&req->rex);
1518 fd_delete(t->cli_fd);
1519 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001520 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001521 if (!(t->flags & SN_ERR_MASK))
1522 t->flags |= SN_ERR_CLICL;
1523 if (!(t->flags & SN_FINST_MASK))
1524 t->flags |= SN_FINST_R;
1525 return 1;
1526 }
1527
1528 /* 3: has the read timeout expired ? */
Willy Tarreau8d7d1492007-04-29 10:50:43 +02001529 else if (unlikely(tv_cmp2_le(&req->rex, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001530 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001531 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001532 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001533 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001534 if (!(t->flags & SN_ERR_MASK))
1535 t->flags |= SN_ERR_CLITO;
1536 if (!(t->flags & SN_FINST_MASK))
1537 t->flags |= SN_FINST_R;
1538 return 1;
1539 }
1540
1541 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001542 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001543 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001544 * full. We cannot loop here since stream_sock_read will disable it only if
1545 * req->l == rlim-data
1546 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001547 if (t->fe->clitimeout)
1548 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
1549 else
1550 tv_eternity(&req->rex);
1551 }
1552 return t->cli_state != CL_STHEADERS;
1553 }
1554
1555
1556 /****************************************************************
1557 * More interesting part now : we know that we have a complete *
1558 * request which at least looks like HTTP. We have an indicator *
1559 * of each header's length, so we can parse them quickly. *
1560 ****************************************************************/
1561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 /*
1563 * 1: identify the method
1564 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001565 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001566
1567 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001569 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001571 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 if (unlikely((t->fe->monitor_uri_len != 0) &&
1573 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1574 !memcmp(&req->data[msg->sl.rq.u],
1575 t->fe->monitor_uri,
1576 t->fe->monitor_uri_len))) {
1577 /*
1578 * We have found the monitor URI
1579 */
1580 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001581 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 client_retnclose(t, &http_200_chunk);
1583 goto return_prx_cond;
1584 }
1585
1586 /*
1587 * 3: Maybe we have to copy the original REQURI for the logs ?
1588 * Note: we cannot log anymore if the request has been
1589 * classified as invalid.
1590 */
1591 if (unlikely(t->logs.logwait & LW_REQ)) {
1592 /* we have a complete HTTP request that we must log */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001593 if ((txn->uri = pool_alloc(requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 int urilen = msg->sl.rq.l;
1595
1596 if (urilen >= REQURI_LEN)
1597 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001598 memcpy(txn->uri, &req->data[msg->som], urilen);
1599 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600
1601 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001602 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 } else {
1604 Alert("HTTP logging : out of memory.\n");
1605 }
1606 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001607
Willy Tarreau06619262006-12-17 08:37:22 +01001608
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1610 if (unlikely(msg->sl.rq.v_l == 0)) {
1611 int delta;
1612 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001613 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 cur_end = msg->sol + msg->sl.rq.l;
1615 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001616
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 if (msg->sl.rq.u_l == 0) {
1618 /* if no URI was set, add "/" */
1619 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1620 cur_end += delta;
1621 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001622 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 /* add HTTP version */
1624 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1625 msg->eoh += delta;
1626 cur_end += delta;
1627 cur_end = (char *)http_parse_reqline(msg, req->data,
1628 HTTP_MSG_RQMETH,
1629 msg->sol, cur_end + 1,
1630 NULL, NULL);
1631 if (unlikely(!cur_end))
1632 goto return_bad_req;
1633
1634 /* we have a full HTTP/1.0 request now and we know that
1635 * we have either a CR or an LF at <ptr>.
1636 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001637 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001638 }
1639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640
1641 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001642 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001643 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001644 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001645
1646 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001648 * As opposed to version 1.2, now they will be evaluated in the
1649 * filters order and not in the header order. This means that
1650 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001651 *
1652 * We can now check whether we want to switch to another
1653 * backend, in which case we will re-check the backend's
1654 * filters and various options. In order to support 3-level
1655 * switching, here's how we should proceed :
1656 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001657 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001658 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001659 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001660 * There cannot be any switch from there, so ->be cannot be
1661 * changed anymore.
1662 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001663 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001664 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001665 * The response path will be able to apply either ->be, or
1666 * ->be then ->fe filters in order to match the reverse of
1667 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001668 */
1669
Willy Tarreau06619262006-12-17 08:37:22 +01001670 do {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001671 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001672 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001673
Willy Tarreau06619262006-12-17 08:37:22 +01001674 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001675 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1677 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001678 }
1679
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001680 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1681 /* to ensure correct connection accounting on
1682 * the backend, we count the connection for the
1683 * one managing the queue.
1684 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001685 t->be->beconn++;
1686 if (t->be->beconn > t->be->beconn_max)
1687 t->be->beconn_max = t->be->beconn;
1688 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001689 t->flags |= SN_BE_ASSIGNED;
1690 }
1691
Willy Tarreau06619262006-12-17 08:37:22 +01001692 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001693 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001694 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001695 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001696 /* let's log the request time */
1697 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001698 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001699 goto return_prx_cond;
1700 }
1701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001703 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 !(t->flags & SN_CONN_CLOSED)) {
1705 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001706 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001708
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001709 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 old_idx = 0;
1711
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001712 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1713 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 cur_ptr = cur_next;
1715 cur_end = cur_ptr + cur_hdr->len;
1716 cur_next = cur_end + cur_hdr->cr + 1;
1717
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001718 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1719 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 /* 3 possibilities :
1721 * - we have already set Connection: close,
1722 * so we remove this line.
1723 * - we have not yet set Connection: close,
1724 * but this line indicates close. We leave
1725 * it untouched and set the flag.
1726 * - we have not yet set Connection: close,
1727 * and this line indicates non-close. We
1728 * replace it.
1729 */
1730 if (t->flags & SN_CONN_CLOSED) {
1731 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001732 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001734 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1735 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 cur_hdr->len = 0;
1737 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001738 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1739 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1740 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 cur_next += delta;
1742 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001743 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 }
1745 t->flags |= SN_CONN_CLOSED;
1746 }
1747 }
1748 old_idx = cur_idx;
1749 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001750 }
1751 /* add request headers from the rule sets in the same order */
1752 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1753 if (unlikely(http_header_add_tail(req,
1754 &txn->req,
1755 &txn->hdr_idx,
1756 rule_set->req_add[cur_idx])) < 0)
1757 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001758 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001759
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001761 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001762 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001763 /* we have to check the URI and auth for this request */
1764 if (stats_check_uri_auth(t, rule_set))
1765 return 1;
1766 }
1767
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001768 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1769 /* No backend was set, but there was a default
1770 * backend set in the frontend, so we use it and
1771 * loop again.
1772 */
1773 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001774 t->be->beconn++;
1775 if (t->be->beconn > t->be->beconn_max)
1776 t->be->beconn_max = t->be->beconn;
1777 t->be->cum_beconn++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001778 t->flags |= SN_BE_ASSIGNED;
1779 }
1780 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001781
Willy Tarreau58f10d72006-12-04 02:26:12 +01001782
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001783 if (!(t->flags & SN_BE_ASSIGNED)) {
1784 /* To ensure correct connection accounting on
1785 * the backend, we count the connection for the
1786 * one managing the queue.
1787 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001788 t->be->beconn++;
1789 if (t->be->beconn > t->be->beconn_max)
1790 t->be->beconn_max = t->be->beconn;
1791 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001792 t->flags |= SN_BE_ASSIGNED;
1793 }
1794
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001795 /*
1796 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001797 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001798 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001799 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001800 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001801
Willy Tarreau58f10d72006-12-04 02:26:12 +01001802
Willy Tarreau58f10d72006-12-04 02:26:12 +01001803
Willy Tarreau58f10d72006-12-04 02:26:12 +01001804
Willy Tarreau2a324282006-12-05 00:05:46 +01001805 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001807 * so let's do the same now.
1808 */
1809
1810 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001811 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001812 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001813 }
1814
1815
1816 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001818 * Note that doing so might move headers in the request, but
1819 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001820 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001821 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001822 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001823 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001824
Willy Tarreau58f10d72006-12-04 02:26:12 +01001825
Willy Tarreau2a324282006-12-05 00:05:46 +01001826 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001827 * 9: add X-Forwarded-For if either the frontend or the backend
1828 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001829 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001830 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001831 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001832 /* Add an X-Forwarded-For header unless the source IP is
1833 * in the 'except' network range.
1834 */
1835 if ((!t->fe->except_mask.s_addr ||
1836 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1837 != t->fe->except_net.s_addr) &&
1838 (!t->be->except_mask.s_addr ||
1839 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1840 != t->be->except_net.s_addr)) {
1841 int len;
1842 unsigned char *pn;
1843 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001844
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001845 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1846 pn[0], pn[1], pn[2], pn[3]);
1847
1848 if (unlikely(http_header_add_tail2(req, &txn->req,
1849 &txn->hdr_idx, trash, len)) < 0)
1850 goto return_bad_req;
1851 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001852 }
1853 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001854 /* FIXME: for the sake of completeness, we should also support
1855 * 'except' here, although it is mostly useless in this case.
1856 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001857 int len;
1858 char pn[INET6_ADDRSTRLEN];
1859 inet_ntop(AF_INET6,
1860 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1861 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001862 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1863 if (unlikely(http_header_add_tail2(req, &txn->req,
1864 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001865 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001866 }
1867 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001868
Willy Tarreau2a324282006-12-05 00:05:46 +01001869 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001870 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02001871 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01001872 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02001873 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001874 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02001875 if ((unlikely(msg->sl.rq.v_l != 8) ||
1876 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
1877 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001878 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001879 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001880 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001881 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001882
Willy Tarreau2a324282006-12-05 00:05:46 +01001883 /*************************************************************
1884 * OK, that's finished for the headers. We have done what we *
1885 * could. Let's switch to the DATA state. *
1886 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001887
Willy Tarreau2a324282006-12-05 00:05:46 +01001888 t->cli_state = CL_STDATA;
1889 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001890
Willy Tarreau2a324282006-12-05 00:05:46 +01001891 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001892
Willy Tarreau2a324282006-12-05 00:05:46 +01001893 if (!t->fe->clitimeout ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001894 (t->srv_state < SV_STDATA && t->be->srvtimeout)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001895 /* If the client has no timeout, or if the server is not ready yet,
1896 * and we know for sure that it can expire, then it's cleaner to
1897 * disable the timeout on the client side so that too low values
1898 * cannot make the sessions abort too early.
1899 *
1900 * FIXME-20050705: the server needs a way to re-enable this time-out
1901 * when it switches its state, otherwise a client can stay connected
1902 * indefinitely. This now seems to be OK.
1903 */
1904 tv_eternity(&req->rex);
1905 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001906
Willy Tarreau2a324282006-12-05 00:05:46 +01001907 /* When a connection is tarpitted, we use the queue timeout for the
1908 * tarpit delay, which currently happens to be the server's connect
1909 * timeout. If unset, then set it to zero because we really want it
1910 * to expire at one moment.
1911 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001912 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001913 t->req->l = 0;
1914 /* flush the request so that we can drop the connection early
1915 * if the client closes first.
1916 */
1917 tv_delayfrom(&req->cex, &now,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001918 t->be->contimeout ? t->be->contimeout : 0);
Willy Tarreau2a324282006-12-05 00:05:46 +01001919 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001920
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01001922 goto process_data;
1923
1924 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001925 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001926 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01001927 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001928 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01001929 return_prx_cond:
1930 if (!(t->flags & SN_ERR_MASK))
1931 t->flags |= SN_ERR_PRXCOND;
1932 if (!(t->flags & SN_FINST_MASK))
1933 t->flags |= SN_FINST_R;
1934 return 1;
1935
Willy Tarreaubaaee002006-06-26 02:48:02 +02001936 }
1937 else if (c == CL_STDATA) {
1938 process_data:
1939 /* FIXME: this error handling is partly buggy because we always report
1940 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1941 * or HEADER phase. BTW, it's not logical to expire the client while
1942 * we're waiting for the server to connect.
1943 */
1944 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001945 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001946 tv_eternity(&req->rex);
1947 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001948 fd_delete(t->cli_fd);
1949 t->cli_state = CL_STCLOSE;
1950 if (!(t->flags & SN_ERR_MASK))
1951 t->flags |= SN_ERR_CLICL;
1952 if (!(t->flags & SN_FINST_MASK)) {
1953 if (t->pend_pos)
1954 t->flags |= SN_FINST_Q;
1955 else if (s == SV_STCONN)
1956 t->flags |= SN_FINST_C;
1957 else
1958 t->flags |= SN_FINST_D;
1959 }
1960 return 1;
1961 }
1962 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001963 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001964 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02001965 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001966 t->cli_state = CL_STSHUTR;
1967 return 1;
1968 }
1969 /* last server read and buffer empty */
1970 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001971 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02001972 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001973 shutdown(t->cli_fd, SHUT_WR);
1974 /* We must ensure that the read part is still alive when switching
1975 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02001976 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau73de9892006-11-30 11:40:23 +01001977 if (t->fe->clitimeout)
1978 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001979 t->cli_state = CL_STSHUTW;
1980 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1981 return 1;
1982 }
1983 /* read timeout */
Willy Tarreau8d7d1492007-04-29 10:50:43 +02001984 else if (tv_cmp2_le(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001985 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02001986 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001987 t->cli_state = CL_STSHUTR;
1988 if (!(t->flags & SN_ERR_MASK))
1989 t->flags |= SN_ERR_CLITO;
1990 if (!(t->flags & SN_FINST_MASK)) {
1991 if (t->pend_pos)
1992 t->flags |= SN_FINST_Q;
1993 else if (s == SV_STCONN)
1994 t->flags |= SN_FINST_C;
1995 else
1996 t->flags |= SN_FINST_D;
1997 }
1998 return 1;
1999 }
2000 /* write timeout */
Willy Tarreau8d7d1492007-04-29 10:50:43 +02002001 else if (tv_cmp2_le(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002002 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002003 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002004 shutdown(t->cli_fd, SHUT_WR);
2005 /* We must ensure that the read part is still alive when switching
2006 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002007 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau73de9892006-11-30 11:40:23 +01002008 if (t->fe->clitimeout)
2009 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002010
2011 t->cli_state = CL_STSHUTW;
2012 if (!(t->flags & SN_ERR_MASK))
2013 t->flags |= SN_ERR_CLITO;
2014 if (!(t->flags & SN_FINST_MASK)) {
2015 if (t->pend_pos)
2016 t->flags |= SN_FINST_Q;
2017 else if (s == SV_STCONN)
2018 t->flags |= SN_FINST_C;
2019 else
2020 t->flags |= SN_FINST_D;
2021 }
2022 return 1;
2023 }
2024
2025 if (req->l >= req->rlim - req->data) {
2026 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002027 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002028 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002029 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002030 }
2031 } else {
2032 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002033 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreau73de9892006-11-30 11:40:23 +01002034 if (!t->fe->clitimeout ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002035 (t->srv_state < SV_STDATA && t->be->srvtimeout))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002036 /* If the client has no timeout, or if the server not ready yet, and we
2037 * know for sure that it can expire, then it's cleaner to disable the
2038 * timeout on the client side so that too low values cannot make the
2039 * sessions abort too early.
2040 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002041 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002042 else
Willy Tarreau73de9892006-11-30 11:40:23 +01002043 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002044 }
2045 }
2046
2047 if ((rep->l == 0) ||
2048 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002049 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2050 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002051 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002052 }
2053 } else {
2054 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002055 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2056 /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01002057 if (t->fe->clitimeout) {
2058 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002059 /* FIXME: to prevent the client from expiring read timeouts during writes,
2060 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002061 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002062 }
2063 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002064 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002065 }
2066 }
2067 return 0; /* other cases change nothing */
2068 }
2069 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002070 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002071 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002072 fd_delete(t->cli_fd);
2073 t->cli_state = CL_STCLOSE;
2074 if (!(t->flags & SN_ERR_MASK))
2075 t->flags |= SN_ERR_CLICL;
2076 if (!(t->flags & SN_FINST_MASK)) {
2077 if (t->pend_pos)
2078 t->flags |= SN_FINST_Q;
2079 else if (s == SV_STCONN)
2080 t->flags |= SN_FINST_C;
2081 else
2082 t->flags |= SN_FINST_D;
2083 }
2084 return 1;
2085 }
2086 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2087 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002088 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002089 fd_delete(t->cli_fd);
2090 t->cli_state = CL_STCLOSE;
2091 return 1;
2092 }
Willy Tarreau8d7d1492007-04-29 10:50:43 +02002093 else if (tv_cmp2_le(&rep->wex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002094 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002095 fd_delete(t->cli_fd);
2096 t->cli_state = CL_STCLOSE;
2097 if (!(t->flags & SN_ERR_MASK))
2098 t->flags |= SN_ERR_CLITO;
2099 if (!(t->flags & SN_FINST_MASK)) {
2100 if (t->pend_pos)
2101 t->flags |= SN_FINST_Q;
2102 else if (s == SV_STCONN)
2103 t->flags |= SN_FINST_C;
2104 else
2105 t->flags |= SN_FINST_D;
2106 }
2107 return 1;
2108 }
2109
2110 if (t->flags & SN_SELF_GEN) {
2111 produce_content(t);
2112 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002113 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002114 fd_delete(t->cli_fd);
2115 t->cli_state = CL_STCLOSE;
2116 return 1;
2117 }
2118 }
2119
2120 if ((rep->l == 0)
2121 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002122 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2123 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002124 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002125 }
2126 } else {
2127 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002128 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2129 /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01002130 if (t->fe->clitimeout) {
2131 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002132 /* FIXME: to prevent the client from expiring read timeouts during writes,
2133 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002134 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002135 }
2136 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002137 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002138 }
2139 }
2140 return 0;
2141 }
2142 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002143 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002144 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002145 fd_delete(t->cli_fd);
2146 t->cli_state = CL_STCLOSE;
2147 if (!(t->flags & SN_ERR_MASK))
2148 t->flags |= SN_ERR_CLICL;
2149 if (!(t->flags & SN_FINST_MASK)) {
2150 if (t->pend_pos)
2151 t->flags |= SN_FINST_Q;
2152 else if (s == SV_STCONN)
2153 t->flags |= SN_FINST_C;
2154 else
2155 t->flags |= SN_FINST_D;
2156 }
2157 return 1;
2158 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002159 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002160 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002161 fd_delete(t->cli_fd);
2162 t->cli_state = CL_STCLOSE;
2163 return 1;
2164 }
Willy Tarreau8d7d1492007-04-29 10:50:43 +02002165 else if (tv_cmp2_le(&req->rex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002166 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002167 fd_delete(t->cli_fd);
2168 t->cli_state = CL_STCLOSE;
2169 if (!(t->flags & SN_ERR_MASK))
2170 t->flags |= SN_ERR_CLITO;
2171 if (!(t->flags & SN_FINST_MASK)) {
2172 if (t->pend_pos)
2173 t->flags |= SN_FINST_Q;
2174 else if (s == SV_STCONN)
2175 t->flags |= SN_FINST_C;
2176 else
2177 t->flags |= SN_FINST_D;
2178 }
2179 return 1;
2180 }
2181 else if (req->l >= req->rlim - req->data) {
2182 /* no room to read more data */
2183
2184 /* FIXME-20050705: is it possible for a client to maintain a session
2185 * after the timeout by sending more data after it receives a close ?
2186 */
2187
Willy Tarreau66319382007-04-08 17:17:37 +02002188 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002189 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002190 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002191 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2192 }
2193 } else {
2194 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002195 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreau73de9892006-11-30 11:40:23 +01002196 if (t->fe->clitimeout)
2197 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002199 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002200 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2201 }
2202 }
2203 return 0;
2204 }
2205 else { /* CL_STCLOSE: nothing to do */
2206 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2207 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002208 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 +02002209 write(1, trash, len);
2210 }
2211 return 0;
2212 }
2213 return 0;
2214}
2215
2216
2217/*
2218 * manages the server FSM and its socket. It returns 1 if a state has changed
2219 * (and a resync may be needed), 0 else.
2220 */
2221int process_srv(struct session *t)
2222{
2223 int s = t->srv_state;
2224 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002225 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002226 struct buffer *req = t->req;
2227 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228 int conn_err;
2229
2230#ifdef DEBUG_FULL
2231 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2232#endif
2233 //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 +02002234 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2235 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002236 //);
2237 if (s == SV_STIDLE) {
2238 if (c == CL_STHEADERS)
2239 return 0; /* stay in idle, waiting for data to reach the client side */
2240 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2241 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002242 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002243 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002244 if (t->pend_pos)
2245 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2246 /* note that this must not return any error because it would be able to
2247 * overwrite the client_retnclose() output.
2248 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002249 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002250 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002251 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002252 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 +02002253
2254 return 1;
2255 }
2256 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002257 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002258 /* This connection is being tarpitted. The CLIENT side has
2259 * already set the connect expiration date to the right
2260 * timeout. We just have to check that it has not expired.
2261 */
Willy Tarreau8d7d1492007-04-29 10:50:43 +02002262 if (!tv_cmp2_le(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002263 return 0;
2264
2265 /* We will set the queue timer to the time spent, just for
2266 * logging purposes. We fake a 500 server error, so that the
2267 * attacker will not suspect his connection has been tarpitted.
2268 * It will not cause trouble to the logs because we can exclude
2269 * the tarpitted connections by filtering on the 'PT' status flags.
2270 */
2271 tv_eternity(&req->cex);
2272 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2273 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002274 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002275 return 1;
2276 }
2277
Willy Tarreaubaaee002006-06-26 02:48:02 +02002278 /* Right now, we will need to create a connection to the server.
2279 * We might already have tried, and got a connection pending, in
2280 * which case we will not do anything till it's pending. It's up
2281 * to any other session to release it and wake us up again.
2282 */
2283 if (t->pend_pos) {
Willy Tarreau8d7d1492007-04-29 10:50:43 +02002284 if (!tv_cmp2_le(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002285 return 0;
2286 else {
2287 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002288 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002289 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2290 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002291 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002292 if (t->srv)
2293 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002294 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002295 return 1;
2296 }
2297 }
2298
2299 do {
2300 /* first, get a connection */
2301 if (srv_redispatch_connect(t))
2302 return t->srv_state != SV_STIDLE;
2303
2304 /* try to (re-)connect to the server, and fail if we expire the
2305 * number of retries.
2306 */
2307 if (srv_retryable_connect(t)) {
2308 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2309 return t->srv_state != SV_STIDLE;
2310 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002311 } while (1);
2312 }
2313 }
2314 else if (s == SV_STCONN) { /* connection in progress */
2315 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2316 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002317 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002318 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002319 fd_delete(t->srv_fd);
2320 if (t->srv)
2321 t->srv->cur_sess--;
2322
2323 /* note that this must not return any error because it would be able to
2324 * overwrite the client_retnclose() output.
2325 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002326 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002327 return 1;
2328 }
Willy Tarreau8d7d1492007-04-29 10:50:43 +02002329 if (!(req->flags & BF_WRITE_STATUS) && !tv_cmp2_le(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002330 //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 +02002331 return 0; /* nothing changed */
2332 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002333 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002334 /* timeout, asynchronous connect error or first write error */
2335 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2336
2337 fd_delete(t->srv_fd);
2338 if (t->srv)
2339 t->srv->cur_sess--;
2340
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002341 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002342 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2343 else
2344 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2345
2346 /* ensure that we have enough retries left */
2347 if (srv_count_retry_down(t, conn_err))
2348 return 1;
2349
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002350 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002351 /* We're on our last chance, and the REDISP option was specified.
2352 * We will ignore cookie and force to balance or use the dispatcher.
2353 */
2354 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002355 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002356 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002357
2358 if (t->srv)
2359 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002360 t->be->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002361
2362 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2363 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002364 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002365
2366 /* first, get a connection */
2367 if (srv_redispatch_connect(t))
2368 return t->srv_state != SV_STIDLE;
2369 }
2370
Willy Tarreaubaaee002006-06-26 02:48:02 +02002371 do {
2372 /* Now we will try to either reconnect to the same server or
2373 * connect to another server. If the connection gets queued
2374 * because all servers are saturated, then we will go back to
2375 * the SV_STIDLE state.
2376 */
2377 if (srv_retryable_connect(t)) {
2378 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2379 return t->srv_state != SV_STCONN;
2380 }
2381
2382 /* we need to redispatch the connection to another server */
2383 if (srv_redispatch_connect(t))
2384 return t->srv_state != SV_STCONN;
2385 } while (1);
2386 }
2387 else { /* no error or write 0 */
2388 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
2389
2390 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2391 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002392 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002393 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002394 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002395 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002396 if (t->be->srvtimeout) {
2397 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002398 /* FIXME: to prevent the server from expiring read timeouts during writes,
2399 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002400 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002401 }
2402 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002403 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002404 }
2405
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002406 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002407 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002408 if (t->be->srvtimeout)
2409 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002410 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002411 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002412
2413 t->srv_state = SV_STDATA;
2414 if (t->srv)
2415 t->srv->cum_sess++;
2416 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2417
2418 /* if the user wants to log as soon as possible, without counting
2419 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002420 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002421 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002422 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002423 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002424#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002425 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002426 /* TCP splicing supported by both FE and BE */
2427 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2428 }
2429#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002430 }
2431 else {
2432 t->srv_state = SV_STHEADERS;
2433 if (t->srv)
2434 t->srv->cum_sess++;
2435 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002436 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2437 /* reset hdr_idx which was already initialized by the request.
2438 * right now, the http parser does it.
2439 * hdr_idx_init(&t->txn.hdr_idx);
2440 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002441 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002442 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002443 return 1;
2444 }
2445 }
2446 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002447 /*
2448 * Now parse the partial (or complete) lines.
2449 * We will check the response syntax, and also join multi-line
2450 * headers. An index of all the lines will be elaborated while
2451 * parsing.
2452 *
2453 * For the parsing, we use a 28 states FSM.
2454 *
2455 * Here is the information we currently have :
2456 * rep->data + req->som = beginning of response
2457 * rep->data + req->eoh = end of processed headers / start of current one
2458 * rep->data + req->eol = end of current header or line (LF or CRLF)
2459 * rep->lr = first non-visited byte
2460 * rep->r = end of data
2461 */
2462
2463 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002464 struct http_msg *msg = &txn->rsp;
2465 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002466
Willy Tarreaua15645d2007-03-18 16:22:39 +01002467 if (likely(rep->lr < rep->r))
2468 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002469
Willy Tarreaua15645d2007-03-18 16:22:39 +01002470 /* 1: we might have to print this header in debug mode */
2471 if (unlikely((global.mode & MODE_DEBUG) &&
2472 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2473 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2474 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002475
Willy Tarreaua15645d2007-03-18 16:22:39 +01002476 sol = rep->data + msg->som;
2477 eol = sol + msg->sl.rq.l;
2478 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002479
Willy Tarreaua15645d2007-03-18 16:22:39 +01002480 sol += hdr_idx_first_pos(&txn->hdr_idx);
2481 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002482
Willy Tarreaua15645d2007-03-18 16:22:39 +01002483 while (cur_idx) {
2484 eol = sol + txn->hdr_idx.v[cur_idx].len;
2485 debug_hdr("srvhdr", t, sol, eol);
2486 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2487 cur_idx = txn->hdr_idx.v[cur_idx].next;
2488 }
2489 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002490
Willy Tarreaubaaee002006-06-26 02:48:02 +02002491
Willy Tarreau66319382007-04-08 17:17:37 +02002492 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002493 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002494 * full. We cannot loop here since stream_sock_read will disable it only if
2495 * rep->l == rlim-data
2496 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002497 if (t->be->srvtimeout)
2498 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002499 else
2500 tv_eternity(&rep->rex);
2501 }
2502
2503
2504 /*
2505 * Now we quickly check if we have found a full valid response.
2506 * If not so, we check the FD and buffer states before leaving.
2507 * A full response is indicated by the fact that we have seen
2508 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2509 * responses are checked first.
2510 *
2511 * Depending on whether the client is still there or not, we
2512 * may send an error response back or not. Note that normally
2513 * we should only check for HTTP status there, and check I/O
2514 * errors somewhere else.
2515 */
2516
2517 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2518
2519 /* Invalid response, or read error or write error */
2520 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2521 (req->flags & BF_WRITE_ERROR) ||
2522 (rep->flags & BF_READ_ERROR))) {
2523 tv_eternity(&rep->rex);
2524 tv_eternity(&req->wex);
2525 fd_delete(t->srv_fd);
2526 if (t->srv) {
2527 t->srv->cur_sess--;
2528 t->srv->failed_resp++;
2529 }
2530 t->be->failed_resp++;
2531 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002532 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002533 client_return(t, error_message(t, HTTP_ERR_502));
2534 if (!(t->flags & SN_ERR_MASK))
2535 t->flags |= SN_ERR_SRVCL;
2536 if (!(t->flags & SN_FINST_MASK))
2537 t->flags |= SN_FINST_H;
2538 /* We used to have a free connection slot. Since we'll never use it,
2539 * we have to inform the server that it may be used by another session.
2540 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002541 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002542 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002543
Willy Tarreaua15645d2007-03-18 16:22:39 +01002544 return 1;
2545 }
2546
2547 /* end of client write or end of server read.
2548 * since we are in header mode, if there's no space left for headers, we
2549 * won't be able to free more later, so the session will never terminate.
2550 */
2551 else if (unlikely(rep->flags & BF_READ_NULL ||
2552 c == CL_STSHUTW || c == CL_STCLOSE ||
2553 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002554 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002555 tv_eternity(&rep->rex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002556 t->srv_state = SV_STSHUTR;
2557 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2558 return 1;
2559 }
2560
2561 /* read timeout : return a 504 to the client.
2562 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002563 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreau8d7d1492007-04-29 10:50:43 +02002564 tv_cmp2_le(&rep->rex, &now))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002565 tv_eternity(&rep->rex);
2566 tv_eternity(&req->wex);
2567 fd_delete(t->srv_fd);
2568 if (t->srv) {
2569 t->srv->cur_sess--;
2570 t->srv->failed_resp++;
2571 }
2572 t->be->failed_resp++;
2573 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002574 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002575 client_return(t, error_message(t, HTTP_ERR_504));
2576 if (!(t->flags & SN_ERR_MASK))
2577 t->flags |= SN_ERR_SRVTO;
2578 if (!(t->flags & SN_FINST_MASK))
2579 t->flags |= SN_FINST_H;
2580 /* We used to have a free connection slot. Since we'll never use it,
2581 * we have to inform the server that it may be used by another session.
2582 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002583 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002584 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002585 return 1;
2586 }
2587
2588 /* last client read and buffer empty */
2589 /* FIXME!!! here, we don't want to switch to SHUTW if the
2590 * client shuts read too early, because we may still have
2591 * some work to do on the headers.
2592 * The side-effect is that if the client completely closes its
2593 * connection during SV_STHEADER, the connection to the server
2594 * is kept until a response comes back or the timeout is reached.
2595 */
2596 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2597 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002598 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002599 tv_eternity(&req->wex);
2600
2601 /* We must ensure that the read part is still
2602 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002603 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002604 if (t->be->srvtimeout)
2605 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002606
2607 shutdown(t->srv_fd, SHUT_WR);
2608 t->srv_state = SV_STSHUTW;
2609 return 1;
2610 }
2611
2612 /* write timeout */
2613 /* FIXME!!! here, we don't want to switch to SHUTW if the
2614 * client shuts read too early, because we may still have
2615 * some work to do on the headers.
2616 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002617 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreau8d7d1492007-04-29 10:50:43 +02002618 tv_cmp2_le(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002619 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002620 tv_eternity(&req->wex);
2621 shutdown(t->srv_fd, SHUT_WR);
2622 /* We must ensure that the read part is still alive
2623 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002624 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002625 if (t->be->srvtimeout)
2626 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002627
2628 t->srv_state = SV_STSHUTW;
2629 if (!(t->flags & SN_ERR_MASK))
2630 t->flags |= SN_ERR_SRVTO;
2631 if (!(t->flags & SN_FINST_MASK))
2632 t->flags |= SN_FINST_H;
2633 return 1;
2634 }
2635
2636 /*
2637 * And now the non-error cases.
2638 */
2639
2640 /* Data remaining in the request buffer.
2641 * This happens during the first pass here, and during
2642 * long posts.
2643 */
2644 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002645 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2646 /* restart writing */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002647 if (t->be->srvtimeout) {
2648 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002649 /* FIXME: to prevent the server from expiring read timeouts during writes,
2650 * we refresh it. */
2651 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002652 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002653 else
2654 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002655 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002656 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002657
Willy Tarreaua15645d2007-03-18 16:22:39 +01002658 /* nothing left in the request buffer */
2659 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002660 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2661 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002662 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002663 }
2664 }
2665
2666 return t->srv_state != SV_STHEADERS;
2667 }
2668
2669
2670 /*****************************************************************
2671 * More interesting part now : we know that we have a complete *
2672 * response which at least looks like HTTP. We have an indicator *
2673 * of each header's length, so we can parse them quickly. *
2674 ****************************************************************/
2675
2676 /*
2677 * 1: get the status code and check for cacheability.
2678 */
2679
2680 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002681 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002682
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002683 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002684 case 200:
2685 case 203:
2686 case 206:
2687 case 300:
2688 case 301:
2689 case 410:
2690 /* RFC2616 @13.4:
2691 * "A response received with a status code of
2692 * 200, 203, 206, 300, 301 or 410 MAY be stored
2693 * by a cache (...) unless a cache-control
2694 * directive prohibits caching."
2695 *
2696 * RFC2616 @9.5: POST method :
2697 * "Responses to this method are not cacheable,
2698 * unless the response includes appropriate
2699 * Cache-Control or Expires header fields."
2700 */
2701 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002702 unlikely(t->be->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002703 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002704 break;
2705 default:
2706 break;
2707 }
2708
2709 /*
2710 * 2: we may need to capture headers
2711 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002712 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002713 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002714 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002715
2716 /*
2717 * 3: we will have to evaluate the filters.
2718 * As opposed to version 1.2, now they will be evaluated in the
2719 * filters order and not in the header order. This means that
2720 * each filter has to be validated among all headers.
2721 *
2722 * Filters are tried with ->be first, then with ->fe if it is
2723 * different from ->be.
2724 */
2725
2726 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2727
2728 cur_proxy = t->be;
2729 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002730 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002731
2732 /* try headers filters */
2733 if (rule_set->rsp_exp != NULL) {
2734 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2735 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002736 if (t->srv) {
2737 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002738 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002739 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002740 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002741 return_srv_prx_502:
2742 tv_eternity(&rep->rex);
2743 tv_eternity(&req->wex);
2744 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002745 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002746 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002747 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002748 if (!(t->flags & SN_ERR_MASK))
2749 t->flags |= SN_ERR_PRXCOND;
2750 if (!(t->flags & SN_FINST_MASK))
2751 t->flags |= SN_FINST_H;
2752 /* We used to have a free connection slot. Since we'll never use it,
2753 * we have to inform the server that it may be used by another session.
2754 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002755 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002756 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002757 return 1;
2758 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002759 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002760
Willy Tarreaua15645d2007-03-18 16:22:39 +01002761 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002762 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002763 if (t->srv) {
2764 t->srv->cur_sess--;
2765 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002766 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002767 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002768 goto return_srv_prx_502;
2769 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002770
Willy Tarreaua15645d2007-03-18 16:22:39 +01002771 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002772 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002773 !(t->flags & SN_CONN_CLOSED)) {
2774 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002775 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002776 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002777
Willy Tarreaua15645d2007-03-18 16:22:39 +01002778 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2779 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002780
Willy Tarreaua15645d2007-03-18 16:22:39 +01002781 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2782 cur_hdr = &txn->hdr_idx.v[cur_idx];
2783 cur_ptr = cur_next;
2784 cur_end = cur_ptr + cur_hdr->len;
2785 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002786
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002787 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2788 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002789 /* 3 possibilities :
2790 * - we have already set Connection: close,
2791 * so we remove this line.
2792 * - we have not yet set Connection: close,
2793 * but this line indicates close. We leave
2794 * it untouched and set the flag.
2795 * - we have not yet set Connection: close,
2796 * and this line indicates non-close. We
2797 * replace it.
2798 */
2799 if (t->flags & SN_CONN_CLOSED) {
2800 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2801 txn->rsp.eoh += delta;
2802 cur_next += delta;
2803 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2804 txn->hdr_idx.used--;
2805 cur_hdr->len = 0;
2806 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002807 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2808 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2809 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002810 cur_next += delta;
2811 cur_hdr->len += delta;
2812 txn->rsp.eoh += delta;
2813 }
2814 t->flags |= SN_CONN_CLOSED;
2815 }
2816 }
2817 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002818 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002819 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002820
Willy Tarreaua15645d2007-03-18 16:22:39 +01002821 /* add response headers from the rule sets in the same order */
2822 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002823 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2824 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002825 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002826 }
2827
Willy Tarreaua15645d2007-03-18 16:22:39 +01002828 /* check whether we're already working on the frontend */
2829 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002830 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002831 cur_proxy = t->fe;
2832 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002833
Willy Tarreaua15645d2007-03-18 16:22:39 +01002834 /*
2835 * 4: check for server cookie.
2836 */
2837 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002838
Willy Tarreaua15645d2007-03-18 16:22:39 +01002839 /*
2840 * 5: add server cookie in the response if needed
2841 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002842 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2843 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002844 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002845
Willy Tarreaua15645d2007-03-18 16:22:39 +01002846 /* the server is known, it's not the one the client requested, we have to
2847 * insert a set-cookie here, except if we want to insert only on POST
2848 * requests and this one isn't. Note that servers which don't have cookies
2849 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002850 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002851 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002852 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01002853 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002854
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002855 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2856 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002857 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002858 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002859
Willy Tarreaua15645d2007-03-18 16:22:39 +01002860 /* Here, we will tell an eventual cache on the client side that we don't
2861 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2862 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2863 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2864 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002865 if (t->be->options & PR_O_COOK_NOC) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002866 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2867 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002868 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002869 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002870 }
2871
2872
2873 /*
2874 * 6: check for cache-control or pragma headers.
2875 */
2876 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002877
Willy Tarreaubaaee002006-06-26 02:48:02 +02002878
Willy Tarreaua15645d2007-03-18 16:22:39 +01002879 /*
2880 * 7: check if result will be cacheable with a cookie.
2881 * We'll block the response if security checks have caught
2882 * nasty things such as a cacheable cookie.
2883 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002884 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2885 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002886 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002887
Willy Tarreaua15645d2007-03-18 16:22:39 +01002888 /* we're in presence of a cacheable response containing
2889 * a set-cookie header. We'll block it as requested by
2890 * the 'checkcache' option, and send an alert.
2891 */
2892 if (t->srv) {
2893 t->srv->cur_sess--;
2894 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002896 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002897
Willy Tarreaua15645d2007-03-18 16:22:39 +01002898 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002899 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002900 send_log(t->be, LOG_ALERT,
2901 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002902 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002903 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002904 }
2905
Willy Tarreaua15645d2007-03-18 16:22:39 +01002906 /*
2907 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002908 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002909 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002910 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002911 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002912 if ((unlikely(msg->sl.st.v_l != 8) ||
2913 unlikely(req->data[msg->som + 7] != '0')) &&
2914 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002915 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002916 goto return_bad_resp;
2917 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002918 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002919
Willy Tarreaubaaee002006-06-26 02:48:02 +02002920
Willy Tarreaua15645d2007-03-18 16:22:39 +01002921 /*************************************************************
2922 * OK, that's finished for the headers. We have done what we *
2923 * could. Let's switch to the DATA state. *
2924 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002925
Willy Tarreaua15645d2007-03-18 16:22:39 +01002926 t->srv_state = SV_STDATA;
2927 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
2928 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
2929
2930 /* client connection already closed or option 'forceclose' required :
2931 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002932 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002933 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002934 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002935 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002936 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002937
2938 /* We must ensure that the read part is still alive when switching
2939 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002940 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002941 if (t->be->srvtimeout)
2942 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002943
Willy Tarreaua15645d2007-03-18 16:22:39 +01002944 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002945 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002946 }
2947
Willy Tarreaua15645d2007-03-18 16:22:39 +01002948#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002949 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002950 /* TCP splicing supported by both FE and BE */
2951 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002952 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002953#endif
2954 /* if the user wants to log as soon as possible, without counting
2955 bytes from the server, then this is the right moment. */
2956 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2957 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002958 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02002959 if (t->fe->to_log & LW_REQ)
2960 http_sess_log(t);
2961 else
2962 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002963 }
2964
Willy Tarreaua15645d2007-03-18 16:22:39 +01002965 /* Note: we must not try to cheat by jumping directly to DATA,
2966 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002967 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002968
2969 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002970 }
2971 else if (s == SV_STDATA) {
2972 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002973 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002974 tv_eternity(&rep->rex);
2975 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002976 fd_delete(t->srv_fd);
2977 if (t->srv) {
2978 t->srv->cur_sess--;
2979 t->srv->failed_resp++;
2980 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002981 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002982 t->srv_state = SV_STCLOSE;
2983 if (!(t->flags & SN_ERR_MASK))
2984 t->flags |= SN_ERR_SRVCL;
2985 if (!(t->flags & SN_FINST_MASK))
2986 t->flags |= SN_FINST_D;
2987 /* We used to have a free connection slot. Since we'll never use it,
2988 * we have to inform the server that it may be used by another session.
2989 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002990 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002991 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002992
2993 return 1;
2994 }
2995 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002996 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002997 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02002998 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002999 t->srv_state = SV_STSHUTR;
3000 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3001 return 1;
3002 }
3003 /* end of client read and no more data to send */
3004 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003005 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003006 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003007 shutdown(t->srv_fd, SHUT_WR);
3008 /* We must ensure that the read part is still alive when switching
3009 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003010 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003011 if (t->be->srvtimeout)
3012 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003013
3014 t->srv_state = SV_STSHUTW;
3015 return 1;
3016 }
3017 /* read timeout */
Willy Tarreau8d7d1492007-04-29 10:50:43 +02003018 else if (tv_cmp2_le(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003019 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003020 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003021 t->srv_state = SV_STSHUTR;
3022 if (!(t->flags & SN_ERR_MASK))
3023 t->flags |= SN_ERR_SRVTO;
3024 if (!(t->flags & SN_FINST_MASK))
3025 t->flags |= SN_FINST_D;
3026 return 1;
3027 }
3028 /* write timeout */
Willy Tarreau8d7d1492007-04-29 10:50:43 +02003029 else if (tv_cmp2_le(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003030 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003031 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032 shutdown(t->srv_fd, SHUT_WR);
3033 /* We must ensure that the read part is still alive when switching
3034 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003035 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003036 if (t->be->srvtimeout)
3037 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003038 t->srv_state = SV_STSHUTW;
3039 if (!(t->flags & SN_ERR_MASK))
3040 t->flags |= SN_ERR_SRVTO;
3041 if (!(t->flags & SN_FINST_MASK))
3042 t->flags |= SN_FINST_D;
3043 return 1;
3044 }
3045
3046 /* recompute request time-outs */
3047 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003048 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3049 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003050 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003051 }
3052 }
3053 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003054 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3055 /* restart writing */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003056 if (t->be->srvtimeout) {
3057 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003058 /* FIXME: to prevent the server from expiring read timeouts during writes,
3059 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003060 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003061 }
3062 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003063 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003064 }
3065 }
3066
3067 /* recompute response time-outs */
3068 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003069 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003070 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003071 }
3072 }
3073 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003074 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003075 if (t->be->srvtimeout)
3076 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003077 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003078 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003079 }
3080 }
3081
3082 return 0; /* other cases change nothing */
3083 }
3084 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003085 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003086 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003087 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003088 fd_delete(t->srv_fd);
3089 if (t->srv) {
3090 t->srv->cur_sess--;
3091 t->srv->failed_resp++;
3092 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003093 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003094 //close(t->srv_fd);
3095 t->srv_state = SV_STCLOSE;
3096 if (!(t->flags & SN_ERR_MASK))
3097 t->flags |= SN_ERR_SRVCL;
3098 if (!(t->flags & SN_FINST_MASK))
3099 t->flags |= SN_FINST_D;
3100 /* We used to have a free connection slot. Since we'll never use it,
3101 * we have to inform the server that it may be used by another session.
3102 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003103 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003104 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003105
3106 return 1;
3107 }
3108 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003109 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003110 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003111 fd_delete(t->srv_fd);
3112 if (t->srv)
3113 t->srv->cur_sess--;
3114 //close(t->srv_fd);
3115 t->srv_state = SV_STCLOSE;
3116 /* We used to have a free connection slot. Since we'll never use it,
3117 * we have to inform the server that it may be used by another session.
3118 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003119 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003120 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003121
3122 return 1;
3123 }
Willy Tarreau8d7d1492007-04-29 10:50:43 +02003124 else if (tv_cmp2_le(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003125 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02003126 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003127 fd_delete(t->srv_fd);
3128 if (t->srv)
3129 t->srv->cur_sess--;
3130 //close(t->srv_fd);
3131 t->srv_state = SV_STCLOSE;
3132 if (!(t->flags & SN_ERR_MASK))
3133 t->flags |= SN_ERR_SRVTO;
3134 if (!(t->flags & SN_FINST_MASK))
3135 t->flags |= SN_FINST_D;
3136 /* We used to have a free connection slot. Since we'll never use it,
3137 * we have to inform the server that it may be used by another session.
3138 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003139 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003140 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003141
3142 return 1;
3143 }
3144 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003145 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3146 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003147 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003148 }
3149 }
3150 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003151 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3152 /* restart writing */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003153 if (t->be->srvtimeout) {
3154 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003155 /* FIXME: to prevent the server from expiring read timeouts during writes,
3156 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003157 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003158 }
3159 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003160 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003161 }
3162 }
3163 return 0;
3164 }
3165 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003166 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003167 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003168 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003169 fd_delete(t->srv_fd);
3170 if (t->srv) {
3171 t->srv->cur_sess--;
3172 t->srv->failed_resp++;
3173 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003174 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003175 //close(t->srv_fd);
3176 t->srv_state = SV_STCLOSE;
3177 if (!(t->flags & SN_ERR_MASK))
3178 t->flags |= SN_ERR_SRVCL;
3179 if (!(t->flags & SN_FINST_MASK))
3180 t->flags |= SN_FINST_D;
3181 /* We used to have a free connection slot. Since we'll never use it,
3182 * we have to inform the server that it may be used by another session.
3183 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003184 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003185 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003186
3187 return 1;
3188 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003189 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003190 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003191 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003192 fd_delete(t->srv_fd);
3193 if (t->srv)
3194 t->srv->cur_sess--;
3195 //close(t->srv_fd);
3196 t->srv_state = SV_STCLOSE;
3197 /* We used to have a free connection slot. Since we'll never use it,
3198 * we have to inform the server that it may be used by another session.
3199 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003200 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003201 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003202
3203 return 1;
3204 }
Willy Tarreau8d7d1492007-04-29 10:50:43 +02003205 else if (tv_cmp2_le(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003206 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaud7971282006-07-29 18:36:34 +02003207 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003208 fd_delete(t->srv_fd);
3209 if (t->srv)
3210 t->srv->cur_sess--;
3211 //close(t->srv_fd);
3212 t->srv_state = SV_STCLOSE;
3213 if (!(t->flags & SN_ERR_MASK))
3214 t->flags |= SN_ERR_SRVTO;
3215 if (!(t->flags & SN_FINST_MASK))
3216 t->flags |= SN_FINST_D;
3217 /* We used to have a free connection slot. Since we'll never use it,
3218 * we have to inform the server that it may be used by another session.
3219 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003220 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003221 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003222
3223 return 1;
3224 }
3225 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003226 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003227 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003228 }
3229 }
3230 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003231 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003232 if (t->be->srvtimeout)
3233 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003234 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003235 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003236 }
3237 }
3238 return 0;
3239 }
3240 else { /* SV_STCLOSE : nothing to do */
3241 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3242 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003243 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003244 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003245 write(1, trash, len);
3246 }
3247 return 0;
3248 }
3249 return 0;
3250}
3251
3252
3253/*
3254 * Produces data for the session <s> depending on its source. Expects to be
3255 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3256 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3257 * session, which it uses to keep on being called when there is free space in
3258 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3259 * if it changes the session state from CL_STSHUTR, otherwise 0.
3260 */
3261int produce_content(struct session *s)
3262{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003263 if (s->data_source == DATA_SRC_NONE) {
3264 s->flags &= ~SN_SELF_GEN;
3265 return 1;
3266 }
3267 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003268 /* dump server statistics */
3269 return produce_content_stats(s);
3270 }
3271 else {
3272 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003273 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003274 client_retnclose(s, error_message(s, HTTP_ERR_500));
3275 if (!(s->flags & SN_ERR_MASK))
3276 s->flags |= SN_ERR_PRXCOND;
3277 if (!(s->flags & SN_FINST_MASK))
3278 s->flags |= SN_FINST_R;
3279 s->flags &= ~SN_SELF_GEN;
3280 return 1;
3281 }
3282}
3283
3284
3285/*
3286 * Produces statistics data for the session <s>. Expects to be called with
3287 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3288 * flag from the session, which it uses to keep on being called when there is
3289 * free space in the buffer, of simply by letting an empty buffer upon return.
3290 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3291 */
3292int produce_content_stats(struct session *s)
3293{
3294 struct buffer *rep = s->rep;
3295 struct proxy *px;
3296 struct chunk msg;
3297 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003298
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003299 msg.len = 0;
3300 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003301
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003302 switch (s->data_state) {
3303 case DATA_ST_INIT:
3304 /* the function had not been called yet */
3305 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003306
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003307 chunk_printf(&msg, sizeof(trash),
3308 "HTTP/1.0 200 OK\r\n"
3309 "Cache-Control: no-cache\r\n"
3310 "Connection: close\r\n"
3311 "Content-Type: text/html\r\n"
3312 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003313
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003314 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003315 client_retnclose(s, &msg); // send the start of the response.
3316 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003317
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003318 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3319 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3320 if (!(s->flags & SN_FINST_MASK))
3321 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003322
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003323 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003324 /* that's all we return in case of HEAD request */
3325 s->data_state = DATA_ST_FIN;
3326 s->flags &= ~SN_SELF_GEN;
3327 return 1;
3328 }
3329
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003330 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3331 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003332
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003333 case DATA_ST_HEAD:
3334 /* WARNING! This must fit in the first buffer !!! */
3335 chunk_printf(&msg, sizeof(trash),
3336 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3337 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3338 "<style type=\"text/css\"><!--\n"
3339 "body {"
3340 " font-family: helvetica, arial;"
3341 " font-size: 12px;"
3342 " font-weight: normal;"
3343 " color: black;"
3344 " background: white;"
3345 "}\n"
3346 "th,td {"
3347 " font-size: 0.8em;"
3348 " align: center;"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003349 "}\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003350 "h1 {"
3351 " font-size: xx-large;"
3352 " margin-bottom: 0.5em;"
3353 "}\n"
3354 "h2 {"
3355 " font-family: helvetica, arial;"
3356 " font-size: x-large;"
3357 " font-weight: bold;"
3358 " font-style: italic;"
3359 " color: #6020a0;"
3360 " margin-top: 0em;"
3361 " margin-bottom: 0em;"
3362 "}\n"
3363 "h3 {"
3364 " font-family: helvetica, arial;"
3365 " font-size: 16px;"
3366 " font-weight: bold;"
3367 " color: #b00040;"
3368 " background: #e8e8d0;"
3369 " margin-top: 0em;"
3370 " margin-bottom: 0em;"
3371 "}\n"
3372 "li {"
3373 " margin-top: 0.25em;"
3374 " margin-right: 2em;"
3375 "}\n"
3376 ".hr {margin-top: 0.25em;"
3377 " border-color: black;"
3378 " border-bottom-style: solid;"
3379 "}\n"
3380 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3381 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3382 ".total {background: #20D0D0;color: #ffff80;}\n"
3383 ".frontend {background: #e8e8d0;}\n"
3384 ".backend {background: #e8e8d0;}\n"
3385 ".active0 {background: #ff9090;}\n"
3386 ".active1 {background: #ffd020;}\n"
3387 ".active2 {background: #ffffa0;}\n"
3388 ".active3 {background: #c0ffc0;}\n"
3389 ".active4 {background: #e0e0e0;}\n"
3390 ".backup0 {background: #ff9090;}\n"
3391 ".backup1 {background: #ff80ff;}\n"
3392 ".backup2 {background: #c060ff;}\n"
3393 ".backup3 {background: #b0d0ff;}\n"
3394 ".backup4 {background: #e0e0e0;}\n"
3395 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003396 "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 +01003397 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3398 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3399 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3400 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3401 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003402 "-->\n"
3403 "</style></head>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003404
3405 if (buffer_write_chunk(rep, &msg) != 0)
3406 return 0;
3407
3408 s->data_state = DATA_ST_INFO;
3409 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003410
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003411 case DATA_ST_INFO:
3412 up = (now.tv_sec - start_date.tv_sec);
3413
3414 /* WARNING! this has to fit the first packet too.
3415 * We are around 3.5 kB, add adding entries will
3416 * become tricky if we want to support 4kB buffers !
3417 */
3418 chunk_printf(&msg, sizeof(trash),
3419 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3420 PRODUCT_NAME "</a></h1>\n"
3421 "<h2>Statistics Report for pid %d</h2>\n"
3422 "<hr width=\"100%%\" class=\"hr\">\n"
3423 "<h3>&gt; General process information</h3>\n"
3424 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3425 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3426 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3427 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3428 "<b>maxsock = </b> %d<br>\n"
3429 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3430 "</td><td align=\"center\" nowrap>\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003431 "<table class=\"lgd\"><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003432 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3433 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003434 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003435 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3436 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003437 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003438 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3439 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003440 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003441 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3442 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3443 "</tr></table>\n"
3444 "</td>"
3445 "<td align=\"left\" nowrap width=\"1%%\">"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003446 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
3447 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3448 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3449 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003450 "</ul>"
3451 "</td>"
3452 "</tr></table>\n"
3453 "",
3454 pid, pid, global.nbproc,
3455 up / 86400, (up % 86400) / 3600,
3456 (up % 3600) / 60, (up % 60),
3457 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3458 global.rlimit_memmax ? " MB" : "",
3459 global.rlimit_nofile,
3460 global.maxsock,
3461 global.maxconn,
3462 actconn
3463 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003464
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003465 if (buffer_write_chunk(rep, &msg) != 0)
3466 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003467
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003468 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003469
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003470 s->data_ctx.stats.px = proxy;
3471 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3472 s->data_state = DATA_ST_LIST;
3473 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003474
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003475 case DATA_ST_LIST:
3476 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003477 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003478 px = s->data_ctx.stats.px;
3479 /* skip the disabled proxies and non-networked ones */
3480 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3481 if (produce_content_stats_proxy(s, px) == 0)
3482 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003483
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003484 s->data_ctx.stats.px = px->next;
3485 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3486 }
3487 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003488
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003489 s->data_state = DATA_ST_END;
3490 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003491
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003492 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003493 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003494 if (buffer_write_chunk(rep, &msg) != 0)
3495 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003496
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003497 s->data_state = DATA_ST_FIN;
3498 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003499
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003500 case DATA_ST_FIN:
3501 s->flags &= ~SN_SELF_GEN;
3502 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003503
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003504 default:
3505 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003506 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003507 client_retnclose(s, error_message(s, HTTP_ERR_500));
3508 if (!(s->flags & SN_ERR_MASK))
3509 s->flags |= SN_ERR_PRXCOND;
3510 if (!(s->flags & SN_FINST_MASK))
3511 s->flags |= SN_FINST_R;
3512 s->flags &= ~SN_SELF_GEN;
3513 return 1;
3514 }
3515}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003516
Willy Tarreaubaaee002006-06-26 02:48:02 +02003517
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003518/*
3519 * Dumps statistics for a proxy.
3520 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3521 * ot non-zero if everything completed.
3522 */
3523int produce_content_stats_proxy(struct session *s, struct proxy *px)
3524{
3525 struct buffer *rep = s->rep;
3526 struct server *sv;
3527 struct chunk msg;
3528
3529 msg.len = 0;
3530 msg.str = trash;
3531
3532 switch (s->data_ctx.stats.px_st) {
3533 case DATA_ST_PX_INIT:
3534 /* we are on a new proxy */
3535
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003536 if (s->be->uri_auth && s->be->uri_auth->scope) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003537 /* we have a limited scope, we have to check the proxy name */
3538 struct stat_scope *scope;
3539 int len;
3540
3541 len = strlen(px->id);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003542 scope = s->be->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003543
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003544 while (scope) {
3545 /* match exact proxy name */
3546 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3547 break;
3548
3549 /* match '.' which means 'self' proxy */
3550 if (!strcmp(scope->px_id, ".") && px == s->fe)
3551 break;
3552 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003553 }
3554
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003555 /* proxy name not found : don't dump anything */
3556 if (scope == NULL)
3557 return 1;
3558 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003559
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003560 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3561 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003562
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003563 case DATA_ST_PX_TH:
3564 /* print a new table */
3565 chunk_printf(&msg, sizeof(trash),
3566 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3567 "<tr align=\"center\" class=\"titre\">"
3568 "<th colspan=2 class=\"pxname\">%s</th>"
3569 "<th colspan=18 class=\"empty\"></th>"
3570 "</tr>\n"
3571 "<tr align=\"center\" class=\"titre\">"
3572 "<th rowspan=2></th>"
3573 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3574 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3575 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3576 "</tr>\n"
3577 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003578 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3579 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003580 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3581 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3582 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3583 "",
3584 px->id);
3585
3586 if (buffer_write_chunk(rep, &msg) != 0)
3587 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003588
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003589 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3590 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003591
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003592 case DATA_ST_PX_FE:
3593 /* print the frontend */
3594 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003595 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003596 /* name, queue */
3597 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3598 /* sessions : current, max, limit, cumul. */
3599 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3600 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003601 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003602 /* denied: req, resp */
3603 "<td align=right>%d</td><td align=right>%d</td>"
3604 /* errors : request, connect, response */
3605 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3606 /* server status : reflect backend status */
3607 "<td align=center>%s</td>"
3608 /* rest of server: nothing */
3609 "<td align=center colspan=5></td></tr>"
3610 "",
3611 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003612 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003613 px->denied_req, px->denied_resp,
3614 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003615 px->state == PR_STRUN ? "OPEN" :
3616 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003617
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003618 if (buffer_write_chunk(rep, &msg) != 0)
3619 return 0;
3620 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003621
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003622 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3623 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3624 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003625
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003626 case DATA_ST_PX_SV:
3627 /* stats.sv has been initialized above */
3628 while (s->data_ctx.stats.sv != NULL) {
3629 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3630 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003631
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003632 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003633
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003634 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3635 if (!(sv->state & SRV_CHECKED))
3636 sv_state = 4;
3637 else if (sv->state & SRV_RUNNING)
3638 if (sv->health == sv->rise + sv->fall - 1)
3639 sv_state = 3; /* UP */
3640 else
3641 sv_state = 2; /* going down */
3642 else
3643 if (sv->health)
3644 sv_state = 1; /* going up */
3645 else
3646 sv_state = 0; /* DOWN */
3647
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003648 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003649 /* name */
3650 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3651 /* queue : current, max */
3652 "<td align=right>%d</td><td align=right>%d</td>"
3653 /* sessions : current, max, limit, cumul */
3654 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3655 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003656 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003657 /* denied: req, resp */
3658 "<td align=right></td><td align=right>%d</td>"
3659 /* errors : request, connect, response */
3660 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3661 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003662 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003663 sv_state, sv->id,
3664 sv->nbpend, sv->nbpend_max,
3665 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003666 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003667 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003668 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003669
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003670 /* status */
3671 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3672 chunk_printf(&msg, sizeof(trash),
3673 srv_hlt_st[sv_state],
3674 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3675 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3676
Willy Tarreau128e9542007-01-01 22:01:43 +01003677 chunk_printf(&msg, sizeof(trash),
3678 /* weight */
3679 "</td><td>%d</td>"
3680 /* act, bck */
3681 "<td>%s</td><td>%s</td>"
3682 "",
Willy Tarreau417fae02007-03-25 21:16:40 +02003683 sv->uweight,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003684 (sv->state & SRV_BACKUP) ? "-" : "Y",
3685 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003686
3687 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003688 if (sv->state & SRV_CHECKED)
3689 chunk_printf(&msg, sizeof(trash),
3690 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3691 sv->failed_checks, sv->down_trans);
3692 else
3693 chunk_printf(&msg, sizeof(trash),
3694 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003695
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003696 if (buffer_write_chunk(rep, &msg) != 0)
3697 return 0;
3698
3699 s->data_ctx.stats.sv = sv->next;
3700 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003701
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003702 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3703 /* fall through */
3704
3705 case DATA_ST_PX_BE:
3706 /* print the backend */
3707 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003708 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003709 /* name */
3710 "<tr align=center class=\"backend\"><td>Backend</td>"
3711 /* queue : current, max */
3712 "<td align=right>%d</td><td align=right>%d</td>"
3713 /* sessions : current, max, limit, cumul. */
3714 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3715 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003716 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003717 /* denied: req, resp */
3718 "<td align=right>%d</td><td align=right>%d</td>"
3719 /* errors : request, connect, response */
3720 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3721 /* server status : reflect backend status (up/down) : we display UP
3722 * if the backend has known working servers or if it has no server at
3723 * all (eg: for stats). Tthen we display the total weight, number of
3724 * active and backups. */
3725 "<td align=center>%s</td><td align=center>%d</td>"
3726 "<td align=center>%d</td><td align=center>%d</td>"
3727 /* rest of server: nothing */
3728 "<td align=center colspan=2></td></tr>"
3729 "",
3730 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3731 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003732 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003733 px->denied_req, px->denied_resp,
3734 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003735 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3736 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003737
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003738 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003739 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003740 }
3741
3742 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3743 /* fall through */
3744
3745 case DATA_ST_PX_END:
3746 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3747
3748 if (buffer_write_chunk(rep, &msg) != 0)
3749 return 0;
3750
3751 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3752 /* fall through */
3753
3754 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003755 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003756
3757 default:
3758 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003759 return 1;
3760 }
3761}
3762
3763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003764/* Iterate the same filter through all request headers.
3765 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003766 * Since it can manage the switch to another backend, it updates the per-proxy
3767 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003768 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003769int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003770{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003771 char term;
3772 char *cur_ptr, *cur_end, *cur_next;
3773 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003774 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003775 struct hdr_idx_elem *cur_hdr;
3776 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003777
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003778 last_hdr = 0;
3779
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003780 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003781 old_idx = 0;
3782
3783 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003784 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003785 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003786 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003787 (exp->action == ACT_ALLOW ||
3788 exp->action == ACT_DENY ||
3789 exp->action == ACT_TARPIT))
3790 return 0;
3791
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003792 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003793 if (!cur_idx)
3794 break;
3795
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003796 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003797 cur_ptr = cur_next;
3798 cur_end = cur_ptr + cur_hdr->len;
3799 cur_next = cur_end + cur_hdr->cr + 1;
3800
3801 /* Now we have one header between cur_ptr and cur_end,
3802 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003803 */
3804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003805 /* The annoying part is that pattern matching needs
3806 * that we modify the contents to null-terminate all
3807 * strings before testing them.
3808 */
3809
3810 term = *cur_end;
3811 *cur_end = '\0';
3812
3813 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3814 switch (exp->action) {
3815 case ACT_SETBE:
3816 /* It is not possible to jump a second time.
3817 * FIXME: should we return an HTTP/500 here so that
3818 * the admin knows there's a problem ?
3819 */
3820 if (t->be != t->fe)
3821 break;
3822
3823 /* Swithing Proxy */
3824 t->be = (struct proxy *) exp->replace;
3825
3826 /* right now, the backend switch is not too much complicated
3827 * because we have associated req_cap and rsp_cap to the
3828 * frontend, and the beconn will be updated later.
3829 */
3830
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003831 t->rep->rto = t->req->wto = t->be->srvtimeout;
3832 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003833 last_hdr = 1;
3834 break;
3835
3836 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003837 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003838 last_hdr = 1;
3839 break;
3840
3841 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003842 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003843 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003844 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003845 break;
3846
3847 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003848 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003849 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003850 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003851 break;
3852
3853 case ACT_REPLACE:
3854 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3855 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3856 /* FIXME: if the user adds a newline in the replacement, the
3857 * index will not be recalculated for now, and the new line
3858 * will not be counted as a new header.
3859 */
3860
3861 cur_end += delta;
3862 cur_next += delta;
3863 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003864 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003865 break;
3866
3867 case ACT_REMOVE:
3868 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3869 cur_next += delta;
3870
3871 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003872 txn->req.eoh += delta;
3873 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3874 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003875 cur_hdr->len = 0;
3876 cur_end = NULL; /* null-term has been rewritten */
3877 break;
3878
3879 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003880 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003881 if (cur_end)
3882 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003883
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003884 /* keep the link from this header to next one in case of later
3885 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003886 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003887 old_idx = cur_idx;
3888 }
3889 return 0;
3890}
3891
3892
3893/* Apply the filter to the request line.
3894 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3895 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003896 * Since it can manage the switch to another backend, it updates the per-proxy
3897 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003898 */
3899int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3900{
3901 char term;
3902 char *cur_ptr, *cur_end;
3903 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003904 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003905 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003906
Willy Tarreau58f10d72006-12-04 02:26:12 +01003907
Willy Tarreau3d300592007-03-18 18:34:41 +01003908 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003909 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003910 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003911 (exp->action == ACT_ALLOW ||
3912 exp->action == ACT_DENY ||
3913 exp->action == ACT_TARPIT))
3914 return 0;
3915 else if (exp->action == ACT_REMOVE)
3916 return 0;
3917
3918 done = 0;
3919
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003920 cur_ptr = req->data + txn->req.som;
3921 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003922
3923 /* Now we have the request line between cur_ptr and cur_end */
3924
3925 /* The annoying part is that pattern matching needs
3926 * that we modify the contents to null-terminate all
3927 * strings before testing them.
3928 */
3929
3930 term = *cur_end;
3931 *cur_end = '\0';
3932
3933 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3934 switch (exp->action) {
3935 case ACT_SETBE:
3936 /* It is not possible to jump a second time.
3937 * FIXME: should we return an HTTP/500 here so that
3938 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003939 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003940 if (t->be != t->fe)
3941 break;
3942
3943 /* Swithing Proxy */
3944 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003945
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003946 /* right now, the backend switch is not too much complicated
3947 * because we have associated req_cap and rsp_cap to the
3948 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003949 */
3950
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003951 t->rep->rto = t->req->wto = t->be->srvtimeout;
3952 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003953 done = 1;
3954 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003955
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003956 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003957 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003958 done = 1;
3959 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003960
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003961 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003962 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003963 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003964 done = 1;
3965 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003966
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003967 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003968 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003969 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003970 done = 1;
3971 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003972
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003973 case ACT_REPLACE:
3974 *cur_end = term; /* restore the string terminator */
3975 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3976 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3977 /* FIXME: if the user adds a newline in the replacement, the
3978 * index will not be recalculated for now, and the new line
3979 * will not be counted as a new header.
3980 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003981
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003982 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003983 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003984
Willy Tarreaua9bd1982007-04-03 20:03:18 +02003985 txn->req.sol = req->data + txn->req.som;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003986 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003987 HTTP_MSG_RQMETH,
3988 cur_ptr, cur_end + 1,
3989 NULL, NULL);
3990 if (unlikely(!cur_end))
3991 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003992
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003993 /* we have a full request and we know that we have either a CR
3994 * or an LF at <ptr>.
3995 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003996 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3997 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003998 /* there is no point trying this regex on headers */
3999 return 1;
4000 }
4001 }
4002 *cur_end = term; /* restore the string terminator */
4003 return done;
4004}
Willy Tarreau97de6242006-12-27 17:18:38 +01004005
Willy Tarreau58f10d72006-12-04 02:26:12 +01004006
Willy Tarreau58f10d72006-12-04 02:26:12 +01004007
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004008/*
4009 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4010 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004011 * unparsable request. Since it can manage the switch to another backend, it
4012 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004013 */
4014int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4015{
Willy Tarreau3d300592007-03-18 18:34:41 +01004016 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004017 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004018 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004019 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004020
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004021 /*
4022 * The interleaving of transformations and verdicts
4023 * makes it difficult to decide to continue or stop
4024 * the evaluation.
4025 */
4026
Willy Tarreau3d300592007-03-18 18:34:41 +01004027 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004028 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4029 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4030 exp = exp->next;
4031 continue;
4032 }
4033
4034 /* Apply the filter to the request line. */
4035 ret = apply_filter_to_req_line(t, req, exp);
4036 if (unlikely(ret < 0))
4037 return -1;
4038
4039 if (likely(ret == 0)) {
4040 /* The filter did not match the request, it can be
4041 * iterated through all headers.
4042 */
4043 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004044 }
4045 exp = exp->next;
4046 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004047 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004048}
4049
4050
Willy Tarreaua15645d2007-03-18 16:22:39 +01004051
Willy Tarreau58f10d72006-12-04 02:26:12 +01004052/*
4053 * Manager client-side cookie
4054 */
4055void manage_client_side_cookies(struct session *t, struct buffer *req)
4056{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004057 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004058 char *p1, *p2, *p3, *p4;
4059 char *del_colon, *del_cookie, *colon;
4060 int app_cookies;
4061
4062 appsess *asession_temp = NULL;
4063 appsess local_asession;
4064
4065 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004066 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004067
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004068 if (t->be->cookie_name == NULL &&
4069 t->be->appsession_name == NULL &&
4070 t->be->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004071 return;
4072
Willy Tarreau2a324282006-12-05 00:05:46 +01004073 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004074 * we start with the start line.
4075 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004076 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004077 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004078
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004079 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004080 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004081 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004082
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004083 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004084 cur_ptr = cur_next;
4085 cur_end = cur_ptr + cur_hdr->len;
4086 cur_next = cur_end + cur_hdr->cr + 1;
4087
4088 /* We have one full header between cur_ptr and cur_end, and the
4089 * next header starts at cur_next. We're only interested in
4090 * "Cookie:" headers.
4091 */
4092
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004093 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4094 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004095 old_idx = cur_idx;
4096 continue;
4097 }
4098
4099 /* Now look for cookies. Conforming to RFC2109, we have to support
4100 * attributes whose name begin with a '$', and associate them with
4101 * the right cookie, if we want to delete this cookie.
4102 * So there are 3 cases for each cookie read :
4103 * 1) it's a special attribute, beginning with a '$' : ignore it.
4104 * 2) it's a server id cookie that we *MAY* want to delete : save
4105 * some pointers on it (last semi-colon, beginning of cookie...)
4106 * 3) it's an application cookie : we *MAY* have to delete a previous
4107 * "special" cookie.
4108 * At the end of loop, if a "special" cookie remains, we may have to
4109 * remove it. If no application cookie persists in the header, we
4110 * *MUST* delete it
4111 */
4112
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004113 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004114
Willy Tarreau58f10d72006-12-04 02:26:12 +01004115 /* del_cookie == NULL => nothing to be deleted */
4116 del_colon = del_cookie = NULL;
4117 app_cookies = 0;
4118
4119 while (p1 < cur_end) {
4120 /* skip spaces and colons, but keep an eye on these ones */
4121 while (p1 < cur_end) {
4122 if (*p1 == ';' || *p1 == ',')
4123 colon = p1;
4124 else if (!isspace((int)*p1))
4125 break;
4126 p1++;
4127 }
4128
4129 if (p1 == cur_end)
4130 break;
4131
4132 /* p1 is at the beginning of the cookie name */
4133 p2 = p1;
4134 while (p2 < cur_end && *p2 != '=')
4135 p2++;
4136
4137 if (p2 == cur_end)
4138 break;
4139
4140 p3 = p2 + 1; /* skips the '=' sign */
4141 if (p3 == cur_end)
4142 break;
4143
4144 p4 = p3;
4145 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
4146 p4++;
4147
4148 /* here, we have the cookie name between p1 and p2,
4149 * and its value between p3 and p4.
4150 * we can process it :
4151 *
4152 * Cookie: NAME=VALUE;
4153 * | || || |
4154 * | || || +--> p4
4155 * | || |+-------> p3
4156 * | || +--------> p2
4157 * | |+------------> p1
4158 * | +-------------> colon
4159 * +--------------------> cur_ptr
4160 */
4161
4162 if (*p1 == '$') {
4163 /* skip this one */
4164 }
4165 else {
4166 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004167 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004168 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004169 (p4 - p1 >= t->fe->capture_namelen) &&
4170 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004171 int log_len = p4 - p1;
4172
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004173 if ((txn->cli_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004174 Alert("HTTP logging : out of memory.\n");
4175 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004176 if (log_len > t->fe->capture_len)
4177 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004178 memcpy(txn->cli_cookie, p1, log_len);
4179 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004180 }
4181 }
4182
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004183 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4184 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004185 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004186 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004187 char *delim;
4188
4189 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4190 * have the server ID betweek p3 and delim, and the original cookie between
4191 * delim+1 and p4. Otherwise, delim==p4 :
4192 *
4193 * Cookie: NAME=SRV~VALUE;
4194 * | || || | |
4195 * | || || | +--> p4
4196 * | || || +--------> delim
4197 * | || |+-----------> p3
4198 * | || +------------> p2
4199 * | |+----------------> p1
4200 * | +-----------------> colon
4201 * +------------------------> cur_ptr
4202 */
4203
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004204 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004205 for (delim = p3; delim < p4; delim++)
4206 if (*delim == COOKIE_DELIM)
4207 break;
4208 }
4209 else
4210 delim = p4;
4211
4212
4213 /* Here, we'll look for the first running server which supports the cookie.
4214 * This allows to share a same cookie between several servers, for example
4215 * to dedicate backup servers to specific servers only.
4216 * However, to prevent clients from sticking to cookie-less backup server
4217 * when they have incidentely learned an empty cookie, we simply ignore
4218 * empty cookies and mark them as invalid.
4219 */
4220 if (delim == p3)
4221 srv = NULL;
4222
4223 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004224 if (srv->cookie && (srv->cklen == delim - p3) &&
4225 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004226 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004227 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004228 txn->flags &= ~TX_CK_MASK;
4229 txn->flags |= TX_CK_VALID;
4230 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004231 t->srv = srv;
4232 break;
4233 } else {
4234 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004235 txn->flags &= ~TX_CK_MASK;
4236 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004237 }
4238 }
4239 srv = srv->next;
4240 }
4241
Willy Tarreau3d300592007-03-18 18:34:41 +01004242 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004243 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004244 txn->flags &= ~TX_CK_MASK;
4245 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004246 }
4247
4248 /* depending on the cookie mode, we may have to either :
4249 * - delete the complete cookie if we're in insert+indirect mode, so that
4250 * the server never sees it ;
4251 * - remove the server id from the cookie value, and tag the cookie as an
4252 * application cookie so that it does not get accidentely removed later,
4253 * if we're in cookie prefix mode
4254 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004255 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004256 int delta; /* negative */
4257
4258 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4259 p4 += delta;
4260 cur_end += delta;
4261 cur_next += delta;
4262 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004263 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004264
4265 del_cookie = del_colon = NULL;
4266 app_cookies++; /* protect the header from deletion */
4267 }
4268 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004269 (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 +01004270 del_cookie = p1;
4271 del_colon = colon;
4272 }
4273 } else {
4274 /* now we know that we must keep this cookie since it's
4275 * not ours. But if we wanted to delete our cookie
4276 * earlier, we cannot remove the complete header, but we
4277 * can remove the previous block itself.
4278 */
4279 app_cookies++;
4280
4281 if (del_cookie != NULL) {
4282 int delta; /* negative */
4283
4284 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4285 p4 += delta;
4286 cur_end += delta;
4287 cur_next += delta;
4288 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004289 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004290 del_cookie = del_colon = NULL;
4291 }
4292 }
4293
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004294 if ((t->be->appsession_name != NULL) &&
4295 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004296 /* first, let's see if the cookie is our appcookie*/
4297
4298 /* Cool... it's the right one */
4299
4300 asession_temp = &local_asession;
4301
4302 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4303 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4304 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4305 return;
4306 }
4307
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004308 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4309 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004310 asession_temp->serverid = NULL;
4311
4312 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004313 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004314 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4315 /* free previously allocated memory */
4316 pool_free_to(apools.sessid, local_asession.sessid);
4317 Alert("Not enough memory process_cli():asession:calloc().\n");
4318 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4319 return;
4320 }
4321
4322 asession_temp->sessid = local_asession.sessid;
4323 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004324 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004325 } else {
4326 /* free previously allocated memory */
4327 pool_free_to(apools.sessid, local_asession.sessid);
4328 }
4329
4330 if (asession_temp->serverid == NULL) {
4331 Alert("Found Application Session without matching server.\n");
4332 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004333 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004334 while (srv) {
4335 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004336 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004337 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004338 txn->flags &= ~TX_CK_MASK;
4339 txn->flags |= TX_CK_VALID;
4340 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004341 t->srv = srv;
4342 break;
4343 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004344 txn->flags &= ~TX_CK_MASK;
4345 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004346 }
4347 }
4348 srv = srv->next;
4349 }/* end while(srv) */
4350 }/* end else if server == NULL */
4351
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004352 tv_delayfrom(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004353 }/* end if ((t->proxy->appsession_name != NULL) ... */
4354 }
4355
4356 /* we'll have to look for another cookie ... */
4357 p1 = p4;
4358 } /* while (p1 < cur_end) */
4359
4360 /* There's no more cookie on this line.
4361 * We may have marked the last one(s) for deletion.
4362 * We must do this now in two ways :
4363 * - if there is no app cookie, we simply delete the header ;
4364 * - if there are app cookies, we must delete the end of the
4365 * string properly, including the colon/semi-colon before
4366 * the cookie name.
4367 */
4368 if (del_cookie != NULL) {
4369 int delta;
4370 if (app_cookies) {
4371 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4372 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004373 cur_hdr->len += delta;
4374 } else {
4375 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004376
4377 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004378 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4379 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004380 cur_hdr->len = 0;
4381 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004382 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004383 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004384 }
4385
4386 /* keep the link from this header to next one */
4387 old_idx = cur_idx;
4388 } /* end of cookie processing on this header */
4389}
4390
4391
Willy Tarreaua15645d2007-03-18 16:22:39 +01004392/* Iterate the same filter through all response headers contained in <rtr>.
4393 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4394 */
4395int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4396{
4397 char term;
4398 char *cur_ptr, *cur_end, *cur_next;
4399 int cur_idx, old_idx, last_hdr;
4400 struct http_txn *txn = &t->txn;
4401 struct hdr_idx_elem *cur_hdr;
4402 int len, delta;
4403
4404 last_hdr = 0;
4405
4406 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4407 old_idx = 0;
4408
4409 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004410 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004411 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004412 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004413 (exp->action == ACT_ALLOW ||
4414 exp->action == ACT_DENY))
4415 return 0;
4416
4417 cur_idx = txn->hdr_idx.v[old_idx].next;
4418 if (!cur_idx)
4419 break;
4420
4421 cur_hdr = &txn->hdr_idx.v[cur_idx];
4422 cur_ptr = cur_next;
4423 cur_end = cur_ptr + cur_hdr->len;
4424 cur_next = cur_end + cur_hdr->cr + 1;
4425
4426 /* Now we have one header between cur_ptr and cur_end,
4427 * and the next header starts at cur_next.
4428 */
4429
4430 /* The annoying part is that pattern matching needs
4431 * that we modify the contents to null-terminate all
4432 * strings before testing them.
4433 */
4434
4435 term = *cur_end;
4436 *cur_end = '\0';
4437
4438 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4439 switch (exp->action) {
4440 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004441 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004442 last_hdr = 1;
4443 break;
4444
4445 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004446 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004447 last_hdr = 1;
4448 break;
4449
4450 case ACT_REPLACE:
4451 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4452 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4453 /* FIXME: if the user adds a newline in the replacement, the
4454 * index will not be recalculated for now, and the new line
4455 * will not be counted as a new header.
4456 */
4457
4458 cur_end += delta;
4459 cur_next += delta;
4460 cur_hdr->len += delta;
4461 txn->rsp.eoh += delta;
4462 break;
4463
4464 case ACT_REMOVE:
4465 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4466 cur_next += delta;
4467
4468 /* FIXME: this should be a separate function */
4469 txn->rsp.eoh += delta;
4470 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4471 txn->hdr_idx.used--;
4472 cur_hdr->len = 0;
4473 cur_end = NULL; /* null-term has been rewritten */
4474 break;
4475
4476 }
4477 }
4478 if (cur_end)
4479 *cur_end = term; /* restore the string terminator */
4480
4481 /* keep the link from this header to next one in case of later
4482 * removal of next header.
4483 */
4484 old_idx = cur_idx;
4485 }
4486 return 0;
4487}
4488
4489
4490/* Apply the filter to the status line in the response buffer <rtr>.
4491 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4492 * or -1 if a replacement resulted in an invalid status line.
4493 */
4494int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4495{
4496 char term;
4497 char *cur_ptr, *cur_end;
4498 int done;
4499 struct http_txn *txn = &t->txn;
4500 int len, delta;
4501
4502
Willy Tarreau3d300592007-03-18 18:34:41 +01004503 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004504 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004505 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004506 (exp->action == ACT_ALLOW ||
4507 exp->action == ACT_DENY))
4508 return 0;
4509 else if (exp->action == ACT_REMOVE)
4510 return 0;
4511
4512 done = 0;
4513
4514 cur_ptr = rtr->data + txn->rsp.som;
4515 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4516
4517 /* Now we have the status line between cur_ptr and cur_end */
4518
4519 /* The annoying part is that pattern matching needs
4520 * that we modify the contents to null-terminate all
4521 * strings before testing them.
4522 */
4523
4524 term = *cur_end;
4525 *cur_end = '\0';
4526
4527 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4528 switch (exp->action) {
4529 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004530 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004531 done = 1;
4532 break;
4533
4534 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004535 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004536 done = 1;
4537 break;
4538
4539 case ACT_REPLACE:
4540 *cur_end = term; /* restore the string terminator */
4541 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4542 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4543 /* FIXME: if the user adds a newline in the replacement, the
4544 * index will not be recalculated for now, and the new line
4545 * will not be counted as a new header.
4546 */
4547
4548 txn->rsp.eoh += delta;
4549 cur_end += delta;
4550
Willy Tarreaua9bd1982007-04-03 20:03:18 +02004551 txn->rsp.sol = rtr->data + txn->rsp.som;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004552 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004553 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004554 cur_ptr, cur_end + 1,
4555 NULL, NULL);
4556 if (unlikely(!cur_end))
4557 return -1;
4558
4559 /* we have a full respnse and we know that we have either a CR
4560 * or an LF at <ptr>.
4561 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004562 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004563 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4564 /* there is no point trying this regex on headers */
4565 return 1;
4566 }
4567 }
4568 *cur_end = term; /* restore the string terminator */
4569 return done;
4570}
4571
4572
4573
4574/*
4575 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4576 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4577 * unparsable response.
4578 */
4579int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4580{
Willy Tarreau3d300592007-03-18 18:34:41 +01004581 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004582 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004583 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004584 int ret;
4585
4586 /*
4587 * The interleaving of transformations and verdicts
4588 * makes it difficult to decide to continue or stop
4589 * the evaluation.
4590 */
4591
Willy Tarreau3d300592007-03-18 18:34:41 +01004592 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004593 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4594 exp->action == ACT_PASS)) {
4595 exp = exp->next;
4596 continue;
4597 }
4598
4599 /* Apply the filter to the status line. */
4600 ret = apply_filter_to_sts_line(t, rtr, exp);
4601 if (unlikely(ret < 0))
4602 return -1;
4603
4604 if (likely(ret == 0)) {
4605 /* The filter did not match the response, it can be
4606 * iterated through all headers.
4607 */
4608 apply_filter_to_resp_headers(t, rtr, exp);
4609 }
4610 exp = exp->next;
4611 }
4612 return 0;
4613}
4614
4615
4616
4617/*
4618 * Manager server-side cookies
4619 */
4620void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4621{
4622 struct http_txn *txn = &t->txn;
4623 char *p1, *p2, *p3, *p4;
4624
4625 appsess *asession_temp = NULL;
4626 appsess local_asession;
4627
4628 char *cur_ptr, *cur_end, *cur_next;
4629 int cur_idx, old_idx, delta;
4630
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004631 if (t->be->cookie_name == NULL &&
4632 t->be->appsession_name == NULL &&
4633 t->be->capture_name == NULL &&
4634 !(t->be->options & PR_O_CHK_CACHE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004635 return;
4636
4637 /* Iterate through the headers.
4638 * we start with the start line.
4639 */
4640 old_idx = 0;
4641 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4642
4643 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4644 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004645 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004646
4647 cur_hdr = &txn->hdr_idx.v[cur_idx];
4648 cur_ptr = cur_next;
4649 cur_end = cur_ptr + cur_hdr->len;
4650 cur_next = cur_end + cur_hdr->cr + 1;
4651
4652 /* We have one full header between cur_ptr and cur_end, and the
4653 * next header starts at cur_next. We're only interested in
4654 * "Cookie:" headers.
4655 */
4656
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004657 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4658 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004659 old_idx = cur_idx;
4660 continue;
4661 }
4662
4663 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004664 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004665
4666
4667 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004668 if (t->be->cookie_name == NULL &&
4669 t->be->appsession_name == NULL &&
4670 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004671 return;
4672
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004673 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004674
4675 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004676 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4677 break;
4678
4679 /* p1 is at the beginning of the cookie name */
4680 p2 = p1;
4681
4682 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4683 p2++;
4684
4685 if (p2 == cur_end || *p2 == ';') /* next cookie */
4686 break;
4687
4688 p3 = p2 + 1; /* skip the '=' sign */
4689 if (p3 == cur_end)
4690 break;
4691
4692 p4 = p3;
4693 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4694 p4++;
4695
4696 /* here, we have the cookie name between p1 and p2,
4697 * and its value between p3 and p4.
4698 * we can process it.
4699 */
4700
4701 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004702 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004703 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004704 (p4 - p1 >= t->be->capture_namelen) &&
4705 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004706 int log_len = p4 - p1;
4707
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004708 if ((txn->srv_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004709 Alert("HTTP logging : out of memory.\n");
4710 }
4711
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004712 if (log_len > t->be->capture_len)
4713 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004714 memcpy(txn->srv_cookie, p1, log_len);
4715 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004716 }
4717
4718 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004719 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4720 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004721 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004722 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004723
4724 /* If the cookie is in insert mode on a known server, we'll delete
4725 * this occurrence because we'll insert another one later.
4726 * We'll delete it too if the "indirect" option is set and we're in
4727 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004728 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4729 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004730 /* this header must be deleted */
4731 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4732 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4733 txn->hdr_idx.used--;
4734 cur_hdr->len = 0;
4735 cur_next += delta;
4736 txn->rsp.eoh += delta;
4737
Willy Tarreau3d300592007-03-18 18:34:41 +01004738 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004739 }
4740 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004741 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004742 /* replace bytes p3->p4 with the cookie name associated
4743 * with this server since we know it.
4744 */
4745 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4746 cur_hdr->len += delta;
4747 cur_next += delta;
4748 txn->rsp.eoh += delta;
4749
Willy Tarreau3d300592007-03-18 18:34:41 +01004750 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004751 }
4752 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004753 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004754 /* insert the cookie name associated with this server
4755 * before existing cookie, and insert a delimitor between them..
4756 */
4757 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4758 cur_hdr->len += delta;
4759 cur_next += delta;
4760 txn->rsp.eoh += delta;
4761
4762 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004763 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004764 }
4765 }
4766 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004767 else if ((t->be->appsession_name != NULL) &&
4768 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004769
4770 /* Cool... it's the right one */
4771
4772 size_t server_id_len = strlen(t->srv->id) + 1;
4773 asession_temp = &local_asession;
4774
4775 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4776 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4777 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4778 return;
4779 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004780 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4781 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004782 asession_temp->serverid = NULL;
4783
4784 /* only do insert, if lookup fails */
4785 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
4786 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4787 Alert("Not enough Memory process_srv():asession:calloc().\n");
4788 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4789 return;
4790 }
4791 asession_temp->sessid = local_asession.sessid;
4792 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004793 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004794 }/* end if (chtbl_lookup()) */
4795 else {
4796 /* free wasted memory */
4797 pool_free_to(apools.sessid, local_asession.sessid);
4798 } /* end else from if (chtbl_lookup()) */
4799
4800 if (asession_temp->serverid == NULL) {
4801 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
4802 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4803 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4804 return;
4805 }
4806 asession_temp->serverid[0] = '\0';
4807 }
4808
4809 if (asession_temp->serverid[0] == '\0')
4810 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4811
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004812 tv_delayfrom(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004813
4814#if defined(DEBUG_HASH)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004815 print_table(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004816#endif
4817 }/* end if ((t->proxy->appsession_name != NULL) ... */
4818 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4819 } /* we're now at the end of the cookie value */
4820
4821 /* keep the link from this header to next one */
4822 old_idx = cur_idx;
4823 } /* end of cookie processing on this header */
4824}
4825
4826
4827
4828/*
4829 * Check if response is cacheable or not. Updates t->flags.
4830 */
4831void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4832{
4833 struct http_txn *txn = &t->txn;
4834 char *p1, *p2;
4835
4836 char *cur_ptr, *cur_end, *cur_next;
4837 int cur_idx;
4838
Willy Tarreau3d300592007-03-18 18:34:41 +01004839 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004840 return;
4841
4842 /* Iterate through the headers.
4843 * we start with the start line.
4844 */
4845 cur_idx = 0;
4846 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4847
4848 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4849 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004850 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004851
4852 cur_hdr = &txn->hdr_idx.v[cur_idx];
4853 cur_ptr = cur_next;
4854 cur_end = cur_ptr + cur_hdr->len;
4855 cur_next = cur_end + cur_hdr->cr + 1;
4856
4857 /* We have one full header between cur_ptr and cur_end, and the
4858 * next header starts at cur_next. We're only interested in
4859 * "Cookie:" headers.
4860 */
4861
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004862 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4863 if (val) {
4864 if ((cur_end - (cur_ptr + val) >= 8) &&
4865 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4866 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4867 return;
4868 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004869 }
4870
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004871 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4872 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004873 continue;
4874
4875 /* OK, right now we know we have a cache-control header at cur_ptr */
4876
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004877 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004878
4879 if (p1 >= cur_end) /* no more info */
4880 continue;
4881
4882 /* p1 is at the beginning of the value */
4883 p2 = p1;
4884
4885 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4886 p2++;
4887
4888 /* we have a complete value between p1 and p2 */
4889 if (p2 < cur_end && *p2 == '=') {
4890 /* we have something of the form no-cache="set-cookie" */
4891 if ((cur_end - p1 >= 21) &&
4892 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4893 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004894 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004895 continue;
4896 }
4897
4898 /* OK, so we know that either p2 points to the end of string or to a comma */
4899 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4900 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4901 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4902 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004903 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004904 return;
4905 }
4906
4907 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004908 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004909 continue;
4910 }
4911 }
4912}
4913
4914
Willy Tarreau58f10d72006-12-04 02:26:12 +01004915/*
4916 * Try to retrieve a known appsession in the URI, then the associated server.
4917 * If the server is found, it's assigned to the session.
4918 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004919void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004920{
Willy Tarreau3d300592007-03-18 18:34:41 +01004921 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004922 appsess *asession_temp = NULL;
4923 appsess local_asession;
4924 char *request_line;
4925
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004926 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004927 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004928 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004929 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004930 return;
4931
4932 /* skip ';' */
4933 request_line++;
4934
4935 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004936 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004937 return;
4938
4939 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004940 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004941
4942 /* First try if we already have an appsession */
4943 asession_temp = &local_asession;
4944
4945 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4946 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4947 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4948 return;
4949 }
4950
4951 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004952 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4953 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004954 asession_temp->serverid = NULL;
4955
4956 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004957 if (chtbl_lookup(&(t->be->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004958 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4959 /* free previously allocated memory */
4960 pool_free_to(apools.sessid, local_asession.sessid);
4961 Alert("Not enough memory process_cli():asession:calloc().\n");
4962 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4963 return;
4964 }
4965 asession_temp->sessid = local_asession.sessid;
4966 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004967 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004968 }
4969 else {
4970 /* free previously allocated memory */
4971 pool_free_to(apools.sessid, local_asession.sessid);
4972 }
4973
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004974 tv_delayfrom(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004975 asession_temp->request_count++;
4976
4977#if defined(DEBUG_HASH)
4978 print_table(&(t->proxy->htbl_proxy));
4979#endif
4980 if (asession_temp->serverid == NULL) {
4981 Alert("Found Application Session without matching server.\n");
4982 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004983 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004984 while (srv) {
4985 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004986 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004987 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004988 txn->flags &= ~TX_CK_MASK;
4989 txn->flags |= TX_CK_VALID;
4990 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991 t->srv = srv;
4992 break;
4993 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004994 txn->flags &= ~TX_CK_MASK;
4995 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004996 }
4997 }
4998 srv = srv->next;
4999 }
5000 }
5001}
5002
5003
Willy Tarreaub2513902006-12-17 14:52:38 +01005004/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005005 * In a GET or HEAD request, check if the requested URI matches the stats uri
5006 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005007 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005008 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005009 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005010 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005011 *
5012 * Returns 1 if the session's state changes, otherwise 0.
5013 */
5014int stats_check_uri_auth(struct session *t, struct proxy *backend)
5015{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005016 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005017 struct uri_auth *uri_auth = backend->uri_auth;
5018 struct user_auth *user;
5019 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005020 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005021
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005022 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005023 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005024 return 0;
5025
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005026 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005027
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005028 /* the URI is in h */
5029 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005030 return 0;
5031
5032 /* we are in front of a interceptable URI. Let's check
5033 * if there's an authentication and if it's valid.
5034 */
5035 user = uri_auth->users;
5036 if (!user) {
5037 /* no user auth required, it's OK */
5038 authenticated = 1;
5039 } else {
5040 authenticated = 0;
5041
5042 /* a user list is defined, we have to check.
5043 * skip 21 chars for "Authorization: Basic ".
5044 */
5045
5046 /* FIXME: this should move to an earlier place */
5047 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005048 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5049 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5050 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005051 if (len > 14 &&
5052 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005053 txn->auth_hdr.str = h;
5054 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005055 break;
5056 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005057 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005058 }
5059
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005060 if (txn->auth_hdr.len < 21 ||
5061 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005062 user = NULL;
5063
5064 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005065 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5066 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005067 user->user_pwd, user->user_len)) {
5068 authenticated = 1;
5069 break;
5070 }
5071 user = user->next;
5072 }
5073 }
5074
5075 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005076 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005077
5078 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005079 msg.str = trash;
5080 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005081 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005082 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01005083 if (!(t->flags & SN_ERR_MASK))
5084 t->flags |= SN_ERR_PRXCOND;
5085 if (!(t->flags & SN_FINST_MASK))
5086 t->flags |= SN_FINST_R;
5087 return 1;
5088 }
5089
5090 /* The request is valid, the user is authenticate. Let's start sending
5091 * data.
5092 */
5093 t->cli_state = CL_STSHUTR;
5094 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
5095 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
5096 t->data_source = DATA_SRC_STATS;
5097 t->data_state = DATA_ST_INIT;
5098 produce_content(t);
5099 return 1;
5100}
5101
5102
Willy Tarreaubaaee002006-06-26 02:48:02 +02005103/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005104 * Print a debug line with a header
5105 */
5106void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5107{
5108 int len, max;
5109 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5110 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5111 max = end - start;
5112 UBOUND(max, sizeof(trash) - len - 1);
5113 len += strlcpy2(trash + len, start, max + 1);
5114 trash[len++] = '\n';
5115 write(1, trash, len);
5116}
5117
5118
5119/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005120 * Local variables:
5121 * c-indent-level: 8
5122 * c-basic-offset: 8
5123 * End:
5124 */