blob: d3bc5f6659a519ea9fc7054ef7b2d57c28f382db [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) {
1659 int len;
1660 unsigned char *pn;
1661 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001662 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
Willy Tarreau2a324282006-12-05 00:05:46 +01001663 pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001664
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001665 if (unlikely(http_header_add_tail2(req, &txn->req,
1666 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001667 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001668 }
1669 else if (t->cli_addr.ss_family == AF_INET6) {
1670 int len;
1671 char pn[INET6_ADDRSTRLEN];
1672 inet_ntop(AF_INET6,
1673 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1674 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001675 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1676 if (unlikely(http_header_add_tail2(req, &txn->req,
1677 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001678 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001679 }
1680 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001681
Willy Tarreau2a324282006-12-05 00:05:46 +01001682 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreaub2513902006-12-17 14:52:38 +01001684 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
1686 !(t->flags & SN_CONN_CLOSED)) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001687 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
1688 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001689 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001690 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001691 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001692
Willy Tarreau2a324282006-12-05 00:05:46 +01001693 /*************************************************************
1694 * OK, that's finished for the headers. We have done what we *
1695 * could. Let's switch to the DATA state. *
1696 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001697
Willy Tarreau2a324282006-12-05 00:05:46 +01001698 t->cli_state = CL_STDATA;
1699 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001700
Willy Tarreau2a324282006-12-05 00:05:46 +01001701 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001702
Willy Tarreau2a324282006-12-05 00:05:46 +01001703 if (!t->fe->clitimeout ||
Willy Tarreau830ff452006-12-17 19:31:23 +01001704 (t->srv_state < SV_STDATA && t->be->beprm->srvtimeout)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001705 /* If the client has no timeout, or if the server is not ready yet,
1706 * and we know for sure that it can expire, then it's cleaner to
1707 * disable the timeout on the client side so that too low values
1708 * cannot make the sessions abort too early.
1709 *
1710 * FIXME-20050705: the server needs a way to re-enable this time-out
1711 * when it switches its state, otherwise a client can stay connected
1712 * indefinitely. This now seems to be OK.
1713 */
1714 tv_eternity(&req->rex);
1715 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001716
Willy Tarreau2a324282006-12-05 00:05:46 +01001717 /* When a connection is tarpitted, we use the queue timeout for the
1718 * tarpit delay, which currently happens to be the server's connect
1719 * timeout. If unset, then set it to zero because we really want it
1720 * to expire at one moment.
1721 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001722 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001723 t->req->l = 0;
1724 /* flush the request so that we can drop the connection early
1725 * if the client closes first.
1726 */
1727 tv_delayfrom(&req->cex, &now,
Willy Tarreau830ff452006-12-17 19:31:23 +01001728 t->be->beprm->contimeout ? t->be->beprm->contimeout : 0);
Willy Tarreau2a324282006-12-05 00:05:46 +01001729 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01001732 goto process_data;
1733
1734 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001735 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001736 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01001737 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001738 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01001739 return_prx_cond:
1740 if (!(t->flags & SN_ERR_MASK))
1741 t->flags |= SN_ERR_PRXCOND;
1742 if (!(t->flags & SN_FINST_MASK))
1743 t->flags |= SN_FINST_R;
1744 return 1;
1745
Willy Tarreaubaaee002006-06-26 02:48:02 +02001746 }
1747 else if (c == CL_STDATA) {
1748 process_data:
1749 /* FIXME: this error handling is partly buggy because we always report
1750 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1751 * or HEADER phase. BTW, it's not logical to expire the client while
1752 * we're waiting for the server to connect.
1753 */
1754 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001755 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001756 tv_eternity(&req->rex);
1757 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001758 fd_delete(t->cli_fd);
1759 t->cli_state = CL_STCLOSE;
1760 if (!(t->flags & SN_ERR_MASK))
1761 t->flags |= SN_ERR_CLICL;
1762 if (!(t->flags & SN_FINST_MASK)) {
1763 if (t->pend_pos)
1764 t->flags |= SN_FINST_Q;
1765 else if (s == SV_STCONN)
1766 t->flags |= SN_FINST_C;
1767 else
1768 t->flags |= SN_FINST_D;
1769 }
1770 return 1;
1771 }
1772 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001773 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001774 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001775 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001776 shutdown(t->cli_fd, SHUT_RD);
1777 t->cli_state = CL_STSHUTR;
1778 return 1;
1779 }
1780 /* last server read and buffer empty */
1781 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001782 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001783 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001784 shutdown(t->cli_fd, SHUT_WR);
1785 /* We must ensure that the read part is still alive when switching
1786 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001787 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001788 if (t->fe->clitimeout)
1789 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001790 t->cli_state = CL_STSHUTW;
1791 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1792 return 1;
1793 }
1794 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001795 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001796 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001797 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001798 shutdown(t->cli_fd, SHUT_RD);
1799 t->cli_state = CL_STSHUTR;
1800 if (!(t->flags & SN_ERR_MASK))
1801 t->flags |= SN_ERR_CLITO;
1802 if (!(t->flags & SN_FINST_MASK)) {
1803 if (t->pend_pos)
1804 t->flags |= SN_FINST_Q;
1805 else if (s == SV_STCONN)
1806 t->flags |= SN_FINST_C;
1807 else
1808 t->flags |= SN_FINST_D;
1809 }
1810 return 1;
1811 }
1812 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001813 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001814 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001815 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001816 shutdown(t->cli_fd, SHUT_WR);
1817 /* We must ensure that the read part is still alive when switching
1818 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001819 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001820 if (t->fe->clitimeout)
1821 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001822
1823 t->cli_state = CL_STSHUTW;
1824 if (!(t->flags & SN_ERR_MASK))
1825 t->flags |= SN_ERR_CLITO;
1826 if (!(t->flags & SN_FINST_MASK)) {
1827 if (t->pend_pos)
1828 t->flags |= SN_FINST_Q;
1829 else if (s == SV_STCONN)
1830 t->flags |= SN_FINST_C;
1831 else
1832 t->flags |= SN_FINST_D;
1833 }
1834 return 1;
1835 }
1836
1837 if (req->l >= req->rlim - req->data) {
1838 /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02001839 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001840 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02001841 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001842 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001843 }
1844 } else {
1845 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02001846 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
1847 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001848 if (!t->fe->clitimeout ||
Willy Tarreau830ff452006-12-17 19:31:23 +01001849 (t->srv_state < SV_STDATA && t->be->beprm->srvtimeout))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001850 /* If the client has no timeout, or if the server not ready yet, and we
1851 * know for sure that it can expire, then it's cleaner to disable the
1852 * timeout on the client side so that too low values cannot make the
1853 * sessions abort too early.
1854 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001855 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001856 else
Willy Tarreau73de9892006-11-30 11:40:23 +01001857 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001858 }
1859 }
1860
1861 if ((rep->l == 0) ||
1862 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001863 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1864 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001865 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001866 }
1867 } else {
1868 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001869 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1870 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001871 if (t->fe->clitimeout) {
1872 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001873 /* FIXME: to prevent the client from expiring read timeouts during writes,
1874 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001875 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001876 }
1877 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001878 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001879 }
1880 }
1881 return 0; /* other cases change nothing */
1882 }
1883 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001884 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001885 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001886 fd_delete(t->cli_fd);
1887 t->cli_state = CL_STCLOSE;
1888 if (!(t->flags & SN_ERR_MASK))
1889 t->flags |= SN_ERR_CLICL;
1890 if (!(t->flags & SN_FINST_MASK)) {
1891 if (t->pend_pos)
1892 t->flags |= SN_FINST_Q;
1893 else if (s == SV_STCONN)
1894 t->flags |= SN_FINST_C;
1895 else
1896 t->flags |= SN_FINST_D;
1897 }
1898 return 1;
1899 }
1900 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
1901 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001902 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001903 fd_delete(t->cli_fd);
1904 t->cli_state = CL_STCLOSE;
1905 return 1;
1906 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001907 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
1908 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001909 fd_delete(t->cli_fd);
1910 t->cli_state = CL_STCLOSE;
1911 if (!(t->flags & SN_ERR_MASK))
1912 t->flags |= SN_ERR_CLITO;
1913 if (!(t->flags & SN_FINST_MASK)) {
1914 if (t->pend_pos)
1915 t->flags |= SN_FINST_Q;
1916 else if (s == SV_STCONN)
1917 t->flags |= SN_FINST_C;
1918 else
1919 t->flags |= SN_FINST_D;
1920 }
1921 return 1;
1922 }
1923
1924 if (t->flags & SN_SELF_GEN) {
1925 produce_content(t);
1926 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001927 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001928 fd_delete(t->cli_fd);
1929 t->cli_state = CL_STCLOSE;
1930 return 1;
1931 }
1932 }
1933
1934 if ((rep->l == 0)
1935 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001936 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1937 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001938 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001939 }
1940 } else {
1941 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001942 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1943 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001944 if (t->fe->clitimeout) {
1945 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001946 /* FIXME: to prevent the client from expiring read timeouts during writes,
1947 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001948 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001949 }
1950 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001951 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001952 }
1953 }
1954 return 0;
1955 }
1956 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001957 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001958 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001959 fd_delete(t->cli_fd);
1960 t->cli_state = CL_STCLOSE;
1961 if (!(t->flags & SN_ERR_MASK))
1962 t->flags |= SN_ERR_CLICL;
1963 if (!(t->flags & SN_FINST_MASK)) {
1964 if (t->pend_pos)
1965 t->flags |= SN_FINST_Q;
1966 else if (s == SV_STCONN)
1967 t->flags |= SN_FINST_C;
1968 else
1969 t->flags |= SN_FINST_D;
1970 }
1971 return 1;
1972 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001973 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001974 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001975 fd_delete(t->cli_fd);
1976 t->cli_state = CL_STCLOSE;
1977 return 1;
1978 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001979 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
1980 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001981 fd_delete(t->cli_fd);
1982 t->cli_state = CL_STCLOSE;
1983 if (!(t->flags & SN_ERR_MASK))
1984 t->flags |= SN_ERR_CLITO;
1985 if (!(t->flags & SN_FINST_MASK)) {
1986 if (t->pend_pos)
1987 t->flags |= SN_FINST_Q;
1988 else if (s == SV_STCONN)
1989 t->flags |= SN_FINST_C;
1990 else
1991 t->flags |= SN_FINST_D;
1992 }
1993 return 1;
1994 }
1995 else if (req->l >= req->rlim - req->data) {
1996 /* no room to read more data */
1997
1998 /* FIXME-20050705: is it possible for a client to maintain a session
1999 * after the timeout by sending more data after it receives a close ?
2000 */
2001
Willy Tarreau2a429502006-10-15 14:52:29 +02002002 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002003 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02002004 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002005 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002006 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2007 }
2008 } else {
2009 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02002010 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
2011 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002012 if (t->fe->clitimeout)
2013 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002014 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002015 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002016 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2017 }
2018 }
2019 return 0;
2020 }
2021 else { /* CL_STCLOSE: nothing to do */
2022 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2023 int len;
Willy Tarreau830ff452006-12-17 19:31:23 +01002024 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 +02002025 write(1, trash, len);
2026 }
2027 return 0;
2028 }
2029 return 0;
2030}
2031
2032
2033/*
2034 * manages the server FSM and its socket. It returns 1 if a state has changed
2035 * (and a resync may be needed), 0 else.
2036 */
2037int process_srv(struct session *t)
2038{
2039 int s = t->srv_state;
2040 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002041 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002042 struct buffer *req = t->req;
2043 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002044 int conn_err;
2045
2046#ifdef DEBUG_FULL
2047 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2048#endif
2049 //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 +02002050 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
2051 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002052 //);
2053 if (s == SV_STIDLE) {
2054 if (c == CL_STHEADERS)
2055 return 0; /* stay in idle, waiting for data to reach the client side */
2056 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2057 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002058 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002059 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002060 if (t->pend_pos)
2061 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2062 /* note that this must not return any error because it would be able to
2063 * overwrite the client_retnclose() output.
2064 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002065 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002066 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002067 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002068 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 +02002069
2070 return 1;
2071 }
2072 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002073 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002074 /* This connection is being tarpitted. The CLIENT side has
2075 * already set the connect expiration date to the right
2076 * timeout. We just have to check that it has not expired.
2077 */
2078 if (tv_cmp2_ms(&req->cex, &now) > 0)
2079 return 0;
2080
2081 /* We will set the queue timer to the time spent, just for
2082 * logging purposes. We fake a 500 server error, so that the
2083 * attacker will not suspect his connection has been tarpitted.
2084 * It will not cause trouble to the logs because we can exclude
2085 * the tarpitted connections by filtering on the 'PT' status flags.
2086 */
2087 tv_eternity(&req->cex);
2088 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2089 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002090 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002091 return 1;
2092 }
2093
Willy Tarreaubaaee002006-06-26 02:48:02 +02002094 /* Right now, we will need to create a connection to the server.
2095 * We might already have tried, and got a connection pending, in
2096 * which case we will not do anything till it's pending. It's up
2097 * to any other session to release it and wake us up again.
2098 */
2099 if (t->pend_pos) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002100 if (tv_cmp2_ms(&req->cex, &now) > 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002101 return 0;
2102 else {
2103 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002104 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002105 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2106 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002107 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002108 if (t->srv)
2109 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002110 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002111 return 1;
2112 }
2113 }
2114
2115 do {
2116 /* first, get a connection */
2117 if (srv_redispatch_connect(t))
2118 return t->srv_state != SV_STIDLE;
2119
2120 /* try to (re-)connect to the server, and fail if we expire the
2121 * number of retries.
2122 */
2123 if (srv_retryable_connect(t)) {
2124 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2125 return t->srv_state != SV_STIDLE;
2126 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002127 } while (1);
2128 }
2129 }
2130 else if (s == SV_STCONN) { /* connection in progress */
2131 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2132 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002133 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002134 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002135 fd_delete(t->srv_fd);
2136 if (t->srv)
2137 t->srv->cur_sess--;
2138
2139 /* note that this must not return any error because it would be able to
2140 * overwrite the client_retnclose() output.
2141 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002142 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002143 return 1;
2144 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002145 if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) {
2146 //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 +02002147 return 0; /* nothing changed */
2148 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002149 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002150 /* timeout, asynchronous connect error or first write error */
2151 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2152
2153 fd_delete(t->srv_fd);
2154 if (t->srv)
2155 t->srv->cur_sess--;
2156
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002157 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002158 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2159 else
2160 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2161
2162 /* ensure that we have enough retries left */
2163 if (srv_count_retry_down(t, conn_err))
2164 return 1;
2165
Willy Tarreau830ff452006-12-17 19:31:23 +01002166 if (t->srv && t->conn_retries == 0 && t->be->beprm->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002167 /* We're on our last chance, and the REDISP option was specified.
2168 * We will ignore cookie and force to balance or use the dispatcher.
2169 */
2170 /* let's try to offer this slot to anybody */
Willy Tarreau830ff452006-12-17 19:31:23 +01002171 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002172 task_wakeup(&rq, t->srv->queue_mgt);
2173
2174 if (t->srv)
2175 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +01002176 t->be->beprm->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002177
2178 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2179 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002180 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002181
2182 /* first, get a connection */
2183 if (srv_redispatch_connect(t))
2184 return t->srv_state != SV_STIDLE;
2185 }
2186
Willy Tarreaubaaee002006-06-26 02:48:02 +02002187 do {
2188 /* Now we will try to either reconnect to the same server or
2189 * connect to another server. If the connection gets queued
2190 * because all servers are saturated, then we will go back to
2191 * the SV_STIDLE state.
2192 */
2193 if (srv_retryable_connect(t)) {
2194 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2195 return t->srv_state != SV_STCONN;
2196 }
2197
2198 /* we need to redispatch the connection to another server */
2199 if (srv_redispatch_connect(t))
2200 return t->srv_state != SV_STCONN;
2201 } while (1);
2202 }
2203 else { /* no error or write 0 */
2204 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
2205
2206 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2207 if (req->l == 0) /* nothing to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002208 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002209 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002210 } else /* need the right to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002211 MY_FD_SET(t->srv_fd, StaticWriteEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002212 if (t->be->beprm->srvtimeout) {
2213 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002214 /* FIXME: to prevent the server from expiring read timeouts during writes,
2215 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002216 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002217 }
2218 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002219 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002220 }
2221
Willy Tarreau830ff452006-12-17 19:31:23 +01002222 if (t->be->beprm->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau2a429502006-10-15 14:52:29 +02002223 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002224 if (t->be->beprm->srvtimeout)
2225 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002226 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002227 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228
2229 t->srv_state = SV_STDATA;
2230 if (t->srv)
2231 t->srv->cum_sess++;
2232 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2233
2234 /* if the user wants to log as soon as possible, without counting
2235 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002236 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002237 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
2238 sess_log(t);
2239 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002240#ifdef CONFIG_HAP_TCPSPLICE
2241 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2242 /* TCP splicing supported by both FE and BE */
2243 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2244 }
2245#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002246 }
2247 else {
2248 t->srv_state = SV_STHEADERS;
2249 if (t->srv)
2250 t->srv->cum_sess++;
2251 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002252 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2253 /* reset hdr_idx which was already initialized by the request.
2254 * right now, the http parser does it.
2255 * hdr_idx_init(&t->txn.hdr_idx);
2256 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002257 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002258 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002259 return 1;
2260 }
2261 }
2262 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002263 /*
2264 * Now parse the partial (or complete) lines.
2265 * We will check the response syntax, and also join multi-line
2266 * headers. An index of all the lines will be elaborated while
2267 * parsing.
2268 *
2269 * For the parsing, we use a 28 states FSM.
2270 *
2271 * Here is the information we currently have :
2272 * rep->data + req->som = beginning of response
2273 * rep->data + req->eoh = end of processed headers / start of current one
2274 * rep->data + req->eol = end of current header or line (LF or CRLF)
2275 * rep->lr = first non-visited byte
2276 * rep->r = end of data
2277 */
2278
2279 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002280 struct http_msg *msg = &txn->rsp;
2281 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002282
Willy Tarreaua15645d2007-03-18 16:22:39 +01002283 if (likely(rep->lr < rep->r))
2284 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002285
Willy Tarreaua15645d2007-03-18 16:22:39 +01002286 /* 1: we might have to print this header in debug mode */
2287 if (unlikely((global.mode & MODE_DEBUG) &&
2288 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2289 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2290 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002291
Willy Tarreaua15645d2007-03-18 16:22:39 +01002292 sol = rep->data + msg->som;
2293 eol = sol + msg->sl.rq.l;
2294 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002295
Willy Tarreaua15645d2007-03-18 16:22:39 +01002296 sol += hdr_idx_first_pos(&txn->hdr_idx);
2297 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002298
Willy Tarreaua15645d2007-03-18 16:22:39 +01002299 while (cur_idx) {
2300 eol = sol + txn->hdr_idx.v[cur_idx].len;
2301 debug_hdr("srvhdr", t, sol, eol);
2302 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2303 cur_idx = txn->hdr_idx.v[cur_idx].next;
2304 }
2305 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002306
Willy Tarreaubaaee002006-06-26 02:48:02 +02002307
Willy Tarreaua15645d2007-03-18 16:22:39 +01002308 if ((rep->l < rep->rlim - rep->data) && ! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2309 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
2310 * full. We cannot loop here since stream_sock_read will disable it only if
2311 * rep->l == rlim-data
2312 */
2313 MY_FD_SET(t->srv_fd, StaticReadEvent);
2314 if (t->be->beprm->srvtimeout)
2315 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2316 else
2317 tv_eternity(&rep->rex);
2318 }
2319
2320
2321 /*
2322 * Now we quickly check if we have found a full valid response.
2323 * If not so, we check the FD and buffer states before leaving.
2324 * A full response is indicated by the fact that we have seen
2325 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2326 * responses are checked first.
2327 *
2328 * Depending on whether the client is still there or not, we
2329 * may send an error response back or not. Note that normally
2330 * we should only check for HTTP status there, and check I/O
2331 * errors somewhere else.
2332 */
2333
2334 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2335
2336 /* Invalid response, or read error or write error */
2337 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2338 (req->flags & BF_WRITE_ERROR) ||
2339 (rep->flags & BF_READ_ERROR))) {
2340 tv_eternity(&rep->rex);
2341 tv_eternity(&req->wex);
2342 fd_delete(t->srv_fd);
2343 if (t->srv) {
2344 t->srv->cur_sess--;
2345 t->srv->failed_resp++;
2346 }
2347 t->be->failed_resp++;
2348 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002349 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002350 client_return(t, error_message(t, HTTP_ERR_502));
2351 if (!(t->flags & SN_ERR_MASK))
2352 t->flags |= SN_ERR_SRVCL;
2353 if (!(t->flags & SN_FINST_MASK))
2354 t->flags |= SN_FINST_H;
2355 /* We used to have a free connection slot. Since we'll never use it,
2356 * we have to inform the server that it may be used by another session.
2357 */
2358 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2359 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002360
Willy Tarreaua15645d2007-03-18 16:22:39 +01002361 return 1;
2362 }
2363
2364 /* end of client write or end of server read.
2365 * since we are in header mode, if there's no space left for headers, we
2366 * won't be able to free more later, so the session will never terminate.
2367 */
2368 else if (unlikely(rep->flags & BF_READ_NULL ||
2369 c == CL_STSHUTW || c == CL_STCLOSE ||
2370 rep->l >= rep->rlim - rep->data)) {
2371 MY_FD_CLR(t->srv_fd, StaticReadEvent);
2372 tv_eternity(&rep->rex);
2373 shutdown(t->srv_fd, SHUT_RD);
2374 t->srv_state = SV_STSHUTR;
2375 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2376 return 1;
2377 }
2378
2379 /* read timeout : return a 504 to the client.
2380 */
2381 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticReadEvent) &&
2382 tv_cmp2_ms(&rep->rex, &now) <= 0)) {
2383 tv_eternity(&rep->rex);
2384 tv_eternity(&req->wex);
2385 fd_delete(t->srv_fd);
2386 if (t->srv) {
2387 t->srv->cur_sess--;
2388 t->srv->failed_resp++;
2389 }
2390 t->be->failed_resp++;
2391 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002392 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002393 client_return(t, error_message(t, HTTP_ERR_504));
2394 if (!(t->flags & SN_ERR_MASK))
2395 t->flags |= SN_ERR_SRVTO;
2396 if (!(t->flags & SN_FINST_MASK))
2397 t->flags |= SN_FINST_H;
2398 /* We used to have a free connection slot. Since we'll never use it,
2399 * we have to inform the server that it may be used by another session.
2400 */
2401 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2402 task_wakeup(&rq, t->srv->queue_mgt);
2403 return 1;
2404 }
2405
2406 /* last client read and buffer empty */
2407 /* FIXME!!! here, we don't want to switch to SHUTW if the
2408 * client shuts read too early, because we may still have
2409 * some work to do on the headers.
2410 * The side-effect is that if the client completely closes its
2411 * connection during SV_STHEADER, the connection to the server
2412 * is kept until a response comes back or the timeout is reached.
2413 */
2414 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2415 (req->l == 0))) {
2416 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2417 tv_eternity(&req->wex);
2418
2419 /* We must ensure that the read part is still
2420 * alive when switching to shutw */
2421 MY_FD_SET(t->srv_fd, StaticReadEvent);
2422 if (t->be->beprm->srvtimeout)
2423 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2424
2425 shutdown(t->srv_fd, SHUT_WR);
2426 t->srv_state = SV_STSHUTW;
2427 return 1;
2428 }
2429
2430 /* write timeout */
2431 /* FIXME!!! here, we don't want to switch to SHUTW if the
2432 * client shuts read too early, because we may still have
2433 * some work to do on the headers.
2434 */
2435 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticWriteEvent) &&
2436 tv_cmp2_ms(&req->wex, &now) <= 0)) {
2437 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2438 tv_eternity(&req->wex);
2439 shutdown(t->srv_fd, SHUT_WR);
2440 /* We must ensure that the read part is still alive
2441 * when switching to shutw */
2442 MY_FD_SET(t->srv_fd, StaticReadEvent);
2443 if (t->be->beprm->srvtimeout)
2444 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2445
2446 t->srv_state = SV_STSHUTW;
2447 if (!(t->flags & SN_ERR_MASK))
2448 t->flags |= SN_ERR_SRVTO;
2449 if (!(t->flags & SN_FINST_MASK))
2450 t->flags |= SN_FINST_H;
2451 return 1;
2452 }
2453
2454 /*
2455 * And now the non-error cases.
2456 */
2457
2458 /* Data remaining in the request buffer.
2459 * This happens during the first pass here, and during
2460 * long posts.
2461 */
2462 else if (likely(req->l)) {
2463 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2464 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2465 if (t->be->beprm->srvtimeout) {
2466 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
2467 /* FIXME: to prevent the server from expiring read timeouts during writes,
2468 * we refresh it. */
2469 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002470 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002471 else
2472 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002473 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002474 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002475
Willy Tarreaua15645d2007-03-18 16:22:39 +01002476 /* nothing left in the request buffer */
2477 else {
2478 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2479 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002480 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002481 }
2482 }
2483
2484 return t->srv_state != SV_STHEADERS;
2485 }
2486
2487
2488 /*****************************************************************
2489 * More interesting part now : we know that we have a complete *
2490 * response which at least looks like HTTP. We have an indicator *
2491 * of each header's length, so we can parse them quickly. *
2492 ****************************************************************/
2493
2494 /*
2495 * 1: get the status code and check for cacheability.
2496 */
2497
2498 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002499 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002500
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002501 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002502 case 200:
2503 case 203:
2504 case 206:
2505 case 300:
2506 case 301:
2507 case 410:
2508 /* RFC2616 @13.4:
2509 * "A response received with a status code of
2510 * 200, 203, 206, 300, 301 or 410 MAY be stored
2511 * by a cache (...) unless a cache-control
2512 * directive prohibits caching."
2513 *
2514 * RFC2616 @9.5: POST method :
2515 * "Responses to this method are not cacheable,
2516 * unless the response includes appropriate
2517 * Cache-Control or Expires header fields."
2518 */
2519 if (likely(txn->meth != HTTP_METH_POST) &&
2520 unlikely(t->be->beprm->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002521 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002522 break;
2523 default:
2524 break;
2525 }
2526
2527 /*
2528 * 2: we may need to capture headers
2529 */
2530 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->fiprm->rsp_cap))
2531 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2532 txn->rsp.cap, t->fe->fiprm->rsp_cap);
2533
2534 /*
2535 * 3: we will have to evaluate the filters.
2536 * As opposed to version 1.2, now they will be evaluated in the
2537 * filters order and not in the header order. This means that
2538 * each filter has to be validated among all headers.
2539 *
2540 * Filters are tried with ->be first, then with ->fe if it is
2541 * different from ->be.
2542 */
2543
2544 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2545
2546 cur_proxy = t->be;
2547 while (1) {
2548 struct proxy *rule_set = cur_proxy->fiprm;
2549
2550 /* try headers filters */
2551 if (rule_set->rsp_exp != NULL) {
2552 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2553 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002554 if (t->srv) {
2555 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002556 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002557 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002558 cur_proxy->beprm->failed_resp++;
2559 return_srv_prx_502:
2560 tv_eternity(&rep->rex);
2561 tv_eternity(&req->wex);
2562 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002563 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002564 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002565 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002566 if (!(t->flags & SN_ERR_MASK))
2567 t->flags |= SN_ERR_PRXCOND;
2568 if (!(t->flags & SN_FINST_MASK))
2569 t->flags |= SN_FINST_H;
2570 /* We used to have a free connection slot. Since we'll never use it,
2571 * we have to inform the server that it may be used by another session.
2572 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002573 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002574 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002575 return 1;
2576 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002577 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002578
Willy Tarreaua15645d2007-03-18 16:22:39 +01002579 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002580 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002581 if (t->srv) {
2582 t->srv->cur_sess--;
2583 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002584 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002585 cur_proxy->beprm->denied_resp++;
2586 goto return_srv_prx_502;
2587 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002588
Willy Tarreaua15645d2007-03-18 16:22:39 +01002589 /* We might have to check for "Connection:" */
2590 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
2591 !(t->flags & SN_CONN_CLOSED)) {
2592 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002593 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002594 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002595
Willy Tarreaua15645d2007-03-18 16:22:39 +01002596 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2597 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002598
Willy Tarreaua15645d2007-03-18 16:22:39 +01002599 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2600 cur_hdr = &txn->hdr_idx.v[cur_idx];
2601 cur_ptr = cur_next;
2602 cur_end = cur_ptr + cur_hdr->len;
2603 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002604
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002605 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2606 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002607 /* 3 possibilities :
2608 * - we have already set Connection: close,
2609 * so we remove this line.
2610 * - we have not yet set Connection: close,
2611 * but this line indicates close. We leave
2612 * it untouched and set the flag.
2613 * - we have not yet set Connection: close,
2614 * and this line indicates non-close. We
2615 * replace it.
2616 */
2617 if (t->flags & SN_CONN_CLOSED) {
2618 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2619 txn->rsp.eoh += delta;
2620 cur_next += delta;
2621 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2622 txn->hdr_idx.used--;
2623 cur_hdr->len = 0;
2624 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002625 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2626 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2627 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002628 cur_next += delta;
2629 cur_hdr->len += delta;
2630 txn->rsp.eoh += delta;
2631 }
2632 t->flags |= SN_CONN_CLOSED;
2633 }
2634 }
2635 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002636 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002637 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002638
Willy Tarreaua15645d2007-03-18 16:22:39 +01002639 /* add response headers from the rule sets in the same order */
2640 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002641 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2642 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002643 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002644 }
2645
Willy Tarreaua15645d2007-03-18 16:22:39 +01002646 /* check whether we're already working on the frontend */
2647 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002648 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002649 cur_proxy = t->fe;
2650 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002651
Willy Tarreaua15645d2007-03-18 16:22:39 +01002652 /*
2653 * 4: check for server cookie.
2654 */
2655 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002656
Willy Tarreaua15645d2007-03-18 16:22:39 +01002657 /*
2658 * 5: add server cookie in the response if needed
2659 */
2660 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_INS) &&
2661 (!(t->be->beprm->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2662 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002663
Willy Tarreaua15645d2007-03-18 16:22:39 +01002664 /* the server is known, it's not the one the client requested, we have to
2665 * insert a set-cookie here, except if we want to insert only on POST
2666 * requests and this one isn't. Note that servers which don't have cookies
2667 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002668 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002669 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaua15645d2007-03-18 16:22:39 +01002670 t->be->beprm->cookie_name,
2671 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002672
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002673 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2674 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002675 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002676 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002677
Willy Tarreaua15645d2007-03-18 16:22:39 +01002678 /* Here, we will tell an eventual cache on the client side that we don't
2679 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2680 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2681 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2682 */
2683 if (t->be->beprm->options & PR_O_COOK_NOC) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002684 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2685 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002686 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002687 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002688 }
2689
2690
2691 /*
2692 * 6: check for cache-control or pragma headers.
2693 */
2694 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002695
Willy Tarreaubaaee002006-06-26 02:48:02 +02002696
Willy Tarreaua15645d2007-03-18 16:22:39 +01002697 /*
2698 * 7: check if result will be cacheable with a cookie.
2699 * We'll block the response if security checks have caught
2700 * nasty things such as a cacheable cookie.
2701 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002702 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2703 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002704 (t->be->beprm->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002705
Willy Tarreaua15645d2007-03-18 16:22:39 +01002706 /* we're in presence of a cacheable response containing
2707 * a set-cookie header. We'll block it as requested by
2708 * the 'checkcache' option, and send an alert.
2709 */
2710 if (t->srv) {
2711 t->srv->cur_sess--;
2712 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002713 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002714 t->be->beprm->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002715
Willy Tarreaua15645d2007-03-18 16:22:39 +01002716 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2717 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2718 send_log(t->be, LOG_ALERT,
2719 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2720 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2721 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002722 }
2723
Willy Tarreaua15645d2007-03-18 16:22:39 +01002724 /*
2725 * 8: add "Connection: close" if needed and not yet set.
2726 */
2727 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
2728 !(t->flags & SN_CONN_CLOSED)) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002729 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2730 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002731 goto return_bad_resp;
2732 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002733 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002734
Willy Tarreaubaaee002006-06-26 02:48:02 +02002735
Willy Tarreaua15645d2007-03-18 16:22:39 +01002736 /*************************************************************
2737 * OK, that's finished for the headers. We have done what we *
2738 * could. Let's switch to the DATA state. *
2739 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002740
Willy Tarreaua15645d2007-03-18 16:22:39 +01002741 t->srv_state = SV_STDATA;
2742 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
2743 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
2744
2745 /* client connection already closed or option 'forceclose' required :
2746 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002747 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002748 if ((req->l == 0) &&
2749 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->beprm->options & PR_O_FORCE_CLO)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002750 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002751 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002752
2753 /* We must ensure that the read part is still alive when switching
2754 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002755 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002756 if (t->be->beprm->srvtimeout)
2757 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002758
Willy Tarreaua15645d2007-03-18 16:22:39 +01002759 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002760 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002761 }
2762
Willy Tarreaua15645d2007-03-18 16:22:39 +01002763#ifdef CONFIG_HAP_TCPSPLICE
2764 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2765 /* TCP splicing supported by both FE and BE */
2766 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002767 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002768#endif
2769 /* if the user wants to log as soon as possible, without counting
2770 bytes from the server, then this is the right moment. */
2771 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2772 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002773 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002774 sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002775 }
2776
Willy Tarreaua15645d2007-03-18 16:22:39 +01002777 /* Note: we must not try to cheat by jumping directly to DATA,
2778 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002779 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002780
2781 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002782 }
2783 else if (s == SV_STDATA) {
2784 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002785 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002786 tv_eternity(&rep->rex);
2787 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002788 fd_delete(t->srv_fd);
2789 if (t->srv) {
2790 t->srv->cur_sess--;
2791 t->srv->failed_resp++;
2792 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002793 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002794 t->srv_state = SV_STCLOSE;
2795 if (!(t->flags & SN_ERR_MASK))
2796 t->flags |= SN_ERR_SRVCL;
2797 if (!(t->flags & SN_FINST_MASK))
2798 t->flags |= SN_FINST_D;
2799 /* We used to have a free connection slot. Since we'll never use it,
2800 * we have to inform the server that it may be used by another session.
2801 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002802 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002803 task_wakeup(&rq, t->srv->queue_mgt);
2804
2805 return 1;
2806 }
2807 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002808 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002809 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002810 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002811 shutdown(t->srv_fd, SHUT_RD);
2812 t->srv_state = SV_STSHUTR;
2813 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2814 return 1;
2815 }
2816 /* end of client read and no more data to send */
2817 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002818 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002819 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002820 shutdown(t->srv_fd, SHUT_WR);
2821 /* We must ensure that the read part is still alive when switching
2822 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002823 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002824 if (t->be->beprm->srvtimeout)
2825 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002826
2827 t->srv_state = SV_STSHUTW;
2828 return 1;
2829 }
2830 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002831 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002832 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002833 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002834 shutdown(t->srv_fd, SHUT_RD);
2835 t->srv_state = SV_STSHUTR;
2836 if (!(t->flags & SN_ERR_MASK))
2837 t->flags |= SN_ERR_SRVTO;
2838 if (!(t->flags & SN_FINST_MASK))
2839 t->flags |= SN_FINST_D;
2840 return 1;
2841 }
2842 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002843 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002844 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002845 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002846 shutdown(t->srv_fd, SHUT_WR);
2847 /* We must ensure that the read part is still alive when switching
2848 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002849 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002850 if (t->be->beprm->srvtimeout)
2851 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002852 t->srv_state = SV_STSHUTW;
2853 if (!(t->flags & SN_ERR_MASK))
2854 t->flags |= SN_ERR_SRVTO;
2855 if (!(t->flags & SN_FINST_MASK))
2856 t->flags |= SN_FINST_D;
2857 return 1;
2858 }
2859
2860 /* recompute request time-outs */
2861 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002862 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2863 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002864 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002865 }
2866 }
2867 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau2a429502006-10-15 14:52:29 +02002868 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2869 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002870 if (t->be->beprm->srvtimeout) {
2871 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002872 /* FIXME: to prevent the server from expiring read timeouts during writes,
2873 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002874 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002875 }
2876 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002877 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002878 }
2879 }
2880
2881 /* recompute response time-outs */
2882 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002883 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2884 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002885 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002886 }
2887 }
2888 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02002889 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2890 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002891 if (t->be->beprm->srvtimeout)
2892 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002893 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002894 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895 }
2896 }
2897
2898 return 0; /* other cases change nothing */
2899 }
2900 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002901 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002902 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002903 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002904 fd_delete(t->srv_fd);
2905 if (t->srv) {
2906 t->srv->cur_sess--;
2907 t->srv->failed_resp++;
2908 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002909 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002910 //close(t->srv_fd);
2911 t->srv_state = SV_STCLOSE;
2912 if (!(t->flags & SN_ERR_MASK))
2913 t->flags |= SN_ERR_SRVCL;
2914 if (!(t->flags & SN_FINST_MASK))
2915 t->flags |= SN_FINST_D;
2916 /* We used to have a free connection slot. Since we'll never use it,
2917 * we have to inform the server that it may be used by another session.
2918 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002919 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002920 task_wakeup(&rq, t->srv->queue_mgt);
2921
2922 return 1;
2923 }
2924 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002925 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002926 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002927 fd_delete(t->srv_fd);
2928 if (t->srv)
2929 t->srv->cur_sess--;
2930 //close(t->srv_fd);
2931 t->srv_state = SV_STCLOSE;
2932 /* We used to have a free connection slot. Since we'll never use it,
2933 * we have to inform the server that it may be used by another session.
2934 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002935 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002936 task_wakeup(&rq, t->srv->queue_mgt);
2937
2938 return 1;
2939 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002940 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002941 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002942 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002943 fd_delete(t->srv_fd);
2944 if (t->srv)
2945 t->srv->cur_sess--;
2946 //close(t->srv_fd);
2947 t->srv_state = SV_STCLOSE;
2948 if (!(t->flags & SN_ERR_MASK))
2949 t->flags |= SN_ERR_SRVTO;
2950 if (!(t->flags & SN_FINST_MASK))
2951 t->flags |= SN_FINST_D;
2952 /* We used to have a free connection slot. Since we'll never use it,
2953 * we have to inform the server that it may be used by another session.
2954 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002955 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002956 task_wakeup(&rq, t->srv->queue_mgt);
2957
2958 return 1;
2959 }
2960 else if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002961 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2962 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002963 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002964 }
2965 }
2966 else { /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002967 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2968 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002969 if (t->be->beprm->srvtimeout) {
2970 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002971 /* FIXME: to prevent the server from expiring read timeouts during writes,
2972 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002973 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002974 }
2975 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002976 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002977 }
2978 }
2979 return 0;
2980 }
2981 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002982 if (rep->flags & BF_READ_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002983 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002984 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985 fd_delete(t->srv_fd);
2986 if (t->srv) {
2987 t->srv->cur_sess--;
2988 t->srv->failed_resp++;
2989 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002990 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002991 //close(t->srv_fd);
2992 t->srv_state = SV_STCLOSE;
2993 if (!(t->flags & SN_ERR_MASK))
2994 t->flags |= SN_ERR_SRVCL;
2995 if (!(t->flags & SN_FINST_MASK))
2996 t->flags |= SN_FINST_D;
2997 /* We used to have a free connection slot. Since we'll never use it,
2998 * we have to inform the server that it may be used by another session.
2999 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003000 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003001 task_wakeup(&rq, t->srv->queue_mgt);
3002
3003 return 1;
3004 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003005 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003006 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003007 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003008 fd_delete(t->srv_fd);
3009 if (t->srv)
3010 t->srv->cur_sess--;
3011 //close(t->srv_fd);
3012 t->srv_state = SV_STCLOSE;
3013 /* We used to have a free connection slot. Since we'll never use it,
3014 * we have to inform the server that it may be used by another session.
3015 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003016 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003017 task_wakeup(&rq, t->srv->queue_mgt);
3018
3019 return 1;
3020 }
Willy Tarreaud7971282006-07-29 18:36:34 +02003021 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003022 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003023 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003024 fd_delete(t->srv_fd);
3025 if (t->srv)
3026 t->srv->cur_sess--;
3027 //close(t->srv_fd);
3028 t->srv_state = SV_STCLOSE;
3029 if (!(t->flags & SN_ERR_MASK))
3030 t->flags |= SN_ERR_SRVTO;
3031 if (!(t->flags & SN_FINST_MASK))
3032 t->flags |= SN_FINST_D;
3033 /* We used to have a free connection slot. Since we'll never use it,
3034 * we have to inform the server that it may be used by another session.
3035 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003036 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003037 task_wakeup(&rq, t->srv->queue_mgt);
3038
3039 return 1;
3040 }
3041 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02003042 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3043 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003044 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003045 }
3046 }
3047 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02003048 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3049 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01003050 if (t->be->beprm->srvtimeout)
3051 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003052 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003053 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003054 }
3055 }
3056 return 0;
3057 }
3058 else { /* SV_STCLOSE : nothing to do */
3059 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3060 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003061 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
3062 t->uniq_id, t->be->beprm->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003063 write(1, trash, len);
3064 }
3065 return 0;
3066 }
3067 return 0;
3068}
3069
3070
3071/*
3072 * Produces data for the session <s> depending on its source. Expects to be
3073 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3074 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3075 * session, which it uses to keep on being called when there is free space in
3076 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3077 * if it changes the session state from CL_STSHUTR, otherwise 0.
3078 */
3079int produce_content(struct session *s)
3080{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003081 if (s->data_source == DATA_SRC_NONE) {
3082 s->flags &= ~SN_SELF_GEN;
3083 return 1;
3084 }
3085 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003086 /* dump server statistics */
3087 return produce_content_stats(s);
3088 }
3089 else {
3090 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003091 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003092 client_retnclose(s, error_message(s, HTTP_ERR_500));
3093 if (!(s->flags & SN_ERR_MASK))
3094 s->flags |= SN_ERR_PRXCOND;
3095 if (!(s->flags & SN_FINST_MASK))
3096 s->flags |= SN_FINST_R;
3097 s->flags &= ~SN_SELF_GEN;
3098 return 1;
3099 }
3100}
3101
3102
3103/*
3104 * Produces statistics data for the session <s>. Expects to be called with
3105 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3106 * flag from the session, which it uses to keep on being called when there is
3107 * free space in the buffer, of simply by letting an empty buffer upon return.
3108 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3109 */
3110int produce_content_stats(struct session *s)
3111{
3112 struct buffer *rep = s->rep;
3113 struct proxy *px;
3114 struct chunk msg;
3115 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003116
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003117 msg.len = 0;
3118 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003119
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003120 switch (s->data_state) {
3121 case DATA_ST_INIT:
3122 /* the function had not been called yet */
3123 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003124
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003125 chunk_printf(&msg, sizeof(trash),
3126 "HTTP/1.0 200 OK\r\n"
3127 "Cache-Control: no-cache\r\n"
3128 "Connection: close\r\n"
3129 "Content-Type: text/html\r\n"
3130 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003131
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003132 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003133 client_retnclose(s, &msg); // send the start of the response.
3134 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003135
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003136 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3137 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3138 if (!(s->flags & SN_FINST_MASK))
3139 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003140
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003141 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003142 /* that's all we return in case of HEAD request */
3143 s->data_state = DATA_ST_FIN;
3144 s->flags &= ~SN_SELF_GEN;
3145 return 1;
3146 }
3147
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003148 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3149 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003150
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003151 case DATA_ST_HEAD:
3152 /* WARNING! This must fit in the first buffer !!! */
3153 chunk_printf(&msg, sizeof(trash),
3154 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3155 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3156 "<style type=\"text/css\"><!--\n"
3157 "body {"
3158 " font-family: helvetica, arial;"
3159 " font-size: 12px;"
3160 " font-weight: normal;"
3161 " color: black;"
3162 " background: white;"
3163 "}\n"
3164 "th,td {"
3165 " font-size: 0.8em;"
3166 " align: center;"
3167 "}"
3168 "h1 {"
3169 " font-size: xx-large;"
3170 " margin-bottom: 0.5em;"
3171 "}\n"
3172 "h2 {"
3173 " font-family: helvetica, arial;"
3174 " font-size: x-large;"
3175 " font-weight: bold;"
3176 " font-style: italic;"
3177 " color: #6020a0;"
3178 " margin-top: 0em;"
3179 " margin-bottom: 0em;"
3180 "}\n"
3181 "h3 {"
3182 " font-family: helvetica, arial;"
3183 " font-size: 16px;"
3184 " font-weight: bold;"
3185 " color: #b00040;"
3186 " background: #e8e8d0;"
3187 " margin-top: 0em;"
3188 " margin-bottom: 0em;"
3189 "}\n"
3190 "li {"
3191 " margin-top: 0.25em;"
3192 " margin-right: 2em;"
3193 "}\n"
3194 ".hr {margin-top: 0.25em;"
3195 " border-color: black;"
3196 " border-bottom-style: solid;"
3197 "}\n"
3198 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3199 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3200 ".total {background: #20D0D0;color: #ffff80;}\n"
3201 ".frontend {background: #e8e8d0;}\n"
3202 ".backend {background: #e8e8d0;}\n"
3203 ".active0 {background: #ff9090;}\n"
3204 ".active1 {background: #ffd020;}\n"
3205 ".active2 {background: #ffffa0;}\n"
3206 ".active3 {background: #c0ffc0;}\n"
3207 ".active4 {background: #e0e0e0;}\n"
3208 ".backup0 {background: #ff9090;}\n"
3209 ".backup1 {background: #ff80ff;}\n"
3210 ".backup2 {background: #c060ff;}\n"
3211 ".backup3 {background: #b0d0ff;}\n"
3212 ".backup4 {background: #e0e0e0;}\n"
3213 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003214 "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 +01003215 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3216 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3217 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3218 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3219 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3220 "-->"
3221 "</style></head>");
3222
3223 if (buffer_write_chunk(rep, &msg) != 0)
3224 return 0;
3225
3226 s->data_state = DATA_ST_INFO;
3227 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003228
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003229 case DATA_ST_INFO:
3230 up = (now.tv_sec - start_date.tv_sec);
3231
3232 /* WARNING! this has to fit the first packet too.
3233 * We are around 3.5 kB, add adding entries will
3234 * become tricky if we want to support 4kB buffers !
3235 */
3236 chunk_printf(&msg, sizeof(trash),
3237 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3238 PRODUCT_NAME "</a></h1>\n"
3239 "<h2>Statistics Report for pid %d</h2>\n"
3240 "<hr width=\"100%%\" class=\"hr\">\n"
3241 "<h3>&gt; General process information</h3>\n"
3242 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3243 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3244 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3245 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3246 "<b>maxsock = </b> %d<br>\n"
3247 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3248 "</td><td align=\"center\" nowrap>\n"
3249 "<table class=\"lgd\"><tr>"
3250 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3251 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3252 "</tr><tr>"
3253 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3254 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3255 "</tr><tr>"
3256 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3257 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3258 "</tr><tr>"
3259 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3260 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3261 "</tr></table>\n"
3262 "</td>"
3263 "<td align=\"left\" nowrap width=\"1%%\">"
3264 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">"
3265 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>"
3266 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>"
3267 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>"
3268 "</ul>"
3269 "</td>"
3270 "</tr></table>\n"
3271 "",
3272 pid, pid, global.nbproc,
3273 up / 86400, (up % 86400) / 3600,
3274 (up % 3600) / 60, (up % 60),
3275 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3276 global.rlimit_memmax ? " MB" : "",
3277 global.rlimit_nofile,
3278 global.maxsock,
3279 global.maxconn,
3280 actconn
3281 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003282
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003283 if (buffer_write_chunk(rep, &msg) != 0)
3284 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003285
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003286 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003287
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003288 s->data_ctx.stats.px = proxy;
3289 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3290 s->data_state = DATA_ST_LIST;
3291 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003292
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003293 case DATA_ST_LIST:
3294 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003295 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003296 px = s->data_ctx.stats.px;
3297 /* skip the disabled proxies and non-networked ones */
3298 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3299 if (produce_content_stats_proxy(s, px) == 0)
3300 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003301
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003302 s->data_ctx.stats.px = px->next;
3303 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3304 }
3305 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003306
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003307 s->data_state = DATA_ST_END;
3308 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003309
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003310 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003311 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003312 if (buffer_write_chunk(rep, &msg) != 0)
3313 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003314
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003315 s->data_state = DATA_ST_FIN;
3316 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003317
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003318 case DATA_ST_FIN:
3319 s->flags &= ~SN_SELF_GEN;
3320 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003321
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003322 default:
3323 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003324 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003325 client_retnclose(s, error_message(s, HTTP_ERR_500));
3326 if (!(s->flags & SN_ERR_MASK))
3327 s->flags |= SN_ERR_PRXCOND;
3328 if (!(s->flags & SN_FINST_MASK))
3329 s->flags |= SN_FINST_R;
3330 s->flags &= ~SN_SELF_GEN;
3331 return 1;
3332 }
3333}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003334
Willy Tarreaubaaee002006-06-26 02:48:02 +02003335
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003336/*
3337 * Dumps statistics for a proxy.
3338 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3339 * ot non-zero if everything completed.
3340 */
3341int produce_content_stats_proxy(struct session *s, struct proxy *px)
3342{
3343 struct buffer *rep = s->rep;
3344 struct server *sv;
3345 struct chunk msg;
3346
3347 msg.len = 0;
3348 msg.str = trash;
3349
3350 switch (s->data_ctx.stats.px_st) {
3351 case DATA_ST_PX_INIT:
3352 /* we are on a new proxy */
3353
3354 if (s->be->fiprm->uri_auth && s->be->fiprm->uri_auth->scope) {
3355 /* we have a limited scope, we have to check the proxy name */
3356 struct stat_scope *scope;
3357 int len;
3358
3359 len = strlen(px->id);
3360 scope = s->be->fiprm->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003361
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003362 while (scope) {
3363 /* match exact proxy name */
3364 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3365 break;
3366
3367 /* match '.' which means 'self' proxy */
3368 if (!strcmp(scope->px_id, ".") && px == s->fe)
3369 break;
3370 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003371 }
3372
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003373 /* proxy name not found : don't dump anything */
3374 if (scope == NULL)
3375 return 1;
3376 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003377
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003378 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3379 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003380
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003381 case DATA_ST_PX_TH:
3382 /* print a new table */
3383 chunk_printf(&msg, sizeof(trash),
3384 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3385 "<tr align=\"center\" class=\"titre\">"
3386 "<th colspan=2 class=\"pxname\">%s</th>"
3387 "<th colspan=18 class=\"empty\"></th>"
3388 "</tr>\n"
3389 "<tr align=\"center\" class=\"titre\">"
3390 "<th rowspan=2></th>"
3391 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3392 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3393 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3394 "</tr>\n"
3395 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003396 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3397 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003398 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3399 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3400 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3401 "",
3402 px->id);
3403
3404 if (buffer_write_chunk(rep, &msg) != 0)
3405 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003406
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003407 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3408 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003409
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003410 case DATA_ST_PX_FE:
3411 /* print the frontend */
3412 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003413 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003414 /* name, queue */
3415 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3416 /* sessions : current, max, limit, cumul. */
3417 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3418 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003419 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003420 /* denied: req, resp */
3421 "<td align=right>%d</td><td align=right>%d</td>"
3422 /* errors : request, connect, response */
3423 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3424 /* server status : reflect backend status */
3425 "<td align=center>%s</td>"
3426 /* rest of server: nothing */
3427 "<td align=center colspan=5></td></tr>"
3428 "",
3429 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003430 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003431 px->denied_req, px->denied_resp,
3432 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003433 px->state == PR_STRUN ? "OPEN" :
3434 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003435
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003436 if (buffer_write_chunk(rep, &msg) != 0)
3437 return 0;
3438 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003439
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003440 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3441 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3442 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003443
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003444 case DATA_ST_PX_SV:
3445 /* stats.sv has been initialized above */
3446 while (s->data_ctx.stats.sv != NULL) {
3447 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3448 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003449
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003450 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003451
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003452 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3453 if (!(sv->state & SRV_CHECKED))
3454 sv_state = 4;
3455 else if (sv->state & SRV_RUNNING)
3456 if (sv->health == sv->rise + sv->fall - 1)
3457 sv_state = 3; /* UP */
3458 else
3459 sv_state = 2; /* going down */
3460 else
3461 if (sv->health)
3462 sv_state = 1; /* going up */
3463 else
3464 sv_state = 0; /* DOWN */
3465
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003466 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003467 /* name */
3468 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3469 /* queue : current, max */
3470 "<td align=right>%d</td><td align=right>%d</td>"
3471 /* sessions : current, max, limit, cumul */
3472 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3473 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003474 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003475 /* denied: req, resp */
3476 "<td align=right></td><td align=right>%d</td>"
3477 /* errors : request, connect, response */
3478 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3479 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003480 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003481 sv_state, sv->id,
3482 sv->nbpend, sv->nbpend_max,
3483 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003484 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003485 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003486 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003487
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003488 /* status */
3489 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3490 chunk_printf(&msg, sizeof(trash),
3491 srv_hlt_st[sv_state],
3492 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3493 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3494
Willy Tarreau128e9542007-01-01 22:01:43 +01003495 chunk_printf(&msg, sizeof(trash),
3496 /* weight */
3497 "</td><td>%d</td>"
3498 /* act, bck */
3499 "<td>%s</td><td>%s</td>"
3500 "",
3501 sv->uweight+1,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003502 (sv->state & SRV_BACKUP) ? "-" : "Y",
3503 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003504
3505 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003506 if (sv->state & SRV_CHECKED)
3507 chunk_printf(&msg, sizeof(trash),
3508 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3509 sv->failed_checks, sv->down_trans);
3510 else
3511 chunk_printf(&msg, sizeof(trash),
3512 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003513
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003514 if (buffer_write_chunk(rep, &msg) != 0)
3515 return 0;
3516
3517 s->data_ctx.stats.sv = sv->next;
3518 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003519
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003520 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3521 /* fall through */
3522
3523 case DATA_ST_PX_BE:
3524 /* print the backend */
3525 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003526 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003527 /* name */
3528 "<tr align=center class=\"backend\"><td>Backend</td>"
3529 /* queue : current, max */
3530 "<td align=right>%d</td><td align=right>%d</td>"
3531 /* sessions : current, max, limit, cumul. */
3532 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3533 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003534 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003535 /* denied: req, resp */
3536 "<td align=right>%d</td><td align=right>%d</td>"
3537 /* errors : request, connect, response */
3538 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3539 /* server status : reflect backend status (up/down) : we display UP
3540 * if the backend has known working servers or if it has no server at
3541 * all (eg: for stats). Tthen we display the total weight, number of
3542 * active and backups. */
3543 "<td align=center>%s</td><td align=center>%d</td>"
3544 "<td align=center>%d</td><td align=center>%d</td>"
3545 /* rest of server: nothing */
3546 "<td align=center colspan=2></td></tr>"
3547 "",
3548 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3549 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003550 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003551 px->denied_req, px->denied_resp,
3552 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003553 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3554 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003555
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003556 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003557 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003558 }
3559
3560 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3561 /* fall through */
3562
3563 case DATA_ST_PX_END:
3564 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3565
3566 if (buffer_write_chunk(rep, &msg) != 0)
3567 return 0;
3568
3569 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3570 /* fall through */
3571
3572 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003573 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003574
3575 default:
3576 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003577 return 1;
3578 }
3579}
3580
3581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003582/* Iterate the same filter through all request headers.
3583 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003584 * Since it can manage the switch to another backend, it updates the per-proxy
3585 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003586 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003587int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003588{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003589 char term;
3590 char *cur_ptr, *cur_end, *cur_next;
3591 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003592 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003593 struct hdr_idx_elem *cur_hdr;
3594 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003596 last_hdr = 0;
3597
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003598 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003599 old_idx = 0;
3600
3601 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003602 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003603 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003604 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003605 (exp->action == ACT_ALLOW ||
3606 exp->action == ACT_DENY ||
3607 exp->action == ACT_TARPIT))
3608 return 0;
3609
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003610 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003611 if (!cur_idx)
3612 break;
3613
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003614 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003615 cur_ptr = cur_next;
3616 cur_end = cur_ptr + cur_hdr->len;
3617 cur_next = cur_end + cur_hdr->cr + 1;
3618
3619 /* Now we have one header between cur_ptr and cur_end,
3620 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003621 */
3622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003623 /* The annoying part is that pattern matching needs
3624 * that we modify the contents to null-terminate all
3625 * strings before testing them.
3626 */
3627
3628 term = *cur_end;
3629 *cur_end = '\0';
3630
3631 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3632 switch (exp->action) {
3633 case ACT_SETBE:
3634 /* It is not possible to jump a second time.
3635 * FIXME: should we return an HTTP/500 here so that
3636 * the admin knows there's a problem ?
3637 */
3638 if (t->be != t->fe)
3639 break;
3640
3641 /* Swithing Proxy */
3642 t->be = (struct proxy *) exp->replace;
3643
3644 /* right now, the backend switch is not too much complicated
3645 * because we have associated req_cap and rsp_cap to the
3646 * frontend, and the beconn will be updated later.
3647 */
3648
3649 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3650 t->req->cto = t->be->beprm->contimeout;
3651 last_hdr = 1;
3652 break;
3653
3654 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003655 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003656 last_hdr = 1;
3657 break;
3658
3659 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003660 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003661 last_hdr = 1;
3662 t->be->beprm->denied_req++;
3663 break;
3664
3665 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003666 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003667 last_hdr = 1;
3668 t->be->beprm->denied_req++;
3669 break;
3670
3671 case ACT_REPLACE:
3672 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3673 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3674 /* FIXME: if the user adds a newline in the replacement, the
3675 * index will not be recalculated for now, and the new line
3676 * will not be counted as a new header.
3677 */
3678
3679 cur_end += delta;
3680 cur_next += delta;
3681 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003682 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003683 break;
3684
3685 case ACT_REMOVE:
3686 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3687 cur_next += delta;
3688
3689 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003690 txn->req.eoh += delta;
3691 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3692 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003693 cur_hdr->len = 0;
3694 cur_end = NULL; /* null-term has been rewritten */
3695 break;
3696
3697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003698 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003699 if (cur_end)
3700 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003702 /* keep the link from this header to next one in case of later
3703 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003704 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003705 old_idx = cur_idx;
3706 }
3707 return 0;
3708}
3709
3710
3711/* Apply the filter to the request line.
3712 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3713 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003714 * Since it can manage the switch to another backend, it updates the per-proxy
3715 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003716 */
3717int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3718{
3719 char term;
3720 char *cur_ptr, *cur_end;
3721 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003722 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003723 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003724
Willy Tarreau58f10d72006-12-04 02:26:12 +01003725
Willy Tarreau3d300592007-03-18 18:34:41 +01003726 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003727 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003728 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003729 (exp->action == ACT_ALLOW ||
3730 exp->action == ACT_DENY ||
3731 exp->action == ACT_TARPIT))
3732 return 0;
3733 else if (exp->action == ACT_REMOVE)
3734 return 0;
3735
3736 done = 0;
3737
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003738 cur_ptr = req->data + txn->req.som;
3739 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003740
3741 /* Now we have the request line between cur_ptr and cur_end */
3742
3743 /* The annoying part is that pattern matching needs
3744 * that we modify the contents to null-terminate all
3745 * strings before testing them.
3746 */
3747
3748 term = *cur_end;
3749 *cur_end = '\0';
3750
3751 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3752 switch (exp->action) {
3753 case ACT_SETBE:
3754 /* It is not possible to jump a second time.
3755 * FIXME: should we return an HTTP/500 here so that
3756 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003757 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003758 if (t->be != t->fe)
3759 break;
3760
3761 /* Swithing Proxy */
3762 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003764 /* right now, the backend switch is not too much complicated
3765 * because we have associated req_cap and rsp_cap to the
3766 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003767 */
3768
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003769 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3770 t->req->cto = t->be->beprm->contimeout;
3771 done = 1;
3772 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003773
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003774 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003775 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003776 done = 1;
3777 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003778
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003779 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003780 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003781 t->be->beprm->denied_req++;
3782 done = 1;
3783 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003785 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003786 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003787 t->be->beprm->denied_req++;
3788 done = 1;
3789 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003790
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003791 case ACT_REPLACE:
3792 *cur_end = term; /* restore the string terminator */
3793 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3794 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3795 /* FIXME: if the user adds a newline in the replacement, the
3796 * index will not be recalculated for now, and the new line
3797 * will not be counted as a new header.
3798 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003799
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003800 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003801 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003802
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003803 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003804 HTTP_MSG_RQMETH,
3805 cur_ptr, cur_end + 1,
3806 NULL, NULL);
3807 if (unlikely(!cur_end))
3808 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003809
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003810 /* we have a full request and we know that we have either a CR
3811 * or an LF at <ptr>.
3812 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003813 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3814 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003815 /* there is no point trying this regex on headers */
3816 return 1;
3817 }
3818 }
3819 *cur_end = term; /* restore the string terminator */
3820 return done;
3821}
Willy Tarreau97de6242006-12-27 17:18:38 +01003822
Willy Tarreau58f10d72006-12-04 02:26:12 +01003823
Willy Tarreau58f10d72006-12-04 02:26:12 +01003824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003825/*
3826 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3827 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003828 * unparsable request. Since it can manage the switch to another backend, it
3829 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003830 */
3831int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3832{
Willy Tarreau3d300592007-03-18 18:34:41 +01003833 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003834 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003835 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003836 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003838 /*
3839 * The interleaving of transformations and verdicts
3840 * makes it difficult to decide to continue or stop
3841 * the evaluation.
3842 */
3843
Willy Tarreau3d300592007-03-18 18:34:41 +01003844 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003845 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3846 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3847 exp = exp->next;
3848 continue;
3849 }
3850
3851 /* Apply the filter to the request line. */
3852 ret = apply_filter_to_req_line(t, req, exp);
3853 if (unlikely(ret < 0))
3854 return -1;
3855
3856 if (likely(ret == 0)) {
3857 /* The filter did not match the request, it can be
3858 * iterated through all headers.
3859 */
3860 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003861 }
3862 exp = exp->next;
3863 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003864 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003865}
3866
3867
Willy Tarreaua15645d2007-03-18 16:22:39 +01003868
Willy Tarreau58f10d72006-12-04 02:26:12 +01003869/*
3870 * Manager client-side cookie
3871 */
3872void manage_client_side_cookies(struct session *t, struct buffer *req)
3873{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003874 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003875 char *p1, *p2, *p3, *p4;
3876 char *del_colon, *del_cookie, *colon;
3877 int app_cookies;
3878
3879 appsess *asession_temp = NULL;
3880 appsess local_asession;
3881
3882 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003883 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003884
Willy Tarreau830ff452006-12-17 19:31:23 +01003885 if (t->be->beprm->cookie_name == NULL &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003886 t->be->beprm->appsession_name == NULL &&
3887 t->be->fiprm->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003888 return;
3889
Willy Tarreau2a324282006-12-05 00:05:46 +01003890 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003891 * we start with the start line.
3892 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003893 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003894 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003895
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003896 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003897 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003898 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003899
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003900 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003901 cur_ptr = cur_next;
3902 cur_end = cur_ptr + cur_hdr->len;
3903 cur_next = cur_end + cur_hdr->cr + 1;
3904
3905 /* We have one full header between cur_ptr and cur_end, and the
3906 * next header starts at cur_next. We're only interested in
3907 * "Cookie:" headers.
3908 */
3909
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003910 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3911 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003912 old_idx = cur_idx;
3913 continue;
3914 }
3915
3916 /* Now look for cookies. Conforming to RFC2109, we have to support
3917 * attributes whose name begin with a '$', and associate them with
3918 * the right cookie, if we want to delete this cookie.
3919 * So there are 3 cases for each cookie read :
3920 * 1) it's a special attribute, beginning with a '$' : ignore it.
3921 * 2) it's a server id cookie that we *MAY* want to delete : save
3922 * some pointers on it (last semi-colon, beginning of cookie...)
3923 * 3) it's an application cookie : we *MAY* have to delete a previous
3924 * "special" cookie.
3925 * At the end of loop, if a "special" cookie remains, we may have to
3926 * remove it. If no application cookie persists in the header, we
3927 * *MUST* delete it
3928 */
3929
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003930 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003931
Willy Tarreau58f10d72006-12-04 02:26:12 +01003932 /* del_cookie == NULL => nothing to be deleted */
3933 del_colon = del_cookie = NULL;
3934 app_cookies = 0;
3935
3936 while (p1 < cur_end) {
3937 /* skip spaces and colons, but keep an eye on these ones */
3938 while (p1 < cur_end) {
3939 if (*p1 == ';' || *p1 == ',')
3940 colon = p1;
3941 else if (!isspace((int)*p1))
3942 break;
3943 p1++;
3944 }
3945
3946 if (p1 == cur_end)
3947 break;
3948
3949 /* p1 is at the beginning of the cookie name */
3950 p2 = p1;
3951 while (p2 < cur_end && *p2 != '=')
3952 p2++;
3953
3954 if (p2 == cur_end)
3955 break;
3956
3957 p3 = p2 + 1; /* skips the '=' sign */
3958 if (p3 == cur_end)
3959 break;
3960
3961 p4 = p3;
3962 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
3963 p4++;
3964
3965 /* here, we have the cookie name between p1 and p2,
3966 * and its value between p3 and p4.
3967 * we can process it :
3968 *
3969 * Cookie: NAME=VALUE;
3970 * | || || |
3971 * | || || +--> p4
3972 * | || |+-------> p3
3973 * | || +--------> p2
3974 * | |+------------> p1
3975 * | +-------------> colon
3976 * +--------------------> cur_ptr
3977 */
3978
3979 if (*p1 == '$') {
3980 /* skip this one */
3981 }
3982 else {
3983 /* first, let's see if we want to capture it */
Willy Tarreau830ff452006-12-17 19:31:23 +01003984 if (t->fe->fiprm->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003985 txn->cli_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01003986 (p4 - p1 >= t->fe->fiprm->capture_namelen) &&
3987 memcmp(p1, t->fe->fiprm->capture_name, t->fe->fiprm->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003988 int log_len = p4 - p1;
3989
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003990 if ((txn->cli_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003991 Alert("HTTP logging : out of memory.\n");
3992 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01003993 if (log_len > t->fe->fiprm->capture_len)
3994 log_len = t->fe->fiprm->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003995 memcpy(txn->cli_cookie, p1, log_len);
3996 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003997 }
3998 }
3999
Willy Tarreau830ff452006-12-17 19:31:23 +01004000 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
4001 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004002 /* Cool... it's the right one */
Willy Tarreau830ff452006-12-17 19:31:23 +01004003 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004004 char *delim;
4005
4006 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4007 * have the server ID betweek p3 and delim, and the original cookie between
4008 * delim+1 and p4. Otherwise, delim==p4 :
4009 *
4010 * Cookie: NAME=SRV~VALUE;
4011 * | || || | |
4012 * | || || | +--> p4
4013 * | || || +--------> delim
4014 * | || |+-----------> p3
4015 * | || +------------> p2
4016 * | |+----------------> p1
4017 * | +-----------------> colon
4018 * +------------------------> cur_ptr
4019 */
4020
Willy Tarreau830ff452006-12-17 19:31:23 +01004021 if (t->be->beprm->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004022 for (delim = p3; delim < p4; delim++)
4023 if (*delim == COOKIE_DELIM)
4024 break;
4025 }
4026 else
4027 delim = p4;
4028
4029
4030 /* Here, we'll look for the first running server which supports the cookie.
4031 * This allows to share a same cookie between several servers, for example
4032 * to dedicate backup servers to specific servers only.
4033 * However, to prevent clients from sticking to cookie-less backup server
4034 * when they have incidentely learned an empty cookie, we simply ignore
4035 * empty cookies and mark them as invalid.
4036 */
4037 if (delim == p3)
4038 srv = NULL;
4039
4040 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004041 if (srv->cookie && (srv->cklen == delim - p3) &&
4042 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004043 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004044 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004045 txn->flags &= ~TX_CK_MASK;
4046 txn->flags |= TX_CK_VALID;
4047 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004048 t->srv = srv;
4049 break;
4050 } else {
4051 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004052 txn->flags &= ~TX_CK_MASK;
4053 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004054 }
4055 }
4056 srv = srv->next;
4057 }
4058
Willy Tarreau3d300592007-03-18 18:34:41 +01004059 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004060 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004061 txn->flags &= ~TX_CK_MASK;
4062 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004063 }
4064
4065 /* depending on the cookie mode, we may have to either :
4066 * - delete the complete cookie if we're in insert+indirect mode, so that
4067 * the server never sees it ;
4068 * - remove the server id from the cookie value, and tag the cookie as an
4069 * application cookie so that it does not get accidentely removed later,
4070 * if we're in cookie prefix mode
4071 */
Willy Tarreau830ff452006-12-17 19:31:23 +01004072 if ((t->be->beprm->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004073 int delta; /* negative */
4074
4075 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4076 p4 += delta;
4077 cur_end += delta;
4078 cur_next += delta;
4079 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004080 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004081
4082 del_cookie = del_colon = NULL;
4083 app_cookies++; /* protect the header from deletion */
4084 }
4085 else if (del_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01004086 (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 +01004087 del_cookie = p1;
4088 del_colon = colon;
4089 }
4090 } else {
4091 /* now we know that we must keep this cookie since it's
4092 * not ours. But if we wanted to delete our cookie
4093 * earlier, we cannot remove the complete header, but we
4094 * can remove the previous block itself.
4095 */
4096 app_cookies++;
4097
4098 if (del_cookie != NULL) {
4099 int delta; /* negative */
4100
4101 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4102 p4 += delta;
4103 cur_end += delta;
4104 cur_next += delta;
4105 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004106 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004107 del_cookie = del_colon = NULL;
4108 }
4109 }
4110
Willy Tarreau830ff452006-12-17 19:31:23 +01004111 if ((t->be->beprm->appsession_name != NULL) &&
4112 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004113 /* first, let's see if the cookie is our appcookie*/
4114
4115 /* Cool... it's the right one */
4116
4117 asession_temp = &local_asession;
4118
4119 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4120 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4121 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4122 return;
4123 }
4124
Willy Tarreau830ff452006-12-17 19:31:23 +01004125 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4126 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004127 asession_temp->serverid = NULL;
4128
4129 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004130 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004131 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4132 /* free previously allocated memory */
4133 pool_free_to(apools.sessid, local_asession.sessid);
4134 Alert("Not enough memory process_cli():asession:calloc().\n");
4135 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4136 return;
4137 }
4138
4139 asession_temp->sessid = local_asession.sessid;
4140 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004141 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004142 } else {
4143 /* free previously allocated memory */
4144 pool_free_to(apools.sessid, local_asession.sessid);
4145 }
4146
4147 if (asession_temp->serverid == NULL) {
4148 Alert("Found Application Session without matching server.\n");
4149 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004150 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004151 while (srv) {
4152 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004153 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004154 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004155 txn->flags &= ~TX_CK_MASK;
4156 txn->flags |= TX_CK_VALID;
4157 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004158 t->srv = srv;
4159 break;
4160 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004161 txn->flags &= ~TX_CK_MASK;
4162 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004163 }
4164 }
4165 srv = srv->next;
4166 }/* end while(srv) */
4167 }/* end else if server == NULL */
4168
Willy Tarreau830ff452006-12-17 19:31:23 +01004169 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004170 }/* end if ((t->proxy->appsession_name != NULL) ... */
4171 }
4172
4173 /* we'll have to look for another cookie ... */
4174 p1 = p4;
4175 } /* while (p1 < cur_end) */
4176
4177 /* There's no more cookie on this line.
4178 * We may have marked the last one(s) for deletion.
4179 * We must do this now in two ways :
4180 * - if there is no app cookie, we simply delete the header ;
4181 * - if there are app cookies, we must delete the end of the
4182 * string properly, including the colon/semi-colon before
4183 * the cookie name.
4184 */
4185 if (del_cookie != NULL) {
4186 int delta;
4187 if (app_cookies) {
4188 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4189 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004190 cur_hdr->len += delta;
4191 } else {
4192 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004193
4194 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004195 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4196 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004197 cur_hdr->len = 0;
4198 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004199 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004200 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004201 }
4202
4203 /* keep the link from this header to next one */
4204 old_idx = cur_idx;
4205 } /* end of cookie processing on this header */
4206}
4207
4208
Willy Tarreaua15645d2007-03-18 16:22:39 +01004209/* Iterate the same filter through all response headers contained in <rtr>.
4210 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4211 */
4212int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4213{
4214 char term;
4215 char *cur_ptr, *cur_end, *cur_next;
4216 int cur_idx, old_idx, last_hdr;
4217 struct http_txn *txn = &t->txn;
4218 struct hdr_idx_elem *cur_hdr;
4219 int len, delta;
4220
4221 last_hdr = 0;
4222
4223 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4224 old_idx = 0;
4225
4226 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004227 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004228 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004229 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004230 (exp->action == ACT_ALLOW ||
4231 exp->action == ACT_DENY))
4232 return 0;
4233
4234 cur_idx = txn->hdr_idx.v[old_idx].next;
4235 if (!cur_idx)
4236 break;
4237
4238 cur_hdr = &txn->hdr_idx.v[cur_idx];
4239 cur_ptr = cur_next;
4240 cur_end = cur_ptr + cur_hdr->len;
4241 cur_next = cur_end + cur_hdr->cr + 1;
4242
4243 /* Now we have one header between cur_ptr and cur_end,
4244 * and the next header starts at cur_next.
4245 */
4246
4247 /* The annoying part is that pattern matching needs
4248 * that we modify the contents to null-terminate all
4249 * strings before testing them.
4250 */
4251
4252 term = *cur_end;
4253 *cur_end = '\0';
4254
4255 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4256 switch (exp->action) {
4257 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004258 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004259 last_hdr = 1;
4260 break;
4261
4262 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004263 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004264 last_hdr = 1;
4265 break;
4266
4267 case ACT_REPLACE:
4268 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4269 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4270 /* FIXME: if the user adds a newline in the replacement, the
4271 * index will not be recalculated for now, and the new line
4272 * will not be counted as a new header.
4273 */
4274
4275 cur_end += delta;
4276 cur_next += delta;
4277 cur_hdr->len += delta;
4278 txn->rsp.eoh += delta;
4279 break;
4280
4281 case ACT_REMOVE:
4282 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4283 cur_next += delta;
4284
4285 /* FIXME: this should be a separate function */
4286 txn->rsp.eoh += delta;
4287 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4288 txn->hdr_idx.used--;
4289 cur_hdr->len = 0;
4290 cur_end = NULL; /* null-term has been rewritten */
4291 break;
4292
4293 }
4294 }
4295 if (cur_end)
4296 *cur_end = term; /* restore the string terminator */
4297
4298 /* keep the link from this header to next one in case of later
4299 * removal of next header.
4300 */
4301 old_idx = cur_idx;
4302 }
4303 return 0;
4304}
4305
4306
4307/* Apply the filter to the status line in the response buffer <rtr>.
4308 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4309 * or -1 if a replacement resulted in an invalid status line.
4310 */
4311int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4312{
4313 char term;
4314 char *cur_ptr, *cur_end;
4315 int done;
4316 struct http_txn *txn = &t->txn;
4317 int len, delta;
4318
4319
Willy Tarreau3d300592007-03-18 18:34:41 +01004320 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004321 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004322 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004323 (exp->action == ACT_ALLOW ||
4324 exp->action == ACT_DENY))
4325 return 0;
4326 else if (exp->action == ACT_REMOVE)
4327 return 0;
4328
4329 done = 0;
4330
4331 cur_ptr = rtr->data + txn->rsp.som;
4332 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4333
4334 /* Now we have the status line between cur_ptr and cur_end */
4335
4336 /* The annoying part is that pattern matching needs
4337 * that we modify the contents to null-terminate all
4338 * strings before testing them.
4339 */
4340
4341 term = *cur_end;
4342 *cur_end = '\0';
4343
4344 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4345 switch (exp->action) {
4346 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004347 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004348 done = 1;
4349 break;
4350
4351 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004352 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004353 done = 1;
4354 break;
4355
4356 case ACT_REPLACE:
4357 *cur_end = term; /* restore the string terminator */
4358 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4359 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4360 /* FIXME: if the user adds a newline in the replacement, the
4361 * index will not be recalculated for now, and the new line
4362 * will not be counted as a new header.
4363 */
4364
4365 txn->rsp.eoh += delta;
4366 cur_end += delta;
4367
4368 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
4369 HTTP_MSG_RQMETH,
4370 cur_ptr, cur_end + 1,
4371 NULL, NULL);
4372 if (unlikely(!cur_end))
4373 return -1;
4374
4375 /* we have a full respnse and we know that we have either a CR
4376 * or an LF at <ptr>.
4377 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004378 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004379 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4380 /* there is no point trying this regex on headers */
4381 return 1;
4382 }
4383 }
4384 *cur_end = term; /* restore the string terminator */
4385 return done;
4386}
4387
4388
4389
4390/*
4391 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4392 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4393 * unparsable response.
4394 */
4395int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4396{
Willy Tarreau3d300592007-03-18 18:34:41 +01004397 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004398 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004399 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004400 int ret;
4401
4402 /*
4403 * The interleaving of transformations and verdicts
4404 * makes it difficult to decide to continue or stop
4405 * the evaluation.
4406 */
4407
Willy Tarreau3d300592007-03-18 18:34:41 +01004408 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004409 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4410 exp->action == ACT_PASS)) {
4411 exp = exp->next;
4412 continue;
4413 }
4414
4415 /* Apply the filter to the status line. */
4416 ret = apply_filter_to_sts_line(t, rtr, exp);
4417 if (unlikely(ret < 0))
4418 return -1;
4419
4420 if (likely(ret == 0)) {
4421 /* The filter did not match the response, it can be
4422 * iterated through all headers.
4423 */
4424 apply_filter_to_resp_headers(t, rtr, exp);
4425 }
4426 exp = exp->next;
4427 }
4428 return 0;
4429}
4430
4431
4432
4433/*
4434 * Manager server-side cookies
4435 */
4436void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4437{
4438 struct http_txn *txn = &t->txn;
4439 char *p1, *p2, *p3, *p4;
4440
4441 appsess *asession_temp = NULL;
4442 appsess local_asession;
4443
4444 char *cur_ptr, *cur_end, *cur_next;
4445 int cur_idx, old_idx, delta;
4446
4447 if (t->be->beprm->cookie_name == NULL &&
4448 t->be->beprm->appsession_name == NULL &&
4449 t->be->fiprm->capture_name == NULL &&
4450 !(t->be->beprm->options & PR_O_CHK_CACHE))
4451 return;
4452
4453 /* Iterate through the headers.
4454 * we start with the start line.
4455 */
4456 old_idx = 0;
4457 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4458
4459 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4460 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004461 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004462
4463 cur_hdr = &txn->hdr_idx.v[cur_idx];
4464 cur_ptr = cur_next;
4465 cur_end = cur_ptr + cur_hdr->len;
4466 cur_next = cur_end + cur_hdr->cr + 1;
4467
4468 /* We have one full header between cur_ptr and cur_end, and the
4469 * next header starts at cur_next. We're only interested in
4470 * "Cookie:" headers.
4471 */
4472
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004473 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4474 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004475 old_idx = cur_idx;
4476 continue;
4477 }
4478
4479 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004480 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004481
4482
4483 /* maybe we only wanted to see if there was a set-cookie */
4484 if (t->be->beprm->cookie_name == NULL &&
4485 t->be->beprm->appsession_name == NULL &&
4486 t->be->fiprm->capture_name == NULL)
4487 return;
4488
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004489 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004490
4491 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004492 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4493 break;
4494
4495 /* p1 is at the beginning of the cookie name */
4496 p2 = p1;
4497
4498 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4499 p2++;
4500
4501 if (p2 == cur_end || *p2 == ';') /* next cookie */
4502 break;
4503
4504 p3 = p2 + 1; /* skip the '=' sign */
4505 if (p3 == cur_end)
4506 break;
4507
4508 p4 = p3;
4509 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4510 p4++;
4511
4512 /* here, we have the cookie name between p1 and p2,
4513 * and its value between p3 and p4.
4514 * we can process it.
4515 */
4516
4517 /* first, let's see if we want to capture it */
4518 if (t->be->fiprm->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004519 txn->srv_cookie == NULL &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004520 (p4 - p1 >= t->be->fiprm->capture_namelen) &&
4521 memcmp(p1, t->be->fiprm->capture_name, t->be->fiprm->capture_namelen) == 0) {
4522 int log_len = p4 - p1;
4523
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004524 if ((txn->srv_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004525 Alert("HTTP logging : out of memory.\n");
4526 }
4527
4528 if (log_len > t->be->fiprm->capture_len)
4529 log_len = t->be->fiprm->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004530 memcpy(txn->srv_cookie, p1, log_len);
4531 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004532 }
4533
4534 /* now check if we need to process it for persistence */
4535 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
4536 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
4537 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004538 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004539
4540 /* If the cookie is in insert mode on a known server, we'll delete
4541 * this occurrence because we'll insert another one later.
4542 * We'll delete it too if the "indirect" option is set and we're in
4543 * a direct access. */
4544 if (((t->srv) && (t->be->beprm->options & PR_O_COOK_INS)) ||
4545 ((t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_IND))) {
4546 /* this header must be deleted */
4547 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4548 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4549 txn->hdr_idx.used--;
4550 cur_hdr->len = 0;
4551 cur_next += delta;
4552 txn->rsp.eoh += delta;
4553
Willy Tarreau3d300592007-03-18 18:34:41 +01004554 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004555 }
4556 else if ((t->srv) && (t->srv->cookie) &&
4557 (t->be->beprm->options & PR_O_COOK_RW)) {
4558 /* replace bytes p3->p4 with the cookie name associated
4559 * with this server since we know it.
4560 */
4561 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4562 cur_hdr->len += delta;
4563 cur_next += delta;
4564 txn->rsp.eoh += delta;
4565
Willy Tarreau3d300592007-03-18 18:34:41 +01004566 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004567 }
4568 else if ((t->srv) && (t->srv->cookie) &&
4569 (t->be->beprm->options & PR_O_COOK_PFX)) {
4570 /* insert the cookie name associated with this server
4571 * before existing cookie, and insert a delimitor between them..
4572 */
4573 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4574 cur_hdr->len += delta;
4575 cur_next += delta;
4576 txn->rsp.eoh += delta;
4577
4578 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004579 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004580 }
4581 }
4582 /* next, let's see if the cookie is our appcookie */
4583 else if ((t->be->beprm->appsession_name != NULL) &&
4584 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
4585
4586 /* Cool... it's the right one */
4587
4588 size_t server_id_len = strlen(t->srv->id) + 1;
4589 asession_temp = &local_asession;
4590
4591 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4592 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4593 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4594 return;
4595 }
4596 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4597 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
4598 asession_temp->serverid = NULL;
4599
4600 /* only do insert, if lookup fails */
4601 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
4602 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4603 Alert("Not enough Memory process_srv():asession:calloc().\n");
4604 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4605 return;
4606 }
4607 asession_temp->sessid = local_asession.sessid;
4608 asession_temp->serverid = local_asession.serverid;
4609 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
4610 }/* end if (chtbl_lookup()) */
4611 else {
4612 /* free wasted memory */
4613 pool_free_to(apools.sessid, local_asession.sessid);
4614 } /* end else from if (chtbl_lookup()) */
4615
4616 if (asession_temp->serverid == NULL) {
4617 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
4618 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4619 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4620 return;
4621 }
4622 asession_temp->serverid[0] = '\0';
4623 }
4624
4625 if (asession_temp->serverid[0] == '\0')
4626 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4627
4628 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
4629
4630#if defined(DEBUG_HASH)
4631 print_table(&(t->be->beprm->htbl_proxy));
4632#endif
4633 }/* end if ((t->proxy->appsession_name != NULL) ... */
4634 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4635 } /* we're now at the end of the cookie value */
4636
4637 /* keep the link from this header to next one */
4638 old_idx = cur_idx;
4639 } /* end of cookie processing on this header */
4640}
4641
4642
4643
4644/*
4645 * Check if response is cacheable or not. Updates t->flags.
4646 */
4647void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4648{
4649 struct http_txn *txn = &t->txn;
4650 char *p1, *p2;
4651
4652 char *cur_ptr, *cur_end, *cur_next;
4653 int cur_idx;
4654
Willy Tarreau3d300592007-03-18 18:34:41 +01004655 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004656 return;
4657
4658 /* Iterate through the headers.
4659 * we start with the start line.
4660 */
4661 cur_idx = 0;
4662 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4663
4664 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4665 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004666 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004667
4668 cur_hdr = &txn->hdr_idx.v[cur_idx];
4669 cur_ptr = cur_next;
4670 cur_end = cur_ptr + cur_hdr->len;
4671 cur_next = cur_end + cur_hdr->cr + 1;
4672
4673 /* We have one full header between cur_ptr and cur_end, and the
4674 * next header starts at cur_next. We're only interested in
4675 * "Cookie:" headers.
4676 */
4677
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004678 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4679 if (val) {
4680 if ((cur_end - (cur_ptr + val) >= 8) &&
4681 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4682 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4683 return;
4684 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004685 }
4686
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004687 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4688 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004689 continue;
4690
4691 /* OK, right now we know we have a cache-control header at cur_ptr */
4692
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004693 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004694
4695 if (p1 >= cur_end) /* no more info */
4696 continue;
4697
4698 /* p1 is at the beginning of the value */
4699 p2 = p1;
4700
4701 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4702 p2++;
4703
4704 /* we have a complete value between p1 and p2 */
4705 if (p2 < cur_end && *p2 == '=') {
4706 /* we have something of the form no-cache="set-cookie" */
4707 if ((cur_end - p1 >= 21) &&
4708 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4709 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004710 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004711 continue;
4712 }
4713
4714 /* OK, so we know that either p2 points to the end of string or to a comma */
4715 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4716 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4717 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4718 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004719 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004720 return;
4721 }
4722
4723 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004724 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004725 continue;
4726 }
4727 }
4728}
4729
4730
Willy Tarreau58f10d72006-12-04 02:26:12 +01004731/*
4732 * Try to retrieve a known appsession in the URI, then the associated server.
4733 * If the server is found, it's assigned to the session.
4734 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004735void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004736{
Willy Tarreau3d300592007-03-18 18:34:41 +01004737 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004738 appsess *asession_temp = NULL;
4739 appsess local_asession;
4740 char *request_line;
4741
Willy Tarreau830ff452006-12-17 19:31:23 +01004742 if (t->be->beprm->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004743 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004744 (request_line = memchr(begin, ';', len)) == NULL ||
4745 ((1 + t->be->beprm->appsession_name_len + 1 + t->be->beprm->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004746 return;
4747
4748 /* skip ';' */
4749 request_line++;
4750
4751 /* look if we have a jsessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004752 if (strncasecmp(request_line, t->be->beprm->appsession_name, t->be->beprm->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004753 return;
4754
4755 /* skip jsessionid= */
Willy Tarreau830ff452006-12-17 19:31:23 +01004756 request_line += t->be->beprm->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004757
4758 /* First try if we already have an appsession */
4759 asession_temp = &local_asession;
4760
4761 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4762 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4763 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4764 return;
4765 }
4766
4767 /* Copy the sessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004768 memcpy(asession_temp->sessid, request_line, t->be->beprm->appsession_len);
4769 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004770 asession_temp->serverid = NULL;
4771
4772 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004773 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004774 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4775 /* free previously allocated memory */
4776 pool_free_to(apools.sessid, local_asession.sessid);
4777 Alert("Not enough memory process_cli():asession:calloc().\n");
4778 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4779 return;
4780 }
4781 asession_temp->sessid = local_asession.sessid;
4782 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004783 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004784 }
4785 else {
4786 /* free previously allocated memory */
4787 pool_free_to(apools.sessid, local_asession.sessid);
4788 }
4789
Willy Tarreau830ff452006-12-17 19:31:23 +01004790 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004791 asession_temp->request_count++;
4792
4793#if defined(DEBUG_HASH)
4794 print_table(&(t->proxy->htbl_proxy));
4795#endif
4796 if (asession_temp->serverid == NULL) {
4797 Alert("Found Application Session without matching server.\n");
4798 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004799 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004800 while (srv) {
4801 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004802 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004803 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004804 txn->flags &= ~TX_CK_MASK;
4805 txn->flags |= TX_CK_VALID;
4806 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004807 t->srv = srv;
4808 break;
4809 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004810 txn->flags &= ~TX_CK_MASK;
4811 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004812 }
4813 }
4814 srv = srv->next;
4815 }
4816 }
4817}
4818
4819
Willy Tarreaub2513902006-12-17 14:52:38 +01004820/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004821 * In a GET or HEAD request, check if the requested URI matches the stats uri
4822 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004823 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004824 * It is assumed that the request is either a HEAD or GET and that the
4825 * t->be->fiprm->uri_auth field is valid. An HTTP/401 response may be sent, or
4826 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004827 *
4828 * Returns 1 if the session's state changes, otherwise 0.
4829 */
4830int stats_check_uri_auth(struct session *t, struct proxy *backend)
4831{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004832 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004833 struct uri_auth *uri_auth = backend->uri_auth;
4834 struct user_auth *user;
4835 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004836 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004838 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004839 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004840 return 0;
4841
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004842 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004843
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004844 /* the URI is in h */
4845 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004846 return 0;
4847
4848 /* we are in front of a interceptable URI. Let's check
4849 * if there's an authentication and if it's valid.
4850 */
4851 user = uri_auth->users;
4852 if (!user) {
4853 /* no user auth required, it's OK */
4854 authenticated = 1;
4855 } else {
4856 authenticated = 0;
4857
4858 /* a user list is defined, we have to check.
4859 * skip 21 chars for "Authorization: Basic ".
4860 */
4861
4862 /* FIXME: this should move to an earlier place */
4863 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004864 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4865 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4866 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004867 if (len > 14 &&
4868 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004869 txn->auth_hdr.str = h;
4870 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004871 break;
4872 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004873 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004874 }
4875
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004876 if (txn->auth_hdr.len < 21 ||
4877 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004878 user = NULL;
4879
4880 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004881 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4882 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004883 user->user_pwd, user->user_len)) {
4884 authenticated = 1;
4885 break;
4886 }
4887 user = user->next;
4888 }
4889 }
4890
4891 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004892 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004893
4894 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004895 msg.str = trash;
4896 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004897 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004898 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004899 if (!(t->flags & SN_ERR_MASK))
4900 t->flags |= SN_ERR_PRXCOND;
4901 if (!(t->flags & SN_FINST_MASK))
4902 t->flags |= SN_FINST_R;
4903 return 1;
4904 }
4905
4906 /* The request is valid, the user is authenticate. Let's start sending
4907 * data.
4908 */
4909 t->cli_state = CL_STSHUTR;
4910 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
4911 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
4912 t->data_source = DATA_SRC_STATS;
4913 t->data_state = DATA_ST_INIT;
4914 produce_content(t);
4915 return 1;
4916}
4917
4918
Willy Tarreaubaaee002006-06-26 02:48:02 +02004919/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004920 * Print a debug line with a header
4921 */
4922void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4923{
4924 int len, max;
4925 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4926 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4927 max = end - start;
4928 UBOUND(max, sizeof(trash) - len - 1);
4929 len += strlcpy2(trash + len, start, max + 1);
4930 trash[len++] = '\n';
4931 write(1, trash, len);
4932}
4933
4934
4935/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004936 * Local variables:
4937 * c-indent-level: 8
4938 * c-basic-offset: 8
4939 * End:
4940 */