blob: ea9ebc851341d1eb737fd76ec9f3f194e7a80586 [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 Tarreaubaaee002006-06-26 02:48:02 +0200365 t->logs.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. */
1290 t->logs.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;
1341 t->logs.status = 200;
1342 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 */
1353 if ((t->logs.uri = pool_alloc(requri)) != NULL) {
1354 int urilen = msg->sl.rq.l;
1355
1356 if (urilen >= REQURI_LEN)
1357 urilen = REQURI_LEN - 1;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001358 memcpy(t->logs.uri, &req->data[msg->som], urilen);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001359 t->logs.uri[urilen] = 0;
1360
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 ? */
1453 if (t->flags & SN_CLDENY) {
1454 /* no need to go further */
1455 t->logs.status = 403;
1456 /* 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 */
1588 if (!(t->flags & (SN_CLDENY|SN_CLTARPIT)))
1589 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 */
1670 if (t->flags & SN_CLTARPIT) {
1671 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 Tarreau06619262006-12-17 08:37:22 +01001684 t->logs.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;
1989 struct buffer *req = t->req;
1990 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001991 int conn_err;
1992
1993#ifdef DEBUG_FULL
1994 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
1995#endif
1996 //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 +02001997 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
1998 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001999 //);
2000 if (s == SV_STIDLE) {
2001 if (c == CL_STHEADERS)
2002 return 0; /* stay in idle, waiting for data to reach the client side */
2003 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2004 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002005 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002006 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002007 if (t->pend_pos)
2008 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2009 /* note that this must not return any error because it would be able to
2010 * overwrite the client_retnclose() output.
2011 */
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002012 if (t->flags & SN_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002013 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002014 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002015 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 +02002016
2017 return 1;
2018 }
2019 else {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002020 if (t->flags & SN_CLTARPIT) {
2021 /* This connection is being tarpitted. The CLIENT side has
2022 * already set the connect expiration date to the right
2023 * timeout. We just have to check that it has not expired.
2024 */
2025 if (tv_cmp2_ms(&req->cex, &now) > 0)
2026 return 0;
2027
2028 /* We will set the queue timer to the time spent, just for
2029 * logging purposes. We fake a 500 server error, so that the
2030 * attacker will not suspect his connection has been tarpitted.
2031 * It will not cause trouble to the logs because we can exclude
2032 * the tarpitted connections by filtering on the 'PT' status flags.
2033 */
2034 tv_eternity(&req->cex);
2035 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2036 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002037 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002038 return 1;
2039 }
2040
Willy Tarreaubaaee002006-06-26 02:48:02 +02002041 /* Right now, we will need to create a connection to the server.
2042 * We might already have tried, and got a connection pending, in
2043 * which case we will not do anything till it's pending. It's up
2044 * to any other session to release it and wake us up again.
2045 */
2046 if (t->pend_pos) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002047 if (tv_cmp2_ms(&req->cex, &now) > 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002048 return 0;
2049 else {
2050 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002051 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002052 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2053 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002054 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002055 if (t->srv)
2056 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002057 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002058 return 1;
2059 }
2060 }
2061
2062 do {
2063 /* first, get a connection */
2064 if (srv_redispatch_connect(t))
2065 return t->srv_state != SV_STIDLE;
2066
2067 /* try to (re-)connect to the server, and fail if we expire the
2068 * number of retries.
2069 */
2070 if (srv_retryable_connect(t)) {
2071 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2072 return t->srv_state != SV_STIDLE;
2073 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002074 } while (1);
2075 }
2076 }
2077 else if (s == SV_STCONN) { /* connection in progress */
2078 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2079 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002080 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002081 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002082 fd_delete(t->srv_fd);
2083 if (t->srv)
2084 t->srv->cur_sess--;
2085
2086 /* note that this must not return any error because it would be able to
2087 * overwrite the client_retnclose() output.
2088 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002089 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002090 return 1;
2091 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002092 if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) {
2093 //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 +02002094 return 0; /* nothing changed */
2095 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002096 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002097 /* timeout, asynchronous connect error or first write error */
2098 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2099
2100 fd_delete(t->srv_fd);
2101 if (t->srv)
2102 t->srv->cur_sess--;
2103
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002104 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002105 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2106 else
2107 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2108
2109 /* ensure that we have enough retries left */
2110 if (srv_count_retry_down(t, conn_err))
2111 return 1;
2112
Willy Tarreau830ff452006-12-17 19:31:23 +01002113 if (t->srv && t->conn_retries == 0 && t->be->beprm->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002114 /* We're on our last chance, and the REDISP option was specified.
2115 * We will ignore cookie and force to balance or use the dispatcher.
2116 */
2117 /* let's try to offer this slot to anybody */
Willy Tarreau830ff452006-12-17 19:31:23 +01002118 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002119 task_wakeup(&rq, t->srv->queue_mgt);
2120
2121 if (t->srv)
2122 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +01002123 t->be->beprm->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002124
2125 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2126 t->srv = NULL; /* it's left to the dispatcher to choose a server */
2127 if ((t->flags & SN_CK_MASK) == SN_CK_VALID) {
2128 t->flags &= ~SN_CK_MASK;
2129 t->flags |= SN_CK_DOWN;
2130 }
2131
2132 /* first, get a connection */
2133 if (srv_redispatch_connect(t))
2134 return t->srv_state != SV_STIDLE;
2135 }
2136
Willy Tarreaubaaee002006-06-26 02:48:02 +02002137 do {
2138 /* Now we will try to either reconnect to the same server or
2139 * connect to another server. If the connection gets queued
2140 * because all servers are saturated, then we will go back to
2141 * the SV_STIDLE state.
2142 */
2143 if (srv_retryable_connect(t)) {
2144 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2145 return t->srv_state != SV_STCONN;
2146 }
2147
2148 /* we need to redispatch the connection to another server */
2149 if (srv_redispatch_connect(t))
2150 return t->srv_state != SV_STCONN;
2151 } while (1);
2152 }
2153 else { /* no error or write 0 */
2154 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
2155
2156 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2157 if (req->l == 0) /* nothing to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002158 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002159 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002160 } else /* need the right to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002161 MY_FD_SET(t->srv_fd, StaticWriteEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002162 if (t->be->beprm->srvtimeout) {
2163 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002164 /* FIXME: to prevent the server from expiring read timeouts during writes,
2165 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002166 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002167 }
2168 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002169 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002170 }
2171
Willy Tarreau830ff452006-12-17 19:31:23 +01002172 if (t->be->beprm->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau2a429502006-10-15 14:52:29 +02002173 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002174 if (t->be->beprm->srvtimeout)
2175 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002176 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002177 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002178
2179 t->srv_state = SV_STDATA;
2180 if (t->srv)
2181 t->srv->cum_sess++;
2182 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2183
2184 /* if the user wants to log as soon as possible, without counting
2185 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002186 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002187 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
2188 sess_log(t);
2189 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002190#ifdef CONFIG_HAP_TCPSPLICE
2191 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2192 /* TCP splicing supported by both FE and BE */
2193 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2194 }
2195#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002196 }
2197 else {
2198 t->srv_state = SV_STHEADERS;
2199 if (t->srv)
2200 t->srv->cum_sess++;
2201 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002202 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2203 /* reset hdr_idx which was already initialized by the request.
2204 * right now, the http parser does it.
2205 * hdr_idx_init(&t->txn.hdr_idx);
2206 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002207 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002208 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002209 return 1;
2210 }
2211 }
2212 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002213 /*
2214 * Now parse the partial (or complete) lines.
2215 * We will check the response syntax, and also join multi-line
2216 * headers. An index of all the lines will be elaborated while
2217 * parsing.
2218 *
2219 * For the parsing, we use a 28 states FSM.
2220 *
2221 * Here is the information we currently have :
2222 * rep->data + req->som = beginning of response
2223 * rep->data + req->eoh = end of processed headers / start of current one
2224 * rep->data + req->eol = end of current header or line (LF or CRLF)
2225 * rep->lr = first non-visited byte
2226 * rep->r = end of data
2227 */
2228
2229 int cur_idx;
2230 struct http_txn *txn = &t->txn;
2231 struct http_msg *msg = &txn->rsp;
2232 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002233
Willy Tarreaua15645d2007-03-18 16:22:39 +01002234 if (likely(rep->lr < rep->r))
2235 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002236
Willy Tarreaua15645d2007-03-18 16:22:39 +01002237 /* 1: we might have to print this header in debug mode */
2238 if (unlikely((global.mode & MODE_DEBUG) &&
2239 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2240 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2241 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002242
Willy Tarreaua15645d2007-03-18 16:22:39 +01002243 sol = rep->data + msg->som;
2244 eol = sol + msg->sl.rq.l;
2245 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002246
Willy Tarreaua15645d2007-03-18 16:22:39 +01002247 sol += hdr_idx_first_pos(&txn->hdr_idx);
2248 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002249
Willy Tarreaua15645d2007-03-18 16:22:39 +01002250 while (cur_idx) {
2251 eol = sol + txn->hdr_idx.v[cur_idx].len;
2252 debug_hdr("srvhdr", t, sol, eol);
2253 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2254 cur_idx = txn->hdr_idx.v[cur_idx].next;
2255 }
2256 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002257
Willy Tarreaubaaee002006-06-26 02:48:02 +02002258
Willy Tarreaua15645d2007-03-18 16:22:39 +01002259 if ((rep->l < rep->rlim - rep->data) && ! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2260 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
2261 * full. We cannot loop here since stream_sock_read will disable it only if
2262 * rep->l == rlim-data
2263 */
2264 MY_FD_SET(t->srv_fd, StaticReadEvent);
2265 if (t->be->beprm->srvtimeout)
2266 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2267 else
2268 tv_eternity(&rep->rex);
2269 }
2270
2271
2272 /*
2273 * Now we quickly check if we have found a full valid response.
2274 * If not so, we check the FD and buffer states before leaving.
2275 * A full response is indicated by the fact that we have seen
2276 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2277 * responses are checked first.
2278 *
2279 * Depending on whether the client is still there or not, we
2280 * may send an error response back or not. Note that normally
2281 * we should only check for HTTP status there, and check I/O
2282 * errors somewhere else.
2283 */
2284
2285 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2286
2287 /* Invalid response, or read error or write error */
2288 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2289 (req->flags & BF_WRITE_ERROR) ||
2290 (rep->flags & BF_READ_ERROR))) {
2291 tv_eternity(&rep->rex);
2292 tv_eternity(&req->wex);
2293 fd_delete(t->srv_fd);
2294 if (t->srv) {
2295 t->srv->cur_sess--;
2296 t->srv->failed_resp++;
2297 }
2298 t->be->failed_resp++;
2299 t->srv_state = SV_STCLOSE;
2300 t->logs.status = 502;
2301 client_return(t, error_message(t, HTTP_ERR_502));
2302 if (!(t->flags & SN_ERR_MASK))
2303 t->flags |= SN_ERR_SRVCL;
2304 if (!(t->flags & SN_FINST_MASK))
2305 t->flags |= SN_FINST_H;
2306 /* We used to have a free connection slot. Since we'll never use it,
2307 * we have to inform the server that it may be used by another session.
2308 */
2309 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2310 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002311
Willy Tarreaua15645d2007-03-18 16:22:39 +01002312 return 1;
2313 }
2314
2315 /* end of client write or end of server read.
2316 * since we are in header mode, if there's no space left for headers, we
2317 * won't be able to free more later, so the session will never terminate.
2318 */
2319 else if (unlikely(rep->flags & BF_READ_NULL ||
2320 c == CL_STSHUTW || c == CL_STCLOSE ||
2321 rep->l >= rep->rlim - rep->data)) {
2322 MY_FD_CLR(t->srv_fd, StaticReadEvent);
2323 tv_eternity(&rep->rex);
2324 shutdown(t->srv_fd, SHUT_RD);
2325 t->srv_state = SV_STSHUTR;
2326 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2327 return 1;
2328 }
2329
2330 /* read timeout : return a 504 to the client.
2331 */
2332 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticReadEvent) &&
2333 tv_cmp2_ms(&rep->rex, &now) <= 0)) {
2334 tv_eternity(&rep->rex);
2335 tv_eternity(&req->wex);
2336 fd_delete(t->srv_fd);
2337 if (t->srv) {
2338 t->srv->cur_sess--;
2339 t->srv->failed_resp++;
2340 }
2341 t->be->failed_resp++;
2342 t->srv_state = SV_STCLOSE;
2343 t->logs.status = 504;
2344 client_return(t, error_message(t, HTTP_ERR_504));
2345 if (!(t->flags & SN_ERR_MASK))
2346 t->flags |= SN_ERR_SRVTO;
2347 if (!(t->flags & SN_FINST_MASK))
2348 t->flags |= SN_FINST_H;
2349 /* We used to have a free connection slot. Since we'll never use it,
2350 * we have to inform the server that it may be used by another session.
2351 */
2352 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2353 task_wakeup(&rq, t->srv->queue_mgt);
2354 return 1;
2355 }
2356
2357 /* last client read and buffer empty */
2358 /* FIXME!!! here, we don't want to switch to SHUTW if the
2359 * client shuts read too early, because we may still have
2360 * some work to do on the headers.
2361 * The side-effect is that if the client completely closes its
2362 * connection during SV_STHEADER, the connection to the server
2363 * is kept until a response comes back or the timeout is reached.
2364 */
2365 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2366 (req->l == 0))) {
2367 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2368 tv_eternity(&req->wex);
2369
2370 /* We must ensure that the read part is still
2371 * alive when switching to shutw */
2372 MY_FD_SET(t->srv_fd, StaticReadEvent);
2373 if (t->be->beprm->srvtimeout)
2374 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2375
2376 shutdown(t->srv_fd, SHUT_WR);
2377 t->srv_state = SV_STSHUTW;
2378 return 1;
2379 }
2380
2381 /* write timeout */
2382 /* FIXME!!! here, we don't want to switch to SHUTW if the
2383 * client shuts read too early, because we may still have
2384 * some work to do on the headers.
2385 */
2386 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticWriteEvent) &&
2387 tv_cmp2_ms(&req->wex, &now) <= 0)) {
2388 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2389 tv_eternity(&req->wex);
2390 shutdown(t->srv_fd, SHUT_WR);
2391 /* We must ensure that the read part is still alive
2392 * when switching to shutw */
2393 MY_FD_SET(t->srv_fd, StaticReadEvent);
2394 if (t->be->beprm->srvtimeout)
2395 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2396
2397 t->srv_state = SV_STSHUTW;
2398 if (!(t->flags & SN_ERR_MASK))
2399 t->flags |= SN_ERR_SRVTO;
2400 if (!(t->flags & SN_FINST_MASK))
2401 t->flags |= SN_FINST_H;
2402 return 1;
2403 }
2404
2405 /*
2406 * And now the non-error cases.
2407 */
2408
2409 /* Data remaining in the request buffer.
2410 * This happens during the first pass here, and during
2411 * long posts.
2412 */
2413 else if (likely(req->l)) {
2414 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2415 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2416 if (t->be->beprm->srvtimeout) {
2417 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
2418 /* FIXME: to prevent the server from expiring read timeouts during writes,
2419 * we refresh it. */
2420 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002421 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002422 else
2423 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002424 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002425 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002426
Willy Tarreaua15645d2007-03-18 16:22:39 +01002427 /* nothing left in the request buffer */
2428 else {
2429 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2430 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002431 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002432 }
2433 }
2434
2435 return t->srv_state != SV_STHEADERS;
2436 }
2437
2438
2439 /*****************************************************************
2440 * More interesting part now : we know that we have a complete *
2441 * response which at least looks like HTTP. We have an indicator *
2442 * of each header's length, so we can parse them quickly. *
2443 ****************************************************************/
2444
2445 /*
2446 * 1: get the status code and check for cacheability.
2447 */
2448
2449 t->logs.logwait &= ~LW_RESP;
2450 t->logs.status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
2451
2452 switch (t->logs.status) {
2453 case 200:
2454 case 203:
2455 case 206:
2456 case 300:
2457 case 301:
2458 case 410:
2459 /* RFC2616 @13.4:
2460 * "A response received with a status code of
2461 * 200, 203, 206, 300, 301 or 410 MAY be stored
2462 * by a cache (...) unless a cache-control
2463 * directive prohibits caching."
2464 *
2465 * RFC2616 @9.5: POST method :
2466 * "Responses to this method are not cacheable,
2467 * unless the response includes appropriate
2468 * Cache-Control or Expires header fields."
2469 */
2470 if (likely(txn->meth != HTTP_METH_POST) &&
2471 unlikely(t->be->beprm->options & PR_O_CHK_CACHE))
2472 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
2473 break;
2474 default:
2475 break;
2476 }
2477
2478 /*
2479 * 2: we may need to capture headers
2480 */
2481 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->fiprm->rsp_cap))
2482 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2483 txn->rsp.cap, t->fe->fiprm->rsp_cap);
2484
2485 /*
2486 * 3: we will have to evaluate the filters.
2487 * As opposed to version 1.2, now they will be evaluated in the
2488 * filters order and not in the header order. This means that
2489 * each filter has to be validated among all headers.
2490 *
2491 * Filters are tried with ->be first, then with ->fe if it is
2492 * different from ->be.
2493 */
2494
2495 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2496
2497 cur_proxy = t->be;
2498 while (1) {
2499 struct proxy *rule_set = cur_proxy->fiprm;
2500
2501 /* try headers filters */
2502 if (rule_set->rsp_exp != NULL) {
2503 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2504 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002505 if (t->srv) {
2506 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002507 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002508 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002509 cur_proxy->beprm->failed_resp++;
2510 return_srv_prx_502:
2511 tv_eternity(&rep->rex);
2512 tv_eternity(&req->wex);
2513 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002514 t->srv_state = SV_STCLOSE;
2515 t->logs.status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002516 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002517 if (!(t->flags & SN_ERR_MASK))
2518 t->flags |= SN_ERR_PRXCOND;
2519 if (!(t->flags & SN_FINST_MASK))
2520 t->flags |= SN_FINST_H;
2521 /* We used to have a free connection slot. Since we'll never use it,
2522 * we have to inform the server that it may be used by another session.
2523 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002524 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002525 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002526 return 1;
2527 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002528 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002529
Willy Tarreaua15645d2007-03-18 16:22:39 +01002530 /* has the response been denied ? */
2531 if (t->flags & SN_SVDENY) {
2532 if (t->srv) {
2533 t->srv->cur_sess--;
2534 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002535 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002536 cur_proxy->beprm->denied_resp++;
2537 goto return_srv_prx_502;
2538 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002539
Willy Tarreaua15645d2007-03-18 16:22:39 +01002540 /* We might have to check for "Connection:" */
2541 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
2542 !(t->flags & SN_CONN_CLOSED)) {
2543 char *cur_ptr, *cur_end, *cur_next;
2544 int cur_idx, old_idx, delta;
2545 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002546
Willy Tarreaua15645d2007-03-18 16:22:39 +01002547 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2548 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002549
Willy Tarreaua15645d2007-03-18 16:22:39 +01002550 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2551 cur_hdr = &txn->hdr_idx.v[cur_idx];
2552 cur_ptr = cur_next;
2553 cur_end = cur_ptr + cur_hdr->len;
2554 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002555
Willy Tarreaua15645d2007-03-18 16:22:39 +01002556 if (strncasecmp(cur_ptr, "Connection:", 11) == 0) {
2557 /* 3 possibilities :
2558 * - we have already set Connection: close,
2559 * so we remove this line.
2560 * - we have not yet set Connection: close,
2561 * but this line indicates close. We leave
2562 * it untouched and set the flag.
2563 * - we have not yet set Connection: close,
2564 * and this line indicates non-close. We
2565 * replace it.
2566 */
2567 if (t->flags & SN_CONN_CLOSED) {
2568 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2569 txn->rsp.eoh += delta;
2570 cur_next += delta;
2571 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2572 txn->hdr_idx.used--;
2573 cur_hdr->len = 0;
2574 } else {
2575 if (cur_ptr + 17 > cur_end ||
2576 !http_is_lws[(unsigned char)*(cur_ptr+17)] ||
2577 strncasecmp(cur_ptr+11, " close", 6)) {
2578 delta = buffer_replace2(rep, cur_ptr+11, cur_end,
2579 " close", 6);
2580 cur_next += delta;
2581 cur_hdr->len += delta;
2582 txn->rsp.eoh += delta;
2583 }
2584 t->flags |= SN_CONN_CLOSED;
2585 }
2586 }
2587 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002588 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002589 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002590
Willy Tarreaua15645d2007-03-18 16:22:39 +01002591 /* add response headers from the rule sets in the same order */
2592 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2593 int len;
2594
2595 len = sprintf(trash, "%s\r\n", rule_set->rsp_add[cur_idx]);
2596 len = buffer_replace2(rep, rep->data + txn->rsp.eoh,
2597 rep->data + txn->rsp.eoh, trash, len);
2598 txn->rsp.eoh += len;
2599
2600 if (unlikely(hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0))
2601 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002602 }
2603
Willy Tarreaua15645d2007-03-18 16:22:39 +01002604 /* check whether we're already working on the frontend */
2605 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002606 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002607 cur_proxy = t->fe;
2608 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002609
Willy Tarreaua15645d2007-03-18 16:22:39 +01002610 /*
2611 * 4: check for server cookie.
2612 */
2613 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002614
Willy Tarreaua15645d2007-03-18 16:22:39 +01002615 /*
2616 * 5: add server cookie in the response if needed
2617 */
2618 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_INS) &&
2619 (!(t->be->beprm->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2620 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002621
Willy Tarreaua15645d2007-03-18 16:22:39 +01002622 /* the server is known, it's not the one the client requested, we have to
2623 * insert a set-cookie here, except if we want to insert only on POST
2624 * requests and this one isn't. Note that servers which don't have cookies
2625 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002626 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002627 len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n",
2628 t->be->beprm->cookie_name,
2629 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002630
Willy Tarreaua15645d2007-03-18 16:22:39 +01002631 len = buffer_replace2(rep, rep->data + txn->rsp.eoh, rep->data + txn->rsp.eoh, trash, len);
2632 txn->rsp.eoh += len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002633
Willy Tarreaua15645d2007-03-18 16:22:39 +01002634 if (hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)
2635 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002636
Willy Tarreaua15645d2007-03-18 16:22:39 +01002637 t->flags |= SN_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002638
Willy Tarreaua15645d2007-03-18 16:22:39 +01002639 /* Here, we will tell an eventual cache on the client side that we don't
2640 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2641 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2642 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2643 */
2644 if (t->be->beprm->options & PR_O_COOK_NOC) {
2645 //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n");
2646 len = sprintf(trash, "Cache-control: private\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002647
Willy Tarreaua15645d2007-03-18 16:22:39 +01002648 len = buffer_replace2(rep, rep->data + txn->rsp.eoh,
2649 rep->data + txn->rsp.eoh, trash, len);
2650 txn->rsp.eoh += len;
2651 if (hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)
2652 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002653 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002654 }
2655
2656
2657 /*
2658 * 6: check for cache-control or pragma headers.
2659 */
2660 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002661
Willy Tarreaubaaee002006-06-26 02:48:02 +02002662
Willy Tarreaua15645d2007-03-18 16:22:39 +01002663 /*
2664 * 7: check if result will be cacheable with a cookie.
2665 * We'll block the response if security checks have caught
2666 * nasty things such as a cacheable cookie.
2667 */
2668 if (((t->flags & (SN_CACHEABLE | SN_CACHE_COOK | SN_SCK_ANY)) ==
2669 (SN_CACHEABLE | SN_CACHE_COOK | SN_SCK_ANY)) &&
2670 (t->be->beprm->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002671
Willy Tarreaua15645d2007-03-18 16:22:39 +01002672 /* we're in presence of a cacheable response containing
2673 * a set-cookie header. We'll block it as requested by
2674 * the 'checkcache' option, and send an alert.
2675 */
2676 if (t->srv) {
2677 t->srv->cur_sess--;
2678 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002679 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002680 t->be->beprm->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002681
Willy Tarreaua15645d2007-03-18 16:22:39 +01002682 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2683 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2684 send_log(t->be, LOG_ALERT,
2685 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2686 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2687 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002688 }
2689
Willy Tarreaua15645d2007-03-18 16:22:39 +01002690 /*
2691 * 8: add "Connection: close" if needed and not yet set.
2692 */
2693 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
2694 !(t->flags & SN_CONN_CLOSED)) {
2695 int len;
2696 len = buffer_replace2(rep, rep->data + txn->rsp.eoh,
2697 rep->data + txn->rsp.eoh, "Connection: close\r\n", 19);
2698 txn->rsp.eoh += len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002699
Willy Tarreaua15645d2007-03-18 16:22:39 +01002700 if (hdr_idx_add(17, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)
2701 goto return_bad_resp;
2702 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002703 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002704
Willy Tarreaubaaee002006-06-26 02:48:02 +02002705
Willy Tarreaua15645d2007-03-18 16:22:39 +01002706 /*************************************************************
2707 * OK, that's finished for the headers. We have done what we *
2708 * could. Let's switch to the DATA state. *
2709 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002710
Willy Tarreaua15645d2007-03-18 16:22:39 +01002711 t->srv_state = SV_STDATA;
2712 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
2713 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
2714
2715 /* client connection already closed or option 'forceclose' required :
2716 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002717 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002718 if ((req->l == 0) &&
2719 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->beprm->options & PR_O_FORCE_CLO)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002720 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002721 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002722
2723 /* We must ensure that the read part is still alive when switching
2724 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002725 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002726 if (t->be->beprm->srvtimeout)
2727 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002728
Willy Tarreaua15645d2007-03-18 16:22:39 +01002729 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002730 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002731 }
2732
Willy Tarreaua15645d2007-03-18 16:22:39 +01002733#ifdef CONFIG_HAP_TCPSPLICE
2734 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2735 /* TCP splicing supported by both FE and BE */
2736 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002737 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002738#endif
2739 /* if the user wants to log as soon as possible, without counting
2740 bytes from the server, then this is the right moment. */
2741 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2742 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002743 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002744 sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002745 }
2746
Willy Tarreaua15645d2007-03-18 16:22:39 +01002747 /* Note: we must not try to cheat by jumping directly to DATA,
2748 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002749 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002750
2751 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002752 }
2753 else if (s == SV_STDATA) {
2754 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002755 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002756 tv_eternity(&rep->rex);
2757 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002758 fd_delete(t->srv_fd);
2759 if (t->srv) {
2760 t->srv->cur_sess--;
2761 t->srv->failed_resp++;
2762 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002763 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002764 t->srv_state = SV_STCLOSE;
2765 if (!(t->flags & SN_ERR_MASK))
2766 t->flags |= SN_ERR_SRVCL;
2767 if (!(t->flags & SN_FINST_MASK))
2768 t->flags |= SN_FINST_D;
2769 /* We used to have a free connection slot. Since we'll never use it,
2770 * we have to inform the server that it may be used by another session.
2771 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002772 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002773 task_wakeup(&rq, t->srv->queue_mgt);
2774
2775 return 1;
2776 }
2777 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002778 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002779 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002780 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002781 shutdown(t->srv_fd, SHUT_RD);
2782 t->srv_state = SV_STSHUTR;
2783 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2784 return 1;
2785 }
2786 /* end of client read and no more data to send */
2787 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002788 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002789 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002790 shutdown(t->srv_fd, SHUT_WR);
2791 /* We must ensure that the read part is still alive when switching
2792 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002793 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002794 if (t->be->beprm->srvtimeout)
2795 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002796
2797 t->srv_state = SV_STSHUTW;
2798 return 1;
2799 }
2800 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002801 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002802 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002803 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002804 shutdown(t->srv_fd, SHUT_RD);
2805 t->srv_state = SV_STSHUTR;
2806 if (!(t->flags & SN_ERR_MASK))
2807 t->flags |= SN_ERR_SRVTO;
2808 if (!(t->flags & SN_FINST_MASK))
2809 t->flags |= SN_FINST_D;
2810 return 1;
2811 }
2812 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002813 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002814 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002815 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002816 shutdown(t->srv_fd, SHUT_WR);
2817 /* We must ensure that the read part is still alive when switching
2818 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002819 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002820 if (t->be->beprm->srvtimeout)
2821 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002822 t->srv_state = SV_STSHUTW;
2823 if (!(t->flags & SN_ERR_MASK))
2824 t->flags |= SN_ERR_SRVTO;
2825 if (!(t->flags & SN_FINST_MASK))
2826 t->flags |= SN_FINST_D;
2827 return 1;
2828 }
2829
2830 /* recompute request time-outs */
2831 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002832 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2833 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002834 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002835 }
2836 }
2837 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau2a429502006-10-15 14:52:29 +02002838 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2839 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002840 if (t->be->beprm->srvtimeout) {
2841 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002842 /* FIXME: to prevent the server from expiring read timeouts during writes,
2843 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002844 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002845 }
2846 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002847 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002848 }
2849 }
2850
2851 /* recompute response time-outs */
2852 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002853 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2854 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002855 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002856 }
2857 }
2858 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02002859 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2860 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002861 if (t->be->beprm->srvtimeout)
2862 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002863 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002864 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002865 }
2866 }
2867
2868 return 0; /* other cases change nothing */
2869 }
2870 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002871 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002872 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002873 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002874 fd_delete(t->srv_fd);
2875 if (t->srv) {
2876 t->srv->cur_sess--;
2877 t->srv->failed_resp++;
2878 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002879 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002880 //close(t->srv_fd);
2881 t->srv_state = SV_STCLOSE;
2882 if (!(t->flags & SN_ERR_MASK))
2883 t->flags |= SN_ERR_SRVCL;
2884 if (!(t->flags & SN_FINST_MASK))
2885 t->flags |= SN_FINST_D;
2886 /* We used to have a free connection slot. Since we'll never use it,
2887 * we have to inform the server that it may be used by another session.
2888 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002889 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002890 task_wakeup(&rq, t->srv->queue_mgt);
2891
2892 return 1;
2893 }
2894 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002895 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002896 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002897 fd_delete(t->srv_fd);
2898 if (t->srv)
2899 t->srv->cur_sess--;
2900 //close(t->srv_fd);
2901 t->srv_state = SV_STCLOSE;
2902 /* We used to have a free connection slot. Since we'll never use it,
2903 * we have to inform the server that it may be used by another session.
2904 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002905 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002906 task_wakeup(&rq, t->srv->queue_mgt);
2907
2908 return 1;
2909 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002910 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002911 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002912 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002913 fd_delete(t->srv_fd);
2914 if (t->srv)
2915 t->srv->cur_sess--;
2916 //close(t->srv_fd);
2917 t->srv_state = SV_STCLOSE;
2918 if (!(t->flags & SN_ERR_MASK))
2919 t->flags |= SN_ERR_SRVTO;
2920 if (!(t->flags & SN_FINST_MASK))
2921 t->flags |= SN_FINST_D;
2922 /* We used to have a free connection slot. Since we'll never use it,
2923 * we have to inform the server that it may be used by another session.
2924 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002925 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002926 task_wakeup(&rq, t->srv->queue_mgt);
2927
2928 return 1;
2929 }
2930 else if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002931 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2932 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002933 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002934 }
2935 }
2936 else { /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002937 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2938 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002939 if (t->be->beprm->srvtimeout) {
2940 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002941 /* FIXME: to prevent the server from expiring read timeouts during writes,
2942 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002943 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002944 }
2945 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002946 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002947 }
2948 }
2949 return 0;
2950 }
2951 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002952 if (rep->flags & BF_READ_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002953 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002954 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002955 fd_delete(t->srv_fd);
2956 if (t->srv) {
2957 t->srv->cur_sess--;
2958 t->srv->failed_resp++;
2959 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002960 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002961 //close(t->srv_fd);
2962 t->srv_state = SV_STCLOSE;
2963 if (!(t->flags & SN_ERR_MASK))
2964 t->flags |= SN_ERR_SRVCL;
2965 if (!(t->flags & SN_FINST_MASK))
2966 t->flags |= SN_FINST_D;
2967 /* We used to have a free connection slot. Since we'll never use it,
2968 * we have to inform the server that it may be used by another session.
2969 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002970 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002971 task_wakeup(&rq, t->srv->queue_mgt);
2972
2973 return 1;
2974 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002975 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002976 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002977 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002978 fd_delete(t->srv_fd);
2979 if (t->srv)
2980 t->srv->cur_sess--;
2981 //close(t->srv_fd);
2982 t->srv_state = SV_STCLOSE;
2983 /* We used to have a free connection slot. Since we'll never use it,
2984 * we have to inform the server that it may be used by another session.
2985 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002986 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002987 task_wakeup(&rq, t->srv->queue_mgt);
2988
2989 return 1;
2990 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002991 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002992 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002993 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002994 fd_delete(t->srv_fd);
2995 if (t->srv)
2996 t->srv->cur_sess--;
2997 //close(t->srv_fd);
2998 t->srv_state = SV_STCLOSE;
2999 if (!(t->flags & SN_ERR_MASK))
3000 t->flags |= SN_ERR_SRVTO;
3001 if (!(t->flags & SN_FINST_MASK))
3002 t->flags |= SN_FINST_D;
3003 /* We used to have a free connection slot. Since we'll never use it,
3004 * we have to inform the server that it may be used by another session.
3005 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003006 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003007 task_wakeup(&rq, t->srv->queue_mgt);
3008
3009 return 1;
3010 }
3011 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02003012 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3013 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003014 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003015 }
3016 }
3017 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02003018 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3019 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01003020 if (t->be->beprm->srvtimeout)
3021 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003022 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003023 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003024 }
3025 }
3026 return 0;
3027 }
3028 else { /* SV_STCLOSE : nothing to do */
3029 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3030 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003031 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
3032 t->uniq_id, t->be->beprm->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003033 write(1, trash, len);
3034 }
3035 return 0;
3036 }
3037 return 0;
3038}
3039
3040
3041/*
3042 * Produces data for the session <s> depending on its source. Expects to be
3043 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3044 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3045 * session, which it uses to keep on being called when there is free space in
3046 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3047 * if it changes the session state from CL_STSHUTR, otherwise 0.
3048 */
3049int produce_content(struct session *s)
3050{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003051 if (s->data_source == DATA_SRC_NONE) {
3052 s->flags &= ~SN_SELF_GEN;
3053 return 1;
3054 }
3055 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003056 /* dump server statistics */
3057 return produce_content_stats(s);
3058 }
3059 else {
3060 /* unknown data source */
3061 s->logs.status = 500;
3062 client_retnclose(s, error_message(s, HTTP_ERR_500));
3063 if (!(s->flags & SN_ERR_MASK))
3064 s->flags |= SN_ERR_PRXCOND;
3065 if (!(s->flags & SN_FINST_MASK))
3066 s->flags |= SN_FINST_R;
3067 s->flags &= ~SN_SELF_GEN;
3068 return 1;
3069 }
3070}
3071
3072
3073/*
3074 * Produces statistics data for the session <s>. Expects to be called with
3075 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3076 * flag from the session, which it uses to keep on being called when there is
3077 * free space in the buffer, of simply by letting an empty buffer upon return.
3078 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3079 */
3080int produce_content_stats(struct session *s)
3081{
3082 struct buffer *rep = s->rep;
3083 struct proxy *px;
3084 struct chunk msg;
3085 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003086
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003087 msg.len = 0;
3088 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003089
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003090 switch (s->data_state) {
3091 case DATA_ST_INIT:
3092 /* the function had not been called yet */
3093 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003094
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003095 chunk_printf(&msg, sizeof(trash),
3096 "HTTP/1.0 200 OK\r\n"
3097 "Cache-Control: no-cache\r\n"
3098 "Connection: close\r\n"
3099 "Content-Type: text/html\r\n"
3100 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003101
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003102 s->logs.status = 200;
3103 client_retnclose(s, &msg); // send the start of the response.
3104 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003105
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003106 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3107 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3108 if (!(s->flags & SN_FINST_MASK))
3109 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003110
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003111 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003112 /* that's all we return in case of HEAD request */
3113 s->data_state = DATA_ST_FIN;
3114 s->flags &= ~SN_SELF_GEN;
3115 return 1;
3116 }
3117
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003118 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3119 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003120
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003121 case DATA_ST_HEAD:
3122 /* WARNING! This must fit in the first buffer !!! */
3123 chunk_printf(&msg, sizeof(trash),
3124 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3125 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3126 "<style type=\"text/css\"><!--\n"
3127 "body {"
3128 " font-family: helvetica, arial;"
3129 " font-size: 12px;"
3130 " font-weight: normal;"
3131 " color: black;"
3132 " background: white;"
3133 "}\n"
3134 "th,td {"
3135 " font-size: 0.8em;"
3136 " align: center;"
3137 "}"
3138 "h1 {"
3139 " font-size: xx-large;"
3140 " margin-bottom: 0.5em;"
3141 "}\n"
3142 "h2 {"
3143 " font-family: helvetica, arial;"
3144 " font-size: x-large;"
3145 " font-weight: bold;"
3146 " font-style: italic;"
3147 " color: #6020a0;"
3148 " margin-top: 0em;"
3149 " margin-bottom: 0em;"
3150 "}\n"
3151 "h3 {"
3152 " font-family: helvetica, arial;"
3153 " font-size: 16px;"
3154 " font-weight: bold;"
3155 " color: #b00040;"
3156 " background: #e8e8d0;"
3157 " margin-top: 0em;"
3158 " margin-bottom: 0em;"
3159 "}\n"
3160 "li {"
3161 " margin-top: 0.25em;"
3162 " margin-right: 2em;"
3163 "}\n"
3164 ".hr {margin-top: 0.25em;"
3165 " border-color: black;"
3166 " border-bottom-style: solid;"
3167 "}\n"
3168 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3169 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3170 ".total {background: #20D0D0;color: #ffff80;}\n"
3171 ".frontend {background: #e8e8d0;}\n"
3172 ".backend {background: #e8e8d0;}\n"
3173 ".active0 {background: #ff9090;}\n"
3174 ".active1 {background: #ffd020;}\n"
3175 ".active2 {background: #ffffa0;}\n"
3176 ".active3 {background: #c0ffc0;}\n"
3177 ".active4 {background: #e0e0e0;}\n"
3178 ".backup0 {background: #ff9090;}\n"
3179 ".backup1 {background: #ff80ff;}\n"
3180 ".backup2 {background: #c060ff;}\n"
3181 ".backup3 {background: #b0d0ff;}\n"
3182 ".backup4 {background: #e0e0e0;}\n"
3183 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003184 "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 +01003185 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3186 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3187 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3188 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3189 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3190 "-->"
3191 "</style></head>");
3192
3193 if (buffer_write_chunk(rep, &msg) != 0)
3194 return 0;
3195
3196 s->data_state = DATA_ST_INFO;
3197 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003198
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003199 case DATA_ST_INFO:
3200 up = (now.tv_sec - start_date.tv_sec);
3201
3202 /* WARNING! this has to fit the first packet too.
3203 * We are around 3.5 kB, add adding entries will
3204 * become tricky if we want to support 4kB buffers !
3205 */
3206 chunk_printf(&msg, sizeof(trash),
3207 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3208 PRODUCT_NAME "</a></h1>\n"
3209 "<h2>Statistics Report for pid %d</h2>\n"
3210 "<hr width=\"100%%\" class=\"hr\">\n"
3211 "<h3>&gt; General process information</h3>\n"
3212 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3213 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3214 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3215 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3216 "<b>maxsock = </b> %d<br>\n"
3217 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3218 "</td><td align=\"center\" nowrap>\n"
3219 "<table class=\"lgd\"><tr>"
3220 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3221 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3222 "</tr><tr>"
3223 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3224 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3225 "</tr><tr>"
3226 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3227 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3228 "</tr><tr>"
3229 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3230 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3231 "</tr></table>\n"
3232 "</td>"
3233 "<td align=\"left\" nowrap width=\"1%%\">"
3234 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">"
3235 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>"
3236 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>"
3237 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>"
3238 "</ul>"
3239 "</td>"
3240 "</tr></table>\n"
3241 "",
3242 pid, pid, global.nbproc,
3243 up / 86400, (up % 86400) / 3600,
3244 (up % 3600) / 60, (up % 60),
3245 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3246 global.rlimit_memmax ? " MB" : "",
3247 global.rlimit_nofile,
3248 global.maxsock,
3249 global.maxconn,
3250 actconn
3251 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003252
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003253 if (buffer_write_chunk(rep, &msg) != 0)
3254 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003255
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003256 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003257
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003258 s->data_ctx.stats.px = proxy;
3259 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3260 s->data_state = DATA_ST_LIST;
3261 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003262
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003263 case DATA_ST_LIST:
3264 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003265 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003266 px = s->data_ctx.stats.px;
3267 /* skip the disabled proxies and non-networked ones */
3268 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3269 if (produce_content_stats_proxy(s, px) == 0)
3270 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003271
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003272 s->data_ctx.stats.px = px->next;
3273 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3274 }
3275 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003276
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003277 s->data_state = DATA_ST_END;
3278 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003279
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003280 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003281 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003282 if (buffer_write_chunk(rep, &msg) != 0)
3283 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003284
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003285 s->data_state = DATA_ST_FIN;
3286 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003287
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003288 case DATA_ST_FIN:
3289 s->flags &= ~SN_SELF_GEN;
3290 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003291
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003292 default:
3293 /* unknown state ! */
3294 s->logs.status = 500;
3295 client_retnclose(s, error_message(s, HTTP_ERR_500));
3296 if (!(s->flags & SN_ERR_MASK))
3297 s->flags |= SN_ERR_PRXCOND;
3298 if (!(s->flags & SN_FINST_MASK))
3299 s->flags |= SN_FINST_R;
3300 s->flags &= ~SN_SELF_GEN;
3301 return 1;
3302 }
3303}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003304
Willy Tarreaubaaee002006-06-26 02:48:02 +02003305
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003306/*
3307 * Dumps statistics for a proxy.
3308 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3309 * ot non-zero if everything completed.
3310 */
3311int produce_content_stats_proxy(struct session *s, struct proxy *px)
3312{
3313 struct buffer *rep = s->rep;
3314 struct server *sv;
3315 struct chunk msg;
3316
3317 msg.len = 0;
3318 msg.str = trash;
3319
3320 switch (s->data_ctx.stats.px_st) {
3321 case DATA_ST_PX_INIT:
3322 /* we are on a new proxy */
3323
3324 if (s->be->fiprm->uri_auth && s->be->fiprm->uri_auth->scope) {
3325 /* we have a limited scope, we have to check the proxy name */
3326 struct stat_scope *scope;
3327 int len;
3328
3329 len = strlen(px->id);
3330 scope = s->be->fiprm->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003331
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003332 while (scope) {
3333 /* match exact proxy name */
3334 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3335 break;
3336
3337 /* match '.' which means 'self' proxy */
3338 if (!strcmp(scope->px_id, ".") && px == s->fe)
3339 break;
3340 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003341 }
3342
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003343 /* proxy name not found : don't dump anything */
3344 if (scope == NULL)
3345 return 1;
3346 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003347
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003348 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3349 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003350
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003351 case DATA_ST_PX_TH:
3352 /* print a new table */
3353 chunk_printf(&msg, sizeof(trash),
3354 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3355 "<tr align=\"center\" class=\"titre\">"
3356 "<th colspan=2 class=\"pxname\">%s</th>"
3357 "<th colspan=18 class=\"empty\"></th>"
3358 "</tr>\n"
3359 "<tr align=\"center\" class=\"titre\">"
3360 "<th rowspan=2></th>"
3361 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3362 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3363 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3364 "</tr>\n"
3365 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003366 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3367 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003368 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3369 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3370 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3371 "",
3372 px->id);
3373
3374 if (buffer_write_chunk(rep, &msg) != 0)
3375 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003376
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003377 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3378 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003379
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003380 case DATA_ST_PX_FE:
3381 /* print the frontend */
3382 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003383 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003384 /* name, queue */
3385 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3386 /* sessions : current, max, limit, cumul. */
3387 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3388 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003389 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003390 /* denied: req, resp */
3391 "<td align=right>%d</td><td align=right>%d</td>"
3392 /* errors : request, connect, response */
3393 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3394 /* server status : reflect backend status */
3395 "<td align=center>%s</td>"
3396 /* rest of server: nothing */
3397 "<td align=center colspan=5></td></tr>"
3398 "",
3399 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003400 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003401 px->denied_req, px->denied_resp,
3402 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003403 px->state == PR_STRUN ? "OPEN" :
3404 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003405
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003406 if (buffer_write_chunk(rep, &msg) != 0)
3407 return 0;
3408 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003409
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003410 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3411 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3412 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003413
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003414 case DATA_ST_PX_SV:
3415 /* stats.sv has been initialized above */
3416 while (s->data_ctx.stats.sv != NULL) {
3417 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3418 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003419
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003420 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003421
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003422 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3423 if (!(sv->state & SRV_CHECKED))
3424 sv_state = 4;
3425 else if (sv->state & SRV_RUNNING)
3426 if (sv->health == sv->rise + sv->fall - 1)
3427 sv_state = 3; /* UP */
3428 else
3429 sv_state = 2; /* going down */
3430 else
3431 if (sv->health)
3432 sv_state = 1; /* going up */
3433 else
3434 sv_state = 0; /* DOWN */
3435
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003436 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003437 /* name */
3438 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3439 /* queue : current, max */
3440 "<td align=right>%d</td><td align=right>%d</td>"
3441 /* sessions : current, max, limit, cumul */
3442 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3443 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003444 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003445 /* denied: req, resp */
3446 "<td align=right></td><td align=right>%d</td>"
3447 /* errors : request, connect, response */
3448 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3449 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003450 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003451 sv_state, sv->id,
3452 sv->nbpend, sv->nbpend_max,
3453 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003454 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003455 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003456 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003457
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003458 /* status */
3459 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3460 chunk_printf(&msg, sizeof(trash),
3461 srv_hlt_st[sv_state],
3462 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3463 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3464
Willy Tarreau128e9542007-01-01 22:01:43 +01003465 chunk_printf(&msg, sizeof(trash),
3466 /* weight */
3467 "</td><td>%d</td>"
3468 /* act, bck */
3469 "<td>%s</td><td>%s</td>"
3470 "",
3471 sv->uweight+1,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003472 (sv->state & SRV_BACKUP) ? "-" : "Y",
3473 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003474
3475 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003476 if (sv->state & SRV_CHECKED)
3477 chunk_printf(&msg, sizeof(trash),
3478 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3479 sv->failed_checks, sv->down_trans);
3480 else
3481 chunk_printf(&msg, sizeof(trash),
3482 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003483
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003484 if (buffer_write_chunk(rep, &msg) != 0)
3485 return 0;
3486
3487 s->data_ctx.stats.sv = sv->next;
3488 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003489
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003490 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3491 /* fall through */
3492
3493 case DATA_ST_PX_BE:
3494 /* print the backend */
3495 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003496 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003497 /* name */
3498 "<tr align=center class=\"backend\"><td>Backend</td>"
3499 /* queue : current, max */
3500 "<td align=right>%d</td><td align=right>%d</td>"
3501 /* sessions : current, max, limit, cumul. */
3502 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3503 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003504 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003505 /* denied: req, resp */
3506 "<td align=right>%d</td><td align=right>%d</td>"
3507 /* errors : request, connect, response */
3508 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3509 /* server status : reflect backend status (up/down) : we display UP
3510 * if the backend has known working servers or if it has no server at
3511 * all (eg: for stats). Tthen we display the total weight, number of
3512 * active and backups. */
3513 "<td align=center>%s</td><td align=center>%d</td>"
3514 "<td align=center>%d</td><td align=center>%d</td>"
3515 /* rest of server: nothing */
3516 "<td align=center colspan=2></td></tr>"
3517 "",
3518 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3519 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003520 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003521 px->denied_req, px->denied_resp,
3522 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003523 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3524 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003525
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003526 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003527 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003528 }
3529
3530 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3531 /* fall through */
3532
3533 case DATA_ST_PX_END:
3534 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3535
3536 if (buffer_write_chunk(rep, &msg) != 0)
3537 return 0;
3538
3539 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3540 /* fall through */
3541
3542 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003543 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003544
3545 default:
3546 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003547 return 1;
3548 }
3549}
3550
3551
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003552/* Iterate the same filter through all request headers.
3553 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003554 * Since it can manage the switch to another backend, it updates the per-proxy
3555 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003556 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003557int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003558{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003559 char term;
3560 char *cur_ptr, *cur_end, *cur_next;
3561 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003562 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003563 struct hdr_idx_elem *cur_hdr;
3564 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003566 last_hdr = 0;
3567
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003568 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003569 old_idx = 0;
3570
3571 while (!last_hdr) {
3572 if (unlikely(t->flags & (SN_CLDENY | SN_CLTARPIT)))
3573 return 1;
3574 else if (unlikely(t->flags & SN_CLALLOW) &&
3575 (exp->action == ACT_ALLOW ||
3576 exp->action == ACT_DENY ||
3577 exp->action == ACT_TARPIT))
3578 return 0;
3579
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003580 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003581 if (!cur_idx)
3582 break;
3583
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003584 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003585 cur_ptr = cur_next;
3586 cur_end = cur_ptr + cur_hdr->len;
3587 cur_next = cur_end + cur_hdr->cr + 1;
3588
3589 /* Now we have one header between cur_ptr and cur_end,
3590 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003591 */
3592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003593 /* The annoying part is that pattern matching needs
3594 * that we modify the contents to null-terminate all
3595 * strings before testing them.
3596 */
3597
3598 term = *cur_end;
3599 *cur_end = '\0';
3600
3601 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3602 switch (exp->action) {
3603 case ACT_SETBE:
3604 /* It is not possible to jump a second time.
3605 * FIXME: should we return an HTTP/500 here so that
3606 * the admin knows there's a problem ?
3607 */
3608 if (t->be != t->fe)
3609 break;
3610
3611 /* Swithing Proxy */
3612 t->be = (struct proxy *) exp->replace;
3613
3614 /* right now, the backend switch is not too much complicated
3615 * because we have associated req_cap and rsp_cap to the
3616 * frontend, and the beconn will be updated later.
3617 */
3618
3619 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3620 t->req->cto = t->be->beprm->contimeout;
3621 last_hdr = 1;
3622 break;
3623
3624 case ACT_ALLOW:
3625 t->flags |= SN_CLALLOW;
3626 last_hdr = 1;
3627 break;
3628
3629 case ACT_DENY:
3630 t->flags |= SN_CLDENY;
3631 last_hdr = 1;
3632 t->be->beprm->denied_req++;
3633 break;
3634
3635 case ACT_TARPIT:
3636 t->flags |= SN_CLTARPIT;
3637 last_hdr = 1;
3638 t->be->beprm->denied_req++;
3639 break;
3640
3641 case ACT_REPLACE:
3642 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3643 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3644 /* FIXME: if the user adds a newline in the replacement, the
3645 * index will not be recalculated for now, and the new line
3646 * will not be counted as a new header.
3647 */
3648
3649 cur_end += delta;
3650 cur_next += delta;
3651 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003652 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003653 break;
3654
3655 case ACT_REMOVE:
3656 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3657 cur_next += delta;
3658
3659 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003660 txn->req.eoh += delta;
3661 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3662 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003663 cur_hdr->len = 0;
3664 cur_end = NULL; /* null-term has been rewritten */
3665 break;
3666
3667 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003668 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003669 if (cur_end)
3670 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003672 /* keep the link from this header to next one in case of later
3673 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003674 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003675 old_idx = cur_idx;
3676 }
3677 return 0;
3678}
3679
3680
3681/* Apply the filter to the request line.
3682 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3683 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003684 * Since it can manage the switch to another backend, it updates the per-proxy
3685 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003686 */
3687int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3688{
3689 char term;
3690 char *cur_ptr, *cur_end;
3691 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003692 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003693 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003694
Willy Tarreau58f10d72006-12-04 02:26:12 +01003695
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003696 if (unlikely(t->flags & (SN_CLDENY | SN_CLTARPIT)))
3697 return 1;
3698 else if (unlikely(t->flags & SN_CLALLOW) &&
3699 (exp->action == ACT_ALLOW ||
3700 exp->action == ACT_DENY ||
3701 exp->action == ACT_TARPIT))
3702 return 0;
3703 else if (exp->action == ACT_REMOVE)
3704 return 0;
3705
3706 done = 0;
3707
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003708 cur_ptr = req->data + txn->req.som;
3709 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003710
3711 /* Now we have the request line between cur_ptr and cur_end */
3712
3713 /* The annoying part is that pattern matching needs
3714 * that we modify the contents to null-terminate all
3715 * strings before testing them.
3716 */
3717
3718 term = *cur_end;
3719 *cur_end = '\0';
3720
3721 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3722 switch (exp->action) {
3723 case ACT_SETBE:
3724 /* It is not possible to jump a second time.
3725 * FIXME: should we return an HTTP/500 here so that
3726 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003727 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003728 if (t->be != t->fe)
3729 break;
3730
3731 /* Swithing Proxy */
3732 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003734 /* right now, the backend switch is not too much complicated
3735 * because we have associated req_cap and rsp_cap to the
3736 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003737 */
3738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003739 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3740 t->req->cto = t->be->beprm->contimeout;
3741 done = 1;
3742 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003743
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003744 case ACT_ALLOW:
3745 t->flags |= SN_CLALLOW;
3746 done = 1;
3747 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003748
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003749 case ACT_DENY:
3750 t->flags |= SN_CLDENY;
3751 t->be->beprm->denied_req++;
3752 done = 1;
3753 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003754
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003755 case ACT_TARPIT:
3756 t->flags |= SN_CLTARPIT;
3757 t->be->beprm->denied_req++;
3758 done = 1;
3759 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003761 case ACT_REPLACE:
3762 *cur_end = term; /* restore the string terminator */
3763 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3764 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3765 /* FIXME: if the user adds a newline in the replacement, the
3766 * index will not be recalculated for now, and the new line
3767 * will not be counted as a new header.
3768 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003769
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003770 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003771 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003772
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003773 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003774 HTTP_MSG_RQMETH,
3775 cur_ptr, cur_end + 1,
3776 NULL, NULL);
3777 if (unlikely(!cur_end))
3778 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003779
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003780 /* we have a full request and we know that we have either a CR
3781 * or an LF at <ptr>.
3782 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003783 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3784 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003785 /* there is no point trying this regex on headers */
3786 return 1;
3787 }
3788 }
3789 *cur_end = term; /* restore the string terminator */
3790 return done;
3791}
Willy Tarreau97de6242006-12-27 17:18:38 +01003792
Willy Tarreau58f10d72006-12-04 02:26:12 +01003793
Willy Tarreau58f10d72006-12-04 02:26:12 +01003794
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003795/*
3796 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3797 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003798 * unparsable request. Since it can manage the switch to another backend, it
3799 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003800 */
3801int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3802{
3803 /* iterate through the filters in the outer loop */
3804 while (exp && !(t->flags & (SN_CLDENY|SN_CLTARPIT))) {
3805 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003806
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003807 /*
3808 * The interleaving of transformations and verdicts
3809 * makes it difficult to decide to continue or stop
3810 * the evaluation.
3811 */
3812
3813 if ((t->flags & SN_CLALLOW) &&
3814 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3815 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3816 exp = exp->next;
3817 continue;
3818 }
3819
3820 /* Apply the filter to the request line. */
3821 ret = apply_filter_to_req_line(t, req, exp);
3822 if (unlikely(ret < 0))
3823 return -1;
3824
3825 if (likely(ret == 0)) {
3826 /* The filter did not match the request, it can be
3827 * iterated through all headers.
3828 */
3829 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003830 }
3831 exp = exp->next;
3832 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003833 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003834}
3835
3836
Willy Tarreaua15645d2007-03-18 16:22:39 +01003837
Willy Tarreau58f10d72006-12-04 02:26:12 +01003838/*
3839 * Manager client-side cookie
3840 */
3841void manage_client_side_cookies(struct session *t, struct buffer *req)
3842{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003843 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003844 char *p1, *p2, *p3, *p4;
3845 char *del_colon, *del_cookie, *colon;
3846 int app_cookies;
3847
3848 appsess *asession_temp = NULL;
3849 appsess local_asession;
3850
3851 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003852 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003853
Willy Tarreau830ff452006-12-17 19:31:23 +01003854 if (t->be->beprm->cookie_name == NULL &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003855 t->be->beprm->appsession_name == NULL &&
3856 t->be->fiprm->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003857 return;
3858
Willy Tarreau2a324282006-12-05 00:05:46 +01003859 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003860 * we start with the start line.
3861 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003862 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003863 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003864
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003865 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003866 struct hdr_idx_elem *cur_hdr;
3867
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003868 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003869 cur_ptr = cur_next;
3870 cur_end = cur_ptr + cur_hdr->len;
3871 cur_next = cur_end + cur_hdr->cr + 1;
3872
3873 /* We have one full header between cur_ptr and cur_end, and the
3874 * next header starts at cur_next. We're only interested in
3875 * "Cookie:" headers.
3876 */
3877
3878 if ((cur_end - cur_ptr <= 7) ||
3879 (strncasecmp(cur_ptr, "Cookie:", 7) != 0)) {
3880 old_idx = cur_idx;
3881 continue;
3882 }
3883
3884 /* Now look for cookies. Conforming to RFC2109, we have to support
3885 * attributes whose name begin with a '$', and associate them with
3886 * the right cookie, if we want to delete this cookie.
3887 * So there are 3 cases for each cookie read :
3888 * 1) it's a special attribute, beginning with a '$' : ignore it.
3889 * 2) it's a server id cookie that we *MAY* want to delete : save
3890 * some pointers on it (last semi-colon, beginning of cookie...)
3891 * 3) it's an application cookie : we *MAY* have to delete a previous
3892 * "special" cookie.
3893 * At the end of loop, if a "special" cookie remains, we may have to
3894 * remove it. If no application cookie persists in the header, we
3895 * *MUST* delete it
3896 */
3897
3898
3899 p1 = cur_ptr + 7; /* first char after 'Cookie:' */
3900 if (isspace((int)*p1)) /* try to get the first space with it */
3901 p1++;
3902
3903 colon = p1;
3904 /* del_cookie == NULL => nothing to be deleted */
3905 del_colon = del_cookie = NULL;
3906 app_cookies = 0;
3907
3908 while (p1 < cur_end) {
3909 /* skip spaces and colons, but keep an eye on these ones */
3910 while (p1 < cur_end) {
3911 if (*p1 == ';' || *p1 == ',')
3912 colon = p1;
3913 else if (!isspace((int)*p1))
3914 break;
3915 p1++;
3916 }
3917
3918 if (p1 == cur_end)
3919 break;
3920
3921 /* p1 is at the beginning of the cookie name */
3922 p2 = p1;
3923 while (p2 < cur_end && *p2 != '=')
3924 p2++;
3925
3926 if (p2 == cur_end)
3927 break;
3928
3929 p3 = p2 + 1; /* skips the '=' sign */
3930 if (p3 == cur_end)
3931 break;
3932
3933 p4 = p3;
3934 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
3935 p4++;
3936
3937 /* here, we have the cookie name between p1 and p2,
3938 * and its value between p3 and p4.
3939 * we can process it :
3940 *
3941 * Cookie: NAME=VALUE;
3942 * | || || |
3943 * | || || +--> p4
3944 * | || |+-------> p3
3945 * | || +--------> p2
3946 * | |+------------> p1
3947 * | +-------------> colon
3948 * +--------------------> cur_ptr
3949 */
3950
3951 if (*p1 == '$') {
3952 /* skip this one */
3953 }
3954 else {
3955 /* first, let's see if we want to capture it */
Willy Tarreau830ff452006-12-17 19:31:23 +01003956 if (t->fe->fiprm->capture_name != NULL &&
Willy Tarreau58f10d72006-12-04 02:26:12 +01003957 t->logs.cli_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01003958 (p4 - p1 >= t->fe->fiprm->capture_namelen) &&
3959 memcmp(p1, t->fe->fiprm->capture_name, t->fe->fiprm->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003960 int log_len = p4 - p1;
3961
3962 if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) {
3963 Alert("HTTP logging : out of memory.\n");
3964 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01003965 if (log_len > t->fe->fiprm->capture_len)
3966 log_len = t->fe->fiprm->capture_len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003967 memcpy(t->logs.cli_cookie, p1, log_len);
3968 t->logs.cli_cookie[log_len] = 0;
3969 }
3970 }
3971
Willy Tarreau830ff452006-12-17 19:31:23 +01003972 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
3973 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003974 /* Cool... it's the right one */
Willy Tarreau830ff452006-12-17 19:31:23 +01003975 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003976 char *delim;
3977
3978 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3979 * have the server ID betweek p3 and delim, and the original cookie between
3980 * delim+1 and p4. Otherwise, delim==p4 :
3981 *
3982 * Cookie: NAME=SRV~VALUE;
3983 * | || || | |
3984 * | || || | +--> p4
3985 * | || || +--------> delim
3986 * | || |+-----------> p3
3987 * | || +------------> p2
3988 * | |+----------------> p1
3989 * | +-----------------> colon
3990 * +------------------------> cur_ptr
3991 */
3992
Willy Tarreau830ff452006-12-17 19:31:23 +01003993 if (t->be->beprm->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003994 for (delim = p3; delim < p4; delim++)
3995 if (*delim == COOKIE_DELIM)
3996 break;
3997 }
3998 else
3999 delim = p4;
4000
4001
4002 /* Here, we'll look for the first running server which supports the cookie.
4003 * This allows to share a same cookie between several servers, for example
4004 * to dedicate backup servers to specific servers only.
4005 * However, to prevent clients from sticking to cookie-less backup server
4006 * when they have incidentely learned an empty cookie, we simply ignore
4007 * empty cookies and mark them as invalid.
4008 */
4009 if (delim == p3)
4010 srv = NULL;
4011
4012 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004013 if (srv->cookie && (srv->cklen == delim - p3) &&
4014 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004015 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004016 /* we found the server and it's usable */
4017 t->flags &= ~SN_CK_MASK;
4018 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
4019 t->srv = srv;
4020 break;
4021 } else {
4022 /* we found a server, but it's down */
4023 t->flags &= ~SN_CK_MASK;
4024 t->flags |= SN_CK_DOWN;
4025 }
4026 }
4027 srv = srv->next;
4028 }
4029
4030 if (!srv && !(t->flags & SN_CK_DOWN)) {
4031 /* no server matched this cookie */
4032 t->flags &= ~SN_CK_MASK;
4033 t->flags |= SN_CK_INVALID;
4034 }
4035
4036 /* depending on the cookie mode, we may have to either :
4037 * - delete the complete cookie if we're in insert+indirect mode, so that
4038 * the server never sees it ;
4039 * - remove the server id from the cookie value, and tag the cookie as an
4040 * application cookie so that it does not get accidentely removed later,
4041 * if we're in cookie prefix mode
4042 */
Willy Tarreau830ff452006-12-17 19:31:23 +01004043 if ((t->be->beprm->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004044 int delta; /* negative */
4045
4046 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4047 p4 += delta;
4048 cur_end += delta;
4049 cur_next += delta;
4050 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004051 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004052
4053 del_cookie = del_colon = NULL;
4054 app_cookies++; /* protect the header from deletion */
4055 }
4056 else if (del_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01004057 (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 +01004058 del_cookie = p1;
4059 del_colon = colon;
4060 }
4061 } else {
4062 /* now we know that we must keep this cookie since it's
4063 * not ours. But if we wanted to delete our cookie
4064 * earlier, we cannot remove the complete header, but we
4065 * can remove the previous block itself.
4066 */
4067 app_cookies++;
4068
4069 if (del_cookie != NULL) {
4070 int delta; /* negative */
4071
4072 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4073 p4 += delta;
4074 cur_end += delta;
4075 cur_next += delta;
4076 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004077 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004078 del_cookie = del_colon = NULL;
4079 }
4080 }
4081
Willy Tarreau830ff452006-12-17 19:31:23 +01004082 if ((t->be->beprm->appsession_name != NULL) &&
4083 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004084 /* first, let's see if the cookie is our appcookie*/
4085
4086 /* Cool... it's the right one */
4087
4088 asession_temp = &local_asession;
4089
4090 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4091 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4092 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4093 return;
4094 }
4095
Willy Tarreau830ff452006-12-17 19:31:23 +01004096 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4097 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004098 asession_temp->serverid = NULL;
4099
4100 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004101 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004102 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4103 /* free previously allocated memory */
4104 pool_free_to(apools.sessid, local_asession.sessid);
4105 Alert("Not enough memory process_cli():asession:calloc().\n");
4106 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4107 return;
4108 }
4109
4110 asession_temp->sessid = local_asession.sessid;
4111 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004112 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004113 } else {
4114 /* free previously allocated memory */
4115 pool_free_to(apools.sessid, local_asession.sessid);
4116 }
4117
4118 if (asession_temp->serverid == NULL) {
4119 Alert("Found Application Session without matching server.\n");
4120 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004121 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004122 while (srv) {
4123 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004124 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004125 /* we found the server and it's usable */
4126 t->flags &= ~SN_CK_MASK;
4127 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
4128 t->srv = srv;
4129 break;
4130 } else {
4131 t->flags &= ~SN_CK_MASK;
4132 t->flags |= SN_CK_DOWN;
4133 }
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) {
4197 if (unlikely(t->flags & SN_SVDENY))
4198 return 1;
4199 else if (unlikely(t->flags & SN_SVALLOW) &&
4200 (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:
4228 t->flags |= SN_SVALLOW;
4229 last_hdr = 1;
4230 break;
4231
4232 case ACT_DENY:
4233 t->flags |= SN_SVDENY;
4234 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
4290 if (unlikely(t->flags & SN_SVDENY))
4291 return 1;
4292 else if (unlikely(t->flags & SN_SVALLOW) &&
4293 (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:
4317 t->flags |= SN_SVALLOW;
4318 done = 1;
4319 break;
4320
4321 case ACT_DENY:
4322 t->flags |= SN_SVDENY;
4323 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 */
4348 t->logs.status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
4349 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{
4367 /* iterate through the filters in the outer loop */
4368 while (exp && !(t->flags & SN_SVDENY)) {
4369 int ret;
4370
4371 /*
4372 * The interleaving of transformations and verdicts
4373 * makes it difficult to decide to continue or stop
4374 * the evaluation.
4375 */
4376
4377 if ((t->flags & SN_SVALLOW) &&
4378 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4379 exp->action == ACT_PASS)) {
4380 exp = exp->next;
4381 continue;
4382 }
4383
4384 /* Apply the filter to the status line. */
4385 ret = apply_filter_to_sts_line(t, rtr, exp);
4386 if (unlikely(ret < 0))
4387 return -1;
4388
4389 if (likely(ret == 0)) {
4390 /* The filter did not match the response, it can be
4391 * iterated through all headers.
4392 */
4393 apply_filter_to_resp_headers(t, rtr, exp);
4394 }
4395 exp = exp->next;
4396 }
4397 return 0;
4398}
4399
4400
4401
4402/*
4403 * Manager server-side cookies
4404 */
4405void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4406{
4407 struct http_txn *txn = &t->txn;
4408 char *p1, *p2, *p3, *p4;
4409
4410 appsess *asession_temp = NULL;
4411 appsess local_asession;
4412
4413 char *cur_ptr, *cur_end, *cur_next;
4414 int cur_idx, old_idx, delta;
4415
4416 if (t->be->beprm->cookie_name == NULL &&
4417 t->be->beprm->appsession_name == NULL &&
4418 t->be->fiprm->capture_name == NULL &&
4419 !(t->be->beprm->options & PR_O_CHK_CACHE))
4420 return;
4421
4422 /* Iterate through the headers.
4423 * we start with the start line.
4424 */
4425 old_idx = 0;
4426 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4427
4428 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4429 struct hdr_idx_elem *cur_hdr;
4430
4431 cur_hdr = &txn->hdr_idx.v[cur_idx];
4432 cur_ptr = cur_next;
4433 cur_end = cur_ptr + cur_hdr->len;
4434 cur_next = cur_end + cur_hdr->cr + 1;
4435
4436 /* We have one full header between cur_ptr and cur_end, and the
4437 * next header starts at cur_next. We're only interested in
4438 * "Cookie:" headers.
4439 */
4440
4441 if ((cur_end - cur_ptr <= 11) ||
4442 (strncasecmp(cur_ptr, "Set-Cookie:", 11) != 0)) {
4443 old_idx = cur_idx;
4444 continue;
4445 }
4446
4447 /* OK, right now we know we have a set-cookie at cur_ptr */
4448 t->flags |= SN_SCK_ANY;
4449
4450
4451 /* maybe we only wanted to see if there was a set-cookie */
4452 if (t->be->beprm->cookie_name == NULL &&
4453 t->be->beprm->appsession_name == NULL &&
4454 t->be->fiprm->capture_name == NULL)
4455 return;
4456
4457 p1 = cur_ptr + 11; /* first char after 'Set-Cookie:' */
4458
4459 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
4460 while (p1 < cur_end && (isspace((int)*p1)))
4461 p1++;
4462
4463 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4464 break;
4465
4466 /* p1 is at the beginning of the cookie name */
4467 p2 = p1;
4468
4469 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4470 p2++;
4471
4472 if (p2 == cur_end || *p2 == ';') /* next cookie */
4473 break;
4474
4475 p3 = p2 + 1; /* skip the '=' sign */
4476 if (p3 == cur_end)
4477 break;
4478
4479 p4 = p3;
4480 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4481 p4++;
4482
4483 /* here, we have the cookie name between p1 and p2,
4484 * and its value between p3 and p4.
4485 * we can process it.
4486 */
4487
4488 /* first, let's see if we want to capture it */
4489 if (t->be->fiprm->capture_name != NULL &&
4490 t->logs.srv_cookie == NULL &&
4491 (p4 - p1 >= t->be->fiprm->capture_namelen) &&
4492 memcmp(p1, t->be->fiprm->capture_name, t->be->fiprm->capture_namelen) == 0) {
4493 int log_len = p4 - p1;
4494
4495 if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) {
4496 Alert("HTTP logging : out of memory.\n");
4497 }
4498
4499 if (log_len > t->be->fiprm->capture_len)
4500 log_len = t->be->fiprm->capture_len;
4501 memcpy(t->logs.srv_cookie, p1, log_len);
4502 t->logs.srv_cookie[log_len] = 0;
4503 }
4504
4505 /* now check if we need to process it for persistence */
4506 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
4507 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
4508 /* Cool... it's the right one */
4509 t->flags |= SN_SCK_SEEN;
4510
4511 /* If the cookie is in insert mode on a known server, we'll delete
4512 * this occurrence because we'll insert another one later.
4513 * We'll delete it too if the "indirect" option is set and we're in
4514 * a direct access. */
4515 if (((t->srv) && (t->be->beprm->options & PR_O_COOK_INS)) ||
4516 ((t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_IND))) {
4517 /* this header must be deleted */
4518 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4519 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4520 txn->hdr_idx.used--;
4521 cur_hdr->len = 0;
4522 cur_next += delta;
4523 txn->rsp.eoh += delta;
4524
4525 t->flags |= SN_SCK_DELETED;
4526 }
4527 else if ((t->srv) && (t->srv->cookie) &&
4528 (t->be->beprm->options & PR_O_COOK_RW)) {
4529 /* replace bytes p3->p4 with the cookie name associated
4530 * with this server since we know it.
4531 */
4532 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4533 cur_hdr->len += delta;
4534 cur_next += delta;
4535 txn->rsp.eoh += delta;
4536
4537 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
4538 }
4539 else if ((t->srv) && (t->srv->cookie) &&
4540 (t->be->beprm->options & PR_O_COOK_PFX)) {
4541 /* insert the cookie name associated with this server
4542 * before existing cookie, and insert a delimitor between them..
4543 */
4544 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4545 cur_hdr->len += delta;
4546 cur_next += delta;
4547 txn->rsp.eoh += delta;
4548
4549 p3[t->srv->cklen] = COOKIE_DELIM;
4550 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
4551 }
4552 }
4553 /* next, let's see if the cookie is our appcookie */
4554 else if ((t->be->beprm->appsession_name != NULL) &&
4555 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
4556
4557 /* Cool... it's the right one */
4558
4559 size_t server_id_len = strlen(t->srv->id) + 1;
4560 asession_temp = &local_asession;
4561
4562 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4563 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4564 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4565 return;
4566 }
4567 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4568 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
4569 asession_temp->serverid = NULL;
4570
4571 /* only do insert, if lookup fails */
4572 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
4573 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4574 Alert("Not enough Memory process_srv():asession:calloc().\n");
4575 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4576 return;
4577 }
4578 asession_temp->sessid = local_asession.sessid;
4579 asession_temp->serverid = local_asession.serverid;
4580 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
4581 }/* end if (chtbl_lookup()) */
4582 else {
4583 /* free wasted memory */
4584 pool_free_to(apools.sessid, local_asession.sessid);
4585 } /* end else from if (chtbl_lookup()) */
4586
4587 if (asession_temp->serverid == NULL) {
4588 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
4589 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4590 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4591 return;
4592 }
4593 asession_temp->serverid[0] = '\0';
4594 }
4595
4596 if (asession_temp->serverid[0] == '\0')
4597 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4598
4599 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
4600
4601#if defined(DEBUG_HASH)
4602 print_table(&(t->be->beprm->htbl_proxy));
4603#endif
4604 }/* end if ((t->proxy->appsession_name != NULL) ... */
4605 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4606 } /* we're now at the end of the cookie value */
4607
4608 /* keep the link from this header to next one */
4609 old_idx = cur_idx;
4610 } /* end of cookie processing on this header */
4611}
4612
4613
4614
4615/*
4616 * Check if response is cacheable or not. Updates t->flags.
4617 */
4618void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4619{
4620 struct http_txn *txn = &t->txn;
4621 char *p1, *p2;
4622
4623 char *cur_ptr, *cur_end, *cur_next;
4624 int cur_idx;
4625
4626 if (!t->flags & SN_CACHEABLE)
4627 return;
4628
4629 /* Iterate through the headers.
4630 * we start with the start line.
4631 */
4632 cur_idx = 0;
4633 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4634
4635 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4636 struct hdr_idx_elem *cur_hdr;
4637
4638 cur_hdr = &txn->hdr_idx.v[cur_idx];
4639 cur_ptr = cur_next;
4640 cur_end = cur_ptr + cur_hdr->len;
4641 cur_next = cur_end + cur_hdr->cr + 1;
4642
4643 /* We have one full header between cur_ptr and cur_end, and the
4644 * next header starts at cur_next. We're only interested in
4645 * "Cookie:" headers.
4646 */
4647
4648 if ((cur_end - cur_ptr >= 16) &&
4649 strncasecmp(cur_ptr, "Pragma: no-cache", 16) == 0) {
4650 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
4651 return;
4652 }
4653
4654 if ((cur_end - cur_ptr <= 14) ||
4655 (strncasecmp(cur_ptr, "Cache-control:", 14) != 0))
4656 continue;
4657
4658 /* OK, right now we know we have a cache-control header at cur_ptr */
4659
4660 p1 = cur_ptr + 14; /* first char after 'cache-control:' */
4661
4662 while (p1 < cur_end && (isspace((int)*p1)))
4663 p1++;
4664
4665 if (p1 >= cur_end) /* no more info */
4666 continue;
4667
4668 /* p1 is at the beginning of the value */
4669 p2 = p1;
4670
4671 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4672 p2++;
4673
4674 /* we have a complete value between p1 and p2 */
4675 if (p2 < cur_end && *p2 == '=') {
4676 /* we have something of the form no-cache="set-cookie" */
4677 if ((cur_end - p1 >= 21) &&
4678 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4679 && (p1[20] == '"' || p1[20] == ','))
4680 t->flags &= ~SN_CACHE_COOK;
4681 continue;
4682 }
4683
4684 /* OK, so we know that either p2 points to the end of string or to a comma */
4685 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4686 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4687 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4688 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
4689 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
4690 return;
4691 }
4692
4693 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
4694 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
4695 continue;
4696 }
4697 }
4698}
4699
4700
Willy Tarreau58f10d72006-12-04 02:26:12 +01004701/*
4702 * Try to retrieve a known appsession in the URI, then the associated server.
4703 * If the server is found, it's assigned to the session.
4704 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004705void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004706{
4707 appsess *asession_temp = NULL;
4708 appsess local_asession;
4709 char *request_line;
4710
Willy Tarreau830ff452006-12-17 19:31:23 +01004711 if (t->be->beprm->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004712 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004713 (request_line = memchr(begin, ';', len)) == NULL ||
4714 ((1 + t->be->beprm->appsession_name_len + 1 + t->be->beprm->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004715 return;
4716
4717 /* skip ';' */
4718 request_line++;
4719
4720 /* look if we have a jsessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004721 if (strncasecmp(request_line, t->be->beprm->appsession_name, t->be->beprm->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004722 return;
4723
4724 /* skip jsessionid= */
Willy Tarreau830ff452006-12-17 19:31:23 +01004725 request_line += t->be->beprm->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004726
4727 /* First try if we already have an appsession */
4728 asession_temp = &local_asession;
4729
4730 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4731 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4732 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4733 return;
4734 }
4735
4736 /* Copy the sessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004737 memcpy(asession_temp->sessid, request_line, t->be->beprm->appsession_len);
4738 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004739 asession_temp->serverid = NULL;
4740
4741 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004742 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004743 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4744 /* free previously allocated memory */
4745 pool_free_to(apools.sessid, local_asession.sessid);
4746 Alert("Not enough memory process_cli():asession:calloc().\n");
4747 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4748 return;
4749 }
4750 asession_temp->sessid = local_asession.sessid;
4751 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004752 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004753 }
4754 else {
4755 /* free previously allocated memory */
4756 pool_free_to(apools.sessid, local_asession.sessid);
4757 }
4758
Willy Tarreau830ff452006-12-17 19:31:23 +01004759 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004760 asession_temp->request_count++;
4761
4762#if defined(DEBUG_HASH)
4763 print_table(&(t->proxy->htbl_proxy));
4764#endif
4765 if (asession_temp->serverid == NULL) {
4766 Alert("Found Application Session without matching server.\n");
4767 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004768 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004769 while (srv) {
4770 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004771 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004772 /* we found the server and it's usable */
4773 t->flags &= ~SN_CK_MASK;
4774 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
4775 t->srv = srv;
4776 break;
4777 } else {
4778 t->flags &= ~SN_CK_MASK;
4779 t->flags |= SN_CK_DOWN;
4780 }
4781 }
4782 srv = srv->next;
4783 }
4784 }
4785}
4786
4787
Willy Tarreaub2513902006-12-17 14:52:38 +01004788/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004789 * In a GET or HEAD request, check if the requested URI matches the stats uri
4790 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004791 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004792 * It is assumed that the request is either a HEAD or GET and that the
4793 * t->be->fiprm->uri_auth field is valid. An HTTP/401 response may be sent, or
4794 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004795 *
4796 * Returns 1 if the session's state changes, otherwise 0.
4797 */
4798int stats_check_uri_auth(struct session *t, struct proxy *backend)
4799{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004800 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004801 struct uri_auth *uri_auth = backend->uri_auth;
4802 struct user_auth *user;
4803 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004804 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004806 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004807 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004808 return 0;
4809
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004810 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004811
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004812 /* the URI is in h */
4813 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004814 return 0;
4815
4816 /* we are in front of a interceptable URI. Let's check
4817 * if there's an authentication and if it's valid.
4818 */
4819 user = uri_auth->users;
4820 if (!user) {
4821 /* no user auth required, it's OK */
4822 authenticated = 1;
4823 } else {
4824 authenticated = 0;
4825
4826 /* a user list is defined, we have to check.
4827 * skip 21 chars for "Authorization: Basic ".
4828 */
4829
4830 /* FIXME: this should move to an earlier place */
4831 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004832 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4833 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4834 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004835 if (len > 14 &&
4836 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004837 txn->auth_hdr.str = h;
4838 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004839 break;
4840 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004841 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004842 }
4843
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004844 if (txn->auth_hdr.len < 21 ||
4845 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004846 user = NULL;
4847
4848 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004849 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4850 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004851 user->user_pwd, user->user_len)) {
4852 authenticated = 1;
4853 break;
4854 }
4855 user = user->next;
4856 }
4857 }
4858
4859 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004860 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004861
4862 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004863 msg.str = trash;
4864 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreaub2513902006-12-17 14:52:38 +01004865 t->logs.status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004866 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004867 if (!(t->flags & SN_ERR_MASK))
4868 t->flags |= SN_ERR_PRXCOND;
4869 if (!(t->flags & SN_FINST_MASK))
4870 t->flags |= SN_FINST_R;
4871 return 1;
4872 }
4873
4874 /* The request is valid, the user is authenticate. Let's start sending
4875 * data.
4876 */
4877 t->cli_state = CL_STSHUTR;
4878 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
4879 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
4880 t->data_source = DATA_SRC_STATS;
4881 t->data_state = DATA_ST_INIT;
4882 produce_content(t);
4883 return 1;
4884}
4885
4886
Willy Tarreaubaaee002006-06-26 02:48:02 +02004887/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004888 * Print a debug line with a header
4889 */
4890void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4891{
4892 int len, max;
4893 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4894 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4895 max = end - start;
4896 UBOUND(max, sizeof(trash) - len - 1);
4897 len += strlcpy2(trash + len, start, max + 1);
4898 trash[len++] = '\n';
4899 write(1, trash, len);
4900}
4901
4902
4903/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004904 * Local variables:
4905 * c-indent-level: 8
4906 * c-basic-offset: 8
4907 * End:
4908 */