blob: ad5188aea1cbba519b2d3c2b00a598bb3e814a6e [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>
20
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/appsession.h>
26#include <common/compat.h>
27#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010028#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/memory.h>
30#include <common/mini-clist.h>
31#include <common/standard.h>
32#include <common/time.h>
33#include <common/uri_auth.h>
34#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36#include <types/capture.h>
37#include <types/client.h>
38#include <types/global.h>
39#include <types/httperr.h>
40#include <types/polling.h>
41#include <types/proxy.h>
42#include <types/server.h>
43
44#include <proto/backend.h>
45#include <proto/buffers.h>
46#include <proto/fd.h>
47#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010048#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
51#include <proto/session.h>
52#include <proto/task.h>
53
Willy Tarreau6d1a9882007-01-07 02:03:04 +010054#ifdef CONFIG_HAP_TCPSPLICE
55#include <libtcpsplice.h>
56#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
Willy Tarreau58f10d72006-12-04 02:26:12 +010058#define DEBUG_PARSE_NO_SPEEDUP
59#undef DEBUG_PARSE_NO_SPEEDUP
60
Willy Tarreau976f1ee2006-12-17 10:06:03 +010061/* This is used to perform a quick jump as an alternative to a break/continue
62 * instruction. The first argument is the label for normal operation, and the
63 * second one is the break/continue instruction in the no_speedup mode.
64 */
65
66#ifdef DEBUG_PARSE_NO_SPEEDUP
67#define QUICK_JUMP(x,y) y
68#else
69#define QUICK_JUMP(x,y) goto x
70#endif
71
Willy Tarreau1c47f852006-07-09 08:22:27 +020072/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010073const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020074 "HTTP/1.0 200 OK\r\n"
75 "Cache-Control: no-cache\r\n"
76 "Connection: close\r\n"
77 "Content-Type: text/html\r\n"
78 "\r\n"
79 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const struct chunk http_200_chunk = {
82 .str = (char *)&HTTP_200,
83 .len = sizeof(HTTP_200)-1
84};
85
86const char *HTTP_302 =
87 "HTTP/1.0 302 Found\r\n"
88 "Cache-Control: no-cache\r\n"
89 "Connection: close\r\n"
90 "Location: "; /* not terminated since it will be concatenated with the URL */
91
92/* same as 302 except that the browser MUST retry with the GET method */
93const char *HTTP_303 =
94 "HTTP/1.0 303 See Other\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
97 "Location: "; /* not terminated since it will be concatenated with the URL */
98
Willy Tarreaubaaee002006-06-26 02:48:02 +020099/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
100const char *HTTP_401_fmt =
101 "HTTP/1.0 401 Unauthorized\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200104 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
106 "\r\n"
107 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
108
Willy Tarreau0f772532006-12-23 20:51:41 +0100109
110const int http_err_codes[HTTP_ERR_SIZE] = {
111 [HTTP_ERR_400] = 400,
112 [HTTP_ERR_403] = 403,
113 [HTTP_ERR_408] = 408,
114 [HTTP_ERR_500] = 500,
115 [HTTP_ERR_502] = 502,
116 [HTTP_ERR_503] = 503,
117 [HTTP_ERR_504] = 504,
118};
119
Willy Tarreau80587432006-12-24 17:47:20 +0100120static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100121 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100122 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "\r\n"
127 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
128
129 [HTTP_ERR_403] =
130 "HTTP/1.0 403 Forbidden\r\n"
131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "\r\n"
135 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
136
137 [HTTP_ERR_408] =
138 "HTTP/1.0 408 Request Time-out\r\n"
139 "Cache-Control: no-cache\r\n"
140 "Connection: close\r\n"
141 "Content-Type: text/html\r\n"
142 "\r\n"
143 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
144
145 [HTTP_ERR_500] =
146 "HTTP/1.0 500 Server Error\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
152
153 [HTTP_ERR_502] =
154 "HTTP/1.0 502 Bad Gateway\r\n"
155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
160
161 [HTTP_ERR_503] =
162 "HTTP/1.0 503 Service Unavailable\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
168
169 [HTTP_ERR_504] =
170 "HTTP/1.0 504 Gateway Time-out\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
176
177};
178
Willy Tarreau80587432006-12-24 17:47:20 +0100179/* We must put the messages here since GCC cannot initialize consts depending
180 * on strlen().
181 */
182struct chunk http_err_chunks[HTTP_ERR_SIZE];
183
184void init_proto_http()
185{
186 int msg;
187 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
188 if (!http_err_msgs[msg]) {
189 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
190 abort();
191 }
192
193 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
194 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
195 }
196}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197
Willy Tarreau53b6c742006-12-17 13:37:46 +0100198/*
199 * We have 26 list of methods (1 per first letter), each of which can have
200 * up to 3 entries (2 valid, 1 null).
201 */
202struct http_method_desc {
203 http_meth_t meth;
204 int len;
205 const char text[8];
206};
207
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100208const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100209 ['C' - 'A'] = {
210 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
211 },
212 ['D' - 'A'] = {
213 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
214 },
215 ['G' - 'A'] = {
216 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
217 },
218 ['H' - 'A'] = {
219 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
220 },
221 ['P' - 'A'] = {
222 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
223 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
224 },
225 ['T' - 'A'] = {
226 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
227 },
228 /* rest is empty like this :
229 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
230 */
231};
232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100233/* It is about twice as fast on recent architectures to lookup a byte in a
234 * table than two perform a boolean AND or OR between two tests. Refer to
235 * RFC2616 for those chars.
236 */
237
238const char http_is_spht[256] = {
239 [' '] = 1, ['\t'] = 1,
240};
241
242const char http_is_crlf[256] = {
243 ['\r'] = 1, ['\n'] = 1,
244};
245
246const char http_is_lws[256] = {
247 [' '] = 1, ['\t'] = 1,
248 ['\r'] = 1, ['\n'] = 1,
249};
250
251const char http_is_sep[256] = {
252 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
253 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
254 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
255 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
256 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
257};
258
259const char http_is_ctl[256] = {
260 [0 ... 31] = 1,
261 [127] = 1,
262};
263
264/*
265 * A token is any ASCII char that is neither a separator nor a CTL char.
266 * Do not overwrite values in assignment since gcc-2.95 will not handle
267 * them correctly. Instead, define every non-CTL char's status.
268 */
269const char http_is_token[256] = {
270 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
271 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
272 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
273 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
274 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
275 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
276 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
277 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
278 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
279 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
280 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
281 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
282 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
283 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
284 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
285 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
286 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
287 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
288 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
289 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
290 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
291 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
292 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
293 ['|'] = 1, ['}'] = 0, ['~'] = 1,
294};
295
296
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100297/*
298 * An http ver_token is any ASCII which can be found in an HTTP version,
299 * which includes 'H', 'T', 'P', '/', '.' and any digit.
300 */
301const char http_is_ver_token[256] = {
302 ['.'] = 1, ['/'] = 1,
303 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
304 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
305 ['H'] = 1, ['P'] = 1, ['T'] = 1,
306};
307
308
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309#ifdef DEBUG_FULL
310static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
311static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
312#endif
313
314
315/*
316 * returns a message to the client ; the connection is shut down for read,
317 * and the request is cleared so that no server connection can be initiated.
318 * The client must be in a valid state for this (HEADER, DATA ...).
Willy Tarreau0f772532006-12-23 20:51:41 +0100319 * Nothing is performed on the server side. The message is contained in a
320 * "chunk". If it is null, then an empty message is used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321 * The reply buffer doesn't need to be empty before this.
322 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100323void client_retnclose(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324{
Willy Tarreau2a429502006-10-15 14:52:29 +0200325 MY_FD_CLR(s->cli_fd, StaticReadEvent);
326 MY_FD_SET(s->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +0200327 tv_eternity(&s->req->rex);
Willy Tarreau73de9892006-11-30 11:40:23 +0100328 if (s->fe->clitimeout)
329 tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330 else
Willy Tarreaud7971282006-07-29 18:36:34 +0200331 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332 shutdown(s->cli_fd, SHUT_RD);
333 s->cli_state = CL_STSHUTR;
334 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100335 if (msg->len)
336 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337 s->req->l = 0;
338}
339
340
341/*
342 * returns a message into the rep buffer, and flushes the req buffer.
Willy Tarreau0f772532006-12-23 20:51:41 +0100343 * The reply buffer doesn't need to be empty before this. The message
344 * is contained in a "chunk". If it is null, then an empty message is
345 * used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100347void client_return(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348{
349 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100350 if (msg->len)
351 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352 s->req->l = 0;
353}
354
355
356/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100357 * indicators accordingly. Note that if <status> is 0, or if the message
358 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359 */
360void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100361 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362{
363 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100364 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100365 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100366 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100367 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368 }
369 if (!(t->flags & SN_ERR_MASK))
370 t->flags |= err;
371 if (!(t->flags & SN_FINST_MASK))
372 t->flags |= finst;
373}
374
Willy Tarreau80587432006-12-24 17:47:20 +0100375/* This function returns the appropriate error location for the given session
376 * and message.
377 */
378
379struct chunk *error_message(struct session *s, int msgnum)
380{
381 if (s->be->beprm->errmsg[msgnum].str)
382 return &s->be->beprm->errmsg[msgnum];
383 else if (s->fe->errmsg[msgnum].str)
384 return &s->fe->errmsg[msgnum];
385 else
386 return &http_err_chunks[msgnum];
387}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388
Willy Tarreau53b6c742006-12-17 13:37:46 +0100389/*
390 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
391 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
392 */
393static http_meth_t find_http_meth(const char *str, const int len)
394{
395 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100396 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100397
398 m = ((unsigned)*str - 'A');
399
400 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100401 for (h = http_methods[m]; h->len > 0; h++) {
402 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100403 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100404 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100405 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100406 };
407 return HTTP_METH_OTHER;
408 }
409 return HTTP_METH_NONE;
410
411}
412
413
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414/* Processes the client and server jobs of a session task, then
415 * puts it back to the wait queue in a clean state, or
416 * cleans up its resources if it must be deleted. Returns
417 * the time the task accepts to wait, or TIME_ETERNITY for
418 * infinity.
419 */
420int process_session(struct task *t)
421{
422 struct session *s = t->context;
423 int fsm_resync = 0;
424
425 do {
426 fsm_resync = 0;
427 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
428 fsm_resync |= process_cli(s);
429 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
430 fsm_resync |= process_srv(s);
431 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
432 } while (fsm_resync);
433
434 if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) {
435 struct timeval min1, min2;
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200436 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
437 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200438
Willy Tarreaud7971282006-07-29 18:36:34 +0200439 tv_min(&min1, &s->req->rex, &s->req->wex);
440 tv_min(&min2, &s->rep->rex, &s->rep->wex);
441 tv_min(&min1, &min1, &s->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442 tv_min(&t->expire, &min1, &min2);
443
444 /* restore t to its place in the task list */
445 task_queue(t);
446
447#ifdef DEBUG_FULL
448 /* DEBUG code : this should never ever happen, otherwise it indicates
449 * that a task still has something to do and will provoke a quick loop.
450 */
451 if (tv_remain2(&now, &t->expire) <= 0)
452 exit(100);
453#endif
454
455 return tv_remain2(&now, &t->expire); /* nothing more to do */
456 }
457
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100458 s->fe->feconn--;
459 if (s->flags & SN_BE_ASSIGNED)
460 s->be->beprm->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200461 actconn--;
462
463 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
464 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100465 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100466 s->uniq_id, s->be->beprm->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100467 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468 write(1, trash, len);
469 }
470
471 s->logs.t_close = tv_diff(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100472 if (s->req != NULL)
473 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100475 s->logs.bytes_out = s->rep->total;
476
477 s->fe->bytes_in += s->logs.bytes_in;
478 s->fe->bytes_out += s->logs.bytes_out;
479 if (s->be->beprm != s->fe) {
480 s->be->beprm->bytes_in += s->logs.bytes_in;
481 s->be->beprm->bytes_out += s->logs.bytes_out;
482 }
483 if (s->srv) {
484 s->srv->bytes_in += s->logs.bytes_in;
485 s->srv->bytes_out += s->logs.bytes_out;
486 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200487
488 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200489 if (s->logs.logwait &&
490 !(s->flags & SN_MONITOR) &&
Willy Tarreau73de9892006-11-30 11:40:23 +0100491 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492 sess_log(s);
493
494 /* the task MUST not be in the run queue anymore */
495 task_delete(t);
496 session_free(s);
497 task_free(t);
498 return TIME_ETERNITY; /* rest in peace for eternity */
499}
500
501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100502/* either we find an LF at <ptr> or we jump to <bad>.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100504#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
505
506/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
507 * otherwise to <http_msg_ood> with <state> set to <st>.
508 */
509#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
510 ptr++; \
511 if (likely(ptr < end)) \
512 goto good; \
513 else { \
514 state = (st); \
515 goto http_msg_ood; \
516 } \
517 } while (0)
518
Willy Tarreau117f59e2007-03-04 18:17:17 +0100519
520/*
521 * Capture headers from message starting at <som> according to header list
522 * <cap_hdr>, and fill the <idx> structure appropriately.
523 */
524void capture_headers(char *som, struct hdr_idx *idx,
525 char **cap, struct cap_hdr *cap_hdr)
526{
527 char *eol, *sol, *col, *sov;
528 int cur_idx;
529 struct cap_hdr *h;
530 int len;
531
532 sol = som + hdr_idx_first_pos(idx);
533 cur_idx = hdr_idx_first_idx(idx);
534
535 while (cur_idx) {
536 eol = sol + idx->v[cur_idx].len;
537
538 col = sol;
539 while (col < eol && *col != ':')
540 col++;
541
542 sov = col + 1;
543 while (sov < eol && http_is_lws[(unsigned char)*sov])
544 sov++;
545
546 for (h = cap_hdr; h; h = h->next) {
547 if ((h->namelen == col - sol) &&
548 (strncasecmp(sol, h->name, h->namelen) == 0)) {
549 if (cap[h->index] == NULL)
550 cap[h->index] =
551 pool_alloc_from(h->pool, h->len + 1);
552
553 if (cap[h->index] == NULL) {
554 Alert("HTTP capture : out of memory.\n");
555 continue;
556 }
557
558 len = eol - sov;
559 if (len > h->len)
560 len = h->len;
561
562 memcpy(cap[h->index], sov, len);
563 cap[h->index][len]=0;
564 }
565 }
566 sol = eol + idx->v[cur_idx].cr + 1;
567 cur_idx = idx->v[cur_idx].next;
568 }
569}
570
571
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100573 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100574 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
575 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
576 * will give undefined results.
577 * Note that it is upon the caller's responsibility to ensure that ptr < end,
578 * and that msg->sol points to the beginning of the response.
579 * If a complete line is found (which implies that at least one CR or LF is
580 * found before <end>, the updated <ptr> is returned, otherwise NULL is
581 * returned indicating an incomplete line (which does not mean that parts have
582 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
583 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
584 * upon next call.
585 *
586 * This function was intentionnally designed to be called from
587 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
588 * within its state machine and use the same macros, hence the need for same
589 * labels and variable names.
590 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100591const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100592 const char *ptr, const char *end,
593 char **ret_ptr, int *ret_state)
594{
595 __label__
596 http_msg_rpver,
597 http_msg_rpver_sp,
598 http_msg_rpcode,
599 http_msg_rpcode_sp,
600 http_msg_rpreason,
601 http_msg_rpline_eol,
602 http_msg_ood, /* out of data */
603 http_msg_invalid;
604
605 switch (state) {
606 http_msg_rpver:
607 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100608 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100609 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
610
611 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100612 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100613 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
614 }
615 goto http_msg_invalid;
616
617 http_msg_rpver_sp:
618 case HTTP_MSG_RPVER_SP:
619 if (likely(!HTTP_IS_LWS(*ptr))) {
620 msg->sl.st.c = ptr - msg_buf;
621 goto http_msg_rpcode;
622 }
623 if (likely(HTTP_IS_SPHT(*ptr)))
624 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
625 /* so it's a CR/LF, this is invalid */
626 goto http_msg_invalid;
627
628 http_msg_rpcode:
629 case HTTP_MSG_RPCODE:
630 if (likely(!HTTP_IS_LWS(*ptr)))
631 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
632
633 if (likely(HTTP_IS_SPHT(*ptr))) {
634 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
635 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
636 }
637
638 /* so it's a CR/LF, so there is no reason phrase */
639 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
640 http_msg_rsp_reason:
641 /* FIXME: should we support HTTP responses without any reason phrase ? */
642 msg->sl.st.r = ptr - msg_buf;
643 msg->sl.st.r_l = 0;
644 goto http_msg_rpline_eol;
645
646 http_msg_rpcode_sp:
647 case HTTP_MSG_RPCODE_SP:
648 if (likely(!HTTP_IS_LWS(*ptr))) {
649 msg->sl.st.r = ptr - msg_buf;
650 goto http_msg_rpreason;
651 }
652 if (likely(HTTP_IS_SPHT(*ptr)))
653 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
654 /* so it's a CR/LF, so there is no reason phrase */
655 goto http_msg_rsp_reason;
656
657 http_msg_rpreason:
658 case HTTP_MSG_RPREASON:
659 if (likely(!HTTP_IS_CRLF(*ptr)))
660 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
661 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
662 http_msg_rpline_eol:
663 /* We have seen the end of line. Note that we do not
664 * necessarily have the \n yet, but at least we know that we
665 * have EITHER \r OR \n, otherwise the response would not be
666 * complete. We can then record the response length and return
667 * to the caller which will be able to register it.
668 */
669 msg->sl.st.l = ptr - msg->sol;
670 return ptr;
671
672#ifdef DEBUG_FULL
673 default:
674 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
675 exit(1);
676#endif
677 }
678
679 http_msg_ood:
680 /* out of data */
681 if (ret_state)
682 *ret_state = state;
683 if (ret_ptr)
684 *ret_ptr = (char *)ptr;
685 return NULL;
686
687 http_msg_invalid:
688 /* invalid message */
689 if (ret_state)
690 *ret_state = HTTP_MSG_ERROR;
691 return NULL;
692}
693
694
695/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100696 * This function parses a request line between <ptr> and <end>, starting with
697 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
698 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
699 * will give undefined results.
700 * Note that it is upon the caller's responsibility to ensure that ptr < end,
701 * and that msg->sol points to the beginning of the request.
702 * If a complete line is found (which implies that at least one CR or LF is
703 * found before <end>, the updated <ptr> is returned, otherwise NULL is
704 * returned indicating an incomplete line (which does not mean that parts have
705 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
706 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
707 * upon next call.
708 *
709 * This function was intentionnally designed to be called from
710 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
711 * within its state machine and use the same macros, hence the need for same
712 * labels and variable names.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200713 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100714const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100715 const char *ptr, const char *end,
716 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100718 __label__
719 http_msg_rqmeth,
720 http_msg_rqmeth_sp,
721 http_msg_rquri,
722 http_msg_rquri_sp,
723 http_msg_rqver,
724 http_msg_rqline_eol,
725 http_msg_ood, /* out of data */
726 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100727
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100728 switch (state) {
729 http_msg_rqmeth:
730 case HTTP_MSG_RQMETH:
731 if (likely(HTTP_IS_TOKEN(*ptr)))
732 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100734 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100735 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100736 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
737 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100739 if (likely(HTTP_IS_CRLF(*ptr))) {
740 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100741 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100742 http_msg_req09_uri:
743 msg->sl.rq.u = ptr - msg_buf;
744 http_msg_req09_uri_e:
745 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
746 http_msg_req09_ver:
747 msg->sl.rq.v = ptr - msg_buf;
748 msg->sl.rq.v_l = 0;
749 goto http_msg_rqline_eol;
750 }
751 goto http_msg_invalid;
752
753 http_msg_rqmeth_sp:
754 case HTTP_MSG_RQMETH_SP:
755 if (likely(!HTTP_IS_LWS(*ptr))) {
756 msg->sl.rq.u = ptr - msg_buf;
757 goto http_msg_rquri;
758 }
759 if (likely(HTTP_IS_SPHT(*ptr)))
760 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
761 /* so it's a CR/LF, meaning an HTTP 0.9 request */
762 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100764 http_msg_rquri:
765 case HTTP_MSG_RQURI:
766 if (likely(!HTTP_IS_LWS(*ptr)))
767 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100768
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100769 if (likely(HTTP_IS_SPHT(*ptr))) {
770 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
771 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
772 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100773
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100774 /* so it's a CR/LF, meaning an HTTP 0.9 request */
775 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100777 http_msg_rquri_sp:
778 case HTTP_MSG_RQURI_SP:
779 if (likely(!HTTP_IS_LWS(*ptr))) {
780 msg->sl.rq.v = ptr - msg_buf;
781 goto http_msg_rqver;
782 }
783 if (likely(HTTP_IS_SPHT(*ptr)))
784 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
785 /* so it's a CR/LF, meaning an HTTP 0.9 request */
786 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100788 http_msg_rqver:
789 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100790 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100791 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100792
793 if (likely(HTTP_IS_CRLF(*ptr))) {
794 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
795 http_msg_rqline_eol:
796 /* We have seen the end of line. Note that we do not
797 * necessarily have the \n yet, but at least we know that we
798 * have EITHER \r OR \n, otherwise the request would not be
799 * complete. We can then record the request length and return
800 * to the caller which will be able to register it.
801 */
802 msg->sl.rq.l = ptr - msg->sol;
803 return ptr;
804 }
805
806 /* neither an HTTP_VER token nor a CRLF */
807 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100808
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100809#ifdef DEBUG_FULL
810 default:
811 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
812 exit(1);
813#endif
814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100815
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100816 http_msg_ood:
817 /* out of data */
818 if (ret_state)
819 *ret_state = state;
820 if (ret_ptr)
821 *ret_ptr = (char *)ptr;
822 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100824 http_msg_invalid:
825 /* invalid message */
826 if (ret_state)
827 *ret_state = HTTP_MSG_ERROR;
828 return NULL;
829}
Willy Tarreau58f10d72006-12-04 02:26:12 +0100830
831
Willy Tarreau8973c702007-01-21 23:58:29 +0100832/*
833 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100834 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +0100835 * when data are missing and recalled at the exact same location with no
836 * information loss. The header index is re-initialized when switching from
837 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH.
838 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100839void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
840{
841 __label__
842 http_msg_rqbefore,
843 http_msg_rqbefore_cr,
844 http_msg_rqmeth,
845 http_msg_rqline_end,
846 http_msg_hdr_first,
847 http_msg_hdr_name,
848 http_msg_hdr_l1_sp,
849 http_msg_hdr_l1_lf,
850 http_msg_hdr_l1_lws,
851 http_msg_hdr_val,
852 http_msg_hdr_l2_lf,
853 http_msg_hdr_l2_lws,
854 http_msg_complete_header,
855 http_msg_last_lf,
856 http_msg_ood, /* out of data */
857 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100858
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100859 int state; /* updated only when leaving the FSM */
860 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100861
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100862 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100863 ptr = buf->lr;
864 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100865
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100866 if (unlikely(ptr >= end))
867 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100868
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100869 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100870 /*
871 * First, states that are specific to the response only.
872 * We check them first so that request and headers are
873 * closer to each other (accessed more often).
874 */
875 http_msg_rpbefore:
876 case HTTP_MSG_RPBEFORE:
877 if (likely(HTTP_IS_TOKEN(*ptr))) {
878 if (likely(ptr == buf->data)) {
879 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100880 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +0100881 } else {
882#if PARSE_PRESERVE_EMPTY_LINES
883 /* only skip empty leading lines, don't remove them */
884 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100885 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +0100886#else
887 /* Remove empty leading lines, as recommended by
888 * RFC2616. This takes a lot of time because we
889 * must move all the buffer backwards, but this
890 * is rarely needed. The method above will be
891 * cleaner when we'll be able to start sending
892 * the request from any place in the buffer.
893 */
894 buf->lr = ptr;
895 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100896 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +0100897 msg->sol = buf->data;
898 ptr = buf->data;
899 end = buf->r;
900#endif
901 }
902 hdr_idx_init(idx);
903 state = HTTP_MSG_RPVER;
904 goto http_msg_rpver;
905 }
906
907 if (unlikely(!HTTP_IS_CRLF(*ptr)))
908 goto http_msg_invalid;
909
910 if (unlikely(*ptr == '\n'))
911 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
912 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
913 /* stop here */
914
915 http_msg_rpbefore_cr:
916 case HTTP_MSG_RPBEFORE_CR:
917 EXPECT_LF_HERE(ptr, http_msg_invalid);
918 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
919 /* stop here */
920
921 http_msg_rpver:
922 case HTTP_MSG_RPVER:
923 case HTTP_MSG_RPVER_SP:
924 case HTTP_MSG_RPCODE:
925 case HTTP_MSG_RPCODE_SP:
926 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +0100927 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100928 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +0100929 if (unlikely(!ptr))
930 return;
931
932 /* we have a full response and we know that we have either a CR
933 * or an LF at <ptr>.
934 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100935 //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 +0100936 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
937
938 msg->sol = ptr;
939 if (likely(*ptr == '\r'))
940 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
941 goto http_msg_rpline_end;
942
943 http_msg_rpline_end:
944 case HTTP_MSG_RPLINE_END:
945 /* msg->sol must point to the first of CR or LF. */
946 EXPECT_LF_HERE(ptr, http_msg_invalid);
947 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
948 /* stop here */
949
950 /*
951 * Second, states that are specific to the request only
952 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100953 http_msg_rqbefore:
954 case HTTP_MSG_RQBEFORE:
955 if (likely(HTTP_IS_TOKEN(*ptr))) {
956 if (likely(ptr == buf->data)) {
957 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100958 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100959 } else {
960#if PARSE_PRESERVE_EMPTY_LINES
961 /* only skip empty leading lines, don't remove them */
962 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100963 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100964#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100965 /* Remove empty leading lines, as recommended by
966 * RFC2616. This takes a lot of time because we
967 * must move all the buffer backwards, but this
968 * is rarely needed. The method above will be
969 * cleaner when we'll be able to start sending
970 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100971 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100972 buf->lr = ptr;
973 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100974 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100975 msg->sol = buf->data;
976 ptr = buf->data;
977 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100978#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100979 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +0100980 /* we will need this when keep-alive will be supported
981 hdr_idx_init(idx);
982 */
Willy Tarreau8973c702007-01-21 23:58:29 +0100983 state = HTTP_MSG_RQMETH;
984 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100985 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100987 if (unlikely(!HTTP_IS_CRLF(*ptr)))
988 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100989
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100990 if (unlikely(*ptr == '\n'))
991 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
992 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +0100993 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100994
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100995 http_msg_rqbefore_cr:
996 case HTTP_MSG_RQBEFORE_CR:
997 EXPECT_LF_HERE(ptr, http_msg_invalid);
998 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +0100999 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001000
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001001 http_msg_rqmeth:
1002 case HTTP_MSG_RQMETH:
1003 case HTTP_MSG_RQMETH_SP:
1004 case HTTP_MSG_RQURI:
1005 case HTTP_MSG_RQURI_SP:
1006 case HTTP_MSG_RQVER:
1007 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001008 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001009 if (unlikely(!ptr))
1010 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001011
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001012 /* we have a full request and we know that we have either a CR
1013 * or an LF at <ptr>.
1014 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001015 //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 +01001016 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001017
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001018 msg->sol = ptr;
1019 if (likely(*ptr == '\r'))
1020 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001021 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001022
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001023 http_msg_rqline_end:
1024 case HTTP_MSG_RQLINE_END:
1025 /* check for HTTP/0.9 request : no version information available.
1026 * msg->sol must point to the first of CR or LF.
1027 */
1028 if (unlikely(msg->sl.rq.v_l == 0))
1029 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001030
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001031 EXPECT_LF_HERE(ptr, http_msg_invalid);
1032 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001033 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001034
Willy Tarreau8973c702007-01-21 23:58:29 +01001035 /*
1036 * Common states below
1037 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001038 http_msg_hdr_first:
1039 case HTTP_MSG_HDR_FIRST:
1040 msg->sol = ptr;
1041 if (likely(!HTTP_IS_CRLF(*ptr))) {
1042 goto http_msg_hdr_name;
1043 }
1044
1045 if (likely(*ptr == '\r'))
1046 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1047 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001048
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001049 http_msg_hdr_name:
1050 case HTTP_MSG_HDR_NAME:
1051 /* assumes msg->sol points to the first char */
1052 if (likely(HTTP_IS_TOKEN(*ptr)))
1053 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001054
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001055 if (likely(*ptr == ':')) {
1056 msg->col = ptr - buf->data;
1057 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1058 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001059
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001060 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001062 http_msg_hdr_l1_sp:
1063 case HTTP_MSG_HDR_L1_SP:
1064 /* assumes msg->sol points to the first char and msg->col to the colon */
1065 if (likely(HTTP_IS_SPHT(*ptr)))
1066 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 /* header value can be basically anything except CR/LF */
1069 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 if (likely(!HTTP_IS_CRLF(*ptr))) {
1072 goto http_msg_hdr_val;
1073 }
1074
1075 if (likely(*ptr == '\r'))
1076 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1077 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 http_msg_hdr_l1_lf:
1080 case HTTP_MSG_HDR_L1_LF:
1081 EXPECT_LF_HERE(ptr, http_msg_invalid);
1082 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 http_msg_hdr_l1_lws:
1085 case HTTP_MSG_HDR_L1_LWS:
1086 if (likely(HTTP_IS_SPHT(*ptr))) {
1087 /* replace HT,CR,LF with spaces */
1088 for (; buf->data+msg->sov < ptr; msg->sov++)
1089 buf->data[msg->sov] = ' ';
1090 goto http_msg_hdr_l1_sp;
1091 }
Willy Tarreaub9ebf702007-01-26 23:39:38 +01001092 msg->eol = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 goto http_msg_complete_header;
1094
1095 http_msg_hdr_val:
1096 case HTTP_MSG_HDR_VAL:
1097 /* assumes msg->sol points to the first char, msg->col to the
1098 * colon, and msg->sov points to the first character of the
1099 * value.
1100 */
1101 if (likely(!HTTP_IS_CRLF(*ptr)))
1102 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001103
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 msg->eol = ptr;
1105 /* Note: we could also copy eol into ->eoh so that we have the
1106 * real header end in case it ends with lots of LWS, but is this
1107 * really needed ?
1108 */
1109 if (likely(*ptr == '\r'))
1110 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1111 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 http_msg_hdr_l2_lf:
1114 case HTTP_MSG_HDR_L2_LF:
1115 EXPECT_LF_HERE(ptr, http_msg_invalid);
1116 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001117
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001118 http_msg_hdr_l2_lws:
1119 case HTTP_MSG_HDR_L2_LWS:
1120 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1121 /* LWS: replace HT,CR,LF with spaces */
1122 for (; msg->eol < ptr; msg->eol++)
1123 *msg->eol = ' ';
1124 goto http_msg_hdr_val;
1125 }
1126 http_msg_complete_header:
1127 /*
1128 * It was a new header, so the last one is finished.
1129 * Assumes msg->sol points to the first char, msg->col to the
1130 * colon, msg->sov points to the first character of the value
1131 * and msg->eol to the first CR or LF so we know how the line
1132 * ends. We insert last header into the index.
1133 */
1134 /*
1135 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1136 write(2, msg->sol, msg->eol-msg->sol);
1137 fprintf(stderr,"\n");
1138 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001139
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001140 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1141 idx, idx->tail) < 0))
1142 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144 msg->sol = ptr;
1145 if (likely(!HTTP_IS_CRLF(*ptr))) {
1146 goto http_msg_hdr_name;
1147 }
1148
1149 if (likely(*ptr == '\r'))
1150 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1151 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001152
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001153 http_msg_last_lf:
1154 case HTTP_MSG_LAST_LF:
1155 /* Assumes msg->sol points to the first of either CR or LF */
1156 EXPECT_LF_HERE(ptr, http_msg_invalid);
1157 ptr++;
1158 buf->lr = ptr;
1159 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001160 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 return;
1162#ifdef DEBUG_FULL
1163 default:
1164 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1165 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001166#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001167 }
1168 http_msg_ood:
1169 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001170 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 buf->lr = ptr;
1172 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001173
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001174 http_msg_invalid:
1175 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001176 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 return;
1178}
1179
1180/*
1181 * manages the client FSM and its socket. BTW, it also tries to handle the
1182 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1183 * 0 else.
1184 */
1185int process_cli(struct session *t)
1186{
1187 int s = t->srv_state;
1188 int c = t->cli_state;
1189 struct buffer *req = t->req;
1190 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001191
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001192 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1193 cli_stnames[c], srv_stnames[s],
1194 MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
1195 req->rex.tv_sec, req->rex.tv_usec,
1196 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001197
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001198 if (c == CL_STHEADERS) {
1199 /*
1200 * Now parse the partial (or complete) lines.
1201 * We will check the request syntax, and also join multi-line
1202 * headers. An index of all the lines will be elaborated while
1203 * parsing.
1204 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001205 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001206 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001207 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001208 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001209 * req->data + req->eoh = end of processed headers / start of current one
1210 * req->data + req->eol = end of current header or line (LF or CRLF)
1211 * req->lr = first non-visited byte
1212 * req->r = end of data
1213 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001214
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001215 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001216 struct http_txn *txn = &t->txn;
1217 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001218 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001219
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001220 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001221 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001222
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 /* 1: we might have to print this header in debug mode */
1224 if (unlikely((global.mode & MODE_DEBUG) &&
1225 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001226 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001227 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001228
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001229 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230 eol = sol + msg->sl.rq.l;
1231 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001232
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001233 sol += hdr_idx_first_pos(&txn->hdr_idx);
1234 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001237 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001238 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001239 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1240 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 }
1242 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001243
Willy Tarreau58f10d72006-12-04 02:26:12 +01001244
1245 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001246 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001247 * If not so, we check the FD and buffer states before leaving.
1248 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001249 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1250 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001251 *
1252 */
1253
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001254 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 /*
1256 * First, let's catch bad requests.
1257 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001258 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001260
1261 /* 1: Since we are in header mode, if there's no space
1262 * left for headers, we won't be able to free more
1263 * later, so the session will never terminate. We
1264 * must terminate it now.
1265 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 if (unlikely(req->l >= req->rlim - req->data)) {
1267 /* FIXME: check if URI is set and return Status
1268 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001269 */
Willy Tarreau06619262006-12-17 08:37:22 +01001270 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001271 }
1272
1273 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1275 /* read error, or last read : give up. */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001276 tv_eternity(&req->rex);
1277 fd_delete(t->cli_fd);
1278 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001279 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001280 if (!(t->flags & SN_ERR_MASK))
1281 t->flags |= SN_ERR_CLICL;
1282 if (!(t->flags & SN_FINST_MASK))
1283 t->flags |= SN_FINST_R;
1284 return 1;
1285 }
1286
1287 /* 3: has the read timeout expired ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 else if (unlikely(tv_cmp2_ms(&req->rex, &now) <= 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001290 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001291 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001292 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001293 if (!(t->flags & SN_ERR_MASK))
1294 t->flags |= SN_ERR_CLITO;
1295 if (!(t->flags & SN_FINST_MASK))
1296 t->flags |= SN_FINST_R;
1297 return 1;
1298 }
1299
1300 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 else if (unlikely(! MY_FD_ISSET(t->cli_fd, StaticReadEvent))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001302 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
1303 * full. We cannot loop here since stream_sock_read will disable it only if
1304 * req->l == rlim-data
1305 */
1306 MY_FD_SET(t->cli_fd, StaticReadEvent);
1307 if (t->fe->clitimeout)
1308 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
1309 else
1310 tv_eternity(&req->rex);
1311 }
1312 return t->cli_state != CL_STHEADERS;
1313 }
1314
1315
1316 /****************************************************************
1317 * More interesting part now : we know that we have a complete *
1318 * request which at least looks like HTTP. We have an indicator *
1319 * of each header's length, so we can parse them quickly. *
1320 ****************************************************************/
1321
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 /*
1323 * 1: identify the method
1324 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001325 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001326
1327 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001329 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001331 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 if (unlikely((t->fe->monitor_uri_len != 0) &&
1333 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1334 !memcmp(&req->data[msg->sl.rq.u],
1335 t->fe->monitor_uri,
1336 t->fe->monitor_uri_len))) {
1337 /*
1338 * We have found the monitor URI
1339 */
1340 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001341 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 client_retnclose(t, &http_200_chunk);
1343 goto return_prx_cond;
1344 }
1345
1346 /*
1347 * 3: Maybe we have to copy the original REQURI for the logs ?
1348 * Note: we cannot log anymore if the request has been
1349 * classified as invalid.
1350 */
1351 if (unlikely(t->logs.logwait & LW_REQ)) {
1352 /* we have a complete HTTP request that we must log */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001353 if ((txn->uri = pool_alloc(requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 int urilen = msg->sl.rq.l;
1355
1356 if (urilen >= REQURI_LEN)
1357 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001358 memcpy(txn->uri, &req->data[msg->som], urilen);
1359 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360
1361 if (!(t->logs.logwait &= ~LW_REQ))
1362 sess_log(t);
1363 } else {
1364 Alert("HTTP logging : out of memory.\n");
1365 }
1366 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
Willy Tarreau06619262006-12-17 08:37:22 +01001368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1370 if (unlikely(msg->sl.rq.v_l == 0)) {
1371 int delta;
1372 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001373 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 cur_end = msg->sol + msg->sl.rq.l;
1375 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001376
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 if (msg->sl.rq.u_l == 0) {
1378 /* if no URI was set, add "/" */
1379 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1380 cur_end += delta;
1381 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001382 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 /* add HTTP version */
1384 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1385 msg->eoh += delta;
1386 cur_end += delta;
1387 cur_end = (char *)http_parse_reqline(msg, req->data,
1388 HTTP_MSG_RQMETH,
1389 msg->sol, cur_end + 1,
1390 NULL, NULL);
1391 if (unlikely(!cur_end))
1392 goto return_bad_req;
1393
1394 /* we have a full HTTP/1.0 request now and we know that
1395 * we have either a CR or an LF at <ptr>.
1396 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001397 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001398 }
1399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400
1401 /* 5: we may need to capture headers */
Willy Tarreau117f59e2007-03-04 18:17:17 +01001402 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->fiprm->req_cap))
1403 capture_headers(req->data + msg->som, &txn->hdr_idx,
1404 txn->req.cap, t->fe->fiprm->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001405
1406 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001408 * As opposed to version 1.2, now they will be evaluated in the
1409 * filters order and not in the header order. This means that
1410 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001411 *
1412 * We can now check whether we want to switch to another
1413 * backend, in which case we will re-check the backend's
1414 * filters and various options. In order to support 3-level
1415 * switching, here's how we should proceed :
1416 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001417 * a) run be->fiprm.
1418 * if (switch) then switch ->be to the new backend.
1419 * b) run be->fiprm if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001420 * There cannot be any switch from there, so ->be cannot be
1421 * changed anymore.
1422 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001423 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001424 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001425 * The response path will be able to apply either ->be, or
1426 * ->be then ->fe filters in order to match the reverse of
1427 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001428 */
1429
Willy Tarreau06619262006-12-17 08:37:22 +01001430 do {
Willy Tarreau830ff452006-12-17 19:31:23 +01001431 struct proxy *rule_set = t->be->fiprm;
1432 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau06619262006-12-17 08:37:22 +01001434 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001435 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1437 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001438 }
1439
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001440 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1441 /* to ensure correct connection accounting on
1442 * the backend, we count the connection for the
1443 * one managing the queue.
1444 */
1445 t->be->beprm->beconn++;
1446 if (t->be->beprm->beconn > t->be->beprm->beconn_max)
1447 t->be->beprm->beconn_max = t->be->beprm->beconn;
1448 t->be->beprm->cum_beconn++;
1449 t->flags |= SN_BE_ASSIGNED;
1450 }
1451
Willy Tarreau06619262006-12-17 08:37:22 +01001452 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001453 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001454 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001455 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001456 /* let's log the request time */
1457 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001458 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001459 goto return_prx_cond;
1460 }
1461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 /* We might have to check for "Connection:" */
1463 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
1464 !(t->flags & SN_CONN_CLOSED)) {
1465 char *cur_ptr, *cur_end, *cur_next;
1466 int cur_idx, old_idx, delta;
1467 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001468
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001469 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 old_idx = 0;
1471
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001472 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1473 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 cur_ptr = cur_next;
1475 cur_end = cur_ptr + cur_hdr->len;
1476 cur_next = cur_end + cur_hdr->cr + 1;
1477
1478 if (strncasecmp(cur_ptr, "Connection:", 11) == 0) {
1479 /* 3 possibilities :
1480 * - we have already set Connection: close,
1481 * so we remove this line.
1482 * - we have not yet set Connection: close,
1483 * but this line indicates close. We leave
1484 * it untouched and set the flag.
1485 * - we have not yet set Connection: close,
1486 * and this line indicates non-close. We
1487 * replace it.
1488 */
1489 if (t->flags & SN_CONN_CLOSED) {
1490 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001491 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001493 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1494 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 cur_hdr->len = 0;
1496 } else {
1497 if (cur_ptr + 17 > cur_end ||
1498 !http_is_lws[(unsigned char)*(cur_ptr+17)] ||
1499 strncasecmp(cur_ptr+11, " close", 6)) {
1500 delta = buffer_replace2(req, cur_ptr+11, cur_end,
1501 " close", 6);
1502 cur_next += delta;
1503 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001504 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 }
1506 t->flags |= SN_CONN_CLOSED;
1507 }
1508 }
1509 old_idx = cur_idx;
1510 }
1511
1512 /* add request headers from the rule sets in the same order */
1513 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1514 int len;
1515
1516 len = sprintf(trash, "%s\r\n", rule_set->req_add[cur_idx]);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001517 len = buffer_replace2(req, req->data + txn->req.eoh,
1518 req->data + txn->req.eoh, trash, len);
1519 txn->req.eoh += len;
Willy Tarreau06619262006-12-17 08:37:22 +01001520
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001521 if (unlikely(hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 goto return_bad_req;
1523 }
Willy Tarreau06619262006-12-17 08:37:22 +01001524 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001527 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001528 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001529 /* we have to check the URI and auth for this request */
1530 if (stats_check_uri_auth(t, rule_set))
1531 return 1;
1532 }
1533
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001534 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1535 /* No backend was set, but there was a default
1536 * backend set in the frontend, so we use it and
1537 * loop again.
1538 */
1539 t->be = cur_proxy->defbe.be;
1540 t->be->beprm->beconn++;
1541 if (t->be->beprm->beconn > t->be->beprm->beconn_max)
1542 t->be->beprm->beconn_max = t->be->beprm->beconn;
1543 t->be->beprm->cum_beconn++;
1544 t->flags |= SN_BE_ASSIGNED;
1545 }
1546 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001547
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001549 if (!(t->flags & SN_BE_ASSIGNED)) {
1550 /* To ensure correct connection accounting on
1551 * the backend, we count the connection for the
1552 * one managing the queue.
1553 */
1554 t->be->beprm->beconn++;
1555 if (t->be->beprm->beconn > t->be->beprm->beconn_max)
1556 t->be->beprm->beconn_max = t->be->beprm->beconn;
1557 t->be->beprm->cum_beconn++;
1558 t->flags |= SN_BE_ASSIGNED;
1559 }
1560
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001561 /*
1562 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001563 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001564 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001565 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001566 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001567
Willy Tarreau58f10d72006-12-04 02:26:12 +01001568
Willy Tarreau58f10d72006-12-04 02:26:12 +01001569
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreau2a324282006-12-05 00:05:46 +01001571 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001573 * so let's do the same now.
1574 */
1575
1576 /* It needs to look into the URI */
Willy Tarreau830ff452006-12-17 19:31:23 +01001577 if (t->be->beprm->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001578 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001579 }
1580
1581
1582 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001584 * Note that doing so might move headers in the request, but
1585 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001586 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001587 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001588 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001589 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001590
Willy Tarreau58f10d72006-12-04 02:26:12 +01001591
Willy Tarreau2a324282006-12-05 00:05:46 +01001592 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001593 * 9: add X-Forwarded-For if either the frontend or the backend
1594 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001595 */
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001596 if ((t->fe->options | t->be->beprm->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001597 if (t->cli_addr.ss_family == AF_INET) {
1598 int len;
1599 unsigned char *pn;
1600 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
1601 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d\r\n",
1602 pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001603 len = buffer_replace2(req, req->data + txn->req.eoh,
1604 req->data + txn->req.eoh, trash, len);
1605 txn->req.eoh += len;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001606
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001607 if (hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001608 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001609 }
1610 else if (t->cli_addr.ss_family == AF_INET6) {
1611 int len;
1612 char pn[INET6_ADDRSTRLEN];
1613 inet_ntop(AF_INET6,
1614 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1615 pn, sizeof(pn));
1616 len = sprintf(trash, "X-Forwarded-For: %s\r\n", pn);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001617 len = buffer_replace2(req, req->data + txn->req.eoh,
1618 req->data + txn->req.eoh, trash, len);
1619 txn->req.eoh += len;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001620
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001621 if (hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001622 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001623 }
1624 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001625
Willy Tarreau2a324282006-12-05 00:05:46 +01001626 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreaub2513902006-12-17 14:52:38 +01001628 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
1630 !(t->flags & SN_CONN_CLOSED)) {
Willy Tarreau45e73e32006-12-17 00:05:15 +01001631 int len;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001632 len = buffer_replace2(req, req->data + txn->req.eoh,
1633 req->data + txn->req.eoh, "Connection: close\r\n", 19);
1634 txn->req.eoh += len;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001635
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001636 if (hdr_idx_add(17, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001637 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001638 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001639 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001640
Willy Tarreau2a324282006-12-05 00:05:46 +01001641 /*************************************************************
1642 * OK, that's finished for the headers. We have done what we *
1643 * could. Let's switch to the DATA state. *
1644 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001645
Willy Tarreau2a324282006-12-05 00:05:46 +01001646 t->cli_state = CL_STDATA;
1647 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001648
Willy Tarreau2a324282006-12-05 00:05:46 +01001649 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001650
Willy Tarreau2a324282006-12-05 00:05:46 +01001651 if (!t->fe->clitimeout ||
Willy Tarreau830ff452006-12-17 19:31:23 +01001652 (t->srv_state < SV_STDATA && t->be->beprm->srvtimeout)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001653 /* If the client has no timeout, or if the server is not ready yet,
1654 * and we know for sure that it can expire, then it's cleaner to
1655 * disable the timeout on the client side so that too low values
1656 * cannot make the sessions abort too early.
1657 *
1658 * FIXME-20050705: the server needs a way to re-enable this time-out
1659 * when it switches its state, otherwise a client can stay connected
1660 * indefinitely. This now seems to be OK.
1661 */
1662 tv_eternity(&req->rex);
1663 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001664
Willy Tarreau2a324282006-12-05 00:05:46 +01001665 /* When a connection is tarpitted, we use the queue timeout for the
1666 * tarpit delay, which currently happens to be the server's connect
1667 * timeout. If unset, then set it to zero because we really want it
1668 * to expire at one moment.
1669 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001670 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001671 t->req->l = 0;
1672 /* flush the request so that we can drop the connection early
1673 * if the client closes first.
1674 */
1675 tv_delayfrom(&req->cex, &now,
Willy Tarreau830ff452006-12-17 19:31:23 +01001676 t->be->beprm->contimeout ? t->be->beprm->contimeout : 0);
Willy Tarreau2a324282006-12-05 00:05:46 +01001677 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001678
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01001680 goto process_data;
1681
1682 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001683 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001684 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01001685 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001686 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01001687 return_prx_cond:
1688 if (!(t->flags & SN_ERR_MASK))
1689 t->flags |= SN_ERR_PRXCOND;
1690 if (!(t->flags & SN_FINST_MASK))
1691 t->flags |= SN_FINST_R;
1692 return 1;
1693
Willy Tarreaubaaee002006-06-26 02:48:02 +02001694 }
1695 else if (c == CL_STDATA) {
1696 process_data:
1697 /* FIXME: this error handling is partly buggy because we always report
1698 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1699 * or HEADER phase. BTW, it's not logical to expire the client while
1700 * we're waiting for the server to connect.
1701 */
1702 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001703 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001704 tv_eternity(&req->rex);
1705 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001706 fd_delete(t->cli_fd);
1707 t->cli_state = CL_STCLOSE;
1708 if (!(t->flags & SN_ERR_MASK))
1709 t->flags |= SN_ERR_CLICL;
1710 if (!(t->flags & SN_FINST_MASK)) {
1711 if (t->pend_pos)
1712 t->flags |= SN_FINST_Q;
1713 else if (s == SV_STCONN)
1714 t->flags |= SN_FINST_C;
1715 else
1716 t->flags |= SN_FINST_D;
1717 }
1718 return 1;
1719 }
1720 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001721 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001722 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001723 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001724 shutdown(t->cli_fd, SHUT_RD);
1725 t->cli_state = CL_STSHUTR;
1726 return 1;
1727 }
1728 /* last server read and buffer empty */
1729 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001730 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001731 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001732 shutdown(t->cli_fd, SHUT_WR);
1733 /* We must ensure that the read part is still alive when switching
1734 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001735 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001736 if (t->fe->clitimeout)
1737 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001738 t->cli_state = CL_STSHUTW;
1739 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1740 return 1;
1741 }
1742 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001743 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001744 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001745 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001746 shutdown(t->cli_fd, SHUT_RD);
1747 t->cli_state = CL_STSHUTR;
1748 if (!(t->flags & SN_ERR_MASK))
1749 t->flags |= SN_ERR_CLITO;
1750 if (!(t->flags & SN_FINST_MASK)) {
1751 if (t->pend_pos)
1752 t->flags |= SN_FINST_Q;
1753 else if (s == SV_STCONN)
1754 t->flags |= SN_FINST_C;
1755 else
1756 t->flags |= SN_FINST_D;
1757 }
1758 return 1;
1759 }
1760 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001761 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001762 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001763 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001764 shutdown(t->cli_fd, SHUT_WR);
1765 /* We must ensure that the read part is still alive when switching
1766 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001767 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001768 if (t->fe->clitimeout)
1769 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001770
1771 t->cli_state = CL_STSHUTW;
1772 if (!(t->flags & SN_ERR_MASK))
1773 t->flags |= SN_ERR_CLITO;
1774 if (!(t->flags & SN_FINST_MASK)) {
1775 if (t->pend_pos)
1776 t->flags |= SN_FINST_Q;
1777 else if (s == SV_STCONN)
1778 t->flags |= SN_FINST_C;
1779 else
1780 t->flags |= SN_FINST_D;
1781 }
1782 return 1;
1783 }
1784
1785 if (req->l >= req->rlim - req->data) {
1786 /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02001787 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001788 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02001789 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001790 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001791 }
1792 } else {
1793 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02001794 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
1795 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001796 if (!t->fe->clitimeout ||
Willy Tarreau830ff452006-12-17 19:31:23 +01001797 (t->srv_state < SV_STDATA && t->be->beprm->srvtimeout))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001798 /* If the client has no timeout, or if the server not ready yet, and we
1799 * know for sure that it can expire, then it's cleaner to disable the
1800 * timeout on the client side so that too low values cannot make the
1801 * sessions abort too early.
1802 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001803 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001804 else
Willy Tarreau73de9892006-11-30 11:40:23 +01001805 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001806 }
1807 }
1808
1809 if ((rep->l == 0) ||
1810 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001811 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1812 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001813 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001814 }
1815 } else {
1816 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001817 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1818 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001819 if (t->fe->clitimeout) {
1820 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001821 /* FIXME: to prevent the client from expiring read timeouts during writes,
1822 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001823 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001824 }
1825 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001826 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001827 }
1828 }
1829 return 0; /* other cases change nothing */
1830 }
1831 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001832 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001833 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001834 fd_delete(t->cli_fd);
1835 t->cli_state = CL_STCLOSE;
1836 if (!(t->flags & SN_ERR_MASK))
1837 t->flags |= SN_ERR_CLICL;
1838 if (!(t->flags & SN_FINST_MASK)) {
1839 if (t->pend_pos)
1840 t->flags |= SN_FINST_Q;
1841 else if (s == SV_STCONN)
1842 t->flags |= SN_FINST_C;
1843 else
1844 t->flags |= SN_FINST_D;
1845 }
1846 return 1;
1847 }
1848 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
1849 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001850 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001851 fd_delete(t->cli_fd);
1852 t->cli_state = CL_STCLOSE;
1853 return 1;
1854 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001855 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
1856 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001857 fd_delete(t->cli_fd);
1858 t->cli_state = CL_STCLOSE;
1859 if (!(t->flags & SN_ERR_MASK))
1860 t->flags |= SN_ERR_CLITO;
1861 if (!(t->flags & SN_FINST_MASK)) {
1862 if (t->pend_pos)
1863 t->flags |= SN_FINST_Q;
1864 else if (s == SV_STCONN)
1865 t->flags |= SN_FINST_C;
1866 else
1867 t->flags |= SN_FINST_D;
1868 }
1869 return 1;
1870 }
1871
1872 if (t->flags & SN_SELF_GEN) {
1873 produce_content(t);
1874 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001875 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001876 fd_delete(t->cli_fd);
1877 t->cli_state = CL_STCLOSE;
1878 return 1;
1879 }
1880 }
1881
1882 if ((rep->l == 0)
1883 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001884 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1885 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001886 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001887 }
1888 } else {
1889 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001890 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1891 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001892 if (t->fe->clitimeout) {
1893 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001894 /* FIXME: to prevent the client from expiring read timeouts during writes,
1895 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001896 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001897 }
1898 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001899 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001900 }
1901 }
1902 return 0;
1903 }
1904 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001905 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001906 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001907 fd_delete(t->cli_fd);
1908 t->cli_state = CL_STCLOSE;
1909 if (!(t->flags & SN_ERR_MASK))
1910 t->flags |= SN_ERR_CLICL;
1911 if (!(t->flags & SN_FINST_MASK)) {
1912 if (t->pend_pos)
1913 t->flags |= SN_FINST_Q;
1914 else if (s == SV_STCONN)
1915 t->flags |= SN_FINST_C;
1916 else
1917 t->flags |= SN_FINST_D;
1918 }
1919 return 1;
1920 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001921 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001922 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001923 fd_delete(t->cli_fd);
1924 t->cli_state = CL_STCLOSE;
1925 return 1;
1926 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001927 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
1928 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001929 fd_delete(t->cli_fd);
1930 t->cli_state = CL_STCLOSE;
1931 if (!(t->flags & SN_ERR_MASK))
1932 t->flags |= SN_ERR_CLITO;
1933 if (!(t->flags & SN_FINST_MASK)) {
1934 if (t->pend_pos)
1935 t->flags |= SN_FINST_Q;
1936 else if (s == SV_STCONN)
1937 t->flags |= SN_FINST_C;
1938 else
1939 t->flags |= SN_FINST_D;
1940 }
1941 return 1;
1942 }
1943 else if (req->l >= req->rlim - req->data) {
1944 /* no room to read more data */
1945
1946 /* FIXME-20050705: is it possible for a client to maintain a session
1947 * after the timeout by sending more data after it receives a close ?
1948 */
1949
Willy Tarreau2a429502006-10-15 14:52:29 +02001950 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001951 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02001952 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001953 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001954 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1955 }
1956 } else {
1957 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02001958 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
1959 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001960 if (t->fe->clitimeout)
1961 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001962 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001963 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001964 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1965 }
1966 }
1967 return 0;
1968 }
1969 else { /* CL_STCLOSE: nothing to do */
1970 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1971 int len;
Willy Tarreau830ff452006-12-17 19:31:23 +01001972 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->beprm->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001973 write(1, trash, len);
1974 }
1975 return 0;
1976 }
1977 return 0;
1978}
1979
1980
1981/*
1982 * manages the server FSM and its socket. It returns 1 if a state has changed
1983 * (and a resync may be needed), 0 else.
1984 */
1985int process_srv(struct session *t)
1986{
1987 int s = t->srv_state;
1988 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01001989 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001990 struct buffer *req = t->req;
1991 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001992 int conn_err;
1993
1994#ifdef DEBUG_FULL
1995 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
1996#endif
1997 //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 +02001998 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
1999 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002000 //);
2001 if (s == SV_STIDLE) {
2002 if (c == CL_STHEADERS)
2003 return 0; /* stay in idle, waiting for data to reach the client side */
2004 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2005 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002006 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002007 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002008 if (t->pend_pos)
2009 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2010 /* note that this must not return any error because it would be able to
2011 * overwrite the client_retnclose() output.
2012 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002013 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002014 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002015 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002016 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 +02002017
2018 return 1;
2019 }
2020 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002021 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002022 /* This connection is being tarpitted. The CLIENT side has
2023 * already set the connect expiration date to the right
2024 * timeout. We just have to check that it has not expired.
2025 */
2026 if (tv_cmp2_ms(&req->cex, &now) > 0)
2027 return 0;
2028
2029 /* We will set the queue timer to the time spent, just for
2030 * logging purposes. We fake a 500 server error, so that the
2031 * attacker will not suspect his connection has been tarpitted.
2032 * It will not cause trouble to the logs because we can exclude
2033 * the tarpitted connections by filtering on the 'PT' status flags.
2034 */
2035 tv_eternity(&req->cex);
2036 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2037 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002038 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002039 return 1;
2040 }
2041
Willy Tarreaubaaee002006-06-26 02:48:02 +02002042 /* Right now, we will need to create a connection to the server.
2043 * We might already have tried, and got a connection pending, in
2044 * which case we will not do anything till it's pending. It's up
2045 * to any other session to release it and wake us up again.
2046 */
2047 if (t->pend_pos) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002048 if (tv_cmp2_ms(&req->cex, &now) > 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002049 return 0;
2050 else {
2051 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002052 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002053 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2054 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002055 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002056 if (t->srv)
2057 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002058 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002059 return 1;
2060 }
2061 }
2062
2063 do {
2064 /* first, get a connection */
2065 if (srv_redispatch_connect(t))
2066 return t->srv_state != SV_STIDLE;
2067
2068 /* try to (re-)connect to the server, and fail if we expire the
2069 * number of retries.
2070 */
2071 if (srv_retryable_connect(t)) {
2072 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2073 return t->srv_state != SV_STIDLE;
2074 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002075 } while (1);
2076 }
2077 }
2078 else if (s == SV_STCONN) { /* connection in progress */
2079 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2080 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002081 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002082 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002083 fd_delete(t->srv_fd);
2084 if (t->srv)
2085 t->srv->cur_sess--;
2086
2087 /* note that this must not return any error because it would be able to
2088 * overwrite the client_retnclose() output.
2089 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002090 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002091 return 1;
2092 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002093 if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) {
2094 //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 +02002095 return 0; /* nothing changed */
2096 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002097 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002098 /* timeout, asynchronous connect error or first write error */
2099 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2100
2101 fd_delete(t->srv_fd);
2102 if (t->srv)
2103 t->srv->cur_sess--;
2104
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002105 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002106 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2107 else
2108 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2109
2110 /* ensure that we have enough retries left */
2111 if (srv_count_retry_down(t, conn_err))
2112 return 1;
2113
Willy Tarreau830ff452006-12-17 19:31:23 +01002114 if (t->srv && t->conn_retries == 0 && t->be->beprm->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002115 /* We're on our last chance, and the REDISP option was specified.
2116 * We will ignore cookie and force to balance or use the dispatcher.
2117 */
2118 /* let's try to offer this slot to anybody */
Willy Tarreau830ff452006-12-17 19:31:23 +01002119 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002120 task_wakeup(&rq, t->srv->queue_mgt);
2121
2122 if (t->srv)
2123 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +01002124 t->be->beprm->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002125
2126 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2127 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002128 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002129
2130 /* first, get a connection */
2131 if (srv_redispatch_connect(t))
2132 return t->srv_state != SV_STIDLE;
2133 }
2134
Willy Tarreaubaaee002006-06-26 02:48:02 +02002135 do {
2136 /* Now we will try to either reconnect to the same server or
2137 * connect to another server. If the connection gets queued
2138 * because all servers are saturated, then we will go back to
2139 * the SV_STIDLE state.
2140 */
2141 if (srv_retryable_connect(t)) {
2142 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2143 return t->srv_state != SV_STCONN;
2144 }
2145
2146 /* we need to redispatch the connection to another server */
2147 if (srv_redispatch_connect(t))
2148 return t->srv_state != SV_STCONN;
2149 } while (1);
2150 }
2151 else { /* no error or write 0 */
2152 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
2153
2154 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2155 if (req->l == 0) /* nothing to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002156 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002157 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002158 } else /* need the right to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002159 MY_FD_SET(t->srv_fd, StaticWriteEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002160 if (t->be->beprm->srvtimeout) {
2161 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002162 /* FIXME: to prevent the server from expiring read timeouts during writes,
2163 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002164 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002165 }
2166 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002167 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002168 }
2169
Willy Tarreau830ff452006-12-17 19:31:23 +01002170 if (t->be->beprm->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau2a429502006-10-15 14:52:29 +02002171 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002172 if (t->be->beprm->srvtimeout)
2173 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002174 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002175 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002176
2177 t->srv_state = SV_STDATA;
2178 if (t->srv)
2179 t->srv->cum_sess++;
2180 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2181
2182 /* if the user wants to log as soon as possible, without counting
2183 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002184 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002185 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
2186 sess_log(t);
2187 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002188#ifdef CONFIG_HAP_TCPSPLICE
2189 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2190 /* TCP splicing supported by both FE and BE */
2191 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2192 }
2193#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002194 }
2195 else {
2196 t->srv_state = SV_STHEADERS;
2197 if (t->srv)
2198 t->srv->cum_sess++;
2199 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002200 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2201 /* reset hdr_idx which was already initialized by the request.
2202 * right now, the http parser does it.
2203 * hdr_idx_init(&t->txn.hdr_idx);
2204 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002205 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002206 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002207 return 1;
2208 }
2209 }
2210 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002211 /*
2212 * Now parse the partial (or complete) lines.
2213 * We will check the response syntax, and also join multi-line
2214 * headers. An index of all the lines will be elaborated while
2215 * parsing.
2216 *
2217 * For the parsing, we use a 28 states FSM.
2218 *
2219 * Here is the information we currently have :
2220 * rep->data + req->som = beginning of response
2221 * rep->data + req->eoh = end of processed headers / start of current one
2222 * rep->data + req->eol = end of current header or line (LF or CRLF)
2223 * rep->lr = first non-visited byte
2224 * rep->r = end of data
2225 */
2226
2227 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002228 struct http_msg *msg = &txn->rsp;
2229 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002230
Willy Tarreaua15645d2007-03-18 16:22:39 +01002231 if (likely(rep->lr < rep->r))
2232 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002233
Willy Tarreaua15645d2007-03-18 16:22:39 +01002234 /* 1: we might have to print this header in debug mode */
2235 if (unlikely((global.mode & MODE_DEBUG) &&
2236 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2237 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2238 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002239
Willy Tarreaua15645d2007-03-18 16:22:39 +01002240 sol = rep->data + msg->som;
2241 eol = sol + msg->sl.rq.l;
2242 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002243
Willy Tarreaua15645d2007-03-18 16:22:39 +01002244 sol += hdr_idx_first_pos(&txn->hdr_idx);
2245 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002246
Willy Tarreaua15645d2007-03-18 16:22:39 +01002247 while (cur_idx) {
2248 eol = sol + txn->hdr_idx.v[cur_idx].len;
2249 debug_hdr("srvhdr", t, sol, eol);
2250 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2251 cur_idx = txn->hdr_idx.v[cur_idx].next;
2252 }
2253 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002254
Willy Tarreaubaaee002006-06-26 02:48:02 +02002255
Willy Tarreaua15645d2007-03-18 16:22:39 +01002256 if ((rep->l < rep->rlim - rep->data) && ! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2257 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
2258 * full. We cannot loop here since stream_sock_read will disable it only if
2259 * rep->l == rlim-data
2260 */
2261 MY_FD_SET(t->srv_fd, StaticReadEvent);
2262 if (t->be->beprm->srvtimeout)
2263 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2264 else
2265 tv_eternity(&rep->rex);
2266 }
2267
2268
2269 /*
2270 * Now we quickly check if we have found a full valid response.
2271 * If not so, we check the FD and buffer states before leaving.
2272 * A full response is indicated by the fact that we have seen
2273 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2274 * responses are checked first.
2275 *
2276 * Depending on whether the client is still there or not, we
2277 * may send an error response back or not. Note that normally
2278 * we should only check for HTTP status there, and check I/O
2279 * errors somewhere else.
2280 */
2281
2282 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2283
2284 /* Invalid response, or read error or write error */
2285 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2286 (req->flags & BF_WRITE_ERROR) ||
2287 (rep->flags & BF_READ_ERROR))) {
2288 tv_eternity(&rep->rex);
2289 tv_eternity(&req->wex);
2290 fd_delete(t->srv_fd);
2291 if (t->srv) {
2292 t->srv->cur_sess--;
2293 t->srv->failed_resp++;
2294 }
2295 t->be->failed_resp++;
2296 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002297 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002298 client_return(t, error_message(t, HTTP_ERR_502));
2299 if (!(t->flags & SN_ERR_MASK))
2300 t->flags |= SN_ERR_SRVCL;
2301 if (!(t->flags & SN_FINST_MASK))
2302 t->flags |= SN_FINST_H;
2303 /* We used to have a free connection slot. Since we'll never use it,
2304 * we have to inform the server that it may be used by another session.
2305 */
2306 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2307 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002308
Willy Tarreaua15645d2007-03-18 16:22:39 +01002309 return 1;
2310 }
2311
2312 /* end of client write or end of server read.
2313 * since we are in header mode, if there's no space left for headers, we
2314 * won't be able to free more later, so the session will never terminate.
2315 */
2316 else if (unlikely(rep->flags & BF_READ_NULL ||
2317 c == CL_STSHUTW || c == CL_STCLOSE ||
2318 rep->l >= rep->rlim - rep->data)) {
2319 MY_FD_CLR(t->srv_fd, StaticReadEvent);
2320 tv_eternity(&rep->rex);
2321 shutdown(t->srv_fd, SHUT_RD);
2322 t->srv_state = SV_STSHUTR;
2323 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2324 return 1;
2325 }
2326
2327 /* read timeout : return a 504 to the client.
2328 */
2329 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticReadEvent) &&
2330 tv_cmp2_ms(&rep->rex, &now) <= 0)) {
2331 tv_eternity(&rep->rex);
2332 tv_eternity(&req->wex);
2333 fd_delete(t->srv_fd);
2334 if (t->srv) {
2335 t->srv->cur_sess--;
2336 t->srv->failed_resp++;
2337 }
2338 t->be->failed_resp++;
2339 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002340 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002341 client_return(t, error_message(t, HTTP_ERR_504));
2342 if (!(t->flags & SN_ERR_MASK))
2343 t->flags |= SN_ERR_SRVTO;
2344 if (!(t->flags & SN_FINST_MASK))
2345 t->flags |= SN_FINST_H;
2346 /* We used to have a free connection slot. Since we'll never use it,
2347 * we have to inform the server that it may be used by another session.
2348 */
2349 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2350 task_wakeup(&rq, t->srv->queue_mgt);
2351 return 1;
2352 }
2353
2354 /* last client read and buffer empty */
2355 /* FIXME!!! here, we don't want to switch to SHUTW if the
2356 * client shuts read too early, because we may still have
2357 * some work to do on the headers.
2358 * The side-effect is that if the client completely closes its
2359 * connection during SV_STHEADER, the connection to the server
2360 * is kept until a response comes back or the timeout is reached.
2361 */
2362 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2363 (req->l == 0))) {
2364 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2365 tv_eternity(&req->wex);
2366
2367 /* We must ensure that the read part is still
2368 * alive when switching to shutw */
2369 MY_FD_SET(t->srv_fd, StaticReadEvent);
2370 if (t->be->beprm->srvtimeout)
2371 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2372
2373 shutdown(t->srv_fd, SHUT_WR);
2374 t->srv_state = SV_STSHUTW;
2375 return 1;
2376 }
2377
2378 /* write timeout */
2379 /* FIXME!!! here, we don't want to switch to SHUTW if the
2380 * client shuts read too early, because we may still have
2381 * some work to do on the headers.
2382 */
2383 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticWriteEvent) &&
2384 tv_cmp2_ms(&req->wex, &now) <= 0)) {
2385 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2386 tv_eternity(&req->wex);
2387 shutdown(t->srv_fd, SHUT_WR);
2388 /* We must ensure that the read part is still alive
2389 * when switching to shutw */
2390 MY_FD_SET(t->srv_fd, StaticReadEvent);
2391 if (t->be->beprm->srvtimeout)
2392 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2393
2394 t->srv_state = SV_STSHUTW;
2395 if (!(t->flags & SN_ERR_MASK))
2396 t->flags |= SN_ERR_SRVTO;
2397 if (!(t->flags & SN_FINST_MASK))
2398 t->flags |= SN_FINST_H;
2399 return 1;
2400 }
2401
2402 /*
2403 * And now the non-error cases.
2404 */
2405
2406 /* Data remaining in the request buffer.
2407 * This happens during the first pass here, and during
2408 * long posts.
2409 */
2410 else if (likely(req->l)) {
2411 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2412 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2413 if (t->be->beprm->srvtimeout) {
2414 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
2415 /* FIXME: to prevent the server from expiring read timeouts during writes,
2416 * we refresh it. */
2417 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002418 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002419 else
2420 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002421 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002422 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002423
Willy Tarreaua15645d2007-03-18 16:22:39 +01002424 /* nothing left in the request buffer */
2425 else {
2426 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2427 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002428 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002429 }
2430 }
2431
2432 return t->srv_state != SV_STHEADERS;
2433 }
2434
2435
2436 /*****************************************************************
2437 * More interesting part now : we know that we have a complete *
2438 * response which at least looks like HTTP. We have an indicator *
2439 * of each header's length, so we can parse them quickly. *
2440 ****************************************************************/
2441
2442 /*
2443 * 1: get the status code and check for cacheability.
2444 */
2445
2446 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002447 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002448
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002449 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002450 case 200:
2451 case 203:
2452 case 206:
2453 case 300:
2454 case 301:
2455 case 410:
2456 /* RFC2616 @13.4:
2457 * "A response received with a status code of
2458 * 200, 203, 206, 300, 301 or 410 MAY be stored
2459 * by a cache (...) unless a cache-control
2460 * directive prohibits caching."
2461 *
2462 * RFC2616 @9.5: POST method :
2463 * "Responses to this method are not cacheable,
2464 * unless the response includes appropriate
2465 * Cache-Control or Expires header fields."
2466 */
2467 if (likely(txn->meth != HTTP_METH_POST) &&
2468 unlikely(t->be->beprm->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002469 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002470 break;
2471 default:
2472 break;
2473 }
2474
2475 /*
2476 * 2: we may need to capture headers
2477 */
2478 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->fiprm->rsp_cap))
2479 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2480 txn->rsp.cap, t->fe->fiprm->rsp_cap);
2481
2482 /*
2483 * 3: we will have to evaluate the filters.
2484 * As opposed to version 1.2, now they will be evaluated in the
2485 * filters order and not in the header order. This means that
2486 * each filter has to be validated among all headers.
2487 *
2488 * Filters are tried with ->be first, then with ->fe if it is
2489 * different from ->be.
2490 */
2491
2492 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2493
2494 cur_proxy = t->be;
2495 while (1) {
2496 struct proxy *rule_set = cur_proxy->fiprm;
2497
2498 /* try headers filters */
2499 if (rule_set->rsp_exp != NULL) {
2500 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2501 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002502 if (t->srv) {
2503 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002504 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002505 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002506 cur_proxy->beprm->failed_resp++;
2507 return_srv_prx_502:
2508 tv_eternity(&rep->rex);
2509 tv_eternity(&req->wex);
2510 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002511 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002512 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002513 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002514 if (!(t->flags & SN_ERR_MASK))
2515 t->flags |= SN_ERR_PRXCOND;
2516 if (!(t->flags & SN_FINST_MASK))
2517 t->flags |= SN_FINST_H;
2518 /* We used to have a free connection slot. Since we'll never use it,
2519 * we have to inform the server that it may be used by another session.
2520 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002521 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002522 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002523 return 1;
2524 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002525 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002526
Willy Tarreaua15645d2007-03-18 16:22:39 +01002527 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002528 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002529 if (t->srv) {
2530 t->srv->cur_sess--;
2531 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002532 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002533 cur_proxy->beprm->denied_resp++;
2534 goto return_srv_prx_502;
2535 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002536
Willy Tarreaua15645d2007-03-18 16:22:39 +01002537 /* We might have to check for "Connection:" */
2538 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
2539 !(t->flags & SN_CONN_CLOSED)) {
2540 char *cur_ptr, *cur_end, *cur_next;
2541 int cur_idx, old_idx, delta;
2542 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002543
Willy Tarreaua15645d2007-03-18 16:22:39 +01002544 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2545 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002546
Willy Tarreaua15645d2007-03-18 16:22:39 +01002547 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2548 cur_hdr = &txn->hdr_idx.v[cur_idx];
2549 cur_ptr = cur_next;
2550 cur_end = cur_ptr + cur_hdr->len;
2551 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002552
Willy Tarreaua15645d2007-03-18 16:22:39 +01002553 if (strncasecmp(cur_ptr, "Connection:", 11) == 0) {
2554 /* 3 possibilities :
2555 * - we have already set Connection: close,
2556 * so we remove this line.
2557 * - we have not yet set Connection: close,
2558 * but this line indicates close. We leave
2559 * it untouched and set the flag.
2560 * - we have not yet set Connection: close,
2561 * and this line indicates non-close. We
2562 * replace it.
2563 */
2564 if (t->flags & SN_CONN_CLOSED) {
2565 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2566 txn->rsp.eoh += delta;
2567 cur_next += delta;
2568 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2569 txn->hdr_idx.used--;
2570 cur_hdr->len = 0;
2571 } else {
2572 if (cur_ptr + 17 > cur_end ||
2573 !http_is_lws[(unsigned char)*(cur_ptr+17)] ||
2574 strncasecmp(cur_ptr+11, " close", 6)) {
2575 delta = buffer_replace2(rep, cur_ptr+11, cur_end,
2576 " close", 6);
2577 cur_next += delta;
2578 cur_hdr->len += delta;
2579 txn->rsp.eoh += delta;
2580 }
2581 t->flags |= SN_CONN_CLOSED;
2582 }
2583 }
2584 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002585 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002586 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002587
Willy Tarreaua15645d2007-03-18 16:22:39 +01002588 /* add response headers from the rule sets in the same order */
2589 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2590 int len;
2591
2592 len = sprintf(trash, "%s\r\n", rule_set->rsp_add[cur_idx]);
2593 len = buffer_replace2(rep, rep->data + txn->rsp.eoh,
2594 rep->data + txn->rsp.eoh, trash, len);
2595 txn->rsp.eoh += len;
2596
2597 if (unlikely(hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0))
2598 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002599 }
2600
Willy Tarreaua15645d2007-03-18 16:22:39 +01002601 /* check whether we're already working on the frontend */
2602 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002603 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002604 cur_proxy = t->fe;
2605 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002606
Willy Tarreaua15645d2007-03-18 16:22:39 +01002607 /*
2608 * 4: check for server cookie.
2609 */
2610 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002611
Willy Tarreaua15645d2007-03-18 16:22:39 +01002612 /*
2613 * 5: add server cookie in the response if needed
2614 */
2615 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_INS) &&
2616 (!(t->be->beprm->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2617 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002618
Willy Tarreaua15645d2007-03-18 16:22:39 +01002619 /* the server is known, it's not the one the client requested, we have to
2620 * insert a set-cookie here, except if we want to insert only on POST
2621 * requests and this one isn't. Note that servers which don't have cookies
2622 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002623 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002624 len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n",
2625 t->be->beprm->cookie_name,
2626 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002627
Willy Tarreaua15645d2007-03-18 16:22:39 +01002628 len = buffer_replace2(rep, rep->data + txn->rsp.eoh, rep->data + txn->rsp.eoh, trash, len);
2629 txn->rsp.eoh += len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002630
Willy Tarreaua15645d2007-03-18 16:22:39 +01002631 if (hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)
2632 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002633
Willy Tarreau3d300592007-03-18 18:34:41 +01002634 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002635
Willy Tarreaua15645d2007-03-18 16:22:39 +01002636 /* Here, we will tell an eventual cache on the client side that we don't
2637 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2638 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2639 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2640 */
2641 if (t->be->beprm->options & PR_O_COOK_NOC) {
2642 //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n");
2643 len = sprintf(trash, "Cache-control: private\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002644
Willy Tarreaua15645d2007-03-18 16:22:39 +01002645 len = buffer_replace2(rep, rep->data + txn->rsp.eoh,
2646 rep->data + txn->rsp.eoh, trash, len);
2647 txn->rsp.eoh += len;
2648 if (hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)
2649 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002650 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002651 }
2652
2653
2654 /*
2655 * 6: check for cache-control or pragma headers.
2656 */
2657 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002658
Willy Tarreaubaaee002006-06-26 02:48:02 +02002659
Willy Tarreaua15645d2007-03-18 16:22:39 +01002660 /*
2661 * 7: check if result will be cacheable with a cookie.
2662 * We'll block the response if security checks have caught
2663 * nasty things such as a cacheable cookie.
2664 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002665 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2666 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002667 (t->be->beprm->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002668
Willy Tarreaua15645d2007-03-18 16:22:39 +01002669 /* we're in presence of a cacheable response containing
2670 * a set-cookie header. We'll block it as requested by
2671 * the 'checkcache' option, and send an alert.
2672 */
2673 if (t->srv) {
2674 t->srv->cur_sess--;
2675 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002676 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002677 t->be->beprm->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002678
Willy Tarreaua15645d2007-03-18 16:22:39 +01002679 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2680 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2681 send_log(t->be, LOG_ALERT,
2682 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2683 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2684 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002685 }
2686
Willy Tarreaua15645d2007-03-18 16:22:39 +01002687 /*
2688 * 8: add "Connection: close" if needed and not yet set.
2689 */
2690 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
2691 !(t->flags & SN_CONN_CLOSED)) {
2692 int len;
2693 len = buffer_replace2(rep, rep->data + txn->rsp.eoh,
2694 rep->data + txn->rsp.eoh, "Connection: close\r\n", 19);
2695 txn->rsp.eoh += len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002696
Willy Tarreaua15645d2007-03-18 16:22:39 +01002697 if (hdr_idx_add(17, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)
2698 goto return_bad_resp;
2699 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002700 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002701
Willy Tarreaubaaee002006-06-26 02:48:02 +02002702
Willy Tarreaua15645d2007-03-18 16:22:39 +01002703 /*************************************************************
2704 * OK, that's finished for the headers. We have done what we *
2705 * could. Let's switch to the DATA state. *
2706 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002707
Willy Tarreaua15645d2007-03-18 16:22:39 +01002708 t->srv_state = SV_STDATA;
2709 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
2710 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
2711
2712 /* client connection already closed or option 'forceclose' required :
2713 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002714 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002715 if ((req->l == 0) &&
2716 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->beprm->options & PR_O_FORCE_CLO)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002717 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002718 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002719
2720 /* We must ensure that the read part is still alive when switching
2721 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002722 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002723 if (t->be->beprm->srvtimeout)
2724 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002725
Willy Tarreaua15645d2007-03-18 16:22:39 +01002726 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002727 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002728 }
2729
Willy Tarreaua15645d2007-03-18 16:22:39 +01002730#ifdef CONFIG_HAP_TCPSPLICE
2731 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2732 /* TCP splicing supported by both FE and BE */
2733 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002734 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002735#endif
2736 /* if the user wants to log as soon as possible, without counting
2737 bytes from the server, then this is the right moment. */
2738 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2739 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002740 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002741 sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002742 }
2743
Willy Tarreaua15645d2007-03-18 16:22:39 +01002744 /* Note: we must not try to cheat by jumping directly to DATA,
2745 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002746 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002747
2748 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002749 }
2750 else if (s == SV_STDATA) {
2751 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002752 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002753 tv_eternity(&rep->rex);
2754 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002755 fd_delete(t->srv_fd);
2756 if (t->srv) {
2757 t->srv->cur_sess--;
2758 t->srv->failed_resp++;
2759 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002760 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002761 t->srv_state = SV_STCLOSE;
2762 if (!(t->flags & SN_ERR_MASK))
2763 t->flags |= SN_ERR_SRVCL;
2764 if (!(t->flags & SN_FINST_MASK))
2765 t->flags |= SN_FINST_D;
2766 /* We used to have a free connection slot. Since we'll never use it,
2767 * we have to inform the server that it may be used by another session.
2768 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002769 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002770 task_wakeup(&rq, t->srv->queue_mgt);
2771
2772 return 1;
2773 }
2774 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002775 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002776 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002777 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002778 shutdown(t->srv_fd, SHUT_RD);
2779 t->srv_state = SV_STSHUTR;
2780 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2781 return 1;
2782 }
2783 /* end of client read and no more data to send */
2784 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002785 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002786 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002787 shutdown(t->srv_fd, SHUT_WR);
2788 /* We must ensure that the read part is still alive when switching
2789 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002790 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002791 if (t->be->beprm->srvtimeout)
2792 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002793
2794 t->srv_state = SV_STSHUTW;
2795 return 1;
2796 }
2797 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002798 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002799 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002800 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002801 shutdown(t->srv_fd, SHUT_RD);
2802 t->srv_state = SV_STSHUTR;
2803 if (!(t->flags & SN_ERR_MASK))
2804 t->flags |= SN_ERR_SRVTO;
2805 if (!(t->flags & SN_FINST_MASK))
2806 t->flags |= SN_FINST_D;
2807 return 1;
2808 }
2809 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002810 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002811 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002812 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002813 shutdown(t->srv_fd, SHUT_WR);
2814 /* We must ensure that the read part is still alive when switching
2815 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002816 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002817 if (t->be->beprm->srvtimeout)
2818 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002819 t->srv_state = SV_STSHUTW;
2820 if (!(t->flags & SN_ERR_MASK))
2821 t->flags |= SN_ERR_SRVTO;
2822 if (!(t->flags & SN_FINST_MASK))
2823 t->flags |= SN_FINST_D;
2824 return 1;
2825 }
2826
2827 /* recompute request time-outs */
2828 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002829 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2830 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002831 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002832 }
2833 }
2834 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau2a429502006-10-15 14:52:29 +02002835 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2836 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002837 if (t->be->beprm->srvtimeout) {
2838 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002839 /* FIXME: to prevent the server from expiring read timeouts during writes,
2840 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002841 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002842 }
2843 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002844 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002845 }
2846 }
2847
2848 /* recompute response time-outs */
2849 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002850 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2851 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002852 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002853 }
2854 }
2855 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02002856 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2857 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002858 if (t->be->beprm->srvtimeout)
2859 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002860 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002861 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002862 }
2863 }
2864
2865 return 0; /* other cases change nothing */
2866 }
2867 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002868 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002869 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002870 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002871 fd_delete(t->srv_fd);
2872 if (t->srv) {
2873 t->srv->cur_sess--;
2874 t->srv->failed_resp++;
2875 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002876 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002877 //close(t->srv_fd);
2878 t->srv_state = SV_STCLOSE;
2879 if (!(t->flags & SN_ERR_MASK))
2880 t->flags |= SN_ERR_SRVCL;
2881 if (!(t->flags & SN_FINST_MASK))
2882 t->flags |= SN_FINST_D;
2883 /* We used to have a free connection slot. Since we'll never use it,
2884 * we have to inform the server that it may be used by another session.
2885 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002886 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002887 task_wakeup(&rq, t->srv->queue_mgt);
2888
2889 return 1;
2890 }
2891 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002892 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002893 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002894 fd_delete(t->srv_fd);
2895 if (t->srv)
2896 t->srv->cur_sess--;
2897 //close(t->srv_fd);
2898 t->srv_state = SV_STCLOSE;
2899 /* We used to have a free connection slot. Since we'll never use it,
2900 * we have to inform the server that it may be used by another session.
2901 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002902 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002903 task_wakeup(&rq, t->srv->queue_mgt);
2904
2905 return 1;
2906 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002907 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002908 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002909 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002910 fd_delete(t->srv_fd);
2911 if (t->srv)
2912 t->srv->cur_sess--;
2913 //close(t->srv_fd);
2914 t->srv_state = SV_STCLOSE;
2915 if (!(t->flags & SN_ERR_MASK))
2916 t->flags |= SN_ERR_SRVTO;
2917 if (!(t->flags & SN_FINST_MASK))
2918 t->flags |= SN_FINST_D;
2919 /* We used to have a free connection slot. Since we'll never use it,
2920 * we have to inform the server that it may be used by another session.
2921 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002922 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002923 task_wakeup(&rq, t->srv->queue_mgt);
2924
2925 return 1;
2926 }
2927 else if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002928 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2929 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002930 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002931 }
2932 }
2933 else { /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002934 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2935 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002936 if (t->be->beprm->srvtimeout) {
2937 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002938 /* FIXME: to prevent the server from expiring read timeouts during writes,
2939 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002940 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002941 }
2942 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002943 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002944 }
2945 }
2946 return 0;
2947 }
2948 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002949 if (rep->flags & BF_READ_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002950 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002951 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002952 fd_delete(t->srv_fd);
2953 if (t->srv) {
2954 t->srv->cur_sess--;
2955 t->srv->failed_resp++;
2956 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002957 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002958 //close(t->srv_fd);
2959 t->srv_state = SV_STCLOSE;
2960 if (!(t->flags & SN_ERR_MASK))
2961 t->flags |= SN_ERR_SRVCL;
2962 if (!(t->flags & SN_FINST_MASK))
2963 t->flags |= SN_FINST_D;
2964 /* We used to have a free connection slot. Since we'll never use it,
2965 * we have to inform the server that it may be used by another session.
2966 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002967 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002968 task_wakeup(&rq, t->srv->queue_mgt);
2969
2970 return 1;
2971 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002972 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002973 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002974 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002975 fd_delete(t->srv_fd);
2976 if (t->srv)
2977 t->srv->cur_sess--;
2978 //close(t->srv_fd);
2979 t->srv_state = SV_STCLOSE;
2980 /* We used to have a free connection slot. Since we'll never use it,
2981 * we have to inform the server that it may be used by another session.
2982 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002983 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002984 task_wakeup(&rq, t->srv->queue_mgt);
2985
2986 return 1;
2987 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002988 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002989 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002990 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002991 fd_delete(t->srv_fd);
2992 if (t->srv)
2993 t->srv->cur_sess--;
2994 //close(t->srv_fd);
2995 t->srv_state = SV_STCLOSE;
2996 if (!(t->flags & SN_ERR_MASK))
2997 t->flags |= SN_ERR_SRVTO;
2998 if (!(t->flags & SN_FINST_MASK))
2999 t->flags |= SN_FINST_D;
3000 /* We used to have a free connection slot. Since we'll never use it,
3001 * we have to inform the server that it may be used by another session.
3002 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003003 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003004 task_wakeup(&rq, t->srv->queue_mgt);
3005
3006 return 1;
3007 }
3008 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02003009 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3010 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003011 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003012 }
3013 }
3014 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02003015 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3016 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01003017 if (t->be->beprm->srvtimeout)
3018 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003019 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003020 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003021 }
3022 }
3023 return 0;
3024 }
3025 else { /* SV_STCLOSE : nothing to do */
3026 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3027 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003028 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
3029 t->uniq_id, t->be->beprm->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003030 write(1, trash, len);
3031 }
3032 return 0;
3033 }
3034 return 0;
3035}
3036
3037
3038/*
3039 * Produces data for the session <s> depending on its source. Expects to be
3040 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3041 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3042 * session, which it uses to keep on being called when there is free space in
3043 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3044 * if it changes the session state from CL_STSHUTR, otherwise 0.
3045 */
3046int produce_content(struct session *s)
3047{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003048 if (s->data_source == DATA_SRC_NONE) {
3049 s->flags &= ~SN_SELF_GEN;
3050 return 1;
3051 }
3052 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003053 /* dump server statistics */
3054 return produce_content_stats(s);
3055 }
3056 else {
3057 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003058 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003059 client_retnclose(s, error_message(s, HTTP_ERR_500));
3060 if (!(s->flags & SN_ERR_MASK))
3061 s->flags |= SN_ERR_PRXCOND;
3062 if (!(s->flags & SN_FINST_MASK))
3063 s->flags |= SN_FINST_R;
3064 s->flags &= ~SN_SELF_GEN;
3065 return 1;
3066 }
3067}
3068
3069
3070/*
3071 * Produces statistics data for the session <s>. Expects to be called with
3072 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3073 * flag from the session, which it uses to keep on being called when there is
3074 * free space in the buffer, of simply by letting an empty buffer upon return.
3075 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3076 */
3077int produce_content_stats(struct session *s)
3078{
3079 struct buffer *rep = s->rep;
3080 struct proxy *px;
3081 struct chunk msg;
3082 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003083
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003084 msg.len = 0;
3085 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003086
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003087 switch (s->data_state) {
3088 case DATA_ST_INIT:
3089 /* the function had not been called yet */
3090 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003091
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003092 chunk_printf(&msg, sizeof(trash),
3093 "HTTP/1.0 200 OK\r\n"
3094 "Cache-Control: no-cache\r\n"
3095 "Connection: close\r\n"
3096 "Content-Type: text/html\r\n"
3097 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003098
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003099 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003100 client_retnclose(s, &msg); // send the start of the response.
3101 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003102
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003103 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3104 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3105 if (!(s->flags & SN_FINST_MASK))
3106 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003107
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003108 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003109 /* that's all we return in case of HEAD request */
3110 s->data_state = DATA_ST_FIN;
3111 s->flags &= ~SN_SELF_GEN;
3112 return 1;
3113 }
3114
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003115 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3116 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003117
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003118 case DATA_ST_HEAD:
3119 /* WARNING! This must fit in the first buffer !!! */
3120 chunk_printf(&msg, sizeof(trash),
3121 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3122 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3123 "<style type=\"text/css\"><!--\n"
3124 "body {"
3125 " font-family: helvetica, arial;"
3126 " font-size: 12px;"
3127 " font-weight: normal;"
3128 " color: black;"
3129 " background: white;"
3130 "}\n"
3131 "th,td {"
3132 " font-size: 0.8em;"
3133 " align: center;"
3134 "}"
3135 "h1 {"
3136 " font-size: xx-large;"
3137 " margin-bottom: 0.5em;"
3138 "}\n"
3139 "h2 {"
3140 " font-family: helvetica, arial;"
3141 " font-size: x-large;"
3142 " font-weight: bold;"
3143 " font-style: italic;"
3144 " color: #6020a0;"
3145 " margin-top: 0em;"
3146 " margin-bottom: 0em;"
3147 "}\n"
3148 "h3 {"
3149 " font-family: helvetica, arial;"
3150 " font-size: 16px;"
3151 " font-weight: bold;"
3152 " color: #b00040;"
3153 " background: #e8e8d0;"
3154 " margin-top: 0em;"
3155 " margin-bottom: 0em;"
3156 "}\n"
3157 "li {"
3158 " margin-top: 0.25em;"
3159 " margin-right: 2em;"
3160 "}\n"
3161 ".hr {margin-top: 0.25em;"
3162 " border-color: black;"
3163 " border-bottom-style: solid;"
3164 "}\n"
3165 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3166 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3167 ".total {background: #20D0D0;color: #ffff80;}\n"
3168 ".frontend {background: #e8e8d0;}\n"
3169 ".backend {background: #e8e8d0;}\n"
3170 ".active0 {background: #ff9090;}\n"
3171 ".active1 {background: #ffd020;}\n"
3172 ".active2 {background: #ffffa0;}\n"
3173 ".active3 {background: #c0ffc0;}\n"
3174 ".active4 {background: #e0e0e0;}\n"
3175 ".backup0 {background: #ff9090;}\n"
3176 ".backup1 {background: #ff80ff;}\n"
3177 ".backup2 {background: #c060ff;}\n"
3178 ".backup3 {background: #b0d0ff;}\n"
3179 ".backup4 {background: #e0e0e0;}\n"
3180 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003181 "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 +01003182 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3183 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3184 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3185 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3186 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3187 "-->"
3188 "</style></head>");
3189
3190 if (buffer_write_chunk(rep, &msg) != 0)
3191 return 0;
3192
3193 s->data_state = DATA_ST_INFO;
3194 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003195
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003196 case DATA_ST_INFO:
3197 up = (now.tv_sec - start_date.tv_sec);
3198
3199 /* WARNING! this has to fit the first packet too.
3200 * We are around 3.5 kB, add adding entries will
3201 * become tricky if we want to support 4kB buffers !
3202 */
3203 chunk_printf(&msg, sizeof(trash),
3204 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3205 PRODUCT_NAME "</a></h1>\n"
3206 "<h2>Statistics Report for pid %d</h2>\n"
3207 "<hr width=\"100%%\" class=\"hr\">\n"
3208 "<h3>&gt; General process information</h3>\n"
3209 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3210 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3211 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3212 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3213 "<b>maxsock = </b> %d<br>\n"
3214 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3215 "</td><td align=\"center\" nowrap>\n"
3216 "<table class=\"lgd\"><tr>"
3217 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3218 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3219 "</tr><tr>"
3220 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3221 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3222 "</tr><tr>"
3223 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3224 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3225 "</tr><tr>"
3226 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3227 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3228 "</tr></table>\n"
3229 "</td>"
3230 "<td align=\"left\" nowrap width=\"1%%\">"
3231 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">"
3232 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>"
3233 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>"
3234 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>"
3235 "</ul>"
3236 "</td>"
3237 "</tr></table>\n"
3238 "",
3239 pid, pid, global.nbproc,
3240 up / 86400, (up % 86400) / 3600,
3241 (up % 3600) / 60, (up % 60),
3242 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3243 global.rlimit_memmax ? " MB" : "",
3244 global.rlimit_nofile,
3245 global.maxsock,
3246 global.maxconn,
3247 actconn
3248 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003249
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003250 if (buffer_write_chunk(rep, &msg) != 0)
3251 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003252
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003253 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003254
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003255 s->data_ctx.stats.px = proxy;
3256 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3257 s->data_state = DATA_ST_LIST;
3258 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003259
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003260 case DATA_ST_LIST:
3261 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003262 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003263 px = s->data_ctx.stats.px;
3264 /* skip the disabled proxies and non-networked ones */
3265 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3266 if (produce_content_stats_proxy(s, px) == 0)
3267 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003268
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003269 s->data_ctx.stats.px = px->next;
3270 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3271 }
3272 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003273
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003274 s->data_state = DATA_ST_END;
3275 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003276
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003277 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003278 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003279 if (buffer_write_chunk(rep, &msg) != 0)
3280 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003281
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003282 s->data_state = DATA_ST_FIN;
3283 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003284
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003285 case DATA_ST_FIN:
3286 s->flags &= ~SN_SELF_GEN;
3287 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003288
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003289 default:
3290 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003291 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003292 client_retnclose(s, error_message(s, HTTP_ERR_500));
3293 if (!(s->flags & SN_ERR_MASK))
3294 s->flags |= SN_ERR_PRXCOND;
3295 if (!(s->flags & SN_FINST_MASK))
3296 s->flags |= SN_FINST_R;
3297 s->flags &= ~SN_SELF_GEN;
3298 return 1;
3299 }
3300}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003301
Willy Tarreaubaaee002006-06-26 02:48:02 +02003302
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003303/*
3304 * Dumps statistics for a proxy.
3305 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3306 * ot non-zero if everything completed.
3307 */
3308int produce_content_stats_proxy(struct session *s, struct proxy *px)
3309{
3310 struct buffer *rep = s->rep;
3311 struct server *sv;
3312 struct chunk msg;
3313
3314 msg.len = 0;
3315 msg.str = trash;
3316
3317 switch (s->data_ctx.stats.px_st) {
3318 case DATA_ST_PX_INIT:
3319 /* we are on a new proxy */
3320
3321 if (s->be->fiprm->uri_auth && s->be->fiprm->uri_auth->scope) {
3322 /* we have a limited scope, we have to check the proxy name */
3323 struct stat_scope *scope;
3324 int len;
3325
3326 len = strlen(px->id);
3327 scope = s->be->fiprm->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003328
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003329 while (scope) {
3330 /* match exact proxy name */
3331 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3332 break;
3333
3334 /* match '.' which means 'self' proxy */
3335 if (!strcmp(scope->px_id, ".") && px == s->fe)
3336 break;
3337 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003338 }
3339
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003340 /* proxy name not found : don't dump anything */
3341 if (scope == NULL)
3342 return 1;
3343 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003344
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003345 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3346 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003347
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003348 case DATA_ST_PX_TH:
3349 /* print a new table */
3350 chunk_printf(&msg, sizeof(trash),
3351 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3352 "<tr align=\"center\" class=\"titre\">"
3353 "<th colspan=2 class=\"pxname\">%s</th>"
3354 "<th colspan=18 class=\"empty\"></th>"
3355 "</tr>\n"
3356 "<tr align=\"center\" class=\"titre\">"
3357 "<th rowspan=2></th>"
3358 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3359 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3360 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3361 "</tr>\n"
3362 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003363 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3364 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003365 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3366 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3367 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3368 "",
3369 px->id);
3370
3371 if (buffer_write_chunk(rep, &msg) != 0)
3372 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003373
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003374 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3375 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003376
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003377 case DATA_ST_PX_FE:
3378 /* print the frontend */
3379 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003380 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003381 /* name, queue */
3382 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3383 /* sessions : current, max, limit, cumul. */
3384 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3385 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003386 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003387 /* denied: req, resp */
3388 "<td align=right>%d</td><td align=right>%d</td>"
3389 /* errors : request, connect, response */
3390 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3391 /* server status : reflect backend status */
3392 "<td align=center>%s</td>"
3393 /* rest of server: nothing */
3394 "<td align=center colspan=5></td></tr>"
3395 "",
3396 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003397 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003398 px->denied_req, px->denied_resp,
3399 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003400 px->state == PR_STRUN ? "OPEN" :
3401 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003402
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003403 if (buffer_write_chunk(rep, &msg) != 0)
3404 return 0;
3405 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003406
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003407 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3408 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3409 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003410
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003411 case DATA_ST_PX_SV:
3412 /* stats.sv has been initialized above */
3413 while (s->data_ctx.stats.sv != NULL) {
3414 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3415 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003416
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003417 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003418
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003419 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3420 if (!(sv->state & SRV_CHECKED))
3421 sv_state = 4;
3422 else if (sv->state & SRV_RUNNING)
3423 if (sv->health == sv->rise + sv->fall - 1)
3424 sv_state = 3; /* UP */
3425 else
3426 sv_state = 2; /* going down */
3427 else
3428 if (sv->health)
3429 sv_state = 1; /* going up */
3430 else
3431 sv_state = 0; /* DOWN */
3432
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003433 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003434 /* name */
3435 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3436 /* queue : current, max */
3437 "<td align=right>%d</td><td align=right>%d</td>"
3438 /* sessions : current, max, limit, cumul */
3439 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3440 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003441 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003442 /* denied: req, resp */
3443 "<td align=right></td><td align=right>%d</td>"
3444 /* errors : request, connect, response */
3445 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3446 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003447 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003448 sv_state, sv->id,
3449 sv->nbpend, sv->nbpend_max,
3450 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003451 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003452 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003453 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003454
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003455 /* status */
3456 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3457 chunk_printf(&msg, sizeof(trash),
3458 srv_hlt_st[sv_state],
3459 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3460 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3461
Willy Tarreau128e9542007-01-01 22:01:43 +01003462 chunk_printf(&msg, sizeof(trash),
3463 /* weight */
3464 "</td><td>%d</td>"
3465 /* act, bck */
3466 "<td>%s</td><td>%s</td>"
3467 "",
3468 sv->uweight+1,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003469 (sv->state & SRV_BACKUP) ? "-" : "Y",
3470 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003471
3472 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003473 if (sv->state & SRV_CHECKED)
3474 chunk_printf(&msg, sizeof(trash),
3475 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3476 sv->failed_checks, sv->down_trans);
3477 else
3478 chunk_printf(&msg, sizeof(trash),
3479 "<td colspan=2></td></tr>\n");
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;
3483
3484 s->data_ctx.stats.sv = sv->next;
3485 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003486
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003487 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3488 /* fall through */
3489
3490 case DATA_ST_PX_BE:
3491 /* print the backend */
3492 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003493 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003494 /* name */
3495 "<tr align=center class=\"backend\"><td>Backend</td>"
3496 /* queue : current, max */
3497 "<td align=right>%d</td><td align=right>%d</td>"
3498 /* sessions : current, max, limit, cumul. */
3499 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3500 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003501 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003502 /* denied: req, resp */
3503 "<td align=right>%d</td><td align=right>%d</td>"
3504 /* errors : request, connect, response */
3505 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3506 /* server status : reflect backend status (up/down) : we display UP
3507 * if the backend has known working servers or if it has no server at
3508 * all (eg: for stats). Tthen we display the total weight, number of
3509 * active and backups. */
3510 "<td align=center>%s</td><td align=center>%d</td>"
3511 "<td align=center>%d</td><td align=center>%d</td>"
3512 /* rest of server: nothing */
3513 "<td align=center colspan=2></td></tr>"
3514 "",
3515 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3516 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003517 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003518 px->denied_req, px->denied_resp,
3519 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003520 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3521 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003522
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003523 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003524 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003525 }
3526
3527 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3528 /* fall through */
3529
3530 case DATA_ST_PX_END:
3531 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3532
3533 if (buffer_write_chunk(rep, &msg) != 0)
3534 return 0;
3535
3536 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3537 /* fall through */
3538
3539 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003540 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003541
3542 default:
3543 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003544 return 1;
3545 }
3546}
3547
3548
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003549/* Iterate the same filter through all request headers.
3550 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003551 * Since it can manage the switch to another backend, it updates the per-proxy
3552 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003553 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003554int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003555{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003556 char term;
3557 char *cur_ptr, *cur_end, *cur_next;
3558 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003559 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003560 struct hdr_idx_elem *cur_hdr;
3561 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003563 last_hdr = 0;
3564
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003565 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003566 old_idx = 0;
3567
3568 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003569 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003570 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003571 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003572 (exp->action == ACT_ALLOW ||
3573 exp->action == ACT_DENY ||
3574 exp->action == ACT_TARPIT))
3575 return 0;
3576
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003577 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003578 if (!cur_idx)
3579 break;
3580
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003581 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003582 cur_ptr = cur_next;
3583 cur_end = cur_ptr + cur_hdr->len;
3584 cur_next = cur_end + cur_hdr->cr + 1;
3585
3586 /* Now we have one header between cur_ptr and cur_end,
3587 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003588 */
3589
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003590 /* The annoying part is that pattern matching needs
3591 * that we modify the contents to null-terminate all
3592 * strings before testing them.
3593 */
3594
3595 term = *cur_end;
3596 *cur_end = '\0';
3597
3598 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3599 switch (exp->action) {
3600 case ACT_SETBE:
3601 /* It is not possible to jump a second time.
3602 * FIXME: should we return an HTTP/500 here so that
3603 * the admin knows there's a problem ?
3604 */
3605 if (t->be != t->fe)
3606 break;
3607
3608 /* Swithing Proxy */
3609 t->be = (struct proxy *) exp->replace;
3610
3611 /* right now, the backend switch is not too much complicated
3612 * because we have associated req_cap and rsp_cap to the
3613 * frontend, and the beconn will be updated later.
3614 */
3615
3616 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3617 t->req->cto = t->be->beprm->contimeout;
3618 last_hdr = 1;
3619 break;
3620
3621 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003622 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003623 last_hdr = 1;
3624 break;
3625
3626 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003627 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003628 last_hdr = 1;
3629 t->be->beprm->denied_req++;
3630 break;
3631
3632 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003633 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003634 last_hdr = 1;
3635 t->be->beprm->denied_req++;
3636 break;
3637
3638 case ACT_REPLACE:
3639 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3640 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3641 /* FIXME: if the user adds a newline in the replacement, the
3642 * index will not be recalculated for now, and the new line
3643 * will not be counted as a new header.
3644 */
3645
3646 cur_end += delta;
3647 cur_next += delta;
3648 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003649 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003650 break;
3651
3652 case ACT_REMOVE:
3653 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3654 cur_next += delta;
3655
3656 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003657 txn->req.eoh += delta;
3658 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3659 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003660 cur_hdr->len = 0;
3661 cur_end = NULL; /* null-term has been rewritten */
3662 break;
3663
3664 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003665 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003666 if (cur_end)
3667 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003669 /* keep the link from this header to next one in case of later
3670 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003671 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003672 old_idx = cur_idx;
3673 }
3674 return 0;
3675}
3676
3677
3678/* Apply the filter to the request line.
3679 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3680 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003681 * Since it can manage the switch to another backend, it updates the per-proxy
3682 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003683 */
3684int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3685{
3686 char term;
3687 char *cur_ptr, *cur_end;
3688 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003689 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003690 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003691
Willy Tarreau58f10d72006-12-04 02:26:12 +01003692
Willy Tarreau3d300592007-03-18 18:34:41 +01003693 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003694 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003695 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003696 (exp->action == ACT_ALLOW ||
3697 exp->action == ACT_DENY ||
3698 exp->action == ACT_TARPIT))
3699 return 0;
3700 else if (exp->action == ACT_REMOVE)
3701 return 0;
3702
3703 done = 0;
3704
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003705 cur_ptr = req->data + txn->req.som;
3706 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003707
3708 /* Now we have the request line between cur_ptr and cur_end */
3709
3710 /* The annoying part is that pattern matching needs
3711 * that we modify the contents to null-terminate all
3712 * strings before testing them.
3713 */
3714
3715 term = *cur_end;
3716 *cur_end = '\0';
3717
3718 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3719 switch (exp->action) {
3720 case ACT_SETBE:
3721 /* It is not possible to jump a second time.
3722 * FIXME: should we return an HTTP/500 here so that
3723 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003724 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003725 if (t->be != t->fe)
3726 break;
3727
3728 /* Swithing Proxy */
3729 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003731 /* right now, the backend switch is not too much complicated
3732 * because we have associated req_cap and rsp_cap to the
3733 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003734 */
3735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003736 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3737 t->req->cto = t->be->beprm->contimeout;
3738 done = 1;
3739 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003740
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003741 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003742 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003743 done = 1;
3744 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003745
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003746 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003747 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003748 t->be->beprm->denied_req++;
3749 done = 1;
3750 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003751
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003752 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003753 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003754 t->be->beprm->denied_req++;
3755 done = 1;
3756 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003758 case ACT_REPLACE:
3759 *cur_end = term; /* restore the string terminator */
3760 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3761 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3762 /* FIXME: if the user adds a newline in the replacement, the
3763 * index will not be recalculated for now, and the new line
3764 * will not be counted as a new header.
3765 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003766
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003767 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003768 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003769
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003770 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003771 HTTP_MSG_RQMETH,
3772 cur_ptr, cur_end + 1,
3773 NULL, NULL);
3774 if (unlikely(!cur_end))
3775 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003777 /* we have a full request and we know that we have either a CR
3778 * or an LF at <ptr>.
3779 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003780 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3781 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003782 /* there is no point trying this regex on headers */
3783 return 1;
3784 }
3785 }
3786 *cur_end = term; /* restore the string terminator */
3787 return done;
3788}
Willy Tarreau97de6242006-12-27 17:18:38 +01003789
Willy Tarreau58f10d72006-12-04 02:26:12 +01003790
Willy Tarreau58f10d72006-12-04 02:26:12 +01003791
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003792/*
3793 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3794 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003795 * unparsable request. Since it can manage the switch to another backend, it
3796 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003797 */
3798int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3799{
Willy Tarreau3d300592007-03-18 18:34:41 +01003800 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003801 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003802 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003803 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003805 /*
3806 * The interleaving of transformations and verdicts
3807 * makes it difficult to decide to continue or stop
3808 * the evaluation.
3809 */
3810
Willy Tarreau3d300592007-03-18 18:34:41 +01003811 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003812 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3813 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3814 exp = exp->next;
3815 continue;
3816 }
3817
3818 /* Apply the filter to the request line. */
3819 ret = apply_filter_to_req_line(t, req, exp);
3820 if (unlikely(ret < 0))
3821 return -1;
3822
3823 if (likely(ret == 0)) {
3824 /* The filter did not match the request, it can be
3825 * iterated through all headers.
3826 */
3827 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003828 }
3829 exp = exp->next;
3830 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003831 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003832}
3833
3834
Willy Tarreaua15645d2007-03-18 16:22:39 +01003835
Willy Tarreau58f10d72006-12-04 02:26:12 +01003836/*
3837 * Manager client-side cookie
3838 */
3839void manage_client_side_cookies(struct session *t, struct buffer *req)
3840{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003841 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003842 char *p1, *p2, *p3, *p4;
3843 char *del_colon, *del_cookie, *colon;
3844 int app_cookies;
3845
3846 appsess *asession_temp = NULL;
3847 appsess local_asession;
3848
3849 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003850 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003851
Willy Tarreau830ff452006-12-17 19:31:23 +01003852 if (t->be->beprm->cookie_name == NULL &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003853 t->be->beprm->appsession_name == NULL &&
3854 t->be->fiprm->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003855 return;
3856
Willy Tarreau2a324282006-12-05 00:05:46 +01003857 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003858 * we start with the start line.
3859 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003860 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003861 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003862
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003863 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003864 struct hdr_idx_elem *cur_hdr;
3865
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003866 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003867 cur_ptr = cur_next;
3868 cur_end = cur_ptr + cur_hdr->len;
3869 cur_next = cur_end + cur_hdr->cr + 1;
3870
3871 /* We have one full header between cur_ptr and cur_end, and the
3872 * next header starts at cur_next. We're only interested in
3873 * "Cookie:" headers.
3874 */
3875
3876 if ((cur_end - cur_ptr <= 7) ||
3877 (strncasecmp(cur_ptr, "Cookie:", 7) != 0)) {
3878 old_idx = cur_idx;
3879 continue;
3880 }
3881
3882 /* Now look for cookies. Conforming to RFC2109, we have to support
3883 * attributes whose name begin with a '$', and associate them with
3884 * the right cookie, if we want to delete this cookie.
3885 * So there are 3 cases for each cookie read :
3886 * 1) it's a special attribute, beginning with a '$' : ignore it.
3887 * 2) it's a server id cookie that we *MAY* want to delete : save
3888 * some pointers on it (last semi-colon, beginning of cookie...)
3889 * 3) it's an application cookie : we *MAY* have to delete a previous
3890 * "special" cookie.
3891 * At the end of loop, if a "special" cookie remains, we may have to
3892 * remove it. If no application cookie persists in the header, we
3893 * *MUST* delete it
3894 */
3895
3896
3897 p1 = cur_ptr + 7; /* first char after 'Cookie:' */
3898 if (isspace((int)*p1)) /* try to get the first space with it */
3899 p1++;
3900
3901 colon = p1;
3902 /* del_cookie == NULL => nothing to be deleted */
3903 del_colon = del_cookie = NULL;
3904 app_cookies = 0;
3905
3906 while (p1 < cur_end) {
3907 /* skip spaces and colons, but keep an eye on these ones */
3908 while (p1 < cur_end) {
3909 if (*p1 == ';' || *p1 == ',')
3910 colon = p1;
3911 else if (!isspace((int)*p1))
3912 break;
3913 p1++;
3914 }
3915
3916 if (p1 == cur_end)
3917 break;
3918
3919 /* p1 is at the beginning of the cookie name */
3920 p2 = p1;
3921 while (p2 < cur_end && *p2 != '=')
3922 p2++;
3923
3924 if (p2 == cur_end)
3925 break;
3926
3927 p3 = p2 + 1; /* skips the '=' sign */
3928 if (p3 == cur_end)
3929 break;
3930
3931 p4 = p3;
3932 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
3933 p4++;
3934
3935 /* here, we have the cookie name between p1 and p2,
3936 * and its value between p3 and p4.
3937 * we can process it :
3938 *
3939 * Cookie: NAME=VALUE;
3940 * | || || |
3941 * | || || +--> p4
3942 * | || |+-------> p3
3943 * | || +--------> p2
3944 * | |+------------> p1
3945 * | +-------------> colon
3946 * +--------------------> cur_ptr
3947 */
3948
3949 if (*p1 == '$') {
3950 /* skip this one */
3951 }
3952 else {
3953 /* first, let's see if we want to capture it */
Willy Tarreau830ff452006-12-17 19:31:23 +01003954 if (t->fe->fiprm->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003955 txn->cli_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01003956 (p4 - p1 >= t->fe->fiprm->capture_namelen) &&
3957 memcmp(p1, t->fe->fiprm->capture_name, t->fe->fiprm->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003958 int log_len = p4 - p1;
3959
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003960 if ((txn->cli_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003961 Alert("HTTP logging : out of memory.\n");
3962 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01003963 if (log_len > t->fe->fiprm->capture_len)
3964 log_len = t->fe->fiprm->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003965 memcpy(txn->cli_cookie, p1, log_len);
3966 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003967 }
3968 }
3969
Willy Tarreau830ff452006-12-17 19:31:23 +01003970 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
3971 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003972 /* Cool... it's the right one */
Willy Tarreau830ff452006-12-17 19:31:23 +01003973 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003974 char *delim;
3975
3976 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3977 * have the server ID betweek p3 and delim, and the original cookie between
3978 * delim+1 and p4. Otherwise, delim==p4 :
3979 *
3980 * Cookie: NAME=SRV~VALUE;
3981 * | || || | |
3982 * | || || | +--> p4
3983 * | || || +--------> delim
3984 * | || |+-----------> p3
3985 * | || +------------> p2
3986 * | |+----------------> p1
3987 * | +-----------------> colon
3988 * +------------------------> cur_ptr
3989 */
3990
Willy Tarreau830ff452006-12-17 19:31:23 +01003991 if (t->be->beprm->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003992 for (delim = p3; delim < p4; delim++)
3993 if (*delim == COOKIE_DELIM)
3994 break;
3995 }
3996 else
3997 delim = p4;
3998
3999
4000 /* Here, we'll look for the first running server which supports the cookie.
4001 * This allows to share a same cookie between several servers, for example
4002 * to dedicate backup servers to specific servers only.
4003 * However, to prevent clients from sticking to cookie-less backup server
4004 * when they have incidentely learned an empty cookie, we simply ignore
4005 * empty cookies and mark them as invalid.
4006 */
4007 if (delim == p3)
4008 srv = NULL;
4009
4010 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004011 if (srv->cookie && (srv->cklen == delim - p3) &&
4012 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004013 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004014 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004015 txn->flags &= ~TX_CK_MASK;
4016 txn->flags |= TX_CK_VALID;
4017 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004018 t->srv = srv;
4019 break;
4020 } else {
4021 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004022 txn->flags &= ~TX_CK_MASK;
4023 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004024 }
4025 }
4026 srv = srv->next;
4027 }
4028
Willy Tarreau3d300592007-03-18 18:34:41 +01004029 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004030 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004031 txn->flags &= ~TX_CK_MASK;
4032 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004033 }
4034
4035 /* depending on the cookie mode, we may have to either :
4036 * - delete the complete cookie if we're in insert+indirect mode, so that
4037 * the server never sees it ;
4038 * - remove the server id from the cookie value, and tag the cookie as an
4039 * application cookie so that it does not get accidentely removed later,
4040 * if we're in cookie prefix mode
4041 */
Willy Tarreau830ff452006-12-17 19:31:23 +01004042 if ((t->be->beprm->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004043 int delta; /* negative */
4044
4045 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4046 p4 += delta;
4047 cur_end += delta;
4048 cur_next += delta;
4049 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004050 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004051
4052 del_cookie = del_colon = NULL;
4053 app_cookies++; /* protect the header from deletion */
4054 }
4055 else if (del_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01004056 (t->be->beprm->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004057 del_cookie = p1;
4058 del_colon = colon;
4059 }
4060 } else {
4061 /* now we know that we must keep this cookie since it's
4062 * not ours. But if we wanted to delete our cookie
4063 * earlier, we cannot remove the complete header, but we
4064 * can remove the previous block itself.
4065 */
4066 app_cookies++;
4067
4068 if (del_cookie != NULL) {
4069 int delta; /* negative */
4070
4071 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4072 p4 += delta;
4073 cur_end += delta;
4074 cur_next += delta;
4075 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004076 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004077 del_cookie = del_colon = NULL;
4078 }
4079 }
4080
Willy Tarreau830ff452006-12-17 19:31:23 +01004081 if ((t->be->beprm->appsession_name != NULL) &&
4082 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004083 /* first, let's see if the cookie is our appcookie*/
4084
4085 /* Cool... it's the right one */
4086
4087 asession_temp = &local_asession;
4088
4089 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4090 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4091 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4092 return;
4093 }
4094
Willy Tarreau830ff452006-12-17 19:31:23 +01004095 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4096 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004097 asession_temp->serverid = NULL;
4098
4099 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004100 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004101 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4102 /* free previously allocated memory */
4103 pool_free_to(apools.sessid, local_asession.sessid);
4104 Alert("Not enough memory process_cli():asession:calloc().\n");
4105 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4106 return;
4107 }
4108
4109 asession_temp->sessid = local_asession.sessid;
4110 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004111 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004112 } else {
4113 /* free previously allocated memory */
4114 pool_free_to(apools.sessid, local_asession.sessid);
4115 }
4116
4117 if (asession_temp->serverid == NULL) {
4118 Alert("Found Application Session without matching server.\n");
4119 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004120 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004121 while (srv) {
4122 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004123 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004124 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004125 txn->flags &= ~TX_CK_MASK;
4126 txn->flags |= TX_CK_VALID;
4127 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004128 t->srv = srv;
4129 break;
4130 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004131 txn->flags &= ~TX_CK_MASK;
4132 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004133 }
4134 }
4135 srv = srv->next;
4136 }/* end while(srv) */
4137 }/* end else if server == NULL */
4138
Willy Tarreau830ff452006-12-17 19:31:23 +01004139 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004140 }/* end if ((t->proxy->appsession_name != NULL) ... */
4141 }
4142
4143 /* we'll have to look for another cookie ... */
4144 p1 = p4;
4145 } /* while (p1 < cur_end) */
4146
4147 /* There's no more cookie on this line.
4148 * We may have marked the last one(s) for deletion.
4149 * We must do this now in two ways :
4150 * - if there is no app cookie, we simply delete the header ;
4151 * - if there are app cookies, we must delete the end of the
4152 * string properly, including the colon/semi-colon before
4153 * the cookie name.
4154 */
4155 if (del_cookie != NULL) {
4156 int delta;
4157 if (app_cookies) {
4158 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4159 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004160 cur_hdr->len += delta;
4161 } else {
4162 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004163
4164 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004165 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4166 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004167 cur_hdr->len = 0;
4168 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004169 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004170 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004171 }
4172
4173 /* keep the link from this header to next one */
4174 old_idx = cur_idx;
4175 } /* end of cookie processing on this header */
4176}
4177
4178
Willy Tarreaua15645d2007-03-18 16:22:39 +01004179/* Iterate the same filter through all response headers contained in <rtr>.
4180 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4181 */
4182int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4183{
4184 char term;
4185 char *cur_ptr, *cur_end, *cur_next;
4186 int cur_idx, old_idx, last_hdr;
4187 struct http_txn *txn = &t->txn;
4188 struct hdr_idx_elem *cur_hdr;
4189 int len, delta;
4190
4191 last_hdr = 0;
4192
4193 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4194 old_idx = 0;
4195
4196 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004197 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004198 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004199 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004200 (exp->action == ACT_ALLOW ||
4201 exp->action == ACT_DENY))
4202 return 0;
4203
4204 cur_idx = txn->hdr_idx.v[old_idx].next;
4205 if (!cur_idx)
4206 break;
4207
4208 cur_hdr = &txn->hdr_idx.v[cur_idx];
4209 cur_ptr = cur_next;
4210 cur_end = cur_ptr + cur_hdr->len;
4211 cur_next = cur_end + cur_hdr->cr + 1;
4212
4213 /* Now we have one header between cur_ptr and cur_end,
4214 * and the next header starts at cur_next.
4215 */
4216
4217 /* The annoying part is that pattern matching needs
4218 * that we modify the contents to null-terminate all
4219 * strings before testing them.
4220 */
4221
4222 term = *cur_end;
4223 *cur_end = '\0';
4224
4225 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4226 switch (exp->action) {
4227 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004228 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004229 last_hdr = 1;
4230 break;
4231
4232 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004233 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004234 last_hdr = 1;
4235 break;
4236
4237 case ACT_REPLACE:
4238 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4239 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4240 /* FIXME: if the user adds a newline in the replacement, the
4241 * index will not be recalculated for now, and the new line
4242 * will not be counted as a new header.
4243 */
4244
4245 cur_end += delta;
4246 cur_next += delta;
4247 cur_hdr->len += delta;
4248 txn->rsp.eoh += delta;
4249 break;
4250
4251 case ACT_REMOVE:
4252 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4253 cur_next += delta;
4254
4255 /* FIXME: this should be a separate function */
4256 txn->rsp.eoh += delta;
4257 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4258 txn->hdr_idx.used--;
4259 cur_hdr->len = 0;
4260 cur_end = NULL; /* null-term has been rewritten */
4261 break;
4262
4263 }
4264 }
4265 if (cur_end)
4266 *cur_end = term; /* restore the string terminator */
4267
4268 /* keep the link from this header to next one in case of later
4269 * removal of next header.
4270 */
4271 old_idx = cur_idx;
4272 }
4273 return 0;
4274}
4275
4276
4277/* Apply the filter to the status line in the response buffer <rtr>.
4278 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4279 * or -1 if a replacement resulted in an invalid status line.
4280 */
4281int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4282{
4283 char term;
4284 char *cur_ptr, *cur_end;
4285 int done;
4286 struct http_txn *txn = &t->txn;
4287 int len, delta;
4288
4289
Willy Tarreau3d300592007-03-18 18:34:41 +01004290 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004291 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004292 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004293 (exp->action == ACT_ALLOW ||
4294 exp->action == ACT_DENY))
4295 return 0;
4296 else if (exp->action == ACT_REMOVE)
4297 return 0;
4298
4299 done = 0;
4300
4301 cur_ptr = rtr->data + txn->rsp.som;
4302 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4303
4304 /* Now we have the status line between cur_ptr and cur_end */
4305
4306 /* The annoying part is that pattern matching needs
4307 * that we modify the contents to null-terminate all
4308 * strings before testing them.
4309 */
4310
4311 term = *cur_end;
4312 *cur_end = '\0';
4313
4314 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4315 switch (exp->action) {
4316 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004317 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004318 done = 1;
4319 break;
4320
4321 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004322 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004323 done = 1;
4324 break;
4325
4326 case ACT_REPLACE:
4327 *cur_end = term; /* restore the string terminator */
4328 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4329 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4330 /* FIXME: if the user adds a newline in the replacement, the
4331 * index will not be recalculated for now, and the new line
4332 * will not be counted as a new header.
4333 */
4334
4335 txn->rsp.eoh += delta;
4336 cur_end += delta;
4337
4338 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
4339 HTTP_MSG_RQMETH,
4340 cur_ptr, cur_end + 1,
4341 NULL, NULL);
4342 if (unlikely(!cur_end))
4343 return -1;
4344
4345 /* we have a full respnse and we know that we have either a CR
4346 * or an LF at <ptr>.
4347 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004348 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004349 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4350 /* there is no point trying this regex on headers */
4351 return 1;
4352 }
4353 }
4354 *cur_end = term; /* restore the string terminator */
4355 return done;
4356}
4357
4358
4359
4360/*
4361 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4362 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4363 * unparsable response.
4364 */
4365int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4366{
Willy Tarreau3d300592007-03-18 18:34:41 +01004367 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004368 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004369 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004370 int ret;
4371
4372 /*
4373 * The interleaving of transformations and verdicts
4374 * makes it difficult to decide to continue or stop
4375 * the evaluation.
4376 */
4377
Willy Tarreau3d300592007-03-18 18:34:41 +01004378 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004379 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4380 exp->action == ACT_PASS)) {
4381 exp = exp->next;
4382 continue;
4383 }
4384
4385 /* Apply the filter to the status line. */
4386 ret = apply_filter_to_sts_line(t, rtr, exp);
4387 if (unlikely(ret < 0))
4388 return -1;
4389
4390 if (likely(ret == 0)) {
4391 /* The filter did not match the response, it can be
4392 * iterated through all headers.
4393 */
4394 apply_filter_to_resp_headers(t, rtr, exp);
4395 }
4396 exp = exp->next;
4397 }
4398 return 0;
4399}
4400
4401
4402
4403/*
4404 * Manager server-side cookies
4405 */
4406void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4407{
4408 struct http_txn *txn = &t->txn;
4409 char *p1, *p2, *p3, *p4;
4410
4411 appsess *asession_temp = NULL;
4412 appsess local_asession;
4413
4414 char *cur_ptr, *cur_end, *cur_next;
4415 int cur_idx, old_idx, delta;
4416
4417 if (t->be->beprm->cookie_name == NULL &&
4418 t->be->beprm->appsession_name == NULL &&
4419 t->be->fiprm->capture_name == NULL &&
4420 !(t->be->beprm->options & PR_O_CHK_CACHE))
4421 return;
4422
4423 /* Iterate through the headers.
4424 * we start with the start line.
4425 */
4426 old_idx = 0;
4427 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4428
4429 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4430 struct hdr_idx_elem *cur_hdr;
4431
4432 cur_hdr = &txn->hdr_idx.v[cur_idx];
4433 cur_ptr = cur_next;
4434 cur_end = cur_ptr + cur_hdr->len;
4435 cur_next = cur_end + cur_hdr->cr + 1;
4436
4437 /* We have one full header between cur_ptr and cur_end, and the
4438 * next header starts at cur_next. We're only interested in
4439 * "Cookie:" headers.
4440 */
4441
4442 if ((cur_end - cur_ptr <= 11) ||
4443 (strncasecmp(cur_ptr, "Set-Cookie:", 11) != 0)) {
4444 old_idx = cur_idx;
4445 continue;
4446 }
4447
4448 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004449 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004450
4451
4452 /* maybe we only wanted to see if there was a set-cookie */
4453 if (t->be->beprm->cookie_name == NULL &&
4454 t->be->beprm->appsession_name == NULL &&
4455 t->be->fiprm->capture_name == NULL)
4456 return;
4457
4458 p1 = cur_ptr + 11; /* first char after 'Set-Cookie:' */
4459
4460 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
4461 while (p1 < cur_end && (isspace((int)*p1)))
4462 p1++;
4463
4464 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4465 break;
4466
4467 /* p1 is at the beginning of the cookie name */
4468 p2 = p1;
4469
4470 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4471 p2++;
4472
4473 if (p2 == cur_end || *p2 == ';') /* next cookie */
4474 break;
4475
4476 p3 = p2 + 1; /* skip the '=' sign */
4477 if (p3 == cur_end)
4478 break;
4479
4480 p4 = p3;
4481 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4482 p4++;
4483
4484 /* here, we have the cookie name between p1 and p2,
4485 * and its value between p3 and p4.
4486 * we can process it.
4487 */
4488
4489 /* first, let's see if we want to capture it */
4490 if (t->be->fiprm->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004491 txn->srv_cookie == NULL &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004492 (p4 - p1 >= t->be->fiprm->capture_namelen) &&
4493 memcmp(p1, t->be->fiprm->capture_name, t->be->fiprm->capture_namelen) == 0) {
4494 int log_len = p4 - p1;
4495
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004496 if ((txn->srv_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004497 Alert("HTTP logging : out of memory.\n");
4498 }
4499
4500 if (log_len > t->be->fiprm->capture_len)
4501 log_len = t->be->fiprm->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004502 memcpy(txn->srv_cookie, p1, log_len);
4503 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004504 }
4505
4506 /* now check if we need to process it for persistence */
4507 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
4508 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
4509 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004510 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004511
4512 /* If the cookie is in insert mode on a known server, we'll delete
4513 * this occurrence because we'll insert another one later.
4514 * We'll delete it too if the "indirect" option is set and we're in
4515 * a direct access. */
4516 if (((t->srv) && (t->be->beprm->options & PR_O_COOK_INS)) ||
4517 ((t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_IND))) {
4518 /* this header must be deleted */
4519 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4520 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4521 txn->hdr_idx.used--;
4522 cur_hdr->len = 0;
4523 cur_next += delta;
4524 txn->rsp.eoh += delta;
4525
Willy Tarreau3d300592007-03-18 18:34:41 +01004526 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004527 }
4528 else if ((t->srv) && (t->srv->cookie) &&
4529 (t->be->beprm->options & PR_O_COOK_RW)) {
4530 /* replace bytes p3->p4 with the cookie name associated
4531 * with this server since we know it.
4532 */
4533 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4534 cur_hdr->len += delta;
4535 cur_next += delta;
4536 txn->rsp.eoh += delta;
4537
Willy Tarreau3d300592007-03-18 18:34:41 +01004538 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004539 }
4540 else if ((t->srv) && (t->srv->cookie) &&
4541 (t->be->beprm->options & PR_O_COOK_PFX)) {
4542 /* insert the cookie name associated with this server
4543 * before existing cookie, and insert a delimitor between them..
4544 */
4545 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4546 cur_hdr->len += delta;
4547 cur_next += delta;
4548 txn->rsp.eoh += delta;
4549
4550 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004551 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004552 }
4553 }
4554 /* next, let's see if the cookie is our appcookie */
4555 else if ((t->be->beprm->appsession_name != NULL) &&
4556 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
4557
4558 /* Cool... it's the right one */
4559
4560 size_t server_id_len = strlen(t->srv->id) + 1;
4561 asession_temp = &local_asession;
4562
4563 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4564 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4565 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4566 return;
4567 }
4568 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4569 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
4570 asession_temp->serverid = NULL;
4571
4572 /* only do insert, if lookup fails */
4573 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
4574 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4575 Alert("Not enough Memory process_srv():asession:calloc().\n");
4576 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4577 return;
4578 }
4579 asession_temp->sessid = local_asession.sessid;
4580 asession_temp->serverid = local_asession.serverid;
4581 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
4582 }/* end if (chtbl_lookup()) */
4583 else {
4584 /* free wasted memory */
4585 pool_free_to(apools.sessid, local_asession.sessid);
4586 } /* end else from if (chtbl_lookup()) */
4587
4588 if (asession_temp->serverid == NULL) {
4589 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
4590 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4591 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4592 return;
4593 }
4594 asession_temp->serverid[0] = '\0';
4595 }
4596
4597 if (asession_temp->serverid[0] == '\0')
4598 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4599
4600 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
4601
4602#if defined(DEBUG_HASH)
4603 print_table(&(t->be->beprm->htbl_proxy));
4604#endif
4605 }/* end if ((t->proxy->appsession_name != NULL) ... */
4606 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4607 } /* we're now at the end of the cookie value */
4608
4609 /* keep the link from this header to next one */
4610 old_idx = cur_idx;
4611 } /* end of cookie processing on this header */
4612}
4613
4614
4615
4616/*
4617 * Check if response is cacheable or not. Updates t->flags.
4618 */
4619void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4620{
4621 struct http_txn *txn = &t->txn;
4622 char *p1, *p2;
4623
4624 char *cur_ptr, *cur_end, *cur_next;
4625 int cur_idx;
4626
Willy Tarreau3d300592007-03-18 18:34:41 +01004627 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004628 return;
4629
4630 /* Iterate through the headers.
4631 * we start with the start line.
4632 */
4633 cur_idx = 0;
4634 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4635
4636 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4637 struct hdr_idx_elem *cur_hdr;
4638
4639 cur_hdr = &txn->hdr_idx.v[cur_idx];
4640 cur_ptr = cur_next;
4641 cur_end = cur_ptr + cur_hdr->len;
4642 cur_next = cur_end + cur_hdr->cr + 1;
4643
4644 /* We have one full header between cur_ptr and cur_end, and the
4645 * next header starts at cur_next. We're only interested in
4646 * "Cookie:" headers.
4647 */
4648
4649 if ((cur_end - cur_ptr >= 16) &&
4650 strncasecmp(cur_ptr, "Pragma: no-cache", 16) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004651 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004652 return;
4653 }
4654
4655 if ((cur_end - cur_ptr <= 14) ||
4656 (strncasecmp(cur_ptr, "Cache-control:", 14) != 0))
4657 continue;
4658
4659 /* OK, right now we know we have a cache-control header at cur_ptr */
4660
4661 p1 = cur_ptr + 14; /* first char after 'cache-control:' */
4662
4663 while (p1 < cur_end && (isspace((int)*p1)))
4664 p1++;
4665
4666 if (p1 >= cur_end) /* no more info */
4667 continue;
4668
4669 /* p1 is at the beginning of the value */
4670 p2 = p1;
4671
4672 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4673 p2++;
4674
4675 /* we have a complete value between p1 and p2 */
4676 if (p2 < cur_end && *p2 == '=') {
4677 /* we have something of the form no-cache="set-cookie" */
4678 if ((cur_end - p1 >= 21) &&
4679 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4680 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004681 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004682 continue;
4683 }
4684
4685 /* OK, so we know that either p2 points to the end of string or to a comma */
4686 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4687 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4688 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4689 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004690 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004691 return;
4692 }
4693
4694 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004695 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004696 continue;
4697 }
4698 }
4699}
4700
4701
Willy Tarreau58f10d72006-12-04 02:26:12 +01004702/*
4703 * Try to retrieve a known appsession in the URI, then the associated server.
4704 * If the server is found, it's assigned to the session.
4705 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004706void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004707{
Willy Tarreau3d300592007-03-18 18:34:41 +01004708 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004709 appsess *asession_temp = NULL;
4710 appsess local_asession;
4711 char *request_line;
4712
Willy Tarreau830ff452006-12-17 19:31:23 +01004713 if (t->be->beprm->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004714 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004715 (request_line = memchr(begin, ';', len)) == NULL ||
4716 ((1 + t->be->beprm->appsession_name_len + 1 + t->be->beprm->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004717 return;
4718
4719 /* skip ';' */
4720 request_line++;
4721
4722 /* look if we have a jsessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004723 if (strncasecmp(request_line, t->be->beprm->appsession_name, t->be->beprm->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004724 return;
4725
4726 /* skip jsessionid= */
Willy Tarreau830ff452006-12-17 19:31:23 +01004727 request_line += t->be->beprm->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004728
4729 /* First try if we already have an appsession */
4730 asession_temp = &local_asession;
4731
4732 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4733 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4734 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4735 return;
4736 }
4737
4738 /* Copy the sessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004739 memcpy(asession_temp->sessid, request_line, t->be->beprm->appsession_len);
4740 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004741 asession_temp->serverid = NULL;
4742
4743 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004744 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004745 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4746 /* free previously allocated memory */
4747 pool_free_to(apools.sessid, local_asession.sessid);
4748 Alert("Not enough memory process_cli():asession:calloc().\n");
4749 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4750 return;
4751 }
4752 asession_temp->sessid = local_asession.sessid;
4753 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004754 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004755 }
4756 else {
4757 /* free previously allocated memory */
4758 pool_free_to(apools.sessid, local_asession.sessid);
4759 }
4760
Willy Tarreau830ff452006-12-17 19:31:23 +01004761 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004762 asession_temp->request_count++;
4763
4764#if defined(DEBUG_HASH)
4765 print_table(&(t->proxy->htbl_proxy));
4766#endif
4767 if (asession_temp->serverid == NULL) {
4768 Alert("Found Application Session without matching server.\n");
4769 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004770 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004771 while (srv) {
4772 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004773 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004774 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004775 txn->flags &= ~TX_CK_MASK;
4776 txn->flags |= TX_CK_VALID;
4777 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004778 t->srv = srv;
4779 break;
4780 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004781 txn->flags &= ~TX_CK_MASK;
4782 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004783 }
4784 }
4785 srv = srv->next;
4786 }
4787 }
4788}
4789
4790
Willy Tarreaub2513902006-12-17 14:52:38 +01004791/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004792 * In a GET or HEAD request, check if the requested URI matches the stats uri
4793 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004794 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004795 * It is assumed that the request is either a HEAD or GET and that the
4796 * t->be->fiprm->uri_auth field is valid. An HTTP/401 response may be sent, or
4797 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004798 *
4799 * Returns 1 if the session's state changes, otherwise 0.
4800 */
4801int stats_check_uri_auth(struct session *t, struct proxy *backend)
4802{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004803 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004804 struct uri_auth *uri_auth = backend->uri_auth;
4805 struct user_auth *user;
4806 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004807 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004808
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004809 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004810 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004811 return 0;
4812
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004813 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004814
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004815 /* the URI is in h */
4816 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004817 return 0;
4818
4819 /* we are in front of a interceptable URI. Let's check
4820 * if there's an authentication and if it's valid.
4821 */
4822 user = uri_auth->users;
4823 if (!user) {
4824 /* no user auth required, it's OK */
4825 authenticated = 1;
4826 } else {
4827 authenticated = 0;
4828
4829 /* a user list is defined, we have to check.
4830 * skip 21 chars for "Authorization: Basic ".
4831 */
4832
4833 /* FIXME: this should move to an earlier place */
4834 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004835 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4836 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4837 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004838 if (len > 14 &&
4839 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004840 txn->auth_hdr.str = h;
4841 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004842 break;
4843 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004844 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004845 }
4846
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004847 if (txn->auth_hdr.len < 21 ||
4848 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004849 user = NULL;
4850
4851 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004852 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4853 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004854 user->user_pwd, user->user_len)) {
4855 authenticated = 1;
4856 break;
4857 }
4858 user = user->next;
4859 }
4860 }
4861
4862 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004863 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004864
4865 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004866 msg.str = trash;
4867 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004868 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004869 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004870 if (!(t->flags & SN_ERR_MASK))
4871 t->flags |= SN_ERR_PRXCOND;
4872 if (!(t->flags & SN_FINST_MASK))
4873 t->flags |= SN_FINST_R;
4874 return 1;
4875 }
4876
4877 /* The request is valid, the user is authenticate. Let's start sending
4878 * data.
4879 */
4880 t->cli_state = CL_STSHUTR;
4881 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
4882 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
4883 t->data_source = DATA_SRC_STATS;
4884 t->data_state = DATA_ST_INIT;
4885 produce_content(t);
4886 return 1;
4887}
4888
4889
Willy Tarreaubaaee002006-06-26 02:48:02 +02004890/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004891 * Print a debug line with a header
4892 */
4893void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4894{
4895 int len, max;
4896 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4897 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4898 max = end - start;
4899 UBOUND(max, sizeof(trash) - len - 1);
4900 len += strlcpy2(trash + len, start, max + 1);
4901 trash[len++] = '\n';
4902 write(1, trash, len);
4903}
4904
4905
4906/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004907 * Local variables:
4908 * c-indent-level: 8
4909 * c-basic-offset: 8
4910 * End:
4911 */