blob: b01859080aa46be8981ae22a552935427cfb77ef [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaua15645d2007-03-18 16:22:39 +01004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/appsession.h>
26#include <common/compat.h>
27#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010028#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/memory.h>
30#include <common/mini-clist.h>
31#include <common/standard.h>
32#include <common/time.h>
33#include <common/uri_auth.h>
34#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36#include <types/capture.h>
37#include <types/client.h>
38#include <types/global.h>
39#include <types/httperr.h>
40#include <types/polling.h>
41#include <types/proxy.h>
42#include <types/server.h>
43
44#include <proto/backend.h>
45#include <proto/buffers.h>
46#include <proto/fd.h>
47#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010048#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
51#include <proto/session.h>
52#include <proto/task.h>
53
Willy Tarreau6d1a9882007-01-07 02:03:04 +010054#ifdef CONFIG_HAP_TCPSPLICE
55#include <libtcpsplice.h>
56#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
Willy Tarreau58f10d72006-12-04 02:26:12 +010058#define DEBUG_PARSE_NO_SPEEDUP
59#undef DEBUG_PARSE_NO_SPEEDUP
60
Willy Tarreau976f1ee2006-12-17 10:06:03 +010061/* This is used to perform a quick jump as an alternative to a break/continue
62 * instruction. The first argument is the label for normal operation, and the
63 * second one is the break/continue instruction in the no_speedup mode.
64 */
65
66#ifdef DEBUG_PARSE_NO_SPEEDUP
67#define QUICK_JUMP(x,y) y
68#else
69#define QUICK_JUMP(x,y) goto x
70#endif
71
Willy Tarreau1c47f852006-07-09 08:22:27 +020072/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010073const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020074 "HTTP/1.0 200 OK\r\n"
75 "Cache-Control: no-cache\r\n"
76 "Connection: close\r\n"
77 "Content-Type: text/html\r\n"
78 "\r\n"
79 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const struct chunk http_200_chunk = {
82 .str = (char *)&HTTP_200,
83 .len = sizeof(HTTP_200)-1
84};
85
86const char *HTTP_302 =
87 "HTTP/1.0 302 Found\r\n"
88 "Cache-Control: no-cache\r\n"
89 "Connection: close\r\n"
90 "Location: "; /* not terminated since it will be concatenated with the URL */
91
92/* same as 302 except that the browser MUST retry with the GET method */
93const char *HTTP_303 =
94 "HTTP/1.0 303 See Other\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
97 "Location: "; /* not terminated since it will be concatenated with the URL */
98
Willy Tarreaubaaee002006-06-26 02:48:02 +020099/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
100const char *HTTP_401_fmt =
101 "HTTP/1.0 401 Unauthorized\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200104 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
106 "\r\n"
107 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
108
Willy Tarreau0f772532006-12-23 20:51:41 +0100109
110const int http_err_codes[HTTP_ERR_SIZE] = {
111 [HTTP_ERR_400] = 400,
112 [HTTP_ERR_403] = 403,
113 [HTTP_ERR_408] = 408,
114 [HTTP_ERR_500] = 500,
115 [HTTP_ERR_502] = 502,
116 [HTTP_ERR_503] = 503,
117 [HTTP_ERR_504] = 504,
118};
119
Willy Tarreau80587432006-12-24 17:47:20 +0100120static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100121 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100122 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "\r\n"
127 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
128
129 [HTTP_ERR_403] =
130 "HTTP/1.0 403 Forbidden\r\n"
131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "\r\n"
135 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
136
137 [HTTP_ERR_408] =
138 "HTTP/1.0 408 Request Time-out\r\n"
139 "Cache-Control: no-cache\r\n"
140 "Connection: close\r\n"
141 "Content-Type: text/html\r\n"
142 "\r\n"
143 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
144
145 [HTTP_ERR_500] =
146 "HTTP/1.0 500 Server Error\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
152
153 [HTTP_ERR_502] =
154 "HTTP/1.0 502 Bad Gateway\r\n"
155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
160
161 [HTTP_ERR_503] =
162 "HTTP/1.0 503 Service Unavailable\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
168
169 [HTTP_ERR_504] =
170 "HTTP/1.0 504 Gateway Time-out\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
176
177};
178
Willy Tarreau80587432006-12-24 17:47:20 +0100179/* We must put the messages here since GCC cannot initialize consts depending
180 * on strlen().
181 */
182struct chunk http_err_chunks[HTTP_ERR_SIZE];
183
184void init_proto_http()
185{
186 int msg;
187 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
188 if (!http_err_msgs[msg]) {
189 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
190 abort();
191 }
192
193 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
194 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
195 }
196}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197
Willy Tarreau53b6c742006-12-17 13:37:46 +0100198/*
199 * We have 26 list of methods (1 per first letter), each of which can have
200 * up to 3 entries (2 valid, 1 null).
201 */
202struct http_method_desc {
203 http_meth_t meth;
204 int len;
205 const char text[8];
206};
207
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100208const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100209 ['C' - 'A'] = {
210 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
211 },
212 ['D' - 'A'] = {
213 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
214 },
215 ['G' - 'A'] = {
216 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
217 },
218 ['H' - 'A'] = {
219 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
220 },
221 ['P' - 'A'] = {
222 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
223 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
224 },
225 ['T' - 'A'] = {
226 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
227 },
228 /* rest is empty like this :
229 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
230 */
231};
232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100233/* It is about twice as fast on recent architectures to lookup a byte in a
234 * table than two perform a boolean AND or OR between two tests. Refer to
235 * RFC2616 for those chars.
236 */
237
238const char http_is_spht[256] = {
239 [' '] = 1, ['\t'] = 1,
240};
241
242const char http_is_crlf[256] = {
243 ['\r'] = 1, ['\n'] = 1,
244};
245
246const char http_is_lws[256] = {
247 [' '] = 1, ['\t'] = 1,
248 ['\r'] = 1, ['\n'] = 1,
249};
250
251const char http_is_sep[256] = {
252 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
253 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
254 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
255 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
256 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
257};
258
259const char http_is_ctl[256] = {
260 [0 ... 31] = 1,
261 [127] = 1,
262};
263
264/*
265 * A token is any ASCII char that is neither a separator nor a CTL char.
266 * Do not overwrite values in assignment since gcc-2.95 will not handle
267 * them correctly. Instead, define every non-CTL char's status.
268 */
269const char http_is_token[256] = {
270 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
271 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
272 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
273 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
274 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
275 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
276 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
277 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
278 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
279 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
280 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
281 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
282 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
283 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
284 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
285 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
286 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
287 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
288 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
289 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
290 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
291 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
292 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
293 ['|'] = 1, ['}'] = 0, ['~'] = 1,
294};
295
296
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100297/*
298 * An http ver_token is any ASCII which can be found in an HTTP version,
299 * which includes 'H', 'T', 'P', '/', '.' and any digit.
300 */
301const char http_is_ver_token[256] = {
302 ['.'] = 1, ['/'] = 1,
303 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
304 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
305 ['H'] = 1, ['P'] = 1, ['T'] = 1,
306};
307
308
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309#ifdef DEBUG_FULL
310static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
311static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
312#endif
313
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100314/*
315 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
316 * CRLF. Text length is measured first, so it cannot be NULL.
317 * The header is also automatically added to the index <hdr_idx>, and the end
318 * of headers is automatically adjusted. The number of bytes added is returned
319 * on success, otherwise <0 is returned indicating an error.
320 */
321int http_header_add_tail(struct buffer *b, struct http_msg *msg,
322 struct hdr_idx *hdr_idx, const char *text)
323{
324 int bytes, len;
325
326 len = strlen(text);
327 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
328 if (!bytes)
329 return -1;
330 msg->eoh += bytes;
331 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
332}
333
334/*
335 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
336 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
337 * the buffer is only opened and the space reserved, but nothing is copied.
338 * The header is also automatically added to the index <hdr_idx>, and the end
339 * of headers is automatically adjusted. The number of bytes added is returned
340 * on success, otherwise <0 is returned indicating an error.
341 */
342int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
343 struct hdr_idx *hdr_idx, const char *text, int len)
344{
345 int bytes;
346
347 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
348 if (!bytes)
349 return -1;
350 msg->eoh += bytes;
351 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
352}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353
354/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100355 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
356 * If so, returns the position of the first non-space character relative to
357 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
358 * to return a pointer to the place after the first space. Returns 0 if the
359 * header name does not match. Checks are case-insensitive.
360 */
361int http_header_match2(const char *hdr, const char *end,
362 const char *name, int len)
363{
364 const char *val;
365
366 if (hdr + len >= end)
367 return 0;
368 if (hdr[len] != ':')
369 return 0;
370 if (strncasecmp(hdr, name, len) != 0)
371 return 0;
372 val = hdr + len + 1;
373 while (val < end && HTTP_IS_SPHT(*val))
374 val++;
375 if ((val >= end) && (len + 2 <= end - hdr))
376 return len + 2; /* we may replace starting from second space */
377 return val - hdr;
378}
379
380/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381 * returns a message to the client ; the connection is shut down for read,
382 * and the request is cleared so that no server connection can be initiated.
383 * The client must be in a valid state for this (HEADER, DATA ...).
Willy Tarreau0f772532006-12-23 20:51:41 +0100384 * Nothing is performed on the server side. The message is contained in a
385 * "chunk". If it is null, then an empty message is used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 * The reply buffer doesn't need to be empty before this.
387 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100388void client_retnclose(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389{
Willy Tarreau2a429502006-10-15 14:52:29 +0200390 MY_FD_CLR(s->cli_fd, StaticReadEvent);
391 MY_FD_SET(s->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +0200392 tv_eternity(&s->req->rex);
Willy Tarreau73de9892006-11-30 11:40:23 +0100393 if (s->fe->clitimeout)
394 tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395 else
Willy Tarreaud7971282006-07-29 18:36:34 +0200396 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397 shutdown(s->cli_fd, SHUT_RD);
398 s->cli_state = CL_STSHUTR;
399 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100400 if (msg->len)
401 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402 s->req->l = 0;
403}
404
405
406/*
407 * returns a message into the rep buffer, and flushes the req buffer.
Willy Tarreau0f772532006-12-23 20:51:41 +0100408 * The reply buffer doesn't need to be empty before this. The message
409 * is contained in a "chunk". If it is null, then an empty message is
410 * used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100412void client_return(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413{
414 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100415 if (msg->len)
416 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200417 s->req->l = 0;
418}
419
420
421/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100422 * indicators accordingly. Note that if <status> is 0, or if the message
423 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200424 */
425void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100426 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200427{
428 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100429 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100430 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100431 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100432 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433 }
434 if (!(t->flags & SN_ERR_MASK))
435 t->flags |= err;
436 if (!(t->flags & SN_FINST_MASK))
437 t->flags |= finst;
438}
439
Willy Tarreau80587432006-12-24 17:47:20 +0100440/* This function returns the appropriate error location for the given session
441 * and message.
442 */
443
444struct chunk *error_message(struct session *s, int msgnum)
445{
446 if (s->be->beprm->errmsg[msgnum].str)
447 return &s->be->beprm->errmsg[msgnum];
448 else if (s->fe->errmsg[msgnum].str)
449 return &s->fe->errmsg[msgnum];
450 else
451 return &http_err_chunks[msgnum];
452}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453
Willy Tarreau53b6c742006-12-17 13:37:46 +0100454/*
455 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
456 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
457 */
458static http_meth_t find_http_meth(const char *str, const int len)
459{
460 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100461 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100462
463 m = ((unsigned)*str - 'A');
464
465 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100466 for (h = http_methods[m]; h->len > 0; h++) {
467 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100468 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100469 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100470 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100471 };
472 return HTTP_METH_OTHER;
473 }
474 return HTTP_METH_NONE;
475
476}
477
478
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479/* Processes the client and server jobs of a session task, then
480 * puts it back to the wait queue in a clean state, or
481 * cleans up its resources if it must be deleted. Returns
482 * the time the task accepts to wait, or TIME_ETERNITY for
483 * infinity.
484 */
485int process_session(struct task *t)
486{
487 struct session *s = t->context;
488 int fsm_resync = 0;
489
490 do {
491 fsm_resync = 0;
492 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
493 fsm_resync |= process_cli(s);
494 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
495 fsm_resync |= process_srv(s);
496 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
497 } while (fsm_resync);
498
499 if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) {
500 struct timeval min1, min2;
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200501 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
502 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503
Willy Tarreaud7971282006-07-29 18:36:34 +0200504 tv_min(&min1, &s->req->rex, &s->req->wex);
505 tv_min(&min2, &s->rep->rex, &s->rep->wex);
506 tv_min(&min1, &min1, &s->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200507 tv_min(&t->expire, &min1, &min2);
508
509 /* restore t to its place in the task list */
510 task_queue(t);
511
512#ifdef DEBUG_FULL
513 /* DEBUG code : this should never ever happen, otherwise it indicates
514 * that a task still has something to do and will provoke a quick loop.
515 */
516 if (tv_remain2(&now, &t->expire) <= 0)
517 exit(100);
518#endif
519
520 return tv_remain2(&now, &t->expire); /* nothing more to do */
521 }
522
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100523 s->fe->feconn--;
524 if (s->flags & SN_BE_ASSIGNED)
525 s->be->beprm->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526 actconn--;
527
528 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
529 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100530 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100531 s->uniq_id, s->be->beprm->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100532 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533 write(1, trash, len);
534 }
535
536 s->logs.t_close = tv_diff(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100537 if (s->req != NULL)
538 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100540 s->logs.bytes_out = s->rep->total;
541
542 s->fe->bytes_in += s->logs.bytes_in;
543 s->fe->bytes_out += s->logs.bytes_out;
544 if (s->be->beprm != s->fe) {
545 s->be->beprm->bytes_in += s->logs.bytes_in;
546 s->be->beprm->bytes_out += s->logs.bytes_out;
547 }
548 if (s->srv) {
549 s->srv->bytes_in += s->logs.bytes_in;
550 s->srv->bytes_out += s->logs.bytes_out;
551 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552
553 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200554 if (s->logs.logwait &&
555 !(s->flags & SN_MONITOR) &&
Willy Tarreau73de9892006-11-30 11:40:23 +0100556 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200557 sess_log(s);
558
559 /* the task MUST not be in the run queue anymore */
560 task_delete(t);
561 session_free(s);
562 task_free(t);
563 return TIME_ETERNITY; /* rest in peace for eternity */
564}
565
566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100567/* either we find an LF at <ptr> or we jump to <bad>.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100569#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
570
571/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
572 * otherwise to <http_msg_ood> with <state> set to <st>.
573 */
574#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
575 ptr++; \
576 if (likely(ptr < end)) \
577 goto good; \
578 else { \
579 state = (st); \
580 goto http_msg_ood; \
581 } \
582 } while (0)
583
Willy Tarreau117f59e2007-03-04 18:17:17 +0100584
585/*
586 * Capture headers from message starting at <som> according to header list
587 * <cap_hdr>, and fill the <idx> structure appropriately.
588 */
589void capture_headers(char *som, struct hdr_idx *idx,
590 char **cap, struct cap_hdr *cap_hdr)
591{
592 char *eol, *sol, *col, *sov;
593 int cur_idx;
594 struct cap_hdr *h;
595 int len;
596
597 sol = som + hdr_idx_first_pos(idx);
598 cur_idx = hdr_idx_first_idx(idx);
599
600 while (cur_idx) {
601 eol = sol + idx->v[cur_idx].len;
602
603 col = sol;
604 while (col < eol && *col != ':')
605 col++;
606
607 sov = col + 1;
608 while (sov < eol && http_is_lws[(unsigned char)*sov])
609 sov++;
610
611 for (h = cap_hdr; h; h = h->next) {
612 if ((h->namelen == col - sol) &&
613 (strncasecmp(sol, h->name, h->namelen) == 0)) {
614 if (cap[h->index] == NULL)
615 cap[h->index] =
616 pool_alloc_from(h->pool, h->len + 1);
617
618 if (cap[h->index] == NULL) {
619 Alert("HTTP capture : out of memory.\n");
620 continue;
621 }
622
623 len = eol - sov;
624 if (len > h->len)
625 len = h->len;
626
627 memcpy(cap[h->index], sov, len);
628 cap[h->index][len]=0;
629 }
630 }
631 sol = eol + idx->v[cur_idx].cr + 1;
632 cur_idx = idx->v[cur_idx].next;
633 }
634}
635
636
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100638 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100639 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
640 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
641 * will give undefined results.
642 * Note that it is upon the caller's responsibility to ensure that ptr < end,
643 * and that msg->sol points to the beginning of the response.
644 * If a complete line is found (which implies that at least one CR or LF is
645 * found before <end>, the updated <ptr> is returned, otherwise NULL is
646 * returned indicating an incomplete line (which does not mean that parts have
647 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
648 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
649 * upon next call.
650 *
651 * This function was intentionnally designed to be called from
652 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
653 * within its state machine and use the same macros, hence the need for same
654 * labels and variable names.
655 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100656const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100657 const char *ptr, const char *end,
658 char **ret_ptr, int *ret_state)
659{
660 __label__
661 http_msg_rpver,
662 http_msg_rpver_sp,
663 http_msg_rpcode,
664 http_msg_rpcode_sp,
665 http_msg_rpreason,
666 http_msg_rpline_eol,
667 http_msg_ood, /* out of data */
668 http_msg_invalid;
669
670 switch (state) {
671 http_msg_rpver:
672 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100673 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100674 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
675
676 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100677 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100678 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
679 }
680 goto http_msg_invalid;
681
682 http_msg_rpver_sp:
683 case HTTP_MSG_RPVER_SP:
684 if (likely(!HTTP_IS_LWS(*ptr))) {
685 msg->sl.st.c = ptr - msg_buf;
686 goto http_msg_rpcode;
687 }
688 if (likely(HTTP_IS_SPHT(*ptr)))
689 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
690 /* so it's a CR/LF, this is invalid */
691 goto http_msg_invalid;
692
693 http_msg_rpcode:
694 case HTTP_MSG_RPCODE:
695 if (likely(!HTTP_IS_LWS(*ptr)))
696 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
697
698 if (likely(HTTP_IS_SPHT(*ptr))) {
699 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
700 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
701 }
702
703 /* so it's a CR/LF, so there is no reason phrase */
704 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
705 http_msg_rsp_reason:
706 /* FIXME: should we support HTTP responses without any reason phrase ? */
707 msg->sl.st.r = ptr - msg_buf;
708 msg->sl.st.r_l = 0;
709 goto http_msg_rpline_eol;
710
711 http_msg_rpcode_sp:
712 case HTTP_MSG_RPCODE_SP:
713 if (likely(!HTTP_IS_LWS(*ptr))) {
714 msg->sl.st.r = ptr - msg_buf;
715 goto http_msg_rpreason;
716 }
717 if (likely(HTTP_IS_SPHT(*ptr)))
718 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
719 /* so it's a CR/LF, so there is no reason phrase */
720 goto http_msg_rsp_reason;
721
722 http_msg_rpreason:
723 case HTTP_MSG_RPREASON:
724 if (likely(!HTTP_IS_CRLF(*ptr)))
725 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
726 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
727 http_msg_rpline_eol:
728 /* We have seen the end of line. Note that we do not
729 * necessarily have the \n yet, but at least we know that we
730 * have EITHER \r OR \n, otherwise the response would not be
731 * complete. We can then record the response length and return
732 * to the caller which will be able to register it.
733 */
734 msg->sl.st.l = ptr - msg->sol;
735 return ptr;
736
737#ifdef DEBUG_FULL
738 default:
739 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
740 exit(1);
741#endif
742 }
743
744 http_msg_ood:
745 /* out of data */
746 if (ret_state)
747 *ret_state = state;
748 if (ret_ptr)
749 *ret_ptr = (char *)ptr;
750 return NULL;
751
752 http_msg_invalid:
753 /* invalid message */
754 if (ret_state)
755 *ret_state = HTTP_MSG_ERROR;
756 return NULL;
757}
758
759
760/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100761 * This function parses a request line between <ptr> and <end>, starting with
762 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
763 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
764 * will give undefined results.
765 * Note that it is upon the caller's responsibility to ensure that ptr < end,
766 * and that msg->sol points to the beginning of the request.
767 * If a complete line is found (which implies that at least one CR or LF is
768 * found before <end>, the updated <ptr> is returned, otherwise NULL is
769 * returned indicating an incomplete line (which does not mean that parts have
770 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
771 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
772 * upon next call.
773 *
774 * This function was intentionnally designed to be called from
775 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
776 * within its state machine and use the same macros, hence the need for same
777 * labels and variable names.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100779const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100780 const char *ptr, const char *end,
781 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100783 __label__
784 http_msg_rqmeth,
785 http_msg_rqmeth_sp,
786 http_msg_rquri,
787 http_msg_rquri_sp,
788 http_msg_rqver,
789 http_msg_rqline_eol,
790 http_msg_ood, /* out of data */
791 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100793 switch (state) {
794 http_msg_rqmeth:
795 case HTTP_MSG_RQMETH:
796 if (likely(HTTP_IS_TOKEN(*ptr)))
797 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100799 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100800 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100801 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
802 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100804 if (likely(HTTP_IS_CRLF(*ptr))) {
805 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100806 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100807 http_msg_req09_uri:
808 msg->sl.rq.u = ptr - msg_buf;
809 http_msg_req09_uri_e:
810 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
811 http_msg_req09_ver:
812 msg->sl.rq.v = ptr - msg_buf;
813 msg->sl.rq.v_l = 0;
814 goto http_msg_rqline_eol;
815 }
816 goto http_msg_invalid;
817
818 http_msg_rqmeth_sp:
819 case HTTP_MSG_RQMETH_SP:
820 if (likely(!HTTP_IS_LWS(*ptr))) {
821 msg->sl.rq.u = ptr - msg_buf;
822 goto http_msg_rquri;
823 }
824 if (likely(HTTP_IS_SPHT(*ptr)))
825 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
826 /* so it's a CR/LF, meaning an HTTP 0.9 request */
827 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100828
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100829 http_msg_rquri:
830 case HTTP_MSG_RQURI:
831 if (likely(!HTTP_IS_LWS(*ptr)))
832 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100833
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100834 if (likely(HTTP_IS_SPHT(*ptr))) {
835 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
836 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
837 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100839 /* so it's a CR/LF, meaning an HTTP 0.9 request */
840 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100841
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100842 http_msg_rquri_sp:
843 case HTTP_MSG_RQURI_SP:
844 if (likely(!HTTP_IS_LWS(*ptr))) {
845 msg->sl.rq.v = ptr - msg_buf;
846 goto http_msg_rqver;
847 }
848 if (likely(HTTP_IS_SPHT(*ptr)))
849 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
850 /* so it's a CR/LF, meaning an HTTP 0.9 request */
851 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100853 http_msg_rqver:
854 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100855 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100856 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100857
858 if (likely(HTTP_IS_CRLF(*ptr))) {
859 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
860 http_msg_rqline_eol:
861 /* We have seen the end of line. Note that we do not
862 * necessarily have the \n yet, but at least we know that we
863 * have EITHER \r OR \n, otherwise the request would not be
864 * complete. We can then record the request length and return
865 * to the caller which will be able to register it.
866 */
867 msg->sl.rq.l = ptr - msg->sol;
868 return ptr;
869 }
870
871 /* neither an HTTP_VER token nor a CRLF */
872 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100873
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100874#ifdef DEBUG_FULL
875 default:
876 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
877 exit(1);
878#endif
879 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100880
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100881 http_msg_ood:
882 /* out of data */
883 if (ret_state)
884 *ret_state = state;
885 if (ret_ptr)
886 *ret_ptr = (char *)ptr;
887 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100888
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100889 http_msg_invalid:
890 /* invalid message */
891 if (ret_state)
892 *ret_state = HTTP_MSG_ERROR;
893 return NULL;
894}
Willy Tarreau58f10d72006-12-04 02:26:12 +0100895
896
Willy Tarreau8973c702007-01-21 23:58:29 +0100897/*
898 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100899 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +0100900 * when data are missing and recalled at the exact same location with no
901 * information loss. The header index is re-initialized when switching from
902 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH.
903 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100904void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
905{
906 __label__
907 http_msg_rqbefore,
908 http_msg_rqbefore_cr,
909 http_msg_rqmeth,
910 http_msg_rqline_end,
911 http_msg_hdr_first,
912 http_msg_hdr_name,
913 http_msg_hdr_l1_sp,
914 http_msg_hdr_l1_lf,
915 http_msg_hdr_l1_lws,
916 http_msg_hdr_val,
917 http_msg_hdr_l2_lf,
918 http_msg_hdr_l2_lws,
919 http_msg_complete_header,
920 http_msg_last_lf,
921 http_msg_ood, /* out of data */
922 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100923
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100924 int state; /* updated only when leaving the FSM */
925 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100926
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100927 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100928 ptr = buf->lr;
929 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100930
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100931 if (unlikely(ptr >= end))
932 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100933
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100934 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100935 /*
936 * First, states that are specific to the response only.
937 * We check them first so that request and headers are
938 * closer to each other (accessed more often).
939 */
940 http_msg_rpbefore:
941 case HTTP_MSG_RPBEFORE:
942 if (likely(HTTP_IS_TOKEN(*ptr))) {
943 if (likely(ptr == buf->data)) {
944 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100945 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +0100946 } else {
947#if PARSE_PRESERVE_EMPTY_LINES
948 /* only skip empty leading lines, don't remove them */
949 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100950 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +0100951#else
952 /* Remove empty leading lines, as recommended by
953 * RFC2616. This takes a lot of time because we
954 * must move all the buffer backwards, but this
955 * is rarely needed. The method above will be
956 * cleaner when we'll be able to start sending
957 * the request from any place in the buffer.
958 */
959 buf->lr = ptr;
960 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100961 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 msg->sol = buf->data;
963 ptr = buf->data;
964 end = buf->r;
965#endif
966 }
967 hdr_idx_init(idx);
968 state = HTTP_MSG_RPVER;
969 goto http_msg_rpver;
970 }
971
972 if (unlikely(!HTTP_IS_CRLF(*ptr)))
973 goto http_msg_invalid;
974
975 if (unlikely(*ptr == '\n'))
976 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
977 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
978 /* stop here */
979
980 http_msg_rpbefore_cr:
981 case HTTP_MSG_RPBEFORE_CR:
982 EXPECT_LF_HERE(ptr, http_msg_invalid);
983 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
984 /* stop here */
985
986 http_msg_rpver:
987 case HTTP_MSG_RPVER:
988 case HTTP_MSG_RPVER_SP:
989 case HTTP_MSG_RPCODE:
990 case HTTP_MSG_RPCODE_SP:
991 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +0100992 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100993 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 if (unlikely(!ptr))
995 return;
996
997 /* we have a full response and we know that we have either a CR
998 * or an LF at <ptr>.
999 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001000 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001001 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1002
1003 msg->sol = ptr;
1004 if (likely(*ptr == '\r'))
1005 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1006 goto http_msg_rpline_end;
1007
1008 http_msg_rpline_end:
1009 case HTTP_MSG_RPLINE_END:
1010 /* msg->sol must point to the first of CR or LF. */
1011 EXPECT_LF_HERE(ptr, http_msg_invalid);
1012 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1013 /* stop here */
1014
1015 /*
1016 * Second, states that are specific to the request only
1017 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001018 http_msg_rqbefore:
1019 case HTTP_MSG_RQBEFORE:
1020 if (likely(HTTP_IS_TOKEN(*ptr))) {
1021 if (likely(ptr == buf->data)) {
1022 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001023 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001024 } else {
1025#if PARSE_PRESERVE_EMPTY_LINES
1026 /* only skip empty leading lines, don't remove them */
1027 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001028 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001029#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001030 /* Remove empty leading lines, as recommended by
1031 * RFC2616. This takes a lot of time because we
1032 * must move all the buffer backwards, but this
1033 * is rarely needed. The method above will be
1034 * cleaner when we'll be able to start sending
1035 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001036 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001037 buf->lr = ptr;
1038 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001039 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040 msg->sol = buf->data;
1041 ptr = buf->data;
1042 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001043#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001044 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001045 /* we will need this when keep-alive will be supported
1046 hdr_idx_init(idx);
1047 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001048 state = HTTP_MSG_RQMETH;
1049 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001050 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001052 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1053 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001054
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001055 if (unlikely(*ptr == '\n'))
1056 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1057 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001058 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001059
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001060 http_msg_rqbefore_cr:
1061 case HTTP_MSG_RQBEFORE_CR:
1062 EXPECT_LF_HERE(ptr, http_msg_invalid);
1063 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001064 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001065
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066 http_msg_rqmeth:
1067 case HTTP_MSG_RQMETH:
1068 case HTTP_MSG_RQMETH_SP:
1069 case HTTP_MSG_RQURI:
1070 case HTTP_MSG_RQURI_SP:
1071 case HTTP_MSG_RQVER:
1072 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001073 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 if (unlikely(!ptr))
1075 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 /* we have a full request and we know that we have either a CR
1078 * or an LF at <ptr>.
1079 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001080 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 msg->sol = ptr;
1084 if (likely(*ptr == '\r'))
1085 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 http_msg_rqline_end:
1089 case HTTP_MSG_RQLINE_END:
1090 /* check for HTTP/0.9 request : no version information available.
1091 * msg->sol must point to the first of CR or LF.
1092 */
1093 if (unlikely(msg->sl.rq.v_l == 0))
1094 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001095
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 EXPECT_LF_HERE(ptr, http_msg_invalid);
1097 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001098 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001099
Willy Tarreau8973c702007-01-21 23:58:29 +01001100 /*
1101 * Common states below
1102 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 http_msg_hdr_first:
1104 case HTTP_MSG_HDR_FIRST:
1105 msg->sol = ptr;
1106 if (likely(!HTTP_IS_CRLF(*ptr))) {
1107 goto http_msg_hdr_name;
1108 }
1109
1110 if (likely(*ptr == '\r'))
1111 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1112 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 http_msg_hdr_name:
1115 case HTTP_MSG_HDR_NAME:
1116 /* assumes msg->sol points to the first char */
1117 if (likely(HTTP_IS_TOKEN(*ptr)))
1118 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001119
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001120 if (likely(*ptr == ':')) {
1121 msg->col = ptr - buf->data;
1122 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001125 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001126
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001127 http_msg_hdr_l1_sp:
1128 case HTTP_MSG_HDR_L1_SP:
1129 /* assumes msg->sol points to the first char and msg->col to the colon */
1130 if (likely(HTTP_IS_SPHT(*ptr)))
1131 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001132
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001133 /* header value can be basically anything except CR/LF */
1134 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001135
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001136 if (likely(!HTTP_IS_CRLF(*ptr))) {
1137 goto http_msg_hdr_val;
1138 }
1139
1140 if (likely(*ptr == '\r'))
1141 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1142 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144 http_msg_hdr_l1_lf:
1145 case HTTP_MSG_HDR_L1_LF:
1146 EXPECT_LF_HERE(ptr, http_msg_invalid);
1147 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149 http_msg_hdr_l1_lws:
1150 case HTTP_MSG_HDR_L1_LWS:
1151 if (likely(HTTP_IS_SPHT(*ptr))) {
1152 /* replace HT,CR,LF with spaces */
1153 for (; buf->data+msg->sov < ptr; msg->sov++)
1154 buf->data[msg->sov] = ' ';
1155 goto http_msg_hdr_l1_sp;
1156 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001157 /* we had a header consisting only in spaces ! */
1158 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 goto http_msg_complete_header;
1160
1161 http_msg_hdr_val:
1162 case HTTP_MSG_HDR_VAL:
1163 /* assumes msg->sol points to the first char, msg->col to the
1164 * colon, and msg->sov points to the first character of the
1165 * value.
1166 */
1167 if (likely(!HTTP_IS_CRLF(*ptr)))
1168 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170 msg->eol = ptr;
1171 /* Note: we could also copy eol into ->eoh so that we have the
1172 * real header end in case it ends with lots of LWS, but is this
1173 * really needed ?
1174 */
1175 if (likely(*ptr == '\r'))
1176 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1177 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001178
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 http_msg_hdr_l2_lf:
1180 case HTTP_MSG_HDR_L2_LF:
1181 EXPECT_LF_HERE(ptr, http_msg_invalid);
1182 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001183
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 http_msg_hdr_l2_lws:
1185 case HTTP_MSG_HDR_L2_LWS:
1186 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1187 /* LWS: replace HT,CR,LF with spaces */
1188 for (; msg->eol < ptr; msg->eol++)
1189 *msg->eol = ' ';
1190 goto http_msg_hdr_val;
1191 }
1192 http_msg_complete_header:
1193 /*
1194 * It was a new header, so the last one is finished.
1195 * Assumes msg->sol points to the first char, msg->col to the
1196 * colon, msg->sov points to the first character of the value
1197 * and msg->eol to the first CR or LF so we know how the line
1198 * ends. We insert last header into the index.
1199 */
1200 /*
1201 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1202 write(2, msg->sol, msg->eol-msg->sol);
1203 fprintf(stderr,"\n");
1204 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001205
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001206 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1207 idx, idx->tail) < 0))
1208 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001209
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 msg->sol = ptr;
1211 if (likely(!HTTP_IS_CRLF(*ptr))) {
1212 goto http_msg_hdr_name;
1213 }
1214
1215 if (likely(*ptr == '\r'))
1216 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1217 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001218
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001219 http_msg_last_lf:
1220 case HTTP_MSG_LAST_LF:
1221 /* Assumes msg->sol points to the first of either CR or LF */
1222 EXPECT_LF_HERE(ptr, http_msg_invalid);
1223 ptr++;
1224 buf->lr = ptr;
1225 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001226 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001227 return;
1228#ifdef DEBUG_FULL
1229 default:
1230 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1231 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001232#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233 }
1234 http_msg_ood:
1235 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001236 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 buf->lr = ptr;
1238 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 http_msg_invalid:
1241 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001242 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 return;
1244}
1245
1246/*
1247 * manages the client FSM and its socket. BTW, it also tries to handle the
1248 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1249 * 0 else.
1250 */
1251int process_cli(struct session *t)
1252{
1253 int s = t->srv_state;
1254 int c = t->cli_state;
1255 struct buffer *req = t->req;
1256 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1259 cli_stnames[c], srv_stnames[s],
1260 MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
1261 req->rex.tv_sec, req->rex.tv_usec,
1262 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 if (c == CL_STHEADERS) {
1265 /*
1266 * Now parse the partial (or complete) lines.
1267 * We will check the request syntax, and also join multi-line
1268 * headers. An index of all the lines will be elaborated while
1269 * parsing.
1270 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001271 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001274 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 * req->data + req->eoh = end of processed headers / start of current one
1276 * req->data + req->eol = end of current header or line (LF or CRLF)
1277 * req->lr = first non-visited byte
1278 * req->r = end of data
1279 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001280
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001282 struct http_txn *txn = &t->txn;
1283 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001287 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 /* 1: we might have to print this header in debug mode */
1290 if (unlikely((global.mode & MODE_DEBUG) &&
1291 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001292 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001295 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 eol = sol + msg->sl.rq.l;
1297 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001298
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001299 sol += hdr_idx_first_pos(&txn->hdr_idx);
1300 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001303 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001305 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1306 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 }
1308 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001309
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
1311 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001313 * If not so, we check the FD and buffer states before leaving.
1314 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1316 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317 *
1318 */
1319
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001320 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 /*
1322 * First, let's catch bad requests.
1323 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001324 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001326
1327 /* 1: Since we are in header mode, if there's no space
1328 * left for headers, we won't be able to free more
1329 * later, so the session will never terminate. We
1330 * must terminate it now.
1331 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 if (unlikely(req->l >= req->rlim - req->data)) {
1333 /* FIXME: check if URI is set and return Status
1334 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001335 */
Willy Tarreau06619262006-12-17 08:37:22 +01001336 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001337 }
1338
1339 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1341 /* read error, or last read : give up. */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001342 tv_eternity(&req->rex);
1343 fd_delete(t->cli_fd);
1344 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001345 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346 if (!(t->flags & SN_ERR_MASK))
1347 t->flags |= SN_ERR_CLICL;
1348 if (!(t->flags & SN_FINST_MASK))
1349 t->flags |= SN_FINST_R;
1350 return 1;
1351 }
1352
1353 /* 3: has the read timeout expired ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 else if (unlikely(tv_cmp2_ms(&req->rex, &now) <= 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001356 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001357 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001358 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001359 if (!(t->flags & SN_ERR_MASK))
1360 t->flags |= SN_ERR_CLITO;
1361 if (!(t->flags & SN_FINST_MASK))
1362 t->flags |= SN_FINST_R;
1363 return 1;
1364 }
1365
1366 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 else if (unlikely(! MY_FD_ISSET(t->cli_fd, StaticReadEvent))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001368 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
1369 * full. We cannot loop here since stream_sock_read will disable it only if
1370 * req->l == rlim-data
1371 */
1372 MY_FD_SET(t->cli_fd, StaticReadEvent);
1373 if (t->fe->clitimeout)
1374 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
1375 else
1376 tv_eternity(&req->rex);
1377 }
1378 return t->cli_state != CL_STHEADERS;
1379 }
1380
1381
1382 /****************************************************************
1383 * More interesting part now : we know that we have a complete *
1384 * request which at least looks like HTTP. We have an indicator *
1385 * of each header's length, so we can parse them quickly. *
1386 ****************************************************************/
1387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 /*
1389 * 1: identify the method
1390 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001391 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001392
1393 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001395 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001397 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 if (unlikely((t->fe->monitor_uri_len != 0) &&
1399 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1400 !memcmp(&req->data[msg->sl.rq.u],
1401 t->fe->monitor_uri,
1402 t->fe->monitor_uri_len))) {
1403 /*
1404 * We have found the monitor URI
1405 */
1406 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001407 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 client_retnclose(t, &http_200_chunk);
1409 goto return_prx_cond;
1410 }
1411
1412 /*
1413 * 3: Maybe we have to copy the original REQURI for the logs ?
1414 * Note: we cannot log anymore if the request has been
1415 * classified as invalid.
1416 */
1417 if (unlikely(t->logs.logwait & LW_REQ)) {
1418 /* we have a complete HTTP request that we must log */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001419 if ((txn->uri = pool_alloc(requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 int urilen = msg->sl.rq.l;
1421
1422 if (urilen >= REQURI_LEN)
1423 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001424 memcpy(txn->uri, &req->data[msg->som], urilen);
1425 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426
1427 if (!(t->logs.logwait &= ~LW_REQ))
1428 sess_log(t);
1429 } else {
1430 Alert("HTTP logging : out of memory.\n");
1431 }
1432 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau06619262006-12-17 08:37:22 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1436 if (unlikely(msg->sl.rq.v_l == 0)) {
1437 int delta;
1438 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001439 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 cur_end = msg->sol + msg->sl.rq.l;
1441 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (msg->sl.rq.u_l == 0) {
1444 /* if no URI was set, add "/" */
1445 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1446 cur_end += delta;
1447 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001448 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 /* add HTTP version */
1450 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1451 msg->eoh += delta;
1452 cur_end += delta;
1453 cur_end = (char *)http_parse_reqline(msg, req->data,
1454 HTTP_MSG_RQMETH,
1455 msg->sol, cur_end + 1,
1456 NULL, NULL);
1457 if (unlikely(!cur_end))
1458 goto return_bad_req;
1459
1460 /* we have a full HTTP/1.0 request now and we know that
1461 * we have either a CR or an LF at <ptr>.
1462 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001463 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464 }
1465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466
1467 /* 5: we may need to capture headers */
Willy Tarreau117f59e2007-03-04 18:17:17 +01001468 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->fiprm->req_cap))
1469 capture_headers(req->data + msg->som, &txn->hdr_idx,
1470 txn->req.cap, t->fe->fiprm->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001471
1472 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001474 * As opposed to version 1.2, now they will be evaluated in the
1475 * filters order and not in the header order. This means that
1476 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001477 *
1478 * We can now check whether we want to switch to another
1479 * backend, in which case we will re-check the backend's
1480 * filters and various options. In order to support 3-level
1481 * switching, here's how we should proceed :
1482 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001483 * a) run be->fiprm.
1484 * if (switch) then switch ->be to the new backend.
1485 * b) run be->fiprm if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001486 * There cannot be any switch from there, so ->be cannot be
1487 * changed anymore.
1488 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001489 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001490 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001491 * The response path will be able to apply either ->be, or
1492 * ->be then ->fe filters in order to match the reverse of
1493 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001494 */
1495
Willy Tarreau06619262006-12-17 08:37:22 +01001496 do {
Willy Tarreau830ff452006-12-17 19:31:23 +01001497 struct proxy *rule_set = t->be->fiprm;
1498 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001499
Willy Tarreau06619262006-12-17 08:37:22 +01001500 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001501 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1503 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001504 }
1505
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001506 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1507 /* to ensure correct connection accounting on
1508 * the backend, we count the connection for the
1509 * one managing the queue.
1510 */
1511 t->be->beprm->beconn++;
1512 if (t->be->beprm->beconn > t->be->beprm->beconn_max)
1513 t->be->beprm->beconn_max = t->be->beprm->beconn;
1514 t->be->beprm->cum_beconn++;
1515 t->flags |= SN_BE_ASSIGNED;
1516 }
1517
Willy Tarreau06619262006-12-17 08:37:22 +01001518 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001519 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001520 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001521 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001522 /* let's log the request time */
1523 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001524 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001525 goto return_prx_cond;
1526 }
1527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 /* We might have to check for "Connection:" */
1529 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
1530 !(t->flags & SN_CONN_CLOSED)) {
1531 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001532 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001534
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001535 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 old_idx = 0;
1537
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001538 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1539 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 cur_ptr = cur_next;
1541 cur_end = cur_ptr + cur_hdr->len;
1542 cur_next = cur_end + cur_hdr->cr + 1;
1543
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001544 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1545 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 /* 3 possibilities :
1547 * - we have already set Connection: close,
1548 * so we remove this line.
1549 * - we have not yet set Connection: close,
1550 * but this line indicates close. We leave
1551 * it untouched and set the flag.
1552 * - we have not yet set Connection: close,
1553 * and this line indicates non-close. We
1554 * replace it.
1555 */
1556 if (t->flags & SN_CONN_CLOSED) {
1557 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001558 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001560 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1561 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 cur_hdr->len = 0;
1563 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001564 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1565 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1566 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 cur_next += delta;
1568 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001569 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 }
1571 t->flags |= SN_CONN_CLOSED;
1572 }
1573 }
1574 old_idx = cur_idx;
1575 }
1576
1577 /* add request headers from the rule sets in the same order */
1578 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001579 if (unlikely(http_header_add_tail(req,
1580 &txn->req,
1581 &txn->hdr_idx,
1582 rule_set->req_add[cur_idx])) < 0)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 goto return_bad_req;
1584 }
Willy Tarreau06619262006-12-17 08:37:22 +01001585 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001588 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001589 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001590 /* we have to check the URI and auth for this request */
1591 if (stats_check_uri_auth(t, rule_set))
1592 return 1;
1593 }
1594
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001595 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1596 /* No backend was set, but there was a default
1597 * backend set in the frontend, so we use it and
1598 * loop again.
1599 */
1600 t->be = cur_proxy->defbe.be;
1601 t->be->beprm->beconn++;
1602 if (t->be->beprm->beconn > t->be->beprm->beconn_max)
1603 t->be->beprm->beconn_max = t->be->beprm->beconn;
1604 t->be->beprm->cum_beconn++;
1605 t->flags |= SN_BE_ASSIGNED;
1606 }
1607 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001608
Willy Tarreau58f10d72006-12-04 02:26:12 +01001609
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001610 if (!(t->flags & SN_BE_ASSIGNED)) {
1611 /* To ensure correct connection accounting on
1612 * the backend, we count the connection for the
1613 * one managing the queue.
1614 */
1615 t->be->beprm->beconn++;
1616 if (t->be->beprm->beconn > t->be->beprm->beconn_max)
1617 t->be->beprm->beconn_max = t->be->beprm->beconn;
1618 t->be->beprm->cum_beconn++;
1619 t->flags |= SN_BE_ASSIGNED;
1620 }
1621
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001622 /*
1623 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001624 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001625 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001626 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001627 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001628
Willy Tarreau58f10d72006-12-04 02:26:12 +01001629
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631
Willy Tarreau2a324282006-12-05 00:05:46 +01001632 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001634 * so let's do the same now.
1635 */
1636
1637 /* It needs to look into the URI */
Willy Tarreau830ff452006-12-17 19:31:23 +01001638 if (t->be->beprm->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001639 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001640 }
1641
1642
1643 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001644 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001645 * Note that doing so might move headers in the request, but
1646 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001647 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001648 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001649 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001650 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001651
Willy Tarreau58f10d72006-12-04 02:26:12 +01001652
Willy Tarreau2a324282006-12-05 00:05:46 +01001653 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001654 * 9: add X-Forwarded-For if either the frontend or the backend
1655 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001656 */
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001657 if ((t->fe->options | t->be->beprm->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001658 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001659 /* Add an X-Forwarded-For header unless the source IP is
1660 * in the 'except' network range.
1661 */
1662 if ((!t->fe->except_mask.s_addr ||
1663 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1664 != t->fe->except_net.s_addr) &&
1665 (!t->be->except_mask.s_addr ||
1666 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1667 != t->be->except_net.s_addr)) {
1668 int len;
1669 unsigned char *pn;
1670 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001671
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001672 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1673 pn[0], pn[1], pn[2], pn[3]);
1674
1675 if (unlikely(http_header_add_tail2(req, &txn->req,
1676 &txn->hdr_idx, trash, len)) < 0)
1677 goto return_bad_req;
1678 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001679 }
1680 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001681 /* FIXME: for the sake of completeness, we should also support
1682 * 'except' here, although it is mostly useless in this case.
1683 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001684 int len;
1685 char pn[INET6_ADDRSTRLEN];
1686 inet_ntop(AF_INET6,
1687 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1688 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001689 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1690 if (unlikely(http_header_add_tail2(req, &txn->req,
1691 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001692 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001693 }
1694 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001695
Willy Tarreau2a324282006-12-05 00:05:46 +01001696 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreaub2513902006-12-17 14:52:38 +01001698 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
1700 !(t->flags & SN_CONN_CLOSED)) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001701 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
1702 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001703 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001704 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001705 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001706
Willy Tarreau2a324282006-12-05 00:05:46 +01001707 /*************************************************************
1708 * OK, that's finished for the headers. We have done what we *
1709 * could. Let's switch to the DATA state. *
1710 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001711
Willy Tarreau2a324282006-12-05 00:05:46 +01001712 t->cli_state = CL_STDATA;
1713 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001714
Willy Tarreau2a324282006-12-05 00:05:46 +01001715 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001716
Willy Tarreau2a324282006-12-05 00:05:46 +01001717 if (!t->fe->clitimeout ||
Willy Tarreau830ff452006-12-17 19:31:23 +01001718 (t->srv_state < SV_STDATA && t->be->beprm->srvtimeout)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001719 /* If the client has no timeout, or if the server is not ready yet,
1720 * and we know for sure that it can expire, then it's cleaner to
1721 * disable the timeout on the client side so that too low values
1722 * cannot make the sessions abort too early.
1723 *
1724 * FIXME-20050705: the server needs a way to re-enable this time-out
1725 * when it switches its state, otherwise a client can stay connected
1726 * indefinitely. This now seems to be OK.
1727 */
1728 tv_eternity(&req->rex);
1729 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001730
Willy Tarreau2a324282006-12-05 00:05:46 +01001731 /* When a connection is tarpitted, we use the queue timeout for the
1732 * tarpit delay, which currently happens to be the server's connect
1733 * timeout. If unset, then set it to zero because we really want it
1734 * to expire at one moment.
1735 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001736 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001737 t->req->l = 0;
1738 /* flush the request so that we can drop the connection early
1739 * if the client closes first.
1740 */
1741 tv_delayfrom(&req->cex, &now,
Willy Tarreau830ff452006-12-17 19:31:23 +01001742 t->be->beprm->contimeout ? t->be->beprm->contimeout : 0);
Willy Tarreau2a324282006-12-05 00:05:46 +01001743 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01001746 goto process_data;
1747
1748 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001749 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001750 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01001751 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001752 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01001753 return_prx_cond:
1754 if (!(t->flags & SN_ERR_MASK))
1755 t->flags |= SN_ERR_PRXCOND;
1756 if (!(t->flags & SN_FINST_MASK))
1757 t->flags |= SN_FINST_R;
1758 return 1;
1759
Willy Tarreaubaaee002006-06-26 02:48:02 +02001760 }
1761 else if (c == CL_STDATA) {
1762 process_data:
1763 /* FIXME: this error handling is partly buggy because we always report
1764 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1765 * or HEADER phase. BTW, it's not logical to expire the client while
1766 * we're waiting for the server to connect.
1767 */
1768 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001769 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001770 tv_eternity(&req->rex);
1771 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001772 fd_delete(t->cli_fd);
1773 t->cli_state = CL_STCLOSE;
1774 if (!(t->flags & SN_ERR_MASK))
1775 t->flags |= SN_ERR_CLICL;
1776 if (!(t->flags & SN_FINST_MASK)) {
1777 if (t->pend_pos)
1778 t->flags |= SN_FINST_Q;
1779 else if (s == SV_STCONN)
1780 t->flags |= SN_FINST_C;
1781 else
1782 t->flags |= SN_FINST_D;
1783 }
1784 return 1;
1785 }
1786 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001787 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001788 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001789 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001790 shutdown(t->cli_fd, SHUT_RD);
1791 t->cli_state = CL_STSHUTR;
1792 return 1;
1793 }
1794 /* last server read and buffer empty */
1795 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001796 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001797 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001798 shutdown(t->cli_fd, SHUT_WR);
1799 /* We must ensure that the read part is still alive when switching
1800 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001801 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001802 if (t->fe->clitimeout)
1803 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001804 t->cli_state = CL_STSHUTW;
1805 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1806 return 1;
1807 }
1808 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001809 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001810 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001811 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001812 shutdown(t->cli_fd, SHUT_RD);
1813 t->cli_state = CL_STSHUTR;
1814 if (!(t->flags & SN_ERR_MASK))
1815 t->flags |= SN_ERR_CLITO;
1816 if (!(t->flags & SN_FINST_MASK)) {
1817 if (t->pend_pos)
1818 t->flags |= SN_FINST_Q;
1819 else if (s == SV_STCONN)
1820 t->flags |= SN_FINST_C;
1821 else
1822 t->flags |= SN_FINST_D;
1823 }
1824 return 1;
1825 }
1826 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001827 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001828 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001829 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001830 shutdown(t->cli_fd, SHUT_WR);
1831 /* We must ensure that the read part is still alive when switching
1832 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001833 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001834 if (t->fe->clitimeout)
1835 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001836
1837 t->cli_state = CL_STSHUTW;
1838 if (!(t->flags & SN_ERR_MASK))
1839 t->flags |= SN_ERR_CLITO;
1840 if (!(t->flags & SN_FINST_MASK)) {
1841 if (t->pend_pos)
1842 t->flags |= SN_FINST_Q;
1843 else if (s == SV_STCONN)
1844 t->flags |= SN_FINST_C;
1845 else
1846 t->flags |= SN_FINST_D;
1847 }
1848 return 1;
1849 }
1850
1851 if (req->l >= req->rlim - req->data) {
1852 /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02001853 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001854 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02001855 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001856 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001857 }
1858 } else {
1859 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02001860 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
1861 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001862 if (!t->fe->clitimeout ||
Willy Tarreau830ff452006-12-17 19:31:23 +01001863 (t->srv_state < SV_STDATA && t->be->beprm->srvtimeout))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001864 /* If the client has no timeout, or if the server not ready yet, and we
1865 * know for sure that it can expire, then it's cleaner to disable the
1866 * timeout on the client side so that too low values cannot make the
1867 * sessions abort too early.
1868 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001869 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001870 else
Willy Tarreau73de9892006-11-30 11:40:23 +01001871 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001872 }
1873 }
1874
1875 if ((rep->l == 0) ||
1876 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001877 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1878 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001879 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001880 }
1881 } else {
1882 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001883 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1884 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001885 if (t->fe->clitimeout) {
1886 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001887 /* FIXME: to prevent the client from expiring read timeouts during writes,
1888 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001889 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001890 }
1891 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001892 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001893 }
1894 }
1895 return 0; /* other cases change nothing */
1896 }
1897 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001898 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001899 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001900 fd_delete(t->cli_fd);
1901 t->cli_state = CL_STCLOSE;
1902 if (!(t->flags & SN_ERR_MASK))
1903 t->flags |= SN_ERR_CLICL;
1904 if (!(t->flags & SN_FINST_MASK)) {
1905 if (t->pend_pos)
1906 t->flags |= SN_FINST_Q;
1907 else if (s == SV_STCONN)
1908 t->flags |= SN_FINST_C;
1909 else
1910 t->flags |= SN_FINST_D;
1911 }
1912 return 1;
1913 }
1914 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
1915 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001916 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001917 fd_delete(t->cli_fd);
1918 t->cli_state = CL_STCLOSE;
1919 return 1;
1920 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001921 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
1922 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001923 fd_delete(t->cli_fd);
1924 t->cli_state = CL_STCLOSE;
1925 if (!(t->flags & SN_ERR_MASK))
1926 t->flags |= SN_ERR_CLITO;
1927 if (!(t->flags & SN_FINST_MASK)) {
1928 if (t->pend_pos)
1929 t->flags |= SN_FINST_Q;
1930 else if (s == SV_STCONN)
1931 t->flags |= SN_FINST_C;
1932 else
1933 t->flags |= SN_FINST_D;
1934 }
1935 return 1;
1936 }
1937
1938 if (t->flags & SN_SELF_GEN) {
1939 produce_content(t);
1940 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001941 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001942 fd_delete(t->cli_fd);
1943 t->cli_state = CL_STCLOSE;
1944 return 1;
1945 }
1946 }
1947
1948 if ((rep->l == 0)
1949 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001950 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1951 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001952 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001953 }
1954 } else {
1955 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001956 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1957 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001958 if (t->fe->clitimeout) {
1959 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001960 /* FIXME: to prevent the client from expiring read timeouts during writes,
1961 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001962 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001963 }
1964 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001965 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001966 }
1967 }
1968 return 0;
1969 }
1970 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001971 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001972 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001973 fd_delete(t->cli_fd);
1974 t->cli_state = CL_STCLOSE;
1975 if (!(t->flags & SN_ERR_MASK))
1976 t->flags |= SN_ERR_CLICL;
1977 if (!(t->flags & SN_FINST_MASK)) {
1978 if (t->pend_pos)
1979 t->flags |= SN_FINST_Q;
1980 else if (s == SV_STCONN)
1981 t->flags |= SN_FINST_C;
1982 else
1983 t->flags |= SN_FINST_D;
1984 }
1985 return 1;
1986 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001987 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001988 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001989 fd_delete(t->cli_fd);
1990 t->cli_state = CL_STCLOSE;
1991 return 1;
1992 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001993 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
1994 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001995 fd_delete(t->cli_fd);
1996 t->cli_state = CL_STCLOSE;
1997 if (!(t->flags & SN_ERR_MASK))
1998 t->flags |= SN_ERR_CLITO;
1999 if (!(t->flags & SN_FINST_MASK)) {
2000 if (t->pend_pos)
2001 t->flags |= SN_FINST_Q;
2002 else if (s == SV_STCONN)
2003 t->flags |= SN_FINST_C;
2004 else
2005 t->flags |= SN_FINST_D;
2006 }
2007 return 1;
2008 }
2009 else if (req->l >= req->rlim - req->data) {
2010 /* no room to read more data */
2011
2012 /* FIXME-20050705: is it possible for a client to maintain a session
2013 * after the timeout by sending more data after it receives a close ?
2014 */
2015
Willy Tarreau2a429502006-10-15 14:52:29 +02002016 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002017 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02002018 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002019 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002020 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2021 }
2022 } else {
2023 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02002024 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
2025 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002026 if (t->fe->clitimeout)
2027 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002028 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002029 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002030 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2031 }
2032 }
2033 return 0;
2034 }
2035 else { /* CL_STCLOSE: nothing to do */
2036 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2037 int len;
Willy Tarreau830ff452006-12-17 19:31:23 +01002038 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->beprm->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002039 write(1, trash, len);
2040 }
2041 return 0;
2042 }
2043 return 0;
2044}
2045
2046
2047/*
2048 * manages the server FSM and its socket. It returns 1 if a state has changed
2049 * (and a resync may be needed), 0 else.
2050 */
2051int process_srv(struct session *t)
2052{
2053 int s = t->srv_state;
2054 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002055 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002056 struct buffer *req = t->req;
2057 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002058 int conn_err;
2059
2060#ifdef DEBUG_FULL
2061 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2062#endif
2063 //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 +02002064 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
2065 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002066 //);
2067 if (s == SV_STIDLE) {
2068 if (c == CL_STHEADERS)
2069 return 0; /* stay in idle, waiting for data to reach the client side */
2070 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2071 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002072 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002073 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002074 if (t->pend_pos)
2075 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2076 /* note that this must not return any error because it would be able to
2077 * overwrite the client_retnclose() output.
2078 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002079 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002080 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002081 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002082 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002083
2084 return 1;
2085 }
2086 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002087 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002088 /* This connection is being tarpitted. The CLIENT side has
2089 * already set the connect expiration date to the right
2090 * timeout. We just have to check that it has not expired.
2091 */
2092 if (tv_cmp2_ms(&req->cex, &now) > 0)
2093 return 0;
2094
2095 /* We will set the queue timer to the time spent, just for
2096 * logging purposes. We fake a 500 server error, so that the
2097 * attacker will not suspect his connection has been tarpitted.
2098 * It will not cause trouble to the logs because we can exclude
2099 * the tarpitted connections by filtering on the 'PT' status flags.
2100 */
2101 tv_eternity(&req->cex);
2102 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2103 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002104 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002105 return 1;
2106 }
2107
Willy Tarreaubaaee002006-06-26 02:48:02 +02002108 /* Right now, we will need to create a connection to the server.
2109 * We might already have tried, and got a connection pending, in
2110 * which case we will not do anything till it's pending. It's up
2111 * to any other session to release it and wake us up again.
2112 */
2113 if (t->pend_pos) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002114 if (tv_cmp2_ms(&req->cex, &now) > 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002115 return 0;
2116 else {
2117 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002118 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002119 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2120 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002121 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002122 if (t->srv)
2123 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002124 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002125 return 1;
2126 }
2127 }
2128
2129 do {
2130 /* first, get a connection */
2131 if (srv_redispatch_connect(t))
2132 return t->srv_state != SV_STIDLE;
2133
2134 /* try to (re-)connect to the server, and fail if we expire the
2135 * number of retries.
2136 */
2137 if (srv_retryable_connect(t)) {
2138 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2139 return t->srv_state != SV_STIDLE;
2140 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002141 } while (1);
2142 }
2143 }
2144 else if (s == SV_STCONN) { /* connection in progress */
2145 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2146 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002147 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002148 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002149 fd_delete(t->srv_fd);
2150 if (t->srv)
2151 t->srv->cur_sess--;
2152
2153 /* note that this must not return any error because it would be able to
2154 * overwrite the client_retnclose() output.
2155 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002156 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002157 return 1;
2158 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002159 if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) {
2160 //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 +02002161 return 0; /* nothing changed */
2162 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002163 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002164 /* timeout, asynchronous connect error or first write error */
2165 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2166
2167 fd_delete(t->srv_fd);
2168 if (t->srv)
2169 t->srv->cur_sess--;
2170
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002171 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002172 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2173 else
2174 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2175
2176 /* ensure that we have enough retries left */
2177 if (srv_count_retry_down(t, conn_err))
2178 return 1;
2179
Willy Tarreau830ff452006-12-17 19:31:23 +01002180 if (t->srv && t->conn_retries == 0 && t->be->beprm->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002181 /* We're on our last chance, and the REDISP option was specified.
2182 * We will ignore cookie and force to balance or use the dispatcher.
2183 */
2184 /* let's try to offer this slot to anybody */
Willy Tarreau830ff452006-12-17 19:31:23 +01002185 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002186 task_wakeup(&rq, t->srv->queue_mgt);
2187
2188 if (t->srv)
2189 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +01002190 t->be->beprm->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002191
2192 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2193 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002194 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002195
2196 /* first, get a connection */
2197 if (srv_redispatch_connect(t))
2198 return t->srv_state != SV_STIDLE;
2199 }
2200
Willy Tarreaubaaee002006-06-26 02:48:02 +02002201 do {
2202 /* Now we will try to either reconnect to the same server or
2203 * connect to another server. If the connection gets queued
2204 * because all servers are saturated, then we will go back to
2205 * the SV_STIDLE state.
2206 */
2207 if (srv_retryable_connect(t)) {
2208 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2209 return t->srv_state != SV_STCONN;
2210 }
2211
2212 /* we need to redispatch the connection to another server */
2213 if (srv_redispatch_connect(t))
2214 return t->srv_state != SV_STCONN;
2215 } while (1);
2216 }
2217 else { /* no error or write 0 */
2218 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
2219
2220 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2221 if (req->l == 0) /* nothing to write */ {
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 } else /* need the right to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002225 MY_FD_SET(t->srv_fd, StaticWriteEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002226 if (t->be->beprm->srvtimeout) {
2227 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228 /* FIXME: to prevent the server from expiring read timeouts during writes,
2229 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002230 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002231 }
2232 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002233 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002234 }
2235
Willy Tarreau830ff452006-12-17 19:31:23 +01002236 if (t->be->beprm->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau2a429502006-10-15 14:52:29 +02002237 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002238 if (t->be->beprm->srvtimeout)
2239 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002240 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002241 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002242
2243 t->srv_state = SV_STDATA;
2244 if (t->srv)
2245 t->srv->cum_sess++;
2246 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2247
2248 /* if the user wants to log as soon as possible, without counting
2249 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002250 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002251 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
2252 sess_log(t);
2253 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002254#ifdef CONFIG_HAP_TCPSPLICE
2255 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2256 /* TCP splicing supported by both FE and BE */
2257 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2258 }
2259#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002260 }
2261 else {
2262 t->srv_state = SV_STHEADERS;
2263 if (t->srv)
2264 t->srv->cum_sess++;
2265 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002266 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2267 /* reset hdr_idx which was already initialized by the request.
2268 * right now, the http parser does it.
2269 * hdr_idx_init(&t->txn.hdr_idx);
2270 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002271 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002272 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002273 return 1;
2274 }
2275 }
2276 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002277 /*
2278 * Now parse the partial (or complete) lines.
2279 * We will check the response syntax, and also join multi-line
2280 * headers. An index of all the lines will be elaborated while
2281 * parsing.
2282 *
2283 * For the parsing, we use a 28 states FSM.
2284 *
2285 * Here is the information we currently have :
2286 * rep->data + req->som = beginning of response
2287 * rep->data + req->eoh = end of processed headers / start of current one
2288 * rep->data + req->eol = end of current header or line (LF or CRLF)
2289 * rep->lr = first non-visited byte
2290 * rep->r = end of data
2291 */
2292
2293 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002294 struct http_msg *msg = &txn->rsp;
2295 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002296
Willy Tarreaua15645d2007-03-18 16:22:39 +01002297 if (likely(rep->lr < rep->r))
2298 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002299
Willy Tarreaua15645d2007-03-18 16:22:39 +01002300 /* 1: we might have to print this header in debug mode */
2301 if (unlikely((global.mode & MODE_DEBUG) &&
2302 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2303 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2304 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002305
Willy Tarreaua15645d2007-03-18 16:22:39 +01002306 sol = rep->data + msg->som;
2307 eol = sol + msg->sl.rq.l;
2308 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002309
Willy Tarreaua15645d2007-03-18 16:22:39 +01002310 sol += hdr_idx_first_pos(&txn->hdr_idx);
2311 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002312
Willy Tarreaua15645d2007-03-18 16:22:39 +01002313 while (cur_idx) {
2314 eol = sol + txn->hdr_idx.v[cur_idx].len;
2315 debug_hdr("srvhdr", t, sol, eol);
2316 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2317 cur_idx = txn->hdr_idx.v[cur_idx].next;
2318 }
2319 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002320
Willy Tarreaubaaee002006-06-26 02:48:02 +02002321
Willy Tarreaua15645d2007-03-18 16:22:39 +01002322 if ((rep->l < rep->rlim - rep->data) && ! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2323 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
2324 * full. We cannot loop here since stream_sock_read will disable it only if
2325 * rep->l == rlim-data
2326 */
2327 MY_FD_SET(t->srv_fd, StaticReadEvent);
2328 if (t->be->beprm->srvtimeout)
2329 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2330 else
2331 tv_eternity(&rep->rex);
2332 }
2333
2334
2335 /*
2336 * Now we quickly check if we have found a full valid response.
2337 * If not so, we check the FD and buffer states before leaving.
2338 * A full response is indicated by the fact that we have seen
2339 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2340 * responses are checked first.
2341 *
2342 * Depending on whether the client is still there or not, we
2343 * may send an error response back or not. Note that normally
2344 * we should only check for HTTP status there, and check I/O
2345 * errors somewhere else.
2346 */
2347
2348 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2349
2350 /* Invalid response, or read error or write error */
2351 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2352 (req->flags & BF_WRITE_ERROR) ||
2353 (rep->flags & BF_READ_ERROR))) {
2354 tv_eternity(&rep->rex);
2355 tv_eternity(&req->wex);
2356 fd_delete(t->srv_fd);
2357 if (t->srv) {
2358 t->srv->cur_sess--;
2359 t->srv->failed_resp++;
2360 }
2361 t->be->failed_resp++;
2362 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002363 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002364 client_return(t, error_message(t, HTTP_ERR_502));
2365 if (!(t->flags & SN_ERR_MASK))
2366 t->flags |= SN_ERR_SRVCL;
2367 if (!(t->flags & SN_FINST_MASK))
2368 t->flags |= SN_FINST_H;
2369 /* We used to have a free connection slot. Since we'll never use it,
2370 * we have to inform the server that it may be used by another session.
2371 */
2372 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2373 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002374
Willy Tarreaua15645d2007-03-18 16:22:39 +01002375 return 1;
2376 }
2377
2378 /* end of client write or end of server read.
2379 * since we are in header mode, if there's no space left for headers, we
2380 * won't be able to free more later, so the session will never terminate.
2381 */
2382 else if (unlikely(rep->flags & BF_READ_NULL ||
2383 c == CL_STSHUTW || c == CL_STCLOSE ||
2384 rep->l >= rep->rlim - rep->data)) {
2385 MY_FD_CLR(t->srv_fd, StaticReadEvent);
2386 tv_eternity(&rep->rex);
2387 shutdown(t->srv_fd, SHUT_RD);
2388 t->srv_state = SV_STSHUTR;
2389 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2390 return 1;
2391 }
2392
2393 /* read timeout : return a 504 to the client.
2394 */
2395 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticReadEvent) &&
2396 tv_cmp2_ms(&rep->rex, &now) <= 0)) {
2397 tv_eternity(&rep->rex);
2398 tv_eternity(&req->wex);
2399 fd_delete(t->srv_fd);
2400 if (t->srv) {
2401 t->srv->cur_sess--;
2402 t->srv->failed_resp++;
2403 }
2404 t->be->failed_resp++;
2405 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002406 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002407 client_return(t, error_message(t, HTTP_ERR_504));
2408 if (!(t->flags & SN_ERR_MASK))
2409 t->flags |= SN_ERR_SRVTO;
2410 if (!(t->flags & SN_FINST_MASK))
2411 t->flags |= SN_FINST_H;
2412 /* We used to have a free connection slot. Since we'll never use it,
2413 * we have to inform the server that it may be used by another session.
2414 */
2415 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2416 task_wakeup(&rq, t->srv->queue_mgt);
2417 return 1;
2418 }
2419
2420 /* last client read and buffer empty */
2421 /* FIXME!!! here, we don't want to switch to SHUTW if the
2422 * client shuts read too early, because we may still have
2423 * some work to do on the headers.
2424 * The side-effect is that if the client completely closes its
2425 * connection during SV_STHEADER, the connection to the server
2426 * is kept until a response comes back or the timeout is reached.
2427 */
2428 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2429 (req->l == 0))) {
2430 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2431 tv_eternity(&req->wex);
2432
2433 /* We must ensure that the read part is still
2434 * alive when switching to shutw */
2435 MY_FD_SET(t->srv_fd, StaticReadEvent);
2436 if (t->be->beprm->srvtimeout)
2437 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2438
2439 shutdown(t->srv_fd, SHUT_WR);
2440 t->srv_state = SV_STSHUTW;
2441 return 1;
2442 }
2443
2444 /* write timeout */
2445 /* FIXME!!! here, we don't want to switch to SHUTW if the
2446 * client shuts read too early, because we may still have
2447 * some work to do on the headers.
2448 */
2449 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticWriteEvent) &&
2450 tv_cmp2_ms(&req->wex, &now) <= 0)) {
2451 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2452 tv_eternity(&req->wex);
2453 shutdown(t->srv_fd, SHUT_WR);
2454 /* We must ensure that the read part is still alive
2455 * when switching to shutw */
2456 MY_FD_SET(t->srv_fd, StaticReadEvent);
2457 if (t->be->beprm->srvtimeout)
2458 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2459
2460 t->srv_state = SV_STSHUTW;
2461 if (!(t->flags & SN_ERR_MASK))
2462 t->flags |= SN_ERR_SRVTO;
2463 if (!(t->flags & SN_FINST_MASK))
2464 t->flags |= SN_FINST_H;
2465 return 1;
2466 }
2467
2468 /*
2469 * And now the non-error cases.
2470 */
2471
2472 /* Data remaining in the request buffer.
2473 * This happens during the first pass here, and during
2474 * long posts.
2475 */
2476 else if (likely(req->l)) {
2477 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2478 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2479 if (t->be->beprm->srvtimeout) {
2480 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
2481 /* FIXME: to prevent the server from expiring read timeouts during writes,
2482 * we refresh it. */
2483 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002484 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002485 else
2486 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002487 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002488 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002489
Willy Tarreaua15645d2007-03-18 16:22:39 +01002490 /* nothing left in the request buffer */
2491 else {
2492 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2493 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002494 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002495 }
2496 }
2497
2498 return t->srv_state != SV_STHEADERS;
2499 }
2500
2501
2502 /*****************************************************************
2503 * More interesting part now : we know that we have a complete *
2504 * response which at least looks like HTTP. We have an indicator *
2505 * of each header's length, so we can parse them quickly. *
2506 ****************************************************************/
2507
2508 /*
2509 * 1: get the status code and check for cacheability.
2510 */
2511
2512 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002513 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002514
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002515 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002516 case 200:
2517 case 203:
2518 case 206:
2519 case 300:
2520 case 301:
2521 case 410:
2522 /* RFC2616 @13.4:
2523 * "A response received with a status code of
2524 * 200, 203, 206, 300, 301 or 410 MAY be stored
2525 * by a cache (...) unless a cache-control
2526 * directive prohibits caching."
2527 *
2528 * RFC2616 @9.5: POST method :
2529 * "Responses to this method are not cacheable,
2530 * unless the response includes appropriate
2531 * Cache-Control or Expires header fields."
2532 */
2533 if (likely(txn->meth != HTTP_METH_POST) &&
2534 unlikely(t->be->beprm->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002535 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002536 break;
2537 default:
2538 break;
2539 }
2540
2541 /*
2542 * 2: we may need to capture headers
2543 */
2544 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->fiprm->rsp_cap))
2545 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2546 txn->rsp.cap, t->fe->fiprm->rsp_cap);
2547
2548 /*
2549 * 3: we will have to evaluate the filters.
2550 * As opposed to version 1.2, now they will be evaluated in the
2551 * filters order and not in the header order. This means that
2552 * each filter has to be validated among all headers.
2553 *
2554 * Filters are tried with ->be first, then with ->fe if it is
2555 * different from ->be.
2556 */
2557
2558 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2559
2560 cur_proxy = t->be;
2561 while (1) {
2562 struct proxy *rule_set = cur_proxy->fiprm;
2563
2564 /* try headers filters */
2565 if (rule_set->rsp_exp != NULL) {
2566 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2567 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002568 if (t->srv) {
2569 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002570 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002571 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002572 cur_proxy->beprm->failed_resp++;
2573 return_srv_prx_502:
2574 tv_eternity(&rep->rex);
2575 tv_eternity(&req->wex);
2576 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002577 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002578 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002579 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002580 if (!(t->flags & SN_ERR_MASK))
2581 t->flags |= SN_ERR_PRXCOND;
2582 if (!(t->flags & SN_FINST_MASK))
2583 t->flags |= SN_FINST_H;
2584 /* We used to have a free connection slot. Since we'll never use it,
2585 * we have to inform the server that it may be used by another session.
2586 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002587 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002588 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002589 return 1;
2590 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002591 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002592
Willy Tarreaua15645d2007-03-18 16:22:39 +01002593 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002594 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002595 if (t->srv) {
2596 t->srv->cur_sess--;
2597 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002598 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002599 cur_proxy->beprm->denied_resp++;
2600 goto return_srv_prx_502;
2601 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002602
Willy Tarreaua15645d2007-03-18 16:22:39 +01002603 /* We might have to check for "Connection:" */
2604 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
2605 !(t->flags & SN_CONN_CLOSED)) {
2606 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002607 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002608 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002609
Willy Tarreaua15645d2007-03-18 16:22:39 +01002610 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2611 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002612
Willy Tarreaua15645d2007-03-18 16:22:39 +01002613 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2614 cur_hdr = &txn->hdr_idx.v[cur_idx];
2615 cur_ptr = cur_next;
2616 cur_end = cur_ptr + cur_hdr->len;
2617 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002618
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002619 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2620 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002621 /* 3 possibilities :
2622 * - we have already set Connection: close,
2623 * so we remove this line.
2624 * - we have not yet set Connection: close,
2625 * but this line indicates close. We leave
2626 * it untouched and set the flag.
2627 * - we have not yet set Connection: close,
2628 * and this line indicates non-close. We
2629 * replace it.
2630 */
2631 if (t->flags & SN_CONN_CLOSED) {
2632 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2633 txn->rsp.eoh += delta;
2634 cur_next += delta;
2635 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2636 txn->hdr_idx.used--;
2637 cur_hdr->len = 0;
2638 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002639 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2640 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2641 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002642 cur_next += delta;
2643 cur_hdr->len += delta;
2644 txn->rsp.eoh += delta;
2645 }
2646 t->flags |= SN_CONN_CLOSED;
2647 }
2648 }
2649 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002650 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002651 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002652
Willy Tarreaua15645d2007-03-18 16:22:39 +01002653 /* add response headers from the rule sets in the same order */
2654 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002655 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2656 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002657 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002658 }
2659
Willy Tarreaua15645d2007-03-18 16:22:39 +01002660 /* check whether we're already working on the frontend */
2661 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002662 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002663 cur_proxy = t->fe;
2664 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002665
Willy Tarreaua15645d2007-03-18 16:22:39 +01002666 /*
2667 * 4: check for server cookie.
2668 */
2669 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002670
Willy Tarreaua15645d2007-03-18 16:22:39 +01002671 /*
2672 * 5: add server cookie in the response if needed
2673 */
2674 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_INS) &&
2675 (!(t->be->beprm->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2676 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002677
Willy Tarreaua15645d2007-03-18 16:22:39 +01002678 /* the server is known, it's not the one the client requested, we have to
2679 * insert a set-cookie here, except if we want to insert only on POST
2680 * requests and this one isn't. Note that servers which don't have cookies
2681 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002682 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002683 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaua15645d2007-03-18 16:22:39 +01002684 t->be->beprm->cookie_name,
2685 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002686
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002687 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2688 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002689 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002690 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002691
Willy Tarreaua15645d2007-03-18 16:22:39 +01002692 /* Here, we will tell an eventual cache on the client side that we don't
2693 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2694 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2695 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2696 */
2697 if (t->be->beprm->options & PR_O_COOK_NOC) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002698 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2699 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002700 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002701 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002702 }
2703
2704
2705 /*
2706 * 6: check for cache-control or pragma headers.
2707 */
2708 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002709
Willy Tarreaubaaee002006-06-26 02:48:02 +02002710
Willy Tarreaua15645d2007-03-18 16:22:39 +01002711 /*
2712 * 7: check if result will be cacheable with a cookie.
2713 * We'll block the response if security checks have caught
2714 * nasty things such as a cacheable cookie.
2715 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002716 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2717 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002718 (t->be->beprm->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002719
Willy Tarreaua15645d2007-03-18 16:22:39 +01002720 /* we're in presence of a cacheable response containing
2721 * a set-cookie header. We'll block it as requested by
2722 * the 'checkcache' option, and send an alert.
2723 */
2724 if (t->srv) {
2725 t->srv->cur_sess--;
2726 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002727 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002728 t->be->beprm->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002729
Willy Tarreaua15645d2007-03-18 16:22:39 +01002730 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2731 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2732 send_log(t->be, LOG_ALERT,
2733 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2734 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2735 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002736 }
2737
Willy Tarreaua15645d2007-03-18 16:22:39 +01002738 /*
2739 * 8: add "Connection: close" if needed and not yet set.
2740 */
2741 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
2742 !(t->flags & SN_CONN_CLOSED)) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002743 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2744 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002745 goto return_bad_resp;
2746 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002747 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002748
Willy Tarreaubaaee002006-06-26 02:48:02 +02002749
Willy Tarreaua15645d2007-03-18 16:22:39 +01002750 /*************************************************************
2751 * OK, that's finished for the headers. We have done what we *
2752 * could. Let's switch to the DATA state. *
2753 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002754
Willy Tarreaua15645d2007-03-18 16:22:39 +01002755 t->srv_state = SV_STDATA;
2756 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
2757 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
2758
2759 /* client connection already closed or option 'forceclose' required :
2760 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002761 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002762 if ((req->l == 0) &&
2763 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->beprm->options & PR_O_FORCE_CLO)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002764 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002765 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002766
2767 /* We must ensure that the read part is still alive when switching
2768 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002769 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002770 if (t->be->beprm->srvtimeout)
2771 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002772
Willy Tarreaua15645d2007-03-18 16:22:39 +01002773 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002774 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002775 }
2776
Willy Tarreaua15645d2007-03-18 16:22:39 +01002777#ifdef CONFIG_HAP_TCPSPLICE
2778 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2779 /* TCP splicing supported by both FE and BE */
2780 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002781 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002782#endif
2783 /* if the user wants to log as soon as possible, without counting
2784 bytes from the server, then this is the right moment. */
2785 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2786 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002787 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002788 sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002789 }
2790
Willy Tarreaua15645d2007-03-18 16:22:39 +01002791 /* Note: we must not try to cheat by jumping directly to DATA,
2792 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002793 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002794
2795 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002796 }
2797 else if (s == SV_STDATA) {
2798 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002799 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002800 tv_eternity(&rep->rex);
2801 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002802 fd_delete(t->srv_fd);
2803 if (t->srv) {
2804 t->srv->cur_sess--;
2805 t->srv->failed_resp++;
2806 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002807 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002808 t->srv_state = SV_STCLOSE;
2809 if (!(t->flags & SN_ERR_MASK))
2810 t->flags |= SN_ERR_SRVCL;
2811 if (!(t->flags & SN_FINST_MASK))
2812 t->flags |= SN_FINST_D;
2813 /* We used to have a free connection slot. Since we'll never use it,
2814 * we have to inform the server that it may be used by another session.
2815 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002816 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002817 task_wakeup(&rq, t->srv->queue_mgt);
2818
2819 return 1;
2820 }
2821 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002822 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002823 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002824 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002825 shutdown(t->srv_fd, SHUT_RD);
2826 t->srv_state = SV_STSHUTR;
2827 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2828 return 1;
2829 }
2830 /* end of client read and no more data to send */
2831 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002832 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002833 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002834 shutdown(t->srv_fd, SHUT_WR);
2835 /* We must ensure that the read part is still alive when switching
2836 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002837 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002838 if (t->be->beprm->srvtimeout)
2839 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002840
2841 t->srv_state = SV_STSHUTW;
2842 return 1;
2843 }
2844 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002845 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002846 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002847 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002848 shutdown(t->srv_fd, SHUT_RD);
2849 t->srv_state = SV_STSHUTR;
2850 if (!(t->flags & SN_ERR_MASK))
2851 t->flags |= SN_ERR_SRVTO;
2852 if (!(t->flags & SN_FINST_MASK))
2853 t->flags |= SN_FINST_D;
2854 return 1;
2855 }
2856 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002857 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002858 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002859 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002860 shutdown(t->srv_fd, SHUT_WR);
2861 /* We must ensure that the read part is still alive when switching
2862 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002863 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002864 if (t->be->beprm->srvtimeout)
2865 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002866 t->srv_state = SV_STSHUTW;
2867 if (!(t->flags & SN_ERR_MASK))
2868 t->flags |= SN_ERR_SRVTO;
2869 if (!(t->flags & SN_FINST_MASK))
2870 t->flags |= SN_FINST_D;
2871 return 1;
2872 }
2873
2874 /* recompute request time-outs */
2875 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002876 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2877 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002878 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002879 }
2880 }
2881 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau2a429502006-10-15 14:52:29 +02002882 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2883 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002884 if (t->be->beprm->srvtimeout) {
2885 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002886 /* FIXME: to prevent the server from expiring read timeouts during writes,
2887 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002888 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002889 }
2890 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002891 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002892 }
2893 }
2894
2895 /* recompute response time-outs */
2896 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002897 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2898 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002899 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002900 }
2901 }
2902 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02002903 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2904 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002905 if (t->be->beprm->srvtimeout)
2906 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002907 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002908 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002909 }
2910 }
2911
2912 return 0; /* other cases change nothing */
2913 }
2914 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002915 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002916 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002917 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002918 fd_delete(t->srv_fd);
2919 if (t->srv) {
2920 t->srv->cur_sess--;
2921 t->srv->failed_resp++;
2922 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002923 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002924 //close(t->srv_fd);
2925 t->srv_state = SV_STCLOSE;
2926 if (!(t->flags & SN_ERR_MASK))
2927 t->flags |= SN_ERR_SRVCL;
2928 if (!(t->flags & SN_FINST_MASK))
2929 t->flags |= SN_FINST_D;
2930 /* We used to have a free connection slot. Since we'll never use it,
2931 * we have to inform the server that it may be used by another session.
2932 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002933 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002934 task_wakeup(&rq, t->srv->queue_mgt);
2935
2936 return 1;
2937 }
2938 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002939 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002940 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002941 fd_delete(t->srv_fd);
2942 if (t->srv)
2943 t->srv->cur_sess--;
2944 //close(t->srv_fd);
2945 t->srv_state = SV_STCLOSE;
2946 /* We used to have a free connection slot. Since we'll never use it,
2947 * we have to inform the server that it may be used by another session.
2948 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002949 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002950 task_wakeup(&rq, t->srv->queue_mgt);
2951
2952 return 1;
2953 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002954 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002955 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002956 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002957 fd_delete(t->srv_fd);
2958 if (t->srv)
2959 t->srv->cur_sess--;
2960 //close(t->srv_fd);
2961 t->srv_state = SV_STCLOSE;
2962 if (!(t->flags & SN_ERR_MASK))
2963 t->flags |= SN_ERR_SRVTO;
2964 if (!(t->flags & SN_FINST_MASK))
2965 t->flags |= SN_FINST_D;
2966 /* We used to have a free connection slot. Since we'll never use it,
2967 * we have to inform the server that it may be used by another session.
2968 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002969 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002970 task_wakeup(&rq, t->srv->queue_mgt);
2971
2972 return 1;
2973 }
2974 else if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002975 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2976 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002977 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002978 }
2979 }
2980 else { /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002981 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2982 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002983 if (t->be->beprm->srvtimeout) {
2984 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985 /* FIXME: to prevent the server from expiring read timeouts during writes,
2986 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002987 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002988 }
2989 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002990 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002991 }
2992 }
2993 return 0;
2994 }
2995 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002996 if (rep->flags & BF_READ_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002997 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002998 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002999 fd_delete(t->srv_fd);
3000 if (t->srv) {
3001 t->srv->cur_sess--;
3002 t->srv->failed_resp++;
3003 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003004 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003005 //close(t->srv_fd);
3006 t->srv_state = SV_STCLOSE;
3007 if (!(t->flags & SN_ERR_MASK))
3008 t->flags |= SN_ERR_SRVCL;
3009 if (!(t->flags & SN_FINST_MASK))
3010 t->flags |= SN_FINST_D;
3011 /* We used to have a free connection slot. Since we'll never use it,
3012 * we have to inform the server that it may be used by another session.
3013 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003014 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003015 task_wakeup(&rq, t->srv->queue_mgt);
3016
3017 return 1;
3018 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003019 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003020 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003021 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003022 fd_delete(t->srv_fd);
3023 if (t->srv)
3024 t->srv->cur_sess--;
3025 //close(t->srv_fd);
3026 t->srv_state = SV_STCLOSE;
3027 /* We used to have a free connection slot. Since we'll never use it,
3028 * we have to inform the server that it may be used by another session.
3029 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003030 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003031 task_wakeup(&rq, t->srv->queue_mgt);
3032
3033 return 1;
3034 }
Willy Tarreaud7971282006-07-29 18:36:34 +02003035 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003036 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003037 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003038 fd_delete(t->srv_fd);
3039 if (t->srv)
3040 t->srv->cur_sess--;
3041 //close(t->srv_fd);
3042 t->srv_state = SV_STCLOSE;
3043 if (!(t->flags & SN_ERR_MASK))
3044 t->flags |= SN_ERR_SRVTO;
3045 if (!(t->flags & SN_FINST_MASK))
3046 t->flags |= SN_FINST_D;
3047 /* We used to have a free connection slot. Since we'll never use it,
3048 * we have to inform the server that it may be used by another session.
3049 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003050 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003051 task_wakeup(&rq, t->srv->queue_mgt);
3052
3053 return 1;
3054 }
3055 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02003056 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3057 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003058 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003059 }
3060 }
3061 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02003062 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3063 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01003064 if (t->be->beprm->srvtimeout)
3065 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003066 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003067 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003068 }
3069 }
3070 return 0;
3071 }
3072 else { /* SV_STCLOSE : nothing to do */
3073 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3074 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003075 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
3076 t->uniq_id, t->be->beprm->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003077 write(1, trash, len);
3078 }
3079 return 0;
3080 }
3081 return 0;
3082}
3083
3084
3085/*
3086 * Produces data for the session <s> depending on its source. Expects to be
3087 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3088 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3089 * session, which it uses to keep on being called when there is free space in
3090 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3091 * if it changes the session state from CL_STSHUTR, otherwise 0.
3092 */
3093int produce_content(struct session *s)
3094{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003095 if (s->data_source == DATA_SRC_NONE) {
3096 s->flags &= ~SN_SELF_GEN;
3097 return 1;
3098 }
3099 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003100 /* dump server statistics */
3101 return produce_content_stats(s);
3102 }
3103 else {
3104 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003105 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003106 client_retnclose(s, error_message(s, HTTP_ERR_500));
3107 if (!(s->flags & SN_ERR_MASK))
3108 s->flags |= SN_ERR_PRXCOND;
3109 if (!(s->flags & SN_FINST_MASK))
3110 s->flags |= SN_FINST_R;
3111 s->flags &= ~SN_SELF_GEN;
3112 return 1;
3113 }
3114}
3115
3116
3117/*
3118 * Produces statistics data for the session <s>. Expects to be called with
3119 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3120 * flag from the session, which it uses to keep on being called when there is
3121 * free space in the buffer, of simply by letting an empty buffer upon return.
3122 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3123 */
3124int produce_content_stats(struct session *s)
3125{
3126 struct buffer *rep = s->rep;
3127 struct proxy *px;
3128 struct chunk msg;
3129 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003130
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003131 msg.len = 0;
3132 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003133
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003134 switch (s->data_state) {
3135 case DATA_ST_INIT:
3136 /* the function had not been called yet */
3137 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003138
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003139 chunk_printf(&msg, sizeof(trash),
3140 "HTTP/1.0 200 OK\r\n"
3141 "Cache-Control: no-cache\r\n"
3142 "Connection: close\r\n"
3143 "Content-Type: text/html\r\n"
3144 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003145
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003146 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003147 client_retnclose(s, &msg); // send the start of the response.
3148 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003149
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003150 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3151 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3152 if (!(s->flags & SN_FINST_MASK))
3153 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003154
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003155 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003156 /* that's all we return in case of HEAD request */
3157 s->data_state = DATA_ST_FIN;
3158 s->flags &= ~SN_SELF_GEN;
3159 return 1;
3160 }
3161
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003162 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3163 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003164
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003165 case DATA_ST_HEAD:
3166 /* WARNING! This must fit in the first buffer !!! */
3167 chunk_printf(&msg, sizeof(trash),
3168 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3169 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3170 "<style type=\"text/css\"><!--\n"
3171 "body {"
3172 " font-family: helvetica, arial;"
3173 " font-size: 12px;"
3174 " font-weight: normal;"
3175 " color: black;"
3176 " background: white;"
3177 "}\n"
3178 "th,td {"
3179 " font-size: 0.8em;"
3180 " align: center;"
3181 "}"
3182 "h1 {"
3183 " font-size: xx-large;"
3184 " margin-bottom: 0.5em;"
3185 "}\n"
3186 "h2 {"
3187 " font-family: helvetica, arial;"
3188 " font-size: x-large;"
3189 " font-weight: bold;"
3190 " font-style: italic;"
3191 " color: #6020a0;"
3192 " margin-top: 0em;"
3193 " margin-bottom: 0em;"
3194 "}\n"
3195 "h3 {"
3196 " font-family: helvetica, arial;"
3197 " font-size: 16px;"
3198 " font-weight: bold;"
3199 " color: #b00040;"
3200 " background: #e8e8d0;"
3201 " margin-top: 0em;"
3202 " margin-bottom: 0em;"
3203 "}\n"
3204 "li {"
3205 " margin-top: 0.25em;"
3206 " margin-right: 2em;"
3207 "}\n"
3208 ".hr {margin-top: 0.25em;"
3209 " border-color: black;"
3210 " border-bottom-style: solid;"
3211 "}\n"
3212 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3213 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3214 ".total {background: #20D0D0;color: #ffff80;}\n"
3215 ".frontend {background: #e8e8d0;}\n"
3216 ".backend {background: #e8e8d0;}\n"
3217 ".active0 {background: #ff9090;}\n"
3218 ".active1 {background: #ffd020;}\n"
3219 ".active2 {background: #ffffa0;}\n"
3220 ".active3 {background: #c0ffc0;}\n"
3221 ".active4 {background: #e0e0e0;}\n"
3222 ".backup0 {background: #ff9090;}\n"
3223 ".backup1 {background: #ff80ff;}\n"
3224 ".backup2 {background: #c060ff;}\n"
3225 ".backup3 {background: #b0d0ff;}\n"
3226 ".backup4 {background: #e0e0e0;}\n"
3227 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003228 "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003229 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3230 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3231 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3232 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3233 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3234 "-->"
3235 "</style></head>");
3236
3237 if (buffer_write_chunk(rep, &msg) != 0)
3238 return 0;
3239
3240 s->data_state = DATA_ST_INFO;
3241 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003242
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003243 case DATA_ST_INFO:
3244 up = (now.tv_sec - start_date.tv_sec);
3245
3246 /* WARNING! this has to fit the first packet too.
3247 * We are around 3.5 kB, add adding entries will
3248 * become tricky if we want to support 4kB buffers !
3249 */
3250 chunk_printf(&msg, sizeof(trash),
3251 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3252 PRODUCT_NAME "</a></h1>\n"
3253 "<h2>Statistics Report for pid %d</h2>\n"
3254 "<hr width=\"100%%\" class=\"hr\">\n"
3255 "<h3>&gt; General process information</h3>\n"
3256 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3257 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3258 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3259 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3260 "<b>maxsock = </b> %d<br>\n"
3261 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3262 "</td><td align=\"center\" nowrap>\n"
3263 "<table class=\"lgd\"><tr>"
3264 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3265 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3266 "</tr><tr>"
3267 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3268 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3269 "</tr><tr>"
3270 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3271 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3272 "</tr><tr>"
3273 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3274 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3275 "</tr></table>\n"
3276 "</td>"
3277 "<td align=\"left\" nowrap width=\"1%%\">"
3278 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">"
3279 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>"
3280 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>"
3281 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>"
3282 "</ul>"
3283 "</td>"
3284 "</tr></table>\n"
3285 "",
3286 pid, pid, global.nbproc,
3287 up / 86400, (up % 86400) / 3600,
3288 (up % 3600) / 60, (up % 60),
3289 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3290 global.rlimit_memmax ? " MB" : "",
3291 global.rlimit_nofile,
3292 global.maxsock,
3293 global.maxconn,
3294 actconn
3295 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003296
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003297 if (buffer_write_chunk(rep, &msg) != 0)
3298 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003299
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003300 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003301
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003302 s->data_ctx.stats.px = proxy;
3303 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3304 s->data_state = DATA_ST_LIST;
3305 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003306
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003307 case DATA_ST_LIST:
3308 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003309 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003310 px = s->data_ctx.stats.px;
3311 /* skip the disabled proxies and non-networked ones */
3312 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3313 if (produce_content_stats_proxy(s, px) == 0)
3314 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003315
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003316 s->data_ctx.stats.px = px->next;
3317 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3318 }
3319 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003320
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003321 s->data_state = DATA_ST_END;
3322 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003323
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003324 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003325 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003326 if (buffer_write_chunk(rep, &msg) != 0)
3327 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003328
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003329 s->data_state = DATA_ST_FIN;
3330 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003331
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003332 case DATA_ST_FIN:
3333 s->flags &= ~SN_SELF_GEN;
3334 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003335
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003336 default:
3337 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003338 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003339 client_retnclose(s, error_message(s, HTTP_ERR_500));
3340 if (!(s->flags & SN_ERR_MASK))
3341 s->flags |= SN_ERR_PRXCOND;
3342 if (!(s->flags & SN_FINST_MASK))
3343 s->flags |= SN_FINST_R;
3344 s->flags &= ~SN_SELF_GEN;
3345 return 1;
3346 }
3347}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003348
Willy Tarreaubaaee002006-06-26 02:48:02 +02003349
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003350/*
3351 * Dumps statistics for a proxy.
3352 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3353 * ot non-zero if everything completed.
3354 */
3355int produce_content_stats_proxy(struct session *s, struct proxy *px)
3356{
3357 struct buffer *rep = s->rep;
3358 struct server *sv;
3359 struct chunk msg;
3360
3361 msg.len = 0;
3362 msg.str = trash;
3363
3364 switch (s->data_ctx.stats.px_st) {
3365 case DATA_ST_PX_INIT:
3366 /* we are on a new proxy */
3367
3368 if (s->be->fiprm->uri_auth && s->be->fiprm->uri_auth->scope) {
3369 /* we have a limited scope, we have to check the proxy name */
3370 struct stat_scope *scope;
3371 int len;
3372
3373 len = strlen(px->id);
3374 scope = s->be->fiprm->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003375
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003376 while (scope) {
3377 /* match exact proxy name */
3378 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3379 break;
3380
3381 /* match '.' which means 'self' proxy */
3382 if (!strcmp(scope->px_id, ".") && px == s->fe)
3383 break;
3384 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003385 }
3386
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003387 /* proxy name not found : don't dump anything */
3388 if (scope == NULL)
3389 return 1;
3390 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003391
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003392 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3393 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003394
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003395 case DATA_ST_PX_TH:
3396 /* print a new table */
3397 chunk_printf(&msg, sizeof(trash),
3398 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3399 "<tr align=\"center\" class=\"titre\">"
3400 "<th colspan=2 class=\"pxname\">%s</th>"
3401 "<th colspan=18 class=\"empty\"></th>"
3402 "</tr>\n"
3403 "<tr align=\"center\" class=\"titre\">"
3404 "<th rowspan=2></th>"
3405 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3406 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3407 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3408 "</tr>\n"
3409 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003410 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3411 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003412 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3413 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3414 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3415 "",
3416 px->id);
3417
3418 if (buffer_write_chunk(rep, &msg) != 0)
3419 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003420
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003421 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3422 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003423
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003424 case DATA_ST_PX_FE:
3425 /* print the frontend */
3426 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003427 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003428 /* name, queue */
3429 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3430 /* sessions : current, max, limit, cumul. */
3431 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3432 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003433 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003434 /* denied: req, resp */
3435 "<td align=right>%d</td><td align=right>%d</td>"
3436 /* errors : request, connect, response */
3437 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3438 /* server status : reflect backend status */
3439 "<td align=center>%s</td>"
3440 /* rest of server: nothing */
3441 "<td align=center colspan=5></td></tr>"
3442 "",
3443 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003444 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003445 px->denied_req, px->denied_resp,
3446 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003447 px->state == PR_STRUN ? "OPEN" :
3448 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003449
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003450 if (buffer_write_chunk(rep, &msg) != 0)
3451 return 0;
3452 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003453
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003454 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3455 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3456 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003457
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003458 case DATA_ST_PX_SV:
3459 /* stats.sv has been initialized above */
3460 while (s->data_ctx.stats.sv != NULL) {
3461 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3462 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003463
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003464 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003465
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003466 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3467 if (!(sv->state & SRV_CHECKED))
3468 sv_state = 4;
3469 else if (sv->state & SRV_RUNNING)
3470 if (sv->health == sv->rise + sv->fall - 1)
3471 sv_state = 3; /* UP */
3472 else
3473 sv_state = 2; /* going down */
3474 else
3475 if (sv->health)
3476 sv_state = 1; /* going up */
3477 else
3478 sv_state = 0; /* DOWN */
3479
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003480 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003481 /* name */
3482 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3483 /* queue : current, max */
3484 "<td align=right>%d</td><td align=right>%d</td>"
3485 /* sessions : current, max, limit, cumul */
3486 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3487 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003488 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003489 /* denied: req, resp */
3490 "<td align=right></td><td align=right>%d</td>"
3491 /* errors : request, connect, response */
3492 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3493 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003494 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003495 sv_state, sv->id,
3496 sv->nbpend, sv->nbpend_max,
3497 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003498 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003499 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003500 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003501
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003502 /* status */
3503 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3504 chunk_printf(&msg, sizeof(trash),
3505 srv_hlt_st[sv_state],
3506 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3507 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3508
Willy Tarreau128e9542007-01-01 22:01:43 +01003509 chunk_printf(&msg, sizeof(trash),
3510 /* weight */
3511 "</td><td>%d</td>"
3512 /* act, bck */
3513 "<td>%s</td><td>%s</td>"
3514 "",
3515 sv->uweight+1,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003516 (sv->state & SRV_BACKUP) ? "-" : "Y",
3517 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003518
3519 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003520 if (sv->state & SRV_CHECKED)
3521 chunk_printf(&msg, sizeof(trash),
3522 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3523 sv->failed_checks, sv->down_trans);
3524 else
3525 chunk_printf(&msg, sizeof(trash),
3526 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003527
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003528 if (buffer_write_chunk(rep, &msg) != 0)
3529 return 0;
3530
3531 s->data_ctx.stats.sv = sv->next;
3532 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003533
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003534 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3535 /* fall through */
3536
3537 case DATA_ST_PX_BE:
3538 /* print the backend */
3539 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003540 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003541 /* name */
3542 "<tr align=center class=\"backend\"><td>Backend</td>"
3543 /* queue : current, max */
3544 "<td align=right>%d</td><td align=right>%d</td>"
3545 /* sessions : current, max, limit, cumul. */
3546 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3547 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003548 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003549 /* denied: req, resp */
3550 "<td align=right>%d</td><td align=right>%d</td>"
3551 /* errors : request, connect, response */
3552 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3553 /* server status : reflect backend status (up/down) : we display UP
3554 * if the backend has known working servers or if it has no server at
3555 * all (eg: for stats). Tthen we display the total weight, number of
3556 * active and backups. */
3557 "<td align=center>%s</td><td align=center>%d</td>"
3558 "<td align=center>%d</td><td align=center>%d</td>"
3559 /* rest of server: nothing */
3560 "<td align=center colspan=2></td></tr>"
3561 "",
3562 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3563 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003564 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003565 px->denied_req, px->denied_resp,
3566 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003567 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3568 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003569
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003570 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003571 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003572 }
3573
3574 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3575 /* fall through */
3576
3577 case DATA_ST_PX_END:
3578 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3579
3580 if (buffer_write_chunk(rep, &msg) != 0)
3581 return 0;
3582
3583 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3584 /* fall through */
3585
3586 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003587 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003588
3589 default:
3590 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003591 return 1;
3592 }
3593}
3594
3595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003596/* Iterate the same filter through all request headers.
3597 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003598 * Since it can manage the switch to another backend, it updates the per-proxy
3599 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003600 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003601int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003602{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003603 char term;
3604 char *cur_ptr, *cur_end, *cur_next;
3605 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003606 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003607 struct hdr_idx_elem *cur_hdr;
3608 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003610 last_hdr = 0;
3611
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003612 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003613 old_idx = 0;
3614
3615 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003616 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003617 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003618 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003619 (exp->action == ACT_ALLOW ||
3620 exp->action == ACT_DENY ||
3621 exp->action == ACT_TARPIT))
3622 return 0;
3623
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003624 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003625 if (!cur_idx)
3626 break;
3627
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003628 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003629 cur_ptr = cur_next;
3630 cur_end = cur_ptr + cur_hdr->len;
3631 cur_next = cur_end + cur_hdr->cr + 1;
3632
3633 /* Now we have one header between cur_ptr and cur_end,
3634 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003635 */
3636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003637 /* The annoying part is that pattern matching needs
3638 * that we modify the contents to null-terminate all
3639 * strings before testing them.
3640 */
3641
3642 term = *cur_end;
3643 *cur_end = '\0';
3644
3645 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3646 switch (exp->action) {
3647 case ACT_SETBE:
3648 /* It is not possible to jump a second time.
3649 * FIXME: should we return an HTTP/500 here so that
3650 * the admin knows there's a problem ?
3651 */
3652 if (t->be != t->fe)
3653 break;
3654
3655 /* Swithing Proxy */
3656 t->be = (struct proxy *) exp->replace;
3657
3658 /* right now, the backend switch is not too much complicated
3659 * because we have associated req_cap and rsp_cap to the
3660 * frontend, and the beconn will be updated later.
3661 */
3662
3663 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3664 t->req->cto = t->be->beprm->contimeout;
3665 last_hdr = 1;
3666 break;
3667
3668 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003669 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003670 last_hdr = 1;
3671 break;
3672
3673 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003674 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003675 last_hdr = 1;
3676 t->be->beprm->denied_req++;
3677 break;
3678
3679 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003680 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003681 last_hdr = 1;
3682 t->be->beprm->denied_req++;
3683 break;
3684
3685 case ACT_REPLACE:
3686 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3687 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3688 /* FIXME: if the user adds a newline in the replacement, the
3689 * index will not be recalculated for now, and the new line
3690 * will not be counted as a new header.
3691 */
3692
3693 cur_end += delta;
3694 cur_next += delta;
3695 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003696 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003697 break;
3698
3699 case ACT_REMOVE:
3700 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3701 cur_next += delta;
3702
3703 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003704 txn->req.eoh += delta;
3705 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3706 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003707 cur_hdr->len = 0;
3708 cur_end = NULL; /* null-term has been rewritten */
3709 break;
3710
3711 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003712 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003713 if (cur_end)
3714 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003716 /* keep the link from this header to next one in case of later
3717 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003718 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003719 old_idx = cur_idx;
3720 }
3721 return 0;
3722}
3723
3724
3725/* Apply the filter to the request line.
3726 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3727 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003728 * Since it can manage the switch to another backend, it updates the per-proxy
3729 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003730 */
3731int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3732{
3733 char term;
3734 char *cur_ptr, *cur_end;
3735 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003736 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003737 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003738
Willy Tarreau58f10d72006-12-04 02:26:12 +01003739
Willy Tarreau3d300592007-03-18 18:34:41 +01003740 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003741 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003742 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003743 (exp->action == ACT_ALLOW ||
3744 exp->action == ACT_DENY ||
3745 exp->action == ACT_TARPIT))
3746 return 0;
3747 else if (exp->action == ACT_REMOVE)
3748 return 0;
3749
3750 done = 0;
3751
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003752 cur_ptr = req->data + txn->req.som;
3753 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003754
3755 /* Now we have the request line between cur_ptr and cur_end */
3756
3757 /* The annoying part is that pattern matching needs
3758 * that we modify the contents to null-terminate all
3759 * strings before testing them.
3760 */
3761
3762 term = *cur_end;
3763 *cur_end = '\0';
3764
3765 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3766 switch (exp->action) {
3767 case ACT_SETBE:
3768 /* It is not possible to jump a second time.
3769 * FIXME: should we return an HTTP/500 here so that
3770 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003771 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003772 if (t->be != t->fe)
3773 break;
3774
3775 /* Swithing Proxy */
3776 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003777
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003778 /* right now, the backend switch is not too much complicated
3779 * because we have associated req_cap and rsp_cap to the
3780 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003781 */
3782
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003783 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3784 t->req->cto = t->be->beprm->contimeout;
3785 done = 1;
3786 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003788 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003789 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003790 done = 1;
3791 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003793 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003794 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003795 t->be->beprm->denied_req++;
3796 done = 1;
3797 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003799 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003800 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003801 t->be->beprm->denied_req++;
3802 done = 1;
3803 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003805 case ACT_REPLACE:
3806 *cur_end = term; /* restore the string terminator */
3807 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3808 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3809 /* FIXME: if the user adds a newline in the replacement, the
3810 * index will not be recalculated for now, and the new line
3811 * will not be counted as a new header.
3812 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003813
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003814 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003815 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003816
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003817 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003818 HTTP_MSG_RQMETH,
3819 cur_ptr, cur_end + 1,
3820 NULL, NULL);
3821 if (unlikely(!cur_end))
3822 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003824 /* we have a full request and we know that we have either a CR
3825 * or an LF at <ptr>.
3826 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003827 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3828 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003829 /* there is no point trying this regex on headers */
3830 return 1;
3831 }
3832 }
3833 *cur_end = term; /* restore the string terminator */
3834 return done;
3835}
Willy Tarreau97de6242006-12-27 17:18:38 +01003836
Willy Tarreau58f10d72006-12-04 02:26:12 +01003837
Willy Tarreau58f10d72006-12-04 02:26:12 +01003838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003839/*
3840 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3841 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003842 * unparsable request. Since it can manage the switch to another backend, it
3843 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003844 */
3845int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3846{
Willy Tarreau3d300592007-03-18 18:34:41 +01003847 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003848 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003849 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003850 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003851
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003852 /*
3853 * The interleaving of transformations and verdicts
3854 * makes it difficult to decide to continue or stop
3855 * the evaluation.
3856 */
3857
Willy Tarreau3d300592007-03-18 18:34:41 +01003858 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003859 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3860 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3861 exp = exp->next;
3862 continue;
3863 }
3864
3865 /* Apply the filter to the request line. */
3866 ret = apply_filter_to_req_line(t, req, exp);
3867 if (unlikely(ret < 0))
3868 return -1;
3869
3870 if (likely(ret == 0)) {
3871 /* The filter did not match the request, it can be
3872 * iterated through all headers.
3873 */
3874 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003875 }
3876 exp = exp->next;
3877 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003878 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003879}
3880
3881
Willy Tarreaua15645d2007-03-18 16:22:39 +01003882
Willy Tarreau58f10d72006-12-04 02:26:12 +01003883/*
3884 * Manager client-side cookie
3885 */
3886void manage_client_side_cookies(struct session *t, struct buffer *req)
3887{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003888 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003889 char *p1, *p2, *p3, *p4;
3890 char *del_colon, *del_cookie, *colon;
3891 int app_cookies;
3892
3893 appsess *asession_temp = NULL;
3894 appsess local_asession;
3895
3896 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003897 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003898
Willy Tarreau830ff452006-12-17 19:31:23 +01003899 if (t->be->beprm->cookie_name == NULL &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003900 t->be->beprm->appsession_name == NULL &&
3901 t->be->fiprm->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003902 return;
3903
Willy Tarreau2a324282006-12-05 00:05:46 +01003904 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003905 * we start with the start line.
3906 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003907 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003908 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003909
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003910 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003911 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003912 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003913
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003914 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003915 cur_ptr = cur_next;
3916 cur_end = cur_ptr + cur_hdr->len;
3917 cur_next = cur_end + cur_hdr->cr + 1;
3918
3919 /* We have one full header between cur_ptr and cur_end, and the
3920 * next header starts at cur_next. We're only interested in
3921 * "Cookie:" headers.
3922 */
3923
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003924 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3925 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003926 old_idx = cur_idx;
3927 continue;
3928 }
3929
3930 /* Now look for cookies. Conforming to RFC2109, we have to support
3931 * attributes whose name begin with a '$', and associate them with
3932 * the right cookie, if we want to delete this cookie.
3933 * So there are 3 cases for each cookie read :
3934 * 1) it's a special attribute, beginning with a '$' : ignore it.
3935 * 2) it's a server id cookie that we *MAY* want to delete : save
3936 * some pointers on it (last semi-colon, beginning of cookie...)
3937 * 3) it's an application cookie : we *MAY* have to delete a previous
3938 * "special" cookie.
3939 * At the end of loop, if a "special" cookie remains, we may have to
3940 * remove it. If no application cookie persists in the header, we
3941 * *MUST* delete it
3942 */
3943
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003944 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003945
Willy Tarreau58f10d72006-12-04 02:26:12 +01003946 /* del_cookie == NULL => nothing to be deleted */
3947 del_colon = del_cookie = NULL;
3948 app_cookies = 0;
3949
3950 while (p1 < cur_end) {
3951 /* skip spaces and colons, but keep an eye on these ones */
3952 while (p1 < cur_end) {
3953 if (*p1 == ';' || *p1 == ',')
3954 colon = p1;
3955 else if (!isspace((int)*p1))
3956 break;
3957 p1++;
3958 }
3959
3960 if (p1 == cur_end)
3961 break;
3962
3963 /* p1 is at the beginning of the cookie name */
3964 p2 = p1;
3965 while (p2 < cur_end && *p2 != '=')
3966 p2++;
3967
3968 if (p2 == cur_end)
3969 break;
3970
3971 p3 = p2 + 1; /* skips the '=' sign */
3972 if (p3 == cur_end)
3973 break;
3974
3975 p4 = p3;
3976 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
3977 p4++;
3978
3979 /* here, we have the cookie name between p1 and p2,
3980 * and its value between p3 and p4.
3981 * we can process it :
3982 *
3983 * Cookie: NAME=VALUE;
3984 * | || || |
3985 * | || || +--> p4
3986 * | || |+-------> p3
3987 * | || +--------> p2
3988 * | |+------------> p1
3989 * | +-------------> colon
3990 * +--------------------> cur_ptr
3991 */
3992
3993 if (*p1 == '$') {
3994 /* skip this one */
3995 }
3996 else {
3997 /* first, let's see if we want to capture it */
Willy Tarreau830ff452006-12-17 19:31:23 +01003998 if (t->fe->fiprm->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003999 txn->cli_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01004000 (p4 - p1 >= t->fe->fiprm->capture_namelen) &&
4001 memcmp(p1, t->fe->fiprm->capture_name, t->fe->fiprm->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004002 int log_len = p4 - p1;
4003
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004004 if ((txn->cli_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004005 Alert("HTTP logging : out of memory.\n");
4006 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004007 if (log_len > t->fe->fiprm->capture_len)
4008 log_len = t->fe->fiprm->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004009 memcpy(txn->cli_cookie, p1, log_len);
4010 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004011 }
4012 }
4013
Willy Tarreau830ff452006-12-17 19:31:23 +01004014 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
4015 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004016 /* Cool... it's the right one */
Willy Tarreau830ff452006-12-17 19:31:23 +01004017 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004018 char *delim;
4019
4020 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4021 * have the server ID betweek p3 and delim, and the original cookie between
4022 * delim+1 and p4. Otherwise, delim==p4 :
4023 *
4024 * Cookie: NAME=SRV~VALUE;
4025 * | || || | |
4026 * | || || | +--> p4
4027 * | || || +--------> delim
4028 * | || |+-----------> p3
4029 * | || +------------> p2
4030 * | |+----------------> p1
4031 * | +-----------------> colon
4032 * +------------------------> cur_ptr
4033 */
4034
Willy Tarreau830ff452006-12-17 19:31:23 +01004035 if (t->be->beprm->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004036 for (delim = p3; delim < p4; delim++)
4037 if (*delim == COOKIE_DELIM)
4038 break;
4039 }
4040 else
4041 delim = p4;
4042
4043
4044 /* Here, we'll look for the first running server which supports the cookie.
4045 * This allows to share a same cookie between several servers, for example
4046 * to dedicate backup servers to specific servers only.
4047 * However, to prevent clients from sticking to cookie-less backup server
4048 * when they have incidentely learned an empty cookie, we simply ignore
4049 * empty cookies and mark them as invalid.
4050 */
4051 if (delim == p3)
4052 srv = NULL;
4053
4054 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004055 if (srv->cookie && (srv->cklen == delim - p3) &&
4056 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004057 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004058 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004059 txn->flags &= ~TX_CK_MASK;
4060 txn->flags |= TX_CK_VALID;
4061 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004062 t->srv = srv;
4063 break;
4064 } else {
4065 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004066 txn->flags &= ~TX_CK_MASK;
4067 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004068 }
4069 }
4070 srv = srv->next;
4071 }
4072
Willy Tarreau3d300592007-03-18 18:34:41 +01004073 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004074 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004075 txn->flags &= ~TX_CK_MASK;
4076 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004077 }
4078
4079 /* depending on the cookie mode, we may have to either :
4080 * - delete the complete cookie if we're in insert+indirect mode, so that
4081 * the server never sees it ;
4082 * - remove the server id from the cookie value, and tag the cookie as an
4083 * application cookie so that it does not get accidentely removed later,
4084 * if we're in cookie prefix mode
4085 */
Willy Tarreau830ff452006-12-17 19:31:23 +01004086 if ((t->be->beprm->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004087 int delta; /* negative */
4088
4089 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4090 p4 += delta;
4091 cur_end += delta;
4092 cur_next += delta;
4093 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004094 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004095
4096 del_cookie = del_colon = NULL;
4097 app_cookies++; /* protect the header from deletion */
4098 }
4099 else if (del_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01004100 (t->be->beprm->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004101 del_cookie = p1;
4102 del_colon = colon;
4103 }
4104 } else {
4105 /* now we know that we must keep this cookie since it's
4106 * not ours. But if we wanted to delete our cookie
4107 * earlier, we cannot remove the complete header, but we
4108 * can remove the previous block itself.
4109 */
4110 app_cookies++;
4111
4112 if (del_cookie != NULL) {
4113 int delta; /* negative */
4114
4115 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4116 p4 += delta;
4117 cur_end += delta;
4118 cur_next += delta;
4119 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004120 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004121 del_cookie = del_colon = NULL;
4122 }
4123 }
4124
Willy Tarreau830ff452006-12-17 19:31:23 +01004125 if ((t->be->beprm->appsession_name != NULL) &&
4126 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004127 /* first, let's see if the cookie is our appcookie*/
4128
4129 /* Cool... it's the right one */
4130
4131 asession_temp = &local_asession;
4132
4133 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4134 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4135 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4136 return;
4137 }
4138
Willy Tarreau830ff452006-12-17 19:31:23 +01004139 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4140 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004141 asession_temp->serverid = NULL;
4142
4143 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004144 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004145 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4146 /* free previously allocated memory */
4147 pool_free_to(apools.sessid, local_asession.sessid);
4148 Alert("Not enough memory process_cli():asession:calloc().\n");
4149 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4150 return;
4151 }
4152
4153 asession_temp->sessid = local_asession.sessid;
4154 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004155 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004156 } else {
4157 /* free previously allocated memory */
4158 pool_free_to(apools.sessid, local_asession.sessid);
4159 }
4160
4161 if (asession_temp->serverid == NULL) {
4162 Alert("Found Application Session without matching server.\n");
4163 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004164 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004165 while (srv) {
4166 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004167 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004168 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004169 txn->flags &= ~TX_CK_MASK;
4170 txn->flags |= TX_CK_VALID;
4171 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004172 t->srv = srv;
4173 break;
4174 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004175 txn->flags &= ~TX_CK_MASK;
4176 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004177 }
4178 }
4179 srv = srv->next;
4180 }/* end while(srv) */
4181 }/* end else if server == NULL */
4182
Willy Tarreau830ff452006-12-17 19:31:23 +01004183 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004184 }/* end if ((t->proxy->appsession_name != NULL) ... */
4185 }
4186
4187 /* we'll have to look for another cookie ... */
4188 p1 = p4;
4189 } /* while (p1 < cur_end) */
4190
4191 /* There's no more cookie on this line.
4192 * We may have marked the last one(s) for deletion.
4193 * We must do this now in two ways :
4194 * - if there is no app cookie, we simply delete the header ;
4195 * - if there are app cookies, we must delete the end of the
4196 * string properly, including the colon/semi-colon before
4197 * the cookie name.
4198 */
4199 if (del_cookie != NULL) {
4200 int delta;
4201 if (app_cookies) {
4202 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4203 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004204 cur_hdr->len += delta;
4205 } else {
4206 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004207
4208 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004209 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4210 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004211 cur_hdr->len = 0;
4212 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004213 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004214 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004215 }
4216
4217 /* keep the link from this header to next one */
4218 old_idx = cur_idx;
4219 } /* end of cookie processing on this header */
4220}
4221
4222
Willy Tarreaua15645d2007-03-18 16:22:39 +01004223/* Iterate the same filter through all response headers contained in <rtr>.
4224 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4225 */
4226int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4227{
4228 char term;
4229 char *cur_ptr, *cur_end, *cur_next;
4230 int cur_idx, old_idx, last_hdr;
4231 struct http_txn *txn = &t->txn;
4232 struct hdr_idx_elem *cur_hdr;
4233 int len, delta;
4234
4235 last_hdr = 0;
4236
4237 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4238 old_idx = 0;
4239
4240 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004241 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004242 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004243 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004244 (exp->action == ACT_ALLOW ||
4245 exp->action == ACT_DENY))
4246 return 0;
4247
4248 cur_idx = txn->hdr_idx.v[old_idx].next;
4249 if (!cur_idx)
4250 break;
4251
4252 cur_hdr = &txn->hdr_idx.v[cur_idx];
4253 cur_ptr = cur_next;
4254 cur_end = cur_ptr + cur_hdr->len;
4255 cur_next = cur_end + cur_hdr->cr + 1;
4256
4257 /* Now we have one header between cur_ptr and cur_end,
4258 * and the next header starts at cur_next.
4259 */
4260
4261 /* The annoying part is that pattern matching needs
4262 * that we modify the contents to null-terminate all
4263 * strings before testing them.
4264 */
4265
4266 term = *cur_end;
4267 *cur_end = '\0';
4268
4269 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4270 switch (exp->action) {
4271 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004272 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004273 last_hdr = 1;
4274 break;
4275
4276 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004277 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004278 last_hdr = 1;
4279 break;
4280
4281 case ACT_REPLACE:
4282 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4283 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4284 /* FIXME: if the user adds a newline in the replacement, the
4285 * index will not be recalculated for now, and the new line
4286 * will not be counted as a new header.
4287 */
4288
4289 cur_end += delta;
4290 cur_next += delta;
4291 cur_hdr->len += delta;
4292 txn->rsp.eoh += delta;
4293 break;
4294
4295 case ACT_REMOVE:
4296 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4297 cur_next += delta;
4298
4299 /* FIXME: this should be a separate function */
4300 txn->rsp.eoh += delta;
4301 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4302 txn->hdr_idx.used--;
4303 cur_hdr->len = 0;
4304 cur_end = NULL; /* null-term has been rewritten */
4305 break;
4306
4307 }
4308 }
4309 if (cur_end)
4310 *cur_end = term; /* restore the string terminator */
4311
4312 /* keep the link from this header to next one in case of later
4313 * removal of next header.
4314 */
4315 old_idx = cur_idx;
4316 }
4317 return 0;
4318}
4319
4320
4321/* Apply the filter to the status line in the response buffer <rtr>.
4322 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4323 * or -1 if a replacement resulted in an invalid status line.
4324 */
4325int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4326{
4327 char term;
4328 char *cur_ptr, *cur_end;
4329 int done;
4330 struct http_txn *txn = &t->txn;
4331 int len, delta;
4332
4333
Willy Tarreau3d300592007-03-18 18:34:41 +01004334 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004335 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004336 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004337 (exp->action == ACT_ALLOW ||
4338 exp->action == ACT_DENY))
4339 return 0;
4340 else if (exp->action == ACT_REMOVE)
4341 return 0;
4342
4343 done = 0;
4344
4345 cur_ptr = rtr->data + txn->rsp.som;
4346 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4347
4348 /* Now we have the status line between cur_ptr and cur_end */
4349
4350 /* The annoying part is that pattern matching needs
4351 * that we modify the contents to null-terminate all
4352 * strings before testing them.
4353 */
4354
4355 term = *cur_end;
4356 *cur_end = '\0';
4357
4358 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4359 switch (exp->action) {
4360 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004361 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004362 done = 1;
4363 break;
4364
4365 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004366 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004367 done = 1;
4368 break;
4369
4370 case ACT_REPLACE:
4371 *cur_end = term; /* restore the string terminator */
4372 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4373 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4374 /* FIXME: if the user adds a newline in the replacement, the
4375 * index will not be recalculated for now, and the new line
4376 * will not be counted as a new header.
4377 */
4378
4379 txn->rsp.eoh += delta;
4380 cur_end += delta;
4381
4382 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
4383 HTTP_MSG_RQMETH,
4384 cur_ptr, cur_end + 1,
4385 NULL, NULL);
4386 if (unlikely(!cur_end))
4387 return -1;
4388
4389 /* we have a full respnse and we know that we have either a CR
4390 * or an LF at <ptr>.
4391 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004392 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004393 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4394 /* there is no point trying this regex on headers */
4395 return 1;
4396 }
4397 }
4398 *cur_end = term; /* restore the string terminator */
4399 return done;
4400}
4401
4402
4403
4404/*
4405 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4406 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4407 * unparsable response.
4408 */
4409int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4410{
Willy Tarreau3d300592007-03-18 18:34:41 +01004411 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004412 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004413 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004414 int ret;
4415
4416 /*
4417 * The interleaving of transformations and verdicts
4418 * makes it difficult to decide to continue or stop
4419 * the evaluation.
4420 */
4421
Willy Tarreau3d300592007-03-18 18:34:41 +01004422 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004423 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4424 exp->action == ACT_PASS)) {
4425 exp = exp->next;
4426 continue;
4427 }
4428
4429 /* Apply the filter to the status line. */
4430 ret = apply_filter_to_sts_line(t, rtr, exp);
4431 if (unlikely(ret < 0))
4432 return -1;
4433
4434 if (likely(ret == 0)) {
4435 /* The filter did not match the response, it can be
4436 * iterated through all headers.
4437 */
4438 apply_filter_to_resp_headers(t, rtr, exp);
4439 }
4440 exp = exp->next;
4441 }
4442 return 0;
4443}
4444
4445
4446
4447/*
4448 * Manager server-side cookies
4449 */
4450void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4451{
4452 struct http_txn *txn = &t->txn;
4453 char *p1, *p2, *p3, *p4;
4454
4455 appsess *asession_temp = NULL;
4456 appsess local_asession;
4457
4458 char *cur_ptr, *cur_end, *cur_next;
4459 int cur_idx, old_idx, delta;
4460
4461 if (t->be->beprm->cookie_name == NULL &&
4462 t->be->beprm->appsession_name == NULL &&
4463 t->be->fiprm->capture_name == NULL &&
4464 !(t->be->beprm->options & PR_O_CHK_CACHE))
4465 return;
4466
4467 /* Iterate through the headers.
4468 * we start with the start line.
4469 */
4470 old_idx = 0;
4471 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4472
4473 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4474 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004475 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004476
4477 cur_hdr = &txn->hdr_idx.v[cur_idx];
4478 cur_ptr = cur_next;
4479 cur_end = cur_ptr + cur_hdr->len;
4480 cur_next = cur_end + cur_hdr->cr + 1;
4481
4482 /* We have one full header between cur_ptr and cur_end, and the
4483 * next header starts at cur_next. We're only interested in
4484 * "Cookie:" headers.
4485 */
4486
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004487 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4488 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004489 old_idx = cur_idx;
4490 continue;
4491 }
4492
4493 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004494 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004495
4496
4497 /* maybe we only wanted to see if there was a set-cookie */
4498 if (t->be->beprm->cookie_name == NULL &&
4499 t->be->beprm->appsession_name == NULL &&
4500 t->be->fiprm->capture_name == NULL)
4501 return;
4502
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004503 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004504
4505 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004506 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4507 break;
4508
4509 /* p1 is at the beginning of the cookie name */
4510 p2 = p1;
4511
4512 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4513 p2++;
4514
4515 if (p2 == cur_end || *p2 == ';') /* next cookie */
4516 break;
4517
4518 p3 = p2 + 1; /* skip the '=' sign */
4519 if (p3 == cur_end)
4520 break;
4521
4522 p4 = p3;
4523 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4524 p4++;
4525
4526 /* here, we have the cookie name between p1 and p2,
4527 * and its value between p3 and p4.
4528 * we can process it.
4529 */
4530
4531 /* first, let's see if we want to capture it */
4532 if (t->be->fiprm->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004533 txn->srv_cookie == NULL &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004534 (p4 - p1 >= t->be->fiprm->capture_namelen) &&
4535 memcmp(p1, t->be->fiprm->capture_name, t->be->fiprm->capture_namelen) == 0) {
4536 int log_len = p4 - p1;
4537
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004538 if ((txn->srv_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004539 Alert("HTTP logging : out of memory.\n");
4540 }
4541
4542 if (log_len > t->be->fiprm->capture_len)
4543 log_len = t->be->fiprm->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004544 memcpy(txn->srv_cookie, p1, log_len);
4545 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004546 }
4547
4548 /* now check if we need to process it for persistence */
4549 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
4550 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
4551 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004552 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004553
4554 /* If the cookie is in insert mode on a known server, we'll delete
4555 * this occurrence because we'll insert another one later.
4556 * We'll delete it too if the "indirect" option is set and we're in
4557 * a direct access. */
4558 if (((t->srv) && (t->be->beprm->options & PR_O_COOK_INS)) ||
4559 ((t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_IND))) {
4560 /* this header must be deleted */
4561 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4562 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4563 txn->hdr_idx.used--;
4564 cur_hdr->len = 0;
4565 cur_next += delta;
4566 txn->rsp.eoh += delta;
4567
Willy Tarreau3d300592007-03-18 18:34:41 +01004568 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004569 }
4570 else if ((t->srv) && (t->srv->cookie) &&
4571 (t->be->beprm->options & PR_O_COOK_RW)) {
4572 /* replace bytes p3->p4 with the cookie name associated
4573 * with this server since we know it.
4574 */
4575 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4576 cur_hdr->len += delta;
4577 cur_next += delta;
4578 txn->rsp.eoh += delta;
4579
Willy Tarreau3d300592007-03-18 18:34:41 +01004580 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004581 }
4582 else if ((t->srv) && (t->srv->cookie) &&
4583 (t->be->beprm->options & PR_O_COOK_PFX)) {
4584 /* insert the cookie name associated with this server
4585 * before existing cookie, and insert a delimitor between them..
4586 */
4587 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4588 cur_hdr->len += delta;
4589 cur_next += delta;
4590 txn->rsp.eoh += delta;
4591
4592 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004593 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004594 }
4595 }
4596 /* next, let's see if the cookie is our appcookie */
4597 else if ((t->be->beprm->appsession_name != NULL) &&
4598 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
4599
4600 /* Cool... it's the right one */
4601
4602 size_t server_id_len = strlen(t->srv->id) + 1;
4603 asession_temp = &local_asession;
4604
4605 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4606 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4607 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4608 return;
4609 }
4610 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4611 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
4612 asession_temp->serverid = NULL;
4613
4614 /* only do insert, if lookup fails */
4615 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
4616 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4617 Alert("Not enough Memory process_srv():asession:calloc().\n");
4618 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4619 return;
4620 }
4621 asession_temp->sessid = local_asession.sessid;
4622 asession_temp->serverid = local_asession.serverid;
4623 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
4624 }/* end if (chtbl_lookup()) */
4625 else {
4626 /* free wasted memory */
4627 pool_free_to(apools.sessid, local_asession.sessid);
4628 } /* end else from if (chtbl_lookup()) */
4629
4630 if (asession_temp->serverid == NULL) {
4631 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
4632 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4633 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4634 return;
4635 }
4636 asession_temp->serverid[0] = '\0';
4637 }
4638
4639 if (asession_temp->serverid[0] == '\0')
4640 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4641
4642 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
4643
4644#if defined(DEBUG_HASH)
4645 print_table(&(t->be->beprm->htbl_proxy));
4646#endif
4647 }/* end if ((t->proxy->appsession_name != NULL) ... */
4648 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4649 } /* we're now at the end of the cookie value */
4650
4651 /* keep the link from this header to next one */
4652 old_idx = cur_idx;
4653 } /* end of cookie processing on this header */
4654}
4655
4656
4657
4658/*
4659 * Check if response is cacheable or not. Updates t->flags.
4660 */
4661void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4662{
4663 struct http_txn *txn = &t->txn;
4664 char *p1, *p2;
4665
4666 char *cur_ptr, *cur_end, *cur_next;
4667 int cur_idx;
4668
Willy Tarreau3d300592007-03-18 18:34:41 +01004669 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004670 return;
4671
4672 /* Iterate through the headers.
4673 * we start with the start line.
4674 */
4675 cur_idx = 0;
4676 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4677
4678 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4679 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004680 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004681
4682 cur_hdr = &txn->hdr_idx.v[cur_idx];
4683 cur_ptr = cur_next;
4684 cur_end = cur_ptr + cur_hdr->len;
4685 cur_next = cur_end + cur_hdr->cr + 1;
4686
4687 /* We have one full header between cur_ptr and cur_end, and the
4688 * next header starts at cur_next. We're only interested in
4689 * "Cookie:" headers.
4690 */
4691
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004692 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4693 if (val) {
4694 if ((cur_end - (cur_ptr + val) >= 8) &&
4695 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4696 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4697 return;
4698 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004699 }
4700
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004701 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4702 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004703 continue;
4704
4705 /* OK, right now we know we have a cache-control header at cur_ptr */
4706
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004707 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004708
4709 if (p1 >= cur_end) /* no more info */
4710 continue;
4711
4712 /* p1 is at the beginning of the value */
4713 p2 = p1;
4714
4715 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4716 p2++;
4717
4718 /* we have a complete value between p1 and p2 */
4719 if (p2 < cur_end && *p2 == '=') {
4720 /* we have something of the form no-cache="set-cookie" */
4721 if ((cur_end - p1 >= 21) &&
4722 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4723 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004724 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004725 continue;
4726 }
4727
4728 /* OK, so we know that either p2 points to the end of string or to a comma */
4729 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4730 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4731 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4732 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004733 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004734 return;
4735 }
4736
4737 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004738 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004739 continue;
4740 }
4741 }
4742}
4743
4744
Willy Tarreau58f10d72006-12-04 02:26:12 +01004745/*
4746 * Try to retrieve a known appsession in the URI, then the associated server.
4747 * If the server is found, it's assigned to the session.
4748 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004749void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004750{
Willy Tarreau3d300592007-03-18 18:34:41 +01004751 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004752 appsess *asession_temp = NULL;
4753 appsess local_asession;
4754 char *request_line;
4755
Willy Tarreau830ff452006-12-17 19:31:23 +01004756 if (t->be->beprm->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004757 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004758 (request_line = memchr(begin, ';', len)) == NULL ||
4759 ((1 + t->be->beprm->appsession_name_len + 1 + t->be->beprm->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004760 return;
4761
4762 /* skip ';' */
4763 request_line++;
4764
4765 /* look if we have a jsessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004766 if (strncasecmp(request_line, t->be->beprm->appsession_name, t->be->beprm->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004767 return;
4768
4769 /* skip jsessionid= */
Willy Tarreau830ff452006-12-17 19:31:23 +01004770 request_line += t->be->beprm->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004771
4772 /* First try if we already have an appsession */
4773 asession_temp = &local_asession;
4774
4775 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4776 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4777 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4778 return;
4779 }
4780
4781 /* Copy the sessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004782 memcpy(asession_temp->sessid, request_line, t->be->beprm->appsession_len);
4783 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004784 asession_temp->serverid = NULL;
4785
4786 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004787 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004788 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4789 /* free previously allocated memory */
4790 pool_free_to(apools.sessid, local_asession.sessid);
4791 Alert("Not enough memory process_cli():asession:calloc().\n");
4792 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4793 return;
4794 }
4795 asession_temp->sessid = local_asession.sessid;
4796 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004797 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004798 }
4799 else {
4800 /* free previously allocated memory */
4801 pool_free_to(apools.sessid, local_asession.sessid);
4802 }
4803
Willy Tarreau830ff452006-12-17 19:31:23 +01004804 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004805 asession_temp->request_count++;
4806
4807#if defined(DEBUG_HASH)
4808 print_table(&(t->proxy->htbl_proxy));
4809#endif
4810 if (asession_temp->serverid == NULL) {
4811 Alert("Found Application Session without matching server.\n");
4812 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004813 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004814 while (srv) {
4815 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004816 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004817 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004818 txn->flags &= ~TX_CK_MASK;
4819 txn->flags |= TX_CK_VALID;
4820 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004821 t->srv = srv;
4822 break;
4823 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004824 txn->flags &= ~TX_CK_MASK;
4825 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004826 }
4827 }
4828 srv = srv->next;
4829 }
4830 }
4831}
4832
4833
Willy Tarreaub2513902006-12-17 14:52:38 +01004834/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004835 * In a GET or HEAD request, check if the requested URI matches the stats uri
4836 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004837 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004838 * It is assumed that the request is either a HEAD or GET and that the
4839 * t->be->fiprm->uri_auth field is valid. An HTTP/401 response may be sent, or
4840 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004841 *
4842 * Returns 1 if the session's state changes, otherwise 0.
4843 */
4844int stats_check_uri_auth(struct session *t, struct proxy *backend)
4845{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004846 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004847 struct uri_auth *uri_auth = backend->uri_auth;
4848 struct user_auth *user;
4849 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004850 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004851
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004852 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004853 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004854 return 0;
4855
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004856 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004857
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004858 /* the URI is in h */
4859 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004860 return 0;
4861
4862 /* we are in front of a interceptable URI. Let's check
4863 * if there's an authentication and if it's valid.
4864 */
4865 user = uri_auth->users;
4866 if (!user) {
4867 /* no user auth required, it's OK */
4868 authenticated = 1;
4869 } else {
4870 authenticated = 0;
4871
4872 /* a user list is defined, we have to check.
4873 * skip 21 chars for "Authorization: Basic ".
4874 */
4875
4876 /* FIXME: this should move to an earlier place */
4877 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004878 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4879 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4880 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004881 if (len > 14 &&
4882 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004883 txn->auth_hdr.str = h;
4884 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004885 break;
4886 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004887 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004888 }
4889
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004890 if (txn->auth_hdr.len < 21 ||
4891 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004892 user = NULL;
4893
4894 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004895 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4896 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004897 user->user_pwd, user->user_len)) {
4898 authenticated = 1;
4899 break;
4900 }
4901 user = user->next;
4902 }
4903 }
4904
4905 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004906 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004907
4908 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004909 msg.str = trash;
4910 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004911 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004912 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004913 if (!(t->flags & SN_ERR_MASK))
4914 t->flags |= SN_ERR_PRXCOND;
4915 if (!(t->flags & SN_FINST_MASK))
4916 t->flags |= SN_FINST_R;
4917 return 1;
4918 }
4919
4920 /* The request is valid, the user is authenticate. Let's start sending
4921 * data.
4922 */
4923 t->cli_state = CL_STSHUTR;
4924 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
4925 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
4926 t->data_source = DATA_SRC_STATS;
4927 t->data_state = DATA_ST_INIT;
4928 produce_content(t);
4929 return 1;
4930}
4931
4932
Willy Tarreaubaaee002006-06-26 02:48:02 +02004933/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004934 * Print a debug line with a header
4935 */
4936void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4937{
4938 int len, max;
4939 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4940 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4941 max = end - start;
4942 UBOUND(max, sizeof(trash) - len - 1);
4943 len += strlcpy2(trash + len, start, max + 1);
4944 trash[len++] = '\n';
4945 write(1, trash, len);
4946}
4947
4948
4949/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004950 * Local variables:
4951 * c-indent-level: 8
4952 * c-basic-offset: 8
4953 * End:
4954 */