blob: 51a910a7306c001b4e088d87f520d0c662bff48e [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) {
Willy Tarreau08fa2e32006-09-03 10:47:37 +0200461 t->req->l = 0;
462 /* flush the request so that we can drop the connection early
463 * if the client closes first.
464 */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200465 tv_delayfrom(&req->cex, &now,
466 t->proxy->contimeout ? t->proxy->contimeout : 0);
467 }
468
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 goto process_data;
470 }
471
472 /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */
473 if (ptr > req->r - 2) {
474 /* this is a partial header, let's wait for more to come */
475 req->lr = ptr;
476 break;
477 }
478
479 /* now we know that *ptr is either \r or \n,
480 * and that there are at least 1 char after it.
481 */
482 if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n'))
483 req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */
484 else
485 req->lr = ptr + 2; /* \r\n or \n\r */
486
487 /*
488 * now we know that we have a full header ; we can do whatever
489 * we want with these pointers :
490 * req->h = beginning of header
491 * ptr = end of header (first \r or \n)
492 * req->lr = beginning of next line (next rep->h)
493 * req->r = end of data (not used at this stage)
494 */
495
496 if (!method_checked && (t->proxy->appsession_name != NULL) &&
497 ((memcmp(req->h, "GET ", 4) == 0) || (memcmp(req->h, "POST ", 4) == 0)) &&
498 ((request_line = memchr(req->h, ';', req->lr - req->h)) != NULL)) {
499
500 /* skip ; */
501 request_line++;
502
503 /* look if we have a jsessionid */
504
505 if (strncasecmp(request_line, t->proxy->appsession_name, t->proxy->appsession_name_len) == 0) {
506
507 /* skip jsessionid= */
508 request_line += t->proxy->appsession_name_len + 1;
509
510 /* First try if we allready have an appsession */
511 asession_temp = &local_asession;
512
513 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
514 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
515 send_log(t->proxy, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
516 return 0;
517 }
518
519 /* Copy the sessionid */
520 memcpy(asession_temp->sessid, request_line, t->proxy->appsession_len);
521 asession_temp->sessid[t->proxy->appsession_len] = 0;
522 asession_temp->serverid = NULL;
523
524 /* only do insert, if lookup fails */
525 if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *)&asession_temp)) {
526 if ((asession_temp = pool_alloc(appsess)) == NULL) {
527 Alert("Not enough memory process_cli():asession:calloc().\n");
528 send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
529 return 0;
530 }
531 asession_temp->sessid = local_asession.sessid;
532 asession_temp->serverid = local_asession.serverid;
533 chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp);
534 } /* end if (chtbl_lookup()) */
535 else {
536 /*free wasted memory;*/
537 pool_free_to(apools.sessid, local_asession.sessid);
538 }
539
540 tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout);
541 asession_temp->request_count++;
542
543#if defined(DEBUG_HASH)
544 print_table(&(t->proxy->htbl_proxy));
545#endif
546
547 if (asession_temp->serverid == NULL) {
548 Alert("Found Application Session without matching server.\n");
549 } else {
550 struct server *srv = t->proxy->srv;
551 while (srv) {
552 if (strcmp(srv->id, asession_temp->serverid) == 0) {
553 if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) {
554 /* we found the server and it's usable */
555 t->flags &= ~SN_CK_MASK;
556 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
557 t->srv = srv;
558 break;
559 } else {
560 t->flags &= ~SN_CK_MASK;
561 t->flags |= SN_CK_DOWN;
562 }
563 } /* end if (strcmp()) */
564 srv = srv->next;
565 }/* end while(srv) */
566 }/* end else of if (asession_temp->serverid == NULL) */
567 }/* end if (strncasecmp(request_line,t->proxy->appsession_name,apssesion_name_len) == 0) */
568 else {
569 //fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>NO SESSION\n");
570 }
571 method_checked = 1;
572 } /* end if (!method_checked ...) */
573 else{
574 //printf("No Methode-Header with Session-String\n");
575 }
576
577 if (t->logs.logwait & LW_REQ) {
578 /* we have a complete HTTP request that we must log */
579 int urilen;
580
581 if ((t->logs.uri = pool_alloc(requri)) == NULL) {
582 Alert("HTTP logging : out of memory.\n");
583 t->logs.status = 500;
584 client_retnclose(t, t->proxy->errmsg.len500, t->proxy->errmsg.msg500);
585 if (!(t->flags & SN_ERR_MASK))
586 t->flags |= SN_ERR_PRXCOND;
587 if (!(t->flags & SN_FINST_MASK))
588 t->flags |= SN_FINST_R;
589 return 1;
590 }
591
592 urilen = ptr - req->h;
593 if (urilen >= REQURI_LEN)
594 urilen = REQURI_LEN - 1;
595 memcpy(t->logs.uri, req->h, urilen);
596 t->logs.uri[urilen] = 0;
597
598 if (!(t->logs.logwait &= ~LW_REQ))
599 sess_log(t);
600 }
601 else if (t->logs.logwait & LW_REQHDR) {
602 struct cap_hdr *h;
603 int len;
604 for (h = t->proxy->req_cap; h; h = h->next) {
605 if ((h->namelen + 2 <= ptr - req->h) &&
606 (req->h[h->namelen] == ':') &&
607 (strncasecmp(req->h, h->name, h->namelen) == 0)) {
608
609 if (t->req_cap[h->index] == NULL)
610 t->req_cap[h->index] = pool_alloc_from(h->pool, h->len + 1);
611
612 len = ptr - (req->h + h->namelen + 2);
613 if (len > h->len)
614 len = h->len;
615
616 memcpy(t->req_cap[h->index], req->h + h->namelen + 2, len);
617 t->req_cap[h->index][len]=0;
618 }
619 }
620
621 }
622
623 delete_header = 0;
624
625 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
626 int len, max;
627 len = sprintf(trash, "%08x:%s.clihdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
628 max = ptr - req->h;
629 UBOUND(max, sizeof(trash) - len - 1);
630 len += strlcpy2(trash + len, req->h, max + 1);
631 trash[len++] = '\n';
632 write(1, trash, len);
633 }
634
635
636 /* remove "connection: " if needed */
637 if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE)
638 && (strncasecmp(req->h, "Connection: ", 12) == 0)) {
639 delete_header = 1;
640 }
641
642 /* try headers regexps */
643 if (!delete_header && t->proxy->req_exp != NULL
644 && !(t->flags & SN_CLDENY)) {
645 struct hdr_exp *exp;
646 char term;
647
648 term = *ptr;
649 *ptr = '\0';
650 exp = t->proxy->req_exp;
651 do {
652 if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) {
653 switch (exp->action) {
654 case ACT_ALLOW:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200655 if (!(t->flags & (SN_CLDENY | SN_CLTARPIT)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200656 t->flags |= SN_CLALLOW;
657 break;
658 case ACT_REPLACE:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200659 if (!(t->flags & (SN_CLDENY | SN_CLTARPIT))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660 int len = exp_replace(trash, req->h, exp->replace, pmatch);
661 ptr += buffer_replace2(req, req->h, ptr, trash, len);
662 }
663 break;
664 case ACT_REMOVE:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200665 if (!(t->flags & (SN_CLDENY | SN_CLTARPIT)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666 delete_header = 1;
667 break;
668 case ACT_DENY:
Willy Tarreaub8750a82006-09-03 09:56:00 +0200669 if (!(t->flags & (SN_CLALLOW | SN_CLTARPIT)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670 t->flags |= SN_CLDENY;
671 break;
Willy Tarreaub8750a82006-09-03 09:56:00 +0200672 case ACT_TARPIT:
673 if (!(t->flags & (SN_CLALLOW | SN_CLDENY)))
674 t->flags |= SN_CLTARPIT;
675 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676 case ACT_PASS: /* we simply don't deny this one */
677 break;
678 }
679 break;
680 }
681 } while ((exp = exp->next) != NULL);
682 *ptr = term; /* restore the string terminator */
683 }
684
685 /* Now look for cookies. Conforming to RFC2109, we have to support
686 * attributes whose name begin with a '$', and associate them with
687 * the right cookie, if we want to delete this cookie.
688 * So there are 3 cases for each cookie read :
689 * 1) it's a special attribute, beginning with a '$' : ignore it.
690 * 2) it's a server id cookie that we *MAY* want to delete : save
691 * some pointers on it (last semi-colon, beginning of cookie...)
692 * 3) it's an application cookie : we *MAY* have to delete a previous
693 * "special" cookie.
694 * At the end of loop, if a "special" cookie remains, we may have to
695 * remove it. If no application cookie persists in the header, we
696 * *MUST* delete it
697 */
698 if (!delete_header &&
699 (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL || t->proxy->appsession_name !=NULL)
Willy Tarreaub8750a82006-09-03 09:56:00 +0200700 && !(t->flags & (SN_CLDENY|SN_CLTARPIT)) && (ptr >= req->h + 8)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200701 && (strncasecmp(req->h, "Cookie: ", 8) == 0)) {
702 char *p1, *p2, *p3, *p4;
703 char *del_colon, *del_cookie, *colon;
704 int app_cookies;
705
706 p1 = req->h + 8; /* first char after 'Cookie: ' */
707 colon = p1;
708 /* del_cookie == NULL => nothing to be deleted */
709 del_colon = del_cookie = NULL;
710 app_cookies = 0;
711
712 while (p1 < ptr) {
713 /* skip spaces and colons, but keep an eye on these ones */
714 while (p1 < ptr) {
715 if (*p1 == ';' || *p1 == ',')
716 colon = p1;
717 else if (!isspace((int)*p1))
718 break;
719 p1++;
720 }
721
722 if (p1 == ptr)
723 break;
724
725 /* p1 is at the beginning of the cookie name */
726 p2 = p1;
727 while (p2 < ptr && *p2 != '=')
728 p2++;
729
730 if (p2 == ptr)
731 break;
732
733 p3 = p2 + 1; /* skips the '=' sign */
734 if (p3 == ptr)
735 break;
736
737 p4 = p3;
738 while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
739 p4++;
740
741 /* here, we have the cookie name between p1 and p2,
742 * and its value between p3 and p4.
743 * we can process it :
744 *
745 * Cookie: NAME=VALUE;
746 * | || || |
747 * | || || +--> p4
748 * | || |+-------> p3
749 * | || +--------> p2
750 * | |+------------> p1
751 * | +-------------> colon
752 * +--------------------> req->h
753 */
754
755 if (*p1 == '$') {
756 /* skip this one */
757 }
758 else {
759 /* first, let's see if we want to capture it */
760 if (t->proxy->capture_name != NULL &&
761 t->logs.cli_cookie == NULL &&
762 (p4 - p1 >= t->proxy->capture_namelen) &&
763 memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) {
764 int log_len = p4 - p1;
765
766 if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) {
767 Alert("HTTP logging : out of memory.\n");
768 } else {
769 if (log_len > t->proxy->capture_len)
770 log_len = t->proxy->capture_len;
771 memcpy(t->logs.cli_cookie, p1, log_len);
772 t->logs.cli_cookie[log_len] = 0;
773 }
774 }
775
776 if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) &&
777 (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) {
778 /* Cool... it's the right one */
779 struct server *srv = t->proxy->srv;
780 char *delim;
781
782 /* if we're in cookie prefix mode, we'll search the delimitor so that we
783 * have the server ID betweek p3 and delim, and the original cookie between
784 * delim+1 and p4. Otherwise, delim==p4 :
785 *
786 * Cookie: NAME=SRV~VALUE;
787 * | || || | |
788 * | || || | +--> p4
789 * | || || +--------> delim
790 * | || |+-----------> p3
791 * | || +------------> p2
792 * | |+----------------> p1
793 * | +-----------------> colon
794 * +------------------------> req->h
795 */
796
797 if (t->proxy->options & PR_O_COOK_PFX) {
798 for (delim = p3; delim < p4; delim++)
799 if (*delim == COOKIE_DELIM)
800 break;
801 }
802 else
803 delim = p4;
804
805
806 /* Here, we'll look for the first running server which supports the cookie.
807 * This allows to share a same cookie between several servers, for example
808 * to dedicate backup servers to specific servers only.
809 * However, to prevent clients from sticking to cookie-less backup server
810 * when they have incidentely learned an empty cookie, we simply ignore
811 * empty cookies and mark them as invalid.
812 */
813 if (delim == p3)
814 srv = NULL;
815
816 while (srv) {
817 if ((srv->cklen == delim - p3) && !memcmp(p3, srv->cookie, delim - p3)) {
818 if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) {
819 /* we found the server and it's usable */
820 t->flags &= ~SN_CK_MASK;
821 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
822 t->srv = srv;
823 break;
824 } else {
825 /* we found a server, but it's down */
826 t->flags &= ~SN_CK_MASK;
827 t->flags |= SN_CK_DOWN;
828 }
829 }
830 srv = srv->next;
831 }
832
833 if (!srv && !(t->flags & SN_CK_DOWN)) {
834 /* no server matched this cookie */
835 t->flags &= ~SN_CK_MASK;
836 t->flags |= SN_CK_INVALID;
837 }
838
839 /* depending on the cookie mode, we may have to either :
840 * - delete the complete cookie if we're in insert+indirect mode, so that
841 * the server never sees it ;
842 * - remove the server id from the cookie value, and tag the cookie as an
843 * application cookie so that it does not get accidentely removed later,
844 * if we're in cookie prefix mode
845 */
846 if ((t->proxy->options & PR_O_COOK_PFX) && (delim != p4)) {
847 buffer_replace2(req, p3, delim + 1, NULL, 0);
848 p4 -= (delim + 1 - p3);
849 ptr -= (delim + 1 - p3);
850 del_cookie = del_colon = NULL;
851 app_cookies++; /* protect the header from deletion */
852 }
853 else if (del_cookie == NULL &&
854 (t->proxy->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
855 del_cookie = p1;
856 del_colon = colon;
857 }
858 } else {
859 /* now we know that we must keep this cookie since it's
860 * not ours. But if we wanted to delete our cookie
861 * earlier, we cannot remove the complete header, but we
862 * can remove the previous block itself.
863 */
864 app_cookies++;
865
866 if (del_cookie != NULL) {
867 buffer_replace2(req, del_cookie, p1, NULL, 0);
868 p4 -= (p1 - del_cookie);
869 ptr -= (p1 - del_cookie);
870 del_cookie = del_colon = NULL;
871 }
872 }
873
874 if ((t->proxy->appsession_name != NULL) &&
875 (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) {
876 /* first, let's see if the cookie is our appcookie*/
877
878 /* Cool... it's the right one */
879
880 asession_temp = &local_asession;
881
882 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
883 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
884 send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
885 return 0;
886 }
887
888 memcpy(asession_temp->sessid, p3, t->proxy->appsession_len);
889 asession_temp->sessid[t->proxy->appsession_len] = 0;
890 asession_temp->serverid = NULL;
891
892 /* only do insert, if lookup fails */
893 if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) {
894 if ((asession_temp = pool_alloc(appsess)) == NULL) {
895 Alert("Not enough memory process_cli():asession:calloc().\n");
896 send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
897 return 0;
898 }
899
900 asession_temp->sessid = local_asession.sessid;
901 asession_temp->serverid = local_asession.serverid;
902 chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp);
903 } else {
904 /* free wasted memory */
905 pool_free_to(apools.sessid, local_asession.sessid);
906 }
907
908 if (asession_temp->serverid == NULL) {
909 Alert("Found Application Session without matching server.\n");
910 } else {
911 struct server *srv = t->proxy->srv;
912 while (srv) {
913 if (strcmp(srv->id, asession_temp->serverid) == 0) {
914 if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) {
915 /* we found the server and it's usable */
916 t->flags &= ~SN_CK_MASK;
917 t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED;
918 t->srv = srv;
919 break;
920 } else {
921 t->flags &= ~SN_CK_MASK;
922 t->flags |= SN_CK_DOWN;
923 }
924 }
925 srv = srv->next;
926 }/* end while(srv) */
927 }/* end else if server == NULL */
928
929 tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout);
930 }/* end if ((t->proxy->appsession_name != NULL) ... */
931 }
932
933 /* we'll have to look for another cookie ... */
934 p1 = p4;
935 } /* while (p1 < ptr) */
936
937 /* There's no more cookie on this line.
938 * We may have marked the last one(s) for deletion.
939 * We must do this now in two ways :
940 * - if there is no app cookie, we simply delete the header ;
941 * - if there are app cookies, we must delete the end of the
942 * string properly, including the colon/semi-colon before
943 * the cookie name.
944 */
945 if (del_cookie != NULL) {
946 if (app_cookies) {
947 buffer_replace2(req, del_colon, ptr, NULL, 0);
948 /* WARNING! <ptr> becomes invalid for now. If some code
949 * below needs to rely on it before the end of the global
950 * header loop, we need to correct it with this code :
951 */
952 ptr = del_colon;
953 }
954 else
955 delete_header = 1;
956 }
957 } /* end of cookie processing on this header */
958
959 /* let's look if we have to delete this header */
Willy Tarreaub8750a82006-09-03 09:56:00 +0200960 if (delete_header && !(t->flags & (SN_CLDENY|SN_CLTARPIT))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200961 buffer_replace2(req, req->h, req->lr, NULL, 0);
962 /* WARNING: ptr is not valid anymore, since the header may have
963 * been deleted or truncated ! */
964 } else {
965 /* try to catch the first line as the request */
966 if (t->req_line.len < 0) {
967 t->req_line.str = req->h;
968 t->req_line.len = ptr - req->h;
969 }
970
971 /* We might also need the 'Authorization: ' header */
972 if (t->auth_hdr.len < 0 &&
973 t->proxy->uri_auth != NULL &&
974 ptr > req->h + 15 &&
975 !strncasecmp("Authorization: ", req->h, 15)) {
976 t->auth_hdr.str = req->h;
977 t->auth_hdr.len = ptr - req->h;
978 }
979 }
980
981 req->h = req->lr;
982 } /* while (req->lr < req->r) */
983
984 /* end of header processing (even if incomplete) */
985
986 if ((req->l < req->rlim - req->data) && ! FD_ISSET(t->cli_fd, StaticReadEvent)) {
987 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
Willy Tarreaud7971282006-07-29 18:36:34 +0200988 * full. We cannot loop here since stream_sock_read will disable it only if
Willy Tarreaubaaee002006-06-26 02:48:02 +0200989 * req->l == rlim-data
990 */
991 FD_SET(t->cli_fd, StaticReadEvent);
992 if (t->proxy->clitimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +0200993 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200994 else
Willy Tarreaud7971282006-07-29 18:36:34 +0200995 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200996 }
997
998 /* Since we are in header mode, if there's no space left for headers, we
999 * won't be able to free more later, so the session will never terminate.
1000 */
1001 if (req->l >= req->rlim - req->data) {
1002 t->logs.status = 400;
1003 client_retnclose(t, t->proxy->errmsg.len400, t->proxy->errmsg.msg400);
1004 if (!(t->flags & SN_ERR_MASK))
1005 t->flags |= SN_ERR_PRXCOND;
1006 if (!(t->flags & SN_FINST_MASK))
1007 t->flags |= SN_FINST_R;
1008 return 1;
1009 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001010 else if (req->flags & (BF_READ_ERROR | BF_READ_NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011 /* read error, or last read : give up. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001012 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001013 fd_delete(t->cli_fd);
1014 t->cli_state = CL_STCLOSE;
1015 if (!(t->flags & SN_ERR_MASK))
1016 t->flags |= SN_ERR_CLICL;
1017 if (!(t->flags & SN_FINST_MASK))
1018 t->flags |= SN_FINST_R;
1019 return 1;
1020 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001021 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001022
1023 /* read timeout : give up with an error message.
1024 */
1025 t->logs.status = 408;
1026 client_retnclose(t, t->proxy->errmsg.len408, t->proxy->errmsg.msg408);
1027 if (!(t->flags & SN_ERR_MASK))
1028 t->flags |= SN_ERR_CLITO;
1029 if (!(t->flags & SN_FINST_MASK))
1030 t->flags |= SN_FINST_R;
1031 return 1;
1032 }
1033
1034 return t->cli_state != CL_STHEADERS;
1035 }
1036 else if (c == CL_STDATA) {
1037 process_data:
1038 /* FIXME: this error handling is partly buggy because we always report
1039 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1040 * or HEADER phase. BTW, it's not logical to expire the client while
1041 * we're waiting for the server to connect.
1042 */
1043 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001044 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001045 tv_eternity(&req->rex);
1046 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001047 fd_delete(t->cli_fd);
1048 t->cli_state = CL_STCLOSE;
1049 if (!(t->flags & SN_ERR_MASK))
1050 t->flags |= SN_ERR_CLICL;
1051 if (!(t->flags & SN_FINST_MASK)) {
1052 if (t->pend_pos)
1053 t->flags |= SN_FINST_Q;
1054 else if (s == SV_STCONN)
1055 t->flags |= SN_FINST_C;
1056 else
1057 t->flags |= SN_FINST_D;
1058 }
1059 return 1;
1060 }
1061 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001062 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001063 FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001064 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001065 shutdown(t->cli_fd, SHUT_RD);
1066 t->cli_state = CL_STSHUTR;
1067 return 1;
1068 }
1069 /* last server read and buffer empty */
1070 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
1071 FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001072 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001073 shutdown(t->cli_fd, SHUT_WR);
1074 /* We must ensure that the read part is still alive when switching
1075 * to shutw */
1076 FD_SET(t->cli_fd, StaticReadEvent);
1077 if (t->proxy->clitimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001078 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079 t->cli_state = CL_STSHUTW;
1080 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1081 return 1;
1082 }
1083 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001084 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001085 FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001086 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001087 shutdown(t->cli_fd, SHUT_RD);
1088 t->cli_state = CL_STSHUTR;
1089 if (!(t->flags & SN_ERR_MASK))
1090 t->flags |= SN_ERR_CLITO;
1091 if (!(t->flags & SN_FINST_MASK)) {
1092 if (t->pend_pos)
1093 t->flags |= SN_FINST_Q;
1094 else if (s == SV_STCONN)
1095 t->flags |= SN_FINST_C;
1096 else
1097 t->flags |= SN_FINST_D;
1098 }
1099 return 1;
1100 }
1101 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001102 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001103 FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001104 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001105 shutdown(t->cli_fd, SHUT_WR);
1106 /* We must ensure that the read part is still alive when switching
1107 * to shutw */
1108 FD_SET(t->cli_fd, StaticReadEvent);
1109 if (t->proxy->clitimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001110 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001111
1112 t->cli_state = CL_STSHUTW;
1113 if (!(t->flags & SN_ERR_MASK))
1114 t->flags |= SN_ERR_CLITO;
1115 if (!(t->flags & SN_FINST_MASK)) {
1116 if (t->pend_pos)
1117 t->flags |= SN_FINST_Q;
1118 else if (s == SV_STCONN)
1119 t->flags |= SN_FINST_C;
1120 else
1121 t->flags |= SN_FINST_D;
1122 }
1123 return 1;
1124 }
1125
1126 if (req->l >= req->rlim - req->data) {
1127 /* no room to read more data */
1128 if (FD_ISSET(t->cli_fd, StaticReadEvent)) {
1129 /* stop reading until we get some space */
1130 FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001131 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001132 }
1133 } else {
1134 /* there's still some space in the buffer */
1135 if (! FD_ISSET(t->cli_fd, StaticReadEvent)) {
1136 FD_SET(t->cli_fd, StaticReadEvent);
1137 if (!t->proxy->clitimeout ||
1138 (t->srv_state < SV_STDATA && t->proxy->srvtimeout))
1139 /* If the client has no timeout, or if the server not ready yet, and we
1140 * know for sure that it can expire, then it's cleaner to disable the
1141 * timeout on the client side so that too low values cannot make the
1142 * sessions abort too early.
1143 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001144 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001145 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001146 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001147 }
1148 }
1149
1150 if ((rep->l == 0) ||
1151 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
1152 if (FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1153 FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001154 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001155 }
1156 } else {
1157 /* buffer not empty */
1158 if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1159 FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
1160 if (t->proxy->clitimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001161 tv_delayfrom(&rep->wex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001162 /* FIXME: to prevent the client from expiring read timeouts during writes,
1163 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001164 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001165 }
1166 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001167 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001168 }
1169 }
1170 return 0; /* other cases change nothing */
1171 }
1172 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001173 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001174 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001175 fd_delete(t->cli_fd);
1176 t->cli_state = CL_STCLOSE;
1177 if (!(t->flags & SN_ERR_MASK))
1178 t->flags |= SN_ERR_CLICL;
1179 if (!(t->flags & SN_FINST_MASK)) {
1180 if (t->pend_pos)
1181 t->flags |= SN_FINST_Q;
1182 else if (s == SV_STCONN)
1183 t->flags |= SN_FINST_C;
1184 else
1185 t->flags |= SN_FINST_D;
1186 }
1187 return 1;
1188 }
1189 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
1190 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001191 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001192 fd_delete(t->cli_fd);
1193 t->cli_state = CL_STCLOSE;
1194 return 1;
1195 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001196 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
1197 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001198 fd_delete(t->cli_fd);
1199 t->cli_state = CL_STCLOSE;
1200 if (!(t->flags & SN_ERR_MASK))
1201 t->flags |= SN_ERR_CLITO;
1202 if (!(t->flags & SN_FINST_MASK)) {
1203 if (t->pend_pos)
1204 t->flags |= SN_FINST_Q;
1205 else if (s == SV_STCONN)
1206 t->flags |= SN_FINST_C;
1207 else
1208 t->flags |= SN_FINST_D;
1209 }
1210 return 1;
1211 }
1212
1213 if (t->flags & SN_SELF_GEN) {
1214 produce_content(t);
1215 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001216 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001217 fd_delete(t->cli_fd);
1218 t->cli_state = CL_STCLOSE;
1219 return 1;
1220 }
1221 }
1222
1223 if ((rep->l == 0)
1224 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
1225 if (FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1226 FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001227 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001228 }
1229 } else {
1230 /* buffer not empty */
1231 if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1232 FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
1233 if (t->proxy->clitimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001234 tv_delayfrom(&rep->wex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001235 /* FIXME: to prevent the client from expiring read timeouts during writes,
1236 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001237 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001238 }
1239 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001240 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001241 }
1242 }
1243 return 0;
1244 }
1245 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001246 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001247 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001248 fd_delete(t->cli_fd);
1249 t->cli_state = CL_STCLOSE;
1250 if (!(t->flags & SN_ERR_MASK))
1251 t->flags |= SN_ERR_CLICL;
1252 if (!(t->flags & SN_FINST_MASK)) {
1253 if (t->pend_pos)
1254 t->flags |= SN_FINST_Q;
1255 else if (s == SV_STCONN)
1256 t->flags |= SN_FINST_C;
1257 else
1258 t->flags |= SN_FINST_D;
1259 }
1260 return 1;
1261 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001262 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001263 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001264 fd_delete(t->cli_fd);
1265 t->cli_state = CL_STCLOSE;
1266 return 1;
1267 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001268 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
1269 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001270 fd_delete(t->cli_fd);
1271 t->cli_state = CL_STCLOSE;
1272 if (!(t->flags & SN_ERR_MASK))
1273 t->flags |= SN_ERR_CLITO;
1274 if (!(t->flags & SN_FINST_MASK)) {
1275 if (t->pend_pos)
1276 t->flags |= SN_FINST_Q;
1277 else if (s == SV_STCONN)
1278 t->flags |= SN_FINST_C;
1279 else
1280 t->flags |= SN_FINST_D;
1281 }
1282 return 1;
1283 }
1284 else if (req->l >= req->rlim - req->data) {
1285 /* no room to read more data */
1286
1287 /* FIXME-20050705: is it possible for a client to maintain a session
1288 * after the timeout by sending more data after it receives a close ?
1289 */
1290
1291 if (FD_ISSET(t->cli_fd, StaticReadEvent)) {
1292 /* stop reading until we get some space */
1293 FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001294 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001295 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1296 }
1297 } else {
1298 /* there's still some space in the buffer */
1299 if (! FD_ISSET(t->cli_fd, StaticReadEvent)) {
1300 FD_SET(t->cli_fd, StaticReadEvent);
1301 if (t->proxy->clitimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001302 tv_delayfrom(&req->rex, &now, t->proxy->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001303 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001304 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001305 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1306 }
1307 }
1308 return 0;
1309 }
1310 else { /* CL_STCLOSE: nothing to do */
1311 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1312 int len;
1313 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);
1314 write(1, trash, len);
1315 }
1316 return 0;
1317 }
1318 return 0;
1319}
1320
1321
1322/*
1323 * manages the server FSM and its socket. It returns 1 if a state has changed
1324 * (and a resync may be needed), 0 else.
1325 */
1326int process_srv(struct session *t)
1327{
1328 int s = t->srv_state;
1329 int c = t->cli_state;
1330 struct buffer *req = t->req;
1331 struct buffer *rep = t->rep;
1332 appsess *asession_temp = NULL;
1333 appsess local_asession;
1334 int conn_err;
1335
1336#ifdef DEBUG_FULL
1337 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
1338#endif
1339 //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s,
1340 //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent),
1341 //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent)
1342 //);
1343 if (s == SV_STIDLE) {
1344 if (c == CL_STHEADERS)
1345 return 0; /* stay in idle, waiting for data to reach the client side */
1346 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
1347 (c == CL_STSHUTR &&
1348 (t->req->l == 0 || t->proxy->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02001349 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001350 if (t->pend_pos)
1351 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1352 /* note that this must not return any error because it would be able to
1353 * overwrite the client_retnclose() output.
1354 */
Willy Tarreau08fa2e32006-09-03 10:47:37 +02001355 if (t->flags & SN_CLTARPIT)
1356 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, 0, NULL);
1357 else
1358 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001359
1360 return 1;
1361 }
1362 else {
Willy Tarreaub8750a82006-09-03 09:56:00 +02001363 if (t->flags & SN_CLTARPIT) {
1364 /* This connection is being tarpitted. The CLIENT side has
1365 * already set the connect expiration date to the right
1366 * timeout. We just have to check that it has not expired.
1367 */
1368 if (tv_cmp2_ms(&req->cex, &now) > 0)
1369 return 0;
1370
1371 /* We will set the queue timer to the time spent, just for
1372 * logging purposes. We fake a 500 server error, so that the
1373 * attacker will not suspect his connection has been tarpitted.
1374 * It will not cause trouble to the logs because we can exclude
1375 * the tarpitted connections by filtering on the 'PT' status flags.
1376 */
1377 tv_eternity(&req->cex);
1378 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1379 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
1380 500, t->proxy->errmsg.len500, t->proxy->errmsg.msg500);
1381 return 1;
1382 }
1383
Willy Tarreaubaaee002006-06-26 02:48:02 +02001384 /* Right now, we will need to create a connection to the server.
1385 * We might already have tried, and got a connection pending, in
1386 * which case we will not do anything till it's pending. It's up
1387 * to any other session to release it and wake us up again.
1388 */
1389 if (t->pend_pos) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001390 if (tv_cmp2_ms(&req->cex, &now) > 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001391 return 0;
1392 else {
1393 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02001394 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001395 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1396 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
1397 503, t->proxy->errmsg.len503, t->proxy->errmsg.msg503);
1398 if (t->srv)
1399 t->srv->failed_conns++;
1400 t->proxy->failed_conns++;
1401 return 1;
1402 }
1403 }
1404
1405 do {
1406 /* first, get a connection */
1407 if (srv_redispatch_connect(t))
1408 return t->srv_state != SV_STIDLE;
1409
1410 /* try to (re-)connect to the server, and fail if we expire the
1411 * number of retries.
1412 */
1413 if (srv_retryable_connect(t)) {
1414 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1415 return t->srv_state != SV_STIDLE;
1416 }
1417
1418 } while (1);
1419 }
1420 }
1421 else if (s == SV_STCONN) { /* connection in progress */
1422 if (c == CL_STCLOSE || c == CL_STSHUTW ||
1423 (c == CL_STSHUTR &&
1424 (t->req->l == 0 || t->proxy->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02001425 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001426 fd_delete(t->srv_fd);
1427 if (t->srv)
1428 t->srv->cur_sess--;
1429
1430 /* note that this must not return any error because it would be able to
1431 * overwrite the client_retnclose() output.
1432 */
1433 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, 0, NULL);
1434 return 1;
1435 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001436 if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) {
1437 //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 +02001438 return 0; /* nothing changed */
1439 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001440 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001441 /* timeout, asynchronous connect error or first write error */
1442 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
1443
1444 fd_delete(t->srv_fd);
1445 if (t->srv)
1446 t->srv->cur_sess--;
1447
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001448 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001449 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
1450 else
1451 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
1452
1453 /* ensure that we have enough retries left */
1454 if (srv_count_retry_down(t, conn_err))
1455 return 1;
1456
1457 do {
1458 /* Now we will try to either reconnect to the same server or
1459 * connect to another server. If the connection gets queued
1460 * because all servers are saturated, then we will go back to
1461 * the SV_STIDLE state.
1462 */
1463 if (srv_retryable_connect(t)) {
1464 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1465 return t->srv_state != SV_STCONN;
1466 }
1467
1468 /* we need to redispatch the connection to another server */
1469 if (srv_redispatch_connect(t))
1470 return t->srv_state != SV_STCONN;
1471 } while (1);
1472 }
1473 else { /* no error or write 0 */
1474 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
1475
1476 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
1477 if (req->l == 0) /* nothing to write */ {
1478 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001479 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001480 } else /* need the right to write */ {
1481 FD_SET(t->srv_fd, StaticWriteEvent);
1482 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001483 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001484 /* FIXME: to prevent the server from expiring read timeouts during writes,
1485 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001486 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001487 }
1488 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001489 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001490 }
1491
1492 if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
1493 FD_SET(t->srv_fd, StaticReadEvent);
1494 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001495 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001496 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001497 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001498
1499 t->srv_state = SV_STDATA;
1500 if (t->srv)
1501 t->srv->cum_sess++;
1502 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
1503
1504 /* if the user wants to log as soon as possible, without counting
1505 bytes from the server, then this is the right moment. */
1506 if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) {
1507 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
1508 sess_log(t);
1509 }
1510 }
1511 else {
1512 t->srv_state = SV_STHEADERS;
1513 if (t->srv)
1514 t->srv->cum_sess++;
1515 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
1516 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001517 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001518 return 1;
1519 }
1520 }
1521 else if (s == SV_STHEADERS) { /* receiving server headers */
1522 /* now parse the partial (or complete) headers */
1523 while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */
1524 char *ptr;
1525 int delete_header;
1526
1527 ptr = rep->lr;
1528
1529 /* look for the end of the current header */
1530 while (ptr < rep->r && *ptr != '\n' && *ptr != '\r')
1531 ptr++;
1532
1533 if (ptr == rep->h) {
1534 int line, len;
1535
1536 /* we can only get here after an end of headers */
1537
1538 /* first, we'll block if security checks have caught nasty things */
1539 if (t->flags & SN_CACHEABLE) {
1540 if ((t->flags & SN_CACHE_COOK) &&
1541 (t->flags & SN_SCK_ANY) &&
1542 (t->proxy->options & PR_O_CHK_CACHE)) {
1543
1544 /* we're in presence of a cacheable response containing
1545 * a set-cookie header. We'll block it as requested by
1546 * the 'checkcache' option, and send an alert.
1547 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001548 tv_eternity(&rep->rex);
1549 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001550 fd_delete(t->srv_fd);
1551 if (t->srv) {
1552 t->srv->cur_sess--;
1553 t->srv->failed_secu++;
1554 }
1555 t->proxy->failed_secu++;
1556 t->srv_state = SV_STCLOSE;
1557 t->logs.status = 502;
1558 client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502);
1559 if (!(t->flags & SN_ERR_MASK))
1560 t->flags |= SN_ERR_PRXCOND;
1561 if (!(t->flags & SN_FINST_MASK))
1562 t->flags |= SN_FINST_H;
1563
1564 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id);
1565 send_log(t->proxy, LOG_ALERT, "Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id);
1566
1567 /* We used to have a free connection slot. Since we'll never use it,
1568 * we have to inform the server that it may be used by another session.
1569 */
1570 if (may_dequeue_tasks(t->srv, t->proxy))
1571 task_wakeup(&rq, t->srv->queue_mgt);
1572
1573 return 1;
1574 }
1575 }
1576
1577 /* next, we'll block if an 'rspideny' or 'rspdeny' filter matched */
1578 if (t->flags & SN_SVDENY) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001579 tv_eternity(&rep->rex);
1580 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001581 fd_delete(t->srv_fd);
1582 if (t->srv) {
1583 t->srv->cur_sess--;
1584 t->srv->failed_secu++;
1585 }
1586 t->proxy->failed_secu++;
1587 t->srv_state = SV_STCLOSE;
1588 t->logs.status = 502;
1589 client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502);
1590 if (!(t->flags & SN_ERR_MASK))
1591 t->flags |= SN_ERR_PRXCOND;
1592 if (!(t->flags & SN_FINST_MASK))
1593 t->flags |= SN_FINST_H;
1594 /* We used to have a free connection slot. Since we'll never use it,
1595 * we have to inform the server that it may be used by another session.
1596 */
1597 if (may_dequeue_tasks(t->srv, t->proxy))
1598 task_wakeup(&rq, t->srv->queue_mgt);
1599
1600 return 1;
1601 }
1602
1603 /* we'll have something else to do here : add new headers ... */
1604
1605 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS) &&
1606 (!(t->proxy->options & PR_O_COOK_POST) || (t->flags & SN_POST))) {
1607 /* the server is known, it's not the one the client requested, we have to
1608 * insert a set-cookie here, except if we want to insert only on POST
1609 * requests and this one isn't. Note that servers which don't have cookies
1610 * (eg: some backup servers) will return a full cookie removal request.
1611 */
1612 len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n",
1613 t->proxy->cookie_name,
1614 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
1615
1616 t->flags |= SN_SCK_INSERTED;
1617
1618 /* Here, we will tell an eventual cache on the client side that we don't
1619 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1620 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1621 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1622 */
1623 if (t->proxy->options & PR_O_COOK_NOC)
1624 //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n");
1625 len += sprintf(trash + len, "Cache-control: private\r\n");
1626
1627 if (rep->data + rep->l < rep->h)
1628 /* The data has been stolen, we will crash cleanly instead of corrupting memory */
1629 *(int *)0 = 0;
1630 buffer_replace2(rep, rep->h, rep->h, trash, len);
1631 }
1632
1633 /* headers to be added */
1634 for (line = 0; line < t->proxy->nb_rspadd; line++) {
1635 len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]);
1636 buffer_replace2(rep, rep->h, rep->h, trash, len);
1637 }
1638
1639 /* add a "connection: close" line if needed */
1640 if (t->proxy->options & PR_O_HTTP_CLOSE)
1641 buffer_replace2(rep, rep->h, rep->h, "Connection: close\r\n", 19);
1642
1643 t->srv_state = SV_STDATA;
1644 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
1645 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
1646
1647 /* client connection already closed or option 'httpclose' required :
1648 * we close the server's outgoing connection right now.
1649 */
1650 if ((req->l == 0) &&
1651 (c == CL_STSHUTR || c == CL_STCLOSE || t->proxy->options & PR_O_FORCE_CLO)) {
1652 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001653 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001654
1655 /* We must ensure that the read part is still alive when switching
1656 * to shutw */
1657 FD_SET(t->srv_fd, StaticReadEvent);
1658 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001659 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001660
1661 shutdown(t->srv_fd, SHUT_WR);
1662 t->srv_state = SV_STSHUTW;
1663 }
1664
1665 /* if the user wants to log as soon as possible, without counting
1666 bytes from the server, then this is the right moment. */
1667 if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) {
1668 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
1669 t->logs.bytes = rep->h - rep->data;
1670 sess_log(t);
1671 }
1672 break;
1673 }
1674
1675 /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */
1676 if (ptr > rep->r - 2) {
1677 /* this is a partial header, let's wait for more to come */
1678 rep->lr = ptr;
1679 break;
1680 }
1681
1682 // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r);
1683 // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n");
1684
1685 /* now we know that *ptr is either \r or \n,
1686 * and that there are at least 1 char after it.
1687 */
1688 if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n'))
1689 rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */
1690 else
1691 rep->lr = ptr + 2; /* \r\n or \n\r */
1692
1693 /*
1694 * now we know that we have a full header ; we can do whatever
1695 * we want with these pointers :
1696 * rep->h = beginning of header
1697 * ptr = end of header (first \r or \n)
1698 * rep->lr = beginning of next line (next rep->h)
1699 * rep->r = end of data (not used at this stage)
1700 */
1701
1702
1703 if (t->logs.status == -1) {
1704 t->logs.logwait &= ~LW_RESP;
1705 t->logs.status = atoi(rep->h + 9);
1706 switch (t->logs.status) {
1707 case 200:
1708 case 203:
1709 case 206:
1710 case 300:
1711 case 301:
1712 case 410:
1713 /* RFC2616 @13.4:
1714 * "A response received with a status code of
1715 * 200, 203, 206, 300, 301 or 410 MAY be stored
1716 * by a cache (...) unless a cache-control
1717 * directive prohibits caching."
1718 *
1719 * RFC2616 @9.5: POST method :
1720 * "Responses to this method are not cacheable,
1721 * unless the response includes appropriate
1722 * Cache-Control or Expires header fields."
1723 */
1724 if (!(t->flags & SN_POST) && (t->proxy->options & PR_O_CHK_CACHE))
1725 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
1726 break;
1727 default:
1728 break;
1729 }
1730 }
1731 else if (t->logs.logwait & LW_RSPHDR) {
1732 struct cap_hdr *h;
1733 int len;
1734 for (h = t->proxy->rsp_cap; h; h = h->next) {
1735 if ((h->namelen + 2 <= ptr - rep->h) &&
1736 (rep->h[h->namelen] == ':') &&
1737 (strncasecmp(rep->h, h->name, h->namelen) == 0)) {
1738
1739 if (t->rsp_cap[h->index] == NULL)
1740 t->rsp_cap[h->index] = pool_alloc_from(h->pool, h->len + 1);
1741
1742 len = ptr - (rep->h + h->namelen + 2);
1743 if (len > h->len)
1744 len = h->len;
1745
1746 memcpy(t->rsp_cap[h->index], rep->h + h->namelen + 2, len);
1747 t->rsp_cap[h->index][len]=0;
1748 }
1749 }
1750
1751 }
1752
1753 delete_header = 0;
1754
1755 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1756 int len, max;
1757 len = sprintf(trash, "%08x:%s.srvhdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
1758 max = ptr - rep->h;
1759 UBOUND(max, sizeof(trash) - len - 1);
1760 len += strlcpy2(trash + len, rep->h, max + 1);
1761 trash[len++] = '\n';
1762 write(1, trash, len);
1763 }
1764
1765 /* remove "connection: " if needed */
1766 if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE)
1767 && (strncasecmp(rep->h, "Connection: ", 12) == 0)) {
1768 delete_header = 1;
1769 }
1770
1771 /* try headers regexps */
1772 if (!delete_header && t->proxy->rsp_exp != NULL
1773 && !(t->flags & SN_SVDENY)) {
1774 struct hdr_exp *exp;
1775 char term;
1776
1777 term = *ptr;
1778 *ptr = '\0';
1779 exp = t->proxy->rsp_exp;
1780 do {
1781 if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) {
1782 switch (exp->action) {
1783 case ACT_ALLOW:
1784 if (!(t->flags & SN_SVDENY))
1785 t->flags |= SN_SVALLOW;
1786 break;
1787 case ACT_REPLACE:
1788 if (!(t->flags & SN_SVDENY)) {
1789 int len = exp_replace(trash, rep->h, exp->replace, pmatch);
1790 ptr += buffer_replace2(rep, rep->h, ptr, trash, len);
1791 }
1792 break;
1793 case ACT_REMOVE:
1794 if (!(t->flags & SN_SVDENY))
1795 delete_header = 1;
1796 break;
1797 case ACT_DENY:
1798 if (!(t->flags & SN_SVALLOW))
1799 t->flags |= SN_SVDENY;
1800 break;
1801 case ACT_PASS: /* we simply don't deny this one */
1802 break;
1803 }
1804 break;
1805 }
1806 } while ((exp = exp->next) != NULL);
1807 *ptr = term; /* restore the string terminator */
1808 }
1809
1810 /* check for cache-control: or pragma: headers */
1811 if (!delete_header && (t->flags & SN_CACHEABLE)) {
1812 if (strncasecmp(rep->h, "Pragma: no-cache", 16) == 0)
1813 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1814 else if (strncasecmp(rep->h, "Cache-control: ", 15) == 0) {
1815 if (strncasecmp(rep->h + 15, "no-cache", 8) == 0) {
1816 if (rep->h + 23 == ptr || rep->h[23] == ',')
1817 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1818 else {
1819 if (strncasecmp(rep->h + 23, "=\"set-cookie", 12) == 0
1820 && (rep->h[35] == '"' || rep->h[35] == ','))
1821 t->flags &= ~SN_CACHE_COOK;
1822 }
1823 } else if ((strncasecmp(rep->h + 15, "private", 7) == 0 &&
1824 (rep->h + 22 == ptr || rep->h[22] == ','))
1825 || (strncasecmp(rep->h + 15, "no-store", 8) == 0 &&
1826 (rep->h + 23 == ptr || rep->h[23] == ','))) {
1827 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1828 } else if (strncasecmp(rep->h + 15, "max-age=0", 9) == 0 &&
1829 (rep->h + 24 == ptr || rep->h[24] == ',')) {
1830 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1831 } else if (strncasecmp(rep->h + 15, "s-maxage=0", 10) == 0 &&
1832 (rep->h + 25 == ptr || rep->h[25] == ',')) {
1833 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1834 } else if (strncasecmp(rep->h + 15, "public", 6) == 0 &&
1835 (rep->h + 21 == ptr || rep->h[21] == ',')) {
1836 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
1837 }
1838 }
1839 }
1840
1841 /* check for server cookies */
1842 if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/
1843 && (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL || t->proxy->appsession_name !=NULL)
1844 && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) {
1845 char *p1, *p2, *p3, *p4;
1846
1847 t->flags |= SN_SCK_ANY;
1848
1849 p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */
1850
1851 while (p1 < ptr) { /* in fact, we'll break after the first cookie */
1852 while (p1 < ptr && (isspace((int)*p1)))
1853 p1++;
1854
1855 if (p1 == ptr || *p1 == ';') /* end of cookie */
1856 break;
1857
1858 /* p1 is at the beginning of the cookie name */
1859 p2 = p1;
1860
1861 while (p2 < ptr && *p2 != '=' && *p2 != ';')
1862 p2++;
1863
1864 if (p2 == ptr || *p2 == ';') /* next cookie */
1865 break;
1866
1867 p3 = p2 + 1; /* skips the '=' sign */
1868 if (p3 == ptr)
1869 break;
1870
1871 p4 = p3;
1872 while (p4 < ptr && !isspace((int)*p4) && *p4 != ';')
1873 p4++;
1874
1875 /* here, we have the cookie name between p1 and p2,
1876 * and its value between p3 and p4.
1877 * we can process it.
1878 */
1879
1880 /* first, let's see if we want to capture it */
1881 if (t->proxy->capture_name != NULL &&
1882 t->logs.srv_cookie == NULL &&
1883 (p4 - p1 >= t->proxy->capture_namelen) &&
1884 memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) {
1885 int log_len = p4 - p1;
1886
1887 if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) {
1888 Alert("HTTP logging : out of memory.\n");
1889 }
1890
1891 if (log_len > t->proxy->capture_len)
1892 log_len = t->proxy->capture_len;
1893 memcpy(t->logs.srv_cookie, p1, log_len);
1894 t->logs.srv_cookie[log_len] = 0;
1895 }
1896
1897 if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) &&
1898 (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) {
1899 /* Cool... it's the right one */
1900 t->flags |= SN_SCK_SEEN;
1901
1902 /* If the cookie is in insert mode on a known server, we'll delete
1903 * this occurrence because we'll insert another one later.
1904 * We'll delete it too if the "indirect" option is set and we're in
1905 * a direct access. */
1906 if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) ||
1907 ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) {
1908 /* this header must be deleted */
1909 delete_header = 1;
1910 t->flags |= SN_SCK_DELETED;
1911 }
1912 else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) {
1913 /* replace bytes p3->p4 with the cookie name associated
1914 * with this server since we know it.
1915 */
1916 buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen);
1917 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
1918 }
1919 else if ((t->srv) && (t->proxy->options & PR_O_COOK_PFX)) {
1920 /* insert the cookie name associated with this server
1921 * before existing cookie, and insert a delimitor between them..
1922 */
1923 buffer_replace2(rep, p3, p3, t->srv->cookie, t->srv->cklen + 1);
1924 p3[t->srv->cklen] = COOKIE_DELIM;
1925 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
1926 }
1927 break;
1928 }
1929
1930 /* first, let's see if the cookie is our appcookie*/
1931 if ((t->proxy->appsession_name != NULL) &&
1932 (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) {
1933
1934 /* Cool... it's the right one */
1935
1936 size_t server_id_len = strlen(t->srv->id) + 1;
1937 asession_temp = &local_asession;
1938
1939 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
1940 Alert("Not enought Memory process_srv():asession->sessid:malloc().\n");
1941 send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n");
1942 }
1943 memcpy(asession_temp->sessid, p3, t->proxy->appsession_len);
1944 asession_temp->sessid[t->proxy->appsession_len] = 0;
1945 asession_temp->serverid = NULL;
1946
1947 /* only do insert, if lookup fails */
1948 if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) {
1949 if ((asession_temp = pool_alloc(appsess)) == NULL) {
1950 Alert("Not enought Memory process_srv():asession:calloc().\n");
1951 send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession:calloc().\n");
1952 return 0;
1953 }
1954 asession_temp->sessid = local_asession.sessid;
1955 asession_temp->serverid = local_asession.serverid;
1956 chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp);
1957 }/* end if (chtbl_lookup()) */
1958 else {
1959 /* free wasted memory */
1960 pool_free_to(apools.sessid, local_asession.sessid);
1961 } /* end else from if (chtbl_lookup()) */
1962
1963 if (asession_temp->serverid == NULL) {
1964 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
1965 Alert("Not enought Memory process_srv():asession->sessid:malloc().\n");
1966 send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n");
1967 }
1968 asession_temp->serverid[0] = '\0';
1969 }
1970
1971 if (asession_temp->serverid[0] == '\0')
1972 memcpy(asession_temp->serverid,t->srv->id,server_id_len);
1973
1974 tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout);
1975
1976#if defined(DEBUG_HASH)
1977 print_table(&(t->proxy->htbl_proxy));
1978#endif
1979 break;
1980 }/* end if ((t->proxy->appsession_name != NULL) ... */
1981 else {
1982 // fprintf(stderr,"Ignoring unknown cookie : ");
1983 // write(2, p1, p2-p1);
1984 // fprintf(stderr," = ");
1985 // write(2, p3, p4-p3);
1986 // fprintf(stderr,"\n");
1987 }
1988 break; /* we don't want to loop again since there cannot be another cookie on the same line */
1989 } /* we're now at the end of the cookie value */
1990 } /* end of cookie processing */
1991
1992 /* check for any set-cookie in case we check for cacheability */
1993 if (!delete_header && !(t->flags & SN_SCK_ANY) &&
1994 (t->proxy->options & PR_O_CHK_CACHE) &&
1995 (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) {
1996 t->flags |= SN_SCK_ANY;
1997 }
1998
1999 /* let's look if we have to delete this header */
2000 if (delete_header && !(t->flags & SN_SVDENY))
2001 buffer_replace2(rep, rep->h, rep->lr, "", 0);
2002
2003 rep->h = rep->lr;
2004 } /* while (rep->lr < rep->r) */
2005
2006 /* end of header processing (even if incomplete) */
2007
2008 if ((rep->l < rep->rlim - rep->data) && ! FD_ISSET(t->srv_fd, StaticReadEvent)) {
2009 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
Willy Tarreaud7971282006-07-29 18:36:34 +02002010 * full. We cannot loop here since stream_sock_read will disable it only if
Willy Tarreaubaaee002006-06-26 02:48:02 +02002011 * rep->l == rlim-data
2012 */
2013 FD_SET(t->srv_fd, StaticReadEvent);
2014 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002015 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002016 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002017 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002018 }
2019
2020 /* read error, write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002021 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002022 tv_eternity(&rep->rex);
2023 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002024 fd_delete(t->srv_fd);
2025 if (t->srv) {
2026 t->srv->cur_sess--;
2027 t->srv->failed_resp++;
2028 }
2029 t->proxy->failed_resp++;
2030
2031 t->srv_state = SV_STCLOSE;
2032 t->logs.status = 502;
2033 client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502);
2034 if (!(t->flags & SN_ERR_MASK))
2035 t->flags |= SN_ERR_SRVCL;
2036 if (!(t->flags & SN_FINST_MASK))
2037 t->flags |= SN_FINST_H;
2038 /* We used to have a free connection slot. Since we'll never use it,
2039 * we have to inform the server that it may be used by another session.
2040 */
2041 if (may_dequeue_tasks(t->srv, t->proxy))
2042 task_wakeup(&rq, t->srv->queue_mgt);
2043
2044 return 1;
2045 }
2046 /* end of client write or end of server read.
2047 * since we are in header mode, if there's no space left for headers, we
2048 * won't be able to free more later, so the session will never terminate.
2049 */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002050 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 +02002051 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002052 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002053 shutdown(t->srv_fd, SHUT_RD);
2054 t->srv_state = SV_STSHUTR;
2055 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2056 return 1;
2057 }
2058 /* read timeout : return a 504 to the client.
2059 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002060 else if (FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&rep->rex, &now) <= 0) {
2061 tv_eternity(&rep->rex);
2062 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002063 fd_delete(t->srv_fd);
2064 if (t->srv) {
2065 t->srv->cur_sess--;
2066 t->srv->failed_resp++;
2067 }
2068 t->proxy->failed_resp++;
2069 t->srv_state = SV_STCLOSE;
2070 t->logs.status = 504;
2071 client_return(t, t->proxy->errmsg.len504, t->proxy->errmsg.msg504);
2072 if (!(t->flags & SN_ERR_MASK))
2073 t->flags |= SN_ERR_SRVTO;
2074 if (!(t->flags & SN_FINST_MASK))
2075 t->flags |= SN_FINST_H;
2076 /* We used to have a free connection slot. Since we'll never use it,
2077 * we have to inform the server that it may be used by another session.
2078 */
2079 if (may_dequeue_tasks(t->srv, t->proxy))
2080 task_wakeup(&rq, t->srv->queue_mgt);
2081
2082 return 1;
2083 }
2084 /* last client read and buffer empty */
2085 /* FIXME!!! here, we don't want to switch to SHUTW if the
2086 * client shuts read too early, because we may still have
2087 * some work to do on the headers.
2088 * The side-effect is that if the client completely closes its
2089 * connection during SV_STHEADER, the connection to the server
2090 * is kept until a response comes back or the timeout is reached.
2091 */
2092 else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) {
2093 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002094 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002095
2096 /* We must ensure that the read part is still alive when switching
2097 * to shutw */
2098 FD_SET(t->srv_fd, StaticReadEvent);
2099 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002100 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002101
2102 shutdown(t->srv_fd, SHUT_WR);
2103 t->srv_state = SV_STSHUTW;
2104 return 1;
2105 }
2106 /* write timeout */
2107 /* FIXME!!! here, we don't want to switch to SHUTW if the
2108 * client shuts read too early, because we may still have
2109 * some work to do on the headers.
2110 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002111 else if (FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002112 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002113 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002114 shutdown(t->srv_fd, SHUT_WR);
2115 /* We must ensure that the read part is still alive when switching
2116 * to shutw */
2117 FD_SET(t->srv_fd, StaticReadEvent);
2118 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002119 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002120
2121 /* We must ensure that the read part is still alive when switching
2122 * to shutw */
2123 FD_SET(t->srv_fd, StaticReadEvent);
2124 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002125 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002126
2127 t->srv_state = SV_STSHUTW;
2128 if (!(t->flags & SN_ERR_MASK))
2129 t->flags |= SN_ERR_SRVTO;
2130 if (!(t->flags & SN_FINST_MASK))
2131 t->flags |= SN_FINST_H;
2132 return 1;
2133 }
2134
2135 if (req->l == 0) {
2136 if (FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2137 FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002138 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002139 }
2140 }
2141 else { /* client buffer not empty */
2142 if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2143 FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2144 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002145 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002146 /* FIXME: to prevent the server from expiring read timeouts during writes,
2147 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002148 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002149 }
2150 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002151 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002152 }
2153 }
2154
2155 /* be nice with the client side which would like to send a complete header
2156 * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client
2157 * would read all remaining data at once ! The client should not write past rep->lr
2158 * when the server is in header state.
2159 */
2160 //return header_processed;
2161 return t->srv_state != SV_STHEADERS;
2162 }
2163 else if (s == SV_STDATA) {
2164 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002165 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002166 tv_eternity(&rep->rex);
2167 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002168 fd_delete(t->srv_fd);
2169 if (t->srv) {
2170 t->srv->cur_sess--;
2171 t->srv->failed_resp++;
2172 }
2173 t->proxy->failed_resp++;
2174 t->srv_state = SV_STCLOSE;
2175 if (!(t->flags & SN_ERR_MASK))
2176 t->flags |= SN_ERR_SRVCL;
2177 if (!(t->flags & SN_FINST_MASK))
2178 t->flags |= SN_FINST_D;
2179 /* We used to have a free connection slot. Since we'll never use it,
2180 * we have to inform the server that it may be used by another session.
2181 */
2182 if (may_dequeue_tasks(t->srv, t->proxy))
2183 task_wakeup(&rq, t->srv->queue_mgt);
2184
2185 return 1;
2186 }
2187 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002188 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002189 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002190 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002191 shutdown(t->srv_fd, SHUT_RD);
2192 t->srv_state = SV_STSHUTR;
2193 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2194 return 1;
2195 }
2196 /* end of client read and no more data to send */
2197 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
2198 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002199 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002200 shutdown(t->srv_fd, SHUT_WR);
2201 /* We must ensure that the read part is still alive when switching
2202 * to shutw */
2203 FD_SET(t->srv_fd, StaticReadEvent);
2204 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002205 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002206
2207 t->srv_state = SV_STSHUTW;
2208 return 1;
2209 }
2210 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002211 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002212 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002213 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002214 shutdown(t->srv_fd, SHUT_RD);
2215 t->srv_state = SV_STSHUTR;
2216 if (!(t->flags & SN_ERR_MASK))
2217 t->flags |= SN_ERR_SRVTO;
2218 if (!(t->flags & SN_FINST_MASK))
2219 t->flags |= SN_FINST_D;
2220 return 1;
2221 }
2222 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002223 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002224 FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002225 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002226 shutdown(t->srv_fd, SHUT_WR);
2227 /* We must ensure that the read part is still alive when switching
2228 * to shutw */
2229 FD_SET(t->srv_fd, StaticReadEvent);
2230 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002231 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002232 t->srv_state = SV_STSHUTW;
2233 if (!(t->flags & SN_ERR_MASK))
2234 t->flags |= SN_ERR_SRVTO;
2235 if (!(t->flags & SN_FINST_MASK))
2236 t->flags |= SN_FINST_D;
2237 return 1;
2238 }
2239
2240 /* recompute request time-outs */
2241 if (req->l == 0) {
2242 if (FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2243 FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002244 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002245 }
2246 }
2247 else { /* buffer not empty, there are still data to be transferred */
2248 if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2249 FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2250 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002251 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002252 /* FIXME: to prevent the server from expiring read timeouts during writes,
2253 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002254 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002255 }
2256 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002257 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002258 }
2259 }
2260
2261 /* recompute response time-outs */
2262 if (rep->l == BUFSIZE) { /* no room to read more data */
2263 if (FD_ISSET(t->srv_fd, StaticReadEvent)) {
2264 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002265 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002266 }
2267 }
2268 else {
2269 if (! FD_ISSET(t->srv_fd, StaticReadEvent)) {
2270 FD_SET(t->srv_fd, StaticReadEvent);
2271 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002272 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002273 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002274 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002275 }
2276 }
2277
2278 return 0; /* other cases change nothing */
2279 }
2280 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002281 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002282 //FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002283 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002284 fd_delete(t->srv_fd);
2285 if (t->srv) {
2286 t->srv->cur_sess--;
2287 t->srv->failed_resp++;
2288 }
2289 t->proxy->failed_resp++;
2290 //close(t->srv_fd);
2291 t->srv_state = SV_STCLOSE;
2292 if (!(t->flags & SN_ERR_MASK))
2293 t->flags |= SN_ERR_SRVCL;
2294 if (!(t->flags & SN_FINST_MASK))
2295 t->flags |= SN_FINST_D;
2296 /* We used to have a free connection slot. Since we'll never use it,
2297 * we have to inform the server that it may be used by another session.
2298 */
2299 if (may_dequeue_tasks(t->srv, t->proxy))
2300 task_wakeup(&rq, t->srv->queue_mgt);
2301
2302 return 1;
2303 }
2304 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
2305 //FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002306 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002307 fd_delete(t->srv_fd);
2308 if (t->srv)
2309 t->srv->cur_sess--;
2310 //close(t->srv_fd);
2311 t->srv_state = SV_STCLOSE;
2312 /* We used to have a free connection slot. Since we'll never use it,
2313 * we have to inform the server that it may be used by another session.
2314 */
2315 if (may_dequeue_tasks(t->srv, t->proxy))
2316 task_wakeup(&rq, t->srv->queue_mgt);
2317
2318 return 1;
2319 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002320 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002321 //FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002322 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002323 fd_delete(t->srv_fd);
2324 if (t->srv)
2325 t->srv->cur_sess--;
2326 //close(t->srv_fd);
2327 t->srv_state = SV_STCLOSE;
2328 if (!(t->flags & SN_ERR_MASK))
2329 t->flags |= SN_ERR_SRVTO;
2330 if (!(t->flags & SN_FINST_MASK))
2331 t->flags |= SN_FINST_D;
2332 /* We used to have a free connection slot. Since we'll never use it,
2333 * we have to inform the server that it may be used by another session.
2334 */
2335 if (may_dequeue_tasks(t->srv, t->proxy))
2336 task_wakeup(&rq, t->srv->queue_mgt);
2337
2338 return 1;
2339 }
2340 else if (req->l == 0) {
2341 if (FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2342 FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002343 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002344 }
2345 }
2346 else { /* buffer not empty */
2347 if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2348 FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2349 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002350 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002351 /* FIXME: to prevent the server from expiring read timeouts during writes,
2352 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002353 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002354 }
2355 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002356 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002357 }
2358 }
2359 return 0;
2360 }
2361 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002362 if (rep->flags & BF_READ_ERROR) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002363 //FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002364 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002365 fd_delete(t->srv_fd);
2366 if (t->srv) {
2367 t->srv->cur_sess--;
2368 t->srv->failed_resp++;
2369 }
2370 t->proxy->failed_resp++;
2371 //close(t->srv_fd);
2372 t->srv_state = SV_STCLOSE;
2373 if (!(t->flags & SN_ERR_MASK))
2374 t->flags |= SN_ERR_SRVCL;
2375 if (!(t->flags & SN_FINST_MASK))
2376 t->flags |= SN_FINST_D;
2377 /* We used to have a free connection slot. Since we'll never use it,
2378 * we have to inform the server that it may be used by another session.
2379 */
2380 if (may_dequeue_tasks(t->srv, t->proxy))
2381 task_wakeup(&rq, t->srv->queue_mgt);
2382
2383 return 1;
2384 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002385 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002386 //FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002387 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002388 fd_delete(t->srv_fd);
2389 if (t->srv)
2390 t->srv->cur_sess--;
2391 //close(t->srv_fd);
2392 t->srv_state = SV_STCLOSE;
2393 /* We used to have a free connection slot. Since we'll never use it,
2394 * we have to inform the server that it may be used by another session.
2395 */
2396 if (may_dequeue_tasks(t->srv, t->proxy))
2397 task_wakeup(&rq, t->srv->queue_mgt);
2398
2399 return 1;
2400 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002401 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002402 //FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002403 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002404 fd_delete(t->srv_fd);
2405 if (t->srv)
2406 t->srv->cur_sess--;
2407 //close(t->srv_fd);
2408 t->srv_state = SV_STCLOSE;
2409 if (!(t->flags & SN_ERR_MASK))
2410 t->flags |= SN_ERR_SRVTO;
2411 if (!(t->flags & SN_FINST_MASK))
2412 t->flags |= SN_FINST_D;
2413 /* We used to have a free connection slot. Since we'll never use it,
2414 * we have to inform the server that it may be used by another session.
2415 */
2416 if (may_dequeue_tasks(t->srv, t->proxy))
2417 task_wakeup(&rq, t->srv->queue_mgt);
2418
2419 return 1;
2420 }
2421 else if (rep->l == BUFSIZE) { /* no room to read more data */
2422 if (FD_ISSET(t->srv_fd, StaticReadEvent)) {
2423 FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002424 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002425 }
2426 }
2427 else {
2428 if (! FD_ISSET(t->srv_fd, StaticReadEvent)) {
2429 FD_SET(t->srv_fd, StaticReadEvent);
2430 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002431 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002432 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002433 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002434 }
2435 }
2436 return 0;
2437 }
2438 else { /* SV_STCLOSE : nothing to do */
2439 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2440 int len;
2441 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);
2442 write(1, trash, len);
2443 }
2444 return 0;
2445 }
2446 return 0;
2447}
2448
2449
2450/*
2451 * Produces data for the session <s> depending on its source. Expects to be
2452 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
2453 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
2454 * session, which it uses to keep on being called when there is free space in
2455 * the buffer, of simply by letting an empty buffer upon return. It returns 1
2456 * if it changes the session state from CL_STSHUTR, otherwise 0.
2457 */
2458int produce_content(struct session *s)
2459{
2460 struct buffer *rep = s->rep;
2461 struct proxy *px;
2462 struct server *sv;
2463 int msglen;
2464
2465 if (s->data_source == DATA_SRC_NONE) {
2466 s->flags &= ~SN_SELF_GEN;
2467 return 1;
2468 }
2469 else if (s->data_source == DATA_SRC_STATS) {
2470 msglen = 0;
2471
2472 if (s->data_state == DATA_ST_INIT) { /* the function had not been called yet */
2473 unsigned int up;
2474
2475 s->flags |= SN_SELF_GEN; // more data will follow
2476
2477 /* send the start of the HTTP response */
2478 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2479 "HTTP/1.0 200 OK\r\n"
2480 "Cache-Control: no-cache\r\n"
2481 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +02002482 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +02002483 "\r\n\r\n");
2484
2485 s->logs.status = 200;
2486 client_retnclose(s, msglen, trash); // send the start of the response.
2487 msglen = 0;
2488
2489 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
2490 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
2491 if (!(s->flags & SN_FINST_MASK))
2492 s->flags |= SN_FINST_R;
2493
2494 /* WARNING! This must fit in the first buffer !!! */
2495 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2496 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
2497 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2498 "<style type=\"text/css\"><!--\n"
2499 "body {"
2500 " font-family: helvetica, arial;"
2501 " font-size: 12px;"
2502 " font-weight: normal;"
2503 " color: black;"
2504 " background: white;"
2505 "}\n"
2506 "td {"
2507 " font-size: 12px;"
2508 " align: center;"
2509 "}\n"
2510 "h1 {"
2511 " font-size: xx-large;"
2512 " margin-bottom: 0.5em;"
2513 "}\n"
2514 "h2 {"
2515 " font-family: helvetica, arial;"
2516 " font-size: x-large;"
2517 " font-weight: bold;"
2518 " font-style: italic;"
2519 " color: #6020a0;"
2520 " margin-top: 0em;"
2521 " margin-bottom: 0em;"
2522 "}\n"
2523 "h3 {"
2524 " font-family: helvetica, arial;"
2525 " font-size: 16px;"
2526 " font-weight: bold;"
2527 " color: #b00040;"
2528 " background: #e8e8d0;"
2529 " margin-top: 0em;"
2530 " margin-bottom: 0em;"
2531 "}\n"
2532 "li {"
2533 " margin-top: 0.25em;"
2534 " margin-right: 2em;"
2535 "}\n"
2536 ".hr {"
2537 " margin-top: 0.25em;"
2538 " border-color: black;"
2539 " border-bottom-style: solid;"
2540 "}\n"
2541 "table.tbl { border-collapse: collapse; border-width: 1px; border-style: solid; border-color: gray;}\n"
2542 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; border-color: gray; }\n"
2543 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray; }\n"
2544 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2545 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2546 "-->"
2547 "</style></head>");
2548
2549 if (buffer_write(rep, trash, msglen) != 0)
2550 return 0;
2551 msglen = 0;
2552
2553 up = (now.tv_sec - start_date.tv_sec);
2554
2555 /* WARNING! this has to fit the first packet too */
2556 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2557 "<body><h1>" PRODUCT_NAME "</h1>\n"
2558 "<h2>Statistics Report for pid %d</h2>\n"
2559 "<hr width=\"100%%\" class=\"hr\">\n"
2560 "<h3>&gt; General process information</h3>\n"
2561 "<table border=0><tr><td align=\"left\">\n"
2562 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
2563 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2564 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
2565 "<b>maxsock = </b> %d<br>\n"
2566 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
2567 "</td><td width=\"10%%\">\n"
2568 "</td><td align=\"right\">\n"
2569 "<table class=\"lgd\">"
2570 "<tr><td bgcolor=\"#C0FFC0\">&nbsp;</td><td style=\"border-style: none;\">active UP </td>"
2571 "<td bgcolor=\"#B0D0FF\">&nbsp;</td><td style=\"border-style: none;\">backup UP </td></tr>"
2572 "<tr><td bgcolor=\"#FFFFA0\"></td><td style=\"border-style: none;\">active UP, going down </td>"
2573 "<td bgcolor=\"#C060FF\"></td><td style=\"border-style: none;\">backup UP, going down </td></tr>"
2574 "<tr><td bgcolor=\"#FFD020\"></td><td style=\"border-style: none;\">active DOWN, going up </td>"
2575 "<td bgcolor=\"#FF80FF\"></td><td style=\"border-style: none;\">backup DOWN, going up </td></tr>"
2576 "<tr><td bgcolor=\"#FF9090\"></td><td style=\"border-style: none;\">active or backup DOWN &nbsp;</td>"
2577 "<td bgcolor=\"#E0E0E0\"></td><td style=\"border-style: none;\">not checked </td></tr>"
2578 "</table>\n"
2579 "</tr></table>\n"
2580 "",
2581 pid, pid, global.nbproc,
2582 up / 86400, (up % 86400) / 3600,
2583 (up % 3600) / 60, (up % 60),
2584 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2585 global.rlimit_memmax ? " MB" : "",
2586 global.rlimit_nofile,
2587 global.maxsock,
2588 global.maxconn,
2589 actconn
2590 );
2591
2592 if (buffer_write(rep, trash, msglen) != 0)
2593 return 0;
2594 msglen = 0;
2595
2596 s->data_state = DATA_ST_DATA;
2597 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
2598
2599 px = s->data_ctx.stats.px = proxy;
2600 s->data_ctx.stats.px_st = DATA_ST_INIT;
2601 }
2602
2603 while (s->data_ctx.stats.px) {
2604 int dispatch_sess, dispatch_cum;
2605 int failed_checks, down_trans;
2606 int failed_secu, failed_conns, failed_resp;
2607
2608 if (s->data_ctx.stats.px_st == DATA_ST_INIT) {
2609 /* we are on a new proxy */
2610 px = s->data_ctx.stats.px;
2611
2612 /* skip the disabled proxies */
2613 if (px->state == PR_STSTOPPED)
2614 goto next_proxy;
2615
2616 if (s->proxy->uri_auth && s->proxy->uri_auth->scope) {
2617 /* we have a limited scope, we have to check the proxy name */
2618 struct stat_scope *scope;
2619 int len;
2620
2621 len = strlen(px->id);
2622 scope = s->proxy->uri_auth->scope;
2623
2624 while (scope) {
2625 /* match exact proxy name */
2626 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2627 break;
2628
2629 /* match '.' which means 'self' proxy */
2630 if (!strcmp(scope->px_id, ".") && px == s->proxy)
2631 break;
2632 scope = scope->next;
2633 }
2634
2635 /* proxy name not found */
2636 if (scope == NULL)
2637 goto next_proxy;
2638 }
2639
2640 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2641 "<h3>&gt; Proxy instance %s : "
2642 "%d conns (maxconn=%d), %d queued (%d unassigned), %d total conns</h3>\n"
2643 "",
2644 px->id,
2645 px->nbconn, px->maxconn, px->totpend, px->nbpend, px->cum_conn);
2646
2647 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2648 "<table cols=\"16\" class=\"tbl\">\n"
2649 "<tr align=\"center\" bgcolor=\"#20C0C0\">"
2650 "<th colspan=5>Server</th>"
2651 "<th colspan=2>Queue</th>"
2652 "<th colspan=4>Sessions</th>"
2653 "<th colspan=5>Errors</th></tr>\n"
2654 "<tr align=\"center\" bgcolor=\"#20C0C0\">"
2655 "<th>Name</th><th>Weight</th><th>Status</th><th>Act.</th><th>Bck.</th>"
2656 "<th>Curr.</th><th>Max.</th>"
2657 "<th>Curr.</th><th>Max.</th><th>Limit</th><th>Cumul.</th>"
2658 "<th>Conn.</th><th>Resp.</th><th>Sec.</th><th>Check</th><th>Down</th></tr>\n");
2659
2660 if (buffer_write(rep, trash, msglen) != 0)
2661 return 0;
2662 msglen = 0;
2663
2664 s->data_ctx.stats.sv = px->srv;
2665 s->data_ctx.stats.px_st = DATA_ST_DATA;
2666 }
2667
2668 px = s->data_ctx.stats.px;
2669
2670 /* stats.sv has been initialized above */
2671 while (s->data_ctx.stats.sv != NULL) {
2672 static char *act_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FFD020", /*failing*/"#FFFFA0", /*up*/"#C0FFC0", /*unchecked*/"#E0E0E0" };
2673 static char *bck_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FF80ff", /*failing*/"#C060FF", /*up*/"#B0D0FF", /*unchecked*/"#E0E0E0" };
2674 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
2675 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP */
2676
2677 sv = s->data_ctx.stats.sv;
2678
2679 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
2680 if (!(sv->state & SRV_CHECKED))
2681 sv_state = 4;
2682 else if (sv->state & SRV_RUNNING)
2683 if (sv->health == sv->rise + sv->fall - 1)
2684 sv_state = 3; /* UP */
2685 else
2686 sv_state = 2; /* going down */
2687 else
2688 if (sv->health)
2689 sv_state = 1; /* going up */
2690 else
2691 sv_state = 0; /* DOWN */
2692
2693 /* name, weight */
2694 msglen += snprintf(trash, sizeof(trash),
2695 "<tr align=center bgcolor=\"%s\"><td>%s</td><td>%d</td><td>",
2696 (sv->state & SRV_BACKUP) ? bck_tab_bg[sv_state] : act_tab_bg[sv_state],
2697 sv->id, sv->uweight+1);
2698 /* status */
2699 msglen += snprintf(trash + msglen, sizeof(trash) - msglen, srv_hlt_st[sv_state],
2700 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
2701 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
2702
2703 /* act, bck */
2704 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2705 "</td><td>%s</td><td>%s</td>",
2706 (sv->state & SRV_BACKUP) ? "-" : "Y",
2707 (sv->state & SRV_BACKUP) ? "Y" : "-");
2708
2709 /* queue : current, max */
2710 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2711 "<td align=right>%d</td><td align=right>%d</td>",
2712 sv->nbpend, sv->nbpend_max);
2713
2714 /* sessions : current, max, limit, cumul */
2715 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2716 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>",
2717 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess);
2718
2719 /* errors : connect, response, security */
2720 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2721 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2722 sv->failed_conns, sv->failed_resp, sv->failed_secu);
2723
2724 /* check failures : unique, fatal */
2725 if (sv->state & SRV_CHECKED)
2726 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2727 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
2728 sv->failed_checks, sv->down_trans);
2729 else
2730 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2731 "<td align=right>-</td><td align=right>-</td></tr>\n");
2732
2733 if (buffer_write(rep, trash, msglen) != 0)
2734 return 0;
2735 msglen = 0;
2736
2737 s->data_ctx.stats.sv = sv->next;
2738 } /* while sv */
2739
2740 /* now we are past the last server, we'll dump information about the dispatcher */
2741
2742 /* We have to count down from the proxy to the servers to tell how
2743 * many sessions are on the dispatcher, and how many checks have
2744 * failed. We cannot count this during the servers dump because it
2745 * might be interrupted multiple times.
2746 */
2747 dispatch_sess = px->nbconn;
2748 dispatch_cum = px->cum_conn;
2749 failed_secu = px->failed_secu;
2750 failed_conns = px->failed_conns;
2751 failed_resp = px->failed_resp;
2752 failed_checks = down_trans = 0;
2753
2754 sv = px->srv;
2755 while (sv) {
2756 dispatch_sess -= sv->cur_sess;
2757 dispatch_cum -= sv->cum_sess;
2758 failed_conns -= sv->failed_conns;
2759 failed_resp -= sv->failed_resp;
2760 failed_secu -= sv->failed_secu;
2761 if (sv->state & SRV_CHECKED) {
2762 failed_checks += sv->failed_checks;
2763 down_trans += sv->down_trans;
2764 }
2765 sv = sv->next;
2766 }
2767
2768 /* name, weight, status, act, bck */
2769 msglen += snprintf(trash + msglen, sizeof(trash),
2770 "<tr align=center bgcolor=\"#e8e8d0\">"
2771 "<td>Dispatcher</td><td>-</td>"
2772 "<td>%s</td><td>-</td><td>-</td>",
2773 px->state == PR_STRUN ? "UP" : "DOWN");
2774
2775 /* queue : current, max */
2776 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2777 "<td align=right>%d</td><td align=right>%d</td>",
2778 px->nbpend, px->nbpend_max);
2779
2780 /* sessions : current, max, limit, cumul. */
2781 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2782 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>",
2783 dispatch_sess, px->nbconn_max, px->maxconn, dispatch_cum);
2784
2785 /* errors : connect, response, security */
2786 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2787 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2788 failed_conns, failed_resp, failed_secu);
2789
2790 /* check failures : unique, fatal */
2791 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2792 "<td align=right>-</td><td align=right>-</td></tr>\n");
2793
2794
2795 /* now the summary for the whole proxy */
2796 /* name, weight, status, act, bck */
2797 msglen += snprintf(trash + msglen, sizeof(trash),
2798 "<tr align=center style=\"color: #ffff80; background: #20C0C0;\">"
2799 "<td><b>Total</b></td><td>-</td>"
2800 "<td><b>%s</b></td><td><b>%d</b></td><td><b>%d</b></td>",
2801 (px->state == PR_STRUN && ((px->srv == NULL) || px->srv_act || px->srv_bck)) ? "UP" : "DOWN",
2802 px->srv_act, px->srv_bck);
2803
2804 /* queue : current, max */
2805 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2806 "<td align=right><b>%d</b></td><td align=right><b>%d</b></td>",
2807 px->totpend, px->nbpend_max);
2808
2809 /* sessions : current, max, limit, cumul */
2810 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2811 "<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>",
2812 px->nbconn, px->nbconn_max, px->maxconn, px->cum_conn);
2813
2814 /* errors : connect, response, security */
2815 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2816 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2817 px->failed_conns, px->failed_resp, px->failed_secu);
2818
2819 /* check failures : unique, fatal */
2820 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2821 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
2822 failed_checks, down_trans);
2823
2824 msglen += snprintf(trash + msglen, sizeof(trash) - msglen, "</table><p>\n");
2825
2826 if (buffer_write(rep, trash, msglen) != 0)
2827 return 0;
2828 msglen = 0;
2829
2830 s->data_ctx.stats.px_st = DATA_ST_INIT;
2831 next_proxy:
2832 s->data_ctx.stats.px = px->next;
2833 } /* proxy loop */
2834 /* here, we just have reached the sv == NULL and px == NULL */
2835 s->flags &= ~SN_SELF_GEN;
2836 return 1;
2837 }
2838 else {
2839 /* unknown data source */
2840 s->logs.status = 500;
2841 client_retnclose(s, s->proxy->errmsg.len500, s->proxy->errmsg.msg500);
2842 if (!(s->flags & SN_ERR_MASK))
2843 s->flags |= SN_ERR_PRXCOND;
2844 if (!(s->flags & SN_FINST_MASK))
2845 s->flags |= SN_FINST_R;
2846 s->flags &= SN_SELF_GEN;
2847 return 1;
2848 }
2849}
2850
2851
2852/*
2853 * Local variables:
2854 * c-indent-level: 8
2855 * c-basic-offset: 8
2856 * End:
2857 */