blob: 38b2db02c74e71adcbf4b6e810508a6a58778459 [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 Tarreau2a429502006-10-15 14:52:29 +0200440 MY_FD_CLR(s->cli_fd, StaticReadEvent);
441 MY_FD_SET(s->cli_fd, StaticWriteEvent);
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 shutdown(s->cli_fd, SHUT_RD);
448 s->cli_state = CL_STSHUTR;
449 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100450 if (msg->len)
451 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200452 s->req->l = 0;
453}
454
455
456/*
457 * returns a message into the rep buffer, and flushes the req buffer.
Willy Tarreau0f772532006-12-23 20:51:41 +0100458 * The reply buffer doesn't need to be empty before this. The message
459 * is contained in a "chunk". If it is null, then an empty message is
460 * used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100462void client_return(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463{
464 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100465 if (msg->len)
466 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200467 s->req->l = 0;
468}
469
470
471/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100472 * indicators accordingly. Note that if <status> is 0, or if the message
473 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474 */
475void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100476 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477{
478 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100479 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100480 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100481 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100482 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200483 }
484 if (!(t->flags & SN_ERR_MASK))
485 t->flags |= err;
486 if (!(t->flags & SN_FINST_MASK))
487 t->flags |= finst;
488}
489
Willy Tarreau80587432006-12-24 17:47:20 +0100490/* This function returns the appropriate error location for the given session
491 * and message.
492 */
493
494struct chunk *error_message(struct session *s, int msgnum)
495{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200496 if (s->be->errmsg[msgnum].str)
497 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100498 else if (s->fe->errmsg[msgnum].str)
499 return &s->fe->errmsg[msgnum];
500 else
501 return &http_err_chunks[msgnum];
502}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503
Willy Tarreau53b6c742006-12-17 13:37:46 +0100504/*
505 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
506 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
507 */
508static http_meth_t find_http_meth(const char *str, const int len)
509{
510 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100511 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100512
513 m = ((unsigned)*str - 'A');
514
515 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100516 for (h = http_methods[m]; h->len > 0; h++) {
517 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100518 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100519 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100520 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100521 };
522 return HTTP_METH_OTHER;
523 }
524 return HTTP_METH_NONE;
525
526}
527
528
Willy Tarreaubaaee002006-06-26 02:48:02 +0200529/* Processes the client and server jobs of a session task, then
530 * puts it back to the wait queue in a clean state, or
531 * cleans up its resources if it must be deleted. Returns
532 * the time the task accepts to wait, or TIME_ETERNITY for
533 * infinity.
534 */
535int process_session(struct task *t)
536{
537 struct session *s = t->context;
538 int fsm_resync = 0;
539
540 do {
541 fsm_resync = 0;
542 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
543 fsm_resync |= process_cli(s);
544 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
545 fsm_resync |= process_srv(s);
546 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
547 } while (fsm_resync);
548
549 if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) {
550 struct timeval min1, min2;
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200551 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
552 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553
Willy Tarreaud7971282006-07-29 18:36:34 +0200554 tv_min(&min1, &s->req->rex, &s->req->wex);
555 tv_min(&min2, &s->rep->rex, &s->rep->wex);
556 tv_min(&min1, &min1, &s->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200557 tv_min(&t->expire, &min1, &min2);
558
559 /* restore t to its place in the task list */
560 task_queue(t);
561
562#ifdef DEBUG_FULL
563 /* DEBUG code : this should never ever happen, otherwise it indicates
564 * that a task still has something to do and will provoke a quick loop.
565 */
566 if (tv_remain2(&now, &t->expire) <= 0)
567 exit(100);
568#endif
569
570 return tv_remain2(&now, &t->expire); /* nothing more to do */
571 }
572
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100573 s->fe->feconn--;
574 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200575 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576 actconn--;
577
578 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
579 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100580 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200581 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100582 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583 write(1, trash, len);
584 }
585
586 s->logs.t_close = tv_diff(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100587 if (s->req != NULL)
588 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200589 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100590 s->logs.bytes_out = s->rep->total;
591
592 s->fe->bytes_in += s->logs.bytes_in;
593 s->fe->bytes_out += s->logs.bytes_out;
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200594 if (s->be != s->fe) {
595 s->be->bytes_in += s->logs.bytes_in;
596 s->be->bytes_out += s->logs.bytes_out;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100597 }
598 if (s->srv) {
599 s->srv->bytes_in += s->logs.bytes_in;
600 s->srv->bytes_out += s->logs.bytes_out;
601 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602
603 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200604 if (s->logs.logwait &&
605 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200606 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
607 if (s->fe->to_log & LW_REQ)
608 http_sess_log(s);
609 else
610 tcp_sess_log(s);
611 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200612
613 /* the task MUST not be in the run queue anymore */
614 task_delete(t);
615 session_free(s);
616 task_free(t);
617 return TIME_ETERNITY; /* rest in peace for eternity */
618}
619
620
Willy Tarreau42250582007-04-01 01:30:43 +0200621extern const char sess_term_cond[8];
622extern const char sess_fin_state[8];
623extern const char *monthname[12];
624const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
625const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
626 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
627 unknown, Set-cookie Rewritten */
628void **pool_requri = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100629
Willy Tarreau42250582007-04-01 01:30:43 +0200630/*
631 * send a log for the session when we have enough info about it.
632 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100633 */
Willy Tarreau42250582007-04-01 01:30:43 +0200634static void http_sess_log(struct session *s)
635{
636 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
637 struct proxy *fe = s->fe;
638 struct proxy *be = s->be;
639 struct proxy *prx_log;
640 struct http_txn *txn = &s->txn;
641 int tolog;
642 char *uri, *h;
643 char *svid;
644 struct tm *tm;
645 static char tmpline[MAX_SYSLOG_LEN];
646 int hdr;
647
648 if (fe->logfac1 < 0 && fe->logfac2 < 0)
649 return;
650 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100651
Willy Tarreau42250582007-04-01 01:30:43 +0200652 if (s->cli_addr.ss_family == AF_INET)
653 inet_ntop(AF_INET,
654 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
655 pn, sizeof(pn));
656 else
657 inet_ntop(AF_INET6,
658 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
659 pn, sizeof(pn));
660
661 tm = localtime((time_t *)&s->logs.tv_accept.tv_sec);
662
663
664 /* FIXME: let's limit ourselves to frontend logging for now. */
665 tolog = fe->to_log;
666
667 h = tmpline;
668 if (fe->to_log & LW_REQHDR &&
669 txn->req.cap &&
670 (h < tmpline + sizeof(tmpline) - 10)) {
671 *(h++) = ' ';
672 *(h++) = '{';
673 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
674 if (hdr)
675 *(h++) = '|';
676 if (txn->req.cap[hdr] != NULL)
677 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
678 '#', hdr_encode_map, txn->req.cap[hdr]);
679 }
680 *(h++) = '}';
681 }
682
683 if (fe->to_log & LW_RSPHDR &&
684 txn->rsp.cap &&
685 (h < tmpline + sizeof(tmpline) - 7)) {
686 *(h++) = ' ';
687 *(h++) = '{';
688 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
689 if (hdr)
690 *(h++) = '|';
691 if (txn->rsp.cap[hdr] != NULL)
692 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
693 '#', hdr_encode_map, txn->rsp.cap[hdr]);
694 }
695 *(h++) = '}';
696 }
697
698 if (h < tmpline + sizeof(tmpline) - 4) {
699 *(h++) = ' ';
700 *(h++) = '"';
701 uri = txn->uri ? txn->uri : "<BADREQ>";
702 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
703 '#', url_encode_map, uri);
704 *(h++) = '"';
705 }
706 *h = '\0';
707
708 svid = (tolog & LW_SVID) ?
709 (s->data_source != DATA_SRC_STATS) ?
710 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
711
712 send_log(prx_log, LOG_INFO,
713 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
714 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
715 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
716 pn,
717 (s->cli_addr.ss_family == AF_INET) ?
718 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
719 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
720 tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900,
721 tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000,
722 fe->id, be->id, svid,
723 s->logs.t_request,
724 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
725 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
726 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
727 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
728 txn->status,
729 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
730 txn->cli_cookie ? txn->cli_cookie : "-",
731 txn->srv_cookie ? txn->srv_cookie : "-",
732 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
733 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
734 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
735 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
736 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
737 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
738
739 s->logs.logwait = 0;
740}
741
Willy Tarreau117f59e2007-03-04 18:17:17 +0100742
743/*
744 * Capture headers from message starting at <som> according to header list
745 * <cap_hdr>, and fill the <idx> structure appropriately.
746 */
747void capture_headers(char *som, struct hdr_idx *idx,
748 char **cap, struct cap_hdr *cap_hdr)
749{
750 char *eol, *sol, *col, *sov;
751 int cur_idx;
752 struct cap_hdr *h;
753 int len;
754
755 sol = som + hdr_idx_first_pos(idx);
756 cur_idx = hdr_idx_first_idx(idx);
757
758 while (cur_idx) {
759 eol = sol + idx->v[cur_idx].len;
760
761 col = sol;
762 while (col < eol && *col != ':')
763 col++;
764
765 sov = col + 1;
766 while (sov < eol && http_is_lws[(unsigned char)*sov])
767 sov++;
768
769 for (h = cap_hdr; h; h = h->next) {
770 if ((h->namelen == col - sol) &&
771 (strncasecmp(sol, h->name, h->namelen) == 0)) {
772 if (cap[h->index] == NULL)
773 cap[h->index] =
774 pool_alloc_from(h->pool, h->len + 1);
775
776 if (cap[h->index] == NULL) {
777 Alert("HTTP capture : out of memory.\n");
778 continue;
779 }
780
781 len = eol - sov;
782 if (len > h->len)
783 len = h->len;
784
785 memcpy(cap[h->index], sov, len);
786 cap[h->index][len]=0;
787 }
788 }
789 sol = eol + idx->v[cur_idx].cr + 1;
790 cur_idx = idx->v[cur_idx].next;
791 }
792}
793
794
Willy Tarreau42250582007-04-01 01:30:43 +0200795/* either we find an LF at <ptr> or we jump to <bad>.
796 */
797#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
798
799/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
800 * otherwise to <http_msg_ood> with <state> set to <st>.
801 */
802#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
803 ptr++; \
804 if (likely(ptr < end)) \
805 goto good; \
806 else { \
807 state = (st); \
808 goto http_msg_ood; \
809 } \
810 } while (0)
811
812
Willy Tarreaubaaee002006-06-26 02:48:02 +0200813/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100814 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100815 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
816 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
817 * will give undefined results.
818 * Note that it is upon the caller's responsibility to ensure that ptr < end,
819 * and that msg->sol points to the beginning of the response.
820 * If a complete line is found (which implies that at least one CR or LF is
821 * found before <end>, the updated <ptr> is returned, otherwise NULL is
822 * returned indicating an incomplete line (which does not mean that parts have
823 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
824 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
825 * upon next call.
826 *
827 * This function was intentionnally designed to be called from
828 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
829 * within its state machine and use the same macros, hence the need for same
830 * labels and variable names.
831 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100832const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100833 const char *ptr, const char *end,
834 char **ret_ptr, int *ret_state)
835{
836 __label__
837 http_msg_rpver,
838 http_msg_rpver_sp,
839 http_msg_rpcode,
840 http_msg_rpcode_sp,
841 http_msg_rpreason,
842 http_msg_rpline_eol,
843 http_msg_ood, /* out of data */
844 http_msg_invalid;
845
846 switch (state) {
847 http_msg_rpver:
848 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100849 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100850 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
851
852 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100853 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100854 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
855 }
856 goto http_msg_invalid;
857
858 http_msg_rpver_sp:
859 case HTTP_MSG_RPVER_SP:
860 if (likely(!HTTP_IS_LWS(*ptr))) {
861 msg->sl.st.c = ptr - msg_buf;
862 goto http_msg_rpcode;
863 }
864 if (likely(HTTP_IS_SPHT(*ptr)))
865 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
866 /* so it's a CR/LF, this is invalid */
867 goto http_msg_invalid;
868
869 http_msg_rpcode:
870 case HTTP_MSG_RPCODE:
871 if (likely(!HTTP_IS_LWS(*ptr)))
872 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
873
874 if (likely(HTTP_IS_SPHT(*ptr))) {
875 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
876 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
877 }
878
879 /* so it's a CR/LF, so there is no reason phrase */
880 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
881 http_msg_rsp_reason:
882 /* FIXME: should we support HTTP responses without any reason phrase ? */
883 msg->sl.st.r = ptr - msg_buf;
884 msg->sl.st.r_l = 0;
885 goto http_msg_rpline_eol;
886
887 http_msg_rpcode_sp:
888 case HTTP_MSG_RPCODE_SP:
889 if (likely(!HTTP_IS_LWS(*ptr))) {
890 msg->sl.st.r = ptr - msg_buf;
891 goto http_msg_rpreason;
892 }
893 if (likely(HTTP_IS_SPHT(*ptr)))
894 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
895 /* so it's a CR/LF, so there is no reason phrase */
896 goto http_msg_rsp_reason;
897
898 http_msg_rpreason:
899 case HTTP_MSG_RPREASON:
900 if (likely(!HTTP_IS_CRLF(*ptr)))
901 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
902 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
903 http_msg_rpline_eol:
904 /* We have seen the end of line. Note that we do not
905 * necessarily have the \n yet, but at least we know that we
906 * have EITHER \r OR \n, otherwise the response would not be
907 * complete. We can then record the response length and return
908 * to the caller which will be able to register it.
909 */
910 msg->sl.st.l = ptr - msg->sol;
911 return ptr;
912
913#ifdef DEBUG_FULL
914 default:
915 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
916 exit(1);
917#endif
918 }
919
920 http_msg_ood:
921 /* out of data */
922 if (ret_state)
923 *ret_state = state;
924 if (ret_ptr)
925 *ret_ptr = (char *)ptr;
926 return NULL;
927
928 http_msg_invalid:
929 /* invalid message */
930 if (ret_state)
931 *ret_state = HTTP_MSG_ERROR;
932 return NULL;
933}
934
935
936/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100937 * This function parses a request line between <ptr> and <end>, starting with
938 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
939 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
940 * will give undefined results.
941 * Note that it is upon the caller's responsibility to ensure that ptr < end,
942 * and that msg->sol points to the beginning of the request.
943 * If a complete line is found (which implies that at least one CR or LF is
944 * found before <end>, the updated <ptr> is returned, otherwise NULL is
945 * returned indicating an incomplete line (which does not mean that parts have
946 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
947 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
948 * upon next call.
949 *
950 * This function was intentionnally designed to be called from
951 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
952 * within its state machine and use the same macros, hence the need for same
953 * labels and variable names.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200954 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100955const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 const char *ptr, const char *end,
957 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200958{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100959 __label__
960 http_msg_rqmeth,
961 http_msg_rqmeth_sp,
962 http_msg_rquri,
963 http_msg_rquri_sp,
964 http_msg_rqver,
965 http_msg_rqline_eol,
966 http_msg_ood, /* out of data */
967 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100968
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100969 switch (state) {
970 http_msg_rqmeth:
971 case HTTP_MSG_RQMETH:
972 if (likely(HTTP_IS_TOKEN(*ptr)))
973 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100974
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100975 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100976 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100977 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
978 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100979
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100980 if (likely(HTTP_IS_CRLF(*ptr))) {
981 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100982 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100983 http_msg_req09_uri:
984 msg->sl.rq.u = ptr - msg_buf;
985 http_msg_req09_uri_e:
986 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
987 http_msg_req09_ver:
988 msg->sl.rq.v = ptr - msg_buf;
989 msg->sl.rq.v_l = 0;
990 goto http_msg_rqline_eol;
991 }
992 goto http_msg_invalid;
993
994 http_msg_rqmeth_sp:
995 case HTTP_MSG_RQMETH_SP:
996 if (likely(!HTTP_IS_LWS(*ptr))) {
997 msg->sl.rq.u = ptr - msg_buf;
998 goto http_msg_rquri;
999 }
1000 if (likely(HTTP_IS_SPHT(*ptr)))
1001 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1002 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1003 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001004
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001005 http_msg_rquri:
1006 case HTTP_MSG_RQURI:
1007 if (likely(!HTTP_IS_LWS(*ptr)))
1008 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001009
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001010 if (likely(HTTP_IS_SPHT(*ptr))) {
1011 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1012 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1013 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001014
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001015 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1016 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001017
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001018 http_msg_rquri_sp:
1019 case HTTP_MSG_RQURI_SP:
1020 if (likely(!HTTP_IS_LWS(*ptr))) {
1021 msg->sl.rq.v = ptr - msg_buf;
1022 goto http_msg_rqver;
1023 }
1024 if (likely(HTTP_IS_SPHT(*ptr)))
1025 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1026 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1027 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001028
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029 http_msg_rqver:
1030 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001031 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001032 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001033
1034 if (likely(HTTP_IS_CRLF(*ptr))) {
1035 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1036 http_msg_rqline_eol:
1037 /* We have seen the end of line. Note that we do not
1038 * necessarily have the \n yet, but at least we know that we
1039 * have EITHER \r OR \n, otherwise the request would not be
1040 * complete. We can then record the request length and return
1041 * to the caller which will be able to register it.
1042 */
1043 msg->sl.rq.l = ptr - msg->sol;
1044 return ptr;
1045 }
1046
1047 /* neither an HTTP_VER token nor a CRLF */
1048 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001049
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001050#ifdef DEBUG_FULL
1051 default:
1052 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1053 exit(1);
1054#endif
1055 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001056
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001057 http_msg_ood:
1058 /* out of data */
1059 if (ret_state)
1060 *ret_state = state;
1061 if (ret_ptr)
1062 *ret_ptr = (char *)ptr;
1063 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001065 http_msg_invalid:
1066 /* invalid message */
1067 if (ret_state)
1068 *ret_state = HTTP_MSG_ERROR;
1069 return NULL;
1070}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001071
1072
Willy Tarreau8973c702007-01-21 23:58:29 +01001073/*
1074 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001075 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001076 * when data are missing and recalled at the exact same location with no
1077 * information loss. The header index is re-initialized when switching from
1078 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH.
1079 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1081{
1082 __label__
1083 http_msg_rqbefore,
1084 http_msg_rqbefore_cr,
1085 http_msg_rqmeth,
1086 http_msg_rqline_end,
1087 http_msg_hdr_first,
1088 http_msg_hdr_name,
1089 http_msg_hdr_l1_sp,
1090 http_msg_hdr_l1_lf,
1091 http_msg_hdr_l1_lws,
1092 http_msg_hdr_val,
1093 http_msg_hdr_l2_lf,
1094 http_msg_hdr_l2_lws,
1095 http_msg_complete_header,
1096 http_msg_last_lf,
1097 http_msg_ood, /* out of data */
1098 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001099
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 int state; /* updated only when leaving the FSM */
1101 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001102
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001103 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 ptr = buf->lr;
1105 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 if (unlikely(ptr >= end))
1108 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001111 /*
1112 * First, states that are specific to the response only.
1113 * We check them first so that request and headers are
1114 * closer to each other (accessed more often).
1115 */
1116 http_msg_rpbefore:
1117 case HTTP_MSG_RPBEFORE:
1118 if (likely(HTTP_IS_TOKEN(*ptr))) {
1119 if (likely(ptr == buf->data)) {
1120 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001121 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001122 } else {
1123#if PARSE_PRESERVE_EMPTY_LINES
1124 /* only skip empty leading lines, don't remove them */
1125 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001126 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001127#else
1128 /* Remove empty leading lines, as recommended by
1129 * RFC2616. This takes a lot of time because we
1130 * must move all the buffer backwards, but this
1131 * is rarely needed. The method above will be
1132 * cleaner when we'll be able to start sending
1133 * the request from any place in the buffer.
1134 */
1135 buf->lr = ptr;
1136 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001137 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 msg->sol = buf->data;
1139 ptr = buf->data;
1140 end = buf->r;
1141#endif
1142 }
1143 hdr_idx_init(idx);
1144 state = HTTP_MSG_RPVER;
1145 goto http_msg_rpver;
1146 }
1147
1148 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1149 goto http_msg_invalid;
1150
1151 if (unlikely(*ptr == '\n'))
1152 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1154 /* stop here */
1155
1156 http_msg_rpbefore_cr:
1157 case HTTP_MSG_RPBEFORE_CR:
1158 EXPECT_LF_HERE(ptr, http_msg_invalid);
1159 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1160 /* stop here */
1161
1162 http_msg_rpver:
1163 case HTTP_MSG_RPVER:
1164 case HTTP_MSG_RPVER_SP:
1165 case HTTP_MSG_RPCODE:
1166 case HTTP_MSG_RPCODE_SP:
1167 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001168 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001169 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 if (unlikely(!ptr))
1171 return;
1172
1173 /* we have a full response and we know that we have either a CR
1174 * or an LF at <ptr>.
1175 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001176 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001177 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1178
1179 msg->sol = ptr;
1180 if (likely(*ptr == '\r'))
1181 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1182 goto http_msg_rpline_end;
1183
1184 http_msg_rpline_end:
1185 case HTTP_MSG_RPLINE_END:
1186 /* msg->sol must point to the first of CR or LF. */
1187 EXPECT_LF_HERE(ptr, http_msg_invalid);
1188 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1189 /* stop here */
1190
1191 /*
1192 * Second, states that are specific to the request only
1193 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 http_msg_rqbefore:
1195 case HTTP_MSG_RQBEFORE:
1196 if (likely(HTTP_IS_TOKEN(*ptr))) {
1197 if (likely(ptr == buf->data)) {
1198 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001199 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 } else {
1201#if PARSE_PRESERVE_EMPTY_LINES
1202 /* only skip empty leading lines, don't remove them */
1203 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001204 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001205#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001206 /* Remove empty leading lines, as recommended by
1207 * RFC2616. This takes a lot of time because we
1208 * must move all the buffer backwards, but this
1209 * is rarely needed. The method above will be
1210 * cleaner when we'll be able to start sending
1211 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001212 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001213 buf->lr = ptr;
1214 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001215 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001216 msg->sol = buf->data;
1217 ptr = buf->data;
1218 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001219#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001220 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001221 /* we will need this when keep-alive will be supported
1222 hdr_idx_init(idx);
1223 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001224 state = HTTP_MSG_RQMETH;
1225 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001227
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001228 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1229 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001230
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 if (unlikely(*ptr == '\n'))
1232 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1233 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001234 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 http_msg_rqbefore_cr:
1237 case HTTP_MSG_RQBEFORE_CR:
1238 EXPECT_LF_HERE(ptr, http_msg_invalid);
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001240 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 http_msg_rqmeth:
1243 case HTTP_MSG_RQMETH:
1244 case HTTP_MSG_RQMETH_SP:
1245 case HTTP_MSG_RQURI:
1246 case HTTP_MSG_RQURI_SP:
1247 case HTTP_MSG_RQVER:
1248 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001249 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 if (unlikely(!ptr))
1251 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001252
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 /* we have a full request and we know that we have either a CR
1254 * or an LF at <ptr>.
1255 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001256 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001257 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259 msg->sol = ptr;
1260 if (likely(*ptr == '\r'))
1261 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 http_msg_rqline_end:
1265 case HTTP_MSG_RQLINE_END:
1266 /* check for HTTP/0.9 request : no version information available.
1267 * msg->sol must point to the first of CR or LF.
1268 */
1269 if (unlikely(msg->sl.rq.v_l == 0))
1270 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001271
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 EXPECT_LF_HERE(ptr, http_msg_invalid);
1273 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001275
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 /*
1277 * Common states below
1278 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 http_msg_hdr_first:
1280 case HTTP_MSG_HDR_FIRST:
1281 msg->sol = ptr;
1282 if (likely(!HTTP_IS_CRLF(*ptr))) {
1283 goto http_msg_hdr_name;
1284 }
1285
1286 if (likely(*ptr == '\r'))
1287 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1288 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 http_msg_hdr_name:
1291 case HTTP_MSG_HDR_NAME:
1292 /* assumes msg->sol points to the first char */
1293 if (likely(HTTP_IS_TOKEN(*ptr)))
1294 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 if (likely(*ptr == ':')) {
1297 msg->col = ptr - buf->data;
1298 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1299 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001300
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 http_msg_hdr_l1_sp:
1304 case HTTP_MSG_HDR_L1_SP:
1305 /* assumes msg->sol points to the first char and msg->col to the colon */
1306 if (likely(HTTP_IS_SPHT(*ptr)))
1307 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 /* header value can be basically anything except CR/LF */
1310 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001311
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 if (likely(!HTTP_IS_CRLF(*ptr))) {
1313 goto http_msg_hdr_val;
1314 }
1315
1316 if (likely(*ptr == '\r'))
1317 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1318 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 http_msg_hdr_l1_lf:
1321 case HTTP_MSG_HDR_L1_LF:
1322 EXPECT_LF_HERE(ptr, http_msg_invalid);
1323 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 http_msg_hdr_l1_lws:
1326 case HTTP_MSG_HDR_L1_LWS:
1327 if (likely(HTTP_IS_SPHT(*ptr))) {
1328 /* replace HT,CR,LF with spaces */
1329 for (; buf->data+msg->sov < ptr; msg->sov++)
1330 buf->data[msg->sov] = ' ';
1331 goto http_msg_hdr_l1_sp;
1332 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001333 /* we had a header consisting only in spaces ! */
1334 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 goto http_msg_complete_header;
1336
1337 http_msg_hdr_val:
1338 case HTTP_MSG_HDR_VAL:
1339 /* assumes msg->sol points to the first char, msg->col to the
1340 * colon, and msg->sov points to the first character of the
1341 * value.
1342 */
1343 if (likely(!HTTP_IS_CRLF(*ptr)))
1344 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 msg->eol = ptr;
1347 /* Note: we could also copy eol into ->eoh so that we have the
1348 * real header end in case it ends with lots of LWS, but is this
1349 * really needed ?
1350 */
1351 if (likely(*ptr == '\r'))
1352 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1353 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001355 http_msg_hdr_l2_lf:
1356 case HTTP_MSG_HDR_L2_LF:
1357 EXPECT_LF_HERE(ptr, http_msg_invalid);
1358 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001359
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 http_msg_hdr_l2_lws:
1361 case HTTP_MSG_HDR_L2_LWS:
1362 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1363 /* LWS: replace HT,CR,LF with spaces */
1364 for (; msg->eol < ptr; msg->eol++)
1365 *msg->eol = ' ';
1366 goto http_msg_hdr_val;
1367 }
1368 http_msg_complete_header:
1369 /*
1370 * It was a new header, so the last one is finished.
1371 * Assumes msg->sol points to the first char, msg->col to the
1372 * colon, msg->sov points to the first character of the value
1373 * and msg->eol to the first CR or LF so we know how the line
1374 * ends. We insert last header into the index.
1375 */
1376 /*
1377 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1378 write(2, msg->sol, msg->eol-msg->sol);
1379 fprintf(stderr,"\n");
1380 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1383 idx, idx->tail) < 0))
1384 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 msg->sol = ptr;
1387 if (likely(!HTTP_IS_CRLF(*ptr))) {
1388 goto http_msg_hdr_name;
1389 }
1390
1391 if (likely(*ptr == '\r'))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1393 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 http_msg_last_lf:
1396 case HTTP_MSG_LAST_LF:
1397 /* Assumes msg->sol points to the first of either CR or LF */
1398 EXPECT_LF_HERE(ptr, http_msg_invalid);
1399 ptr++;
1400 buf->lr = ptr;
1401 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001402 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 return;
1404#ifdef DEBUG_FULL
1405 default:
1406 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1407 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001408#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 }
1410 http_msg_ood:
1411 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001412 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 buf->lr = ptr;
1414 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 http_msg_invalid:
1417 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001418 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 return;
1420}
1421
1422/*
1423 * manages the client FSM and its socket. BTW, it also tries to handle the
1424 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1425 * 0 else.
1426 */
1427int process_cli(struct session *t)
1428{
1429 int s = t->srv_state;
1430 int c = t->cli_state;
1431 struct buffer *req = t->req;
1432 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1435 cli_stnames[c], srv_stnames[s],
1436 MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
1437 req->rex.tv_sec, req->rex.tv_usec,
1438 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (c == CL_STHEADERS) {
1441 /*
1442 * Now parse the partial (or complete) lines.
1443 * We will check the request syntax, and also join multi-line
1444 * headers. An index of all the lines will be elaborated while
1445 * parsing.
1446 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001450 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 * req->data + req->eoh = end of processed headers / start of current one
1452 * req->data + req->eol = end of current header or line (LF or CRLF)
1453 * req->lr = first non-visited byte
1454 * req->r = end of data
1455 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001456
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001458 struct http_txn *txn = &t->txn;
1459 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001463 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 /* 1: we might have to print this header in debug mode */
1466 if (unlikely((global.mode & MODE_DEBUG) &&
1467 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001470
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001471 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 eol = sol + msg->sl.rq.l;
1473 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001474
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001475 sol += hdr_idx_first_pos(&txn->hdr_idx);
1476 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001479 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001481 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1482 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 }
1484 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001485
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
1487 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001489 * If not so, we check the FD and buffer states before leaving.
1490 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1492 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493 *
1494 */
1495
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001496 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 /*
1498 * First, let's catch bad requests.
1499 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001500 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
1503 /* 1: Since we are in header mode, if there's no space
1504 * left for headers, we won't be able to free more
1505 * later, so the session will never terminate. We
1506 * must terminate it now.
1507 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (unlikely(req->l >= req->rlim - req->data)) {
1509 /* FIXME: check if URI is set and return Status
1510 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001511 */
Willy Tarreau06619262006-12-17 08:37:22 +01001512 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001513 }
1514
1515 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1517 /* read error, or last read : give up. */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001518 tv_eternity(&req->rex);
1519 fd_delete(t->cli_fd);
1520 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001521 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522 if (!(t->flags & SN_ERR_MASK))
1523 t->flags |= SN_ERR_CLICL;
1524 if (!(t->flags & SN_FINST_MASK))
1525 t->flags |= SN_FINST_R;
1526 return 1;
1527 }
1528
1529 /* 3: has the read timeout expired ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 else if (unlikely(tv_cmp2_ms(&req->rex, &now) <= 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001531 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001532 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001533 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001534 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001535 if (!(t->flags & SN_ERR_MASK))
1536 t->flags |= SN_ERR_CLITO;
1537 if (!(t->flags & SN_FINST_MASK))
1538 t->flags |= SN_FINST_R;
1539 return 1;
1540 }
1541
1542 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 else if (unlikely(! MY_FD_ISSET(t->cli_fd, StaticReadEvent))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001544 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
1545 * full. We cannot loop here since stream_sock_read will disable it only if
1546 * req->l == rlim-data
1547 */
1548 MY_FD_SET(t->cli_fd, StaticReadEvent);
1549 if (t->fe->clitimeout)
1550 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
1551 else
1552 tv_eternity(&req->rex);
1553 }
1554 return t->cli_state != CL_STHEADERS;
1555 }
1556
1557
1558 /****************************************************************
1559 * More interesting part now : we know that we have a complete *
1560 * request which at least looks like HTTP. We have an indicator *
1561 * of each header's length, so we can parse them quickly. *
1562 ****************************************************************/
1563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 /*
1565 * 1: identify the method
1566 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001567 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001568
1569 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001571 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001573 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 if (unlikely((t->fe->monitor_uri_len != 0) &&
1575 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1576 !memcmp(&req->data[msg->sl.rq.u],
1577 t->fe->monitor_uri,
1578 t->fe->monitor_uri_len))) {
1579 /*
1580 * We have found the monitor URI
1581 */
1582 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001583 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 client_retnclose(t, &http_200_chunk);
1585 goto return_prx_cond;
1586 }
1587
1588 /*
1589 * 3: Maybe we have to copy the original REQURI for the logs ?
1590 * Note: we cannot log anymore if the request has been
1591 * classified as invalid.
1592 */
1593 if (unlikely(t->logs.logwait & LW_REQ)) {
1594 /* we have a complete HTTP request that we must log */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001595 if ((txn->uri = pool_alloc(requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 int urilen = msg->sl.rq.l;
1597
1598 if (urilen >= REQURI_LEN)
1599 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001600 memcpy(txn->uri, &req->data[msg->som], urilen);
1601 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602
1603 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001604 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 } else {
1606 Alert("HTTP logging : out of memory.\n");
1607 }
1608 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001609
Willy Tarreau06619262006-12-17 08:37:22 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1612 if (unlikely(msg->sl.rq.v_l == 0)) {
1613 int delta;
1614 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001615 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 cur_end = msg->sol + msg->sl.rq.l;
1617 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 if (msg->sl.rq.u_l == 0) {
1620 /* if no URI was set, add "/" */
1621 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1622 cur_end += delta;
1623 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001624 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 /* add HTTP version */
1626 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1627 msg->eoh += delta;
1628 cur_end += delta;
1629 cur_end = (char *)http_parse_reqline(msg, req->data,
1630 HTTP_MSG_RQMETH,
1631 msg->sol, cur_end + 1,
1632 NULL, NULL);
1633 if (unlikely(!cur_end))
1634 goto return_bad_req;
1635
1636 /* we have a full HTTP/1.0 request now and we know that
1637 * we have either a CR or an LF at <ptr>.
1638 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001639 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001640 }
1641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642
1643 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001644 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001645 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001646 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001647
1648 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001650 * As opposed to version 1.2, now they will be evaluated in the
1651 * filters order and not in the header order. This means that
1652 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001653 *
1654 * We can now check whether we want to switch to another
1655 * backend, in which case we will re-check the backend's
1656 * filters and various options. In order to support 3-level
1657 * switching, here's how we should proceed :
1658 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001659 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001660 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001661 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001662 * There cannot be any switch from there, so ->be cannot be
1663 * changed anymore.
1664 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001665 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001666 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001667 * The response path will be able to apply either ->be, or
1668 * ->be then ->fe filters in order to match the reverse of
1669 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670 */
1671
Willy Tarreau06619262006-12-17 08:37:22 +01001672 do {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001673 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001674 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001675
Willy Tarreau06619262006-12-17 08:37:22 +01001676 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001677 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1679 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001680 }
1681
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001682 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1683 /* to ensure correct connection accounting on
1684 * the backend, we count the connection for the
1685 * one managing the queue.
1686 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001687 t->be->beconn++;
1688 if (t->be->beconn > t->be->beconn_max)
1689 t->be->beconn_max = t->be->beconn;
1690 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001691 t->flags |= SN_BE_ASSIGNED;
1692 }
1693
Willy Tarreau06619262006-12-17 08:37:22 +01001694 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001695 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001696 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001697 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001698 /* let's log the request time */
1699 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001700 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001701 goto return_prx_cond;
1702 }
1703
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001705 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 !(t->flags & SN_CONN_CLOSED)) {
1707 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001708 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001710
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001711 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 old_idx = 0;
1713
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001714 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1715 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 cur_ptr = cur_next;
1717 cur_end = cur_ptr + cur_hdr->len;
1718 cur_next = cur_end + cur_hdr->cr + 1;
1719
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001720 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1721 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 /* 3 possibilities :
1723 * - we have already set Connection: close,
1724 * so we remove this line.
1725 * - we have not yet set Connection: close,
1726 * but this line indicates close. We leave
1727 * it untouched and set the flag.
1728 * - we have not yet set Connection: close,
1729 * and this line indicates non-close. We
1730 * replace it.
1731 */
1732 if (t->flags & SN_CONN_CLOSED) {
1733 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001734 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001736 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1737 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 cur_hdr->len = 0;
1739 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001740 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1741 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1742 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 cur_next += delta;
1744 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001745 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 }
1747 t->flags |= SN_CONN_CLOSED;
1748 }
1749 }
1750 old_idx = cur_idx;
1751 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001752 }
1753 /* add request headers from the rule sets in the same order */
1754 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1755 if (unlikely(http_header_add_tail(req,
1756 &txn->req,
1757 &txn->hdr_idx,
1758 rule_set->req_add[cur_idx])) < 0)
1759 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001760 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001763 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001764 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001765 /* we have to check the URI and auth for this request */
1766 if (stats_check_uri_auth(t, rule_set))
1767 return 1;
1768 }
1769
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001770 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1771 /* No backend was set, but there was a default
1772 * backend set in the frontend, so we use it and
1773 * loop again.
1774 */
1775 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001776 t->be->beconn++;
1777 if (t->be->beconn > t->be->beconn_max)
1778 t->be->beconn_max = t->be->beconn;
1779 t->be->cum_beconn++;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001780 t->flags |= SN_BE_ASSIGNED;
1781 }
1782 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001783
Willy Tarreau58f10d72006-12-04 02:26:12 +01001784
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001785 if (!(t->flags & SN_BE_ASSIGNED)) {
1786 /* To ensure correct connection accounting on
1787 * the backend, we count the connection for the
1788 * one managing the queue.
1789 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001790 t->be->beconn++;
1791 if (t->be->beconn > t->be->beconn_max)
1792 t->be->beconn_max = t->be->beconn;
1793 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001794 t->flags |= SN_BE_ASSIGNED;
1795 }
1796
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001797 /*
1798 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001799 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001800 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001801 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001802 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001803
Willy Tarreau58f10d72006-12-04 02:26:12 +01001804
Willy Tarreau58f10d72006-12-04 02:26:12 +01001805
Willy Tarreau58f10d72006-12-04 02:26:12 +01001806
Willy Tarreau2a324282006-12-05 00:05:46 +01001807 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001809 * so let's do the same now.
1810 */
1811
1812 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001813 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001814 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001815 }
1816
1817
1818 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001820 * Note that doing so might move headers in the request, but
1821 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001822 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001823 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001824 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001825 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001826
Willy Tarreau58f10d72006-12-04 02:26:12 +01001827
Willy Tarreau2a324282006-12-05 00:05:46 +01001828 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001829 * 9: add X-Forwarded-For if either the frontend or the backend
1830 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001831 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001832 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001833 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001834 /* Add an X-Forwarded-For header unless the source IP is
1835 * in the 'except' network range.
1836 */
1837 if ((!t->fe->except_mask.s_addr ||
1838 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1839 != t->fe->except_net.s_addr) &&
1840 (!t->be->except_mask.s_addr ||
1841 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1842 != t->be->except_net.s_addr)) {
1843 int len;
1844 unsigned char *pn;
1845 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001846
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001847 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1848 pn[0], pn[1], pn[2], pn[3]);
1849
1850 if (unlikely(http_header_add_tail2(req, &txn->req,
1851 &txn->hdr_idx, trash, len)) < 0)
1852 goto return_bad_req;
1853 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001854 }
1855 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001856 /* FIXME: for the sake of completeness, we should also support
1857 * 'except' here, although it is mostly useless in this case.
1858 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001859 int len;
1860 char pn[INET6_ADDRSTRLEN];
1861 inet_ntop(AF_INET6,
1862 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1863 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001864 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1865 if (unlikely(http_header_add_tail2(req, &txn->req,
1866 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001867 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001868 }
1869 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001870
Willy Tarreau2a324282006-12-05 00:05:46 +01001871 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001872 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02001873 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01001874 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02001875 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001876 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02001877 if ((unlikely(msg->sl.rq.v_l != 8) ||
1878 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
1879 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001880 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001881 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001882 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001883 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001884
Willy Tarreau2a324282006-12-05 00:05:46 +01001885 /*************************************************************
1886 * OK, that's finished for the headers. We have done what we *
1887 * could. Let's switch to the DATA state. *
1888 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001889
Willy Tarreau2a324282006-12-05 00:05:46 +01001890 t->cli_state = CL_STDATA;
1891 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001892
Willy Tarreau2a324282006-12-05 00:05:46 +01001893 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001894
Willy Tarreau2a324282006-12-05 00:05:46 +01001895 if (!t->fe->clitimeout ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001896 (t->srv_state < SV_STDATA && t->be->srvtimeout)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001897 /* If the client has no timeout, or if the server is not ready yet,
1898 * and we know for sure that it can expire, then it's cleaner to
1899 * disable the timeout on the client side so that too low values
1900 * cannot make the sessions abort too early.
1901 *
1902 * FIXME-20050705: the server needs a way to re-enable this time-out
1903 * when it switches its state, otherwise a client can stay connected
1904 * indefinitely. This now seems to be OK.
1905 */
1906 tv_eternity(&req->rex);
1907 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001908
Willy Tarreau2a324282006-12-05 00:05:46 +01001909 /* When a connection is tarpitted, we use the queue timeout for the
1910 * tarpit delay, which currently happens to be the server's connect
1911 * timeout. If unset, then set it to zero because we really want it
1912 * to expire at one moment.
1913 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001914 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001915 t->req->l = 0;
1916 /* flush the request so that we can drop the connection early
1917 * if the client closes first.
1918 */
1919 tv_delayfrom(&req->cex, &now,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001920 t->be->contimeout ? t->be->contimeout : 0);
Willy Tarreau2a324282006-12-05 00:05:46 +01001921 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001922
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01001924 goto process_data;
1925
1926 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001927 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001928 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01001929 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001930 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01001931 return_prx_cond:
1932 if (!(t->flags & SN_ERR_MASK))
1933 t->flags |= SN_ERR_PRXCOND;
1934 if (!(t->flags & SN_FINST_MASK))
1935 t->flags |= SN_FINST_R;
1936 return 1;
1937
Willy Tarreaubaaee002006-06-26 02:48:02 +02001938 }
1939 else if (c == CL_STDATA) {
1940 process_data:
1941 /* FIXME: this error handling is partly buggy because we always report
1942 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1943 * or HEADER phase. BTW, it's not logical to expire the client while
1944 * we're waiting for the server to connect.
1945 */
1946 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001947 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001948 tv_eternity(&req->rex);
1949 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001950 fd_delete(t->cli_fd);
1951 t->cli_state = CL_STCLOSE;
1952 if (!(t->flags & SN_ERR_MASK))
1953 t->flags |= SN_ERR_CLICL;
1954 if (!(t->flags & SN_FINST_MASK)) {
1955 if (t->pend_pos)
1956 t->flags |= SN_FINST_Q;
1957 else if (s == SV_STCONN)
1958 t->flags |= SN_FINST_C;
1959 else
1960 t->flags |= SN_FINST_D;
1961 }
1962 return 1;
1963 }
1964 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001965 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001966 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001967 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001968 shutdown(t->cli_fd, SHUT_RD);
1969 t->cli_state = CL_STSHUTR;
1970 return 1;
1971 }
1972 /* last server read and buffer empty */
1973 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001974 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001975 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001976 shutdown(t->cli_fd, SHUT_WR);
1977 /* We must ensure that the read part is still alive when switching
1978 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001979 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001980 if (t->fe->clitimeout)
1981 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001982 t->cli_state = CL_STSHUTW;
1983 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1984 return 1;
1985 }
1986 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001987 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001988 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001989 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001990 shutdown(t->cli_fd, SHUT_RD);
1991 t->cli_state = CL_STSHUTR;
1992 if (!(t->flags & SN_ERR_MASK))
1993 t->flags |= SN_ERR_CLITO;
1994 if (!(t->flags & SN_FINST_MASK)) {
1995 if (t->pend_pos)
1996 t->flags |= SN_FINST_Q;
1997 else if (s == SV_STCONN)
1998 t->flags |= SN_FINST_C;
1999 else
2000 t->flags |= SN_FINST_D;
2001 }
2002 return 1;
2003 }
2004 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002005 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002006 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002007 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002008 shutdown(t->cli_fd, SHUT_WR);
2009 /* We must ensure that the read part is still alive when switching
2010 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002011 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002012 if (t->fe->clitimeout)
2013 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002014
2015 t->cli_state = CL_STSHUTW;
2016 if (!(t->flags & SN_ERR_MASK))
2017 t->flags |= SN_ERR_CLITO;
2018 if (!(t->flags & SN_FINST_MASK)) {
2019 if (t->pend_pos)
2020 t->flags |= SN_FINST_Q;
2021 else if (s == SV_STCONN)
2022 t->flags |= SN_FINST_C;
2023 else
2024 t->flags |= SN_FINST_D;
2025 }
2026 return 1;
2027 }
2028
2029 if (req->l >= req->rlim - req->data) {
2030 /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002031 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002032 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02002033 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002034 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002035 }
2036 } else {
2037 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02002038 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
2039 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002040 if (!t->fe->clitimeout ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002041 (t->srv_state < SV_STDATA && t->be->srvtimeout))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002042 /* If the client has no timeout, or if the server not ready yet, and we
2043 * know for sure that it can expire, then it's cleaner to disable the
2044 * timeout on the client side so that too low values cannot make the
2045 * sessions abort too early.
2046 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002047 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002048 else
Willy Tarreau73de9892006-11-30 11:40:23 +01002049 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002050 }
2051 }
2052
2053 if ((rep->l == 0) ||
2054 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002055 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
2056 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002057 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002058 }
2059 } else {
2060 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002061 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
2062 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01002063 if (t->fe->clitimeout) {
2064 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002065 /* FIXME: to prevent the client from expiring read timeouts during writes,
2066 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002067 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002068 }
2069 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002070 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002071 }
2072 }
2073 return 0; /* other cases change nothing */
2074 }
2075 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002076 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002077 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002078 fd_delete(t->cli_fd);
2079 t->cli_state = CL_STCLOSE;
2080 if (!(t->flags & SN_ERR_MASK))
2081 t->flags |= SN_ERR_CLICL;
2082 if (!(t->flags & SN_FINST_MASK)) {
2083 if (t->pend_pos)
2084 t->flags |= SN_FINST_Q;
2085 else if (s == SV_STCONN)
2086 t->flags |= SN_FINST_C;
2087 else
2088 t->flags |= SN_FINST_D;
2089 }
2090 return 1;
2091 }
2092 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2093 && !(t->flags & SN_SELF_GEN)) {
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 return 1;
2098 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002099 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
2100 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002101 fd_delete(t->cli_fd);
2102 t->cli_state = CL_STCLOSE;
2103 if (!(t->flags & SN_ERR_MASK))
2104 t->flags |= SN_ERR_CLITO;
2105 if (!(t->flags & SN_FINST_MASK)) {
2106 if (t->pend_pos)
2107 t->flags |= SN_FINST_Q;
2108 else if (s == SV_STCONN)
2109 t->flags |= SN_FINST_C;
2110 else
2111 t->flags |= SN_FINST_D;
2112 }
2113 return 1;
2114 }
2115
2116 if (t->flags & SN_SELF_GEN) {
2117 produce_content(t);
2118 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002119 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002120 fd_delete(t->cli_fd);
2121 t->cli_state = CL_STCLOSE;
2122 return 1;
2123 }
2124 }
2125
2126 if ((rep->l == 0)
2127 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002128 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
2129 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002130 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002131 }
2132 } else {
2133 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002134 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
2135 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01002136 if (t->fe->clitimeout) {
2137 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002138 /* FIXME: to prevent the client from expiring read timeouts during writes,
2139 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002140 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002141 }
2142 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002143 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002144 }
2145 }
2146 return 0;
2147 }
2148 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002149 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002150 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002151 fd_delete(t->cli_fd);
2152 t->cli_state = CL_STCLOSE;
2153 if (!(t->flags & SN_ERR_MASK))
2154 t->flags |= SN_ERR_CLICL;
2155 if (!(t->flags & SN_FINST_MASK)) {
2156 if (t->pend_pos)
2157 t->flags |= SN_FINST_Q;
2158 else if (s == SV_STCONN)
2159 t->flags |= SN_FINST_C;
2160 else
2161 t->flags |= SN_FINST_D;
2162 }
2163 return 1;
2164 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002165 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
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 return 1;
2170 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002171 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
2172 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002173 fd_delete(t->cli_fd);
2174 t->cli_state = CL_STCLOSE;
2175 if (!(t->flags & SN_ERR_MASK))
2176 t->flags |= SN_ERR_CLITO;
2177 if (!(t->flags & SN_FINST_MASK)) {
2178 if (t->pend_pos)
2179 t->flags |= SN_FINST_Q;
2180 else if (s == SV_STCONN)
2181 t->flags |= SN_FINST_C;
2182 else
2183 t->flags |= SN_FINST_D;
2184 }
2185 return 1;
2186 }
2187 else if (req->l >= req->rlim - req->data) {
2188 /* no room to read more data */
2189
2190 /* FIXME-20050705: is it possible for a client to maintain a session
2191 * after the timeout by sending more data after it receives a close ?
2192 */
2193
Willy Tarreau2a429502006-10-15 14:52:29 +02002194 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002195 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02002196 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002197 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2199 }
2200 } else {
2201 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02002202 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
2203 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002204 if (t->fe->clitimeout)
2205 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002206 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002207 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002208 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2209 }
2210 }
2211 return 0;
2212 }
2213 else { /* CL_STCLOSE: nothing to do */
2214 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2215 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002216 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 +02002217 write(1, trash, len);
2218 }
2219 return 0;
2220 }
2221 return 0;
2222}
2223
2224
2225/*
2226 * manages the server FSM and its socket. It returns 1 if a state has changed
2227 * (and a resync may be needed), 0 else.
2228 */
2229int process_srv(struct session *t)
2230{
2231 int s = t->srv_state;
2232 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002233 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002234 struct buffer *req = t->req;
2235 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002236 int conn_err;
2237
2238#ifdef DEBUG_FULL
2239 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2240#endif
2241 //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s,
Willy Tarreau2a429502006-10-15 14:52:29 +02002242 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
2243 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002244 //);
2245 if (s == SV_STIDLE) {
2246 if (c == CL_STHEADERS)
2247 return 0; /* stay in idle, waiting for data to reach the client side */
2248 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2249 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002250 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002251 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002252 if (t->pend_pos)
2253 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2254 /* note that this must not return any error because it would be able to
2255 * overwrite the client_retnclose() output.
2256 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002257 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002258 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002259 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002260 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 +02002261
2262 return 1;
2263 }
2264 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002265 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002266 /* This connection is being tarpitted. The CLIENT side has
2267 * already set the connect expiration date to the right
2268 * timeout. We just have to check that it has not expired.
2269 */
2270 if (tv_cmp2_ms(&req->cex, &now) > 0)
2271 return 0;
2272
2273 /* We will set the queue timer to the time spent, just for
2274 * logging purposes. We fake a 500 server error, so that the
2275 * attacker will not suspect his connection has been tarpitted.
2276 * It will not cause trouble to the logs because we can exclude
2277 * the tarpitted connections by filtering on the 'PT' status flags.
2278 */
2279 tv_eternity(&req->cex);
2280 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2281 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002282 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002283 return 1;
2284 }
2285
Willy Tarreaubaaee002006-06-26 02:48:02 +02002286 /* Right now, we will need to create a connection to the server.
2287 * We might already have tried, and got a connection pending, in
2288 * which case we will not do anything till it's pending. It's up
2289 * to any other session to release it and wake us up again.
2290 */
2291 if (t->pend_pos) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002292 if (tv_cmp2_ms(&req->cex, &now) > 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002293 return 0;
2294 else {
2295 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002296 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002297 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2298 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002299 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002300 if (t->srv)
2301 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002302 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002303 return 1;
2304 }
2305 }
2306
2307 do {
2308 /* first, get a connection */
2309 if (srv_redispatch_connect(t))
2310 return t->srv_state != SV_STIDLE;
2311
2312 /* try to (re-)connect to the server, and fail if we expire the
2313 * number of retries.
2314 */
2315 if (srv_retryable_connect(t)) {
2316 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2317 return t->srv_state != SV_STIDLE;
2318 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002319 } while (1);
2320 }
2321 }
2322 else if (s == SV_STCONN) { /* connection in progress */
2323 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2324 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002325 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002326 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002327 fd_delete(t->srv_fd);
2328 if (t->srv)
2329 t->srv->cur_sess--;
2330
2331 /* note that this must not return any error because it would be able to
2332 * overwrite the client_retnclose() output.
2333 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002334 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002335 return 1;
2336 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002337 if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) {
2338 //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 +02002339 return 0; /* nothing changed */
2340 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002341 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002342 /* timeout, asynchronous connect error or first write error */
2343 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2344
2345 fd_delete(t->srv_fd);
2346 if (t->srv)
2347 t->srv->cur_sess--;
2348
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002349 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002350 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2351 else
2352 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2353
2354 /* ensure that we have enough retries left */
2355 if (srv_count_retry_down(t, conn_err))
2356 return 1;
2357
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002358 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002359 /* We're on our last chance, and the REDISP option was specified.
2360 * We will ignore cookie and force to balance or use the dispatcher.
2361 */
2362 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002363 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002364 task_wakeup(&rq, t->srv->queue_mgt);
2365
2366 if (t->srv)
2367 t->srv->failed_conns++;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002368 t->be->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002369
2370 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2371 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002372 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002373
2374 /* first, get a connection */
2375 if (srv_redispatch_connect(t))
2376 return t->srv_state != SV_STIDLE;
2377 }
2378
Willy Tarreaubaaee002006-06-26 02:48:02 +02002379 do {
2380 /* Now we will try to either reconnect to the same server or
2381 * connect to another server. If the connection gets queued
2382 * because all servers are saturated, then we will go back to
2383 * the SV_STIDLE state.
2384 */
2385 if (srv_retryable_connect(t)) {
2386 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2387 return t->srv_state != SV_STCONN;
2388 }
2389
2390 /* we need to redispatch the connection to another server */
2391 if (srv_redispatch_connect(t))
2392 return t->srv_state != SV_STCONN;
2393 } while (1);
2394 }
2395 else { /* no error or write 0 */
2396 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
2397
2398 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2399 if (req->l == 0) /* nothing to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002400 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002401 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402 } else /* need the right to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002403 MY_FD_SET(t->srv_fd, StaticWriteEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002404 if (t->be->srvtimeout) {
2405 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002406 /* FIXME: to prevent the server from expiring read timeouts during writes,
2407 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002408 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002409 }
2410 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002411 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002412 }
2413
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002414 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau2a429502006-10-15 14:52:29 +02002415 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002416 if (t->be->srvtimeout)
2417 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002418 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002419 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002420
2421 t->srv_state = SV_STDATA;
2422 if (t->srv)
2423 t->srv->cum_sess++;
2424 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2425
2426 /* if the user wants to log as soon as possible, without counting
2427 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002428 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002429 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002430 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002431 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002432#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002433 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002434 /* TCP splicing supported by both FE and BE */
2435 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2436 }
2437#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002438 }
2439 else {
2440 t->srv_state = SV_STHEADERS;
2441 if (t->srv)
2442 t->srv->cum_sess++;
2443 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002444 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2445 /* reset hdr_idx which was already initialized by the request.
2446 * right now, the http parser does it.
2447 * hdr_idx_init(&t->txn.hdr_idx);
2448 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002449 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002450 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002451 return 1;
2452 }
2453 }
2454 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002455 /*
2456 * Now parse the partial (or complete) lines.
2457 * We will check the response syntax, and also join multi-line
2458 * headers. An index of all the lines will be elaborated while
2459 * parsing.
2460 *
2461 * For the parsing, we use a 28 states FSM.
2462 *
2463 * Here is the information we currently have :
2464 * rep->data + req->som = beginning of response
2465 * rep->data + req->eoh = end of processed headers / start of current one
2466 * rep->data + req->eol = end of current header or line (LF or CRLF)
2467 * rep->lr = first non-visited byte
2468 * rep->r = end of data
2469 */
2470
2471 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002472 struct http_msg *msg = &txn->rsp;
2473 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002474
Willy Tarreaua15645d2007-03-18 16:22:39 +01002475 if (likely(rep->lr < rep->r))
2476 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002477
Willy Tarreaua15645d2007-03-18 16:22:39 +01002478 /* 1: we might have to print this header in debug mode */
2479 if (unlikely((global.mode & MODE_DEBUG) &&
2480 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2481 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2482 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002483
Willy Tarreaua15645d2007-03-18 16:22:39 +01002484 sol = rep->data + msg->som;
2485 eol = sol + msg->sl.rq.l;
2486 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002487
Willy Tarreaua15645d2007-03-18 16:22:39 +01002488 sol += hdr_idx_first_pos(&txn->hdr_idx);
2489 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002490
Willy Tarreaua15645d2007-03-18 16:22:39 +01002491 while (cur_idx) {
2492 eol = sol + txn->hdr_idx.v[cur_idx].len;
2493 debug_hdr("srvhdr", t, sol, eol);
2494 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2495 cur_idx = txn->hdr_idx.v[cur_idx].next;
2496 }
2497 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002498
Willy Tarreaubaaee002006-06-26 02:48:02 +02002499
Willy Tarreaua15645d2007-03-18 16:22:39 +01002500 if ((rep->l < rep->rlim - rep->data) && ! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2501 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
2502 * full. We cannot loop here since stream_sock_read will disable it only if
2503 * rep->l == rlim-data
2504 */
2505 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002506 if (t->be->srvtimeout)
2507 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002508 else
2509 tv_eternity(&rep->rex);
2510 }
2511
2512
2513 /*
2514 * Now we quickly check if we have found a full valid response.
2515 * If not so, we check the FD and buffer states before leaving.
2516 * A full response is indicated by the fact that we have seen
2517 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2518 * responses are checked first.
2519 *
2520 * Depending on whether the client is still there or not, we
2521 * may send an error response back or not. Note that normally
2522 * we should only check for HTTP status there, and check I/O
2523 * errors somewhere else.
2524 */
2525
2526 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2527
2528 /* Invalid response, or read error or write error */
2529 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2530 (req->flags & BF_WRITE_ERROR) ||
2531 (rep->flags & BF_READ_ERROR))) {
2532 tv_eternity(&rep->rex);
2533 tv_eternity(&req->wex);
2534 fd_delete(t->srv_fd);
2535 if (t->srv) {
2536 t->srv->cur_sess--;
2537 t->srv->failed_resp++;
2538 }
2539 t->be->failed_resp++;
2540 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002541 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002542 client_return(t, error_message(t, HTTP_ERR_502));
2543 if (!(t->flags & SN_ERR_MASK))
2544 t->flags |= SN_ERR_SRVCL;
2545 if (!(t->flags & SN_FINST_MASK))
2546 t->flags |= SN_FINST_H;
2547 /* We used to have a free connection slot. Since we'll never use it,
2548 * we have to inform the server that it may be used by another session.
2549 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002550 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002551 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002552
Willy Tarreaua15645d2007-03-18 16:22:39 +01002553 return 1;
2554 }
2555
2556 /* end of client write or end of server read.
2557 * since we are in header mode, if there's no space left for headers, we
2558 * won't be able to free more later, so the session will never terminate.
2559 */
2560 else if (unlikely(rep->flags & BF_READ_NULL ||
2561 c == CL_STSHUTW || c == CL_STCLOSE ||
2562 rep->l >= rep->rlim - rep->data)) {
2563 MY_FD_CLR(t->srv_fd, StaticReadEvent);
2564 tv_eternity(&rep->rex);
2565 shutdown(t->srv_fd, SHUT_RD);
2566 t->srv_state = SV_STSHUTR;
2567 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2568 return 1;
2569 }
2570
2571 /* read timeout : return a 504 to the client.
2572 */
2573 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticReadEvent) &&
2574 tv_cmp2_ms(&rep->rex, &now) <= 0)) {
2575 tv_eternity(&rep->rex);
2576 tv_eternity(&req->wex);
2577 fd_delete(t->srv_fd);
2578 if (t->srv) {
2579 t->srv->cur_sess--;
2580 t->srv->failed_resp++;
2581 }
2582 t->be->failed_resp++;
2583 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002584 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002585 client_return(t, error_message(t, HTTP_ERR_504));
2586 if (!(t->flags & SN_ERR_MASK))
2587 t->flags |= SN_ERR_SRVTO;
2588 if (!(t->flags & SN_FINST_MASK))
2589 t->flags |= SN_FINST_H;
2590 /* We used to have a free connection slot. Since we'll never use it,
2591 * we have to inform the server that it may be used by another session.
2592 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002593 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002594 task_wakeup(&rq, t->srv->queue_mgt);
2595 return 1;
2596 }
2597
2598 /* last client read and buffer empty */
2599 /* FIXME!!! here, we don't want to switch to SHUTW if the
2600 * client shuts read too early, because we may still have
2601 * some work to do on the headers.
2602 * The side-effect is that if the client completely closes its
2603 * connection during SV_STHEADER, the connection to the server
2604 * is kept until a response comes back or the timeout is reached.
2605 */
2606 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2607 (req->l == 0))) {
2608 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2609 tv_eternity(&req->wex);
2610
2611 /* We must ensure that the read part is still
2612 * alive when switching to shutw */
2613 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002614 if (t->be->srvtimeout)
2615 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002616
2617 shutdown(t->srv_fd, SHUT_WR);
2618 t->srv_state = SV_STSHUTW;
2619 return 1;
2620 }
2621
2622 /* write timeout */
2623 /* FIXME!!! here, we don't want to switch to SHUTW if the
2624 * client shuts read too early, because we may still have
2625 * some work to do on the headers.
2626 */
2627 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticWriteEvent) &&
2628 tv_cmp2_ms(&req->wex, &now) <= 0)) {
2629 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2630 tv_eternity(&req->wex);
2631 shutdown(t->srv_fd, SHUT_WR);
2632 /* We must ensure that the read part is still alive
2633 * when switching to shutw */
2634 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002635 if (t->be->srvtimeout)
2636 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002637
2638 t->srv_state = SV_STSHUTW;
2639 if (!(t->flags & SN_ERR_MASK))
2640 t->flags |= SN_ERR_SRVTO;
2641 if (!(t->flags & SN_FINST_MASK))
2642 t->flags |= SN_FINST_H;
2643 return 1;
2644 }
2645
2646 /*
2647 * And now the non-error cases.
2648 */
2649
2650 /* Data remaining in the request buffer.
2651 * This happens during the first pass here, and during
2652 * long posts.
2653 */
2654 else if (likely(req->l)) {
2655 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2656 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002657 if (t->be->srvtimeout) {
2658 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002659 /* FIXME: to prevent the server from expiring read timeouts during writes,
2660 * we refresh it. */
2661 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002662 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002663 else
2664 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002665 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002666 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002667
Willy Tarreaua15645d2007-03-18 16:22:39 +01002668 /* nothing left in the request buffer */
2669 else {
2670 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2671 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002672 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002673 }
2674 }
2675
2676 return t->srv_state != SV_STHEADERS;
2677 }
2678
2679
2680 /*****************************************************************
2681 * More interesting part now : we know that we have a complete *
2682 * response which at least looks like HTTP. We have an indicator *
2683 * of each header's length, so we can parse them quickly. *
2684 ****************************************************************/
2685
2686 /*
2687 * 1: get the status code and check for cacheability.
2688 */
2689
2690 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002691 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002692
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002693 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002694 case 200:
2695 case 203:
2696 case 206:
2697 case 300:
2698 case 301:
2699 case 410:
2700 /* RFC2616 @13.4:
2701 * "A response received with a status code of
2702 * 200, 203, 206, 300, 301 or 410 MAY be stored
2703 * by a cache (...) unless a cache-control
2704 * directive prohibits caching."
2705 *
2706 * RFC2616 @9.5: POST method :
2707 * "Responses to this method are not cacheable,
2708 * unless the response includes appropriate
2709 * Cache-Control or Expires header fields."
2710 */
2711 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002712 unlikely(t->be->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002713 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002714 break;
2715 default:
2716 break;
2717 }
2718
2719 /*
2720 * 2: we may need to capture headers
2721 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002722 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002723 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002724 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002725
2726 /*
2727 * 3: we will have to evaluate the filters.
2728 * As opposed to version 1.2, now they will be evaluated in the
2729 * filters order and not in the header order. This means that
2730 * each filter has to be validated among all headers.
2731 *
2732 * Filters are tried with ->be first, then with ->fe if it is
2733 * different from ->be.
2734 */
2735
2736 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2737
2738 cur_proxy = t->be;
2739 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002740 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002741
2742 /* try headers filters */
2743 if (rule_set->rsp_exp != NULL) {
2744 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2745 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002746 if (t->srv) {
2747 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002748 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002749 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002750 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002751 return_srv_prx_502:
2752 tv_eternity(&rep->rex);
2753 tv_eternity(&req->wex);
2754 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002755 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002756 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002757 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002758 if (!(t->flags & SN_ERR_MASK))
2759 t->flags |= SN_ERR_PRXCOND;
2760 if (!(t->flags & SN_FINST_MASK))
2761 t->flags |= SN_FINST_H;
2762 /* We used to have a free connection slot. Since we'll never use it,
2763 * we have to inform the server that it may be used by another session.
2764 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002765 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002766 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002767 return 1;
2768 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002769 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002770
Willy Tarreaua15645d2007-03-18 16:22:39 +01002771 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002772 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002773 if (t->srv) {
2774 t->srv->cur_sess--;
2775 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002776 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002777 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002778 goto return_srv_prx_502;
2779 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002780
Willy Tarreaua15645d2007-03-18 16:22:39 +01002781 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002782 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002783 !(t->flags & SN_CONN_CLOSED)) {
2784 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002785 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002786 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002787
Willy Tarreaua15645d2007-03-18 16:22:39 +01002788 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2789 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002790
Willy Tarreaua15645d2007-03-18 16:22:39 +01002791 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2792 cur_hdr = &txn->hdr_idx.v[cur_idx];
2793 cur_ptr = cur_next;
2794 cur_end = cur_ptr + cur_hdr->len;
2795 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002796
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002797 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2798 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002799 /* 3 possibilities :
2800 * - we have already set Connection: close,
2801 * so we remove this line.
2802 * - we have not yet set Connection: close,
2803 * but this line indicates close. We leave
2804 * it untouched and set the flag.
2805 * - we have not yet set Connection: close,
2806 * and this line indicates non-close. We
2807 * replace it.
2808 */
2809 if (t->flags & SN_CONN_CLOSED) {
2810 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2811 txn->rsp.eoh += delta;
2812 cur_next += delta;
2813 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2814 txn->hdr_idx.used--;
2815 cur_hdr->len = 0;
2816 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002817 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2818 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2819 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002820 cur_next += delta;
2821 cur_hdr->len += delta;
2822 txn->rsp.eoh += delta;
2823 }
2824 t->flags |= SN_CONN_CLOSED;
2825 }
2826 }
2827 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002828 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002829 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002830
Willy Tarreaua15645d2007-03-18 16:22:39 +01002831 /* add response headers from the rule sets in the same order */
2832 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002833 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2834 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002835 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002836 }
2837
Willy Tarreaua15645d2007-03-18 16:22:39 +01002838 /* check whether we're already working on the frontend */
2839 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002840 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002841 cur_proxy = t->fe;
2842 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002843
Willy Tarreaua15645d2007-03-18 16:22:39 +01002844 /*
2845 * 4: check for server cookie.
2846 */
2847 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002848
Willy Tarreaua15645d2007-03-18 16:22:39 +01002849 /*
2850 * 5: add server cookie in the response if needed
2851 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002852 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2853 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002854 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002855
Willy Tarreaua15645d2007-03-18 16:22:39 +01002856 /* the server is known, it's not the one the client requested, we have to
2857 * insert a set-cookie here, except if we want to insert only on POST
2858 * requests and this one isn't. Note that servers which don't have cookies
2859 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002860 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002861 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002862 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01002863 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002864
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002865 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2866 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002867 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002868 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002869
Willy Tarreaua15645d2007-03-18 16:22:39 +01002870 /* Here, we will tell an eventual cache on the client side that we don't
2871 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2872 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2873 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2874 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002875 if (t->be->options & PR_O_COOK_NOC) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002876 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2877 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002878 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002879 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002880 }
2881
2882
2883 /*
2884 * 6: check for cache-control or pragma headers.
2885 */
2886 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002887
Willy Tarreaubaaee002006-06-26 02:48:02 +02002888
Willy Tarreaua15645d2007-03-18 16:22:39 +01002889 /*
2890 * 7: check if result will be cacheable with a cookie.
2891 * We'll block the response if security checks have caught
2892 * nasty things such as a cacheable cookie.
2893 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002894 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2895 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002896 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002897
Willy Tarreaua15645d2007-03-18 16:22:39 +01002898 /* we're in presence of a cacheable response containing
2899 * a set-cookie header. We'll block it as requested by
2900 * the 'checkcache' option, and send an alert.
2901 */
2902 if (t->srv) {
2903 t->srv->cur_sess--;
2904 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002905 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002906 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002907
Willy Tarreaua15645d2007-03-18 16:22:39 +01002908 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002909 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002910 send_log(t->be, LOG_ALERT,
2911 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002912 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01002913 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002914 }
2915
Willy Tarreaua15645d2007-03-18 16:22:39 +01002916 /*
2917 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002918 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002919 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002920 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002921 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002922 if ((unlikely(msg->sl.st.v_l != 8) ||
2923 unlikely(req->data[msg->som + 7] != '0')) &&
2924 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002925 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002926 goto return_bad_resp;
2927 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002928 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002929
Willy Tarreaubaaee002006-06-26 02:48:02 +02002930
Willy Tarreaua15645d2007-03-18 16:22:39 +01002931 /*************************************************************
2932 * OK, that's finished for the headers. We have done what we *
2933 * could. Let's switch to the DATA state. *
2934 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002935
Willy Tarreaua15645d2007-03-18 16:22:39 +01002936 t->srv_state = SV_STDATA;
2937 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
2938 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
2939
2940 /* client connection already closed or option 'forceclose' required :
2941 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002942 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002943 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002944 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002945 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002946 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002947
2948 /* We must ensure that the read part is still alive when switching
2949 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002950 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002951 if (t->be->srvtimeout)
2952 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002953
Willy Tarreaua15645d2007-03-18 16:22:39 +01002954 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002955 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002956 }
2957
Willy Tarreaua15645d2007-03-18 16:22:39 +01002958#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002959 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002960 /* TCP splicing supported by both FE and BE */
2961 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002962 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002963#endif
2964 /* if the user wants to log as soon as possible, without counting
2965 bytes from the server, then this is the right moment. */
2966 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2967 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002968 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02002969 if (t->fe->to_log & LW_REQ)
2970 http_sess_log(t);
2971 else
2972 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002973 }
2974
Willy Tarreaua15645d2007-03-18 16:22:39 +01002975 /* Note: we must not try to cheat by jumping directly to DATA,
2976 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002977 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002978
2979 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002980 }
2981 else if (s == SV_STDATA) {
2982 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002983 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002984 tv_eternity(&rep->rex);
2985 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002986 fd_delete(t->srv_fd);
2987 if (t->srv) {
2988 t->srv->cur_sess--;
2989 t->srv->failed_resp++;
2990 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002991 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002992 t->srv_state = SV_STCLOSE;
2993 if (!(t->flags & SN_ERR_MASK))
2994 t->flags |= SN_ERR_SRVCL;
2995 if (!(t->flags & SN_FINST_MASK))
2996 t->flags |= SN_FINST_D;
2997 /* We used to have a free connection slot. Since we'll never use it,
2998 * we have to inform the server that it may be used by another session.
2999 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003000 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003001 task_wakeup(&rq, t->srv->queue_mgt);
3002
3003 return 1;
3004 }
3005 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003006 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003007 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003008 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003009 shutdown(t->srv_fd, SHUT_RD);
3010 t->srv_state = SV_STSHUTR;
3011 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3012 return 1;
3013 }
3014 /* end of client read and no more data to send */
3015 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003016 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003017 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003018 shutdown(t->srv_fd, SHUT_WR);
3019 /* We must ensure that the read part is still alive when switching
3020 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02003021 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003022 if (t->be->srvtimeout)
3023 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003024
3025 t->srv_state = SV_STSHUTW;
3026 return 1;
3027 }
3028 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02003029 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003030 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003031 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032 shutdown(t->srv_fd, SHUT_RD);
3033 t->srv_state = SV_STSHUTR;
3034 if (!(t->flags & SN_ERR_MASK))
3035 t->flags |= SN_ERR_SRVTO;
3036 if (!(t->flags & SN_FINST_MASK))
3037 t->flags |= SN_FINST_D;
3038 return 1;
3039 }
3040 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02003041 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003042 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003043 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003044 shutdown(t->srv_fd, SHUT_WR);
3045 /* We must ensure that the read part is still alive when switching
3046 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02003047 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003048 if (t->be->srvtimeout)
3049 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003050 t->srv_state = SV_STSHUTW;
3051 if (!(t->flags & SN_ERR_MASK))
3052 t->flags |= SN_ERR_SRVTO;
3053 if (!(t->flags & SN_FINST_MASK))
3054 t->flags |= SN_FINST_D;
3055 return 1;
3056 }
3057
3058 /* recompute request time-outs */
3059 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003060 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
3061 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003062 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003063 }
3064 }
3065 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau2a429502006-10-15 14:52:29 +02003066 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
3067 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003068 if (t->be->srvtimeout) {
3069 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003070 /* FIXME: to prevent the server from expiring read timeouts during writes,
3071 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003072 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003073 }
3074 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003075 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003076 }
3077 }
3078
3079 /* recompute response time-outs */
3080 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02003081 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3082 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003083 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003084 }
3085 }
3086 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02003087 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3088 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003089 if (t->be->srvtimeout)
3090 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003091 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003092 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003093 }
3094 }
3095
3096 return 0; /* other cases change nothing */
3097 }
3098 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003099 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003100 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003101 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003102 fd_delete(t->srv_fd);
3103 if (t->srv) {
3104 t->srv->cur_sess--;
3105 t->srv->failed_resp++;
3106 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003107 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003108 //close(t->srv_fd);
3109 t->srv_state = SV_STCLOSE;
3110 if (!(t->flags & SN_ERR_MASK))
3111 t->flags |= SN_ERR_SRVCL;
3112 if (!(t->flags & SN_FINST_MASK))
3113 t->flags |= SN_FINST_D;
3114 /* We used to have a free connection slot. Since we'll never use it,
3115 * we have to inform the server that it may be used by another session.
3116 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003117 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003118 task_wakeup(&rq, t->srv->queue_mgt);
3119
3120 return 1;
3121 }
3122 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003123 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003124 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003125 fd_delete(t->srv_fd);
3126 if (t->srv)
3127 t->srv->cur_sess--;
3128 //close(t->srv_fd);
3129 t->srv_state = SV_STCLOSE;
3130 /* We used to have a free connection slot. Since we'll never use it,
3131 * we have to inform the server that it may be used by another session.
3132 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003133 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003134 task_wakeup(&rq, t->srv->queue_mgt);
3135
3136 return 1;
3137 }
Willy Tarreaud7971282006-07-29 18:36:34 +02003138 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003139 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003140 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003141 fd_delete(t->srv_fd);
3142 if (t->srv)
3143 t->srv->cur_sess--;
3144 //close(t->srv_fd);
3145 t->srv_state = SV_STCLOSE;
3146 if (!(t->flags & SN_ERR_MASK))
3147 t->flags |= SN_ERR_SRVTO;
3148 if (!(t->flags & SN_FINST_MASK))
3149 t->flags |= SN_FINST_D;
3150 /* We used to have a free connection slot. Since we'll never use it,
3151 * we have to inform the server that it may be used by another session.
3152 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003153 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003154 task_wakeup(&rq, t->srv->queue_mgt);
3155
3156 return 1;
3157 }
3158 else if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003159 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
3160 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003161 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003162 }
3163 }
3164 else { /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02003165 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
3166 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003167 if (t->be->srvtimeout) {
3168 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003169 /* FIXME: to prevent the server from expiring read timeouts during writes,
3170 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003171 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003172 }
3173 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003174 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003175 }
3176 }
3177 return 0;
3178 }
3179 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003180 if (rep->flags & BF_READ_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003181 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003182 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003183 fd_delete(t->srv_fd);
3184 if (t->srv) {
3185 t->srv->cur_sess--;
3186 t->srv->failed_resp++;
3187 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003188 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003189 //close(t->srv_fd);
3190 t->srv_state = SV_STCLOSE;
3191 if (!(t->flags & SN_ERR_MASK))
3192 t->flags |= SN_ERR_SRVCL;
3193 if (!(t->flags & SN_FINST_MASK))
3194 t->flags |= SN_FINST_D;
3195 /* We used to have a free connection slot. Since we'll never use it,
3196 * we have to inform the server that it may be used by another session.
3197 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003198 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003199 task_wakeup(&rq, t->srv->queue_mgt);
3200
3201 return 1;
3202 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003203 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003204 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003205 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003206 fd_delete(t->srv_fd);
3207 if (t->srv)
3208 t->srv->cur_sess--;
3209 //close(t->srv_fd);
3210 t->srv_state = SV_STCLOSE;
3211 /* We used to have a free connection slot. Since we'll never use it,
3212 * we have to inform the server that it may be used by another session.
3213 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003214 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003215 task_wakeup(&rq, t->srv->queue_mgt);
3216
3217 return 1;
3218 }
Willy Tarreaud7971282006-07-29 18:36:34 +02003219 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003220 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003221 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003222 fd_delete(t->srv_fd);
3223 if (t->srv)
3224 t->srv->cur_sess--;
3225 //close(t->srv_fd);
3226 t->srv_state = SV_STCLOSE;
3227 if (!(t->flags & SN_ERR_MASK))
3228 t->flags |= SN_ERR_SRVTO;
3229 if (!(t->flags & SN_FINST_MASK))
3230 t->flags |= SN_FINST_D;
3231 /* We used to have a free connection slot. Since we'll never use it,
3232 * we have to inform the server that it may be used by another session.
3233 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003234 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003235 task_wakeup(&rq, t->srv->queue_mgt);
3236
3237 return 1;
3238 }
3239 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02003240 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3241 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003242 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003243 }
3244 }
3245 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02003246 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3247 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003248 if (t->be->srvtimeout)
3249 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003250 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003251 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003252 }
3253 }
3254 return 0;
3255 }
3256 else { /* SV_STCLOSE : nothing to do */
3257 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3258 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003259 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003260 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003261 write(1, trash, len);
3262 }
3263 return 0;
3264 }
3265 return 0;
3266}
3267
3268
3269/*
3270 * Produces data for the session <s> depending on its source. Expects to be
3271 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3272 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3273 * session, which it uses to keep on being called when there is free space in
3274 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3275 * if it changes the session state from CL_STSHUTR, otherwise 0.
3276 */
3277int produce_content(struct session *s)
3278{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003279 if (s->data_source == DATA_SRC_NONE) {
3280 s->flags &= ~SN_SELF_GEN;
3281 return 1;
3282 }
3283 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003284 /* dump server statistics */
3285 return produce_content_stats(s);
3286 }
3287 else {
3288 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003289 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003290 client_retnclose(s, error_message(s, HTTP_ERR_500));
3291 if (!(s->flags & SN_ERR_MASK))
3292 s->flags |= SN_ERR_PRXCOND;
3293 if (!(s->flags & SN_FINST_MASK))
3294 s->flags |= SN_FINST_R;
3295 s->flags &= ~SN_SELF_GEN;
3296 return 1;
3297 }
3298}
3299
3300
3301/*
3302 * Produces statistics data for the session <s>. Expects to be called with
3303 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3304 * flag from the session, which it uses to keep on being called when there is
3305 * free space in the buffer, of simply by letting an empty buffer upon return.
3306 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3307 */
3308int produce_content_stats(struct session *s)
3309{
3310 struct buffer *rep = s->rep;
3311 struct proxy *px;
3312 struct chunk msg;
3313 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003314
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003315 msg.len = 0;
3316 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003317
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003318 switch (s->data_state) {
3319 case DATA_ST_INIT:
3320 /* the function had not been called yet */
3321 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003322
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003323 chunk_printf(&msg, sizeof(trash),
3324 "HTTP/1.0 200 OK\r\n"
3325 "Cache-Control: no-cache\r\n"
3326 "Connection: close\r\n"
3327 "Content-Type: text/html\r\n"
3328 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003329
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003330 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003331 client_retnclose(s, &msg); // send the start of the response.
3332 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003333
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003334 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3335 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3336 if (!(s->flags & SN_FINST_MASK))
3337 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003338
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003339 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003340 /* that's all we return in case of HEAD request */
3341 s->data_state = DATA_ST_FIN;
3342 s->flags &= ~SN_SELF_GEN;
3343 return 1;
3344 }
3345
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003346 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3347 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003348
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003349 case DATA_ST_HEAD:
3350 /* WARNING! This must fit in the first buffer !!! */
3351 chunk_printf(&msg, sizeof(trash),
3352 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3353 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3354 "<style type=\"text/css\"><!--\n"
3355 "body {"
3356 " font-family: helvetica, arial;"
3357 " font-size: 12px;"
3358 " font-weight: normal;"
3359 " color: black;"
3360 " background: white;"
3361 "}\n"
3362 "th,td {"
3363 " font-size: 0.8em;"
3364 " align: center;"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003365 "}\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003366 "h1 {"
3367 " font-size: xx-large;"
3368 " margin-bottom: 0.5em;"
3369 "}\n"
3370 "h2 {"
3371 " font-family: helvetica, arial;"
3372 " font-size: x-large;"
3373 " font-weight: bold;"
3374 " font-style: italic;"
3375 " color: #6020a0;"
3376 " margin-top: 0em;"
3377 " margin-bottom: 0em;"
3378 "}\n"
3379 "h3 {"
3380 " font-family: helvetica, arial;"
3381 " font-size: 16px;"
3382 " font-weight: bold;"
3383 " color: #b00040;"
3384 " background: #e8e8d0;"
3385 " margin-top: 0em;"
3386 " margin-bottom: 0em;"
3387 "}\n"
3388 "li {"
3389 " margin-top: 0.25em;"
3390 " margin-right: 2em;"
3391 "}\n"
3392 ".hr {margin-top: 0.25em;"
3393 " border-color: black;"
3394 " border-bottom-style: solid;"
3395 "}\n"
3396 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3397 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3398 ".total {background: #20D0D0;color: #ffff80;}\n"
3399 ".frontend {background: #e8e8d0;}\n"
3400 ".backend {background: #e8e8d0;}\n"
3401 ".active0 {background: #ff9090;}\n"
3402 ".active1 {background: #ffd020;}\n"
3403 ".active2 {background: #ffffa0;}\n"
3404 ".active3 {background: #c0ffc0;}\n"
3405 ".active4 {background: #e0e0e0;}\n"
3406 ".backup0 {background: #ff9090;}\n"
3407 ".backup1 {background: #ff80ff;}\n"
3408 ".backup2 {background: #c060ff;}\n"
3409 ".backup3 {background: #b0d0ff;}\n"
3410 ".backup4 {background: #e0e0e0;}\n"
3411 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003412 "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 +01003413 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3414 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3415 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3416 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3417 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003418 "-->\n"
3419 "</style></head>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003420
3421 if (buffer_write_chunk(rep, &msg) != 0)
3422 return 0;
3423
3424 s->data_state = DATA_ST_INFO;
3425 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003426
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003427 case DATA_ST_INFO:
3428 up = (now.tv_sec - start_date.tv_sec);
3429
3430 /* WARNING! this has to fit the first packet too.
3431 * We are around 3.5 kB, add adding entries will
3432 * become tricky if we want to support 4kB buffers !
3433 */
3434 chunk_printf(&msg, sizeof(trash),
3435 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3436 PRODUCT_NAME "</a></h1>\n"
3437 "<h2>Statistics Report for pid %d</h2>\n"
3438 "<hr width=\"100%%\" class=\"hr\">\n"
3439 "<h3>&gt; General process information</h3>\n"
3440 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3441 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3442 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3443 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3444 "<b>maxsock = </b> %d<br>\n"
3445 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3446 "</td><td align=\"center\" nowrap>\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003447 "<table class=\"lgd\"><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003448 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3449 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003450 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003451 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3452 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003453 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003454 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3455 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003456 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003457 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3458 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3459 "</tr></table>\n"
3460 "</td>"
3461 "<td align=\"left\" nowrap width=\"1%%\">"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003462 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
3463 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3464 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3465 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003466 "</ul>"
3467 "</td>"
3468 "</tr></table>\n"
3469 "",
3470 pid, pid, global.nbproc,
3471 up / 86400, (up % 86400) / 3600,
3472 (up % 3600) / 60, (up % 60),
3473 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3474 global.rlimit_memmax ? " MB" : "",
3475 global.rlimit_nofile,
3476 global.maxsock,
3477 global.maxconn,
3478 actconn
3479 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003480
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003481 if (buffer_write_chunk(rep, &msg) != 0)
3482 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003483
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003484 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003485
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003486 s->data_ctx.stats.px = proxy;
3487 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3488 s->data_state = DATA_ST_LIST;
3489 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003490
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003491 case DATA_ST_LIST:
3492 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003493 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003494 px = s->data_ctx.stats.px;
3495 /* skip the disabled proxies and non-networked ones */
3496 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3497 if (produce_content_stats_proxy(s, px) == 0)
3498 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003499
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003500 s->data_ctx.stats.px = px->next;
3501 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3502 }
3503 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003504
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003505 s->data_state = DATA_ST_END;
3506 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003507
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003508 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003509 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003510 if (buffer_write_chunk(rep, &msg) != 0)
3511 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003512
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003513 s->data_state = DATA_ST_FIN;
3514 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003515
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003516 case DATA_ST_FIN:
3517 s->flags &= ~SN_SELF_GEN;
3518 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003519
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003520 default:
3521 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003522 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003523 client_retnclose(s, error_message(s, HTTP_ERR_500));
3524 if (!(s->flags & SN_ERR_MASK))
3525 s->flags |= SN_ERR_PRXCOND;
3526 if (!(s->flags & SN_FINST_MASK))
3527 s->flags |= SN_FINST_R;
3528 s->flags &= ~SN_SELF_GEN;
3529 return 1;
3530 }
3531}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003532
Willy Tarreaubaaee002006-06-26 02:48:02 +02003533
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003534/*
3535 * Dumps statistics for a proxy.
3536 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3537 * ot non-zero if everything completed.
3538 */
3539int produce_content_stats_proxy(struct session *s, struct proxy *px)
3540{
3541 struct buffer *rep = s->rep;
3542 struct server *sv;
3543 struct chunk msg;
3544
3545 msg.len = 0;
3546 msg.str = trash;
3547
3548 switch (s->data_ctx.stats.px_st) {
3549 case DATA_ST_PX_INIT:
3550 /* we are on a new proxy */
3551
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003552 if (s->be->uri_auth && s->be->uri_auth->scope) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003553 /* we have a limited scope, we have to check the proxy name */
3554 struct stat_scope *scope;
3555 int len;
3556
3557 len = strlen(px->id);
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003558 scope = s->be->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003559
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003560 while (scope) {
3561 /* match exact proxy name */
3562 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3563 break;
3564
3565 /* match '.' which means 'self' proxy */
3566 if (!strcmp(scope->px_id, ".") && px == s->fe)
3567 break;
3568 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003569 }
3570
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003571 /* proxy name not found : don't dump anything */
3572 if (scope == NULL)
3573 return 1;
3574 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003575
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003576 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3577 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003578
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003579 case DATA_ST_PX_TH:
3580 /* print a new table */
3581 chunk_printf(&msg, sizeof(trash),
3582 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3583 "<tr align=\"center\" class=\"titre\">"
3584 "<th colspan=2 class=\"pxname\">%s</th>"
3585 "<th colspan=18 class=\"empty\"></th>"
3586 "</tr>\n"
3587 "<tr align=\"center\" class=\"titre\">"
3588 "<th rowspan=2></th>"
3589 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3590 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3591 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3592 "</tr>\n"
3593 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003594 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3595 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003596 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3597 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3598 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3599 "",
3600 px->id);
3601
3602 if (buffer_write_chunk(rep, &msg) != 0)
3603 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003604
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003605 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3606 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003607
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003608 case DATA_ST_PX_FE:
3609 /* print the frontend */
3610 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003611 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003612 /* name, queue */
3613 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3614 /* sessions : current, max, limit, cumul. */
3615 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3616 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003617 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003618 /* denied: req, resp */
3619 "<td align=right>%d</td><td align=right>%d</td>"
3620 /* errors : request, connect, response */
3621 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3622 /* server status : reflect backend status */
3623 "<td align=center>%s</td>"
3624 /* rest of server: nothing */
3625 "<td align=center colspan=5></td></tr>"
3626 "",
3627 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003628 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003629 px->denied_req, px->denied_resp,
3630 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003631 px->state == PR_STRUN ? "OPEN" :
3632 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003633
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003634 if (buffer_write_chunk(rep, &msg) != 0)
3635 return 0;
3636 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003637
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003638 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3639 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3640 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003641
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003642 case DATA_ST_PX_SV:
3643 /* stats.sv has been initialized above */
3644 while (s->data_ctx.stats.sv != NULL) {
3645 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3646 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003647
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003648 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003649
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003650 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3651 if (!(sv->state & SRV_CHECKED))
3652 sv_state = 4;
3653 else if (sv->state & SRV_RUNNING)
3654 if (sv->health == sv->rise + sv->fall - 1)
3655 sv_state = 3; /* UP */
3656 else
3657 sv_state = 2; /* going down */
3658 else
3659 if (sv->health)
3660 sv_state = 1; /* going up */
3661 else
3662 sv_state = 0; /* DOWN */
3663
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003664 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003665 /* name */
3666 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3667 /* queue : current, max */
3668 "<td align=right>%d</td><td align=right>%d</td>"
3669 /* sessions : current, max, limit, cumul */
3670 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3671 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003672 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003673 /* denied: req, resp */
3674 "<td align=right></td><td align=right>%d</td>"
3675 /* errors : request, connect, response */
3676 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3677 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003678 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003679 sv_state, sv->id,
3680 sv->nbpend, sv->nbpend_max,
3681 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003682 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003683 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003684 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003685
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003686 /* status */
3687 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3688 chunk_printf(&msg, sizeof(trash),
3689 srv_hlt_st[sv_state],
3690 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3691 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3692
Willy Tarreau128e9542007-01-01 22:01:43 +01003693 chunk_printf(&msg, sizeof(trash),
3694 /* weight */
3695 "</td><td>%d</td>"
3696 /* act, bck */
3697 "<td>%s</td><td>%s</td>"
3698 "",
Willy Tarreau417fae02007-03-25 21:16:40 +02003699 sv->uweight,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003700 (sv->state & SRV_BACKUP) ? "-" : "Y",
3701 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003702
3703 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003704 if (sv->state & SRV_CHECKED)
3705 chunk_printf(&msg, sizeof(trash),
3706 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3707 sv->failed_checks, sv->down_trans);
3708 else
3709 chunk_printf(&msg, sizeof(trash),
3710 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003711
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003712 if (buffer_write_chunk(rep, &msg) != 0)
3713 return 0;
3714
3715 s->data_ctx.stats.sv = sv->next;
3716 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003717
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003718 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3719 /* fall through */
3720
3721 case DATA_ST_PX_BE:
3722 /* print the backend */
3723 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003724 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003725 /* name */
3726 "<tr align=center class=\"backend\"><td>Backend</td>"
3727 /* queue : current, max */
3728 "<td align=right>%d</td><td align=right>%d</td>"
3729 /* sessions : current, max, limit, cumul. */
3730 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3731 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003732 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003733 /* denied: req, resp */
3734 "<td align=right>%d</td><td align=right>%d</td>"
3735 /* errors : request, connect, response */
3736 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3737 /* server status : reflect backend status (up/down) : we display UP
3738 * if the backend has known working servers or if it has no server at
3739 * all (eg: for stats). Tthen we display the total weight, number of
3740 * active and backups. */
3741 "<td align=center>%s</td><td align=center>%d</td>"
3742 "<td align=center>%d</td><td align=center>%d</td>"
3743 /* rest of server: nothing */
3744 "<td align=center colspan=2></td></tr>"
3745 "",
3746 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3747 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003748 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003749 px->denied_req, px->denied_resp,
3750 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003751 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3752 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003753
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003754 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003755 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003756 }
3757
3758 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3759 /* fall through */
3760
3761 case DATA_ST_PX_END:
3762 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3763
3764 if (buffer_write_chunk(rep, &msg) != 0)
3765 return 0;
3766
3767 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3768 /* fall through */
3769
3770 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003771 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003772
3773 default:
3774 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003775 return 1;
3776 }
3777}
3778
3779
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003780/* Iterate the same filter through all request headers.
3781 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003782 * Since it can manage the switch to another backend, it updates the per-proxy
3783 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003784 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003785int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003786{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003787 char term;
3788 char *cur_ptr, *cur_end, *cur_next;
3789 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003790 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003791 struct hdr_idx_elem *cur_hdr;
3792 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003794 last_hdr = 0;
3795
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003796 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003797 old_idx = 0;
3798
3799 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003800 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003801 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003802 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003803 (exp->action == ACT_ALLOW ||
3804 exp->action == ACT_DENY ||
3805 exp->action == ACT_TARPIT))
3806 return 0;
3807
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003808 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003809 if (!cur_idx)
3810 break;
3811
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003812 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003813 cur_ptr = cur_next;
3814 cur_end = cur_ptr + cur_hdr->len;
3815 cur_next = cur_end + cur_hdr->cr + 1;
3816
3817 /* Now we have one header between cur_ptr and cur_end,
3818 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003819 */
3820
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003821 /* The annoying part is that pattern matching needs
3822 * that we modify the contents to null-terminate all
3823 * strings before testing them.
3824 */
3825
3826 term = *cur_end;
3827 *cur_end = '\0';
3828
3829 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3830 switch (exp->action) {
3831 case ACT_SETBE:
3832 /* It is not possible to jump a second time.
3833 * FIXME: should we return an HTTP/500 here so that
3834 * the admin knows there's a problem ?
3835 */
3836 if (t->be != t->fe)
3837 break;
3838
3839 /* Swithing Proxy */
3840 t->be = (struct proxy *) exp->replace;
3841
3842 /* right now, the backend switch is not too much complicated
3843 * because we have associated req_cap and rsp_cap to the
3844 * frontend, and the beconn will be updated later.
3845 */
3846
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003847 t->rep->rto = t->req->wto = t->be->srvtimeout;
3848 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003849 last_hdr = 1;
3850 break;
3851
3852 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003853 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003854 last_hdr = 1;
3855 break;
3856
3857 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003858 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003859 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003860 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003861 break;
3862
3863 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003864 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003865 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003866 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003867 break;
3868
3869 case ACT_REPLACE:
3870 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3871 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3872 /* FIXME: if the user adds a newline in the replacement, the
3873 * index will not be recalculated for now, and the new line
3874 * will not be counted as a new header.
3875 */
3876
3877 cur_end += delta;
3878 cur_next += delta;
3879 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003880 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003881 break;
3882
3883 case ACT_REMOVE:
3884 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3885 cur_next += delta;
3886
3887 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003888 txn->req.eoh += delta;
3889 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3890 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003891 cur_hdr->len = 0;
3892 cur_end = NULL; /* null-term has been rewritten */
3893 break;
3894
3895 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003896 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003897 if (cur_end)
3898 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003899
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003900 /* keep the link from this header to next one in case of later
3901 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003902 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003903 old_idx = cur_idx;
3904 }
3905 return 0;
3906}
3907
3908
3909/* Apply the filter to the request line.
3910 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3911 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003912 * Since it can manage the switch to another backend, it updates the per-proxy
3913 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003914 */
3915int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3916{
3917 char term;
3918 char *cur_ptr, *cur_end;
3919 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003920 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003921 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003922
Willy Tarreau58f10d72006-12-04 02:26:12 +01003923
Willy Tarreau3d300592007-03-18 18:34:41 +01003924 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003925 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003926 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003927 (exp->action == ACT_ALLOW ||
3928 exp->action == ACT_DENY ||
3929 exp->action == ACT_TARPIT))
3930 return 0;
3931 else if (exp->action == ACT_REMOVE)
3932 return 0;
3933
3934 done = 0;
3935
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003936 cur_ptr = req->data + txn->req.som;
3937 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003938
3939 /* Now we have the request line between cur_ptr and cur_end */
3940
3941 /* The annoying part is that pattern matching needs
3942 * that we modify the contents to null-terminate all
3943 * strings before testing them.
3944 */
3945
3946 term = *cur_end;
3947 *cur_end = '\0';
3948
3949 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3950 switch (exp->action) {
3951 case ACT_SETBE:
3952 /* It is not possible to jump a second time.
3953 * FIXME: should we return an HTTP/500 here so that
3954 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003955 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003956 if (t->be != t->fe)
3957 break;
3958
3959 /* Swithing Proxy */
3960 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003961
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003962 /* right now, the backend switch is not too much complicated
3963 * because we have associated req_cap and rsp_cap to the
3964 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003965 */
3966
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003967 t->rep->rto = t->req->wto = t->be->srvtimeout;
3968 t->req->cto = t->be->contimeout;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003969 done = 1;
3970 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003971
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003972 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003973 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003974 done = 1;
3975 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003976
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003977 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003978 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003979 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003980 done = 1;
3981 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003983 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003984 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003985 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003986 done = 1;
3987 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003988
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003989 case ACT_REPLACE:
3990 *cur_end = term; /* restore the string terminator */
3991 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3992 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3993 /* FIXME: if the user adds a newline in the replacement, the
3994 * index will not be recalculated for now, and the new line
3995 * will not be counted as a new header.
3996 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003997
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003998 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003999 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004000
Willy Tarreaua9bd1982007-04-03 20:03:18 +02004001 txn->req.sol = req->data + txn->req.som;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004002 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004003 HTTP_MSG_RQMETH,
4004 cur_ptr, cur_end + 1,
4005 NULL, NULL);
4006 if (unlikely(!cur_end))
4007 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004008
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004009 /* we have a full request and we know that we have either a CR
4010 * or an LF at <ptr>.
4011 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004012 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4013 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004014 /* there is no point trying this regex on headers */
4015 return 1;
4016 }
4017 }
4018 *cur_end = term; /* restore the string terminator */
4019 return done;
4020}
Willy Tarreau97de6242006-12-27 17:18:38 +01004021
Willy Tarreau58f10d72006-12-04 02:26:12 +01004022
Willy Tarreau58f10d72006-12-04 02:26:12 +01004023
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004024/*
4025 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4026 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004027 * unparsable request. Since it can manage the switch to another backend, it
4028 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004029 */
4030int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4031{
Willy Tarreau3d300592007-03-18 18:34:41 +01004032 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004033 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004034 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004035 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004036
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004037 /*
4038 * The interleaving of transformations and verdicts
4039 * makes it difficult to decide to continue or stop
4040 * the evaluation.
4041 */
4042
Willy Tarreau3d300592007-03-18 18:34:41 +01004043 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004044 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4045 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4046 exp = exp->next;
4047 continue;
4048 }
4049
4050 /* Apply the filter to the request line. */
4051 ret = apply_filter_to_req_line(t, req, exp);
4052 if (unlikely(ret < 0))
4053 return -1;
4054
4055 if (likely(ret == 0)) {
4056 /* The filter did not match the request, it can be
4057 * iterated through all headers.
4058 */
4059 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004060 }
4061 exp = exp->next;
4062 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004063 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004064}
4065
4066
Willy Tarreaua15645d2007-03-18 16:22:39 +01004067
Willy Tarreau58f10d72006-12-04 02:26:12 +01004068/*
4069 * Manager client-side cookie
4070 */
4071void manage_client_side_cookies(struct session *t, struct buffer *req)
4072{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004073 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004074 char *p1, *p2, *p3, *p4;
4075 char *del_colon, *del_cookie, *colon;
4076 int app_cookies;
4077
4078 appsess *asession_temp = NULL;
4079 appsess local_asession;
4080
4081 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004082 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004083
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004084 if (t->be->cookie_name == NULL &&
4085 t->be->appsession_name == NULL &&
4086 t->be->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004087 return;
4088
Willy Tarreau2a324282006-12-05 00:05:46 +01004089 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004090 * we start with the start line.
4091 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004092 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004093 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004094
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004095 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004096 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004097 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004098
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004099 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004100 cur_ptr = cur_next;
4101 cur_end = cur_ptr + cur_hdr->len;
4102 cur_next = cur_end + cur_hdr->cr + 1;
4103
4104 /* We have one full header between cur_ptr and cur_end, and the
4105 * next header starts at cur_next. We're only interested in
4106 * "Cookie:" headers.
4107 */
4108
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004109 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4110 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004111 old_idx = cur_idx;
4112 continue;
4113 }
4114
4115 /* Now look for cookies. Conforming to RFC2109, we have to support
4116 * attributes whose name begin with a '$', and associate them with
4117 * the right cookie, if we want to delete this cookie.
4118 * So there are 3 cases for each cookie read :
4119 * 1) it's a special attribute, beginning with a '$' : ignore it.
4120 * 2) it's a server id cookie that we *MAY* want to delete : save
4121 * some pointers on it (last semi-colon, beginning of cookie...)
4122 * 3) it's an application cookie : we *MAY* have to delete a previous
4123 * "special" cookie.
4124 * At the end of loop, if a "special" cookie remains, we may have to
4125 * remove it. If no application cookie persists in the header, we
4126 * *MUST* delete it
4127 */
4128
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004129 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004130
Willy Tarreau58f10d72006-12-04 02:26:12 +01004131 /* del_cookie == NULL => nothing to be deleted */
4132 del_colon = del_cookie = NULL;
4133 app_cookies = 0;
4134
4135 while (p1 < cur_end) {
4136 /* skip spaces and colons, but keep an eye on these ones */
4137 while (p1 < cur_end) {
4138 if (*p1 == ';' || *p1 == ',')
4139 colon = p1;
4140 else if (!isspace((int)*p1))
4141 break;
4142 p1++;
4143 }
4144
4145 if (p1 == cur_end)
4146 break;
4147
4148 /* p1 is at the beginning of the cookie name */
4149 p2 = p1;
4150 while (p2 < cur_end && *p2 != '=')
4151 p2++;
4152
4153 if (p2 == cur_end)
4154 break;
4155
4156 p3 = p2 + 1; /* skips the '=' sign */
4157 if (p3 == cur_end)
4158 break;
4159
4160 p4 = p3;
4161 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
4162 p4++;
4163
4164 /* here, we have the cookie name between p1 and p2,
4165 * and its value between p3 and p4.
4166 * we can process it :
4167 *
4168 * Cookie: NAME=VALUE;
4169 * | || || |
4170 * | || || +--> p4
4171 * | || |+-------> p3
4172 * | || +--------> p2
4173 * | |+------------> p1
4174 * | +-------------> colon
4175 * +--------------------> cur_ptr
4176 */
4177
4178 if (*p1 == '$') {
4179 /* skip this one */
4180 }
4181 else {
4182 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004183 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004184 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004185 (p4 - p1 >= t->fe->capture_namelen) &&
4186 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004187 int log_len = p4 - p1;
4188
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004189 if ((txn->cli_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004190 Alert("HTTP logging : out of memory.\n");
4191 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004192 if (log_len > t->fe->capture_len)
4193 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004194 memcpy(txn->cli_cookie, p1, log_len);
4195 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004196 }
4197 }
4198
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004199 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4200 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004201 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004202 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004203 char *delim;
4204
4205 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4206 * have the server ID betweek p3 and delim, and the original cookie between
4207 * delim+1 and p4. Otherwise, delim==p4 :
4208 *
4209 * Cookie: NAME=SRV~VALUE;
4210 * | || || | |
4211 * | || || | +--> p4
4212 * | || || +--------> delim
4213 * | || |+-----------> p3
4214 * | || +------------> p2
4215 * | |+----------------> p1
4216 * | +-----------------> colon
4217 * +------------------------> cur_ptr
4218 */
4219
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004220 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004221 for (delim = p3; delim < p4; delim++)
4222 if (*delim == COOKIE_DELIM)
4223 break;
4224 }
4225 else
4226 delim = p4;
4227
4228
4229 /* Here, we'll look for the first running server which supports the cookie.
4230 * This allows to share a same cookie between several servers, for example
4231 * to dedicate backup servers to specific servers only.
4232 * However, to prevent clients from sticking to cookie-less backup server
4233 * when they have incidentely learned an empty cookie, we simply ignore
4234 * empty cookies and mark them as invalid.
4235 */
4236 if (delim == p3)
4237 srv = NULL;
4238
4239 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004240 if (srv->cookie && (srv->cklen == delim - p3) &&
4241 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004242 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004243 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004244 txn->flags &= ~TX_CK_MASK;
4245 txn->flags |= TX_CK_VALID;
4246 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004247 t->srv = srv;
4248 break;
4249 } else {
4250 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004251 txn->flags &= ~TX_CK_MASK;
4252 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004253 }
4254 }
4255 srv = srv->next;
4256 }
4257
Willy Tarreau3d300592007-03-18 18:34:41 +01004258 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004259 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004260 txn->flags &= ~TX_CK_MASK;
4261 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004262 }
4263
4264 /* depending on the cookie mode, we may have to either :
4265 * - delete the complete cookie if we're in insert+indirect mode, so that
4266 * the server never sees it ;
4267 * - remove the server id from the cookie value, and tag the cookie as an
4268 * application cookie so that it does not get accidentely removed later,
4269 * if we're in cookie prefix mode
4270 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004271 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004272 int delta; /* negative */
4273
4274 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4275 p4 += delta;
4276 cur_end += delta;
4277 cur_next += delta;
4278 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004279 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004280
4281 del_cookie = del_colon = NULL;
4282 app_cookies++; /* protect the header from deletion */
4283 }
4284 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004285 (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 +01004286 del_cookie = p1;
4287 del_colon = colon;
4288 }
4289 } else {
4290 /* now we know that we must keep this cookie since it's
4291 * not ours. But if we wanted to delete our cookie
4292 * earlier, we cannot remove the complete header, but we
4293 * can remove the previous block itself.
4294 */
4295 app_cookies++;
4296
4297 if (del_cookie != NULL) {
4298 int delta; /* negative */
4299
4300 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4301 p4 += delta;
4302 cur_end += delta;
4303 cur_next += delta;
4304 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004305 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004306 del_cookie = del_colon = NULL;
4307 }
4308 }
4309
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004310 if ((t->be->appsession_name != NULL) &&
4311 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004312 /* first, let's see if the cookie is our appcookie*/
4313
4314 /* Cool... it's the right one */
4315
4316 asession_temp = &local_asession;
4317
4318 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4319 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4320 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4321 return;
4322 }
4323
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004324 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4325 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004326 asession_temp->serverid = NULL;
4327
4328 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004329 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004330 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4331 /* free previously allocated memory */
4332 pool_free_to(apools.sessid, local_asession.sessid);
4333 Alert("Not enough memory process_cli():asession:calloc().\n");
4334 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4335 return;
4336 }
4337
4338 asession_temp->sessid = local_asession.sessid;
4339 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004340 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004341 } else {
4342 /* free previously allocated memory */
4343 pool_free_to(apools.sessid, local_asession.sessid);
4344 }
4345
4346 if (asession_temp->serverid == NULL) {
4347 Alert("Found Application Session without matching server.\n");
4348 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004349 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004350 while (srv) {
4351 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004352 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004353 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004354 txn->flags &= ~TX_CK_MASK;
4355 txn->flags |= TX_CK_VALID;
4356 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004357 t->srv = srv;
4358 break;
4359 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004360 txn->flags &= ~TX_CK_MASK;
4361 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004362 }
4363 }
4364 srv = srv->next;
4365 }/* end while(srv) */
4366 }/* end else if server == NULL */
4367
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004368 tv_delayfrom(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004369 }/* end if ((t->proxy->appsession_name != NULL) ... */
4370 }
4371
4372 /* we'll have to look for another cookie ... */
4373 p1 = p4;
4374 } /* while (p1 < cur_end) */
4375
4376 /* There's no more cookie on this line.
4377 * We may have marked the last one(s) for deletion.
4378 * We must do this now in two ways :
4379 * - if there is no app cookie, we simply delete the header ;
4380 * - if there are app cookies, we must delete the end of the
4381 * string properly, including the colon/semi-colon before
4382 * the cookie name.
4383 */
4384 if (del_cookie != NULL) {
4385 int delta;
4386 if (app_cookies) {
4387 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4388 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004389 cur_hdr->len += delta;
4390 } else {
4391 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004392
4393 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004394 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4395 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004396 cur_hdr->len = 0;
4397 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004398 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004399 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004400 }
4401
4402 /* keep the link from this header to next one */
4403 old_idx = cur_idx;
4404 } /* end of cookie processing on this header */
4405}
4406
4407
Willy Tarreaua15645d2007-03-18 16:22:39 +01004408/* Iterate the same filter through all response headers contained in <rtr>.
4409 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4410 */
4411int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4412{
4413 char term;
4414 char *cur_ptr, *cur_end, *cur_next;
4415 int cur_idx, old_idx, last_hdr;
4416 struct http_txn *txn = &t->txn;
4417 struct hdr_idx_elem *cur_hdr;
4418 int len, delta;
4419
4420 last_hdr = 0;
4421
4422 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4423 old_idx = 0;
4424
4425 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004426 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004427 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004428 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004429 (exp->action == ACT_ALLOW ||
4430 exp->action == ACT_DENY))
4431 return 0;
4432
4433 cur_idx = txn->hdr_idx.v[old_idx].next;
4434 if (!cur_idx)
4435 break;
4436
4437 cur_hdr = &txn->hdr_idx.v[cur_idx];
4438 cur_ptr = cur_next;
4439 cur_end = cur_ptr + cur_hdr->len;
4440 cur_next = cur_end + cur_hdr->cr + 1;
4441
4442 /* Now we have one header between cur_ptr and cur_end,
4443 * and the next header starts at cur_next.
4444 */
4445
4446 /* The annoying part is that pattern matching needs
4447 * that we modify the contents to null-terminate all
4448 * strings before testing them.
4449 */
4450
4451 term = *cur_end;
4452 *cur_end = '\0';
4453
4454 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4455 switch (exp->action) {
4456 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004457 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004458 last_hdr = 1;
4459 break;
4460
4461 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004462 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004463 last_hdr = 1;
4464 break;
4465
4466 case ACT_REPLACE:
4467 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4468 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4469 /* FIXME: if the user adds a newline in the replacement, the
4470 * index will not be recalculated for now, and the new line
4471 * will not be counted as a new header.
4472 */
4473
4474 cur_end += delta;
4475 cur_next += delta;
4476 cur_hdr->len += delta;
4477 txn->rsp.eoh += delta;
4478 break;
4479
4480 case ACT_REMOVE:
4481 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4482 cur_next += delta;
4483
4484 /* FIXME: this should be a separate function */
4485 txn->rsp.eoh += delta;
4486 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4487 txn->hdr_idx.used--;
4488 cur_hdr->len = 0;
4489 cur_end = NULL; /* null-term has been rewritten */
4490 break;
4491
4492 }
4493 }
4494 if (cur_end)
4495 *cur_end = term; /* restore the string terminator */
4496
4497 /* keep the link from this header to next one in case of later
4498 * removal of next header.
4499 */
4500 old_idx = cur_idx;
4501 }
4502 return 0;
4503}
4504
4505
4506/* Apply the filter to the status line in the response buffer <rtr>.
4507 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4508 * or -1 if a replacement resulted in an invalid status line.
4509 */
4510int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4511{
4512 char term;
4513 char *cur_ptr, *cur_end;
4514 int done;
4515 struct http_txn *txn = &t->txn;
4516 int len, delta;
4517
4518
Willy Tarreau3d300592007-03-18 18:34:41 +01004519 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004520 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004521 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004522 (exp->action == ACT_ALLOW ||
4523 exp->action == ACT_DENY))
4524 return 0;
4525 else if (exp->action == ACT_REMOVE)
4526 return 0;
4527
4528 done = 0;
4529
4530 cur_ptr = rtr->data + txn->rsp.som;
4531 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4532
4533 /* Now we have the status line between cur_ptr and cur_end */
4534
4535 /* The annoying part is that pattern matching needs
4536 * that we modify the contents to null-terminate all
4537 * strings before testing them.
4538 */
4539
4540 term = *cur_end;
4541 *cur_end = '\0';
4542
4543 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4544 switch (exp->action) {
4545 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004546 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004547 done = 1;
4548 break;
4549
4550 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004551 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004552 done = 1;
4553 break;
4554
4555 case ACT_REPLACE:
4556 *cur_end = term; /* restore the string terminator */
4557 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4558 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4559 /* FIXME: if the user adds a newline in the replacement, the
4560 * index will not be recalculated for now, and the new line
4561 * will not be counted as a new header.
4562 */
4563
4564 txn->rsp.eoh += delta;
4565 cur_end += delta;
4566
Willy Tarreaua9bd1982007-04-03 20:03:18 +02004567 txn->rsp.sol = rtr->data + txn->rsp.som;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004568 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004569 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004570 cur_ptr, cur_end + 1,
4571 NULL, NULL);
4572 if (unlikely(!cur_end))
4573 return -1;
4574
4575 /* we have a full respnse and we know that we have either a CR
4576 * or an LF at <ptr>.
4577 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004578 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004579 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4580 /* there is no point trying this regex on headers */
4581 return 1;
4582 }
4583 }
4584 *cur_end = term; /* restore the string terminator */
4585 return done;
4586}
4587
4588
4589
4590/*
4591 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4592 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4593 * unparsable response.
4594 */
4595int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4596{
Willy Tarreau3d300592007-03-18 18:34:41 +01004597 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004598 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004599 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004600 int ret;
4601
4602 /*
4603 * The interleaving of transformations and verdicts
4604 * makes it difficult to decide to continue or stop
4605 * the evaluation.
4606 */
4607
Willy Tarreau3d300592007-03-18 18:34:41 +01004608 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004609 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4610 exp->action == ACT_PASS)) {
4611 exp = exp->next;
4612 continue;
4613 }
4614
4615 /* Apply the filter to the status line. */
4616 ret = apply_filter_to_sts_line(t, rtr, exp);
4617 if (unlikely(ret < 0))
4618 return -1;
4619
4620 if (likely(ret == 0)) {
4621 /* The filter did not match the response, it can be
4622 * iterated through all headers.
4623 */
4624 apply_filter_to_resp_headers(t, rtr, exp);
4625 }
4626 exp = exp->next;
4627 }
4628 return 0;
4629}
4630
4631
4632
4633/*
4634 * Manager server-side cookies
4635 */
4636void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4637{
4638 struct http_txn *txn = &t->txn;
4639 char *p1, *p2, *p3, *p4;
4640
4641 appsess *asession_temp = NULL;
4642 appsess local_asession;
4643
4644 char *cur_ptr, *cur_end, *cur_next;
4645 int cur_idx, old_idx, delta;
4646
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004647 if (t->be->cookie_name == NULL &&
4648 t->be->appsession_name == NULL &&
4649 t->be->capture_name == NULL &&
4650 !(t->be->options & PR_O_CHK_CACHE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004651 return;
4652
4653 /* Iterate through the headers.
4654 * we start with the start line.
4655 */
4656 old_idx = 0;
4657 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4658
4659 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4660 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004661 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004662
4663 cur_hdr = &txn->hdr_idx.v[cur_idx];
4664 cur_ptr = cur_next;
4665 cur_end = cur_ptr + cur_hdr->len;
4666 cur_next = cur_end + cur_hdr->cr + 1;
4667
4668 /* We have one full header between cur_ptr and cur_end, and the
4669 * next header starts at cur_next. We're only interested in
4670 * "Cookie:" headers.
4671 */
4672
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004673 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4674 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004675 old_idx = cur_idx;
4676 continue;
4677 }
4678
4679 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004680 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004681
4682
4683 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004684 if (t->be->cookie_name == NULL &&
4685 t->be->appsession_name == NULL &&
4686 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004687 return;
4688
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004689 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004690
4691 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004692 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4693 break;
4694
4695 /* p1 is at the beginning of the cookie name */
4696 p2 = p1;
4697
4698 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4699 p2++;
4700
4701 if (p2 == cur_end || *p2 == ';') /* next cookie */
4702 break;
4703
4704 p3 = p2 + 1; /* skip the '=' sign */
4705 if (p3 == cur_end)
4706 break;
4707
4708 p4 = p3;
4709 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4710 p4++;
4711
4712 /* here, we have the cookie name between p1 and p2,
4713 * and its value between p3 and p4.
4714 * we can process it.
4715 */
4716
4717 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004718 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004719 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004720 (p4 - p1 >= t->be->capture_namelen) &&
4721 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004722 int log_len = p4 - p1;
4723
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004724 if ((txn->srv_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004725 Alert("HTTP logging : out of memory.\n");
4726 }
4727
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004728 if (log_len > t->be->capture_len)
4729 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004730 memcpy(txn->srv_cookie, p1, log_len);
4731 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004732 }
4733
4734 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004735 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4736 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004737 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004738 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004739
4740 /* If the cookie is in insert mode on a known server, we'll delete
4741 * this occurrence because we'll insert another one later.
4742 * We'll delete it too if the "indirect" option is set and we're in
4743 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004744 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4745 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004746 /* this header must be deleted */
4747 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4748 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4749 txn->hdr_idx.used--;
4750 cur_hdr->len = 0;
4751 cur_next += delta;
4752 txn->rsp.eoh += delta;
4753
Willy Tarreau3d300592007-03-18 18:34:41 +01004754 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004755 }
4756 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004757 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004758 /* replace bytes p3->p4 with the cookie name associated
4759 * with this server since we know it.
4760 */
4761 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4762 cur_hdr->len += delta;
4763 cur_next += delta;
4764 txn->rsp.eoh += delta;
4765
Willy Tarreau3d300592007-03-18 18:34:41 +01004766 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004767 }
4768 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004769 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004770 /* insert the cookie name associated with this server
4771 * before existing cookie, and insert a delimitor between them..
4772 */
4773 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4774 cur_hdr->len += delta;
4775 cur_next += delta;
4776 txn->rsp.eoh += delta;
4777
4778 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004779 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004780 }
4781 }
4782 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004783 else if ((t->be->appsession_name != NULL) &&
4784 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004785
4786 /* Cool... it's the right one */
4787
4788 size_t server_id_len = strlen(t->srv->id) + 1;
4789 asession_temp = &local_asession;
4790
4791 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4792 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4793 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4794 return;
4795 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004796 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4797 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004798 asession_temp->serverid = NULL;
4799
4800 /* only do insert, if lookup fails */
4801 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
4802 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4803 Alert("Not enough Memory process_srv():asession:calloc().\n");
4804 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4805 return;
4806 }
4807 asession_temp->sessid = local_asession.sessid;
4808 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004809 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004810 }/* end if (chtbl_lookup()) */
4811 else {
4812 /* free wasted memory */
4813 pool_free_to(apools.sessid, local_asession.sessid);
4814 } /* end else from if (chtbl_lookup()) */
4815
4816 if (asession_temp->serverid == NULL) {
4817 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
4818 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4819 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4820 return;
4821 }
4822 asession_temp->serverid[0] = '\0';
4823 }
4824
4825 if (asession_temp->serverid[0] == '\0')
4826 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4827
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004828 tv_delayfrom(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004829
4830#if defined(DEBUG_HASH)
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004831 print_table(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004832#endif
4833 }/* end if ((t->proxy->appsession_name != NULL) ... */
4834 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4835 } /* we're now at the end of the cookie value */
4836
4837 /* keep the link from this header to next one */
4838 old_idx = cur_idx;
4839 } /* end of cookie processing on this header */
4840}
4841
4842
4843
4844/*
4845 * Check if response is cacheable or not. Updates t->flags.
4846 */
4847void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4848{
4849 struct http_txn *txn = &t->txn;
4850 char *p1, *p2;
4851
4852 char *cur_ptr, *cur_end, *cur_next;
4853 int cur_idx;
4854
Willy Tarreau3d300592007-03-18 18:34:41 +01004855 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004856 return;
4857
4858 /* Iterate through the headers.
4859 * we start with the start line.
4860 */
4861 cur_idx = 0;
4862 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4863
4864 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4865 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004866 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004867
4868 cur_hdr = &txn->hdr_idx.v[cur_idx];
4869 cur_ptr = cur_next;
4870 cur_end = cur_ptr + cur_hdr->len;
4871 cur_next = cur_end + cur_hdr->cr + 1;
4872
4873 /* We have one full header between cur_ptr and cur_end, and the
4874 * next header starts at cur_next. We're only interested in
4875 * "Cookie:" headers.
4876 */
4877
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004878 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4879 if (val) {
4880 if ((cur_end - (cur_ptr + val) >= 8) &&
4881 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4882 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4883 return;
4884 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004885 }
4886
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004887 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4888 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004889 continue;
4890
4891 /* OK, right now we know we have a cache-control header at cur_ptr */
4892
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004893 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004894
4895 if (p1 >= cur_end) /* no more info */
4896 continue;
4897
4898 /* p1 is at the beginning of the value */
4899 p2 = p1;
4900
4901 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4902 p2++;
4903
4904 /* we have a complete value between p1 and p2 */
4905 if (p2 < cur_end && *p2 == '=') {
4906 /* we have something of the form no-cache="set-cookie" */
4907 if ((cur_end - p1 >= 21) &&
4908 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4909 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004910 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004911 continue;
4912 }
4913
4914 /* OK, so we know that either p2 points to the end of string or to a comma */
4915 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4916 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4917 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4918 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004919 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004920 return;
4921 }
4922
4923 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004924 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004925 continue;
4926 }
4927 }
4928}
4929
4930
Willy Tarreau58f10d72006-12-04 02:26:12 +01004931/*
4932 * Try to retrieve a known appsession in the URI, then the associated server.
4933 * If the server is found, it's assigned to the session.
4934 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004935void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004936{
Willy Tarreau3d300592007-03-18 18:34:41 +01004937 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004938 appsess *asession_temp = NULL;
4939 appsess local_asession;
4940 char *request_line;
4941
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004942 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004943 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004944 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004945 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004946 return;
4947
4948 /* skip ';' */
4949 request_line++;
4950
4951 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004952 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004953 return;
4954
4955 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004956 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004957
4958 /* First try if we already have an appsession */
4959 asession_temp = &local_asession;
4960
4961 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4962 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4963 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4964 return;
4965 }
4966
4967 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004968 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4969 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004970 asession_temp->serverid = NULL;
4971
4972 /* only do insert, if lookup fails */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004973 if (chtbl_lookup(&(t->be->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004974 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4975 /* free previously allocated memory */
4976 pool_free_to(apools.sessid, local_asession.sessid);
4977 Alert("Not enough memory process_cli():asession:calloc().\n");
4978 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4979 return;
4980 }
4981 asession_temp->sessid = local_asession.sessid;
4982 asession_temp->serverid = local_asession.serverid;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004983 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004984 }
4985 else {
4986 /* free previously allocated memory */
4987 pool_free_to(apools.sessid, local_asession.sessid);
4988 }
4989
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004990 tv_delayfrom(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004991 asession_temp->request_count++;
4992
4993#if defined(DEBUG_HASH)
4994 print_table(&(t->proxy->htbl_proxy));
4995#endif
4996 if (asession_temp->serverid == NULL) {
4997 Alert("Found Application Session without matching server.\n");
4998 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004999 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005000 while (srv) {
5001 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005002 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005003 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005004 txn->flags &= ~TX_CK_MASK;
5005 txn->flags |= TX_CK_VALID;
5006 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005007 t->srv = srv;
5008 break;
5009 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005010 txn->flags &= ~TX_CK_MASK;
5011 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005012 }
5013 }
5014 srv = srv->next;
5015 }
5016 }
5017}
5018
5019
Willy Tarreaub2513902006-12-17 14:52:38 +01005020/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005021 * In a GET or HEAD request, check if the requested URI matches the stats uri
5022 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005023 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005024 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005025 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005026 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005027 *
5028 * Returns 1 if the session's state changes, otherwise 0.
5029 */
5030int stats_check_uri_auth(struct session *t, struct proxy *backend)
5031{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005032 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005033 struct uri_auth *uri_auth = backend->uri_auth;
5034 struct user_auth *user;
5035 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005036 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005037
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005038 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005039 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005040 return 0;
5041
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005042 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005043
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005044 /* the URI is in h */
5045 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005046 return 0;
5047
5048 /* we are in front of a interceptable URI. Let's check
5049 * if there's an authentication and if it's valid.
5050 */
5051 user = uri_auth->users;
5052 if (!user) {
5053 /* no user auth required, it's OK */
5054 authenticated = 1;
5055 } else {
5056 authenticated = 0;
5057
5058 /* a user list is defined, we have to check.
5059 * skip 21 chars for "Authorization: Basic ".
5060 */
5061
5062 /* FIXME: this should move to an earlier place */
5063 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005064 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5065 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5066 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005067 if (len > 14 &&
5068 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005069 txn->auth_hdr.str = h;
5070 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005071 break;
5072 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005073 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005074 }
5075
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005076 if (txn->auth_hdr.len < 21 ||
5077 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005078 user = NULL;
5079
5080 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005081 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5082 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005083 user->user_pwd, user->user_len)) {
5084 authenticated = 1;
5085 break;
5086 }
5087 user = user->next;
5088 }
5089 }
5090
5091 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005092 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005093
5094 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005095 msg.str = trash;
5096 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005097 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005098 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01005099 if (!(t->flags & SN_ERR_MASK))
5100 t->flags |= SN_ERR_PRXCOND;
5101 if (!(t->flags & SN_FINST_MASK))
5102 t->flags |= SN_FINST_R;
5103 return 1;
5104 }
5105
5106 /* The request is valid, the user is authenticate. Let's start sending
5107 * data.
5108 */
5109 t->cli_state = CL_STSHUTR;
5110 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
5111 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
5112 t->data_source = DATA_SRC_STATS;
5113 t->data_state = DATA_ST_INIT;
5114 produce_content(t);
5115 return 1;
5116}
5117
5118
Willy Tarreaubaaee002006-06-26 02:48:02 +02005119/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005120 * Print a debug line with a header
5121 */
5122void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5123{
5124 int len, max;
5125 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5126 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5127 max = end - start;
5128 UBOUND(max, sizeof(trash) - len - 1);
5129 len += strlcpy2(trash + len, start, max + 1);
5130 trash[len++] = '\n';
5131 write(1, trash, len);
5132}
5133
5134
5135/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005136 * Local variables:
5137 * c-indent-level: 8
5138 * c-basic-offset: 8
5139 * End:
5140 */