blob: 124594b5fe4289b6ddc0c1a2cc4f40605e18200b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
5 *
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>
28#include <common/memory.h>
29#include <common/mini-clist.h>
30#include <common/standard.h>
31#include <common/time.h>
32#include <common/uri_auth.h>
33#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
35#include <types/capture.h>
36#include <types/client.h>
37#include <types/global.h>
38#include <types/httperr.h>
39#include <types/polling.h>
40#include <types/proxy.h>
41#include <types/server.h>
42
43#include <proto/backend.h>
44#include <proto/buffers.h>
45#include <proto/fd.h>
46#include <proto/log.h>
47#include <proto/proto_http.h>
48#include <proto/queue.h>
49#include <proto/session.h>
50#include <proto/task.h>
51
52
Willy Tarreau1c47f852006-07-09 08:22:27 +020053/* This is used by remote monitoring */
54const char *HTTP_200 =
55 "HTTP/1.0 200 OK\r\n"
56 "Cache-Control: no-cache\r\n"
57 "Connection: close\r\n"
58 "Content-Type: text/html\r\n"
59 "\r\n"
60 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
61
Willy Tarreaubaaee002006-06-26 02:48:02 +020062/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
63const char *HTTP_401_fmt =
64 "HTTP/1.0 401 Unauthorized\r\n"
65 "Cache-Control: no-cache\r\n"
66 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020067 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020068 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
69 "\r\n"
70 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
71
72
73#ifdef DEBUG_FULL
74static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
75static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
76#endif
77
78
79/*
80 * returns a message to the client ; the connection is shut down for read,
81 * and the request is cleared so that no server connection can be initiated.
82 * The client must be in a valid state for this (HEADER, DATA ...).
83 * Nothing is performed on the server side.
84 * The reply buffer doesn't need to be empty before this.
85 */
86void client_retnclose(struct session *s, int len, const char *msg)
87{
Willy Tarreau2a429502006-10-15 14:52:29 +020088 MY_FD_CLR(s->cli_fd, StaticReadEvent);
89 MY_FD_SET(s->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +020090 tv_eternity(&s->req->rex);
Willy Tarreau73de9892006-11-30 11:40:23 +010091 if (s->fe->clitimeout)
92 tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +020093 else
Willy Tarreaud7971282006-07-29 18:36:34 +020094 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +020095 shutdown(s->cli_fd, SHUT_RD);
96 s->cli_state = CL_STSHUTR;
97 buffer_flush(s->rep);
98 buffer_write(s->rep, msg, len);
99 s->req->l = 0;
100}
101
102
103/*
104 * returns a message into the rep buffer, and flushes the req buffer.
105 * The reply buffer doesn't need to be empty before this.
106 */
107void client_return(struct session *s, int len, const char *msg)
108{
109 buffer_flush(s->rep);
110 buffer_write(s->rep, msg, len);
111 s->req->l = 0;
112}
113
114
115/* This function turns the server state into the SV_STCLOSE, and sets
116 * indicators accordingly. Note that if <status> is 0, no message is
117 * returned.
118 */
119void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreaub17916e2006-10-15 15:17:57 +0200120 int status, int msglen, const char *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121{
122 t->srv_state = SV_STCLOSE;
123 if (status > 0) {
124 t->logs.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100125 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126 client_return(t, msglen, msg);
127 }
128 if (!(t->flags & SN_ERR_MASK))
129 t->flags |= err;
130 if (!(t->flags & SN_FINST_MASK))
131 t->flags |= finst;
132}
133
134
135/* Processes the client and server jobs of a session task, then
136 * puts it back to the wait queue in a clean state, or
137 * cleans up its resources if it must be deleted. Returns
138 * the time the task accepts to wait, or TIME_ETERNITY for
139 * infinity.
140 */
141int process_session(struct task *t)
142{
143 struct session *s = t->context;
144 int fsm_resync = 0;
145
146 do {
147 fsm_resync = 0;
148 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
149 fsm_resync |= process_cli(s);
150 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
151 fsm_resync |= process_srv(s);
152 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
153 } while (fsm_resync);
154
155 if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) {
156 struct timeval min1, min2;
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200157 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
158 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159
Willy Tarreaud7971282006-07-29 18:36:34 +0200160 tv_min(&min1, &s->req->rex, &s->req->wex);
161 tv_min(&min2, &s->rep->rex, &s->rep->wex);
162 tv_min(&min1, &min1, &s->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163 tv_min(&t->expire, &min1, &min2);
164
165 /* restore t to its place in the task list */
166 task_queue(t);
167
168#ifdef DEBUG_FULL
169 /* DEBUG code : this should never ever happen, otherwise it indicates
170 * that a task still has something to do and will provoke a quick loop.
171 */
172 if (tv_remain2(&now, &t->expire) <= 0)
173 exit(100);
174#endif
175
176 return tv_remain2(&now, &t->expire); /* nothing more to do */
177 }
178
Willy Tarreau73de9892006-11-30 11:40:23 +0100179 s->fe->nbconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180 actconn--;
181
182 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
183 int len;
Willy Tarreau73de9892006-11-30 11:40:23 +0100184 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", s->uniq_id, s->be->id, (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185 write(1, trash, len);
186 }
187
188 s->logs.t_close = tv_diff(&s->logs.tv_accept, &now);
189 if (s->rep != NULL)
190 s->logs.bytes = s->rep->total;
191
192 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200193 if (s->logs.logwait &&
194 !(s->flags & SN_MONITOR) &&
Willy Tarreau73de9892006-11-30 11:40:23 +0100195 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196 sess_log(s);
197
198 /* the task MUST not be in the run queue anymore */
199 task_delete(t);
200 session_free(s);
201 task_free(t);
202 return TIME_ETERNITY; /* rest in peace for eternity */
203}
204
205
206/*
207 * FIXME: This should move to the HTTP_flow_analyzer code
208 */
209
210/*
211 * manages the client FSM and its socket. BTW, it also tries to handle the
212 * cookie. It returns 1 if a state has changed (and a resync may be needed),
213 * 0 else.
214 */
215int process_cli(struct session *t)
216{
217 int s = t->srv_state;
218 int c = t->cli_state;
219 struct buffer *req = t->req;
220 struct buffer *rep = t->rep;
221 int method_checked = 0;
222 appsess *asession_temp = NULL;
223 appsess local_asession;
224
225#ifdef DEBUG_FULL
226 fprintf(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
227 cli_stnames[c], srv_stnames[s],
Willy Tarreau2a429502006-10-15 14:52:29 +0200228 MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
Willy Tarreaud7971282006-07-29 18:36:34 +0200229 req->rex.tv_sec, req->rex.tv_usec,
230 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231#endif
232 //fprintf(stderr,"process_cli: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s,
Willy Tarreau2a429502006-10-15 14:52:29 +0200233 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
234 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235 //);
236 if (c == CL_STHEADERS) {
237 /* now parse the partial (or complete) headers */
238 while (req->lr < req->r) { /* this loop only sees one header at each iteration */
239 char *ptr;
240 int delete_header;
241 char *request_line = NULL;
242
243 ptr = req->lr;
244
245 /* look for the end of the current header */
246 while (ptr < req->r && *ptr != '\n' && *ptr != '\r')
247 ptr++;
248
249 if (ptr == req->h) { /* empty line, end of headers */
250 int line, len;
251
252 /*
253 * first, let's check that it's not a leading empty line, in
254 * which case we'll ignore and remove it (according to RFC2616).
255 */
256 if (req->h == req->data) {
257 /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */
258 if (ptr > req->r - 2) {
259 /* this is a partial header, let's wait for more to come */
260 req->lr = ptr;
261 break;
262 }
263
264 /* now we know that *ptr is either \r or \n,
265 * and that there are at least 1 char after it.
266 */
267 if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n'))
268 req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */
269 else
270 req->lr = ptr + 2; /* \r\n or \n\r */
271 /* ignore empty leading lines */
272 buffer_replace2(req, req->h, req->lr, NULL, 0);
273 req->h = req->lr;
274 continue;
275 }
276
277 /* we can only get here after an end of headers */
278 /* we'll have something else to do here : add new headers ... */
279
280 if (t->flags & SN_CLDENY) {
281 /* no need to go further */
282 t->logs.status = 403;
283 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); /* let's log the request time */
Willy Tarreau73de9892006-11-30 11:40:23 +0100284 client_retnclose(t, t->fe->errmsg.len403, t->fe->errmsg.msg403);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285 if (!(t->flags & SN_ERR_MASK))
286 t->flags |= SN_ERR_PRXCOND;
287 if (!(t->flags & SN_FINST_MASK))
288 t->flags |= SN_FINST_R;
289 return 1;
290 }
291
292 /* Right now, we know that we have processed the entire headers
293 * and that unwanted requests have been filtered out. We can do
294 * whatever we want.
295 */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200296
297
298 /* check if the URI matches the monitor_uri. To speed-up the
299 * test, we include the leading and trailing spaces in the
300 * comparison.
301 */
Willy Tarreau73de9892006-11-30 11:40:23 +0100302 if ((t->be->monitor_uri_len != 0) &&
303 (t->req_line.len >= t->be->monitor_uri_len)) {
Willy Tarreau1c47f852006-07-09 08:22:27 +0200304 char *p = t->req_line.str;
305 int idx = 0;
306
307 /* skip the method so that we accept any method */
308 while (idx < t->req_line.len && p[idx] != ' ')
309 idx++;
310 p += idx;
311
Willy Tarreau73de9892006-11-30 11:40:23 +0100312 if (t->req_line.len - idx >= t->be->monitor_uri_len &&
313 !memcmp(p, t->be->monitor_uri, t->be->monitor_uri_len)) {
Willy Tarreau1c47f852006-07-09 08:22:27 +0200314 /*
315 * We have found the monitor URI
316 */
317 t->flags |= SN_MONITOR;
318 t->logs.status = 200;
319 client_retnclose(t, strlen(HTTP_200), HTTP_200);
320 if (!(t->flags & SN_ERR_MASK))
321 t->flags |= SN_ERR_PRXCOND;
322 if (!(t->flags & SN_FINST_MASK))
323 t->flags |= SN_FINST_R;
324 return 1;
325 }
326 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327
Willy Tarreau73de9892006-11-30 11:40:23 +0100328 if (t->fi->uri_auth != NULL
329 && t->req_line.len >= t->fi->uri_auth->uri_len + 4) { /* +4 for "GET /" */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330 if (!memcmp(t->req_line.str + 4,
Willy Tarreau73de9892006-11-30 11:40:23 +0100331 t->fi->uri_auth->uri_prefix, t->fi->uri_auth->uri_len)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332 && !memcmp(t->req_line.str, "GET ", 4)) {
333 struct user_auth *user;
334 int authenticated;
335
336 /* we are in front of a interceptable URI. Let's check
337 * if there's an authentication and if it's valid.
338 */
Willy Tarreau73de9892006-11-30 11:40:23 +0100339 user = t->fi->uri_auth->users;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340 if (!user) {
341 /* no user auth required, it's OK */
342 authenticated = 1;
343 } else {
344 authenticated = 0;
345
346 /* a user list is defined, we have to check.
347 * skip 21 chars for "Authorization: Basic ".
348 */
349 if (t->auth_hdr.len < 21 || memcmp(t->auth_hdr.str + 14, " Basic ", 7))
350 user = NULL;
351
352 while (user) {
353 if ((t->auth_hdr.len == user->user_len + 21)
354 && !memcmp(t->auth_hdr.str+21, user->user_pwd, user->user_len)) {
355 authenticated = 1;
356 break;
357 }
358 user = user->next;
359 }
360 }
361
362 if (!authenticated) {
363 int msglen;
364
365 /* no need to go further */
366
Willy Tarreau73de9892006-11-30 11:40:23 +0100367 msglen = sprintf(trash, HTTP_401_fmt, t->fi->uri_auth->auth_realm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368 t->logs.status = 401;
369 client_retnclose(t, msglen, trash);
370 if (!(t->flags & SN_ERR_MASK))
371 t->flags |= SN_ERR_PRXCOND;
372 if (!(t->flags & SN_FINST_MASK))
373 t->flags |= SN_FINST_R;
374 return 1;
375 }
376
377 t->cli_state = CL_STSHUTR;
378 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
379 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
380 t->data_source = DATA_SRC_STATS;
381 t->data_state = DATA_ST_INIT;
382 produce_content(t);
383 return 1;
384 }
385 }
386
387
Willy Tarreau73de9892006-11-30 11:40:23 +0100388 for (line = 0; line < t->fi->nb_reqadd; line++) {
389 len = sprintf(trash, "%s\r\n", t->fi->req_add[line]);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200390 buffer_replace2(req, req->h, req->h, trash, len);
391 }
392
Willy Tarreau73de9892006-11-30 11:40:23 +0100393 if (t->be->options & PR_O_FWDFOR) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394 if (t->cli_addr.ss_family == AF_INET) {
395 unsigned char *pn;
396 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
397 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d\r\n",
398 pn[0], pn[1], pn[2], pn[3]);
399 buffer_replace2(req, req->h, req->h, trash, len);
400 }
401 else if (t->cli_addr.ss_family == AF_INET6) {
402 char pn[INET6_ADDRSTRLEN];
403 inet_ntop(AF_INET6,
404 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
405 pn, sizeof(pn));
406 len = sprintf(trash, "X-Forwarded-For: %s\r\n", pn);
407 buffer_replace2(req, req->h, req->h, trash, len);
408 }
409 }
410
411 /* add a "connection: close" line if needed */
Willy Tarreau73de9892006-11-30 11:40:23 +0100412 if (t->fe->options & PR_O_HTTP_CLOSE)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413 buffer_replace2(req, req->h, req->h, "Connection: close\r\n", 19);
414
415 if (!memcmp(req->data, "POST ", 5)) {
416 /* this is a POST request, which is not cacheable by default */
417 t->flags |= SN_POST;
418 }
419
420 t->cli_state = CL_STDATA;
421 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
422
423 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
424 /* FIXME: we'll set the client in a wait state while we try to
425 * connect to the server. Is this really needed ? wouldn't it be
426 * better to release the maximum of system buffers instead ?
427 * The solution is to enable the FD but set its time-out to
428 * eternity as long as the server-side does not enable data xfer.
429 * CL_STDATA also has to take care of this, which is done below.
430 */
Willy Tarreau2a429502006-10-15 14:52:29 +0200431 //MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +0200432 //tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433
434 /* FIXME: if we break here (as up to 1.1.23), having the client
435 * shutdown its connection can lead to an abort further.
436 * it's better to either return 1 or even jump directly to the
437 * data state which will save one schedule.
438 */
439 //break;
440
Willy Tarreau73de9892006-11-30 11:40:23 +0100441 if (!t->fe->clitimeout ||
442 (t->srv_state < SV_STDATA && t->be->srvtimeout))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443 /* If the client has no timeout, or if the server is not ready yet,
444 * and we know for sure that it can expire, then it's cleaner to
445 * disable the timeout on the client side so that too low values
446 * cannot make the sessions abort too early.
447 *
448 * FIXME-20050705: the server needs a way to re-enable this time-out
449 * when it switches its state, otherwise a client can stay connected
450 * indefinitely. This now seems to be OK.
451 */
Willy Tarreaud7971282006-07-29 18:36:34 +0200452 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453
Willy Tarreaub8750a82006-09-03 09:56:00 +0200454
455 /* When a connection is tarpitted, we use the queue timeout for the
456 * tarpit delay, which currently happens to be the server's connect
457 * timeout. If unset, then set it to zero because we really want it
458 * to expire at one moment.
459 */
460 if (t->flags & SN_CLTARPIT) {
Willy Tarreau08fa2e32006-09-03 10:47:37 +0200461 t->req->l = 0;
462 /* flush the request so that we can drop the connection early
463 * if the client closes first.
464 */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200465 tv_delayfrom(&req->cex, &now,
Willy Tarreau73de9892006-11-30 11:40:23 +0100466 t->be->contimeout ? t->be->contimeout : 0);
Willy Tarreaub8750a82006-09-03 09:56:00 +0200467 }
468
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 goto process_data;
470 }
471
Willy Tarreau669e6da2006-12-02 20:12:09 +0100472 /* To get a complete header line, we need the ending \r\n, \n\r,
473 * \r or \n, possibly followed by a white space or tab indicating
474 * that the header goes on next line. */
475 if (ptr > req->r - 3) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476 /* this is a partial header, let's wait for more to come */
477 req->lr = ptr;
478 break;
479 }
480
481 /* now we know that *ptr is either \r or \n,
482 * and that there are at least 1 char after it.
483 */
484 if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n'))
485 req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */
486 else
487 req->lr = ptr + 2; /* \r\n or \n\r */
488
Willy Tarreau669e6da2006-12-02 20:12:09 +0100489 /* Now, try to detect multi-line headers. From RFC 2616 :
490 * HTTP/1.1 header field values can be folded onto multiple lines if the
491 * continuation line begins with a space or horizontal tab. All linear
492 * white space, including folding, has the same semantics as SP. A
493 * recipient MAY replace any linear white space with a single SP before
494 * interpreting the field value or forwarding the message downstream.
495 *
496 * LWS = [CRLF] 1*( SP | HT )
497 */
498 if (req->lr < req->r &&
499 (*req->lr == ' ' || *req->lr == '\t')) {
500 /* we are allowed to replace the \r\n with spaces */
501 while (ptr < req->lr)
502 *ptr++ = ' ';
503 /* now look for end of LWS */
504 do {
505 req->lr++;
506 } while (req->lr < req->r && (*req->lr == ' ' || *req->lr == '\t'));
507
508 /* continue processing on the same header */
509 continue;
510 }
511
Willy Tarreaubaaee002006-06-26 02:48:02 +0200512 /*
513 * now we know that we have a full header ; we can do whatever
514 * we want with these pointers :
515 * req->h = beginning of header
516 * ptr = end of header (first \r or \n)
517 * req->lr = beginning of next line (next rep->h)
518 * req->r = end of data (not used at this stage)
519 */
520
Willy Tarreau73de9892006-11-30 11:40:23 +0100521 if (!method_checked && (t->be->appsession_name != NULL) &&
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522 ((memcmp(req->h, "GET ", 4) == 0) || (memcmp(req->h, "POST ", 4) == 0)) &&
523 ((request_line = memchr(req->h, ';', req->lr - req->h)) != NULL)) {
524
525 /* skip ; */
526 request_line++;
527
528 /* look if we have a jsessionid */
529
Willy Tarreau73de9892006-11-30 11:40:23 +0100530 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200531
532 /* skip jsessionid= */
Willy Tarreau73de9892006-11-30 11:40:23 +0100533 request_line += t->be->appsession_name_len + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200534
535 /* First try if we allready have an appsession */
536 asession_temp = &local_asession;
537
538 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
539 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
Willy Tarreau73de9892006-11-30 11:40:23 +0100540 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200541 return 0;
542 }
543
544 /* Copy the sessionid */
Willy Tarreau73de9892006-11-30 11:40:23 +0100545 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
546 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200547 asession_temp->serverid = NULL;
548
549 /* only do insert, if lookup fails */
Willy Tarreau73de9892006-11-30 11:40:23 +0100550 if (chtbl_lookup(&(t->be->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200551 if ((asession_temp = pool_alloc(appsess)) == NULL) {
552 Alert("Not enough memory process_cli():asession:calloc().\n");
Willy Tarreau73de9892006-11-30 11:40:23 +0100553 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554 return 0;
555 }
556 asession_temp->sessid = local_asession.sessid;
557 asession_temp->serverid = local_asession.serverid;
Willy Tarreau73de9892006-11-30 11:40:23 +0100558 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 } /* end if (chtbl_lookup()) */
560 else {
561 /*free wasted memory;*/
562 pool_free_to(apools.sessid, local_asession.sessid);
563 }
564
Willy Tarreau73de9892006-11-30 11:40:23 +0100565 tv_delayfrom(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566 asession_temp->request_count++;
567
568#if defined(DEBUG_HASH)
569 print_table(&(t->proxy->htbl_proxy));
570#endif
571
572 if (asession_temp->serverid == NULL) {
573 Alert("Found Application Session without matching server.\n");
574 } else {
Willy Tarreau73de9892006-11-30 11:40:23 +0100575 struct server *srv = t->be->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200576 while (srv) {
577 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau73de9892006-11-30 11:40:23 +0100578 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579 /* we found the server and it's usable */
580 t->flags &= ~SN_CK_MASK;
581 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
582 t->srv = srv;
583 break;
584 } else {
585 t->flags &= ~SN_CK_MASK;
586 t->flags |= SN_CK_DOWN;
587 }
588 } /* end if (strcmp()) */
589 srv = srv->next;
590 }/* end while(srv) */
591 }/* end else of if (asession_temp->serverid == NULL) */
592 }/* end if (strncasecmp(request_line,t->proxy->appsession_name,apssesion_name_len) == 0) */
593 else {
594 //fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>NO SESSION\n");
595 }
596 method_checked = 1;
597 } /* end if (!method_checked ...) */
598 else{
599 //printf("No Methode-Header with Session-String\n");
600 }
601
602 if (t->logs.logwait & LW_REQ) {
603 /* we have a complete HTTP request that we must log */
604 int urilen;
605
606 if ((t->logs.uri = pool_alloc(requri)) == NULL) {
607 Alert("HTTP logging : out of memory.\n");
608 t->logs.status = 500;
Willy Tarreau73de9892006-11-30 11:40:23 +0100609 client_retnclose(t, t->fe->errmsg.len500, t->fe->errmsg.msg500);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200610 if (!(t->flags & SN_ERR_MASK))
611 t->flags |= SN_ERR_PRXCOND;
612 if (!(t->flags & SN_FINST_MASK))
613 t->flags |= SN_FINST_R;
614 return 1;
615 }
616
617 urilen = ptr - req->h;
618 if (urilen >= REQURI_LEN)
619 urilen = REQURI_LEN - 1;
620 memcpy(t->logs.uri, req->h, urilen);
621 t->logs.uri[urilen] = 0;
622
623 if (!(t->logs.logwait &= ~LW_REQ))
624 sess_log(t);
625 }
626 else if (t->logs.logwait & LW_REQHDR) {
627 struct cap_hdr *h;
628 int len;
Willy Tarreau73de9892006-11-30 11:40:23 +0100629 for (h = t->fi->req_cap; h; h = h->next) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200630 if ((h->namelen + 2 <= ptr - req->h) &&
631 (req->h[h->namelen] == ':') &&
632 (strncasecmp(req->h, h->name, h->namelen) == 0)) {
633
634 if (t->req_cap[h->index] == NULL)
635 t->req_cap[h->index] = pool_alloc_from(h->pool, h->len + 1);
636
637 len = ptr - (req->h + h->namelen + 2);
638 if (len > h->len)
639 len = h->len;
640
641 memcpy(t->req_cap[h->index], req->h + h->namelen + 2, len);
642 t->req_cap[h->index][len]=0;
643 }
644 }
645
646 }
647
648 delete_header = 0;
649
650 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
651 int len, max;
Willy Tarreau73de9892006-11-30 11:40:23 +0100652 len = sprintf(trash, "%08x:%s.clihdr[%04x:%04x]: ", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200653 max = ptr - req->h;
654 UBOUND(max, sizeof(trash) - len - 1);
655 len += strlcpy2(trash + len, req->h, max + 1);
656 trash[len++] = '\n';
657 write(1, trash, len);
658 }
659
660
661 /* remove "connection: " if needed */
Willy Tarreau73de9892006-11-30 11:40:23 +0100662 if (!delete_header && (t->fe->options & PR_O_HTTP_CLOSE)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663 && (strncasecmp(req->h, "Connection: ", 12) == 0)) {
664 delete_header = 1;
665 }
666
667 /* try headers regexps */
Willy Tarreau73de9892006-11-30 11:40:23 +0100668 if (!delete_header && t->fi->req_exp != NULL
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669 && !(t->flags & SN_CLDENY)) {
670 struct hdr_exp *exp;
671 char term;
672
673 term = *ptr;
674 *ptr = '\0';
Willy Tarreau73de9892006-11-30 11:40:23 +0100675 exp = t->fi->req_exp;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 do {
677 if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) {
678 switch (exp->action) {
679 case ACT_ALLOW:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200680 if (!(t->flags & (SN_CLDENY | SN_CLTARPIT)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200681 t->flags |= SN_CLALLOW;
682 break;
683 case ACT_REPLACE:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200684 if (!(t->flags & (SN_CLDENY | SN_CLTARPIT))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685 int len = exp_replace(trash, req->h, exp->replace, pmatch);
686 ptr += buffer_replace2(req, req->h, ptr, trash, len);
687 }
688 break;
689 case ACT_REMOVE:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200690 if (!(t->flags & (SN_CLDENY | SN_CLTARPIT)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200691 delete_header = 1;
692 break;
693 case ACT_DENY:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200694 if (!(t->flags & (SN_CLALLOW | SN_CLTARPIT)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695 t->flags |= SN_CLDENY;
696 break;
Willy Tarreaub8750a82006-09-03 09:56:00 +0200697 case ACT_TARPIT:
698 if (!(t->flags & (SN_CLALLOW | SN_CLDENY)))
699 t->flags |= SN_CLTARPIT;
700 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701 case ACT_PASS: /* we simply don't deny this one */
702 break;
703 }
704 break;
705 }
706 } while ((exp = exp->next) != NULL);
707 *ptr = term; /* restore the string terminator */
708 }
709
710 /* Now look for cookies. Conforming to RFC2109, we have to support
711 * attributes whose name begin with a '$', and associate them with
712 * the right cookie, if we want to delete this cookie.
713 * So there are 3 cases for each cookie read :
714 * 1) it's a special attribute, beginning with a '$' : ignore it.
715 * 2) it's a server id cookie that we *MAY* want to delete : save
716 * some pointers on it (last semi-colon, beginning of cookie...)
717 * 3) it's an application cookie : we *MAY* have to delete a previous
718 * "special" cookie.
719 * At the end of loop, if a "special" cookie remains, we may have to
720 * remove it. If no application cookie persists in the header, we
721 * *MUST* delete it
722 */
723 if (!delete_header &&
Willy Tarreau73de9892006-11-30 11:40:23 +0100724 (t->be->cookie_name != NULL || t->be->appsession_name !=NULL || t->fi->capture_name != NULL)
Willy Tarreaub8750a82006-09-03 09:56:00 +0200725 && !(t->flags & (SN_CLDENY|SN_CLTARPIT)) && (ptr >= req->h + 8)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200726 && (strncasecmp(req->h, "Cookie: ", 8) == 0)) {
727 char *p1, *p2, *p3, *p4;
728 char *del_colon, *del_cookie, *colon;
729 int app_cookies;
730
731 p1 = req->h + 8; /* first char after 'Cookie: ' */
732 colon = p1;
733 /* del_cookie == NULL => nothing to be deleted */
734 del_colon = del_cookie = NULL;
735 app_cookies = 0;
736
737 while (p1 < ptr) {
738 /* skip spaces and colons, but keep an eye on these ones */
739 while (p1 < ptr) {
740 if (*p1 == ';' || *p1 == ',')
741 colon = p1;
742 else if (!isspace((int)*p1))
743 break;
744 p1++;
745 }
746
747 if (p1 == ptr)
748 break;
749
750 /* p1 is at the beginning of the cookie name */
751 p2 = p1;
752 while (p2 < ptr && *p2 != '=')
753 p2++;
754
755 if (p2 == ptr)
756 break;
757
758 p3 = p2 + 1; /* skips the '=' sign */
759 if (p3 == ptr)
760 break;
761
762 p4 = p3;
763 while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
764 p4++;
765
766 /* here, we have the cookie name between p1 and p2,
767 * and its value between p3 and p4.
768 * we can process it :
769 *
770 * Cookie: NAME=VALUE;
771 * | || || |
772 * | || || +--> p4
773 * | || |+-------> p3
774 * | || +--------> p2
775 * | |+------------> p1
776 * | +-------------> colon
777 * +--------------------> req->h
778 */
779
780 if (*p1 == '$') {
781 /* skip this one */
782 }
783 else {
784 /* first, let's see if we want to capture it */
Willy Tarreau73de9892006-11-30 11:40:23 +0100785 if (t->fi->capture_name != NULL &&
Willy Tarreaubaaee002006-06-26 02:48:02 +0200786 t->logs.cli_cookie == NULL &&
Willy Tarreau73de9892006-11-30 11:40:23 +0100787 (p4 - p1 >= t->fi->capture_namelen) &&
788 memcmp(p1, t->fi->capture_name, t->fi->capture_namelen) == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200789 int log_len = p4 - p1;
790
791 if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) {
792 Alert("HTTP logging : out of memory.\n");
793 } else {
Willy Tarreau73de9892006-11-30 11:40:23 +0100794 if (log_len > t->fi->capture_len)
795 log_len = t->fi->capture_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796 memcpy(t->logs.cli_cookie, p1, log_len);
797 t->logs.cli_cookie[log_len] = 0;
798 }
799 }
800
Willy Tarreau73de9892006-11-30 11:40:23 +0100801 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
802 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200803 /* Cool... it's the right one */
Willy Tarreau73de9892006-11-30 11:40:23 +0100804 struct server *srv = t->be->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805 char *delim;
806
807 /* if we're in cookie prefix mode, we'll search the delimitor so that we
808 * have the server ID betweek p3 and delim, and the original cookie between
809 * delim+1 and p4. Otherwise, delim==p4 :
810 *
811 * Cookie: NAME=SRV~VALUE;
812 * | || || | |
813 * | || || | +--> p4
814 * | || || +--------> delim
815 * | || |+-----------> p3
816 * | || +------------> p2
817 * | |+----------------> p1
818 * | +-----------------> colon
819 * +------------------------> req->h
820 */
821
Willy Tarreau73de9892006-11-30 11:40:23 +0100822 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200823 for (delim = p3; delim < p4; delim++)
824 if (*delim == COOKIE_DELIM)
825 break;
826 }
827 else
828 delim = p4;
829
830
831 /* Here, we'll look for the first running server which supports the cookie.
832 * This allows to share a same cookie between several servers, for example
833 * to dedicate backup servers to specific servers only.
834 * However, to prevent clients from sticking to cookie-less backup server
835 * when they have incidentely learned an empty cookie, we simply ignore
836 * empty cookies and mark them as invalid.
837 */
838 if (delim == p3)
839 srv = NULL;
840
841 while (srv) {
842 if ((srv->cklen == delim - p3) && !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau73de9892006-11-30 11:40:23 +0100843 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 /* we found the server and it's usable */
845 t->flags &= ~SN_CK_MASK;
846 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
847 t->srv = srv;
848 break;
849 } else {
850 /* we found a server, but it's down */
851 t->flags &= ~SN_CK_MASK;
852 t->flags |= SN_CK_DOWN;
853 }
854 }
855 srv = srv->next;
856 }
857
858 if (!srv && !(t->flags & SN_CK_DOWN)) {
859 /* no server matched this cookie */
860 t->flags &= ~SN_CK_MASK;
861 t->flags |= SN_CK_INVALID;
862 }
863
864 /* depending on the cookie mode, we may have to either :
865 * - delete the complete cookie if we're in insert+indirect mode, so that
866 * the server never sees it ;
867 * - remove the server id from the cookie value, and tag the cookie as an
868 * application cookie so that it does not get accidentely removed later,
869 * if we're in cookie prefix mode
870 */
Willy Tarreau73de9892006-11-30 11:40:23 +0100871 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200872 buffer_replace2(req, p3, delim + 1, NULL, 0);
873 p4 -= (delim + 1 - p3);
874 ptr -= (delim + 1 - p3);
875 del_cookie = del_colon = NULL;
876 app_cookies++; /* protect the header from deletion */
877 }
878 else if (del_cookie == NULL &&
Willy Tarreau73de9892006-11-30 11:40:23 +0100879 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200880 del_cookie = p1;
881 del_colon = colon;
882 }
883 } else {
884 /* now we know that we must keep this cookie since it's
885 * not ours. But if we wanted to delete our cookie
886 * earlier, we cannot remove the complete header, but we
887 * can remove the previous block itself.
888 */
889 app_cookies++;
890
891 if (del_cookie != NULL) {
892 buffer_replace2(req, del_cookie, p1, NULL, 0);
893 p4 -= (p1 - del_cookie);
894 ptr -= (p1 - del_cookie);
895 del_cookie = del_colon = NULL;
896 }
897 }
898
Willy Tarreau73de9892006-11-30 11:40:23 +0100899 if ((t->be->appsession_name != NULL) &&
900 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200901 /* first, let's see if the cookie is our appcookie*/
902
903 /* Cool... it's the right one */
904
905 asession_temp = &local_asession;
906
907 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
908 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreau73de9892006-11-30 11:40:23 +0100909 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200910 return 0;
911 }
912
Willy Tarreau73de9892006-11-30 11:40:23 +0100913 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
914 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200915 asession_temp->serverid = NULL;
916
917 /* only do insert, if lookup fails */
Willy Tarreau73de9892006-11-30 11:40:23 +0100918 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200919 if ((asession_temp = pool_alloc(appsess)) == NULL) {
920 Alert("Not enough memory process_cli():asession:calloc().\n");
Willy Tarreau73de9892006-11-30 11:40:23 +0100921 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200922 return 0;
923 }
924
925 asession_temp->sessid = local_asession.sessid;
926 asession_temp->serverid = local_asession.serverid;
Willy Tarreau73de9892006-11-30 11:40:23 +0100927 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928 } else {
929 /* free wasted memory */
930 pool_free_to(apools.sessid, local_asession.sessid);
931 }
932
933 if (asession_temp->serverid == NULL) {
934 Alert("Found Application Session without matching server.\n");
935 } else {
Willy Tarreau73de9892006-11-30 11:40:23 +0100936 struct server *srv = t->be->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937 while (srv) {
938 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau73de9892006-11-30 11:40:23 +0100939 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940 /* we found the server and it's usable */
941 t->flags &= ~SN_CK_MASK;
942 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
943 t->srv = srv;
944 break;
945 } else {
946 t->flags &= ~SN_CK_MASK;
947 t->flags |= SN_CK_DOWN;
948 }
949 }
950 srv = srv->next;
951 }/* end while(srv) */
952 }/* end else if server == NULL */
953
Willy Tarreau73de9892006-11-30 11:40:23 +0100954 tv_delayfrom(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200955 }/* end if ((t->proxy->appsession_name != NULL) ... */
956 }
957
958 /* we'll have to look for another cookie ... */
959 p1 = p4;
960 } /* while (p1 < ptr) */
961
962 /* There's no more cookie on this line.
963 * We may have marked the last one(s) for deletion.
964 * We must do this now in two ways :
965 * - if there is no app cookie, we simply delete the header ;
966 * - if there are app cookies, we must delete the end of the
967 * string properly, including the colon/semi-colon before
968 * the cookie name.
969 */
970 if (del_cookie != NULL) {
971 if (app_cookies) {
972 buffer_replace2(req, del_colon, ptr, NULL, 0);
973 /* WARNING! <ptr> becomes invalid for now. If some code
974 * below needs to rely on it before the end of the global
975 * header loop, we need to correct it with this code :
976 */
977 ptr = del_colon;
978 }
979 else
980 delete_header = 1;
981 }
982 } /* end of cookie processing on this header */
983
984 /* let's look if we have to delete this header */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200985 if (delete_header && !(t->flags & (SN_CLDENY|SN_CLTARPIT))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200986 buffer_replace2(req, req->h, req->lr, NULL, 0);
987 /* WARNING: ptr is not valid anymore, since the header may have
988 * been deleted or truncated ! */
989 } else {
990 /* try to catch the first line as the request */
991 if (t->req_line.len < 0) {
992 t->req_line.str = req->h;
993 t->req_line.len = ptr - req->h;
994 }
995
996 /* We might also need the 'Authorization: ' header */
997 if (t->auth_hdr.len < 0 &&
Willy Tarreau73de9892006-11-30 11:40:23 +0100998 t->fi->uri_auth != NULL &&
Willy Tarreaubaaee002006-06-26 02:48:02 +0200999 ptr > req->h + 15 &&
1000 !strncasecmp("Authorization: ", req->h, 15)) {
1001 t->auth_hdr.str = req->h;
1002 t->auth_hdr.len = ptr - req->h;
1003 }
1004 }
1005
1006 req->h = req->lr;
1007 } /* while (req->lr < req->r) */
1008
1009 /* end of header processing (even if incomplete) */
1010
Willy Tarreau2a429502006-10-15 14:52:29 +02001011 if ((req->l < req->rlim - req->data) && ! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
Willy Tarreaud7971282006-07-29 18:36:34 +02001013 * full. We cannot loop here since stream_sock_read will disable it only if
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 * req->l == rlim-data
1015 */
Willy Tarreau2a429502006-10-15 14:52:29 +02001016 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001017 if (t->fe->clitimeout)
1018 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001020 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001021 }
1022
1023 /* Since we are in header mode, if there's no space left for headers, we
1024 * won't be able to free more later, so the session will never terminate.
1025 */
1026 if (req->l >= req->rlim - req->data) {
1027 t->logs.status = 400;
Willy Tarreau73de9892006-11-30 11:40:23 +01001028 client_retnclose(t, t->fe->errmsg.len400, t->fe->errmsg.msg400);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001029 if (!(t->flags & SN_ERR_MASK))
1030 t->flags |= SN_ERR_PRXCOND;
1031 if (!(t->flags & SN_FINST_MASK))
1032 t->flags |= SN_FINST_R;
1033 return 1;
1034 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001035 else if (req->flags & (BF_READ_ERROR | BF_READ_NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001036 /* read error, or last read : give up. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001037 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001038 fd_delete(t->cli_fd);
1039 t->cli_state = CL_STCLOSE;
1040 if (!(t->flags & SN_ERR_MASK))
1041 t->flags |= SN_ERR_CLICL;
1042 if (!(t->flags & SN_FINST_MASK))
1043 t->flags |= SN_FINST_R;
1044 return 1;
1045 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001046 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001047
1048 /* read timeout : give up with an error message.
1049 */
1050 t->logs.status = 408;
Willy Tarreau73de9892006-11-30 11:40:23 +01001051 client_retnclose(t, t->fe->errmsg.len408, t->fe->errmsg.msg408);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001052 if (!(t->flags & SN_ERR_MASK))
1053 t->flags |= SN_ERR_CLITO;
1054 if (!(t->flags & SN_FINST_MASK))
1055 t->flags |= SN_FINST_R;
1056 return 1;
1057 }
1058
1059 return t->cli_state != CL_STHEADERS;
1060 }
1061 else if (c == CL_STDATA) {
1062 process_data:
1063 /* FIXME: this error handling is partly buggy because we always report
1064 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1065 * or HEADER phase. BTW, it's not logical to expire the client while
1066 * we're waiting for the server to connect.
1067 */
1068 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001069 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001070 tv_eternity(&req->rex);
1071 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001072 fd_delete(t->cli_fd);
1073 t->cli_state = CL_STCLOSE;
1074 if (!(t->flags & SN_ERR_MASK))
1075 t->flags |= SN_ERR_CLICL;
1076 if (!(t->flags & SN_FINST_MASK)) {
1077 if (t->pend_pos)
1078 t->flags |= SN_FINST_Q;
1079 else if (s == SV_STCONN)
1080 t->flags |= SN_FINST_C;
1081 else
1082 t->flags |= SN_FINST_D;
1083 }
1084 return 1;
1085 }
1086 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001087 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001088 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001089 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001090 shutdown(t->cli_fd, SHUT_RD);
1091 t->cli_state = CL_STSHUTR;
1092 return 1;
1093 }
1094 /* last server read and buffer empty */
1095 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001096 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001097 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098 shutdown(t->cli_fd, SHUT_WR);
1099 /* We must ensure that the read part is still alive when switching
1100 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001101 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001102 if (t->fe->clitimeout)
1103 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104 t->cli_state = CL_STSHUTW;
1105 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1106 return 1;
1107 }
1108 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001109 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001110 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001111 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001112 shutdown(t->cli_fd, SHUT_RD);
1113 t->cli_state = CL_STSHUTR;
1114 if (!(t->flags & SN_ERR_MASK))
1115 t->flags |= SN_ERR_CLITO;
1116 if (!(t->flags & SN_FINST_MASK)) {
1117 if (t->pend_pos)
1118 t->flags |= SN_FINST_Q;
1119 else if (s == SV_STCONN)
1120 t->flags |= SN_FINST_C;
1121 else
1122 t->flags |= SN_FINST_D;
1123 }
1124 return 1;
1125 }
1126 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001127 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001128 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001129 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001130 shutdown(t->cli_fd, SHUT_WR);
1131 /* We must ensure that the read part is still alive when switching
1132 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001133 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001134 if (t->fe->clitimeout)
1135 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001136
1137 t->cli_state = CL_STSHUTW;
1138 if (!(t->flags & SN_ERR_MASK))
1139 t->flags |= SN_ERR_CLITO;
1140 if (!(t->flags & SN_FINST_MASK)) {
1141 if (t->pend_pos)
1142 t->flags |= SN_FINST_Q;
1143 else if (s == SV_STCONN)
1144 t->flags |= SN_FINST_C;
1145 else
1146 t->flags |= SN_FINST_D;
1147 }
1148 return 1;
1149 }
1150
1151 if (req->l >= req->rlim - req->data) {
1152 /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02001153 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001154 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02001155 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001156 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001157 }
1158 } else {
1159 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02001160 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
1161 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001162 if (!t->fe->clitimeout ||
1163 (t->srv_state < SV_STDATA && t->be->srvtimeout))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164 /* If the client has no timeout, or if the server not ready yet, and we
1165 * know for sure that it can expire, then it's cleaner to disable the
1166 * timeout on the client side so that too low values cannot make the
1167 * sessions abort too early.
1168 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001169 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001170 else
Willy Tarreau73de9892006-11-30 11:40:23 +01001171 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001172 }
1173 }
1174
1175 if ((rep->l == 0) ||
1176 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001177 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1178 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001179 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001180 }
1181 } else {
1182 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001183 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1184 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001185 if (t->fe->clitimeout) {
1186 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001187 /* FIXME: to prevent the client from expiring read timeouts during writes,
1188 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001189 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001190 }
1191 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001192 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001193 }
1194 }
1195 return 0; /* other cases change nothing */
1196 }
1197 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001198 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001199 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001200 fd_delete(t->cli_fd);
1201 t->cli_state = CL_STCLOSE;
1202 if (!(t->flags & SN_ERR_MASK))
1203 t->flags |= SN_ERR_CLICL;
1204 if (!(t->flags & SN_FINST_MASK)) {
1205 if (t->pend_pos)
1206 t->flags |= SN_FINST_Q;
1207 else if (s == SV_STCONN)
1208 t->flags |= SN_FINST_C;
1209 else
1210 t->flags |= SN_FINST_D;
1211 }
1212 return 1;
1213 }
1214 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
1215 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001216 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001217 fd_delete(t->cli_fd);
1218 t->cli_state = CL_STCLOSE;
1219 return 1;
1220 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001221 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
1222 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001223 fd_delete(t->cli_fd);
1224 t->cli_state = CL_STCLOSE;
1225 if (!(t->flags & SN_ERR_MASK))
1226 t->flags |= SN_ERR_CLITO;
1227 if (!(t->flags & SN_FINST_MASK)) {
1228 if (t->pend_pos)
1229 t->flags |= SN_FINST_Q;
1230 else if (s == SV_STCONN)
1231 t->flags |= SN_FINST_C;
1232 else
1233 t->flags |= SN_FINST_D;
1234 }
1235 return 1;
1236 }
1237
1238 if (t->flags & SN_SELF_GEN) {
1239 produce_content(t);
1240 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001241 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001242 fd_delete(t->cli_fd);
1243 t->cli_state = CL_STCLOSE;
1244 return 1;
1245 }
1246 }
1247
1248 if ((rep->l == 0)
1249 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001250 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1251 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001252 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001253 }
1254 } else {
1255 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001256 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1257 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001258 if (t->fe->clitimeout) {
1259 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001260 /* FIXME: to prevent the client from expiring read timeouts during writes,
1261 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001262 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263 }
1264 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001265 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266 }
1267 }
1268 return 0;
1269 }
1270 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001271 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001272 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273 fd_delete(t->cli_fd);
1274 t->cli_state = CL_STCLOSE;
1275 if (!(t->flags & SN_ERR_MASK))
1276 t->flags |= SN_ERR_CLICL;
1277 if (!(t->flags & SN_FINST_MASK)) {
1278 if (t->pend_pos)
1279 t->flags |= SN_FINST_Q;
1280 else if (s == SV_STCONN)
1281 t->flags |= SN_FINST_C;
1282 else
1283 t->flags |= SN_FINST_D;
1284 }
1285 return 1;
1286 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001287 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001288 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001289 fd_delete(t->cli_fd);
1290 t->cli_state = CL_STCLOSE;
1291 return 1;
1292 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001293 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
1294 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001295 fd_delete(t->cli_fd);
1296 t->cli_state = CL_STCLOSE;
1297 if (!(t->flags & SN_ERR_MASK))
1298 t->flags |= SN_ERR_CLITO;
1299 if (!(t->flags & SN_FINST_MASK)) {
1300 if (t->pend_pos)
1301 t->flags |= SN_FINST_Q;
1302 else if (s == SV_STCONN)
1303 t->flags |= SN_FINST_C;
1304 else
1305 t->flags |= SN_FINST_D;
1306 }
1307 return 1;
1308 }
1309 else if (req->l >= req->rlim - req->data) {
1310 /* no room to read more data */
1311
1312 /* FIXME-20050705: is it possible for a client to maintain a session
1313 * after the timeout by sending more data after it receives a close ?
1314 */
1315
Willy Tarreau2a429502006-10-15 14:52:29 +02001316 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001317 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02001318 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001319 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001320 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1321 }
1322 } else {
1323 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02001324 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
1325 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001326 if (t->fe->clitimeout)
1327 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001328 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001329 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001330 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1331 }
1332 }
1333 return 0;
1334 }
1335 else { /* CL_STCLOSE: nothing to do */
1336 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1337 int len;
Willy Tarreau73de9892006-11-30 11:40:23 +01001338 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001339 write(1, trash, len);
1340 }
1341 return 0;
1342 }
1343 return 0;
1344}
1345
1346
1347/*
1348 * manages the server FSM and its socket. It returns 1 if a state has changed
1349 * (and a resync may be needed), 0 else.
1350 */
1351int process_srv(struct session *t)
1352{
1353 int s = t->srv_state;
1354 int c = t->cli_state;
1355 struct buffer *req = t->req;
1356 struct buffer *rep = t->rep;
1357 appsess *asession_temp = NULL;
1358 appsess local_asession;
1359 int conn_err;
1360
1361#ifdef DEBUG_FULL
1362 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
1363#endif
1364 //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 +02001365 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
1366 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001367 //);
1368 if (s == SV_STIDLE) {
1369 if (c == CL_STHEADERS)
1370 return 0; /* stay in idle, waiting for data to reach the client side */
1371 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
1372 (c == CL_STSHUTR &&
Willy Tarreau73de9892006-11-30 11:40:23 +01001373 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02001374 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001375 if (t->pend_pos)
1376 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1377 /* note that this must not return any error because it would be able to
1378 * overwrite the client_retnclose() output.
1379 */
Willy Tarreau08fa2e32006-09-03 10:47:37 +02001380 if (t->flags & SN_CLTARPIT)
1381 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, 0, NULL);
1382 else
1383 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001384
1385 return 1;
1386 }
1387 else {
Willy Tarreaub8750a82006-09-03 09:56:00 +02001388 if (t->flags & SN_CLTARPIT) {
1389 /* This connection is being tarpitted. The CLIENT side has
1390 * already set the connect expiration date to the right
1391 * timeout. We just have to check that it has not expired.
1392 */
1393 if (tv_cmp2_ms(&req->cex, &now) > 0)
1394 return 0;
1395
1396 /* We will set the queue timer to the time spent, just for
1397 * logging purposes. We fake a 500 server error, so that the
1398 * attacker will not suspect his connection has been tarpitted.
1399 * It will not cause trouble to the logs because we can exclude
1400 * the tarpitted connections by filtering on the 'PT' status flags.
1401 */
1402 tv_eternity(&req->cex);
1403 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1404 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau73de9892006-11-30 11:40:23 +01001405 500, t->fe->errmsg.len500, t->fe->errmsg.msg500);
Willy Tarreaub8750a82006-09-03 09:56:00 +02001406 return 1;
1407 }
1408
Willy Tarreaubaaee002006-06-26 02:48:02 +02001409 /* Right now, we will need to create a connection to the server.
1410 * We might already have tried, and got a connection pending, in
1411 * which case we will not do anything till it's pending. It's up
1412 * to any other session to release it and wake us up again.
1413 */
1414 if (t->pend_pos) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001415 if (tv_cmp2_ms(&req->cex, &now) > 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001416 return 0;
1417 else {
1418 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02001419 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001420 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1421 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau73de9892006-11-30 11:40:23 +01001422 503, t->fe->errmsg.len503, t->fe->errmsg.msg503);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001423 if (t->srv)
1424 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01001425 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001426 return 1;
1427 }
1428 }
1429
1430 do {
1431 /* first, get a connection */
1432 if (srv_redispatch_connect(t))
1433 return t->srv_state != SV_STIDLE;
1434
1435 /* try to (re-)connect to the server, and fail if we expire the
1436 * number of retries.
1437 */
1438 if (srv_retryable_connect(t)) {
1439 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1440 return t->srv_state != SV_STIDLE;
1441 }
1442
1443 } while (1);
1444 }
1445 }
1446 else if (s == SV_STCONN) { /* connection in progress */
1447 if (c == CL_STCLOSE || c == CL_STSHUTW ||
1448 (c == CL_STSHUTR &&
Willy Tarreau73de9892006-11-30 11:40:23 +01001449 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02001450 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001451 fd_delete(t->srv_fd);
1452 if (t->srv)
1453 t->srv->cur_sess--;
1454
1455 /* note that this must not return any error because it would be able to
1456 * overwrite the client_retnclose() output.
1457 */
1458 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, 0, NULL);
1459 return 1;
1460 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001461 if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) {
1462 //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 +02001463 return 0; /* nothing changed */
1464 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001465 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001466 /* timeout, asynchronous connect error or first write error */
1467 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
1468
1469 fd_delete(t->srv_fd);
1470 if (t->srv)
1471 t->srv->cur_sess--;
1472
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001473 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001474 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
1475 else
1476 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
1477
1478 /* ensure that we have enough retries left */
1479 if (srv_count_retry_down(t, conn_err))
1480 return 1;
1481
Willy Tarreau73de9892006-11-30 11:40:23 +01001482 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02001483 /* We're on our last chance, and the REDISP option was specified.
1484 * We will ignore cookie and force to balance or use the dispatcher.
1485 */
1486 /* let's try to offer this slot to anybody */
Willy Tarreau73de9892006-11-30 11:40:23 +01001487 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02001488 task_wakeup(&rq, t->srv->queue_mgt);
1489
1490 if (t->srv)
1491 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01001492 t->be->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02001493
1494 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
1495 t->srv = NULL; /* it's left to the dispatcher to choose a server */
1496 if ((t->flags & SN_CK_MASK) == SN_CK_VALID) {
1497 t->flags &= ~SN_CK_MASK;
1498 t->flags |= SN_CK_DOWN;
1499 }
1500
1501 /* first, get a connection */
1502 if (srv_redispatch_connect(t))
1503 return t->srv_state != SV_STIDLE;
1504 }
1505
Willy Tarreaubaaee002006-06-26 02:48:02 +02001506 do {
1507 /* Now we will try to either reconnect to the same server or
1508 * connect to another server. If the connection gets queued
1509 * because all servers are saturated, then we will go back to
1510 * the SV_STIDLE state.
1511 */
1512 if (srv_retryable_connect(t)) {
1513 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1514 return t->srv_state != SV_STCONN;
1515 }
1516
1517 /* we need to redispatch the connection to another server */
1518 if (srv_redispatch_connect(t))
1519 return t->srv_state != SV_STCONN;
1520 } while (1);
1521 }
1522 else { /* no error or write 0 */
1523 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
1524
1525 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
1526 if (req->l == 0) /* nothing to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02001527 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001528 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001529 } else /* need the right to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02001530 MY_FD_SET(t->srv_fd, StaticWriteEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001531 if (t->be->srvtimeout) {
1532 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001533 /* FIXME: to prevent the server from expiring read timeouts during writes,
1534 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001535 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001536 }
1537 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001538 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001539 }
1540
Willy Tarreau73de9892006-11-30 11:40:23 +01001541 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau2a429502006-10-15 14:52:29 +02001542 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001543 if (t->be->srvtimeout)
1544 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001545 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001546 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001547
1548 t->srv_state = SV_STDATA;
1549 if (t->srv)
1550 t->srv->cum_sess++;
1551 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
1552
1553 /* if the user wants to log as soon as possible, without counting
1554 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01001555 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001556 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
1557 sess_log(t);
1558 }
1559 }
1560 else {
1561 t->srv_state = SV_STHEADERS;
1562 if (t->srv)
1563 t->srv->cum_sess++;
1564 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
1565 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001566 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001567 return 1;
1568 }
1569 }
1570 else if (s == SV_STHEADERS) { /* receiving server headers */
1571 /* now parse the partial (or complete) headers */
1572 while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */
1573 char *ptr;
1574 int delete_header;
1575
1576 ptr = rep->lr;
1577
1578 /* look for the end of the current header */
1579 while (ptr < rep->r && *ptr != '\n' && *ptr != '\r')
1580 ptr++;
1581
1582 if (ptr == rep->h) {
1583 int line, len;
1584
1585 /* we can only get here after an end of headers */
1586
1587 /* first, we'll block if security checks have caught nasty things */
1588 if (t->flags & SN_CACHEABLE) {
1589 if ((t->flags & SN_CACHE_COOK) &&
1590 (t->flags & SN_SCK_ANY) &&
Willy Tarreau73de9892006-11-30 11:40:23 +01001591 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001592
1593 /* we're in presence of a cacheable response containing
1594 * a set-cookie header. We'll block it as requested by
1595 * the 'checkcache' option, and send an alert.
1596 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001597 tv_eternity(&rep->rex);
1598 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001599 fd_delete(t->srv_fd);
1600 if (t->srv) {
1601 t->srv->cur_sess--;
1602 t->srv->failed_secu++;
1603 }
Willy Tarreau73de9892006-11-30 11:40:23 +01001604 t->be->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001605 t->srv_state = SV_STCLOSE;
1606 t->logs.status = 502;
Willy Tarreau73de9892006-11-30 11:40:23 +01001607 client_return(t, t->fe->errmsg.len502, t->fe->errmsg.msg502);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001608 if (!(t->flags & SN_ERR_MASK))
1609 t->flags |= SN_ERR_PRXCOND;
1610 if (!(t->flags & SN_FINST_MASK))
1611 t->flags |= SN_FINST_H;
1612
Willy Tarreau73de9892006-11-30 11:40:23 +01001613 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", t->be->id, t->srv->id);
1614 send_log(t->be, LOG_ALERT, "Blocking cacheable cookie in response from instance %s, server %s.\n", t->be->id, t->srv->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001615
1616 /* We used to have a free connection slot. Since we'll never use it,
1617 * we have to inform the server that it may be used by another session.
1618 */
Willy Tarreau73de9892006-11-30 11:40:23 +01001619 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001620 task_wakeup(&rq, t->srv->queue_mgt);
1621
1622 return 1;
1623 }
1624 }
1625
1626 /* next, we'll block if an 'rspideny' or 'rspdeny' filter matched */
1627 if (t->flags & SN_SVDENY) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001628 tv_eternity(&rep->rex);
1629 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001630 fd_delete(t->srv_fd);
1631 if (t->srv) {
1632 t->srv->cur_sess--;
1633 t->srv->failed_secu++;
1634 }
Willy Tarreau73de9892006-11-30 11:40:23 +01001635 t->be->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001636 t->srv_state = SV_STCLOSE;
1637 t->logs.status = 502;
Willy Tarreau73de9892006-11-30 11:40:23 +01001638 client_return(t, t->fe->errmsg.len502, t->fe->errmsg.msg502);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001639 if (!(t->flags & SN_ERR_MASK))
1640 t->flags |= SN_ERR_PRXCOND;
1641 if (!(t->flags & SN_FINST_MASK))
1642 t->flags |= SN_FINST_H;
1643 /* We used to have a free connection slot. Since we'll never use it,
1644 * we have to inform the server that it may be used by another session.
1645 */
Willy Tarreau73de9892006-11-30 11:40:23 +01001646 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001647 task_wakeup(&rq, t->srv->queue_mgt);
1648
1649 return 1;
1650 }
1651
1652 /* we'll have something else to do here : add new headers ... */
1653
Willy Tarreau73de9892006-11-30 11:40:23 +01001654 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
1655 (!(t->be->options & PR_O_COOK_POST) || (t->flags & SN_POST))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001656 /* the server is known, it's not the one the client requested, we have to
1657 * insert a set-cookie here, except if we want to insert only on POST
1658 * requests and this one isn't. Note that servers which don't have cookies
1659 * (eg: some backup servers) will return a full cookie removal request.
1660 */
1661 len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n",
Willy Tarreau73de9892006-11-30 11:40:23 +01001662 t->be->cookie_name,
Willy Tarreaubaaee002006-06-26 02:48:02 +02001663 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
1664
1665 t->flags |= SN_SCK_INSERTED;
1666
1667 /* Here, we will tell an eventual cache on the client side that we don't
1668 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1669 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1670 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1671 */
Willy Tarreau73de9892006-11-30 11:40:23 +01001672 if (t->be->options & PR_O_COOK_NOC)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001673 //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n");
1674 len += sprintf(trash + len, "Cache-control: private\r\n");
1675
1676 if (rep->data + rep->l < rep->h)
1677 /* The data has been stolen, we will crash cleanly instead of corrupting memory */
1678 *(int *)0 = 0;
1679 buffer_replace2(rep, rep->h, rep->h, trash, len);
1680 }
1681
1682 /* headers to be added */
Willy Tarreau73de9892006-11-30 11:40:23 +01001683 for (line = 0; line < t->fi->nb_rspadd; line++) {
1684 len = sprintf(trash, "%s\r\n", t->fi->rsp_add[line]);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001685 buffer_replace2(rep, rep->h, rep->h, trash, len);
1686 }
1687
1688 /* add a "connection: close" line if needed */
Willy Tarreau73de9892006-11-30 11:40:23 +01001689 if (t->fe->options & PR_O_HTTP_CLOSE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001690 buffer_replace2(rep, rep->h, rep->h, "Connection: close\r\n", 19);
1691
1692 t->srv_state = SV_STDATA;
1693 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
1694 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
1695
1696 /* client connection already closed or option 'httpclose' required :
1697 * we close the server's outgoing connection right now.
1698 */
1699 if ((req->l == 0) &&
Willy Tarreau73de9892006-11-30 11:40:23 +01001700 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001701 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001702 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001703
1704 /* We must ensure that the read part is still alive when switching
1705 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001706 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001707 if (t->be->srvtimeout)
1708 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001709
1710 shutdown(t->srv_fd, SHUT_WR);
1711 t->srv_state = SV_STSHUTW;
1712 }
1713
1714 /* if the user wants to log as soon as possible, without counting
1715 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01001716 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001717 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
1718 t->logs.bytes = rep->h - rep->data;
1719 sess_log(t);
1720 }
1721 break;
1722 }
1723
1724 /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */
1725 if (ptr > rep->r - 2) {
1726 /* this is a partial header, let's wait for more to come */
1727 rep->lr = ptr;
1728 break;
1729 }
1730
1731 // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r);
1732 // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n");
1733
1734 /* now we know that *ptr is either \r or \n,
1735 * and that there are at least 1 char after it.
1736 */
1737 if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n'))
1738 rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */
1739 else
1740 rep->lr = ptr + 2; /* \r\n or \n\r */
1741
1742 /*
1743 * now we know that we have a full header ; we can do whatever
1744 * we want with these pointers :
1745 * rep->h = beginning of header
1746 * ptr = end of header (first \r or \n)
1747 * rep->lr = beginning of next line (next rep->h)
1748 * rep->r = end of data (not used at this stage)
1749 */
1750
1751
1752 if (t->logs.status == -1) {
1753 t->logs.logwait &= ~LW_RESP;
1754 t->logs.status = atoi(rep->h + 9);
1755 switch (t->logs.status) {
1756 case 200:
1757 case 203:
1758 case 206:
1759 case 300:
1760 case 301:
1761 case 410:
1762 /* RFC2616 @13.4:
1763 * "A response received with a status code of
1764 * 200, 203, 206, 300, 301 or 410 MAY be stored
1765 * by a cache (...) unless a cache-control
1766 * directive prohibits caching."
1767 *
1768 * RFC2616 @9.5: POST method :
1769 * "Responses to this method are not cacheable,
1770 * unless the response includes appropriate
1771 * Cache-Control or Expires header fields."
1772 */
Willy Tarreau73de9892006-11-30 11:40:23 +01001773 if (!(t->flags & SN_POST) && (t->be->options & PR_O_CHK_CACHE))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001774 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
1775 break;
1776 default:
1777 break;
1778 }
1779 }
1780 else if (t->logs.logwait & LW_RSPHDR) {
1781 struct cap_hdr *h;
1782 int len;
Willy Tarreau73de9892006-11-30 11:40:23 +01001783 for (h = t->fi->rsp_cap; h; h = h->next) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001784 if ((h->namelen + 2 <= ptr - rep->h) &&
1785 (rep->h[h->namelen] == ':') &&
1786 (strncasecmp(rep->h, h->name, h->namelen) == 0)) {
1787
1788 if (t->rsp_cap[h->index] == NULL)
1789 t->rsp_cap[h->index] = pool_alloc_from(h->pool, h->len + 1);
1790
1791 len = ptr - (rep->h + h->namelen + 2);
1792 if (len > h->len)
1793 len = h->len;
1794
1795 memcpy(t->rsp_cap[h->index], rep->h + h->namelen + 2, len);
1796 t->rsp_cap[h->index][len]=0;
1797 }
1798 }
1799
1800 }
1801
1802 delete_header = 0;
1803
1804 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1805 int len, max;
Willy Tarreau73de9892006-11-30 11:40:23 +01001806 len = sprintf(trash, "%08x:%s.srvhdr[%04x:%04x]: ", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001807 max = ptr - rep->h;
1808 UBOUND(max, sizeof(trash) - len - 1);
1809 len += strlcpy2(trash + len, rep->h, max + 1);
1810 trash[len++] = '\n';
1811 write(1, trash, len);
1812 }
1813
1814 /* remove "connection: " if needed */
Willy Tarreau73de9892006-11-30 11:40:23 +01001815 if (!delete_header && (t->fe->options & PR_O_HTTP_CLOSE)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001816 && (strncasecmp(rep->h, "Connection: ", 12) == 0)) {
1817 delete_header = 1;
1818 }
1819
1820 /* try headers regexps */
Willy Tarreau73de9892006-11-30 11:40:23 +01001821 if (!delete_header && t->fi->rsp_exp != NULL
Willy Tarreaubaaee002006-06-26 02:48:02 +02001822 && !(t->flags & SN_SVDENY)) {
1823 struct hdr_exp *exp;
1824 char term;
1825
1826 term = *ptr;
1827 *ptr = '\0';
Willy Tarreau73de9892006-11-30 11:40:23 +01001828 exp = t->fi->rsp_exp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001829 do {
1830 if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) {
1831 switch (exp->action) {
1832 case ACT_ALLOW:
1833 if (!(t->flags & SN_SVDENY))
1834 t->flags |= SN_SVALLOW;
1835 break;
1836 case ACT_REPLACE:
1837 if (!(t->flags & SN_SVDENY)) {
1838 int len = exp_replace(trash, rep->h, exp->replace, pmatch);
1839 ptr += buffer_replace2(rep, rep->h, ptr, trash, len);
1840 }
1841 break;
1842 case ACT_REMOVE:
1843 if (!(t->flags & SN_SVDENY))
1844 delete_header = 1;
1845 break;
1846 case ACT_DENY:
1847 if (!(t->flags & SN_SVALLOW))
1848 t->flags |= SN_SVDENY;
1849 break;
1850 case ACT_PASS: /* we simply don't deny this one */
1851 break;
1852 }
1853 break;
1854 }
1855 } while ((exp = exp->next) != NULL);
1856 *ptr = term; /* restore the string terminator */
1857 }
1858
1859 /* check for cache-control: or pragma: headers */
1860 if (!delete_header && (t->flags & SN_CACHEABLE)) {
1861 if (strncasecmp(rep->h, "Pragma: no-cache", 16) == 0)
1862 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1863 else if (strncasecmp(rep->h, "Cache-control: ", 15) == 0) {
1864 if (strncasecmp(rep->h + 15, "no-cache", 8) == 0) {
1865 if (rep->h + 23 == ptr || rep->h[23] == ',')
1866 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1867 else {
1868 if (strncasecmp(rep->h + 23, "=\"set-cookie", 12) == 0
1869 && (rep->h[35] == '"' || rep->h[35] == ','))
1870 t->flags &= ~SN_CACHE_COOK;
1871 }
1872 } else if ((strncasecmp(rep->h + 15, "private", 7) == 0 &&
1873 (rep->h + 22 == ptr || rep->h[22] == ','))
1874 || (strncasecmp(rep->h + 15, "no-store", 8) == 0 &&
1875 (rep->h + 23 == ptr || rep->h[23] == ','))) {
1876 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1877 } else if (strncasecmp(rep->h + 15, "max-age=0", 9) == 0 &&
1878 (rep->h + 24 == ptr || rep->h[24] == ',')) {
1879 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1880 } else if (strncasecmp(rep->h + 15, "s-maxage=0", 10) == 0 &&
1881 (rep->h + 25 == ptr || rep->h[25] == ',')) {
1882 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1883 } else if (strncasecmp(rep->h + 15, "public", 6) == 0 &&
1884 (rep->h + 21 == ptr || rep->h[21] == ',')) {
1885 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
1886 }
1887 }
1888 }
1889
1890 /* check for server cookies */
1891 if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/
Willy Tarreau73de9892006-11-30 11:40:23 +01001892 && (t->be->cookie_name != NULL || t->fi->capture_name != NULL || t->be->appsession_name !=NULL)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001893 && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) {
1894 char *p1, *p2, *p3, *p4;
1895
1896 t->flags |= SN_SCK_ANY;
1897
1898 p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */
1899
1900 while (p1 < ptr) { /* in fact, we'll break after the first cookie */
1901 while (p1 < ptr && (isspace((int)*p1)))
1902 p1++;
1903
1904 if (p1 == ptr || *p1 == ';') /* end of cookie */
1905 break;
1906
1907 /* p1 is at the beginning of the cookie name */
1908 p2 = p1;
1909
1910 while (p2 < ptr && *p2 != '=' && *p2 != ';')
1911 p2++;
1912
1913 if (p2 == ptr || *p2 == ';') /* next cookie */
1914 break;
1915
1916 p3 = p2 + 1; /* skips the '=' sign */
1917 if (p3 == ptr)
1918 break;
1919
1920 p4 = p3;
1921 while (p4 < ptr && !isspace((int)*p4) && *p4 != ';')
1922 p4++;
1923
1924 /* here, we have the cookie name between p1 and p2,
1925 * and its value between p3 and p4.
1926 * we can process it.
1927 */
1928
1929 /* first, let's see if we want to capture it */
Willy Tarreau73de9892006-11-30 11:40:23 +01001930 if (t->fi->capture_name != NULL &&
Willy Tarreaubaaee002006-06-26 02:48:02 +02001931 t->logs.srv_cookie == NULL &&
Willy Tarreau73de9892006-11-30 11:40:23 +01001932 (p4 - p1 >= t->fi->capture_namelen) &&
1933 memcmp(p1, t->fi->capture_name, t->fi->capture_namelen) == 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001934 int log_len = p4 - p1;
1935
1936 if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) {
1937 Alert("HTTP logging : out of memory.\n");
1938 }
1939
Willy Tarreau73de9892006-11-30 11:40:23 +01001940 if (log_len > t->fi->capture_len)
1941 log_len = t->fi->capture_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001942 memcpy(t->logs.srv_cookie, p1, log_len);
1943 t->logs.srv_cookie[log_len] = 0;
1944 }
1945
Willy Tarreau73de9892006-11-30 11:40:23 +01001946 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
1947 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001948 /* Cool... it's the right one */
1949 t->flags |= SN_SCK_SEEN;
1950
1951 /* If the cookie is in insert mode on a known server, we'll delete
1952 * this occurrence because we'll insert another one later.
1953 * We'll delete it too if the "indirect" option is set and we're in
1954 * a direct access. */
Willy Tarreau73de9892006-11-30 11:40:23 +01001955 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
1956 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001957 /* this header must be deleted */
1958 delete_header = 1;
1959 t->flags |= SN_SCK_DELETED;
1960 }
Willy Tarreau73de9892006-11-30 11:40:23 +01001961 else if ((t->srv) && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001962 /* replace bytes p3->p4 with the cookie name associated
1963 * with this server since we know it.
1964 */
1965 buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen);
1966 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
1967 }
Willy Tarreau73de9892006-11-30 11:40:23 +01001968 else if ((t->srv) && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001969 /* insert the cookie name associated with this server
1970 * before existing cookie, and insert a delimitor between them..
1971 */
1972 buffer_replace2(rep, p3, p3, t->srv->cookie, t->srv->cklen + 1);
1973 p3[t->srv->cklen] = COOKIE_DELIM;
1974 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
1975 }
1976 break;
1977 }
1978
1979 /* first, let's see if the cookie is our appcookie*/
Willy Tarreau73de9892006-11-30 11:40:23 +01001980 if ((t->be->appsession_name != NULL) &&
1981 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001982
1983 /* Cool... it's the right one */
1984
1985 size_t server_id_len = strlen(t->srv->id) + 1;
1986 asession_temp = &local_asession;
1987
1988 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
1989 Alert("Not enought Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreau73de9892006-11-30 11:40:23 +01001990 send_log(t->be, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001991 }
Willy Tarreau73de9892006-11-30 11:40:23 +01001992 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
1993 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001994 asession_temp->serverid = NULL;
1995
1996 /* only do insert, if lookup fails */
Willy Tarreau73de9892006-11-30 11:40:23 +01001997 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001998 if ((asession_temp = pool_alloc(appsess)) == NULL) {
1999 Alert("Not enought Memory process_srv():asession:calloc().\n");
Willy Tarreau73de9892006-11-30 11:40:23 +01002000 send_log(t->be, LOG_ALERT, "Not enought Memory process_srv():asession:calloc().\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002001 return 0;
2002 }
2003 asession_temp->sessid = local_asession.sessid;
2004 asession_temp->serverid = local_asession.serverid;
Willy Tarreau73de9892006-11-30 11:40:23 +01002005 chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002006 }/* end if (chtbl_lookup()) */
2007 else {
2008 /* free wasted memory */
2009 pool_free_to(apools.sessid, local_asession.sessid);
2010 } /* end else from if (chtbl_lookup()) */
2011
2012 if (asession_temp->serverid == NULL) {
2013 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
2014 Alert("Not enought Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreau73de9892006-11-30 11:40:23 +01002015 send_log(t->be, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002016 }
2017 asession_temp->serverid[0] = '\0';
2018 }
2019
2020 if (asession_temp->serverid[0] == '\0')
2021 memcpy(asession_temp->serverid,t->srv->id,server_id_len);
2022
Willy Tarreau73de9892006-11-30 11:40:23 +01002023 tv_delayfrom(&asession_temp->expire, &now, t->be->appsession_timeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002024
2025#if defined(DEBUG_HASH)
Willy Tarreau73de9892006-11-30 11:40:23 +01002026 print_table(&(t->be->htbl_proxy));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002027#endif
2028 break;
2029 }/* end if ((t->proxy->appsession_name != NULL) ... */
2030 else {
2031 // fprintf(stderr,"Ignoring unknown cookie : ");
2032 // write(2, p1, p2-p1);
2033 // fprintf(stderr," = ");
2034 // write(2, p3, p4-p3);
2035 // fprintf(stderr,"\n");
2036 }
2037 break; /* we don't want to loop again since there cannot be another cookie on the same line */
2038 } /* we're now at the end of the cookie value */
2039 } /* end of cookie processing */
2040
2041 /* check for any set-cookie in case we check for cacheability */
2042 if (!delete_header && !(t->flags & SN_SCK_ANY) &&
Willy Tarreau73de9892006-11-30 11:40:23 +01002043 (t->be->options & PR_O_CHK_CACHE) &&
Willy Tarreaubaaee002006-06-26 02:48:02 +02002044 (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) {
2045 t->flags |= SN_SCK_ANY;
2046 }
2047
2048 /* let's look if we have to delete this header */
2049 if (delete_header && !(t->flags & SN_SVDENY))
2050 buffer_replace2(rep, rep->h, rep->lr, "", 0);
2051
2052 rep->h = rep->lr;
2053 } /* while (rep->lr < rep->r) */
2054
2055 /* end of header processing (even if incomplete) */
2056
Willy Tarreau2a429502006-10-15 14:52:29 +02002057 if ((rep->l < rep->rlim - rep->data) && ! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002058 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
Willy Tarreaud7971282006-07-29 18:36:34 +02002059 * full. We cannot loop here since stream_sock_read will disable it only if
Willy Tarreaubaaee002006-06-26 02:48:02 +02002060 * rep->l == rlim-data
2061 */
Willy Tarreau2a429502006-10-15 14:52:29 +02002062 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002063 if (t->be->srvtimeout)
2064 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002065 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002066 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002067 }
2068
2069 /* read error, write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002070 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002071 tv_eternity(&rep->rex);
2072 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002073 fd_delete(t->srv_fd);
2074 if (t->srv) {
2075 t->srv->cur_sess--;
2076 t->srv->failed_resp++;
2077 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002078 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002079
2080 t->srv_state = SV_STCLOSE;
2081 t->logs.status = 502;
Willy Tarreau73de9892006-11-30 11:40:23 +01002082 client_return(t, t->fe->errmsg.len502, t->fe->errmsg.msg502);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002083 if (!(t->flags & SN_ERR_MASK))
2084 t->flags |= SN_ERR_SRVCL;
2085 if (!(t->flags & SN_FINST_MASK))
2086 t->flags |= SN_FINST_H;
2087 /* We used to have a free connection slot. Since we'll never use it,
2088 * we have to inform the server that it may be used by another session.
2089 */
Willy Tarreau73de9892006-11-30 11:40:23 +01002090 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002091 task_wakeup(&rq, t->srv->queue_mgt);
2092
2093 return 1;
2094 }
2095 /* end of client write or end of server read.
2096 * since we are in header mode, if there's no space left for headers, we
2097 * won't be able to free more later, so the session will never terminate.
2098 */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002099 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE || rep->l >= rep->rlim - rep->data) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002100 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002101 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002102 shutdown(t->srv_fd, SHUT_RD);
2103 t->srv_state = SV_STSHUTR;
2104 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2105 return 1;
2106 }
2107 /* read timeout : return a 504 to the client.
2108 */
Willy Tarreau2a429502006-10-15 14:52:29 +02002109 else if (MY_FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002110 tv_eternity(&rep->rex);
2111 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002112 fd_delete(t->srv_fd);
2113 if (t->srv) {
2114 t->srv->cur_sess--;
2115 t->srv->failed_resp++;
2116 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002117 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002118 t->srv_state = SV_STCLOSE;
2119 t->logs.status = 504;
Willy Tarreau73de9892006-11-30 11:40:23 +01002120 client_return(t, t->fe->errmsg.len504, t->fe->errmsg.msg504);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002121 if (!(t->flags & SN_ERR_MASK))
2122 t->flags |= SN_ERR_SRVTO;
2123 if (!(t->flags & SN_FINST_MASK))
2124 t->flags |= SN_FINST_H;
2125 /* We used to have a free connection slot. Since we'll never use it,
2126 * we have to inform the server that it may be used by another session.
2127 */
Willy Tarreau73de9892006-11-30 11:40:23 +01002128 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002129 task_wakeup(&rq, t->srv->queue_mgt);
2130
2131 return 1;
2132 }
2133 /* last client read and buffer empty */
2134 /* FIXME!!! here, we don't want to switch to SHUTW if the
2135 * client shuts read too early, because we may still have
2136 * some work to do on the headers.
2137 * The side-effect is that if the client completely closes its
2138 * connection during SV_STHEADER, the connection to the server
2139 * is kept until a response comes back or the timeout is reached.
2140 */
2141 else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002142 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002143 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002144
2145 /* We must ensure that the read part is still alive when switching
2146 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002147 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002148 if (t->be->srvtimeout)
2149 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002150
2151 shutdown(t->srv_fd, SHUT_WR);
2152 t->srv_state = SV_STSHUTW;
2153 return 1;
2154 }
2155 /* write timeout */
2156 /* FIXME!!! here, we don't want to switch to SHUTW if the
2157 * client shuts read too early, because we may still have
2158 * some work to do on the headers.
2159 */
Willy Tarreau2a429502006-10-15 14:52:29 +02002160 else if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&req->wex, &now) <= 0) {
2161 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002162 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002163 shutdown(t->srv_fd, SHUT_WR);
2164 /* We must ensure that the read part is still alive when switching
2165 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002166 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002167 if (t->be->srvtimeout)
2168 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002169
2170 /* We must ensure that the read part is still alive when switching
2171 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002172 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002173 if (t->be->srvtimeout)
2174 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002175
2176 t->srv_state = SV_STSHUTW;
2177 if (!(t->flags & SN_ERR_MASK))
2178 t->flags |= SN_ERR_SRVTO;
2179 if (!(t->flags & SN_FINST_MASK))
2180 t->flags |= SN_FINST_H;
2181 return 1;
2182 }
2183
2184 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002185 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2186 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002187 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002188 }
2189 }
2190 else { /* client buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002191 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2192 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01002193 if (t->be->srvtimeout) {
2194 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002195 /* FIXME: to prevent the server from expiring read timeouts during writes,
2196 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002197 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 }
2199 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002200 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002201 }
2202 }
2203
2204 /* be nice with the client side which would like to send a complete header
2205 * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client
2206 * would read all remaining data at once ! The client should not write past rep->lr
2207 * when the server is in header state.
2208 */
2209 //return header_processed;
2210 return t->srv_state != SV_STHEADERS;
2211 }
2212 else if (s == SV_STDATA) {
2213 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002214 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002215 tv_eternity(&rep->rex);
2216 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002217 fd_delete(t->srv_fd);
2218 if (t->srv) {
2219 t->srv->cur_sess--;
2220 t->srv->failed_resp++;
2221 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002222 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002223 t->srv_state = SV_STCLOSE;
2224 if (!(t->flags & SN_ERR_MASK))
2225 t->flags |= SN_ERR_SRVCL;
2226 if (!(t->flags & SN_FINST_MASK))
2227 t->flags |= SN_FINST_D;
2228 /* We used to have a free connection slot. Since we'll never use it,
2229 * we have to inform the server that it may be used by another session.
2230 */
Willy Tarreau73de9892006-11-30 11:40:23 +01002231 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002232 task_wakeup(&rq, t->srv->queue_mgt);
2233
2234 return 1;
2235 }
2236 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002237 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002238 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002239 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002240 shutdown(t->srv_fd, SHUT_RD);
2241 t->srv_state = SV_STSHUTR;
2242 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2243 return 1;
2244 }
2245 /* end of client read and no more data to send */
2246 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002247 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002248 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002249 shutdown(t->srv_fd, SHUT_WR);
2250 /* We must ensure that the read part is still alive when switching
2251 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002252 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002253 if (t->be->srvtimeout)
2254 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002255
2256 t->srv_state = SV_STSHUTW;
2257 return 1;
2258 }
2259 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002260 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002261 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002262 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002263 shutdown(t->srv_fd, SHUT_RD);
2264 t->srv_state = SV_STSHUTR;
2265 if (!(t->flags & SN_ERR_MASK))
2266 t->flags |= SN_ERR_SRVTO;
2267 if (!(t->flags & SN_FINST_MASK))
2268 t->flags |= SN_FINST_D;
2269 return 1;
2270 }
2271 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002272 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002273 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002274 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002275 shutdown(t->srv_fd, SHUT_WR);
2276 /* We must ensure that the read part is still alive when switching
2277 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002278 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002279 if (t->be->srvtimeout)
2280 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002281 t->srv_state = SV_STSHUTW;
2282 if (!(t->flags & SN_ERR_MASK))
2283 t->flags |= SN_ERR_SRVTO;
2284 if (!(t->flags & SN_FINST_MASK))
2285 t->flags |= SN_FINST_D;
2286 return 1;
2287 }
2288
2289 /* recompute request time-outs */
2290 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002291 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2292 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002293 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002294 }
2295 }
2296 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau2a429502006-10-15 14:52:29 +02002297 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2298 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01002299 if (t->be->srvtimeout) {
2300 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002301 /* FIXME: to prevent the server from expiring read timeouts during writes,
2302 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002303 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002304 }
2305 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002306 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002307 }
2308 }
2309
2310 /* recompute response time-outs */
2311 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002312 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2313 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002314 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002315 }
2316 }
2317 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02002318 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2319 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002320 if (t->be->srvtimeout)
2321 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002322 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002323 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002324 }
2325 }
2326
2327 return 0; /* other cases change nothing */
2328 }
2329 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002330 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002331 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002332 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002333 fd_delete(t->srv_fd);
2334 if (t->srv) {
2335 t->srv->cur_sess--;
2336 t->srv->failed_resp++;
2337 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002338 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002339 //close(t->srv_fd);
2340 t->srv_state = SV_STCLOSE;
2341 if (!(t->flags & SN_ERR_MASK))
2342 t->flags |= SN_ERR_SRVCL;
2343 if (!(t->flags & SN_FINST_MASK))
2344 t->flags |= SN_FINST_D;
2345 /* We used to have a free connection slot. Since we'll never use it,
2346 * we have to inform the server that it may be used by another session.
2347 */
Willy Tarreau73de9892006-11-30 11:40:23 +01002348 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002349 task_wakeup(&rq, t->srv->queue_mgt);
2350
2351 return 1;
2352 }
2353 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002354 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002355 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002356 fd_delete(t->srv_fd);
2357 if (t->srv)
2358 t->srv->cur_sess--;
2359 //close(t->srv_fd);
2360 t->srv_state = SV_STCLOSE;
2361 /* We used to have a free connection slot. Since we'll never use it,
2362 * we have to inform the server that it may be used by another session.
2363 */
Willy Tarreau73de9892006-11-30 11:40:23 +01002364 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002365 task_wakeup(&rq, t->srv->queue_mgt);
2366
2367 return 1;
2368 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002369 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002370 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002371 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002372 fd_delete(t->srv_fd);
2373 if (t->srv)
2374 t->srv->cur_sess--;
2375 //close(t->srv_fd);
2376 t->srv_state = SV_STCLOSE;
2377 if (!(t->flags & SN_ERR_MASK))
2378 t->flags |= SN_ERR_SRVTO;
2379 if (!(t->flags & SN_FINST_MASK))
2380 t->flags |= SN_FINST_D;
2381 /* We used to have a free connection slot. Since we'll never use it,
2382 * we have to inform the server that it may be used by another session.
2383 */
Willy Tarreau73de9892006-11-30 11:40:23 +01002384 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002385 task_wakeup(&rq, t->srv->queue_mgt);
2386
2387 return 1;
2388 }
2389 else if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002390 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2391 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002392 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002393 }
2394 }
2395 else { /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002396 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2397 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01002398 if (t->be->srvtimeout) {
2399 tv_delayfrom(&req->wex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002400 /* FIXME: to prevent the server from expiring read timeouts during writes,
2401 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002402 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002403 }
2404 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002405 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002406 }
2407 }
2408 return 0;
2409 }
2410 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002411 if (rep->flags & BF_READ_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002412 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002413 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002414 fd_delete(t->srv_fd);
2415 if (t->srv) {
2416 t->srv->cur_sess--;
2417 t->srv->failed_resp++;
2418 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002419 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002420 //close(t->srv_fd);
2421 t->srv_state = SV_STCLOSE;
2422 if (!(t->flags & SN_ERR_MASK))
2423 t->flags |= SN_ERR_SRVCL;
2424 if (!(t->flags & SN_FINST_MASK))
2425 t->flags |= SN_FINST_D;
2426 /* We used to have a free connection slot. Since we'll never use it,
2427 * we have to inform the server that it may be used by another session.
2428 */
Willy Tarreau73de9892006-11-30 11:40:23 +01002429 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002430 task_wakeup(&rq, t->srv->queue_mgt);
2431
2432 return 1;
2433 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002434 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002435 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002436 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002437 fd_delete(t->srv_fd);
2438 if (t->srv)
2439 t->srv->cur_sess--;
2440 //close(t->srv_fd);
2441 t->srv_state = SV_STCLOSE;
2442 /* We used to have a free connection slot. Since we'll never use it,
2443 * we have to inform the server that it may be used by another session.
2444 */
Willy Tarreau73de9892006-11-30 11:40:23 +01002445 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002446 task_wakeup(&rq, t->srv->queue_mgt);
2447
2448 return 1;
2449 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002450 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002451 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002452 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002453 fd_delete(t->srv_fd);
2454 if (t->srv)
2455 t->srv->cur_sess--;
2456 //close(t->srv_fd);
2457 t->srv_state = SV_STCLOSE;
2458 if (!(t->flags & SN_ERR_MASK))
2459 t->flags |= SN_ERR_SRVTO;
2460 if (!(t->flags & SN_FINST_MASK))
2461 t->flags |= SN_FINST_D;
2462 /* We used to have a free connection slot. Since we'll never use it,
2463 * we have to inform the server that it may be used by another session.
2464 */
Willy Tarreau73de9892006-11-30 11:40:23 +01002465 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002466 task_wakeup(&rq, t->srv->queue_mgt);
2467
2468 return 1;
2469 }
2470 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002471 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2472 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002473 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002474 }
2475 }
2476 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02002477 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2478 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002479 if (t->be->srvtimeout)
2480 tv_delayfrom(&rep->rex, &now, t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002481 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002482 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002483 }
2484 }
2485 return 0;
2486 }
2487 else { /* SV_STCLOSE : nothing to do */
2488 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2489 int len;
Willy Tarreau73de9892006-11-30 11:40:23 +01002490 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002491 write(1, trash, len);
2492 }
2493 return 0;
2494 }
2495 return 0;
2496}
2497
2498
2499/*
2500 * Produces data for the session <s> depending on its source. Expects to be
2501 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
2502 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
2503 * session, which it uses to keep on being called when there is free space in
2504 * the buffer, of simply by letting an empty buffer upon return. It returns 1
2505 * if it changes the session state from CL_STSHUTR, otherwise 0.
2506 */
2507int produce_content(struct session *s)
2508{
2509 struct buffer *rep = s->rep;
2510 struct proxy *px;
2511 struct server *sv;
2512 int msglen;
2513
2514 if (s->data_source == DATA_SRC_NONE) {
2515 s->flags &= ~SN_SELF_GEN;
2516 return 1;
2517 }
2518 else if (s->data_source == DATA_SRC_STATS) {
2519 msglen = 0;
2520
2521 if (s->data_state == DATA_ST_INIT) { /* the function had not been called yet */
2522 unsigned int up;
2523
2524 s->flags |= SN_SELF_GEN; // more data will follow
2525
2526 /* send the start of the HTTP response */
2527 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2528 "HTTP/1.0 200 OK\r\n"
2529 "Cache-Control: no-cache\r\n"
2530 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +02002531 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +02002532 "\r\n\r\n");
2533
2534 s->logs.status = 200;
2535 client_retnclose(s, msglen, trash); // send the start of the response.
2536 msglen = 0;
2537
2538 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
2539 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
2540 if (!(s->flags & SN_FINST_MASK))
2541 s->flags |= SN_FINST_R;
2542
2543 /* WARNING! This must fit in the first buffer !!! */
2544 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2545 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
2546 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2547 "<style type=\"text/css\"><!--\n"
2548 "body {"
2549 " font-family: helvetica, arial;"
2550 " font-size: 12px;"
2551 " font-weight: normal;"
2552 " color: black;"
2553 " background: white;"
2554 "}\n"
2555 "td {"
2556 " font-size: 12px;"
2557 " align: center;"
2558 "}\n"
2559 "h1 {"
2560 " font-size: xx-large;"
2561 " margin-bottom: 0.5em;"
2562 "}\n"
2563 "h2 {"
2564 " font-family: helvetica, arial;"
2565 " font-size: x-large;"
2566 " font-weight: bold;"
2567 " font-style: italic;"
2568 " color: #6020a0;"
2569 " margin-top: 0em;"
2570 " margin-bottom: 0em;"
2571 "}\n"
2572 "h3 {"
2573 " font-family: helvetica, arial;"
2574 " font-size: 16px;"
2575 " font-weight: bold;"
2576 " color: #b00040;"
2577 " background: #e8e8d0;"
2578 " margin-top: 0em;"
2579 " margin-bottom: 0em;"
2580 "}\n"
2581 "li {"
2582 " margin-top: 0.25em;"
2583 " margin-right: 2em;"
2584 "}\n"
2585 ".hr {"
2586 " margin-top: 0.25em;"
2587 " border-color: black;"
2588 " border-bottom-style: solid;"
2589 "}\n"
2590 "table.tbl { border-collapse: collapse; border-width: 1px; border-style: solid; border-color: gray;}\n"
2591 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; border-color: gray; }\n"
2592 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray; }\n"
2593 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2594 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2595 "-->"
2596 "</style></head>");
2597
2598 if (buffer_write(rep, trash, msglen) != 0)
2599 return 0;
2600 msglen = 0;
2601
2602 up = (now.tv_sec - start_date.tv_sec);
2603
2604 /* WARNING! this has to fit the first packet too */
2605 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2606 "<body><h1>" PRODUCT_NAME "</h1>\n"
2607 "<h2>Statistics Report for pid %d</h2>\n"
2608 "<hr width=\"100%%\" class=\"hr\">\n"
2609 "<h3>&gt; General process information</h3>\n"
2610 "<table border=0><tr><td align=\"left\">\n"
2611 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
2612 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2613 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
2614 "<b>maxsock = </b> %d<br>\n"
2615 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
2616 "</td><td width=\"10%%\">\n"
2617 "</td><td align=\"right\">\n"
2618 "<table class=\"lgd\">"
2619 "<tr><td bgcolor=\"#C0FFC0\">&nbsp;</td><td style=\"border-style: none;\">active UP </td>"
2620 "<td bgcolor=\"#B0D0FF\">&nbsp;</td><td style=\"border-style: none;\">backup UP </td></tr>"
2621 "<tr><td bgcolor=\"#FFFFA0\"></td><td style=\"border-style: none;\">active UP, going down </td>"
2622 "<td bgcolor=\"#C060FF\"></td><td style=\"border-style: none;\">backup UP, going down </td></tr>"
2623 "<tr><td bgcolor=\"#FFD020\"></td><td style=\"border-style: none;\">active DOWN, going up </td>"
2624 "<td bgcolor=\"#FF80FF\"></td><td style=\"border-style: none;\">backup DOWN, going up </td></tr>"
2625 "<tr><td bgcolor=\"#FF9090\"></td><td style=\"border-style: none;\">active or backup DOWN &nbsp;</td>"
2626 "<td bgcolor=\"#E0E0E0\"></td><td style=\"border-style: none;\">not checked </td></tr>"
2627 "</table>\n"
2628 "</tr></table>\n"
2629 "",
2630 pid, pid, global.nbproc,
2631 up / 86400, (up % 86400) / 3600,
2632 (up % 3600) / 60, (up % 60),
2633 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2634 global.rlimit_memmax ? " MB" : "",
2635 global.rlimit_nofile,
2636 global.maxsock,
2637 global.maxconn,
2638 actconn
2639 );
2640
2641 if (buffer_write(rep, trash, msglen) != 0)
2642 return 0;
2643 msglen = 0;
2644
2645 s->data_state = DATA_ST_DATA;
2646 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
2647
2648 px = s->data_ctx.stats.px = proxy;
2649 s->data_ctx.stats.px_st = DATA_ST_INIT;
2650 }
2651
2652 while (s->data_ctx.stats.px) {
2653 int dispatch_sess, dispatch_cum;
2654 int failed_checks, down_trans;
2655 int failed_secu, failed_conns, failed_resp;
2656
2657 if (s->data_ctx.stats.px_st == DATA_ST_INIT) {
2658 /* we are on a new proxy */
2659 px = s->data_ctx.stats.px;
2660
2661 /* skip the disabled proxies */
2662 if (px->state == PR_STSTOPPED)
2663 goto next_proxy;
2664
Willy Tarreau73de9892006-11-30 11:40:23 +01002665 if (s->fi->uri_auth && s->fi->uri_auth->scope) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002666 /* we have a limited scope, we have to check the proxy name */
2667 struct stat_scope *scope;
2668 int len;
2669
2670 len = strlen(px->id);
Willy Tarreau73de9892006-11-30 11:40:23 +01002671 scope = s->fi->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002672
2673 while (scope) {
2674 /* match exact proxy name */
2675 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2676 break;
2677
2678 /* match '.' which means 'self' proxy */
Willy Tarreau73de9892006-11-30 11:40:23 +01002679 if (!strcmp(scope->px_id, ".") && px == s->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002680 break;
2681 scope = scope->next;
2682 }
2683
2684 /* proxy name not found */
2685 if (scope == NULL)
2686 goto next_proxy;
2687 }
2688
2689 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2690 "<h3>&gt; Proxy instance %s : "
2691 "%d conns (maxconn=%d), %d queued (%d unassigned), %d total conns</h3>\n"
2692 "",
2693 px->id,
2694 px->nbconn, px->maxconn, px->totpend, px->nbpend, px->cum_conn);
2695
2696 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2697 "<table cols=\"16\" class=\"tbl\">\n"
2698 "<tr align=\"center\" bgcolor=\"#20C0C0\">"
2699 "<th colspan=5>Server</th>"
2700 "<th colspan=2>Queue</th>"
2701 "<th colspan=4>Sessions</th>"
2702 "<th colspan=5>Errors</th></tr>\n"
2703 "<tr align=\"center\" bgcolor=\"#20C0C0\">"
2704 "<th>Name</th><th>Weight</th><th>Status</th><th>Act.</th><th>Bck.</th>"
2705 "<th>Curr.</th><th>Max.</th>"
2706 "<th>Curr.</th><th>Max.</th><th>Limit</th><th>Cumul.</th>"
2707 "<th>Conn.</th><th>Resp.</th><th>Sec.</th><th>Check</th><th>Down</th></tr>\n");
2708
2709 if (buffer_write(rep, trash, msglen) != 0)
2710 return 0;
2711 msglen = 0;
2712
2713 s->data_ctx.stats.sv = px->srv;
2714 s->data_ctx.stats.px_st = DATA_ST_DATA;
2715 }
2716
2717 px = s->data_ctx.stats.px;
2718
2719 /* stats.sv has been initialized above */
2720 while (s->data_ctx.stats.sv != NULL) {
2721 static char *act_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FFD020", /*failing*/"#FFFFA0", /*up*/"#C0FFC0", /*unchecked*/"#E0E0E0" };
2722 static char *bck_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FF80ff", /*failing*/"#C060FF", /*up*/"#B0D0FF", /*unchecked*/"#E0E0E0" };
2723 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
2724 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP */
2725
2726 sv = s->data_ctx.stats.sv;
2727
2728 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
2729 if (!(sv->state & SRV_CHECKED))
2730 sv_state = 4;
2731 else if (sv->state & SRV_RUNNING)
2732 if (sv->health == sv->rise + sv->fall - 1)
2733 sv_state = 3; /* UP */
2734 else
2735 sv_state = 2; /* going down */
2736 else
2737 if (sv->health)
2738 sv_state = 1; /* going up */
2739 else
2740 sv_state = 0; /* DOWN */
2741
2742 /* name, weight */
2743 msglen += snprintf(trash, sizeof(trash),
2744 "<tr align=center bgcolor=\"%s\"><td>%s</td><td>%d</td><td>",
2745 (sv->state & SRV_BACKUP) ? bck_tab_bg[sv_state] : act_tab_bg[sv_state],
2746 sv->id, sv->uweight+1);
2747 /* status */
2748 msglen += snprintf(trash + msglen, sizeof(trash) - msglen, srv_hlt_st[sv_state],
2749 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
2750 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
2751
2752 /* act, bck */
2753 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2754 "</td><td>%s</td><td>%s</td>",
2755 (sv->state & SRV_BACKUP) ? "-" : "Y",
2756 (sv->state & SRV_BACKUP) ? "Y" : "-");
2757
2758 /* queue : current, max */
2759 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2760 "<td align=right>%d</td><td align=right>%d</td>",
2761 sv->nbpend, sv->nbpend_max);
2762
2763 /* sessions : current, max, limit, cumul */
2764 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2765 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>",
2766 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess);
2767
2768 /* errors : connect, response, security */
2769 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2770 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2771 sv->failed_conns, sv->failed_resp, sv->failed_secu);
2772
2773 /* check failures : unique, fatal */
2774 if (sv->state & SRV_CHECKED)
2775 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2776 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
2777 sv->failed_checks, sv->down_trans);
2778 else
2779 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2780 "<td align=right>-</td><td align=right>-</td></tr>\n");
2781
2782 if (buffer_write(rep, trash, msglen) != 0)
2783 return 0;
2784 msglen = 0;
2785
2786 s->data_ctx.stats.sv = sv->next;
2787 } /* while sv */
2788
2789 /* now we are past the last server, we'll dump information about the dispatcher */
2790
2791 /* We have to count down from the proxy to the servers to tell how
2792 * many sessions are on the dispatcher, and how many checks have
2793 * failed. We cannot count this during the servers dump because it
2794 * might be interrupted multiple times.
2795 */
2796 dispatch_sess = px->nbconn;
2797 dispatch_cum = px->cum_conn;
2798 failed_secu = px->failed_secu;
2799 failed_conns = px->failed_conns;
2800 failed_resp = px->failed_resp;
2801 failed_checks = down_trans = 0;
2802
2803 sv = px->srv;
2804 while (sv) {
2805 dispatch_sess -= sv->cur_sess;
2806 dispatch_cum -= sv->cum_sess;
2807 failed_conns -= sv->failed_conns;
2808 failed_resp -= sv->failed_resp;
2809 failed_secu -= sv->failed_secu;
2810 if (sv->state & SRV_CHECKED) {
2811 failed_checks += sv->failed_checks;
2812 down_trans += sv->down_trans;
2813 }
2814 sv = sv->next;
2815 }
2816
2817 /* name, weight, status, act, bck */
2818 msglen += snprintf(trash + msglen, sizeof(trash),
2819 "<tr align=center bgcolor=\"#e8e8d0\">"
2820 "<td>Dispatcher</td><td>-</td>"
2821 "<td>%s</td><td>-</td><td>-</td>",
2822 px->state == PR_STRUN ? "UP" : "DOWN");
2823
2824 /* queue : current, max */
2825 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2826 "<td align=right>%d</td><td align=right>%d</td>",
2827 px->nbpend, px->nbpend_max);
2828
2829 /* sessions : current, max, limit, cumul. */
2830 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2831 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>",
2832 dispatch_sess, px->nbconn_max, px->maxconn, dispatch_cum);
2833
2834 /* errors : connect, response, security */
2835 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2836 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2837 failed_conns, failed_resp, failed_secu);
2838
2839 /* check failures : unique, fatal */
2840 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2841 "<td align=right>-</td><td align=right>-</td></tr>\n");
2842
2843
2844 /* now the summary for the whole proxy */
2845 /* name, weight, status, act, bck */
2846 msglen += snprintf(trash + msglen, sizeof(trash),
2847 "<tr align=center style=\"color: #ffff80; background: #20C0C0;\">"
2848 "<td><b>Total</b></td><td>-</td>"
2849 "<td><b>%s</b></td><td><b>%d</b></td><td><b>%d</b></td>",
2850 (px->state == PR_STRUN && ((px->srv == NULL) || px->srv_act || px->srv_bck)) ? "UP" : "DOWN",
2851 px->srv_act, px->srv_bck);
2852
2853 /* queue : current, max */
2854 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2855 "<td align=right><b>%d</b></td><td align=right><b>%d</b></td>",
2856 px->totpend, px->nbpend_max);
2857
2858 /* sessions : current, max, limit, cumul */
2859 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2860 "<td align=right><b>%d</b></td><td align=right><b>%d</b></td><td align=right><b>%d</b></td><td align=right><b>%d</b></td>",
2861 px->nbconn, px->nbconn_max, px->maxconn, px->cum_conn);
2862
2863 /* errors : connect, response, security */
2864 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2865 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2866 px->failed_conns, px->failed_resp, px->failed_secu);
2867
2868 /* check failures : unique, fatal */
2869 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2870 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
2871 failed_checks, down_trans);
2872
2873 msglen += snprintf(trash + msglen, sizeof(trash) - msglen, "</table><p>\n");
2874
2875 if (buffer_write(rep, trash, msglen) != 0)
2876 return 0;
2877 msglen = 0;
2878
2879 s->data_ctx.stats.px_st = DATA_ST_INIT;
2880 next_proxy:
2881 s->data_ctx.stats.px = px->next;
2882 } /* proxy loop */
2883 /* here, we just have reached the sv == NULL and px == NULL */
2884 s->flags &= ~SN_SELF_GEN;
2885 return 1;
2886 }
2887 else {
2888 /* unknown data source */
2889 s->logs.status = 500;
Willy Tarreau73de9892006-11-30 11:40:23 +01002890 client_retnclose(s, s->fe->errmsg.len500, s->fe->errmsg.msg500);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002891 if (!(s->flags & SN_ERR_MASK))
2892 s->flags |= SN_ERR_PRXCOND;
2893 if (!(s->flags & SN_FINST_MASK))
2894 s->flags |= SN_FINST_R;
2895 s->flags &= SN_SELF_GEN;
2896 return 1;
2897 }
2898}
2899
2900
2901/*
2902 * Local variables:
2903 * c-indent-level: 8
2904 * c-basic-offset: 8
2905 * End:
2906 */