blob: 0b0ce734a86066fa262ac5418196454ec1263085 [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{
88 FD_CLR(s->cli_fd, StaticReadEvent);
89 FD_SET(s->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +020090 tv_eternity(&s->req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 if (s->proxy->clitimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +020092 tv_delayfrom(&s->rep->wex, &now, s->proxy->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,
120 int status, int msglen, char *msg)
121{
122 t->srv_state = SV_STCLOSE;
123 if (status > 0) {
124 t->logs.status = status;
125 if (t->proxy->mode == PR_MODE_HTTP)
126 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
179 s->proxy->nbconn--;
180 actconn--;
181
182 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
183 int len;
184 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", s->uniq_id, s->proxy->id, (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
185 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) &&
195 (!(s->proxy->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],
228 FD_ISSET(t->cli_fd, StaticReadEvent), 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,
233 //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent),
234 //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent)
235 //);
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 */
284 client_retnclose(t, t->proxy->errmsg.len403, t->proxy->errmsg.msg403);
285 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 */
302 if ((t->proxy->monitor_uri_len != 0) &&
303 (t->req_line.len >= t->proxy->monitor_uri_len)) {
304 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
312 if (t->req_line.len - idx >= t->proxy->monitor_uri_len &&
313 !memcmp(p, t->proxy->monitor_uri, t->proxy->monitor_uri_len)) {
314 /*
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
328 if (t->proxy->uri_auth != NULL
329 && t->req_line.len >= t->proxy->uri_auth->uri_len + 4) { /* +4 for "GET /" */
330 if (!memcmp(t->req_line.str + 4,
331 t->proxy->uri_auth->uri_prefix, t->proxy->uri_auth->uri_len)
332 && !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 */
339 user = t->proxy->uri_auth->users;
340 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
367 msglen = sprintf(trash, HTTP_401_fmt, t->proxy->uri_auth->auth_realm);
368 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
388 for (line = 0; line < t->proxy->nb_reqadd; line++) {
389 len = sprintf(trash, "%s\r\n", t->proxy->req_add[line]);
390 buffer_replace2(req, req->h, req->h, trash, len);
391 }
392
393 if (t->proxy->options & PR_O_FWDFOR) {
394 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 */
412 if (t->proxy->options & PR_O_HTTP_CLOSE)
413 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 */
431 //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
441 if (!t->proxy->clitimeout ||
442 (t->srv_state < SV_STDATA && t->proxy->srvtimeout))
443 /* 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) {
461 tv_delayfrom(&req->cex, &now,
462 t->proxy->contimeout ? t->proxy->contimeout : 0);
463 }
464
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465 goto process_data;
466 }
467
468 /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */
469 if (ptr > req->r - 2) {
470 /* this is a partial header, let's wait for more to come */
471 req->lr = ptr;
472 break;
473 }
474
475 /* now we know that *ptr is either \r or \n,
476 * and that there are at least 1 char after it.
477 */
478 if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n'))
479 req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */
480 else
481 req->lr = ptr + 2; /* \r\n or \n\r */
482
483 /*
484 * now we know that we have a full header ; we can do whatever
485 * we want with these pointers :
486 * req->h = beginning of header
487 * ptr = end of header (first \r or \n)
488 * req->lr = beginning of next line (next rep->h)
489 * req->r = end of data (not used at this stage)
490 */
491
492 if (!method_checked && (t->proxy->appsession_name != NULL) &&
493 ((memcmp(req->h, "GET ", 4) == 0) || (memcmp(req->h, "POST ", 4) == 0)) &&
494 ((request_line = memchr(req->h, ';', req->lr - req->h)) != NULL)) {
495
496 /* skip ; */
497 request_line++;
498
499 /* look if we have a jsessionid */
500
501 if (strncasecmp(request_line, t->proxy->appsession_name, t->proxy->appsession_name_len) == 0) {
502
503 /* skip jsessionid= */
504 request_line += t->proxy->appsession_name_len + 1;
505
506 /* First try if we allready have an appsession */
507 asession_temp = &local_asession;
508
509 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
510 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
511 send_log(t->proxy, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
512 return 0;
513 }
514
515 /* Copy the sessionid */
516 memcpy(asession_temp->sessid, request_line, t->proxy->appsession_len);
517 asession_temp->sessid[t->proxy->appsession_len] = 0;
518 asession_temp->serverid = NULL;
519
520 /* only do insert, if lookup fails */
521 if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *)&asession_temp)) {
522 if ((asession_temp = pool_alloc(appsess)) == NULL) {
523 Alert("Not enough memory process_cli():asession:calloc().\n");
524 send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
525 return 0;
526 }
527 asession_temp->sessid = local_asession.sessid;
528 asession_temp->serverid = local_asession.serverid;
529 chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp);
530 } /* end if (chtbl_lookup()) */
531 else {
532 /*free wasted memory;*/
533 pool_free_to(apools.sessid, local_asession.sessid);
534 }
535
536 tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout);
537 asession_temp->request_count++;
538
539#if defined(DEBUG_HASH)
540 print_table(&(t->proxy->htbl_proxy));
541#endif
542
543 if (asession_temp->serverid == NULL) {
544 Alert("Found Application Session without matching server.\n");
545 } else {
546 struct server *srv = t->proxy->srv;
547 while (srv) {
548 if (strcmp(srv->id, asession_temp->serverid) == 0) {
549 if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) {
550 /* we found the server and it's usable */
551 t->flags &= ~SN_CK_MASK;
552 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
553 t->srv = srv;
554 break;
555 } else {
556 t->flags &= ~SN_CK_MASK;
557 t->flags |= SN_CK_DOWN;
558 }
559 } /* end if (strcmp()) */
560 srv = srv->next;
561 }/* end while(srv) */
562 }/* end else of if (asession_temp->serverid == NULL) */
563 }/* end if (strncasecmp(request_line,t->proxy->appsession_name,apssesion_name_len) == 0) */
564 else {
565 //fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>NO SESSION\n");
566 }
567 method_checked = 1;
568 } /* end if (!method_checked ...) */
569 else{
570 //printf("No Methode-Header with Session-String\n");
571 }
572
573 if (t->logs.logwait & LW_REQ) {
574 /* we have a complete HTTP request that we must log */
575 int urilen;
576
577 if ((t->logs.uri = pool_alloc(requri)) == NULL) {
578 Alert("HTTP logging : out of memory.\n");
579 t->logs.status = 500;
580 client_retnclose(t, t->proxy->errmsg.len500, t->proxy->errmsg.msg500);
581 if (!(t->flags & SN_ERR_MASK))
582 t->flags |= SN_ERR_PRXCOND;
583 if (!(t->flags & SN_FINST_MASK))
584 t->flags |= SN_FINST_R;
585 return 1;
586 }
587
588 urilen = ptr - req->h;
589 if (urilen >= REQURI_LEN)
590 urilen = REQURI_LEN - 1;
591 memcpy(t->logs.uri, req->h, urilen);
592 t->logs.uri[urilen] = 0;
593
594 if (!(t->logs.logwait &= ~LW_REQ))
595 sess_log(t);
596 }
597 else if (t->logs.logwait & LW_REQHDR) {
598 struct cap_hdr *h;
599 int len;
600 for (h = t->proxy->req_cap; h; h = h->next) {
601 if ((h->namelen + 2 <= ptr - req->h) &&
602 (req->h[h->namelen] == ':') &&
603 (strncasecmp(req->h, h->name, h->namelen) == 0)) {
604
605 if (t->req_cap[h->index] == NULL)
606 t->req_cap[h->index] = pool_alloc_from(h->pool, h->len + 1);
607
608 len = ptr - (req->h + h->namelen + 2);
609 if (len > h->len)
610 len = h->len;
611
612 memcpy(t->req_cap[h->index], req->h + h->namelen + 2, len);
613 t->req_cap[h->index][len]=0;
614 }
615 }
616
617 }
618
619 delete_header = 0;
620
621 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
622 int len, max;
623 len = sprintf(trash, "%08x:%s.clihdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
624 max = ptr - req->h;
625 UBOUND(max, sizeof(trash) - len - 1);
626 len += strlcpy2(trash + len, req->h, max + 1);
627 trash[len++] = '\n';
628 write(1, trash, len);
629 }
630
631
632 /* remove "connection: " if needed */
633 if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE)
634 && (strncasecmp(req->h, "Connection: ", 12) == 0)) {
635 delete_header = 1;
636 }
637
638 /* try headers regexps */
639 if (!delete_header && t->proxy->req_exp != NULL
640 && !(t->flags & SN_CLDENY)) {
641 struct hdr_exp *exp;
642 char term;
643
644 term = *ptr;
645 *ptr = '\0';
646 exp = t->proxy->req_exp;
647 do {
648 if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) {
649 switch (exp->action) {
650 case ACT_ALLOW:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200651 if (!(t->flags & (SN_CLDENY | SN_CLTARPIT)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200652 t->flags |= SN_CLALLOW;
653 break;
654 case ACT_REPLACE:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200655 if (!(t->flags & (SN_CLDENY | SN_CLTARPIT))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656 int len = exp_replace(trash, req->h, exp->replace, pmatch);
657 ptr += buffer_replace2(req, req->h, ptr, trash, len);
658 }
659 break;
660 case ACT_REMOVE:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200661 if (!(t->flags & (SN_CLDENY | SN_CLTARPIT)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 delete_header = 1;
663 break;
664 case ACT_DENY:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200665 if (!(t->flags & (SN_CLALLOW | SN_CLTARPIT)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666 t->flags |= SN_CLDENY;
667 break;
Willy Tarreaub8750a82006-09-03 09:56:00 +0200668 case ACT_TARPIT:
669 if (!(t->flags & (SN_CLALLOW | SN_CLDENY)))
670 t->flags |= SN_CLTARPIT;
671 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672 case ACT_PASS: /* we simply don't deny this one */
673 break;
674 }
675 break;
676 }
677 } while ((exp = exp->next) != NULL);
678 *ptr = term; /* restore the string terminator */
679 }
680
681 /* Now look for cookies. Conforming to RFC2109, we have to support
682 * attributes whose name begin with a '$', and associate them with
683 * the right cookie, if we want to delete this cookie.
684 * So there are 3 cases for each cookie read :
685 * 1) it's a special attribute, beginning with a '$' : ignore it.
686 * 2) it's a server id cookie that we *MAY* want to delete : save
687 * some pointers on it (last semi-colon, beginning of cookie...)
688 * 3) it's an application cookie : we *MAY* have to delete a previous
689 * "special" cookie.
690 * At the end of loop, if a "special" cookie remains, we may have to
691 * remove it. If no application cookie persists in the header, we
692 * *MUST* delete it
693 */
694 if (!delete_header &&
695 (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL || t->proxy->appsession_name !=NULL)
Willy Tarreaub8750a82006-09-03 09:56:00 +0200696 && !(t->flags & (SN_CLDENY|SN_CLTARPIT)) && (ptr >= req->h + 8)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697 && (strncasecmp(req->h, "Cookie: ", 8) == 0)) {
698 char *p1, *p2, *p3, *p4;
699 char *del_colon, *del_cookie, *colon;
700 int app_cookies;
701
702 p1 = req->h + 8; /* first char after 'Cookie: ' */
703 colon = p1;
704 /* del_cookie == NULL => nothing to be deleted */
705 del_colon = del_cookie = NULL;
706 app_cookies = 0;
707
708 while (p1 < ptr) {
709 /* skip spaces and colons, but keep an eye on these ones */
710 while (p1 < ptr) {
711 if (*p1 == ';' || *p1 == ',')
712 colon = p1;
713 else if (!isspace((int)*p1))
714 break;
715 p1++;
716 }
717
718 if (p1 == ptr)
719 break;
720
721 /* p1 is at the beginning of the cookie name */
722 p2 = p1;
723 while (p2 < ptr && *p2 != '=')
724 p2++;
725
726 if (p2 == ptr)
727 break;
728
729 p3 = p2 + 1; /* skips the '=' sign */
730 if (p3 == ptr)
731 break;
732
733 p4 = p3;
734 while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
735 p4++;
736
737 /* here, we have the cookie name between p1 and p2,
738 * and its value between p3 and p4.
739 * we can process it :
740 *
741 * Cookie: NAME=VALUE;
742 * | || || |
743 * | || || +--> p4
744 * | || |+-------> p3
745 * | || +--------> p2
746 * | |+------------> p1
747 * | +-------------> colon
748 * +--------------------> req->h
749 */
750
751 if (*p1 == '$') {
752 /* skip this one */
753 }
754 else {
755 /* first, let's see if we want to capture it */
756 if (t->proxy->capture_name != NULL &&
757 t->logs.cli_cookie == NULL &&
758 (p4 - p1 >= t->proxy->capture_namelen) &&
759 memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) {
760 int log_len = p4 - p1;
761
762 if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) {
763 Alert("HTTP logging : out of memory.\n");
764 } else {
765 if (log_len > t->proxy->capture_len)
766 log_len = t->proxy->capture_len;
767 memcpy(t->logs.cli_cookie, p1, log_len);
768 t->logs.cli_cookie[log_len] = 0;
769 }
770 }
771
772 if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) &&
773 (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) {
774 /* Cool... it's the right one */
775 struct server *srv = t->proxy->srv;
776 char *delim;
777
778 /* if we're in cookie prefix mode, we'll search the delimitor so that we
779 * have the server ID betweek p3 and delim, and the original cookie between
780 * delim+1 and p4. Otherwise, delim==p4 :
781 *
782 * Cookie: NAME=SRV~VALUE;
783 * | || || | |
784 * | || || | +--> p4
785 * | || || +--------> delim
786 * | || |+-----------> p3
787 * | || +------------> p2
788 * | |+----------------> p1
789 * | +-----------------> colon
790 * +------------------------> req->h
791 */
792
793 if (t->proxy->options & PR_O_COOK_PFX) {
794 for (delim = p3; delim < p4; delim++)
795 if (*delim == COOKIE_DELIM)
796 break;
797 }
798 else
799 delim = p4;
800
801
802 /* Here, we'll look for the first running server which supports the cookie.
803 * This allows to share a same cookie between several servers, for example
804 * to dedicate backup servers to specific servers only.
805 * However, to prevent clients from sticking to cookie-less backup server
806 * when they have incidentely learned an empty cookie, we simply ignore
807 * empty cookies and mark them as invalid.
808 */
809 if (delim == p3)
810 srv = NULL;
811
812 while (srv) {
813 if ((srv->cklen == delim - p3) && !memcmp(p3, srv->cookie, delim - p3)) {
814 if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) {
815 /* we found the server and it's usable */
816 t->flags &= ~SN_CK_MASK;
817 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
818 t->srv = srv;
819 break;
820 } else {
821 /* we found a server, but it's down */
822 t->flags &= ~SN_CK_MASK;
823 t->flags |= SN_CK_DOWN;
824 }
825 }
826 srv = srv->next;
827 }
828
829 if (!srv && !(t->flags & SN_CK_DOWN)) {
830 /* no server matched this cookie */
831 t->flags &= ~SN_CK_MASK;
832 t->flags |= SN_CK_INVALID;
833 }
834
835 /* depending on the cookie mode, we may have to either :
836 * - delete the complete cookie if we're in insert+indirect mode, so that
837 * the server never sees it ;
838 * - remove the server id from the cookie value, and tag the cookie as an
839 * application cookie so that it does not get accidentely removed later,
840 * if we're in cookie prefix mode
841 */
842 if ((t->proxy->options & PR_O_COOK_PFX) && (delim != p4)) {
843 buffer_replace2(req, p3, delim + 1, NULL, 0);
844 p4 -= (delim + 1 - p3);
845 ptr -= (delim + 1 - p3);
846 del_cookie = del_colon = NULL;
847 app_cookies++; /* protect the header from deletion */
848 }
849 else if (del_cookie == NULL &&
850 (t->proxy->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
851 del_cookie = p1;
852 del_colon = colon;
853 }
854 } else {
855 /* now we know that we must keep this cookie since it's
856 * not ours. But if we wanted to delete our cookie
857 * earlier, we cannot remove the complete header, but we
858 * can remove the previous block itself.
859 */
860 app_cookies++;
861
862 if (del_cookie != NULL) {
863 buffer_replace2(req, del_cookie, p1, NULL, 0);
864 p4 -= (p1 - del_cookie);
865 ptr -= (p1 - del_cookie);
866 del_cookie = del_colon = NULL;
867 }
868 }
869
870 if ((t->proxy->appsession_name != NULL) &&
871 (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) {
872 /* first, let's see if the cookie is our appcookie*/
873
874 /* Cool... it's the right one */
875
876 asession_temp = &local_asession;
877
878 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
879 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
880 send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
881 return 0;
882 }
883
884 memcpy(asession_temp->sessid, p3, t->proxy->appsession_len);
885 asession_temp->sessid[t->proxy->appsession_len] = 0;
886 asession_temp->serverid = NULL;
887
888 /* only do insert, if lookup fails */
889 if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) {
890 if ((asession_temp = pool_alloc(appsess)) == NULL) {
891 Alert("Not enough memory process_cli():asession:calloc().\n");
892 send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
893 return 0;
894 }
895
896 asession_temp->sessid = local_asession.sessid;
897 asession_temp->serverid = local_asession.serverid;
898 chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp);
899 } else {
900 /* free wasted memory */
901 pool_free_to(apools.sessid, local_asession.sessid);
902 }
903
904 if (asession_temp->serverid == NULL) {
905 Alert("Found Application Session without matching server.\n");
906 } else {
907 struct server *srv = t->proxy->srv;
908 while (srv) {
909 if (strcmp(srv->id, asession_temp->serverid) == 0) {
910 if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) {
911 /* we found the server and it's usable */
912 t->flags &= ~SN_CK_MASK;
913 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
914 t->srv = srv;
915 break;
916 } else {
917 t->flags &= ~SN_CK_MASK;
918 t->flags |= SN_CK_DOWN;
919 }
920 }
921 srv = srv->next;
922 }/* end while(srv) */
923 }/* end else if server == NULL */
924
925 tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout);
926 }/* end if ((t->proxy->appsession_name != NULL) ... */
927 }
928
929 /* we'll have to look for another cookie ... */
930 p1 = p4;
931 } /* while (p1 < ptr) */
932
933 /* There's no more cookie on this line.
934 * We may have marked the last one(s) for deletion.
935 * We must do this now in two ways :
936 * - if there is no app cookie, we simply delete the header ;
937 * - if there are app cookies, we must delete the end of the
938 * string properly, including the colon/semi-colon before
939 * the cookie name.
940 */
941 if (del_cookie != NULL) {
942 if (app_cookies) {
943 buffer_replace2(req, del_colon, ptr, NULL, 0);
944 /* WARNING! <ptr> becomes invalid for now. If some code
945 * below needs to rely on it before the end of the global
946 * header loop, we need to correct it with this code :
947 */
948 ptr = del_colon;
949 }
950 else
951 delete_header = 1;
952 }
953 } /* end of cookie processing on this header */
954
955 /* let's look if we have to delete this header */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200956 if (delete_header && !(t->flags & (SN_CLDENY|SN_CLTARPIT))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200957 buffer_replace2(req, req->h, req->lr, NULL, 0);
958 /* WARNING: ptr is not valid anymore, since the header may have
959 * been deleted or truncated ! */
960 } else {
961 /* try to catch the first line as the request */
962 if (t->req_line.len < 0) {
963 t->req_line.str = req->h;
964 t->req_line.len = ptr - req->h;
965 }
966
967 /* We might also need the 'Authorization: ' header */
968 if (t->auth_hdr.len < 0 &&
969 t->proxy->uri_auth != NULL &&
970 ptr > req->h + 15 &&
971 !strncasecmp("Authorization: ", req->h, 15)) {
972 t->auth_hdr.str = req->h;
973 t->auth_hdr.len = ptr - req->h;
974 }
975 }
976
977 req->h = req->lr;
978 } /* while (req->lr < req->r) */
979
980 /* end of header processing (even if incomplete) */
981
982 if ((req->l < req->rlim - req->data) && ! FD_ISSET(t->cli_fd, StaticReadEvent)) {
983 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
Willy Tarreaud7971282006-07-29 18:36:34 +0200984 * full. We cannot loop here since stream_sock_read will disable it only if
Willy Tarreaubaaee002006-06-26 02:48:02 +0200985 * req->l == rlim-data
986 */
987 FD_SET(t->cli_fd, StaticReadEvent);
988 if (t->proxy->clitimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +0200989 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200990 else
Willy Tarreaud7971282006-07-29 18:36:34 +0200991 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992 }
993
994 /* Since we are in header mode, if there's no space left for headers, we
995 * won't be able to free more later, so the session will never terminate.
996 */
997 if (req->l >= req->rlim - req->data) {
998 t->logs.status = 400;
999 client_retnclose(t, t->proxy->errmsg.len400, t->proxy->errmsg.msg400);
1000 if (!(t->flags & SN_ERR_MASK))
1001 t->flags |= SN_ERR_PRXCOND;
1002 if (!(t->flags & SN_FINST_MASK))
1003 t->flags |= SN_FINST_R;
1004 return 1;
1005 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001006 else if (req->flags & (BF_READ_ERROR | BF_READ_NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001007 /* read error, or last read : give up. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001008 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009 fd_delete(t->cli_fd);
1010 t->cli_state = CL_STCLOSE;
1011 if (!(t->flags & SN_ERR_MASK))
1012 t->flags |= SN_ERR_CLICL;
1013 if (!(t->flags & SN_FINST_MASK))
1014 t->flags |= SN_FINST_R;
1015 return 1;
1016 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001017 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018
1019 /* read timeout : give up with an error message.
1020 */
1021 t->logs.status = 408;
1022 client_retnclose(t, t->proxy->errmsg.len408, t->proxy->errmsg.msg408);
1023 if (!(t->flags & SN_ERR_MASK))
1024 t->flags |= SN_ERR_CLITO;
1025 if (!(t->flags & SN_FINST_MASK))
1026 t->flags |= SN_FINST_R;
1027 return 1;
1028 }
1029
1030 return t->cli_state != CL_STHEADERS;
1031 }
1032 else if (c == CL_STDATA) {
1033 process_data:
1034 /* FIXME: this error handling is partly buggy because we always report
1035 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1036 * or HEADER phase. BTW, it's not logical to expire the client while
1037 * we're waiting for the server to connect.
1038 */
1039 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001040 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001041 tv_eternity(&req->rex);
1042 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001043 fd_delete(t->cli_fd);
1044 t->cli_state = CL_STCLOSE;
1045 if (!(t->flags & SN_ERR_MASK))
1046 t->flags |= SN_ERR_CLICL;
1047 if (!(t->flags & SN_FINST_MASK)) {
1048 if (t->pend_pos)
1049 t->flags |= SN_FINST_Q;
1050 else if (s == SV_STCONN)
1051 t->flags |= SN_FINST_C;
1052 else
1053 t->flags |= SN_FINST_D;
1054 }
1055 return 1;
1056 }
1057 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001058 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001059 FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001060 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001061 shutdown(t->cli_fd, SHUT_RD);
1062 t->cli_state = CL_STSHUTR;
1063 return 1;
1064 }
1065 /* last server read and buffer empty */
1066 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
1067 FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001068 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069 shutdown(t->cli_fd, SHUT_WR);
1070 /* We must ensure that the read part is still alive when switching
1071 * to shutw */
1072 FD_SET(t->cli_fd, StaticReadEvent);
1073 if (t->proxy->clitimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001074 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001075 t->cli_state = CL_STSHUTW;
1076 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1077 return 1;
1078 }
1079 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001080 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001081 FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001082 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001083 shutdown(t->cli_fd, SHUT_RD);
1084 t->cli_state = CL_STSHUTR;
1085 if (!(t->flags & SN_ERR_MASK))
1086 t->flags |= SN_ERR_CLITO;
1087 if (!(t->flags & SN_FINST_MASK)) {
1088 if (t->pend_pos)
1089 t->flags |= SN_FINST_Q;
1090 else if (s == SV_STCONN)
1091 t->flags |= SN_FINST_C;
1092 else
1093 t->flags |= SN_FINST_D;
1094 }
1095 return 1;
1096 }
1097 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001098 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001099 FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001100 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001101 shutdown(t->cli_fd, SHUT_WR);
1102 /* We must ensure that the read part is still alive when switching
1103 * to shutw */
1104 FD_SET(t->cli_fd, StaticReadEvent);
1105 if (t->proxy->clitimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001106 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001107
1108 t->cli_state = CL_STSHUTW;
1109 if (!(t->flags & SN_ERR_MASK))
1110 t->flags |= SN_ERR_CLITO;
1111 if (!(t->flags & SN_FINST_MASK)) {
1112 if (t->pend_pos)
1113 t->flags |= SN_FINST_Q;
1114 else if (s == SV_STCONN)
1115 t->flags |= SN_FINST_C;
1116 else
1117 t->flags |= SN_FINST_D;
1118 }
1119 return 1;
1120 }
1121
1122 if (req->l >= req->rlim - req->data) {
1123 /* no room to read more data */
1124 if (FD_ISSET(t->cli_fd, StaticReadEvent)) {
1125 /* stop reading until we get some space */
1126 FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001127 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001128 }
1129 } else {
1130 /* there's still some space in the buffer */
1131 if (! FD_ISSET(t->cli_fd, StaticReadEvent)) {
1132 FD_SET(t->cli_fd, StaticReadEvent);
1133 if (!t->proxy->clitimeout ||
1134 (t->srv_state < SV_STDATA && t->proxy->srvtimeout))
1135 /* If the client has no timeout, or if the server not ready yet, and we
1136 * know for sure that it can expire, then it's cleaner to disable the
1137 * timeout on the client side so that too low values cannot make the
1138 * sessions abort too early.
1139 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001140 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001141 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001142 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001143 }
1144 }
1145
1146 if ((rep->l == 0) ||
1147 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
1148 if (FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1149 FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001150 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001151 }
1152 } else {
1153 /* buffer not empty */
1154 if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1155 FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
1156 if (t->proxy->clitimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001157 tv_delayfrom(&rep->wex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001158 /* FIXME: to prevent the client from expiring read timeouts during writes,
1159 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001160 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001161 }
1162 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001163 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001164 }
1165 }
1166 return 0; /* other cases change nothing */
1167 }
1168 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001169 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001170 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001171 fd_delete(t->cli_fd);
1172 t->cli_state = CL_STCLOSE;
1173 if (!(t->flags & SN_ERR_MASK))
1174 t->flags |= SN_ERR_CLICL;
1175 if (!(t->flags & SN_FINST_MASK)) {
1176 if (t->pend_pos)
1177 t->flags |= SN_FINST_Q;
1178 else if (s == SV_STCONN)
1179 t->flags |= SN_FINST_C;
1180 else
1181 t->flags |= SN_FINST_D;
1182 }
1183 return 1;
1184 }
1185 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
1186 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001187 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001188 fd_delete(t->cli_fd);
1189 t->cli_state = CL_STCLOSE;
1190 return 1;
1191 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001192 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
1193 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001194 fd_delete(t->cli_fd);
1195 t->cli_state = CL_STCLOSE;
1196 if (!(t->flags & SN_ERR_MASK))
1197 t->flags |= SN_ERR_CLITO;
1198 if (!(t->flags & SN_FINST_MASK)) {
1199 if (t->pend_pos)
1200 t->flags |= SN_FINST_Q;
1201 else if (s == SV_STCONN)
1202 t->flags |= SN_FINST_C;
1203 else
1204 t->flags |= SN_FINST_D;
1205 }
1206 return 1;
1207 }
1208
1209 if (t->flags & SN_SELF_GEN) {
1210 produce_content(t);
1211 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001212 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001213 fd_delete(t->cli_fd);
1214 t->cli_state = CL_STCLOSE;
1215 return 1;
1216 }
1217 }
1218
1219 if ((rep->l == 0)
1220 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
1221 if (FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1222 FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001223 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001224 }
1225 } else {
1226 /* buffer not empty */
1227 if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1228 FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
1229 if (t->proxy->clitimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001230 tv_delayfrom(&rep->wex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001231 /* FIXME: to prevent the client from expiring read timeouts during writes,
1232 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001233 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001234 }
1235 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001236 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001237 }
1238 }
1239 return 0;
1240 }
1241 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001242 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001243 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244 fd_delete(t->cli_fd);
1245 t->cli_state = CL_STCLOSE;
1246 if (!(t->flags & SN_ERR_MASK))
1247 t->flags |= SN_ERR_CLICL;
1248 if (!(t->flags & SN_FINST_MASK)) {
1249 if (t->pend_pos)
1250 t->flags |= SN_FINST_Q;
1251 else if (s == SV_STCONN)
1252 t->flags |= SN_FINST_C;
1253 else
1254 t->flags |= SN_FINST_D;
1255 }
1256 return 1;
1257 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001258 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001259 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001260 fd_delete(t->cli_fd);
1261 t->cli_state = CL_STCLOSE;
1262 return 1;
1263 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001264 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
1265 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266 fd_delete(t->cli_fd);
1267 t->cli_state = CL_STCLOSE;
1268 if (!(t->flags & SN_ERR_MASK))
1269 t->flags |= SN_ERR_CLITO;
1270 if (!(t->flags & SN_FINST_MASK)) {
1271 if (t->pend_pos)
1272 t->flags |= SN_FINST_Q;
1273 else if (s == SV_STCONN)
1274 t->flags |= SN_FINST_C;
1275 else
1276 t->flags |= SN_FINST_D;
1277 }
1278 return 1;
1279 }
1280 else if (req->l >= req->rlim - req->data) {
1281 /* no room to read more data */
1282
1283 /* FIXME-20050705: is it possible for a client to maintain a session
1284 * after the timeout by sending more data after it receives a close ?
1285 */
1286
1287 if (FD_ISSET(t->cli_fd, StaticReadEvent)) {
1288 /* stop reading until we get some space */
1289 FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001290 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001291 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1292 }
1293 } else {
1294 /* there's still some space in the buffer */
1295 if (! FD_ISSET(t->cli_fd, StaticReadEvent)) {
1296 FD_SET(t->cli_fd, StaticReadEvent);
1297 if (t->proxy->clitimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001298 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001299 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001300 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001301 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1302 }
1303 }
1304 return 0;
1305 }
1306 else { /* CL_STCLOSE: nothing to do */
1307 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1308 int len;
1309 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
1310 write(1, trash, len);
1311 }
1312 return 0;
1313 }
1314 return 0;
1315}
1316
1317
1318/*
1319 * manages the server FSM and its socket. It returns 1 if a state has changed
1320 * (and a resync may be needed), 0 else.
1321 */
1322int process_srv(struct session *t)
1323{
1324 int s = t->srv_state;
1325 int c = t->cli_state;
1326 struct buffer *req = t->req;
1327 struct buffer *rep = t->rep;
1328 appsess *asession_temp = NULL;
1329 appsess local_asession;
1330 int conn_err;
1331
1332#ifdef DEBUG_FULL
1333 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
1334#endif
1335 //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s,
1336 //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent),
1337 //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent)
1338 //);
1339 if (s == SV_STIDLE) {
1340 if (c == CL_STHEADERS)
1341 return 0; /* stay in idle, waiting for data to reach the client side */
1342 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
1343 (c == CL_STSHUTR &&
1344 (t->req->l == 0 || t->proxy->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02001345 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001346 if (t->pend_pos)
1347 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1348 /* note that this must not return any error because it would be able to
1349 * overwrite the client_retnclose() output.
1350 */
1351 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, 0, NULL);
1352
1353 return 1;
1354 }
1355 else {
Willy Tarreaub8750a82006-09-03 09:56:00 +02001356 if (t->flags & SN_CLTARPIT) {
1357 /* This connection is being tarpitted. The CLIENT side has
1358 * already set the connect expiration date to the right
1359 * timeout. We just have to check that it has not expired.
1360 */
1361 if (tv_cmp2_ms(&req->cex, &now) > 0)
1362 return 0;
1363
1364 /* We will set the queue timer to the time spent, just for
1365 * logging purposes. We fake a 500 server error, so that the
1366 * attacker will not suspect his connection has been tarpitted.
1367 * It will not cause trouble to the logs because we can exclude
1368 * the tarpitted connections by filtering on the 'PT' status flags.
1369 */
1370 tv_eternity(&req->cex);
1371 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1372 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
1373 500, t->proxy->errmsg.len500, t->proxy->errmsg.msg500);
1374 return 1;
1375 }
1376
Willy Tarreaubaaee002006-06-26 02:48:02 +02001377 /* Right now, we will need to create a connection to the server.
1378 * We might already have tried, and got a connection pending, in
1379 * which case we will not do anything till it's pending. It's up
1380 * to any other session to release it and wake us up again.
1381 */
1382 if (t->pend_pos) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001383 if (tv_cmp2_ms(&req->cex, &now) > 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001384 return 0;
1385 else {
1386 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02001387 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001388 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1389 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
1390 503, t->proxy->errmsg.len503, t->proxy->errmsg.msg503);
1391 if (t->srv)
1392 t->srv->failed_conns++;
1393 t->proxy->failed_conns++;
1394 return 1;
1395 }
1396 }
1397
1398 do {
1399 /* first, get a connection */
1400 if (srv_redispatch_connect(t))
1401 return t->srv_state != SV_STIDLE;
1402
1403 /* try to (re-)connect to the server, and fail if we expire the
1404 * number of retries.
1405 */
1406 if (srv_retryable_connect(t)) {
1407 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1408 return t->srv_state != SV_STIDLE;
1409 }
1410
1411 } while (1);
1412 }
1413 }
1414 else if (s == SV_STCONN) { /* connection in progress */
1415 if (c == CL_STCLOSE || c == CL_STSHUTW ||
1416 (c == CL_STSHUTR &&
1417 (t->req->l == 0 || t->proxy->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02001418 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001419 fd_delete(t->srv_fd);
1420 if (t->srv)
1421 t->srv->cur_sess--;
1422
1423 /* note that this must not return any error because it would be able to
1424 * overwrite the client_retnclose() output.
1425 */
1426 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, 0, NULL);
1427 return 1;
1428 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001429 if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) {
1430 //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 +02001431 return 0; /* nothing changed */
1432 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001433 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001434 /* timeout, asynchronous connect error or first write error */
1435 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
1436
1437 fd_delete(t->srv_fd);
1438 if (t->srv)
1439 t->srv->cur_sess--;
1440
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001441 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001442 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
1443 else
1444 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
1445
1446 /* ensure that we have enough retries left */
1447 if (srv_count_retry_down(t, conn_err))
1448 return 1;
1449
1450 do {
1451 /* Now we will try to either reconnect to the same server or
1452 * connect to another server. If the connection gets queued
1453 * because all servers are saturated, then we will go back to
1454 * the SV_STIDLE state.
1455 */
1456 if (srv_retryable_connect(t)) {
1457 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1458 return t->srv_state != SV_STCONN;
1459 }
1460
1461 /* we need to redispatch the connection to another server */
1462 if (srv_redispatch_connect(t))
1463 return t->srv_state != SV_STCONN;
1464 } while (1);
1465 }
1466 else { /* no error or write 0 */
1467 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
1468
1469 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
1470 if (req->l == 0) /* nothing to write */ {
1471 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001472 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001473 } else /* need the right to write */ {
1474 FD_SET(t->srv_fd, StaticWriteEvent);
1475 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001476 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001477 /* FIXME: to prevent the server from expiring read timeouts during writes,
1478 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001479 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001480 }
1481 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001482 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001483 }
1484
1485 if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
1486 FD_SET(t->srv_fd, StaticReadEvent);
1487 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001488 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001489 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001490 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001491
1492 t->srv_state = SV_STDATA;
1493 if (t->srv)
1494 t->srv->cum_sess++;
1495 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
1496
1497 /* if the user wants to log as soon as possible, without counting
1498 bytes from the server, then this is the right moment. */
1499 if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) {
1500 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
1501 sess_log(t);
1502 }
1503 }
1504 else {
1505 t->srv_state = SV_STHEADERS;
1506 if (t->srv)
1507 t->srv->cum_sess++;
1508 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
1509 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001510 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001511 return 1;
1512 }
1513 }
1514 else if (s == SV_STHEADERS) { /* receiving server headers */
1515 /* now parse the partial (or complete) headers */
1516 while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */
1517 char *ptr;
1518 int delete_header;
1519
1520 ptr = rep->lr;
1521
1522 /* look for the end of the current header */
1523 while (ptr < rep->r && *ptr != '\n' && *ptr != '\r')
1524 ptr++;
1525
1526 if (ptr == rep->h) {
1527 int line, len;
1528
1529 /* we can only get here after an end of headers */
1530
1531 /* first, we'll block if security checks have caught nasty things */
1532 if (t->flags & SN_CACHEABLE) {
1533 if ((t->flags & SN_CACHE_COOK) &&
1534 (t->flags & SN_SCK_ANY) &&
1535 (t->proxy->options & PR_O_CHK_CACHE)) {
1536
1537 /* we're in presence of a cacheable response containing
1538 * a set-cookie header. We'll block it as requested by
1539 * the 'checkcache' option, and send an alert.
1540 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001541 tv_eternity(&rep->rex);
1542 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001543 fd_delete(t->srv_fd);
1544 if (t->srv) {
1545 t->srv->cur_sess--;
1546 t->srv->failed_secu++;
1547 }
1548 t->proxy->failed_secu++;
1549 t->srv_state = SV_STCLOSE;
1550 t->logs.status = 502;
1551 client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502);
1552 if (!(t->flags & SN_ERR_MASK))
1553 t->flags |= SN_ERR_PRXCOND;
1554 if (!(t->flags & SN_FINST_MASK))
1555 t->flags |= SN_FINST_H;
1556
1557 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id);
1558 send_log(t->proxy, LOG_ALERT, "Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id);
1559
1560 /* We used to have a free connection slot. Since we'll never use it,
1561 * we have to inform the server that it may be used by another session.
1562 */
1563 if (may_dequeue_tasks(t->srv, t->proxy))
1564 task_wakeup(&rq, t->srv->queue_mgt);
1565
1566 return 1;
1567 }
1568 }
1569
1570 /* next, we'll block if an 'rspideny' or 'rspdeny' filter matched */
1571 if (t->flags & SN_SVDENY) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001572 tv_eternity(&rep->rex);
1573 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001574 fd_delete(t->srv_fd);
1575 if (t->srv) {
1576 t->srv->cur_sess--;
1577 t->srv->failed_secu++;
1578 }
1579 t->proxy->failed_secu++;
1580 t->srv_state = SV_STCLOSE;
1581 t->logs.status = 502;
1582 client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502);
1583 if (!(t->flags & SN_ERR_MASK))
1584 t->flags |= SN_ERR_PRXCOND;
1585 if (!(t->flags & SN_FINST_MASK))
1586 t->flags |= SN_FINST_H;
1587 /* We used to have a free connection slot. Since we'll never use it,
1588 * we have to inform the server that it may be used by another session.
1589 */
1590 if (may_dequeue_tasks(t->srv, t->proxy))
1591 task_wakeup(&rq, t->srv->queue_mgt);
1592
1593 return 1;
1594 }
1595
1596 /* we'll have something else to do here : add new headers ... */
1597
1598 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS) &&
1599 (!(t->proxy->options & PR_O_COOK_POST) || (t->flags & SN_POST))) {
1600 /* the server is known, it's not the one the client requested, we have to
1601 * insert a set-cookie here, except if we want to insert only on POST
1602 * requests and this one isn't. Note that servers which don't have cookies
1603 * (eg: some backup servers) will return a full cookie removal request.
1604 */
1605 len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n",
1606 t->proxy->cookie_name,
1607 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
1608
1609 t->flags |= SN_SCK_INSERTED;
1610
1611 /* Here, we will tell an eventual cache on the client side that we don't
1612 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1613 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1614 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1615 */
1616 if (t->proxy->options & PR_O_COOK_NOC)
1617 //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n");
1618 len += sprintf(trash + len, "Cache-control: private\r\n");
1619
1620 if (rep->data + rep->l < rep->h)
1621 /* The data has been stolen, we will crash cleanly instead of corrupting memory */
1622 *(int *)0 = 0;
1623 buffer_replace2(rep, rep->h, rep->h, trash, len);
1624 }
1625
1626 /* headers to be added */
1627 for (line = 0; line < t->proxy->nb_rspadd; line++) {
1628 len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]);
1629 buffer_replace2(rep, rep->h, rep->h, trash, len);
1630 }
1631
1632 /* add a "connection: close" line if needed */
1633 if (t->proxy->options & PR_O_HTTP_CLOSE)
1634 buffer_replace2(rep, rep->h, rep->h, "Connection: close\r\n", 19);
1635
1636 t->srv_state = SV_STDATA;
1637 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
1638 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
1639
1640 /* client connection already closed or option 'httpclose' required :
1641 * we close the server's outgoing connection right now.
1642 */
1643 if ((req->l == 0) &&
1644 (c == CL_STSHUTR || c == CL_STCLOSE || t->proxy->options & PR_O_FORCE_CLO)) {
1645 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001646 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001647
1648 /* We must ensure that the read part is still alive when switching
1649 * to shutw */
1650 FD_SET(t->srv_fd, StaticReadEvent);
1651 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001652 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001653
1654 shutdown(t->srv_fd, SHUT_WR);
1655 t->srv_state = SV_STSHUTW;
1656 }
1657
1658 /* if the user wants to log as soon as possible, without counting
1659 bytes from the server, then this is the right moment. */
1660 if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) {
1661 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
1662 t->logs.bytes = rep->h - rep->data;
1663 sess_log(t);
1664 }
1665 break;
1666 }
1667
1668 /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */
1669 if (ptr > rep->r - 2) {
1670 /* this is a partial header, let's wait for more to come */
1671 rep->lr = ptr;
1672 break;
1673 }
1674
1675 // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r);
1676 // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n");
1677
1678 /* now we know that *ptr is either \r or \n,
1679 * and that there are at least 1 char after it.
1680 */
1681 if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n'))
1682 rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */
1683 else
1684 rep->lr = ptr + 2; /* \r\n or \n\r */
1685
1686 /*
1687 * now we know that we have a full header ; we can do whatever
1688 * we want with these pointers :
1689 * rep->h = beginning of header
1690 * ptr = end of header (first \r or \n)
1691 * rep->lr = beginning of next line (next rep->h)
1692 * rep->r = end of data (not used at this stage)
1693 */
1694
1695
1696 if (t->logs.status == -1) {
1697 t->logs.logwait &= ~LW_RESP;
1698 t->logs.status = atoi(rep->h + 9);
1699 switch (t->logs.status) {
1700 case 200:
1701 case 203:
1702 case 206:
1703 case 300:
1704 case 301:
1705 case 410:
1706 /* RFC2616 @13.4:
1707 * "A response received with a status code of
1708 * 200, 203, 206, 300, 301 or 410 MAY be stored
1709 * by a cache (...) unless a cache-control
1710 * directive prohibits caching."
1711 *
1712 * RFC2616 @9.5: POST method :
1713 * "Responses to this method are not cacheable,
1714 * unless the response includes appropriate
1715 * Cache-Control or Expires header fields."
1716 */
1717 if (!(t->flags & SN_POST) && (t->proxy->options & PR_O_CHK_CACHE))
1718 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
1719 break;
1720 default:
1721 break;
1722 }
1723 }
1724 else if (t->logs.logwait & LW_RSPHDR) {
1725 struct cap_hdr *h;
1726 int len;
1727 for (h = t->proxy->rsp_cap; h; h = h->next) {
1728 if ((h->namelen + 2 <= ptr - rep->h) &&
1729 (rep->h[h->namelen] == ':') &&
1730 (strncasecmp(rep->h, h->name, h->namelen) == 0)) {
1731
1732 if (t->rsp_cap[h->index] == NULL)
1733 t->rsp_cap[h->index] = pool_alloc_from(h->pool, h->len + 1);
1734
1735 len = ptr - (rep->h + h->namelen + 2);
1736 if (len > h->len)
1737 len = h->len;
1738
1739 memcpy(t->rsp_cap[h->index], rep->h + h->namelen + 2, len);
1740 t->rsp_cap[h->index][len]=0;
1741 }
1742 }
1743
1744 }
1745
1746 delete_header = 0;
1747
1748 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1749 int len, max;
1750 len = sprintf(trash, "%08x:%s.srvhdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
1751 max = ptr - rep->h;
1752 UBOUND(max, sizeof(trash) - len - 1);
1753 len += strlcpy2(trash + len, rep->h, max + 1);
1754 trash[len++] = '\n';
1755 write(1, trash, len);
1756 }
1757
1758 /* remove "connection: " if needed */
1759 if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE)
1760 && (strncasecmp(rep->h, "Connection: ", 12) == 0)) {
1761 delete_header = 1;
1762 }
1763
1764 /* try headers regexps */
1765 if (!delete_header && t->proxy->rsp_exp != NULL
1766 && !(t->flags & SN_SVDENY)) {
1767 struct hdr_exp *exp;
1768 char term;
1769
1770 term = *ptr;
1771 *ptr = '\0';
1772 exp = t->proxy->rsp_exp;
1773 do {
1774 if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) {
1775 switch (exp->action) {
1776 case ACT_ALLOW:
1777 if (!(t->flags & SN_SVDENY))
1778 t->flags |= SN_SVALLOW;
1779 break;
1780 case ACT_REPLACE:
1781 if (!(t->flags & SN_SVDENY)) {
1782 int len = exp_replace(trash, rep->h, exp->replace, pmatch);
1783 ptr += buffer_replace2(rep, rep->h, ptr, trash, len);
1784 }
1785 break;
1786 case ACT_REMOVE:
1787 if (!(t->flags & SN_SVDENY))
1788 delete_header = 1;
1789 break;
1790 case ACT_DENY:
1791 if (!(t->flags & SN_SVALLOW))
1792 t->flags |= SN_SVDENY;
1793 break;
1794 case ACT_PASS: /* we simply don't deny this one */
1795 break;
1796 }
1797 break;
1798 }
1799 } while ((exp = exp->next) != NULL);
1800 *ptr = term; /* restore the string terminator */
1801 }
1802
1803 /* check for cache-control: or pragma: headers */
1804 if (!delete_header && (t->flags & SN_CACHEABLE)) {
1805 if (strncasecmp(rep->h, "Pragma: no-cache", 16) == 0)
1806 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1807 else if (strncasecmp(rep->h, "Cache-control: ", 15) == 0) {
1808 if (strncasecmp(rep->h + 15, "no-cache", 8) == 0) {
1809 if (rep->h + 23 == ptr || rep->h[23] == ',')
1810 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1811 else {
1812 if (strncasecmp(rep->h + 23, "=\"set-cookie", 12) == 0
1813 && (rep->h[35] == '"' || rep->h[35] == ','))
1814 t->flags &= ~SN_CACHE_COOK;
1815 }
1816 } else if ((strncasecmp(rep->h + 15, "private", 7) == 0 &&
1817 (rep->h + 22 == ptr || rep->h[22] == ','))
1818 || (strncasecmp(rep->h + 15, "no-store", 8) == 0 &&
1819 (rep->h + 23 == ptr || rep->h[23] == ','))) {
1820 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1821 } else if (strncasecmp(rep->h + 15, "max-age=0", 9) == 0 &&
1822 (rep->h + 24 == ptr || rep->h[24] == ',')) {
1823 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1824 } else if (strncasecmp(rep->h + 15, "s-maxage=0", 10) == 0 &&
1825 (rep->h + 25 == ptr || rep->h[25] == ',')) {
1826 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1827 } else if (strncasecmp(rep->h + 15, "public", 6) == 0 &&
1828 (rep->h + 21 == ptr || rep->h[21] == ',')) {
1829 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
1830 }
1831 }
1832 }
1833
1834 /* check for server cookies */
1835 if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/
1836 && (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL || t->proxy->appsession_name !=NULL)
1837 && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) {
1838 char *p1, *p2, *p3, *p4;
1839
1840 t->flags |= SN_SCK_ANY;
1841
1842 p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */
1843
1844 while (p1 < ptr) { /* in fact, we'll break after the first cookie */
1845 while (p1 < ptr && (isspace((int)*p1)))
1846 p1++;
1847
1848 if (p1 == ptr || *p1 == ';') /* end of cookie */
1849 break;
1850
1851 /* p1 is at the beginning of the cookie name */
1852 p2 = p1;
1853
1854 while (p2 < ptr && *p2 != '=' && *p2 != ';')
1855 p2++;
1856
1857 if (p2 == ptr || *p2 == ';') /* next cookie */
1858 break;
1859
1860 p3 = p2 + 1; /* skips the '=' sign */
1861 if (p3 == ptr)
1862 break;
1863
1864 p4 = p3;
1865 while (p4 < ptr && !isspace((int)*p4) && *p4 != ';')
1866 p4++;
1867
1868 /* here, we have the cookie name between p1 and p2,
1869 * and its value between p3 and p4.
1870 * we can process it.
1871 */
1872
1873 /* first, let's see if we want to capture it */
1874 if (t->proxy->capture_name != NULL &&
1875 t->logs.srv_cookie == NULL &&
1876 (p4 - p1 >= t->proxy->capture_namelen) &&
1877 memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) {
1878 int log_len = p4 - p1;
1879
1880 if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) {
1881 Alert("HTTP logging : out of memory.\n");
1882 }
1883
1884 if (log_len > t->proxy->capture_len)
1885 log_len = t->proxy->capture_len;
1886 memcpy(t->logs.srv_cookie, p1, log_len);
1887 t->logs.srv_cookie[log_len] = 0;
1888 }
1889
1890 if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) &&
1891 (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) {
1892 /* Cool... it's the right one */
1893 t->flags |= SN_SCK_SEEN;
1894
1895 /* If the cookie is in insert mode on a known server, we'll delete
1896 * this occurrence because we'll insert another one later.
1897 * We'll delete it too if the "indirect" option is set and we're in
1898 * a direct access. */
1899 if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) ||
1900 ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) {
1901 /* this header must be deleted */
1902 delete_header = 1;
1903 t->flags |= SN_SCK_DELETED;
1904 }
1905 else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) {
1906 /* replace bytes p3->p4 with the cookie name associated
1907 * with this server since we know it.
1908 */
1909 buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen);
1910 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
1911 }
1912 else if ((t->srv) && (t->proxy->options & PR_O_COOK_PFX)) {
1913 /* insert the cookie name associated with this server
1914 * before existing cookie, and insert a delimitor between them..
1915 */
1916 buffer_replace2(rep, p3, p3, t->srv->cookie, t->srv->cklen + 1);
1917 p3[t->srv->cklen] = COOKIE_DELIM;
1918 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
1919 }
1920 break;
1921 }
1922
1923 /* first, let's see if the cookie is our appcookie*/
1924 if ((t->proxy->appsession_name != NULL) &&
1925 (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) {
1926
1927 /* Cool... it's the right one */
1928
1929 size_t server_id_len = strlen(t->srv->id) + 1;
1930 asession_temp = &local_asession;
1931
1932 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
1933 Alert("Not enought Memory process_srv():asession->sessid:malloc().\n");
1934 send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n");
1935 }
1936 memcpy(asession_temp->sessid, p3, t->proxy->appsession_len);
1937 asession_temp->sessid[t->proxy->appsession_len] = 0;
1938 asession_temp->serverid = NULL;
1939
1940 /* only do insert, if lookup fails */
1941 if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) {
1942 if ((asession_temp = pool_alloc(appsess)) == NULL) {
1943 Alert("Not enought Memory process_srv():asession:calloc().\n");
1944 send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession:calloc().\n");
1945 return 0;
1946 }
1947 asession_temp->sessid = local_asession.sessid;
1948 asession_temp->serverid = local_asession.serverid;
1949 chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp);
1950 }/* end if (chtbl_lookup()) */
1951 else {
1952 /* free wasted memory */
1953 pool_free_to(apools.sessid, local_asession.sessid);
1954 } /* end else from if (chtbl_lookup()) */
1955
1956 if (asession_temp->serverid == NULL) {
1957 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
1958 Alert("Not enought Memory process_srv():asession->sessid:malloc().\n");
1959 send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n");
1960 }
1961 asession_temp->serverid[0] = '\0';
1962 }
1963
1964 if (asession_temp->serverid[0] == '\0')
1965 memcpy(asession_temp->serverid,t->srv->id,server_id_len);
1966
1967 tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout);
1968
1969#if defined(DEBUG_HASH)
1970 print_table(&(t->proxy->htbl_proxy));
1971#endif
1972 break;
1973 }/* end if ((t->proxy->appsession_name != NULL) ... */
1974 else {
1975 // fprintf(stderr,"Ignoring unknown cookie : ");
1976 // write(2, p1, p2-p1);
1977 // fprintf(stderr," = ");
1978 // write(2, p3, p4-p3);
1979 // fprintf(stderr,"\n");
1980 }
1981 break; /* we don't want to loop again since there cannot be another cookie on the same line */
1982 } /* we're now at the end of the cookie value */
1983 } /* end of cookie processing */
1984
1985 /* check for any set-cookie in case we check for cacheability */
1986 if (!delete_header && !(t->flags & SN_SCK_ANY) &&
1987 (t->proxy->options & PR_O_CHK_CACHE) &&
1988 (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) {
1989 t->flags |= SN_SCK_ANY;
1990 }
1991
1992 /* let's look if we have to delete this header */
1993 if (delete_header && !(t->flags & SN_SVDENY))
1994 buffer_replace2(rep, rep->h, rep->lr, "", 0);
1995
1996 rep->h = rep->lr;
1997 } /* while (rep->lr < rep->r) */
1998
1999 /* end of header processing (even if incomplete) */
2000
2001 if ((rep->l < rep->rlim - rep->data) && ! FD_ISSET(t->srv_fd, StaticReadEvent)) {
2002 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
Willy Tarreaud7971282006-07-29 18:36:34 +02002003 * full. We cannot loop here since stream_sock_read will disable it only if
Willy Tarreaubaaee002006-06-26 02:48:02 +02002004 * rep->l == rlim-data
2005 */
2006 FD_SET(t->srv_fd, StaticReadEvent);
2007 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002008 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002009 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002010 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002011 }
2012
2013 /* read error, write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002014 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002015 tv_eternity(&rep->rex);
2016 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002017 fd_delete(t->srv_fd);
2018 if (t->srv) {
2019 t->srv->cur_sess--;
2020 t->srv->failed_resp++;
2021 }
2022 t->proxy->failed_resp++;
2023
2024 t->srv_state = SV_STCLOSE;
2025 t->logs.status = 502;
2026 client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502);
2027 if (!(t->flags & SN_ERR_MASK))
2028 t->flags |= SN_ERR_SRVCL;
2029 if (!(t->flags & SN_FINST_MASK))
2030 t->flags |= SN_FINST_H;
2031 /* We used to have a free connection slot. Since we'll never use it,
2032 * we have to inform the server that it may be used by another session.
2033 */
2034 if (may_dequeue_tasks(t->srv, t->proxy))
2035 task_wakeup(&rq, t->srv->queue_mgt);
2036
2037 return 1;
2038 }
2039 /* end of client write or end of server read.
2040 * since we are in header mode, if there's no space left for headers, we
2041 * won't be able to free more later, so the session will never terminate.
2042 */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002043 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE || rep->l >= rep->rlim - rep->data) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002044 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002045 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002046 shutdown(t->srv_fd, SHUT_RD);
2047 t->srv_state = SV_STSHUTR;
2048 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2049 return 1;
2050 }
2051 /* read timeout : return a 504 to the client.
2052 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002053 else if (FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&rep->rex, &now) <= 0) {
2054 tv_eternity(&rep->rex);
2055 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002056 fd_delete(t->srv_fd);
2057 if (t->srv) {
2058 t->srv->cur_sess--;
2059 t->srv->failed_resp++;
2060 }
2061 t->proxy->failed_resp++;
2062 t->srv_state = SV_STCLOSE;
2063 t->logs.status = 504;
2064 client_return(t, t->proxy->errmsg.len504, t->proxy->errmsg.msg504);
2065 if (!(t->flags & SN_ERR_MASK))
2066 t->flags |= SN_ERR_SRVTO;
2067 if (!(t->flags & SN_FINST_MASK))
2068 t->flags |= SN_FINST_H;
2069 /* We used to have a free connection slot. Since we'll never use it,
2070 * we have to inform the server that it may be used by another session.
2071 */
2072 if (may_dequeue_tasks(t->srv, t->proxy))
2073 task_wakeup(&rq, t->srv->queue_mgt);
2074
2075 return 1;
2076 }
2077 /* last client read and buffer empty */
2078 /* FIXME!!! here, we don't want to switch to SHUTW if the
2079 * client shuts read too early, because we may still have
2080 * some work to do on the headers.
2081 * The side-effect is that if the client completely closes its
2082 * connection during SV_STHEADER, the connection to the server
2083 * is kept until a response comes back or the timeout is reached.
2084 */
2085 else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) {
2086 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002087 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002088
2089 /* We must ensure that the read part is still alive when switching
2090 * to shutw */
2091 FD_SET(t->srv_fd, StaticReadEvent);
2092 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002093 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002094
2095 shutdown(t->srv_fd, SHUT_WR);
2096 t->srv_state = SV_STSHUTW;
2097 return 1;
2098 }
2099 /* write timeout */
2100 /* FIXME!!! here, we don't want to switch to SHUTW if the
2101 * client shuts read too early, because we may still have
2102 * some work to do on the headers.
2103 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002104 else if (FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002105 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002106 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002107 shutdown(t->srv_fd, SHUT_WR);
2108 /* We must ensure that the read part is still alive when switching
2109 * to shutw */
2110 FD_SET(t->srv_fd, StaticReadEvent);
2111 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002112 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002113
2114 /* We must ensure that the read part is still alive when switching
2115 * to shutw */
2116 FD_SET(t->srv_fd, StaticReadEvent);
2117 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002118 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002119
2120 t->srv_state = SV_STSHUTW;
2121 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 return 1;
2126 }
2127
2128 if (req->l == 0) {
2129 if (FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2130 FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002131 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002132 }
2133 }
2134 else { /* client buffer not empty */
2135 if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2136 FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2137 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002138 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002139 /* FIXME: to prevent the server from expiring read timeouts during writes,
2140 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002141 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002142 }
2143 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002144 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002145 }
2146 }
2147
2148 /* be nice with the client side which would like to send a complete header
2149 * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client
2150 * would read all remaining data at once ! The client should not write past rep->lr
2151 * when the server is in header state.
2152 */
2153 //return header_processed;
2154 return t->srv_state != SV_STHEADERS;
2155 }
2156 else if (s == SV_STDATA) {
2157 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002158 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002159 tv_eternity(&rep->rex);
2160 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002161 fd_delete(t->srv_fd);
2162 if (t->srv) {
2163 t->srv->cur_sess--;
2164 t->srv->failed_resp++;
2165 }
2166 t->proxy->failed_resp++;
2167 t->srv_state = SV_STCLOSE;
2168 if (!(t->flags & SN_ERR_MASK))
2169 t->flags |= SN_ERR_SRVCL;
2170 if (!(t->flags & SN_FINST_MASK))
2171 t->flags |= SN_FINST_D;
2172 /* We used to have a free connection slot. Since we'll never use it,
2173 * we have to inform the server that it may be used by another session.
2174 */
2175 if (may_dequeue_tasks(t->srv, t->proxy))
2176 task_wakeup(&rq, t->srv->queue_mgt);
2177
2178 return 1;
2179 }
2180 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002181 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002182 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002183 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002184 shutdown(t->srv_fd, SHUT_RD);
2185 t->srv_state = SV_STSHUTR;
2186 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2187 return 1;
2188 }
2189 /* end of client read and no more data to send */
2190 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
2191 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002192 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002193 shutdown(t->srv_fd, SHUT_WR);
2194 /* We must ensure that the read part is still alive when switching
2195 * to shutw */
2196 FD_SET(t->srv_fd, StaticReadEvent);
2197 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002198 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002199
2200 t->srv_state = SV_STSHUTW;
2201 return 1;
2202 }
2203 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002204 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002205 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002206 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002207 shutdown(t->srv_fd, SHUT_RD);
2208 t->srv_state = SV_STSHUTR;
2209 if (!(t->flags & SN_ERR_MASK))
2210 t->flags |= SN_ERR_SRVTO;
2211 if (!(t->flags & SN_FINST_MASK))
2212 t->flags |= SN_FINST_D;
2213 return 1;
2214 }
2215 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002216 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002217 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002218 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002219 shutdown(t->srv_fd, SHUT_WR);
2220 /* We must ensure that the read part is still alive when switching
2221 * to shutw */
2222 FD_SET(t->srv_fd, StaticReadEvent);
2223 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002224 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002225 t->srv_state = SV_STSHUTW;
2226 if (!(t->flags & SN_ERR_MASK))
2227 t->flags |= SN_ERR_SRVTO;
2228 if (!(t->flags & SN_FINST_MASK))
2229 t->flags |= SN_FINST_D;
2230 return 1;
2231 }
2232
2233 /* recompute request time-outs */
2234 if (req->l == 0) {
2235 if (FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2236 FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002237 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002238 }
2239 }
2240 else { /* buffer not empty, there are still data to be transferred */
2241 if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2242 FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2243 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002244 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002245 /* FIXME: to prevent the server from expiring read timeouts during writes,
2246 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002247 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002248 }
2249 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002250 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002251 }
2252 }
2253
2254 /* recompute response time-outs */
2255 if (rep->l == BUFSIZE) { /* no room to read more data */
2256 if (FD_ISSET(t->srv_fd, StaticReadEvent)) {
2257 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002258 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002259 }
2260 }
2261 else {
2262 if (! FD_ISSET(t->srv_fd, StaticReadEvent)) {
2263 FD_SET(t->srv_fd, StaticReadEvent);
2264 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002265 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002266 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002267 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268 }
2269 }
2270
2271 return 0; /* other cases change nothing */
2272 }
2273 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002274 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002275 //FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002276 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002277 fd_delete(t->srv_fd);
2278 if (t->srv) {
2279 t->srv->cur_sess--;
2280 t->srv->failed_resp++;
2281 }
2282 t->proxy->failed_resp++;
2283 //close(t->srv_fd);
2284 t->srv_state = SV_STCLOSE;
2285 if (!(t->flags & SN_ERR_MASK))
2286 t->flags |= SN_ERR_SRVCL;
2287 if (!(t->flags & SN_FINST_MASK))
2288 t->flags |= SN_FINST_D;
2289 /* We used to have a free connection slot. Since we'll never use it,
2290 * we have to inform the server that it may be used by another session.
2291 */
2292 if (may_dequeue_tasks(t->srv, t->proxy))
2293 task_wakeup(&rq, t->srv->queue_mgt);
2294
2295 return 1;
2296 }
2297 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
2298 //FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002299 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002300 fd_delete(t->srv_fd);
2301 if (t->srv)
2302 t->srv->cur_sess--;
2303 //close(t->srv_fd);
2304 t->srv_state = SV_STCLOSE;
2305 /* We used to have a free connection slot. Since we'll never use it,
2306 * we have to inform the server that it may be used by another session.
2307 */
2308 if (may_dequeue_tasks(t->srv, t->proxy))
2309 task_wakeup(&rq, t->srv->queue_mgt);
2310
2311 return 1;
2312 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002313 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002314 //FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002315 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002316 fd_delete(t->srv_fd);
2317 if (t->srv)
2318 t->srv->cur_sess--;
2319 //close(t->srv_fd);
2320 t->srv_state = SV_STCLOSE;
2321 if (!(t->flags & SN_ERR_MASK))
2322 t->flags |= SN_ERR_SRVTO;
2323 if (!(t->flags & SN_FINST_MASK))
2324 t->flags |= SN_FINST_D;
2325 /* We used to have a free connection slot. Since we'll never use it,
2326 * we have to inform the server that it may be used by another session.
2327 */
2328 if (may_dequeue_tasks(t->srv, t->proxy))
2329 task_wakeup(&rq, t->srv->queue_mgt);
2330
2331 return 1;
2332 }
2333 else if (req->l == 0) {
2334 if (FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2335 FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002336 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002337 }
2338 }
2339 else { /* buffer not empty */
2340 if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2341 FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2342 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002343 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002344 /* FIXME: to prevent the server from expiring read timeouts during writes,
2345 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002346 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002347 }
2348 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002349 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002350 }
2351 }
2352 return 0;
2353 }
2354 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002355 if (rep->flags & BF_READ_ERROR) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002356 //FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002357 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002358 fd_delete(t->srv_fd);
2359 if (t->srv) {
2360 t->srv->cur_sess--;
2361 t->srv->failed_resp++;
2362 }
2363 t->proxy->failed_resp++;
2364 //close(t->srv_fd);
2365 t->srv_state = SV_STCLOSE;
2366 if (!(t->flags & SN_ERR_MASK))
2367 t->flags |= SN_ERR_SRVCL;
2368 if (!(t->flags & SN_FINST_MASK))
2369 t->flags |= SN_FINST_D;
2370 /* We used to have a free connection slot. Since we'll never use it,
2371 * we have to inform the server that it may be used by another session.
2372 */
2373 if (may_dequeue_tasks(t->srv, t->proxy))
2374 task_wakeup(&rq, t->srv->queue_mgt);
2375
2376 return 1;
2377 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002378 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002379 //FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002380 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002381 fd_delete(t->srv_fd);
2382 if (t->srv)
2383 t->srv->cur_sess--;
2384 //close(t->srv_fd);
2385 t->srv_state = SV_STCLOSE;
2386 /* We used to have a free connection slot. Since we'll never use it,
2387 * we have to inform the server that it may be used by another session.
2388 */
2389 if (may_dequeue_tasks(t->srv, t->proxy))
2390 task_wakeup(&rq, t->srv->queue_mgt);
2391
2392 return 1;
2393 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002394 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002395 //FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002396 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002397 fd_delete(t->srv_fd);
2398 if (t->srv)
2399 t->srv->cur_sess--;
2400 //close(t->srv_fd);
2401 t->srv_state = SV_STCLOSE;
2402 if (!(t->flags & SN_ERR_MASK))
2403 t->flags |= SN_ERR_SRVTO;
2404 if (!(t->flags & SN_FINST_MASK))
2405 t->flags |= SN_FINST_D;
2406 /* We used to have a free connection slot. Since we'll never use it,
2407 * we have to inform the server that it may be used by another session.
2408 */
2409 if (may_dequeue_tasks(t->srv, t->proxy))
2410 task_wakeup(&rq, t->srv->queue_mgt);
2411
2412 return 1;
2413 }
2414 else if (rep->l == BUFSIZE) { /* no room to read more data */
2415 if (FD_ISSET(t->srv_fd, StaticReadEvent)) {
2416 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002417 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002418 }
2419 }
2420 else {
2421 if (! FD_ISSET(t->srv_fd, StaticReadEvent)) {
2422 FD_SET(t->srv_fd, StaticReadEvent);
2423 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002424 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002425 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002426 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002427 }
2428 }
2429 return 0;
2430 }
2431 else { /* SV_STCLOSE : nothing to do */
2432 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2433 int len;
2434 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
2435 write(1, trash, len);
2436 }
2437 return 0;
2438 }
2439 return 0;
2440}
2441
2442
2443/*
2444 * Produces data for the session <s> depending on its source. Expects to be
2445 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
2446 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
2447 * session, which it uses to keep on being called when there is free space in
2448 * the buffer, of simply by letting an empty buffer upon return. It returns 1
2449 * if it changes the session state from CL_STSHUTR, otherwise 0.
2450 */
2451int produce_content(struct session *s)
2452{
2453 struct buffer *rep = s->rep;
2454 struct proxy *px;
2455 struct server *sv;
2456 int msglen;
2457
2458 if (s->data_source == DATA_SRC_NONE) {
2459 s->flags &= ~SN_SELF_GEN;
2460 return 1;
2461 }
2462 else if (s->data_source == DATA_SRC_STATS) {
2463 msglen = 0;
2464
2465 if (s->data_state == DATA_ST_INIT) { /* the function had not been called yet */
2466 unsigned int up;
2467
2468 s->flags |= SN_SELF_GEN; // more data will follow
2469
2470 /* send the start of the HTTP response */
2471 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2472 "HTTP/1.0 200 OK\r\n"
2473 "Cache-Control: no-cache\r\n"
2474 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +02002475 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +02002476 "\r\n\r\n");
2477
2478 s->logs.status = 200;
2479 client_retnclose(s, msglen, trash); // send the start of the response.
2480 msglen = 0;
2481
2482 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
2483 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
2484 if (!(s->flags & SN_FINST_MASK))
2485 s->flags |= SN_FINST_R;
2486
2487 /* WARNING! This must fit in the first buffer !!! */
2488 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2489 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
2490 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2491 "<style type=\"text/css\"><!--\n"
2492 "body {"
2493 " font-family: helvetica, arial;"
2494 " font-size: 12px;"
2495 " font-weight: normal;"
2496 " color: black;"
2497 " background: white;"
2498 "}\n"
2499 "td {"
2500 " font-size: 12px;"
2501 " align: center;"
2502 "}\n"
2503 "h1 {"
2504 " font-size: xx-large;"
2505 " margin-bottom: 0.5em;"
2506 "}\n"
2507 "h2 {"
2508 " font-family: helvetica, arial;"
2509 " font-size: x-large;"
2510 " font-weight: bold;"
2511 " font-style: italic;"
2512 " color: #6020a0;"
2513 " margin-top: 0em;"
2514 " margin-bottom: 0em;"
2515 "}\n"
2516 "h3 {"
2517 " font-family: helvetica, arial;"
2518 " font-size: 16px;"
2519 " font-weight: bold;"
2520 " color: #b00040;"
2521 " background: #e8e8d0;"
2522 " margin-top: 0em;"
2523 " margin-bottom: 0em;"
2524 "}\n"
2525 "li {"
2526 " margin-top: 0.25em;"
2527 " margin-right: 2em;"
2528 "}\n"
2529 ".hr {"
2530 " margin-top: 0.25em;"
2531 " border-color: black;"
2532 " border-bottom-style: solid;"
2533 "}\n"
2534 "table.tbl { border-collapse: collapse; border-width: 1px; border-style: solid; border-color: gray;}\n"
2535 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; border-color: gray; }\n"
2536 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray; }\n"
2537 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2538 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2539 "-->"
2540 "</style></head>");
2541
2542 if (buffer_write(rep, trash, msglen) != 0)
2543 return 0;
2544 msglen = 0;
2545
2546 up = (now.tv_sec - start_date.tv_sec);
2547
2548 /* WARNING! this has to fit the first packet too */
2549 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2550 "<body><h1>" PRODUCT_NAME "</h1>\n"
2551 "<h2>Statistics Report for pid %d</h2>\n"
2552 "<hr width=\"100%%\" class=\"hr\">\n"
2553 "<h3>&gt; General process information</h3>\n"
2554 "<table border=0><tr><td align=\"left\">\n"
2555 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
2556 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2557 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
2558 "<b>maxsock = </b> %d<br>\n"
2559 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
2560 "</td><td width=\"10%%\">\n"
2561 "</td><td align=\"right\">\n"
2562 "<table class=\"lgd\">"
2563 "<tr><td bgcolor=\"#C0FFC0\">&nbsp;</td><td style=\"border-style: none;\">active UP </td>"
2564 "<td bgcolor=\"#B0D0FF\">&nbsp;</td><td style=\"border-style: none;\">backup UP </td></tr>"
2565 "<tr><td bgcolor=\"#FFFFA0\"></td><td style=\"border-style: none;\">active UP, going down </td>"
2566 "<td bgcolor=\"#C060FF\"></td><td style=\"border-style: none;\">backup UP, going down </td></tr>"
2567 "<tr><td bgcolor=\"#FFD020\"></td><td style=\"border-style: none;\">active DOWN, going up </td>"
2568 "<td bgcolor=\"#FF80FF\"></td><td style=\"border-style: none;\">backup DOWN, going up </td></tr>"
2569 "<tr><td bgcolor=\"#FF9090\"></td><td style=\"border-style: none;\">active or backup DOWN &nbsp;</td>"
2570 "<td bgcolor=\"#E0E0E0\"></td><td style=\"border-style: none;\">not checked </td></tr>"
2571 "</table>\n"
2572 "</tr></table>\n"
2573 "",
2574 pid, pid, global.nbproc,
2575 up / 86400, (up % 86400) / 3600,
2576 (up % 3600) / 60, (up % 60),
2577 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2578 global.rlimit_memmax ? " MB" : "",
2579 global.rlimit_nofile,
2580 global.maxsock,
2581 global.maxconn,
2582 actconn
2583 );
2584
2585 if (buffer_write(rep, trash, msglen) != 0)
2586 return 0;
2587 msglen = 0;
2588
2589 s->data_state = DATA_ST_DATA;
2590 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
2591
2592 px = s->data_ctx.stats.px = proxy;
2593 s->data_ctx.stats.px_st = DATA_ST_INIT;
2594 }
2595
2596 while (s->data_ctx.stats.px) {
2597 int dispatch_sess, dispatch_cum;
2598 int failed_checks, down_trans;
2599 int failed_secu, failed_conns, failed_resp;
2600
2601 if (s->data_ctx.stats.px_st == DATA_ST_INIT) {
2602 /* we are on a new proxy */
2603 px = s->data_ctx.stats.px;
2604
2605 /* skip the disabled proxies */
2606 if (px->state == PR_STSTOPPED)
2607 goto next_proxy;
2608
2609 if (s->proxy->uri_auth && s->proxy->uri_auth->scope) {
2610 /* we have a limited scope, we have to check the proxy name */
2611 struct stat_scope *scope;
2612 int len;
2613
2614 len = strlen(px->id);
2615 scope = s->proxy->uri_auth->scope;
2616
2617 while (scope) {
2618 /* match exact proxy name */
2619 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2620 break;
2621
2622 /* match '.' which means 'self' proxy */
2623 if (!strcmp(scope->px_id, ".") && px == s->proxy)
2624 break;
2625 scope = scope->next;
2626 }
2627
2628 /* proxy name not found */
2629 if (scope == NULL)
2630 goto next_proxy;
2631 }
2632
2633 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2634 "<h3>&gt; Proxy instance %s : "
2635 "%d conns (maxconn=%d), %d queued (%d unassigned), %d total conns</h3>\n"
2636 "",
2637 px->id,
2638 px->nbconn, px->maxconn, px->totpend, px->nbpend, px->cum_conn);
2639
2640 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2641 "<table cols=\"16\" class=\"tbl\">\n"
2642 "<tr align=\"center\" bgcolor=\"#20C0C0\">"
2643 "<th colspan=5>Server</th>"
2644 "<th colspan=2>Queue</th>"
2645 "<th colspan=4>Sessions</th>"
2646 "<th colspan=5>Errors</th></tr>\n"
2647 "<tr align=\"center\" bgcolor=\"#20C0C0\">"
2648 "<th>Name</th><th>Weight</th><th>Status</th><th>Act.</th><th>Bck.</th>"
2649 "<th>Curr.</th><th>Max.</th>"
2650 "<th>Curr.</th><th>Max.</th><th>Limit</th><th>Cumul.</th>"
2651 "<th>Conn.</th><th>Resp.</th><th>Sec.</th><th>Check</th><th>Down</th></tr>\n");
2652
2653 if (buffer_write(rep, trash, msglen) != 0)
2654 return 0;
2655 msglen = 0;
2656
2657 s->data_ctx.stats.sv = px->srv;
2658 s->data_ctx.stats.px_st = DATA_ST_DATA;
2659 }
2660
2661 px = s->data_ctx.stats.px;
2662
2663 /* stats.sv has been initialized above */
2664 while (s->data_ctx.stats.sv != NULL) {
2665 static char *act_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FFD020", /*failing*/"#FFFFA0", /*up*/"#C0FFC0", /*unchecked*/"#E0E0E0" };
2666 static char *bck_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FF80ff", /*failing*/"#C060FF", /*up*/"#B0D0FF", /*unchecked*/"#E0E0E0" };
2667 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
2668 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP */
2669
2670 sv = s->data_ctx.stats.sv;
2671
2672 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
2673 if (!(sv->state & SRV_CHECKED))
2674 sv_state = 4;
2675 else if (sv->state & SRV_RUNNING)
2676 if (sv->health == sv->rise + sv->fall - 1)
2677 sv_state = 3; /* UP */
2678 else
2679 sv_state = 2; /* going down */
2680 else
2681 if (sv->health)
2682 sv_state = 1; /* going up */
2683 else
2684 sv_state = 0; /* DOWN */
2685
2686 /* name, weight */
2687 msglen += snprintf(trash, sizeof(trash),
2688 "<tr align=center bgcolor=\"%s\"><td>%s</td><td>%d</td><td>",
2689 (sv->state & SRV_BACKUP) ? bck_tab_bg[sv_state] : act_tab_bg[sv_state],
2690 sv->id, sv->uweight+1);
2691 /* status */
2692 msglen += snprintf(trash + msglen, sizeof(trash) - msglen, srv_hlt_st[sv_state],
2693 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
2694 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
2695
2696 /* act, bck */
2697 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2698 "</td><td>%s</td><td>%s</td>",
2699 (sv->state & SRV_BACKUP) ? "-" : "Y",
2700 (sv->state & SRV_BACKUP) ? "Y" : "-");
2701
2702 /* queue : current, max */
2703 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2704 "<td align=right>%d</td><td align=right>%d</td>",
2705 sv->nbpend, sv->nbpend_max);
2706
2707 /* sessions : current, max, limit, cumul */
2708 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2709 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>",
2710 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess);
2711
2712 /* errors : connect, response, security */
2713 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2714 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2715 sv->failed_conns, sv->failed_resp, sv->failed_secu);
2716
2717 /* check failures : unique, fatal */
2718 if (sv->state & SRV_CHECKED)
2719 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2720 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
2721 sv->failed_checks, sv->down_trans);
2722 else
2723 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2724 "<td align=right>-</td><td align=right>-</td></tr>\n");
2725
2726 if (buffer_write(rep, trash, msglen) != 0)
2727 return 0;
2728 msglen = 0;
2729
2730 s->data_ctx.stats.sv = sv->next;
2731 } /* while sv */
2732
2733 /* now we are past the last server, we'll dump information about the dispatcher */
2734
2735 /* We have to count down from the proxy to the servers to tell how
2736 * many sessions are on the dispatcher, and how many checks have
2737 * failed. We cannot count this during the servers dump because it
2738 * might be interrupted multiple times.
2739 */
2740 dispatch_sess = px->nbconn;
2741 dispatch_cum = px->cum_conn;
2742 failed_secu = px->failed_secu;
2743 failed_conns = px->failed_conns;
2744 failed_resp = px->failed_resp;
2745 failed_checks = down_trans = 0;
2746
2747 sv = px->srv;
2748 while (sv) {
2749 dispatch_sess -= sv->cur_sess;
2750 dispatch_cum -= sv->cum_sess;
2751 failed_conns -= sv->failed_conns;
2752 failed_resp -= sv->failed_resp;
2753 failed_secu -= sv->failed_secu;
2754 if (sv->state & SRV_CHECKED) {
2755 failed_checks += sv->failed_checks;
2756 down_trans += sv->down_trans;
2757 }
2758 sv = sv->next;
2759 }
2760
2761 /* name, weight, status, act, bck */
2762 msglen += snprintf(trash + msglen, sizeof(trash),
2763 "<tr align=center bgcolor=\"#e8e8d0\">"
2764 "<td>Dispatcher</td><td>-</td>"
2765 "<td>%s</td><td>-</td><td>-</td>",
2766 px->state == PR_STRUN ? "UP" : "DOWN");
2767
2768 /* queue : current, max */
2769 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2770 "<td align=right>%d</td><td align=right>%d</td>",
2771 px->nbpend, px->nbpend_max);
2772
2773 /* sessions : current, max, limit, cumul. */
2774 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2775 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>",
2776 dispatch_sess, px->nbconn_max, px->maxconn, dispatch_cum);
2777
2778 /* errors : connect, response, security */
2779 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2780 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2781 failed_conns, failed_resp, failed_secu);
2782
2783 /* check failures : unique, fatal */
2784 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2785 "<td align=right>-</td><td align=right>-</td></tr>\n");
2786
2787
2788 /* now the summary for the whole proxy */
2789 /* name, weight, status, act, bck */
2790 msglen += snprintf(trash + msglen, sizeof(trash),
2791 "<tr align=center style=\"color: #ffff80; background: #20C0C0;\">"
2792 "<td><b>Total</b></td><td>-</td>"
2793 "<td><b>%s</b></td><td><b>%d</b></td><td><b>%d</b></td>",
2794 (px->state == PR_STRUN && ((px->srv == NULL) || px->srv_act || px->srv_bck)) ? "UP" : "DOWN",
2795 px->srv_act, px->srv_bck);
2796
2797 /* queue : current, max */
2798 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2799 "<td align=right><b>%d</b></td><td align=right><b>%d</b></td>",
2800 px->totpend, px->nbpend_max);
2801
2802 /* sessions : current, max, limit, cumul */
2803 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2804 "<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>",
2805 px->nbconn, px->nbconn_max, px->maxconn, px->cum_conn);
2806
2807 /* errors : connect, response, security */
2808 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2809 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2810 px->failed_conns, px->failed_resp, px->failed_secu);
2811
2812 /* check failures : unique, fatal */
2813 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2814 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
2815 failed_checks, down_trans);
2816
2817 msglen += snprintf(trash + msglen, sizeof(trash) - msglen, "</table><p>\n");
2818
2819 if (buffer_write(rep, trash, msglen) != 0)
2820 return 0;
2821 msglen = 0;
2822
2823 s->data_ctx.stats.px_st = DATA_ST_INIT;
2824 next_proxy:
2825 s->data_ctx.stats.px = px->next;
2826 } /* proxy loop */
2827 /* here, we just have reached the sv == NULL and px == NULL */
2828 s->flags &= ~SN_SELF_GEN;
2829 return 1;
2830 }
2831 else {
2832 /* unknown data source */
2833 s->logs.status = 500;
2834 client_retnclose(s, s->proxy->errmsg.len500, s->proxy->errmsg.msg500);
2835 if (!(s->flags & SN_ERR_MASK))
2836 s->flags |= SN_ERR_PRXCOND;
2837 if (!(s->flags & SN_FINST_MASK))
2838 s->flags |= SN_FINST_R;
2839 s->flags &= SN_SELF_GEN;
2840 return 1;
2841 }
2842}
2843
2844
2845/*
2846 * Local variables:
2847 * c-indent-level: 8
2848 * c-basic-offset: 8
2849 * End:
2850 */