blob: f6a3ccd363e0d81ef9162eb547cd9341eb5b459d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/appsession.h>
26#include <common/compat.h>
27#include <common/config.h>
28#include <common/memory.h>
29#include <common/mini-clist.h>
30#include <common/standard.h>
31#include <common/time.h>
32#include <common/uri_auth.h>
33#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
35#include <types/capture.h>
36#include <types/client.h>
37#include <types/global.h>
38#include <types/httperr.h>
39#include <types/polling.h>
40#include <types/proxy.h>
41#include <types/server.h>
42
43#include <proto/backend.h>
44#include <proto/buffers.h>
45#include <proto/fd.h>
46#include <proto/log.h>
47#include <proto/proto_http.h>
48#include <proto/queue.h>
49#include <proto/session.h>
50#include <proto/task.h>
51
52
Willy Tarreau1c47f852006-07-09 08:22:27 +020053/* This is used by remote monitoring */
54const char *HTTP_200 =
55 "HTTP/1.0 200 OK\r\n"
56 "Cache-Control: no-cache\r\n"
57 "Connection: close\r\n"
58 "Content-Type: text/html\r\n"
59 "\r\n"
60 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
61
Willy Tarreaubaaee002006-06-26 02:48:02 +020062/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
63const char *HTTP_401_fmt =
64 "HTTP/1.0 401 Unauthorized\r\n"
65 "Cache-Control: no-cache\r\n"
66 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020067 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020068 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
69 "\r\n"
70 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
71
72
73#ifdef DEBUG_FULL
74static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
75static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
76#endif
77
78
79/*
80 * returns a message to the client ; the connection is shut down for read,
81 * and the request is cleared so that no server connection can be initiated.
82 * The client must be in a valid state for this (HEADER, DATA ...).
83 * Nothing is performed on the server side.
84 * The reply buffer doesn't need to be empty before this.
85 */
86void client_retnclose(struct session *s, int len, const char *msg)
87{
Willy Tarreau2a429502006-10-15 14:52:29 +020088 MY_FD_CLR(s->cli_fd, StaticReadEvent);
89 MY_FD_SET(s->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +020090 tv_eternity(&s->req->rex);
Willy 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,
Willy Tarreaub17916e2006-10-15 15:17:57 +0200120 int status, int msglen, const char *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121{
122 t->srv_state = SV_STCLOSE;
123 if (status > 0) {
124 t->logs.status = status;
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],
Willy Tarreau2a429502006-10-15 14:52:29 +0200228 MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
Willy Tarreaud7971282006-07-29 18:36:34 +0200229 req->rex.tv_sec, req->rex.tv_usec,
230 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231#endif
232 //fprintf(stderr,"process_cli: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s,
Willy Tarreau2a429502006-10-15 14:52:29 +0200233 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
234 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235 //);
236 if (c == CL_STHEADERS) {
237 /* now parse the partial (or complete) headers */
238 while (req->lr < req->r) { /* this loop only sees one header at each iteration */
239 char *ptr;
240 int delete_header;
241 char *request_line = NULL;
242
243 ptr = req->lr;
244
245 /* look for the end of the current header */
246 while (ptr < req->r && *ptr != '\n' && *ptr != '\r')
247 ptr++;
248
249 if (ptr == req->h) { /* empty line, end of headers */
250 int line, len;
251
252 /*
253 * first, let's check that it's not a leading empty line, in
254 * which case we'll ignore and remove it (according to RFC2616).
255 */
256 if (req->h == req->data) {
257 /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */
258 if (ptr > req->r - 2) {
259 /* this is a partial header, let's wait for more to come */
260 req->lr = ptr;
261 break;
262 }
263
264 /* now we know that *ptr is either \r or \n,
265 * and that there are at least 1 char after it.
266 */
267 if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n'))
268 req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */
269 else
270 req->lr = ptr + 2; /* \r\n or \n\r */
271 /* ignore empty leading lines */
272 buffer_replace2(req, req->h, req->lr, NULL, 0);
273 req->h = req->lr;
274 continue;
275 }
276
277 /* we can only get here after an end of headers */
278 /* we'll have something else to do here : add new headers ... */
279
280 if (t->flags & SN_CLDENY) {
281 /* no need to go further */
282 t->logs.status = 403;
283 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); /* let's log the request time */
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 */
Willy Tarreau2a429502006-10-15 14:52:29 +0200431 //MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +0200432 //tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433
434 /* FIXME: if we break here (as up to 1.1.23), having the client
435 * shutdown its connection can lead to an abort further.
436 * it's better to either return 1 or even jump directly to the
437 * data state which will save one schedule.
438 */
439 //break;
440
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
Willy Tarreau2a429502006-10-15 14:52:29 +0200986 if ((req->l < req->rlim - req->data) && ! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200987 /* 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 */
Willy Tarreau2a429502006-10-15 14:52:29 +0200991 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200992 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 Tarreau2a429502006-10-15 14:52:29 +02001063 MY_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)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001071 MY_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 */
Willy Tarreau2a429502006-10-15 14:52:29 +02001076 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077 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 Tarreau2a429502006-10-15 14:52:29 +02001085 MY_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 Tarreau2a429502006-10-15 14:52:29 +02001103 MY_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 */
Willy Tarreau2a429502006-10-15 14:52:29 +02001108 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001109 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 */
Willy Tarreau2a429502006-10-15 14:52:29 +02001128 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001129 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02001130 MY_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 */
Willy Tarreau2a429502006-10-15 14:52:29 +02001135 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
1136 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001137 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)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001152 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1153 MY_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 */
Willy Tarreau2a429502006-10-15 14:52:29 +02001158 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1159 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001160 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)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001225 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1226 MY_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 */
Willy Tarreau2a429502006-10-15 14:52:29 +02001231 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1232 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233 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
Willy Tarreau2a429502006-10-15 14:52:29 +02001291 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001292 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02001293 MY_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 */
Willy Tarreau2a429502006-10-15 14:52:29 +02001299 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
1300 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001301 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,
Willy Tarreau2a429502006-10-15 14:52:29 +02001340 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
1341 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001342 //);
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
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02001457 if (t->srv && t->conn_retries == 0 && t->proxy->options & PR_O_REDISP) {
1458 /* We're on our last chance, and the REDISP option was specified.
1459 * We will ignore cookie and force to balance or use the dispatcher.
1460 */
1461 /* let's try to offer this slot to anybody */
1462 if (may_dequeue_tasks(t->srv, t->proxy))
1463 task_wakeup(&rq, t->srv->queue_mgt);
1464
1465 if (t->srv)
1466 t->srv->failed_conns++;
1467 t->proxy->failed_conns++;
1468
1469 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
1470 t->srv = NULL; /* it's left to the dispatcher to choose a server */
1471 if ((t->flags & SN_CK_MASK) == SN_CK_VALID) {
1472 t->flags &= ~SN_CK_MASK;
1473 t->flags |= SN_CK_DOWN;
1474 }
1475
1476 /* first, get a connection */
1477 if (srv_redispatch_connect(t))
1478 return t->srv_state != SV_STIDLE;
1479 }
1480
Willy Tarreaubaaee002006-06-26 02:48:02 +02001481 do {
1482 /* Now we will try to either reconnect to the same server or
1483 * connect to another server. If the connection gets queued
1484 * because all servers are saturated, then we will go back to
1485 * the SV_STIDLE state.
1486 */
1487 if (srv_retryable_connect(t)) {
1488 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
1489 return t->srv_state != SV_STCONN;
1490 }
1491
1492 /* we need to redispatch the connection to another server */
1493 if (srv_redispatch_connect(t))
1494 return t->srv_state != SV_STCONN;
1495 } while (1);
1496 }
1497 else { /* no error or write 0 */
1498 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
1499
1500 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
1501 if (req->l == 0) /* nothing to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02001502 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001503 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001504 } else /* need the right to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02001505 MY_FD_SET(t->srv_fd, StaticWriteEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001506 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001507 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001508 /* FIXME: to prevent the server from expiring read timeouts during writes,
1509 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001510 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001511 }
1512 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001513 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001514 }
1515
1516 if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau2a429502006-10-15 14:52:29 +02001517 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001518 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001519 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001520 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001521 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001522
1523 t->srv_state = SV_STDATA;
1524 if (t->srv)
1525 t->srv->cum_sess++;
1526 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
1527
1528 /* if the user wants to log as soon as possible, without counting
1529 bytes from the server, then this is the right moment. */
1530 if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) {
1531 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
1532 sess_log(t);
1533 }
1534 }
1535 else {
1536 t->srv_state = SV_STHEADERS;
1537 if (t->srv)
1538 t->srv->cum_sess++;
1539 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
1540 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001541 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001542 return 1;
1543 }
1544 }
1545 else if (s == SV_STHEADERS) { /* receiving server headers */
1546 /* now parse the partial (or complete) headers */
1547 while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */
1548 char *ptr;
1549 int delete_header;
1550
1551 ptr = rep->lr;
1552
1553 /* look for the end of the current header */
1554 while (ptr < rep->r && *ptr != '\n' && *ptr != '\r')
1555 ptr++;
1556
1557 if (ptr == rep->h) {
1558 int line, len;
1559
1560 /* we can only get here after an end of headers */
1561
1562 /* first, we'll block if security checks have caught nasty things */
1563 if (t->flags & SN_CACHEABLE) {
1564 if ((t->flags & SN_CACHE_COOK) &&
1565 (t->flags & SN_SCK_ANY) &&
1566 (t->proxy->options & PR_O_CHK_CACHE)) {
1567
1568 /* we're in presence of a cacheable response containing
1569 * a set-cookie header. We'll block it as requested by
1570 * the 'checkcache' option, and send an alert.
1571 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001572 tv_eternity(&rep->rex);
1573 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001574 fd_delete(t->srv_fd);
1575 if (t->srv) {
1576 t->srv->cur_sess--;
1577 t->srv->failed_secu++;
1578 }
1579 t->proxy->failed_secu++;
1580 t->srv_state = SV_STCLOSE;
1581 t->logs.status = 502;
1582 client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502);
1583 if (!(t->flags & SN_ERR_MASK))
1584 t->flags |= SN_ERR_PRXCOND;
1585 if (!(t->flags & SN_FINST_MASK))
1586 t->flags |= SN_FINST_H;
1587
1588 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id);
1589 send_log(t->proxy, LOG_ALERT, "Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id);
1590
1591 /* We used to have a free connection slot. Since we'll never use it,
1592 * we have to inform the server that it may be used by another session.
1593 */
1594 if (may_dequeue_tasks(t->srv, t->proxy))
1595 task_wakeup(&rq, t->srv->queue_mgt);
1596
1597 return 1;
1598 }
1599 }
1600
1601 /* next, we'll block if an 'rspideny' or 'rspdeny' filter matched */
1602 if (t->flags & SN_SVDENY) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001603 tv_eternity(&rep->rex);
1604 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001605 fd_delete(t->srv_fd);
1606 if (t->srv) {
1607 t->srv->cur_sess--;
1608 t->srv->failed_secu++;
1609 }
1610 t->proxy->failed_secu++;
1611 t->srv_state = SV_STCLOSE;
1612 t->logs.status = 502;
1613 client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502);
1614 if (!(t->flags & SN_ERR_MASK))
1615 t->flags |= SN_ERR_PRXCOND;
1616 if (!(t->flags & SN_FINST_MASK))
1617 t->flags |= SN_FINST_H;
1618 /* We used to have a free connection slot. Since we'll never use it,
1619 * we have to inform the server that it may be used by another session.
1620 */
1621 if (may_dequeue_tasks(t->srv, t->proxy))
1622 task_wakeup(&rq, t->srv->queue_mgt);
1623
1624 return 1;
1625 }
1626
1627 /* we'll have something else to do here : add new headers ... */
1628
1629 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS) &&
1630 (!(t->proxy->options & PR_O_COOK_POST) || (t->flags & SN_POST))) {
1631 /* the server is known, it's not the one the client requested, we have to
1632 * insert a set-cookie here, except if we want to insert only on POST
1633 * requests and this one isn't. Note that servers which don't have cookies
1634 * (eg: some backup servers) will return a full cookie removal request.
1635 */
1636 len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n",
1637 t->proxy->cookie_name,
1638 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
1639
1640 t->flags |= SN_SCK_INSERTED;
1641
1642 /* Here, we will tell an eventual cache on the client side that we don't
1643 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1644 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1645 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1646 */
1647 if (t->proxy->options & PR_O_COOK_NOC)
1648 //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n");
1649 len += sprintf(trash + len, "Cache-control: private\r\n");
1650
1651 if (rep->data + rep->l < rep->h)
1652 /* The data has been stolen, we will crash cleanly instead of corrupting memory */
1653 *(int *)0 = 0;
1654 buffer_replace2(rep, rep->h, rep->h, trash, len);
1655 }
1656
1657 /* headers to be added */
1658 for (line = 0; line < t->proxy->nb_rspadd; line++) {
1659 len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]);
1660 buffer_replace2(rep, rep->h, rep->h, trash, len);
1661 }
1662
1663 /* add a "connection: close" line if needed */
1664 if (t->proxy->options & PR_O_HTTP_CLOSE)
1665 buffer_replace2(rep, rep->h, rep->h, "Connection: close\r\n", 19);
1666
1667 t->srv_state = SV_STDATA;
1668 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
1669 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
1670
1671 /* client connection already closed or option 'httpclose' required :
1672 * we close the server's outgoing connection right now.
1673 */
1674 if ((req->l == 0) &&
1675 (c == CL_STSHUTR || c == CL_STCLOSE || t->proxy->options & PR_O_FORCE_CLO)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001676 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001677 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001678
1679 /* We must ensure that the read part is still alive when switching
1680 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001681 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001682 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02001683 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001684
1685 shutdown(t->srv_fd, SHUT_WR);
1686 t->srv_state = SV_STSHUTW;
1687 }
1688
1689 /* if the user wants to log as soon as possible, without counting
1690 bytes from the server, then this is the right moment. */
1691 if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) {
1692 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
1693 t->logs.bytes = rep->h - rep->data;
1694 sess_log(t);
1695 }
1696 break;
1697 }
1698
1699 /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */
1700 if (ptr > rep->r - 2) {
1701 /* this is a partial header, let's wait for more to come */
1702 rep->lr = ptr;
1703 break;
1704 }
1705
1706 // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r);
1707 // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n");
1708
1709 /* now we know that *ptr is either \r or \n,
1710 * and that there are at least 1 char after it.
1711 */
1712 if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n'))
1713 rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */
1714 else
1715 rep->lr = ptr + 2; /* \r\n or \n\r */
1716
1717 /*
1718 * now we know that we have a full header ; we can do whatever
1719 * we want with these pointers :
1720 * rep->h = beginning of header
1721 * ptr = end of header (first \r or \n)
1722 * rep->lr = beginning of next line (next rep->h)
1723 * rep->r = end of data (not used at this stage)
1724 */
1725
1726
1727 if (t->logs.status == -1) {
1728 t->logs.logwait &= ~LW_RESP;
1729 t->logs.status = atoi(rep->h + 9);
1730 switch (t->logs.status) {
1731 case 200:
1732 case 203:
1733 case 206:
1734 case 300:
1735 case 301:
1736 case 410:
1737 /* RFC2616 @13.4:
1738 * "A response received with a status code of
1739 * 200, 203, 206, 300, 301 or 410 MAY be stored
1740 * by a cache (...) unless a cache-control
1741 * directive prohibits caching."
1742 *
1743 * RFC2616 @9.5: POST method :
1744 * "Responses to this method are not cacheable,
1745 * unless the response includes appropriate
1746 * Cache-Control or Expires header fields."
1747 */
1748 if (!(t->flags & SN_POST) && (t->proxy->options & PR_O_CHK_CACHE))
1749 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
1750 break;
1751 default:
1752 break;
1753 }
1754 }
1755 else if (t->logs.logwait & LW_RSPHDR) {
1756 struct cap_hdr *h;
1757 int len;
1758 for (h = t->proxy->rsp_cap; h; h = h->next) {
1759 if ((h->namelen + 2 <= ptr - rep->h) &&
1760 (rep->h[h->namelen] == ':') &&
1761 (strncasecmp(rep->h, h->name, h->namelen) == 0)) {
1762
1763 if (t->rsp_cap[h->index] == NULL)
1764 t->rsp_cap[h->index] = pool_alloc_from(h->pool, h->len + 1);
1765
1766 len = ptr - (rep->h + h->namelen + 2);
1767 if (len > h->len)
1768 len = h->len;
1769
1770 memcpy(t->rsp_cap[h->index], rep->h + h->namelen + 2, len);
1771 t->rsp_cap[h->index][len]=0;
1772 }
1773 }
1774
1775 }
1776
1777 delete_header = 0;
1778
1779 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
1780 int len, max;
1781 len = sprintf(trash, "%08x:%s.srvhdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
1782 max = ptr - rep->h;
1783 UBOUND(max, sizeof(trash) - len - 1);
1784 len += strlcpy2(trash + len, rep->h, max + 1);
1785 trash[len++] = '\n';
1786 write(1, trash, len);
1787 }
1788
1789 /* remove "connection: " if needed */
1790 if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE)
1791 && (strncasecmp(rep->h, "Connection: ", 12) == 0)) {
1792 delete_header = 1;
1793 }
1794
1795 /* try headers regexps */
1796 if (!delete_header && t->proxy->rsp_exp != NULL
1797 && !(t->flags & SN_SVDENY)) {
1798 struct hdr_exp *exp;
1799 char term;
1800
1801 term = *ptr;
1802 *ptr = '\0';
1803 exp = t->proxy->rsp_exp;
1804 do {
1805 if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) {
1806 switch (exp->action) {
1807 case ACT_ALLOW:
1808 if (!(t->flags & SN_SVDENY))
1809 t->flags |= SN_SVALLOW;
1810 break;
1811 case ACT_REPLACE:
1812 if (!(t->flags & SN_SVDENY)) {
1813 int len = exp_replace(trash, rep->h, exp->replace, pmatch);
1814 ptr += buffer_replace2(rep, rep->h, ptr, trash, len);
1815 }
1816 break;
1817 case ACT_REMOVE:
1818 if (!(t->flags & SN_SVDENY))
1819 delete_header = 1;
1820 break;
1821 case ACT_DENY:
1822 if (!(t->flags & SN_SVALLOW))
1823 t->flags |= SN_SVDENY;
1824 break;
1825 case ACT_PASS: /* we simply don't deny this one */
1826 break;
1827 }
1828 break;
1829 }
1830 } while ((exp = exp->next) != NULL);
1831 *ptr = term; /* restore the string terminator */
1832 }
1833
1834 /* check for cache-control: or pragma: headers */
1835 if (!delete_header && (t->flags & SN_CACHEABLE)) {
1836 if (strncasecmp(rep->h, "Pragma: no-cache", 16) == 0)
1837 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1838 else if (strncasecmp(rep->h, "Cache-control: ", 15) == 0) {
1839 if (strncasecmp(rep->h + 15, "no-cache", 8) == 0) {
1840 if (rep->h + 23 == ptr || rep->h[23] == ',')
1841 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1842 else {
1843 if (strncasecmp(rep->h + 23, "=\"set-cookie", 12) == 0
1844 && (rep->h[35] == '"' || rep->h[35] == ','))
1845 t->flags &= ~SN_CACHE_COOK;
1846 }
1847 } else if ((strncasecmp(rep->h + 15, "private", 7) == 0 &&
1848 (rep->h + 22 == ptr || rep->h[22] == ','))
1849 || (strncasecmp(rep->h + 15, "no-store", 8) == 0 &&
1850 (rep->h + 23 == ptr || rep->h[23] == ','))) {
1851 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1852 } else if (strncasecmp(rep->h + 15, "max-age=0", 9) == 0 &&
1853 (rep->h + 24 == ptr || rep->h[24] == ',')) {
1854 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1855 } else if (strncasecmp(rep->h + 15, "s-maxage=0", 10) == 0 &&
1856 (rep->h + 25 == ptr || rep->h[25] == ',')) {
1857 t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK;
1858 } else if (strncasecmp(rep->h + 15, "public", 6) == 0 &&
1859 (rep->h + 21 == ptr || rep->h[21] == ',')) {
1860 t->flags |= SN_CACHEABLE | SN_CACHE_COOK;
1861 }
1862 }
1863 }
1864
1865 /* check for server cookies */
1866 if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/
1867 && (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL || t->proxy->appsession_name !=NULL)
1868 && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) {
1869 char *p1, *p2, *p3, *p4;
1870
1871 t->flags |= SN_SCK_ANY;
1872
1873 p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */
1874
1875 while (p1 < ptr) { /* in fact, we'll break after the first cookie */
1876 while (p1 < ptr && (isspace((int)*p1)))
1877 p1++;
1878
1879 if (p1 == ptr || *p1 == ';') /* end of cookie */
1880 break;
1881
1882 /* p1 is at the beginning of the cookie name */
1883 p2 = p1;
1884
1885 while (p2 < ptr && *p2 != '=' && *p2 != ';')
1886 p2++;
1887
1888 if (p2 == ptr || *p2 == ';') /* next cookie */
1889 break;
1890
1891 p3 = p2 + 1; /* skips the '=' sign */
1892 if (p3 == ptr)
1893 break;
1894
1895 p4 = p3;
1896 while (p4 < ptr && !isspace((int)*p4) && *p4 != ';')
1897 p4++;
1898
1899 /* here, we have the cookie name between p1 and p2,
1900 * and its value between p3 and p4.
1901 * we can process it.
1902 */
1903
1904 /* first, let's see if we want to capture it */
1905 if (t->proxy->capture_name != NULL &&
1906 t->logs.srv_cookie == NULL &&
1907 (p4 - p1 >= t->proxy->capture_namelen) &&
1908 memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) {
1909 int log_len = p4 - p1;
1910
1911 if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) {
1912 Alert("HTTP logging : out of memory.\n");
1913 }
1914
1915 if (log_len > t->proxy->capture_len)
1916 log_len = t->proxy->capture_len;
1917 memcpy(t->logs.srv_cookie, p1, log_len);
1918 t->logs.srv_cookie[log_len] = 0;
1919 }
1920
1921 if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) &&
1922 (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) {
1923 /* Cool... it's the right one */
1924 t->flags |= SN_SCK_SEEN;
1925
1926 /* If the cookie is in insert mode on a known server, we'll delete
1927 * this occurrence because we'll insert another one later.
1928 * We'll delete it too if the "indirect" option is set and we're in
1929 * a direct access. */
1930 if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) ||
1931 ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) {
1932 /* this header must be deleted */
1933 delete_header = 1;
1934 t->flags |= SN_SCK_DELETED;
1935 }
1936 else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) {
1937 /* replace bytes p3->p4 with the cookie name associated
1938 * with this server since we know it.
1939 */
1940 buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen);
1941 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
1942 }
1943 else if ((t->srv) && (t->proxy->options & PR_O_COOK_PFX)) {
1944 /* insert the cookie name associated with this server
1945 * before existing cookie, and insert a delimitor between them..
1946 */
1947 buffer_replace2(rep, p3, p3, t->srv->cookie, t->srv->cklen + 1);
1948 p3[t->srv->cklen] = COOKIE_DELIM;
1949 t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED;
1950 }
1951 break;
1952 }
1953
1954 /* first, let's see if the cookie is our appcookie*/
1955 if ((t->proxy->appsession_name != NULL) &&
1956 (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) {
1957
1958 /* Cool... it's the right one */
1959
1960 size_t server_id_len = strlen(t->srv->id) + 1;
1961 asession_temp = &local_asession;
1962
1963 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
1964 Alert("Not enought Memory process_srv():asession->sessid:malloc().\n");
1965 send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n");
1966 }
1967 memcpy(asession_temp->sessid, p3, t->proxy->appsession_len);
1968 asession_temp->sessid[t->proxy->appsession_len] = 0;
1969 asession_temp->serverid = NULL;
1970
1971 /* only do insert, if lookup fails */
1972 if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) {
1973 if ((asession_temp = pool_alloc(appsess)) == NULL) {
1974 Alert("Not enought Memory process_srv():asession:calloc().\n");
1975 send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession:calloc().\n");
1976 return 0;
1977 }
1978 asession_temp->sessid = local_asession.sessid;
1979 asession_temp->serverid = local_asession.serverid;
1980 chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp);
1981 }/* end if (chtbl_lookup()) */
1982 else {
1983 /* free wasted memory */
1984 pool_free_to(apools.sessid, local_asession.sessid);
1985 } /* end else from if (chtbl_lookup()) */
1986
1987 if (asession_temp->serverid == NULL) {
1988 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
1989 Alert("Not enought Memory process_srv():asession->sessid:malloc().\n");
1990 send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n");
1991 }
1992 asession_temp->serverid[0] = '\0';
1993 }
1994
1995 if (asession_temp->serverid[0] == '\0')
1996 memcpy(asession_temp->serverid,t->srv->id,server_id_len);
1997
1998 tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout);
1999
2000#if defined(DEBUG_HASH)
2001 print_table(&(t->proxy->htbl_proxy));
2002#endif
2003 break;
2004 }/* end if ((t->proxy->appsession_name != NULL) ... */
2005 else {
2006 // fprintf(stderr,"Ignoring unknown cookie : ");
2007 // write(2, p1, p2-p1);
2008 // fprintf(stderr," = ");
2009 // write(2, p3, p4-p3);
2010 // fprintf(stderr,"\n");
2011 }
2012 break; /* we don't want to loop again since there cannot be another cookie on the same line */
2013 } /* we're now at the end of the cookie value */
2014 } /* end of cookie processing */
2015
2016 /* check for any set-cookie in case we check for cacheability */
2017 if (!delete_header && !(t->flags & SN_SCK_ANY) &&
2018 (t->proxy->options & PR_O_CHK_CACHE) &&
2019 (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) {
2020 t->flags |= SN_SCK_ANY;
2021 }
2022
2023 /* let's look if we have to delete this header */
2024 if (delete_header && !(t->flags & SN_SVDENY))
2025 buffer_replace2(rep, rep->h, rep->lr, "", 0);
2026
2027 rep->h = rep->lr;
2028 } /* while (rep->lr < rep->r) */
2029
2030 /* end of header processing (even if incomplete) */
2031
Willy Tarreau2a429502006-10-15 14:52:29 +02002032 if ((rep->l < rep->rlim - rep->data) && ! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002033 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
Willy Tarreaud7971282006-07-29 18:36:34 +02002034 * full. We cannot loop here since stream_sock_read will disable it only if
Willy Tarreaubaaee002006-06-26 02:48:02 +02002035 * rep->l == rlim-data
2036 */
Willy Tarreau2a429502006-10-15 14:52:29 +02002037 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002038 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002039 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002040 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002041 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002042 }
2043
2044 /* read error, write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002045 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002046 tv_eternity(&rep->rex);
2047 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002048 fd_delete(t->srv_fd);
2049 if (t->srv) {
2050 t->srv->cur_sess--;
2051 t->srv->failed_resp++;
2052 }
2053 t->proxy->failed_resp++;
2054
2055 t->srv_state = SV_STCLOSE;
2056 t->logs.status = 502;
2057 client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502);
2058 if (!(t->flags & SN_ERR_MASK))
2059 t->flags |= SN_ERR_SRVCL;
2060 if (!(t->flags & SN_FINST_MASK))
2061 t->flags |= SN_FINST_H;
2062 /* We used to have a free connection slot. Since we'll never use it,
2063 * we have to inform the server that it may be used by another session.
2064 */
2065 if (may_dequeue_tasks(t->srv, t->proxy))
2066 task_wakeup(&rq, t->srv->queue_mgt);
2067
2068 return 1;
2069 }
2070 /* end of client write or end of server read.
2071 * since we are in header mode, if there's no space left for headers, we
2072 * won't be able to free more later, so the session will never terminate.
2073 */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002074 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE || rep->l >= rep->rlim - rep->data) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002075 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002076 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002077 shutdown(t->srv_fd, SHUT_RD);
2078 t->srv_state = SV_STSHUTR;
2079 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2080 return 1;
2081 }
2082 /* read timeout : return a 504 to the client.
2083 */
Willy Tarreau2a429502006-10-15 14:52:29 +02002084 else if (MY_FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002085 tv_eternity(&rep->rex);
2086 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002087 fd_delete(t->srv_fd);
2088 if (t->srv) {
2089 t->srv->cur_sess--;
2090 t->srv->failed_resp++;
2091 }
2092 t->proxy->failed_resp++;
2093 t->srv_state = SV_STCLOSE;
2094 t->logs.status = 504;
2095 client_return(t, t->proxy->errmsg.len504, t->proxy->errmsg.msg504);
2096 if (!(t->flags & SN_ERR_MASK))
2097 t->flags |= SN_ERR_SRVTO;
2098 if (!(t->flags & SN_FINST_MASK))
2099 t->flags |= SN_FINST_H;
2100 /* We used to have a free connection slot. Since we'll never use it,
2101 * we have to inform the server that it may be used by another session.
2102 */
2103 if (may_dequeue_tasks(t->srv, t->proxy))
2104 task_wakeup(&rq, t->srv->queue_mgt);
2105
2106 return 1;
2107 }
2108 /* last client read and buffer empty */
2109 /* FIXME!!! here, we don't want to switch to SHUTW if the
2110 * client shuts read too early, because we may still have
2111 * some work to do on the headers.
2112 * The side-effect is that if the client completely closes its
2113 * connection during SV_STHEADER, the connection to the server
2114 * is kept until a response comes back or the timeout is reached.
2115 */
2116 else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002117 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002118 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002119
2120 /* We must ensure that the read part is still alive when switching
2121 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002122 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002123 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002124 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002125
2126 shutdown(t->srv_fd, SHUT_WR);
2127 t->srv_state = SV_STSHUTW;
2128 return 1;
2129 }
2130 /* write timeout */
2131 /* FIXME!!! here, we don't want to switch to SHUTW if the
2132 * client shuts read too early, because we may still have
2133 * some work to do on the headers.
2134 */
Willy Tarreau2a429502006-10-15 14:52:29 +02002135 else if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&req->wex, &now) <= 0) {
2136 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002137 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002138 shutdown(t->srv_fd, SHUT_WR);
2139 /* We must ensure that the read part is still alive when switching
2140 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002141 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002142 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002143 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002144
2145 /* We must ensure that the read part is still alive when switching
2146 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002147 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002148 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002149 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002150
2151 t->srv_state = SV_STSHUTW;
2152 if (!(t->flags & SN_ERR_MASK))
2153 t->flags |= SN_ERR_SRVTO;
2154 if (!(t->flags & SN_FINST_MASK))
2155 t->flags |= SN_FINST_H;
2156 return 1;
2157 }
2158
2159 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002160 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2161 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002162 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002163 }
2164 }
2165 else { /* client buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002166 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2167 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002168 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002169 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002170 /* FIXME: to prevent the server from expiring read timeouts during writes,
2171 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002172 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002173 }
2174 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002175 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002176 }
2177 }
2178
2179 /* be nice with the client side which would like to send a complete header
2180 * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client
2181 * would read all remaining data at once ! The client should not write past rep->lr
2182 * when the server is in header state.
2183 */
2184 //return header_processed;
2185 return t->srv_state != SV_STHEADERS;
2186 }
2187 else if (s == SV_STDATA) {
2188 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002189 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002190 tv_eternity(&rep->rex);
2191 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002192 fd_delete(t->srv_fd);
2193 if (t->srv) {
2194 t->srv->cur_sess--;
2195 t->srv->failed_resp++;
2196 }
2197 t->proxy->failed_resp++;
2198 t->srv_state = SV_STCLOSE;
2199 if (!(t->flags & SN_ERR_MASK))
2200 t->flags |= SN_ERR_SRVCL;
2201 if (!(t->flags & SN_FINST_MASK))
2202 t->flags |= SN_FINST_D;
2203 /* We used to have a free connection slot. Since we'll never use it,
2204 * we have to inform the server that it may be used by another session.
2205 */
2206 if (may_dequeue_tasks(t->srv, t->proxy))
2207 task_wakeup(&rq, t->srv->queue_mgt);
2208
2209 return 1;
2210 }
2211 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002212 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002213 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002214 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002215 shutdown(t->srv_fd, SHUT_RD);
2216 t->srv_state = SV_STSHUTR;
2217 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2218 return 1;
2219 }
2220 /* end of client read and no more data to send */
2221 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002222 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002223 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002224 shutdown(t->srv_fd, SHUT_WR);
2225 /* We must ensure that the read part is still alive when switching
2226 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002227 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002229 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002230
2231 t->srv_state = SV_STSHUTW;
2232 return 1;
2233 }
2234 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002235 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002236 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002237 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002238 shutdown(t->srv_fd, SHUT_RD);
2239 t->srv_state = SV_STSHUTR;
2240 if (!(t->flags & SN_ERR_MASK))
2241 t->flags |= SN_ERR_SRVTO;
2242 if (!(t->flags & SN_FINST_MASK))
2243 t->flags |= SN_FINST_D;
2244 return 1;
2245 }
2246 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002247 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002248 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002249 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002250 shutdown(t->srv_fd, SHUT_WR);
2251 /* We must ensure that the read part is still alive when switching
2252 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002253 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002254 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002255 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002256 t->srv_state = SV_STSHUTW;
2257 if (!(t->flags & SN_ERR_MASK))
2258 t->flags |= SN_ERR_SRVTO;
2259 if (!(t->flags & SN_FINST_MASK))
2260 t->flags |= SN_FINST_D;
2261 return 1;
2262 }
2263
2264 /* recompute request time-outs */
2265 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002266 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2267 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002268 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002269 }
2270 }
2271 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau2a429502006-10-15 14:52:29 +02002272 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2273 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002274 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002275 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002276 /* FIXME: to prevent the server from expiring read timeouts during writes,
2277 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002278 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002279 }
2280 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002281 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002282 }
2283 }
2284
2285 /* recompute response time-outs */
2286 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002287 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2288 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002289 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002290 }
2291 }
2292 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02002293 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2294 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002295 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002296 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002297 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002298 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002299 }
2300 }
2301
2302 return 0; /* other cases change nothing */
2303 }
2304 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002305 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002306 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002307 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002308 fd_delete(t->srv_fd);
2309 if (t->srv) {
2310 t->srv->cur_sess--;
2311 t->srv->failed_resp++;
2312 }
2313 t->proxy->failed_resp++;
2314 //close(t->srv_fd);
2315 t->srv_state = SV_STCLOSE;
2316 if (!(t->flags & SN_ERR_MASK))
2317 t->flags |= SN_ERR_SRVCL;
2318 if (!(t->flags & SN_FINST_MASK))
2319 t->flags |= SN_FINST_D;
2320 /* We used to have a free connection slot. Since we'll never use it,
2321 * we have to inform the server that it may be used by another session.
2322 */
2323 if (may_dequeue_tasks(t->srv, t->proxy))
2324 task_wakeup(&rq, t->srv->queue_mgt);
2325
2326 return 1;
2327 }
2328 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002329 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002330 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002331 fd_delete(t->srv_fd);
2332 if (t->srv)
2333 t->srv->cur_sess--;
2334 //close(t->srv_fd);
2335 t->srv_state = SV_STCLOSE;
2336 /* We used to have a free connection slot. Since we'll never use it,
2337 * we have to inform the server that it may be used by another session.
2338 */
2339 if (may_dequeue_tasks(t->srv, t->proxy))
2340 task_wakeup(&rq, t->srv->queue_mgt);
2341
2342 return 1;
2343 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002344 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002345 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002346 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002347 fd_delete(t->srv_fd);
2348 if (t->srv)
2349 t->srv->cur_sess--;
2350 //close(t->srv_fd);
2351 t->srv_state = SV_STCLOSE;
2352 if (!(t->flags & SN_ERR_MASK))
2353 t->flags |= SN_ERR_SRVTO;
2354 if (!(t->flags & SN_FINST_MASK))
2355 t->flags |= SN_FINST_D;
2356 /* We used to have a free connection slot. Since we'll never use it,
2357 * we have to inform the server that it may be used by another session.
2358 */
2359 if (may_dequeue_tasks(t->srv, t->proxy))
2360 task_wakeup(&rq, t->srv->queue_mgt);
2361
2362 return 1;
2363 }
2364 else if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002365 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2366 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002367 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002368 }
2369 }
2370 else { /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002371 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2372 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002373 if (t->proxy->srvtimeout) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002374 tv_delayfrom(&req->wex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002375 /* FIXME: to prevent the server from expiring read timeouts during writes,
2376 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002377 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002378 }
2379 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002380 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002381 }
2382 }
2383 return 0;
2384 }
2385 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002386 if (rep->flags & BF_READ_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002387 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002388 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002389 fd_delete(t->srv_fd);
2390 if (t->srv) {
2391 t->srv->cur_sess--;
2392 t->srv->failed_resp++;
2393 }
2394 t->proxy->failed_resp++;
2395 //close(t->srv_fd);
2396 t->srv_state = SV_STCLOSE;
2397 if (!(t->flags & SN_ERR_MASK))
2398 t->flags |= SN_ERR_SRVCL;
2399 if (!(t->flags & SN_FINST_MASK))
2400 t->flags |= SN_FINST_D;
2401 /* We used to have a free connection slot. Since we'll never use it,
2402 * we have to inform the server that it may be used by another session.
2403 */
2404 if (may_dequeue_tasks(t->srv, t->proxy))
2405 task_wakeup(&rq, t->srv->queue_mgt);
2406
2407 return 1;
2408 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002409 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002410 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002411 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002412 fd_delete(t->srv_fd);
2413 if (t->srv)
2414 t->srv->cur_sess--;
2415 //close(t->srv_fd);
2416 t->srv_state = SV_STCLOSE;
2417 /* We used to have a free connection slot. Since we'll never use it,
2418 * we have to inform the server that it may be used by another session.
2419 */
2420 if (may_dequeue_tasks(t->srv, t->proxy))
2421 task_wakeup(&rq, t->srv->queue_mgt);
2422
2423 return 1;
2424 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002425 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002426 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002427 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002428 fd_delete(t->srv_fd);
2429 if (t->srv)
2430 t->srv->cur_sess--;
2431 //close(t->srv_fd);
2432 t->srv_state = SV_STCLOSE;
2433 if (!(t->flags & SN_ERR_MASK))
2434 t->flags |= SN_ERR_SRVTO;
2435 if (!(t->flags & SN_FINST_MASK))
2436 t->flags |= SN_FINST_D;
2437 /* We used to have a free connection slot. Since we'll never use it,
2438 * we have to inform the server that it may be used by another session.
2439 */
2440 if (may_dequeue_tasks(t->srv, t->proxy))
2441 task_wakeup(&rq, t->srv->queue_mgt);
2442
2443 return 1;
2444 }
2445 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002446 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2447 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002448 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002449 }
2450 }
2451 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02002452 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2453 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002454 if (t->proxy->srvtimeout)
Willy Tarreaud7971282006-07-29 18:36:34 +02002455 tv_delayfrom(&rep->rex, &now, t->proxy->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002456 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002457 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002458 }
2459 }
2460 return 0;
2461 }
2462 else { /* SV_STCLOSE : nothing to do */
2463 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2464 int len;
2465 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);
2466 write(1, trash, len);
2467 }
2468 return 0;
2469 }
2470 return 0;
2471}
2472
2473
2474/*
2475 * Produces data for the session <s> depending on its source. Expects to be
2476 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
2477 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
2478 * session, which it uses to keep on being called when there is free space in
2479 * the buffer, of simply by letting an empty buffer upon return. It returns 1
2480 * if it changes the session state from CL_STSHUTR, otherwise 0.
2481 */
2482int produce_content(struct session *s)
2483{
2484 struct buffer *rep = s->rep;
2485 struct proxy *px;
2486 struct server *sv;
2487 int msglen;
2488
2489 if (s->data_source == DATA_SRC_NONE) {
2490 s->flags &= ~SN_SELF_GEN;
2491 return 1;
2492 }
2493 else if (s->data_source == DATA_SRC_STATS) {
2494 msglen = 0;
2495
2496 if (s->data_state == DATA_ST_INIT) { /* the function had not been called yet */
2497 unsigned int up;
2498
2499 s->flags |= SN_SELF_GEN; // more data will follow
2500
2501 /* send the start of the HTTP response */
2502 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2503 "HTTP/1.0 200 OK\r\n"
2504 "Cache-Control: no-cache\r\n"
2505 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +02002506 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +02002507 "\r\n\r\n");
2508
2509 s->logs.status = 200;
2510 client_retnclose(s, msglen, trash); // send the start of the response.
2511 msglen = 0;
2512
2513 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
2514 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
2515 if (!(s->flags & SN_FINST_MASK))
2516 s->flags |= SN_FINST_R;
2517
2518 /* WARNING! This must fit in the first buffer !!! */
2519 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2520 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
2521 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2522 "<style type=\"text/css\"><!--\n"
2523 "body {"
2524 " font-family: helvetica, arial;"
2525 " font-size: 12px;"
2526 " font-weight: normal;"
2527 " color: black;"
2528 " background: white;"
2529 "}\n"
2530 "td {"
2531 " font-size: 12px;"
2532 " align: center;"
2533 "}\n"
2534 "h1 {"
2535 " font-size: xx-large;"
2536 " margin-bottom: 0.5em;"
2537 "}\n"
2538 "h2 {"
2539 " font-family: helvetica, arial;"
2540 " font-size: x-large;"
2541 " font-weight: bold;"
2542 " font-style: italic;"
2543 " color: #6020a0;"
2544 " margin-top: 0em;"
2545 " margin-bottom: 0em;"
2546 "}\n"
2547 "h3 {"
2548 " font-family: helvetica, arial;"
2549 " font-size: 16px;"
2550 " font-weight: bold;"
2551 " color: #b00040;"
2552 " background: #e8e8d0;"
2553 " margin-top: 0em;"
2554 " margin-bottom: 0em;"
2555 "}\n"
2556 "li {"
2557 " margin-top: 0.25em;"
2558 " margin-right: 2em;"
2559 "}\n"
2560 ".hr {"
2561 " margin-top: 0.25em;"
2562 " border-color: black;"
2563 " border-bottom-style: solid;"
2564 "}\n"
2565 "table.tbl { border-collapse: collapse; border-width: 1px; border-style: solid; border-color: gray;}\n"
2566 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; border-color: gray; }\n"
2567 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray; }\n"
2568 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2569 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2570 "-->"
2571 "</style></head>");
2572
2573 if (buffer_write(rep, trash, msglen) != 0)
2574 return 0;
2575 msglen = 0;
2576
2577 up = (now.tv_sec - start_date.tv_sec);
2578
2579 /* WARNING! this has to fit the first packet too */
2580 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2581 "<body><h1>" PRODUCT_NAME "</h1>\n"
2582 "<h2>Statistics Report for pid %d</h2>\n"
2583 "<hr width=\"100%%\" class=\"hr\">\n"
2584 "<h3>&gt; General process information</h3>\n"
2585 "<table border=0><tr><td align=\"left\">\n"
2586 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
2587 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2588 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
2589 "<b>maxsock = </b> %d<br>\n"
2590 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
2591 "</td><td width=\"10%%\">\n"
2592 "</td><td align=\"right\">\n"
2593 "<table class=\"lgd\">"
2594 "<tr><td bgcolor=\"#C0FFC0\">&nbsp;</td><td style=\"border-style: none;\">active UP </td>"
2595 "<td bgcolor=\"#B0D0FF\">&nbsp;</td><td style=\"border-style: none;\">backup UP </td></tr>"
2596 "<tr><td bgcolor=\"#FFFFA0\"></td><td style=\"border-style: none;\">active UP, going down </td>"
2597 "<td bgcolor=\"#C060FF\"></td><td style=\"border-style: none;\">backup UP, going down </td></tr>"
2598 "<tr><td bgcolor=\"#FFD020\"></td><td style=\"border-style: none;\">active DOWN, going up </td>"
2599 "<td bgcolor=\"#FF80FF\"></td><td style=\"border-style: none;\">backup DOWN, going up </td></tr>"
2600 "<tr><td bgcolor=\"#FF9090\"></td><td style=\"border-style: none;\">active or backup DOWN &nbsp;</td>"
2601 "<td bgcolor=\"#E0E0E0\"></td><td style=\"border-style: none;\">not checked </td></tr>"
2602 "</table>\n"
2603 "</tr></table>\n"
2604 "",
2605 pid, pid, global.nbproc,
2606 up / 86400, (up % 86400) / 3600,
2607 (up % 3600) / 60, (up % 60),
2608 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2609 global.rlimit_memmax ? " MB" : "",
2610 global.rlimit_nofile,
2611 global.maxsock,
2612 global.maxconn,
2613 actconn
2614 );
2615
2616 if (buffer_write(rep, trash, msglen) != 0)
2617 return 0;
2618 msglen = 0;
2619
2620 s->data_state = DATA_ST_DATA;
2621 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
2622
2623 px = s->data_ctx.stats.px = proxy;
2624 s->data_ctx.stats.px_st = DATA_ST_INIT;
2625 }
2626
2627 while (s->data_ctx.stats.px) {
2628 int dispatch_sess, dispatch_cum;
2629 int failed_checks, down_trans;
2630 int failed_secu, failed_conns, failed_resp;
2631
2632 if (s->data_ctx.stats.px_st == DATA_ST_INIT) {
2633 /* we are on a new proxy */
2634 px = s->data_ctx.stats.px;
2635
2636 /* skip the disabled proxies */
2637 if (px->state == PR_STSTOPPED)
2638 goto next_proxy;
2639
2640 if (s->proxy->uri_auth && s->proxy->uri_auth->scope) {
2641 /* we have a limited scope, we have to check the proxy name */
2642 struct stat_scope *scope;
2643 int len;
2644
2645 len = strlen(px->id);
2646 scope = s->proxy->uri_auth->scope;
2647
2648 while (scope) {
2649 /* match exact proxy name */
2650 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2651 break;
2652
2653 /* match '.' which means 'self' proxy */
2654 if (!strcmp(scope->px_id, ".") && px == s->proxy)
2655 break;
2656 scope = scope->next;
2657 }
2658
2659 /* proxy name not found */
2660 if (scope == NULL)
2661 goto next_proxy;
2662 }
2663
2664 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2665 "<h3>&gt; Proxy instance %s : "
2666 "%d conns (maxconn=%d), %d queued (%d unassigned), %d total conns</h3>\n"
2667 "",
2668 px->id,
2669 px->nbconn, px->maxconn, px->totpend, px->nbpend, px->cum_conn);
2670
2671 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2672 "<table cols=\"16\" class=\"tbl\">\n"
2673 "<tr align=\"center\" bgcolor=\"#20C0C0\">"
2674 "<th colspan=5>Server</th>"
2675 "<th colspan=2>Queue</th>"
2676 "<th colspan=4>Sessions</th>"
2677 "<th colspan=5>Errors</th></tr>\n"
2678 "<tr align=\"center\" bgcolor=\"#20C0C0\">"
2679 "<th>Name</th><th>Weight</th><th>Status</th><th>Act.</th><th>Bck.</th>"
2680 "<th>Curr.</th><th>Max.</th>"
2681 "<th>Curr.</th><th>Max.</th><th>Limit</th><th>Cumul.</th>"
2682 "<th>Conn.</th><th>Resp.</th><th>Sec.</th><th>Check</th><th>Down</th></tr>\n");
2683
2684 if (buffer_write(rep, trash, msglen) != 0)
2685 return 0;
2686 msglen = 0;
2687
2688 s->data_ctx.stats.sv = px->srv;
2689 s->data_ctx.stats.px_st = DATA_ST_DATA;
2690 }
2691
2692 px = s->data_ctx.stats.px;
2693
2694 /* stats.sv has been initialized above */
2695 while (s->data_ctx.stats.sv != NULL) {
2696 static char *act_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FFD020", /*failing*/"#FFFFA0", /*up*/"#C0FFC0", /*unchecked*/"#E0E0E0" };
2697 static char *bck_tab_bg[5] = { /*down*/"#FF9090", /*rising*/"#FF80ff", /*failing*/"#C060FF", /*up*/"#B0D0FF", /*unchecked*/"#E0E0E0" };
2698 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
2699 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP */
2700
2701 sv = s->data_ctx.stats.sv;
2702
2703 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
2704 if (!(sv->state & SRV_CHECKED))
2705 sv_state = 4;
2706 else if (sv->state & SRV_RUNNING)
2707 if (sv->health == sv->rise + sv->fall - 1)
2708 sv_state = 3; /* UP */
2709 else
2710 sv_state = 2; /* going down */
2711 else
2712 if (sv->health)
2713 sv_state = 1; /* going up */
2714 else
2715 sv_state = 0; /* DOWN */
2716
2717 /* name, weight */
2718 msglen += snprintf(trash, sizeof(trash),
2719 "<tr align=center bgcolor=\"%s\"><td>%s</td><td>%d</td><td>",
2720 (sv->state & SRV_BACKUP) ? bck_tab_bg[sv_state] : act_tab_bg[sv_state],
2721 sv->id, sv->uweight+1);
2722 /* status */
2723 msglen += snprintf(trash + msglen, sizeof(trash) - msglen, srv_hlt_st[sv_state],
2724 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
2725 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
2726
2727 /* act, bck */
2728 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2729 "</td><td>%s</td><td>%s</td>",
2730 (sv->state & SRV_BACKUP) ? "-" : "Y",
2731 (sv->state & SRV_BACKUP) ? "Y" : "-");
2732
2733 /* queue : current, max */
2734 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2735 "<td align=right>%d</td><td align=right>%d</td>",
2736 sv->nbpend, sv->nbpend_max);
2737
2738 /* sessions : current, max, limit, cumul */
2739 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2740 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>",
2741 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess);
2742
2743 /* errors : connect, response, security */
2744 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2745 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2746 sv->failed_conns, sv->failed_resp, sv->failed_secu);
2747
2748 /* check failures : unique, fatal */
2749 if (sv->state & SRV_CHECKED)
2750 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2751 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
2752 sv->failed_checks, sv->down_trans);
2753 else
2754 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2755 "<td align=right>-</td><td align=right>-</td></tr>\n");
2756
2757 if (buffer_write(rep, trash, msglen) != 0)
2758 return 0;
2759 msglen = 0;
2760
2761 s->data_ctx.stats.sv = sv->next;
2762 } /* while sv */
2763
2764 /* now we are past the last server, we'll dump information about the dispatcher */
2765
2766 /* We have to count down from the proxy to the servers to tell how
2767 * many sessions are on the dispatcher, and how many checks have
2768 * failed. We cannot count this during the servers dump because it
2769 * might be interrupted multiple times.
2770 */
2771 dispatch_sess = px->nbconn;
2772 dispatch_cum = px->cum_conn;
2773 failed_secu = px->failed_secu;
2774 failed_conns = px->failed_conns;
2775 failed_resp = px->failed_resp;
2776 failed_checks = down_trans = 0;
2777
2778 sv = px->srv;
2779 while (sv) {
2780 dispatch_sess -= sv->cur_sess;
2781 dispatch_cum -= sv->cum_sess;
2782 failed_conns -= sv->failed_conns;
2783 failed_resp -= sv->failed_resp;
2784 failed_secu -= sv->failed_secu;
2785 if (sv->state & SRV_CHECKED) {
2786 failed_checks += sv->failed_checks;
2787 down_trans += sv->down_trans;
2788 }
2789 sv = sv->next;
2790 }
2791
2792 /* name, weight, status, act, bck */
2793 msglen += snprintf(trash + msglen, sizeof(trash),
2794 "<tr align=center bgcolor=\"#e8e8d0\">"
2795 "<td>Dispatcher</td><td>-</td>"
2796 "<td>%s</td><td>-</td><td>-</td>",
2797 px->state == PR_STRUN ? "UP" : "DOWN");
2798
2799 /* queue : current, max */
2800 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2801 "<td align=right>%d</td><td align=right>%d</td>",
2802 px->nbpend, px->nbpend_max);
2803
2804 /* sessions : current, max, limit, cumul. */
2805 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2806 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>",
2807 dispatch_sess, px->nbconn_max, px->maxconn, dispatch_cum);
2808
2809 /* errors : connect, response, security */
2810 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2811 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2812 failed_conns, failed_resp, failed_secu);
2813
2814 /* check failures : unique, fatal */
2815 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2816 "<td align=right>-</td><td align=right>-</td></tr>\n");
2817
2818
2819 /* now the summary for the whole proxy */
2820 /* name, weight, status, act, bck */
2821 msglen += snprintf(trash + msglen, sizeof(trash),
2822 "<tr align=center style=\"color: #ffff80; background: #20C0C0;\">"
2823 "<td><b>Total</b></td><td>-</td>"
2824 "<td><b>%s</b></td><td><b>%d</b></td><td><b>%d</b></td>",
2825 (px->state == PR_STRUN && ((px->srv == NULL) || px->srv_act || px->srv_bck)) ? "UP" : "DOWN",
2826 px->srv_act, px->srv_bck);
2827
2828 /* queue : current, max */
2829 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2830 "<td align=right><b>%d</b></td><td align=right><b>%d</b></td>",
2831 px->totpend, px->nbpend_max);
2832
2833 /* sessions : current, max, limit, cumul */
2834 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2835 "<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>",
2836 px->nbconn, px->nbconn_max, px->maxconn, px->cum_conn);
2837
2838 /* errors : connect, response, security */
2839 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2840 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>\n",
2841 px->failed_conns, px->failed_resp, px->failed_secu);
2842
2843 /* check failures : unique, fatal */
2844 msglen += snprintf(trash + msglen, sizeof(trash) - msglen,
2845 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
2846 failed_checks, down_trans);
2847
2848 msglen += snprintf(trash + msglen, sizeof(trash) - msglen, "</table><p>\n");
2849
2850 if (buffer_write(rep, trash, msglen) != 0)
2851 return 0;
2852 msglen = 0;
2853
2854 s->data_ctx.stats.px_st = DATA_ST_INIT;
2855 next_proxy:
2856 s->data_ctx.stats.px = px->next;
2857 } /* proxy loop */
2858 /* here, we just have reached the sv == NULL and px == NULL */
2859 s->flags &= ~SN_SELF_GEN;
2860 return 1;
2861 }
2862 else {
2863 /* unknown data source */
2864 s->logs.status = 500;
2865 client_retnclose(s, s->proxy->errmsg.len500, s->proxy->errmsg.msg500);
2866 if (!(s->flags & SN_ERR_MASK))
2867 s->flags |= SN_ERR_PRXCOND;
2868 if (!(s->flags & SN_FINST_MASK))
2869 s->flags |= SN_FINST_R;
2870 s->flags &= SN_SELF_GEN;
2871 return 1;
2872 }
2873}
2874
2875
2876/*
2877 * Local variables:
2878 * c-indent-level: 8
2879 * c-basic-offset: 8
2880 * End:
2881 */