blob: 6728eb3493f358938c0fcdae643cca6d78adc905 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaua15645d2007-03-18 16:22:39 +01004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/appsession.h>
26#include <common/compat.h>
27#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010028#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/memory.h>
30#include <common/mini-clist.h>
31#include <common/standard.h>
32#include <common/time.h>
33#include <common/uri_auth.h>
34#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36#include <types/capture.h>
37#include <types/client.h>
38#include <types/global.h>
39#include <types/httperr.h>
40#include <types/polling.h>
41#include <types/proxy.h>
42#include <types/server.h>
43
44#include <proto/backend.h>
45#include <proto/buffers.h>
46#include <proto/fd.h>
47#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010048#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
51#include <proto/session.h>
52#include <proto/task.h>
53
Willy Tarreau6d1a9882007-01-07 02:03:04 +010054#ifdef CONFIG_HAP_TCPSPLICE
55#include <libtcpsplice.h>
56#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
Willy Tarreau58f10d72006-12-04 02:26:12 +010058#define DEBUG_PARSE_NO_SPEEDUP
59#undef DEBUG_PARSE_NO_SPEEDUP
60
Willy Tarreau976f1ee2006-12-17 10:06:03 +010061/* This is used to perform a quick jump as an alternative to a break/continue
62 * instruction. The first argument is the label for normal operation, and the
63 * second one is the break/continue instruction in the no_speedup mode.
64 */
65
66#ifdef DEBUG_PARSE_NO_SPEEDUP
67#define QUICK_JUMP(x,y) y
68#else
69#define QUICK_JUMP(x,y) goto x
70#endif
71
Willy Tarreau1c47f852006-07-09 08:22:27 +020072/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010073const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020074 "HTTP/1.0 200 OK\r\n"
75 "Cache-Control: no-cache\r\n"
76 "Connection: close\r\n"
77 "Content-Type: text/html\r\n"
78 "\r\n"
79 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const struct chunk http_200_chunk = {
82 .str = (char *)&HTTP_200,
83 .len = sizeof(HTTP_200)-1
84};
85
86const char *HTTP_302 =
87 "HTTP/1.0 302 Found\r\n"
88 "Cache-Control: no-cache\r\n"
89 "Connection: close\r\n"
90 "Location: "; /* not terminated since it will be concatenated with the URL */
91
92/* same as 302 except that the browser MUST retry with the GET method */
93const char *HTTP_303 =
94 "HTTP/1.0 303 See Other\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
97 "Location: "; /* not terminated since it will be concatenated with the URL */
98
Willy Tarreaubaaee002006-06-26 02:48:02 +020099/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
100const char *HTTP_401_fmt =
101 "HTTP/1.0 401 Unauthorized\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200104 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
106 "\r\n"
107 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
108
Willy Tarreau0f772532006-12-23 20:51:41 +0100109
110const int http_err_codes[HTTP_ERR_SIZE] = {
111 [HTTP_ERR_400] = 400,
112 [HTTP_ERR_403] = 403,
113 [HTTP_ERR_408] = 408,
114 [HTTP_ERR_500] = 500,
115 [HTTP_ERR_502] = 502,
116 [HTTP_ERR_503] = 503,
117 [HTTP_ERR_504] = 504,
118};
119
Willy Tarreau80587432006-12-24 17:47:20 +0100120static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100121 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100122 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100123 "Cache-Control: no-cache\r\n"
124 "Connection: close\r\n"
125 "Content-Type: text/html\r\n"
126 "\r\n"
127 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
128
129 [HTTP_ERR_403] =
130 "HTTP/1.0 403 Forbidden\r\n"
131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "\r\n"
135 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
136
137 [HTTP_ERR_408] =
138 "HTTP/1.0 408 Request Time-out\r\n"
139 "Cache-Control: no-cache\r\n"
140 "Connection: close\r\n"
141 "Content-Type: text/html\r\n"
142 "\r\n"
143 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
144
145 [HTTP_ERR_500] =
146 "HTTP/1.0 500 Server Error\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
152
153 [HTTP_ERR_502] =
154 "HTTP/1.0 502 Bad Gateway\r\n"
155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
160
161 [HTTP_ERR_503] =
162 "HTTP/1.0 503 Service Unavailable\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
168
169 [HTTP_ERR_504] =
170 "HTTP/1.0 504 Gateway Time-out\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
176
177};
178
Willy Tarreau80587432006-12-24 17:47:20 +0100179/* We must put the messages here since GCC cannot initialize consts depending
180 * on strlen().
181 */
182struct chunk http_err_chunks[HTTP_ERR_SIZE];
183
184void init_proto_http()
185{
186 int msg;
187 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
188 if (!http_err_msgs[msg]) {
189 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
190 abort();
191 }
192
193 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
194 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
195 }
196}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197
Willy Tarreau53b6c742006-12-17 13:37:46 +0100198/*
199 * We have 26 list of methods (1 per first letter), each of which can have
200 * up to 3 entries (2 valid, 1 null).
201 */
202struct http_method_desc {
203 http_meth_t meth;
204 int len;
205 const char text[8];
206};
207
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100208const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100209 ['C' - 'A'] = {
210 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
211 },
212 ['D' - 'A'] = {
213 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
214 },
215 ['G' - 'A'] = {
216 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
217 },
218 ['H' - 'A'] = {
219 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
220 },
221 ['P' - 'A'] = {
222 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
223 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
224 },
225 ['T' - 'A'] = {
226 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
227 },
228 /* rest is empty like this :
229 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
230 */
231};
232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100233/* It is about twice as fast on recent architectures to lookup a byte in a
234 * table than two perform a boolean AND or OR between two tests. Refer to
235 * RFC2616 for those chars.
236 */
237
238const char http_is_spht[256] = {
239 [' '] = 1, ['\t'] = 1,
240};
241
242const char http_is_crlf[256] = {
243 ['\r'] = 1, ['\n'] = 1,
244};
245
246const char http_is_lws[256] = {
247 [' '] = 1, ['\t'] = 1,
248 ['\r'] = 1, ['\n'] = 1,
249};
250
251const char http_is_sep[256] = {
252 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
253 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
254 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
255 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
256 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
257};
258
259const char http_is_ctl[256] = {
260 [0 ... 31] = 1,
261 [127] = 1,
262};
263
264/*
265 * A token is any ASCII char that is neither a separator nor a CTL char.
266 * Do not overwrite values in assignment since gcc-2.95 will not handle
267 * them correctly. Instead, define every non-CTL char's status.
268 */
269const char http_is_token[256] = {
270 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
271 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
272 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
273 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
274 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
275 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
276 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
277 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
278 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
279 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
280 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
281 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
282 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
283 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
284 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
285 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
286 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
287 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
288 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
289 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
290 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
291 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
292 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
293 ['|'] = 1, ['}'] = 0, ['~'] = 1,
294};
295
296
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100297/*
298 * An http ver_token is any ASCII which can be found in an HTTP version,
299 * which includes 'H', 'T', 'P', '/', '.' and any digit.
300 */
301const char http_is_ver_token[256] = {
302 ['.'] = 1, ['/'] = 1,
303 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
304 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
305 ['H'] = 1, ['P'] = 1, ['T'] = 1,
306};
307
308
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309#ifdef DEBUG_FULL
310static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
311static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
312#endif
313
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100314/*
315 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
316 * CRLF. Text length is measured first, so it cannot be NULL.
317 * The header is also automatically added to the index <hdr_idx>, and the end
318 * of headers is automatically adjusted. The number of bytes added is returned
319 * on success, otherwise <0 is returned indicating an error.
320 */
321int http_header_add_tail(struct buffer *b, struct http_msg *msg,
322 struct hdr_idx *hdr_idx, const char *text)
323{
324 int bytes, len;
325
326 len = strlen(text);
327 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
328 if (!bytes)
329 return -1;
330 msg->eoh += bytes;
331 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
332}
333
334/*
335 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
336 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
337 * the buffer is only opened and the space reserved, but nothing is copied.
338 * The header is also automatically added to the index <hdr_idx>, and the end
339 * of headers is automatically adjusted. The number of bytes added is returned
340 * on success, otherwise <0 is returned indicating an error.
341 */
342int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
343 struct hdr_idx *hdr_idx, const char *text, int len)
344{
345 int bytes;
346
347 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
348 if (!bytes)
349 return -1;
350 msg->eoh += bytes;
351 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
352}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353
354/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100355 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
356 * If so, returns the position of the first non-space character relative to
357 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
358 * to return a pointer to the place after the first space. Returns 0 if the
359 * header name does not match. Checks are case-insensitive.
360 */
361int http_header_match2(const char *hdr, const char *end,
362 const char *name, int len)
363{
364 const char *val;
365
366 if (hdr + len >= end)
367 return 0;
368 if (hdr[len] != ':')
369 return 0;
370 if (strncasecmp(hdr, name, len) != 0)
371 return 0;
372 val = hdr + len + 1;
373 while (val < end && HTTP_IS_SPHT(*val))
374 val++;
375 if ((val >= end) && (len + 2 <= end - hdr))
376 return len + 2; /* we may replace starting from second space */
377 return val - hdr;
378}
379
380/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381 * returns a message to the client ; the connection is shut down for read,
382 * and the request is cleared so that no server connection can be initiated.
383 * The client must be in a valid state for this (HEADER, DATA ...).
Willy Tarreau0f772532006-12-23 20:51:41 +0100384 * Nothing is performed on the server side. The message is contained in a
385 * "chunk". If it is null, then an empty message is used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 * The reply buffer doesn't need to be empty before this.
387 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100388void client_retnclose(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389{
Willy Tarreau2a429502006-10-15 14:52:29 +0200390 MY_FD_CLR(s->cli_fd, StaticReadEvent);
391 MY_FD_SET(s->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +0200392 tv_eternity(&s->req->rex);
Willy Tarreau73de9892006-11-30 11:40:23 +0100393 if (s->fe->clitimeout)
394 tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395 else
Willy Tarreaud7971282006-07-29 18:36:34 +0200396 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200397 shutdown(s->cli_fd, SHUT_RD);
398 s->cli_state = CL_STSHUTR;
399 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100400 if (msg->len)
401 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402 s->req->l = 0;
403}
404
405
406/*
407 * returns a message into the rep buffer, and flushes the req buffer.
Willy Tarreau0f772532006-12-23 20:51:41 +0100408 * The reply buffer doesn't need to be empty before this. The message
409 * is contained in a "chunk". If it is null, then an empty message is
410 * used.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100412void client_return(struct session *s, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413{
414 buffer_flush(s->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100415 if (msg->len)
416 buffer_write(s->rep, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200417 s->req->l = 0;
418}
419
420
421/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100422 * indicators accordingly. Note that if <status> is 0, or if the message
423 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200424 */
425void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100426 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200427{
428 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100429 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100430 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100431 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100432 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433 }
434 if (!(t->flags & SN_ERR_MASK))
435 t->flags |= err;
436 if (!(t->flags & SN_FINST_MASK))
437 t->flags |= finst;
438}
439
Willy Tarreau80587432006-12-24 17:47:20 +0100440/* This function returns the appropriate error location for the given session
441 * and message.
442 */
443
444struct chunk *error_message(struct session *s, int msgnum)
445{
446 if (s->be->beprm->errmsg[msgnum].str)
447 return &s->be->beprm->errmsg[msgnum];
448 else if (s->fe->errmsg[msgnum].str)
449 return &s->fe->errmsg[msgnum];
450 else
451 return &http_err_chunks[msgnum];
452}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200453
Willy Tarreau53b6c742006-12-17 13:37:46 +0100454/*
455 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
456 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
457 */
458static http_meth_t find_http_meth(const char *str, const int len)
459{
460 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100461 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100462
463 m = ((unsigned)*str - 'A');
464
465 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100466 for (h = http_methods[m]; h->len > 0; h++) {
467 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100468 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100469 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100470 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100471 };
472 return HTTP_METH_OTHER;
473 }
474 return HTTP_METH_NONE;
475
476}
477
478
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479/* Processes the client and server jobs of a session task, then
480 * puts it back to the wait queue in a clean state, or
481 * cleans up its resources if it must be deleted. Returns
482 * the time the task accepts to wait, or TIME_ETERNITY for
483 * infinity.
484 */
485int process_session(struct task *t)
486{
487 struct session *s = t->context;
488 int fsm_resync = 0;
489
490 do {
491 fsm_resync = 0;
492 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
493 fsm_resync |= process_cli(s);
494 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
495 fsm_resync |= process_srv(s);
496 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
497 } while (fsm_resync);
498
499 if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) {
500 struct timeval min1, min2;
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200501 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
502 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200503
Willy Tarreaud7971282006-07-29 18:36:34 +0200504 tv_min(&min1, &s->req->rex, &s->req->wex);
505 tv_min(&min2, &s->rep->rex, &s->rep->wex);
506 tv_min(&min1, &min1, &s->req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200507 tv_min(&t->expire, &min1, &min2);
508
509 /* restore t to its place in the task list */
510 task_queue(t);
511
512#ifdef DEBUG_FULL
513 /* DEBUG code : this should never ever happen, otherwise it indicates
514 * that a task still has something to do and will provoke a quick loop.
515 */
516 if (tv_remain2(&now, &t->expire) <= 0)
517 exit(100);
518#endif
519
520 return tv_remain2(&now, &t->expire); /* nothing more to do */
521 }
522
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100523 s->fe->feconn--;
524 if (s->flags & SN_BE_ASSIGNED)
525 s->be->beprm->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526 actconn--;
527
528 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
529 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100530 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau830ff452006-12-17 19:31:23 +0100531 s->uniq_id, s->be->beprm->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100532 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533 write(1, trash, len);
534 }
535
536 s->logs.t_close = tv_diff(&s->logs.tv_accept, &now);
Willy Tarreau35d66b02007-01-02 00:28:21 +0100537 if (s->req != NULL)
538 s->logs.bytes_in = s->req->total;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200539 if (s->rep != NULL)
Willy Tarreau35d66b02007-01-02 00:28:21 +0100540 s->logs.bytes_out = s->rep->total;
541
542 s->fe->bytes_in += s->logs.bytes_in;
543 s->fe->bytes_out += s->logs.bytes_out;
544 if (s->be->beprm != s->fe) {
545 s->be->beprm->bytes_in += s->logs.bytes_in;
546 s->be->beprm->bytes_out += s->logs.bytes_out;
547 }
548 if (s->srv) {
549 s->srv->bytes_in += s->logs.bytes_in;
550 s->srv->bytes_out += s->logs.bytes_out;
551 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552
553 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200554 if (s->logs.logwait &&
555 !(s->flags & SN_MONITOR) &&
Willy Tarreau73de9892006-11-30 11:40:23 +0100556 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200557 sess_log(s);
558
559 /* the task MUST not be in the run queue anymore */
560 task_delete(t);
561 session_free(s);
562 task_free(t);
563 return TIME_ETERNITY; /* rest in peace for eternity */
564}
565
566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100567/* either we find an LF at <ptr> or we jump to <bad>.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100569#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
570
571/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
572 * otherwise to <http_msg_ood> with <state> set to <st>.
573 */
574#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
575 ptr++; \
576 if (likely(ptr < end)) \
577 goto good; \
578 else { \
579 state = (st); \
580 goto http_msg_ood; \
581 } \
582 } while (0)
583
Willy Tarreau117f59e2007-03-04 18:17:17 +0100584
585/*
586 * Capture headers from message starting at <som> according to header list
587 * <cap_hdr>, and fill the <idx> structure appropriately.
588 */
589void capture_headers(char *som, struct hdr_idx *idx,
590 char **cap, struct cap_hdr *cap_hdr)
591{
592 char *eol, *sol, *col, *sov;
593 int cur_idx;
594 struct cap_hdr *h;
595 int len;
596
597 sol = som + hdr_idx_first_pos(idx);
598 cur_idx = hdr_idx_first_idx(idx);
599
600 while (cur_idx) {
601 eol = sol + idx->v[cur_idx].len;
602
603 col = sol;
604 while (col < eol && *col != ':')
605 col++;
606
607 sov = col + 1;
608 while (sov < eol && http_is_lws[(unsigned char)*sov])
609 sov++;
610
611 for (h = cap_hdr; h; h = h->next) {
612 if ((h->namelen == col - sol) &&
613 (strncasecmp(sol, h->name, h->namelen) == 0)) {
614 if (cap[h->index] == NULL)
615 cap[h->index] =
616 pool_alloc_from(h->pool, h->len + 1);
617
618 if (cap[h->index] == NULL) {
619 Alert("HTTP capture : out of memory.\n");
620 continue;
621 }
622
623 len = eol - sov;
624 if (len > h->len)
625 len = h->len;
626
627 memcpy(cap[h->index], sov, len);
628 cap[h->index][len]=0;
629 }
630 }
631 sol = eol + idx->v[cur_idx].cr + 1;
632 cur_idx = idx->v[cur_idx].next;
633 }
634}
635
636
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100638 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100639 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
640 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
641 * will give undefined results.
642 * Note that it is upon the caller's responsibility to ensure that ptr < end,
643 * and that msg->sol points to the beginning of the response.
644 * If a complete line is found (which implies that at least one CR or LF is
645 * found before <end>, the updated <ptr> is returned, otherwise NULL is
646 * returned indicating an incomplete line (which does not mean that parts have
647 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
648 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
649 * upon next call.
650 *
651 * This function was intentionnally designed to be called from
652 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
653 * within its state machine and use the same macros, hence the need for same
654 * labels and variable names.
655 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100656const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100657 const char *ptr, const char *end,
658 char **ret_ptr, int *ret_state)
659{
660 __label__
661 http_msg_rpver,
662 http_msg_rpver_sp,
663 http_msg_rpcode,
664 http_msg_rpcode_sp,
665 http_msg_rpreason,
666 http_msg_rpline_eol,
667 http_msg_ood, /* out of data */
668 http_msg_invalid;
669
670 switch (state) {
671 http_msg_rpver:
672 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100673 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100674 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
675
676 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100677 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100678 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
679 }
680 goto http_msg_invalid;
681
682 http_msg_rpver_sp:
683 case HTTP_MSG_RPVER_SP:
684 if (likely(!HTTP_IS_LWS(*ptr))) {
685 msg->sl.st.c = ptr - msg_buf;
686 goto http_msg_rpcode;
687 }
688 if (likely(HTTP_IS_SPHT(*ptr)))
689 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
690 /* so it's a CR/LF, this is invalid */
691 goto http_msg_invalid;
692
693 http_msg_rpcode:
694 case HTTP_MSG_RPCODE:
695 if (likely(!HTTP_IS_LWS(*ptr)))
696 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
697
698 if (likely(HTTP_IS_SPHT(*ptr))) {
699 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
700 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
701 }
702
703 /* so it's a CR/LF, so there is no reason phrase */
704 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
705 http_msg_rsp_reason:
706 /* FIXME: should we support HTTP responses without any reason phrase ? */
707 msg->sl.st.r = ptr - msg_buf;
708 msg->sl.st.r_l = 0;
709 goto http_msg_rpline_eol;
710
711 http_msg_rpcode_sp:
712 case HTTP_MSG_RPCODE_SP:
713 if (likely(!HTTP_IS_LWS(*ptr))) {
714 msg->sl.st.r = ptr - msg_buf;
715 goto http_msg_rpreason;
716 }
717 if (likely(HTTP_IS_SPHT(*ptr)))
718 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
719 /* so it's a CR/LF, so there is no reason phrase */
720 goto http_msg_rsp_reason;
721
722 http_msg_rpreason:
723 case HTTP_MSG_RPREASON:
724 if (likely(!HTTP_IS_CRLF(*ptr)))
725 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
726 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
727 http_msg_rpline_eol:
728 /* We have seen the end of line. Note that we do not
729 * necessarily have the \n yet, but at least we know that we
730 * have EITHER \r OR \n, otherwise the response would not be
731 * complete. We can then record the response length and return
732 * to the caller which will be able to register it.
733 */
734 msg->sl.st.l = ptr - msg->sol;
735 return ptr;
736
737#ifdef DEBUG_FULL
738 default:
739 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
740 exit(1);
741#endif
742 }
743
744 http_msg_ood:
745 /* out of data */
746 if (ret_state)
747 *ret_state = state;
748 if (ret_ptr)
749 *ret_ptr = (char *)ptr;
750 return NULL;
751
752 http_msg_invalid:
753 /* invalid message */
754 if (ret_state)
755 *ret_state = HTTP_MSG_ERROR;
756 return NULL;
757}
758
759
760/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100761 * This function parses a request line between <ptr> and <end>, starting with
762 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
763 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
764 * will give undefined results.
765 * Note that it is upon the caller's responsibility to ensure that ptr < end,
766 * and that msg->sol points to the beginning of the request.
767 * If a complete line is found (which implies that at least one CR or LF is
768 * found before <end>, the updated <ptr> is returned, otherwise NULL is
769 * returned indicating an incomplete line (which does not mean that parts have
770 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
771 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
772 * upon next call.
773 *
774 * This function was intentionnally designed to be called from
775 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
776 * within its state machine and use the same macros, hence the need for same
777 * labels and variable names.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100779const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100780 const char *ptr, const char *end,
781 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100783 __label__
784 http_msg_rqmeth,
785 http_msg_rqmeth_sp,
786 http_msg_rquri,
787 http_msg_rquri_sp,
788 http_msg_rqver,
789 http_msg_rqline_eol,
790 http_msg_ood, /* out of data */
791 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100793 switch (state) {
794 http_msg_rqmeth:
795 case HTTP_MSG_RQMETH:
796 if (likely(HTTP_IS_TOKEN(*ptr)))
797 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100799 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100800 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100801 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
802 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100804 if (likely(HTTP_IS_CRLF(*ptr))) {
805 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100806 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100807 http_msg_req09_uri:
808 msg->sl.rq.u = ptr - msg_buf;
809 http_msg_req09_uri_e:
810 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
811 http_msg_req09_ver:
812 msg->sl.rq.v = ptr - msg_buf;
813 msg->sl.rq.v_l = 0;
814 goto http_msg_rqline_eol;
815 }
816 goto http_msg_invalid;
817
818 http_msg_rqmeth_sp:
819 case HTTP_MSG_RQMETH_SP:
820 if (likely(!HTTP_IS_LWS(*ptr))) {
821 msg->sl.rq.u = ptr - msg_buf;
822 goto http_msg_rquri;
823 }
824 if (likely(HTTP_IS_SPHT(*ptr)))
825 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
826 /* so it's a CR/LF, meaning an HTTP 0.9 request */
827 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100828
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100829 http_msg_rquri:
830 case HTTP_MSG_RQURI:
831 if (likely(!HTTP_IS_LWS(*ptr)))
832 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100833
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100834 if (likely(HTTP_IS_SPHT(*ptr))) {
835 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
836 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
837 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100839 /* so it's a CR/LF, meaning an HTTP 0.9 request */
840 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100841
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100842 http_msg_rquri_sp:
843 case HTTP_MSG_RQURI_SP:
844 if (likely(!HTTP_IS_LWS(*ptr))) {
845 msg->sl.rq.v = ptr - msg_buf;
846 goto http_msg_rqver;
847 }
848 if (likely(HTTP_IS_SPHT(*ptr)))
849 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
850 /* so it's a CR/LF, meaning an HTTP 0.9 request */
851 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100853 http_msg_rqver:
854 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100855 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100856 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100857
858 if (likely(HTTP_IS_CRLF(*ptr))) {
859 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
860 http_msg_rqline_eol:
861 /* We have seen the end of line. Note that we do not
862 * necessarily have the \n yet, but at least we know that we
863 * have EITHER \r OR \n, otherwise the request would not be
864 * complete. We can then record the request length and return
865 * to the caller which will be able to register it.
866 */
867 msg->sl.rq.l = ptr - msg->sol;
868 return ptr;
869 }
870
871 /* neither an HTTP_VER token nor a CRLF */
872 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100873
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100874#ifdef DEBUG_FULL
875 default:
876 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
877 exit(1);
878#endif
879 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100880
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100881 http_msg_ood:
882 /* out of data */
883 if (ret_state)
884 *ret_state = state;
885 if (ret_ptr)
886 *ret_ptr = (char *)ptr;
887 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100888
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100889 http_msg_invalid:
890 /* invalid message */
891 if (ret_state)
892 *ret_state = HTTP_MSG_ERROR;
893 return NULL;
894}
Willy Tarreau58f10d72006-12-04 02:26:12 +0100895
896
Willy Tarreau8973c702007-01-21 23:58:29 +0100897/*
898 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100899 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +0100900 * when data are missing and recalled at the exact same location with no
901 * information loss. The header index is re-initialized when switching from
902 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH.
903 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100904void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
905{
906 __label__
907 http_msg_rqbefore,
908 http_msg_rqbefore_cr,
909 http_msg_rqmeth,
910 http_msg_rqline_end,
911 http_msg_hdr_first,
912 http_msg_hdr_name,
913 http_msg_hdr_l1_sp,
914 http_msg_hdr_l1_lf,
915 http_msg_hdr_l1_lws,
916 http_msg_hdr_val,
917 http_msg_hdr_l2_lf,
918 http_msg_hdr_l2_lws,
919 http_msg_complete_header,
920 http_msg_last_lf,
921 http_msg_ood, /* out of data */
922 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100923
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100924 int state; /* updated only when leaving the FSM */
925 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100926
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100927 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100928 ptr = buf->lr;
929 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100930
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100931 if (unlikely(ptr >= end))
932 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100933
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100934 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100935 /*
936 * First, states that are specific to the response only.
937 * We check them first so that request and headers are
938 * closer to each other (accessed more often).
939 */
940 http_msg_rpbefore:
941 case HTTP_MSG_RPBEFORE:
942 if (likely(HTTP_IS_TOKEN(*ptr))) {
943 if (likely(ptr == buf->data)) {
944 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100945 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +0100946 } else {
947#if PARSE_PRESERVE_EMPTY_LINES
948 /* only skip empty leading lines, don't remove them */
949 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100950 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +0100951#else
952 /* Remove empty leading lines, as recommended by
953 * RFC2616. This takes a lot of time because we
954 * must move all the buffer backwards, but this
955 * is rarely needed. The method above will be
956 * cleaner when we'll be able to start sending
957 * the request from any place in the buffer.
958 */
959 buf->lr = ptr;
960 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100961 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 msg->sol = buf->data;
963 ptr = buf->data;
964 end = buf->r;
965#endif
966 }
967 hdr_idx_init(idx);
968 state = HTTP_MSG_RPVER;
969 goto http_msg_rpver;
970 }
971
972 if (unlikely(!HTTP_IS_CRLF(*ptr)))
973 goto http_msg_invalid;
974
975 if (unlikely(*ptr == '\n'))
976 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
977 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
978 /* stop here */
979
980 http_msg_rpbefore_cr:
981 case HTTP_MSG_RPBEFORE_CR:
982 EXPECT_LF_HERE(ptr, http_msg_invalid);
983 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
984 /* stop here */
985
986 http_msg_rpver:
987 case HTTP_MSG_RPVER:
988 case HTTP_MSG_RPVER_SP:
989 case HTTP_MSG_RPCODE:
990 case HTTP_MSG_RPCODE_SP:
991 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +0100992 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100993 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 if (unlikely(!ptr))
995 return;
996
997 /* we have a full response and we know that we have either a CR
998 * or an LF at <ptr>.
999 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001000 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001001 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1002
1003 msg->sol = ptr;
1004 if (likely(*ptr == '\r'))
1005 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1006 goto http_msg_rpline_end;
1007
1008 http_msg_rpline_end:
1009 case HTTP_MSG_RPLINE_END:
1010 /* msg->sol must point to the first of CR or LF. */
1011 EXPECT_LF_HERE(ptr, http_msg_invalid);
1012 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1013 /* stop here */
1014
1015 /*
1016 * Second, states that are specific to the request only
1017 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001018 http_msg_rqbefore:
1019 case HTTP_MSG_RQBEFORE:
1020 if (likely(HTTP_IS_TOKEN(*ptr))) {
1021 if (likely(ptr == buf->data)) {
1022 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001023 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001024 } else {
1025#if PARSE_PRESERVE_EMPTY_LINES
1026 /* only skip empty leading lines, don't remove them */
1027 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001028 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001029#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001030 /* Remove empty leading lines, as recommended by
1031 * RFC2616. This takes a lot of time because we
1032 * must move all the buffer backwards, but this
1033 * is rarely needed. The method above will be
1034 * cleaner when we'll be able to start sending
1035 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001036 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001037 buf->lr = ptr;
1038 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001039 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040 msg->sol = buf->data;
1041 ptr = buf->data;
1042 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001043#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001044 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001045 /* we will need this when keep-alive will be supported
1046 hdr_idx_init(idx);
1047 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001048 state = HTTP_MSG_RQMETH;
1049 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001050 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001052 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1053 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001054
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001055 if (unlikely(*ptr == '\n'))
1056 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1057 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001058 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001059
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001060 http_msg_rqbefore_cr:
1061 case HTTP_MSG_RQBEFORE_CR:
1062 EXPECT_LF_HERE(ptr, http_msg_invalid);
1063 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001064 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001065
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066 http_msg_rqmeth:
1067 case HTTP_MSG_RQMETH:
1068 case HTTP_MSG_RQMETH_SP:
1069 case HTTP_MSG_RQURI:
1070 case HTTP_MSG_RQURI_SP:
1071 case HTTP_MSG_RQVER:
1072 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001073 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 if (unlikely(!ptr))
1075 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 /* we have a full request and we know that we have either a CR
1078 * or an LF at <ptr>.
1079 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001080 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 msg->sol = ptr;
1084 if (likely(*ptr == '\r'))
1085 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 http_msg_rqline_end:
1089 case HTTP_MSG_RQLINE_END:
1090 /* check for HTTP/0.9 request : no version information available.
1091 * msg->sol must point to the first of CR or LF.
1092 */
1093 if (unlikely(msg->sl.rq.v_l == 0))
1094 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001095
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 EXPECT_LF_HERE(ptr, http_msg_invalid);
1097 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001098 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001099
Willy Tarreau8973c702007-01-21 23:58:29 +01001100 /*
1101 * Common states below
1102 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 http_msg_hdr_first:
1104 case HTTP_MSG_HDR_FIRST:
1105 msg->sol = ptr;
1106 if (likely(!HTTP_IS_CRLF(*ptr))) {
1107 goto http_msg_hdr_name;
1108 }
1109
1110 if (likely(*ptr == '\r'))
1111 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1112 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 http_msg_hdr_name:
1115 case HTTP_MSG_HDR_NAME:
1116 /* assumes msg->sol points to the first char */
1117 if (likely(HTTP_IS_TOKEN(*ptr)))
1118 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001119
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001120 if (likely(*ptr == ':')) {
1121 msg->col = ptr - buf->data;
1122 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001124
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001125 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001126
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001127 http_msg_hdr_l1_sp:
1128 case HTTP_MSG_HDR_L1_SP:
1129 /* assumes msg->sol points to the first char and msg->col to the colon */
1130 if (likely(HTTP_IS_SPHT(*ptr)))
1131 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001132
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001133 /* header value can be basically anything except CR/LF */
1134 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001135
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001136 if (likely(!HTTP_IS_CRLF(*ptr))) {
1137 goto http_msg_hdr_val;
1138 }
1139
1140 if (likely(*ptr == '\r'))
1141 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1142 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144 http_msg_hdr_l1_lf:
1145 case HTTP_MSG_HDR_L1_LF:
1146 EXPECT_LF_HERE(ptr, http_msg_invalid);
1147 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001149 http_msg_hdr_l1_lws:
1150 case HTTP_MSG_HDR_L1_LWS:
1151 if (likely(HTTP_IS_SPHT(*ptr))) {
1152 /* replace HT,CR,LF with spaces */
1153 for (; buf->data+msg->sov < ptr; msg->sov++)
1154 buf->data[msg->sov] = ' ';
1155 goto http_msg_hdr_l1_sp;
1156 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001157 /* we had a header consisting only in spaces ! */
1158 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 goto http_msg_complete_header;
1160
1161 http_msg_hdr_val:
1162 case HTTP_MSG_HDR_VAL:
1163 /* assumes msg->sol points to the first char, msg->col to the
1164 * colon, and msg->sov points to the first character of the
1165 * value.
1166 */
1167 if (likely(!HTTP_IS_CRLF(*ptr)))
1168 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170 msg->eol = ptr;
1171 /* Note: we could also copy eol into ->eoh so that we have the
1172 * real header end in case it ends with lots of LWS, but is this
1173 * really needed ?
1174 */
1175 if (likely(*ptr == '\r'))
1176 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1177 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001178
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 http_msg_hdr_l2_lf:
1180 case HTTP_MSG_HDR_L2_LF:
1181 EXPECT_LF_HERE(ptr, http_msg_invalid);
1182 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001183
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 http_msg_hdr_l2_lws:
1185 case HTTP_MSG_HDR_L2_LWS:
1186 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1187 /* LWS: replace HT,CR,LF with spaces */
1188 for (; msg->eol < ptr; msg->eol++)
1189 *msg->eol = ' ';
1190 goto http_msg_hdr_val;
1191 }
1192 http_msg_complete_header:
1193 /*
1194 * It was a new header, so the last one is finished.
1195 * Assumes msg->sol points to the first char, msg->col to the
1196 * colon, msg->sov points to the first character of the value
1197 * and msg->eol to the first CR or LF so we know how the line
1198 * ends. We insert last header into the index.
1199 */
1200 /*
1201 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1202 write(2, msg->sol, msg->eol-msg->sol);
1203 fprintf(stderr,"\n");
1204 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001205
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001206 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1207 idx, idx->tail) < 0))
1208 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001209
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 msg->sol = ptr;
1211 if (likely(!HTTP_IS_CRLF(*ptr))) {
1212 goto http_msg_hdr_name;
1213 }
1214
1215 if (likely(*ptr == '\r'))
1216 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1217 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001218
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001219 http_msg_last_lf:
1220 case HTTP_MSG_LAST_LF:
1221 /* Assumes msg->sol points to the first of either CR or LF */
1222 EXPECT_LF_HERE(ptr, http_msg_invalid);
1223 ptr++;
1224 buf->lr = ptr;
1225 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001226 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001227 return;
1228#ifdef DEBUG_FULL
1229 default:
1230 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1231 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001232#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233 }
1234 http_msg_ood:
1235 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001236 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 buf->lr = ptr;
1238 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 http_msg_invalid:
1241 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001242 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 return;
1244}
1245
1246/*
1247 * manages the client FSM and its socket. BTW, it also tries to handle the
1248 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1249 * 0 else.
1250 */
1251int process_cli(struct session *t)
1252{
1253 int s = t->srv_state;
1254 int c = t->cli_state;
1255 struct buffer *req = t->req;
1256 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1259 cli_stnames[c], srv_stnames[s],
1260 MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
1261 req->rex.tv_sec, req->rex.tv_usec,
1262 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 if (c == CL_STHEADERS) {
1265 /*
1266 * Now parse the partial (or complete) lines.
1267 * We will check the request syntax, and also join multi-line
1268 * headers. An index of all the lines will be elaborated while
1269 * parsing.
1270 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001271 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001274 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 * req->data + req->eoh = end of processed headers / start of current one
1276 * req->data + req->eol = end of current header or line (LF or CRLF)
1277 * req->lr = first non-visited byte
1278 * req->r = end of data
1279 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001280
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001282 struct http_txn *txn = &t->txn;
1283 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001287 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 /* 1: we might have to print this header in debug mode */
1290 if (unlikely((global.mode & MODE_DEBUG) &&
1291 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001292 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001295 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 eol = sol + msg->sl.rq.l;
1297 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001298
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001299 sol += hdr_idx_first_pos(&txn->hdr_idx);
1300 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001303 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001305 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1306 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 }
1308 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001309
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
1311 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001313 * If not so, we check the FD and buffer states before leaving.
1314 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1316 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317 *
1318 */
1319
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001320 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 /*
1322 * First, let's catch bad requests.
1323 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001324 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001326
1327 /* 1: Since we are in header mode, if there's no space
1328 * left for headers, we won't be able to free more
1329 * later, so the session will never terminate. We
1330 * must terminate it now.
1331 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 if (unlikely(req->l >= req->rlim - req->data)) {
1333 /* FIXME: check if URI is set and return Status
1334 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001335 */
Willy Tarreau06619262006-12-17 08:37:22 +01001336 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001337 }
1338
1339 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1341 /* read error, or last read : give up. */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001342 tv_eternity(&req->rex);
1343 fd_delete(t->cli_fd);
1344 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001345 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346 if (!(t->flags & SN_ERR_MASK))
1347 t->flags |= SN_ERR_CLICL;
1348 if (!(t->flags & SN_FINST_MASK))
1349 t->flags |= SN_FINST_R;
1350 return 1;
1351 }
1352
1353 /* 3: has the read timeout expired ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 else if (unlikely(tv_cmp2_ms(&req->rex, &now) <= 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001356 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001357 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001358 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001359 if (!(t->flags & SN_ERR_MASK))
1360 t->flags |= SN_ERR_CLITO;
1361 if (!(t->flags & SN_FINST_MASK))
1362 t->flags |= SN_FINST_R;
1363 return 1;
1364 }
1365
1366 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 else if (unlikely(! MY_FD_ISSET(t->cli_fd, StaticReadEvent))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001368 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
1369 * full. We cannot loop here since stream_sock_read will disable it only if
1370 * req->l == rlim-data
1371 */
1372 MY_FD_SET(t->cli_fd, StaticReadEvent);
1373 if (t->fe->clitimeout)
1374 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
1375 else
1376 tv_eternity(&req->rex);
1377 }
1378 return t->cli_state != CL_STHEADERS;
1379 }
1380
1381
1382 /****************************************************************
1383 * More interesting part now : we know that we have a complete *
1384 * request which at least looks like HTTP. We have an indicator *
1385 * of each header's length, so we can parse them quickly. *
1386 ****************************************************************/
1387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 /*
1389 * 1: identify the method
1390 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001391 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001392
1393 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001395 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001397 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 if (unlikely((t->fe->monitor_uri_len != 0) &&
1399 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1400 !memcmp(&req->data[msg->sl.rq.u],
1401 t->fe->monitor_uri,
1402 t->fe->monitor_uri_len))) {
1403 /*
1404 * We have found the monitor URI
1405 */
1406 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001407 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 client_retnclose(t, &http_200_chunk);
1409 goto return_prx_cond;
1410 }
1411
1412 /*
1413 * 3: Maybe we have to copy the original REQURI for the logs ?
1414 * Note: we cannot log anymore if the request has been
1415 * classified as invalid.
1416 */
1417 if (unlikely(t->logs.logwait & LW_REQ)) {
1418 /* we have a complete HTTP request that we must log */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001419 if ((txn->uri = pool_alloc(requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 int urilen = msg->sl.rq.l;
1421
1422 if (urilen >= REQURI_LEN)
1423 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001424 memcpy(txn->uri, &req->data[msg->som], urilen);
1425 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426
1427 if (!(t->logs.logwait &= ~LW_REQ))
1428 sess_log(t);
1429 } else {
1430 Alert("HTTP logging : out of memory.\n");
1431 }
1432 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau06619262006-12-17 08:37:22 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1436 if (unlikely(msg->sl.rq.v_l == 0)) {
1437 int delta;
1438 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001439 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 cur_end = msg->sol + msg->sl.rq.l;
1441 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (msg->sl.rq.u_l == 0) {
1444 /* if no URI was set, add "/" */
1445 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1446 cur_end += delta;
1447 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001448 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 /* add HTTP version */
1450 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1451 msg->eoh += delta;
1452 cur_end += delta;
1453 cur_end = (char *)http_parse_reqline(msg, req->data,
1454 HTTP_MSG_RQMETH,
1455 msg->sol, cur_end + 1,
1456 NULL, NULL);
1457 if (unlikely(!cur_end))
1458 goto return_bad_req;
1459
1460 /* we have a full HTTP/1.0 request now and we know that
1461 * we have either a CR or an LF at <ptr>.
1462 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001463 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464 }
1465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466
1467 /* 5: we may need to capture headers */
Willy Tarreau117f59e2007-03-04 18:17:17 +01001468 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->fiprm->req_cap))
1469 capture_headers(req->data + msg->som, &txn->hdr_idx,
1470 txn->req.cap, t->fe->fiprm->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001471
1472 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001474 * As opposed to version 1.2, now they will be evaluated in the
1475 * filters order and not in the header order. This means that
1476 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001477 *
1478 * We can now check whether we want to switch to another
1479 * backend, in which case we will re-check the backend's
1480 * filters and various options. In order to support 3-level
1481 * switching, here's how we should proceed :
1482 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001483 * a) run be->fiprm.
1484 * if (switch) then switch ->be to the new backend.
1485 * b) run be->fiprm if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001486 * There cannot be any switch from there, so ->be cannot be
1487 * changed anymore.
1488 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001489 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001490 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001491 * The response path will be able to apply either ->be, or
1492 * ->be then ->fe filters in order to match the reverse of
1493 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001494 */
1495
Willy Tarreau06619262006-12-17 08:37:22 +01001496 do {
Willy Tarreau830ff452006-12-17 19:31:23 +01001497 struct proxy *rule_set = t->be->fiprm;
1498 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001499
Willy Tarreau06619262006-12-17 08:37:22 +01001500 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001501 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1503 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001504 }
1505
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001506 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1507 /* to ensure correct connection accounting on
1508 * the backend, we count the connection for the
1509 * one managing the queue.
1510 */
1511 t->be->beprm->beconn++;
1512 if (t->be->beprm->beconn > t->be->beprm->beconn_max)
1513 t->be->beprm->beconn_max = t->be->beprm->beconn;
1514 t->be->beprm->cum_beconn++;
1515 t->flags |= SN_BE_ASSIGNED;
1516 }
1517
Willy Tarreau06619262006-12-17 08:37:22 +01001518 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001519 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001520 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001521 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001522 /* let's log the request time */
1523 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001524 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001525 goto return_prx_cond;
1526 }
1527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 /* We might have to check for "Connection:" */
1529 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
1530 !(t->flags & SN_CONN_CLOSED)) {
1531 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001532 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001534
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001535 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 old_idx = 0;
1537
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001538 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1539 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 cur_ptr = cur_next;
1541 cur_end = cur_ptr + cur_hdr->len;
1542 cur_next = cur_end + cur_hdr->cr + 1;
1543
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001544 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1545 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 /* 3 possibilities :
1547 * - we have already set Connection: close,
1548 * so we remove this line.
1549 * - we have not yet set Connection: close,
1550 * but this line indicates close. We leave
1551 * it untouched and set the flag.
1552 * - we have not yet set Connection: close,
1553 * and this line indicates non-close. We
1554 * replace it.
1555 */
1556 if (t->flags & SN_CONN_CLOSED) {
1557 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001558 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001560 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1561 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 cur_hdr->len = 0;
1563 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001564 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1565 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1566 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 cur_next += delta;
1568 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001569 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 }
1571 t->flags |= SN_CONN_CLOSED;
1572 }
1573 }
1574 old_idx = cur_idx;
1575 }
1576
1577 /* add request headers from the rule sets in the same order */
1578 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001579 if (unlikely(http_header_add_tail(req,
1580 &txn->req,
1581 &txn->hdr_idx,
1582 rule_set->req_add[cur_idx])) < 0)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 goto return_bad_req;
1584 }
Willy Tarreau06619262006-12-17 08:37:22 +01001585 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001588 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001589 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001590 /* we have to check the URI and auth for this request */
1591 if (stats_check_uri_auth(t, rule_set))
1592 return 1;
1593 }
1594
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001595 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1596 /* No backend was set, but there was a default
1597 * backend set in the frontend, so we use it and
1598 * loop again.
1599 */
1600 t->be = cur_proxy->defbe.be;
1601 t->be->beprm->beconn++;
1602 if (t->be->beprm->beconn > t->be->beprm->beconn_max)
1603 t->be->beprm->beconn_max = t->be->beprm->beconn;
1604 t->be->beprm->cum_beconn++;
1605 t->flags |= SN_BE_ASSIGNED;
1606 }
1607 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001608
Willy Tarreau58f10d72006-12-04 02:26:12 +01001609
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001610 if (!(t->flags & SN_BE_ASSIGNED)) {
1611 /* To ensure correct connection accounting on
1612 * the backend, we count the connection for the
1613 * one managing the queue.
1614 */
1615 t->be->beprm->beconn++;
1616 if (t->be->beprm->beconn > t->be->beprm->beconn_max)
1617 t->be->beprm->beconn_max = t->be->beprm->beconn;
1618 t->be->beprm->cum_beconn++;
1619 t->flags |= SN_BE_ASSIGNED;
1620 }
1621
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001622 /*
1623 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001624 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001625 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001626 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001627 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001628
Willy Tarreau58f10d72006-12-04 02:26:12 +01001629
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631
Willy Tarreau2a324282006-12-05 00:05:46 +01001632 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001634 * so let's do the same now.
1635 */
1636
1637 /* It needs to look into the URI */
Willy Tarreau830ff452006-12-17 19:31:23 +01001638 if (t->be->beprm->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001639 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001640 }
1641
1642
1643 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001644 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001645 * Note that doing so might move headers in the request, but
1646 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001647 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001648 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001649 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001650 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001651
Willy Tarreau58f10d72006-12-04 02:26:12 +01001652
Willy Tarreau2a324282006-12-05 00:05:46 +01001653 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001654 * 9: add X-Forwarded-For if either the frontend or the backend
1655 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001656 */
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001657 if ((t->fe->options | t->be->beprm->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001658 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001659 /* Add an X-Forwarded-For header unless the source IP is
1660 * in the 'except' network range.
1661 */
1662 if ((!t->fe->except_mask.s_addr ||
1663 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1664 != t->fe->except_net.s_addr) &&
1665 (!t->be->except_mask.s_addr ||
1666 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1667 != t->be->except_net.s_addr)) {
1668 int len;
1669 unsigned char *pn;
1670 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001671
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001672 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1673 pn[0], pn[1], pn[2], pn[3]);
1674
1675 if (unlikely(http_header_add_tail2(req, &txn->req,
1676 &txn->hdr_idx, trash, len)) < 0)
1677 goto return_bad_req;
1678 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001679 }
1680 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001681 /* FIXME: for the sake of completeness, we should also support
1682 * 'except' here, although it is mostly useless in this case.
1683 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001684 int len;
1685 char pn[INET6_ADDRSTRLEN];
1686 inet_ntop(AF_INET6,
1687 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1688 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001689 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1690 if (unlikely(http_header_add_tail2(req, &txn->req,
1691 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001692 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001693 }
1694 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001695
Willy Tarreau2a324282006-12-05 00:05:46 +01001696 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02001698 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01001699 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02001700 if (!(t->flags & SN_CONN_CLOSED) &&
1701 ((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE)) {
1702 if ((unlikely(msg->sl.rq.v_l != 8) ||
1703 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
1704 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001705 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001706 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001707 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001708 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001709
Willy Tarreau2a324282006-12-05 00:05:46 +01001710 /*************************************************************
1711 * OK, that's finished for the headers. We have done what we *
1712 * could. Let's switch to the DATA state. *
1713 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001714
Willy Tarreau2a324282006-12-05 00:05:46 +01001715 t->cli_state = CL_STDATA;
1716 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001717
Willy Tarreau2a324282006-12-05 00:05:46 +01001718 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001719
Willy Tarreau2a324282006-12-05 00:05:46 +01001720 if (!t->fe->clitimeout ||
Willy Tarreau830ff452006-12-17 19:31:23 +01001721 (t->srv_state < SV_STDATA && t->be->beprm->srvtimeout)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001722 /* If the client has no timeout, or if the server is not ready yet,
1723 * and we know for sure that it can expire, then it's cleaner to
1724 * disable the timeout on the client side so that too low values
1725 * cannot make the sessions abort too early.
1726 *
1727 * FIXME-20050705: the server needs a way to re-enable this time-out
1728 * when it switches its state, otherwise a client can stay connected
1729 * indefinitely. This now seems to be OK.
1730 */
1731 tv_eternity(&req->rex);
1732 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001733
Willy Tarreau2a324282006-12-05 00:05:46 +01001734 /* When a connection is tarpitted, we use the queue timeout for the
1735 * tarpit delay, which currently happens to be the server's connect
1736 * timeout. If unset, then set it to zero because we really want it
1737 * to expire at one moment.
1738 */
Willy Tarreau3d300592007-03-18 18:34:41 +01001739 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001740 t->req->l = 0;
1741 /* flush the request so that we can drop the connection early
1742 * if the client closes first.
1743 */
1744 tv_delayfrom(&req->cex, &now,
Willy Tarreau830ff452006-12-17 19:31:23 +01001745 t->be->beprm->contimeout ? t->be->beprm->contimeout : 0);
Willy Tarreau2a324282006-12-05 00:05:46 +01001746 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001747
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01001749 goto process_data;
1750
1751 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001752 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001753 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01001754 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001755 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01001756 return_prx_cond:
1757 if (!(t->flags & SN_ERR_MASK))
1758 t->flags |= SN_ERR_PRXCOND;
1759 if (!(t->flags & SN_FINST_MASK))
1760 t->flags |= SN_FINST_R;
1761 return 1;
1762
Willy Tarreaubaaee002006-06-26 02:48:02 +02001763 }
1764 else if (c == CL_STDATA) {
1765 process_data:
1766 /* FIXME: this error handling is partly buggy because we always report
1767 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
1768 * or HEADER phase. BTW, it's not logical to expire the client while
1769 * we're waiting for the server to connect.
1770 */
1771 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001772 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001773 tv_eternity(&req->rex);
1774 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001775 fd_delete(t->cli_fd);
1776 t->cli_state = CL_STCLOSE;
1777 if (!(t->flags & SN_ERR_MASK))
1778 t->flags |= SN_ERR_CLICL;
1779 if (!(t->flags & SN_FINST_MASK)) {
1780 if (t->pend_pos)
1781 t->flags |= SN_FINST_Q;
1782 else if (s == SV_STCONN)
1783 t->flags |= SN_FINST_C;
1784 else
1785 t->flags |= SN_FINST_D;
1786 }
1787 return 1;
1788 }
1789 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001790 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001791 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001792 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001793 shutdown(t->cli_fd, SHUT_RD);
1794 t->cli_state = CL_STSHUTR;
1795 return 1;
1796 }
1797 /* last server read and buffer empty */
1798 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001799 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001800 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001801 shutdown(t->cli_fd, SHUT_WR);
1802 /* We must ensure that the read part is still alive when switching
1803 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001804 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001805 if (t->fe->clitimeout)
1806 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001807 t->cli_state = CL_STSHUTW;
1808 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
1809 return 1;
1810 }
1811 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001812 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001813 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001814 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001815 shutdown(t->cli_fd, SHUT_RD);
1816 t->cli_state = CL_STSHUTR;
1817 if (!(t->flags & SN_ERR_MASK))
1818 t->flags |= SN_ERR_CLITO;
1819 if (!(t->flags & SN_FINST_MASK)) {
1820 if (t->pend_pos)
1821 t->flags |= SN_FINST_Q;
1822 else if (s == SV_STCONN)
1823 t->flags |= SN_FINST_C;
1824 else
1825 t->flags |= SN_FINST_D;
1826 }
1827 return 1;
1828 }
1829 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02001830 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001831 MY_FD_CLR(t->cli_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001832 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001833 shutdown(t->cli_fd, SHUT_WR);
1834 /* We must ensure that the read part is still alive when switching
1835 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02001836 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001837 if (t->fe->clitimeout)
1838 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001839
1840 t->cli_state = CL_STSHUTW;
1841 if (!(t->flags & SN_ERR_MASK))
1842 t->flags |= SN_ERR_CLITO;
1843 if (!(t->flags & SN_FINST_MASK)) {
1844 if (t->pend_pos)
1845 t->flags |= SN_FINST_Q;
1846 else if (s == SV_STCONN)
1847 t->flags |= SN_FINST_C;
1848 else
1849 t->flags |= SN_FINST_D;
1850 }
1851 return 1;
1852 }
1853
1854 if (req->l >= req->rlim - req->data) {
1855 /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02001856 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02001857 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02001858 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02001859 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001860 }
1861 } else {
1862 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02001863 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
1864 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01001865 if (!t->fe->clitimeout ||
Willy Tarreau830ff452006-12-17 19:31:23 +01001866 (t->srv_state < SV_STDATA && t->be->beprm->srvtimeout))
Willy Tarreaubaaee002006-06-26 02:48:02 +02001867 /* If the client has no timeout, or if the server not ready yet, and we
1868 * know for sure that it can expire, then it's cleaner to disable the
1869 * timeout on the client side so that too low values cannot make the
1870 * sessions abort too early.
1871 */
Willy Tarreaud7971282006-07-29 18:36:34 +02001872 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001873 else
Willy Tarreau73de9892006-11-30 11:40:23 +01001874 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001875 }
1876 }
1877
1878 if ((rep->l == 0) ||
1879 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001880 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1881 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001882 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001883 }
1884 } else {
1885 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001886 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1887 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001888 if (t->fe->clitimeout) {
1889 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001890 /* FIXME: to prevent the client from expiring read timeouts during writes,
1891 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001892 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001893 }
1894 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001895 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001896 }
1897 }
1898 return 0; /* other cases change nothing */
1899 }
1900 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001901 if (rep->flags & BF_WRITE_ERROR) {
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 if (!(t->flags & SN_ERR_MASK))
1906 t->flags |= SN_ERR_CLICL;
1907 if (!(t->flags & SN_FINST_MASK)) {
1908 if (t->pend_pos)
1909 t->flags |= SN_FINST_Q;
1910 else if (s == SV_STCONN)
1911 t->flags |= SN_FINST_C;
1912 else
1913 t->flags |= SN_FINST_D;
1914 }
1915 return 1;
1916 }
1917 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
1918 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001919 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001920 fd_delete(t->cli_fd);
1921 t->cli_state = CL_STCLOSE;
1922 return 1;
1923 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001924 else if (tv_cmp2_ms(&rep->wex, &now) <= 0) {
1925 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001926 fd_delete(t->cli_fd);
1927 t->cli_state = CL_STCLOSE;
1928 if (!(t->flags & SN_ERR_MASK))
1929 t->flags |= SN_ERR_CLITO;
1930 if (!(t->flags & SN_FINST_MASK)) {
1931 if (t->pend_pos)
1932 t->flags |= SN_FINST_Q;
1933 else if (s == SV_STCONN)
1934 t->flags |= SN_FINST_C;
1935 else
1936 t->flags |= SN_FINST_D;
1937 }
1938 return 1;
1939 }
1940
1941 if (t->flags & SN_SELF_GEN) {
1942 produce_content(t);
1943 if (rep->l == 0) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001944 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001945 fd_delete(t->cli_fd);
1946 t->cli_state = CL_STCLOSE;
1947 return 1;
1948 }
1949 }
1950
1951 if ((rep->l == 0)
1952 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02001953 if (MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1954 MY_FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02001955 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001956 }
1957 } else {
1958 /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02001959 if (! MY_FD_ISSET(t->cli_fd, StaticWriteEvent)) {
1960 MY_FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau73de9892006-11-30 11:40:23 +01001961 if (t->fe->clitimeout) {
1962 tv_delayfrom(&rep->wex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001963 /* FIXME: to prevent the client from expiring read timeouts during writes,
1964 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02001965 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001966 }
1967 else
Willy Tarreaud7971282006-07-29 18:36:34 +02001968 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001969 }
1970 }
1971 return 0;
1972 }
1973 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001974 if (req->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001975 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001976 fd_delete(t->cli_fd);
1977 t->cli_state = CL_STCLOSE;
1978 if (!(t->flags & SN_ERR_MASK))
1979 t->flags |= SN_ERR_CLICL;
1980 if (!(t->flags & SN_FINST_MASK)) {
1981 if (t->pend_pos)
1982 t->flags |= SN_FINST_Q;
1983 else if (s == SV_STCONN)
1984 t->flags |= SN_FINST_C;
1985 else
1986 t->flags |= SN_FINST_D;
1987 }
1988 return 1;
1989 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02001990 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaud7971282006-07-29 18:36:34 +02001991 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001992 fd_delete(t->cli_fd);
1993 t->cli_state = CL_STCLOSE;
1994 return 1;
1995 }
Willy Tarreaud7971282006-07-29 18:36:34 +02001996 else if (tv_cmp2_ms(&req->rex, &now) <= 0) {
1997 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001998 fd_delete(t->cli_fd);
1999 t->cli_state = CL_STCLOSE;
2000 if (!(t->flags & SN_ERR_MASK))
2001 t->flags |= SN_ERR_CLITO;
2002 if (!(t->flags & SN_FINST_MASK)) {
2003 if (t->pend_pos)
2004 t->flags |= SN_FINST_Q;
2005 else if (s == SV_STCONN)
2006 t->flags |= SN_FINST_C;
2007 else
2008 t->flags |= SN_FINST_D;
2009 }
2010 return 1;
2011 }
2012 else if (req->l >= req->rlim - req->data) {
2013 /* no room to read more data */
2014
2015 /* FIXME-20050705: is it possible for a client to maintain a session
2016 * after the timeout by sending more data after it receives a close ?
2017 */
2018
Willy Tarreau2a429502006-10-15 14:52:29 +02002019 if (MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002020 /* stop reading until we get some space */
Willy Tarreau2a429502006-10-15 14:52:29 +02002021 MY_FD_CLR(t->cli_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002022 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002023 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2024 }
2025 } else {
2026 /* there's still some space in the buffer */
Willy Tarreau2a429502006-10-15 14:52:29 +02002027 if (! MY_FD_ISSET(t->cli_fd, StaticReadEvent)) {
2028 MY_FD_SET(t->cli_fd, StaticReadEvent);
Willy Tarreau73de9892006-11-30 11:40:23 +01002029 if (t->fe->clitimeout)
2030 tv_delayfrom(&req->rex, &now, t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002031 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002032 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002033 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2034 }
2035 }
2036 return 0;
2037 }
2038 else { /* CL_STCLOSE: nothing to do */
2039 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2040 int len;
Willy Tarreau830ff452006-12-17 19:31:23 +01002041 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 +02002042 write(1, trash, len);
2043 }
2044 return 0;
2045 }
2046 return 0;
2047}
2048
2049
2050/*
2051 * manages the server FSM and its socket. It returns 1 if a state has changed
2052 * (and a resync may be needed), 0 else.
2053 */
2054int process_srv(struct session *t)
2055{
2056 int s = t->srv_state;
2057 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002058 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002059 struct buffer *req = t->req;
2060 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002061 int conn_err;
2062
2063#ifdef DEBUG_FULL
2064 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2065#endif
2066 //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 +02002067 //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent),
2068 //MY_FD_ISSET(t->srv_fd, StaticReadEvent), MY_FD_ISSET(t->srv_fd, StaticWriteEvent)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002069 //);
2070 if (s == SV_STIDLE) {
2071 if (c == CL_STHEADERS)
2072 return 0; /* stay in idle, waiting for data to reach the client side */
2073 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2074 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002075 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002076 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002077 if (t->pend_pos)
2078 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2079 /* note that this must not return any error because it would be able to
2080 * overwrite the client_retnclose() output.
2081 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002082 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002083 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002084 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002085 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 +02002086
2087 return 1;
2088 }
2089 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002090 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002091 /* This connection is being tarpitted. The CLIENT side has
2092 * already set the connect expiration date to the right
2093 * timeout. We just have to check that it has not expired.
2094 */
2095 if (tv_cmp2_ms(&req->cex, &now) > 0)
2096 return 0;
2097
2098 /* We will set the queue timer to the time spent, just for
2099 * logging purposes. We fake a 500 server error, so that the
2100 * attacker will not suspect his connection has been tarpitted.
2101 * It will not cause trouble to the logs because we can exclude
2102 * the tarpitted connections by filtering on the 'PT' status flags.
2103 */
2104 tv_eternity(&req->cex);
2105 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2106 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002107 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002108 return 1;
2109 }
2110
Willy Tarreaubaaee002006-06-26 02:48:02 +02002111 /* Right now, we will need to create a connection to the server.
2112 * We might already have tried, and got a connection pending, in
2113 * which case we will not do anything till it's pending. It's up
2114 * to any other session to release it and wake us up again.
2115 */
2116 if (t->pend_pos) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002117 if (tv_cmp2_ms(&req->cex, &now) > 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002118 return 0;
2119 else {
2120 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002121 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002122 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2123 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002124 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002125 if (t->srv)
2126 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002127 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002128 return 1;
2129 }
2130 }
2131
2132 do {
2133 /* first, get a connection */
2134 if (srv_redispatch_connect(t))
2135 return t->srv_state != SV_STIDLE;
2136
2137 /* try to (re-)connect to the server, and fail if we expire the
2138 * number of retries.
2139 */
2140 if (srv_retryable_connect(t)) {
2141 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2142 return t->srv_state != SV_STIDLE;
2143 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002144 } while (1);
2145 }
2146 }
2147 else if (s == SV_STCONN) { /* connection in progress */
2148 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2149 (c == CL_STSHUTR &&
Willy Tarreau830ff452006-12-17 19:31:23 +01002150 (t->req->l == 0 || t->be->beprm->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002151 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002152 fd_delete(t->srv_fd);
2153 if (t->srv)
2154 t->srv->cur_sess--;
2155
2156 /* note that this must not return any error because it would be able to
2157 * overwrite the client_retnclose() output.
2158 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002159 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002160 return 1;
2161 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002162 if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) {
2163 //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 +02002164 return 0; /* nothing changed */
2165 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002166 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002167 /* timeout, asynchronous connect error or first write error */
2168 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2169
2170 fd_delete(t->srv_fd);
2171 if (t->srv)
2172 t->srv->cur_sess--;
2173
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002174 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002175 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2176 else
2177 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2178
2179 /* ensure that we have enough retries left */
2180 if (srv_count_retry_down(t, conn_err))
2181 return 1;
2182
Willy Tarreau830ff452006-12-17 19:31:23 +01002183 if (t->srv && t->conn_retries == 0 && t->be->beprm->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002184 /* We're on our last chance, and the REDISP option was specified.
2185 * We will ignore cookie and force to balance or use the dispatcher.
2186 */
2187 /* let's try to offer this slot to anybody */
Willy Tarreau830ff452006-12-17 19:31:23 +01002188 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002189 task_wakeup(&rq, t->srv->queue_mgt);
2190
2191 if (t->srv)
2192 t->srv->failed_conns++;
Willy Tarreau830ff452006-12-17 19:31:23 +01002193 t->be->beprm->failed_conns++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002194
2195 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2196 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002197 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002198
2199 /* first, get a connection */
2200 if (srv_redispatch_connect(t))
2201 return t->srv_state != SV_STIDLE;
2202 }
2203
Willy Tarreaubaaee002006-06-26 02:48:02 +02002204 do {
2205 /* Now we will try to either reconnect to the same server or
2206 * connect to another server. If the connection gets queued
2207 * because all servers are saturated, then we will go back to
2208 * the SV_STIDLE state.
2209 */
2210 if (srv_retryable_connect(t)) {
2211 t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now);
2212 return t->srv_state != SV_STCONN;
2213 }
2214
2215 /* we need to redispatch the connection to another server */
2216 if (srv_redispatch_connect(t))
2217 return t->srv_state != SV_STCONN;
2218 } while (1);
2219 }
2220 else { /* no error or write 0 */
2221 t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now);
2222
2223 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2224 if (req->l == 0) /* nothing to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002225 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002226 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002227 } else /* need the right to write */ {
Willy Tarreau2a429502006-10-15 14:52:29 +02002228 MY_FD_SET(t->srv_fd, StaticWriteEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002229 if (t->be->beprm->srvtimeout) {
2230 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002231 /* FIXME: to prevent the server from expiring read timeouts during writes,
2232 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002233 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002234 }
2235 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002236 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002237 }
2238
Willy Tarreau830ff452006-12-17 19:31:23 +01002239 if (t->be->beprm->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau2a429502006-10-15 14:52:29 +02002240 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002241 if (t->be->beprm->srvtimeout)
2242 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002243 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002244 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002245
2246 t->srv_state = SV_STDATA;
2247 if (t->srv)
2248 t->srv->cum_sess++;
2249 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2250
2251 /* if the user wants to log as soon as possible, without counting
2252 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002253 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002254 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
2255 sess_log(t);
2256 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002257#ifdef CONFIG_HAP_TCPSPLICE
2258 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2259 /* TCP splicing supported by both FE and BE */
2260 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2261 }
2262#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002263 }
2264 else {
2265 t->srv_state = SV_STHEADERS;
2266 if (t->srv)
2267 t->srv->cum_sess++;
2268 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002269 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2270 /* reset hdr_idx which was already initialized by the request.
2271 * right now, the http parser does it.
2272 * hdr_idx_init(&t->txn.hdr_idx);
2273 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002274 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002275 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002276 return 1;
2277 }
2278 }
2279 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002280 /*
2281 * Now parse the partial (or complete) lines.
2282 * We will check the response syntax, and also join multi-line
2283 * headers. An index of all the lines will be elaborated while
2284 * parsing.
2285 *
2286 * For the parsing, we use a 28 states FSM.
2287 *
2288 * Here is the information we currently have :
2289 * rep->data + req->som = beginning of response
2290 * rep->data + req->eoh = end of processed headers / start of current one
2291 * rep->data + req->eol = end of current header or line (LF or CRLF)
2292 * rep->lr = first non-visited byte
2293 * rep->r = end of data
2294 */
2295
2296 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002297 struct http_msg *msg = &txn->rsp;
2298 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002299
Willy Tarreaua15645d2007-03-18 16:22:39 +01002300 if (likely(rep->lr < rep->r))
2301 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002302
Willy Tarreaua15645d2007-03-18 16:22:39 +01002303 /* 1: we might have to print this header in debug mode */
2304 if (unlikely((global.mode & MODE_DEBUG) &&
2305 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2306 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2307 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002308
Willy Tarreaua15645d2007-03-18 16:22:39 +01002309 sol = rep->data + msg->som;
2310 eol = sol + msg->sl.rq.l;
2311 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002312
Willy Tarreaua15645d2007-03-18 16:22:39 +01002313 sol += hdr_idx_first_pos(&txn->hdr_idx);
2314 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002315
Willy Tarreaua15645d2007-03-18 16:22:39 +01002316 while (cur_idx) {
2317 eol = sol + txn->hdr_idx.v[cur_idx].len;
2318 debug_hdr("srvhdr", t, sol, eol);
2319 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2320 cur_idx = txn->hdr_idx.v[cur_idx].next;
2321 }
2322 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002323
Willy Tarreaubaaee002006-06-26 02:48:02 +02002324
Willy Tarreaua15645d2007-03-18 16:22:39 +01002325 if ((rep->l < rep->rlim - rep->data) && ! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2326 /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer
2327 * full. We cannot loop here since stream_sock_read will disable it only if
2328 * rep->l == rlim-data
2329 */
2330 MY_FD_SET(t->srv_fd, StaticReadEvent);
2331 if (t->be->beprm->srvtimeout)
2332 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2333 else
2334 tv_eternity(&rep->rex);
2335 }
2336
2337
2338 /*
2339 * Now we quickly check if we have found a full valid response.
2340 * If not so, we check the FD and buffer states before leaving.
2341 * A full response is indicated by the fact that we have seen
2342 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2343 * responses are checked first.
2344 *
2345 * Depending on whether the client is still there or not, we
2346 * may send an error response back or not. Note that normally
2347 * we should only check for HTTP status there, and check I/O
2348 * errors somewhere else.
2349 */
2350
2351 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2352
2353 /* Invalid response, or read error or write error */
2354 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2355 (req->flags & BF_WRITE_ERROR) ||
2356 (rep->flags & BF_READ_ERROR))) {
2357 tv_eternity(&rep->rex);
2358 tv_eternity(&req->wex);
2359 fd_delete(t->srv_fd);
2360 if (t->srv) {
2361 t->srv->cur_sess--;
2362 t->srv->failed_resp++;
2363 }
2364 t->be->failed_resp++;
2365 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002366 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002367 client_return(t, error_message(t, HTTP_ERR_502));
2368 if (!(t->flags & SN_ERR_MASK))
2369 t->flags |= SN_ERR_SRVCL;
2370 if (!(t->flags & SN_FINST_MASK))
2371 t->flags |= SN_FINST_H;
2372 /* We used to have a free connection slot. Since we'll never use it,
2373 * we have to inform the server that it may be used by another session.
2374 */
2375 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2376 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002377
Willy Tarreaua15645d2007-03-18 16:22:39 +01002378 return 1;
2379 }
2380
2381 /* end of client write or end of server read.
2382 * since we are in header mode, if there's no space left for headers, we
2383 * won't be able to free more later, so the session will never terminate.
2384 */
2385 else if (unlikely(rep->flags & BF_READ_NULL ||
2386 c == CL_STSHUTW || c == CL_STCLOSE ||
2387 rep->l >= rep->rlim - rep->data)) {
2388 MY_FD_CLR(t->srv_fd, StaticReadEvent);
2389 tv_eternity(&rep->rex);
2390 shutdown(t->srv_fd, SHUT_RD);
2391 t->srv_state = SV_STSHUTR;
2392 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2393 return 1;
2394 }
2395
2396 /* read timeout : return a 504 to the client.
2397 */
2398 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticReadEvent) &&
2399 tv_cmp2_ms(&rep->rex, &now) <= 0)) {
2400 tv_eternity(&rep->rex);
2401 tv_eternity(&req->wex);
2402 fd_delete(t->srv_fd);
2403 if (t->srv) {
2404 t->srv->cur_sess--;
2405 t->srv->failed_resp++;
2406 }
2407 t->be->failed_resp++;
2408 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002409 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002410 client_return(t, error_message(t, HTTP_ERR_504));
2411 if (!(t->flags & SN_ERR_MASK))
2412 t->flags |= SN_ERR_SRVTO;
2413 if (!(t->flags & SN_FINST_MASK))
2414 t->flags |= SN_FINST_H;
2415 /* We used to have a free connection slot. Since we'll never use it,
2416 * we have to inform the server that it may be used by another session.
2417 */
2418 if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm))
2419 task_wakeup(&rq, t->srv->queue_mgt);
2420 return 1;
2421 }
2422
2423 /* last client read and buffer empty */
2424 /* FIXME!!! here, we don't want to switch to SHUTW if the
2425 * client shuts read too early, because we may still have
2426 * some work to do on the headers.
2427 * The side-effect is that if the client completely closes its
2428 * connection during SV_STHEADER, the connection to the server
2429 * is kept until a response comes back or the timeout is reached.
2430 */
2431 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2432 (req->l == 0))) {
2433 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2434 tv_eternity(&req->wex);
2435
2436 /* We must ensure that the read part is still
2437 * alive when switching to shutw */
2438 MY_FD_SET(t->srv_fd, StaticReadEvent);
2439 if (t->be->beprm->srvtimeout)
2440 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2441
2442 shutdown(t->srv_fd, SHUT_WR);
2443 t->srv_state = SV_STSHUTW;
2444 return 1;
2445 }
2446
2447 /* write timeout */
2448 /* FIXME!!! here, we don't want to switch to SHUTW if the
2449 * client shuts read too early, because we may still have
2450 * some work to do on the headers.
2451 */
2452 else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticWriteEvent) &&
2453 tv_cmp2_ms(&req->wex, &now) <= 0)) {
2454 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
2455 tv_eternity(&req->wex);
2456 shutdown(t->srv_fd, SHUT_WR);
2457 /* We must ensure that the read part is still alive
2458 * when switching to shutw */
2459 MY_FD_SET(t->srv_fd, StaticReadEvent);
2460 if (t->be->beprm->srvtimeout)
2461 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
2462
2463 t->srv_state = SV_STSHUTW;
2464 if (!(t->flags & SN_ERR_MASK))
2465 t->flags |= SN_ERR_SRVTO;
2466 if (!(t->flags & SN_FINST_MASK))
2467 t->flags |= SN_FINST_H;
2468 return 1;
2469 }
2470
2471 /*
2472 * And now the non-error cases.
2473 */
2474
2475 /* Data remaining in the request buffer.
2476 * This happens during the first pass here, and during
2477 * long posts.
2478 */
2479 else if (likely(req->l)) {
2480 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2481 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
2482 if (t->be->beprm->srvtimeout) {
2483 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
2484 /* FIXME: to prevent the server from expiring read timeouts during writes,
2485 * we refresh it. */
2486 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002487 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002488 else
2489 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002490 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002491 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002492
Willy Tarreaua15645d2007-03-18 16:22:39 +01002493 /* nothing left in the request buffer */
2494 else {
2495 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2496 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002497 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002498 }
2499 }
2500
2501 return t->srv_state != SV_STHEADERS;
2502 }
2503
2504
2505 /*****************************************************************
2506 * More interesting part now : we know that we have a complete *
2507 * response which at least looks like HTTP. We have an indicator *
2508 * of each header's length, so we can parse them quickly. *
2509 ****************************************************************/
2510
2511 /*
2512 * 1: get the status code and check for cacheability.
2513 */
2514
2515 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002516 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002517
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002518 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002519 case 200:
2520 case 203:
2521 case 206:
2522 case 300:
2523 case 301:
2524 case 410:
2525 /* RFC2616 @13.4:
2526 * "A response received with a status code of
2527 * 200, 203, 206, 300, 301 or 410 MAY be stored
2528 * by a cache (...) unless a cache-control
2529 * directive prohibits caching."
2530 *
2531 * RFC2616 @9.5: POST method :
2532 * "Responses to this method are not cacheable,
2533 * unless the response includes appropriate
2534 * Cache-Control or Expires header fields."
2535 */
2536 if (likely(txn->meth != HTTP_METH_POST) &&
2537 unlikely(t->be->beprm->options & PR_O_CHK_CACHE))
Willy Tarreau3d300592007-03-18 18:34:41 +01002538 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002539 break;
2540 default:
2541 break;
2542 }
2543
2544 /*
2545 * 2: we may need to capture headers
2546 */
2547 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->fiprm->rsp_cap))
2548 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2549 txn->rsp.cap, t->fe->fiprm->rsp_cap);
2550
2551 /*
2552 * 3: we will have to evaluate the filters.
2553 * As opposed to version 1.2, now they will be evaluated in the
2554 * filters order and not in the header order. This means that
2555 * each filter has to be validated among all headers.
2556 *
2557 * Filters are tried with ->be first, then with ->fe if it is
2558 * different from ->be.
2559 */
2560
2561 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2562
2563 cur_proxy = t->be;
2564 while (1) {
2565 struct proxy *rule_set = cur_proxy->fiprm;
2566
2567 /* try headers filters */
2568 if (rule_set->rsp_exp != NULL) {
2569 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2570 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002571 if (t->srv) {
2572 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002573 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002574 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002575 cur_proxy->beprm->failed_resp++;
2576 return_srv_prx_502:
2577 tv_eternity(&rep->rex);
2578 tv_eternity(&req->wex);
2579 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002580 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002581 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002582 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002583 if (!(t->flags & SN_ERR_MASK))
2584 t->flags |= SN_ERR_PRXCOND;
2585 if (!(t->flags & SN_FINST_MASK))
2586 t->flags |= SN_FINST_H;
2587 /* We used to have a free connection slot. Since we'll never use it,
2588 * we have to inform the server that it may be used by another session.
2589 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002590 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002591 task_wakeup(&rq, t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002592 return 1;
2593 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002594 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002595
Willy Tarreaua15645d2007-03-18 16:22:39 +01002596 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002597 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002598 if (t->srv) {
2599 t->srv->cur_sess--;
2600 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002601 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002602 cur_proxy->beprm->denied_resp++;
2603 goto return_srv_prx_502;
2604 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002605
Willy Tarreaua15645d2007-03-18 16:22:39 +01002606 /* We might have to check for "Connection:" */
2607 if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) &&
2608 !(t->flags & SN_CONN_CLOSED)) {
2609 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002610 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002611 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002612
Willy Tarreaua15645d2007-03-18 16:22:39 +01002613 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2614 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002615
Willy Tarreaua15645d2007-03-18 16:22:39 +01002616 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2617 cur_hdr = &txn->hdr_idx.v[cur_idx];
2618 cur_ptr = cur_next;
2619 cur_end = cur_ptr + cur_hdr->len;
2620 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002621
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002622 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2623 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002624 /* 3 possibilities :
2625 * - we have already set Connection: close,
2626 * so we remove this line.
2627 * - we have not yet set Connection: close,
2628 * but this line indicates close. We leave
2629 * it untouched and set the flag.
2630 * - we have not yet set Connection: close,
2631 * and this line indicates non-close. We
2632 * replace it.
2633 */
2634 if (t->flags & SN_CONN_CLOSED) {
2635 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2636 txn->rsp.eoh += delta;
2637 cur_next += delta;
2638 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2639 txn->hdr_idx.used--;
2640 cur_hdr->len = 0;
2641 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002642 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2643 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2644 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002645 cur_next += delta;
2646 cur_hdr->len += delta;
2647 txn->rsp.eoh += delta;
2648 }
2649 t->flags |= SN_CONN_CLOSED;
2650 }
2651 }
2652 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002653 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002654 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002655
Willy Tarreaua15645d2007-03-18 16:22:39 +01002656 /* add response headers from the rule sets in the same order */
2657 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002658 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2659 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002660 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002661 }
2662
Willy Tarreaua15645d2007-03-18 16:22:39 +01002663 /* check whether we're already working on the frontend */
2664 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002665 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002666 cur_proxy = t->fe;
2667 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002668
Willy Tarreaua15645d2007-03-18 16:22:39 +01002669 /*
2670 * 4: check for server cookie.
2671 */
2672 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002673
Willy Tarreaua15645d2007-03-18 16:22:39 +01002674 /*
2675 * 5: add server cookie in the response if needed
2676 */
2677 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_INS) &&
2678 (!(t->be->beprm->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2679 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002680
Willy Tarreaua15645d2007-03-18 16:22:39 +01002681 /* the server is known, it's not the one the client requested, we have to
2682 * insert a set-cookie here, except if we want to insert only on POST
2683 * requests and this one isn't. Note that servers which don't have cookies
2684 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002685 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002686 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaua15645d2007-03-18 16:22:39 +01002687 t->be->beprm->cookie_name,
2688 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002689
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002690 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2691 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002692 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002693 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002694
Willy Tarreaua15645d2007-03-18 16:22:39 +01002695 /* Here, we will tell an eventual cache on the client side that we don't
2696 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2697 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2698 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2699 */
2700 if (t->be->beprm->options & PR_O_COOK_NOC) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002701 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2702 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002703 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002704 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002705 }
2706
2707
2708 /*
2709 * 6: check for cache-control or pragma headers.
2710 */
2711 check_response_for_cacheability(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002712
Willy Tarreaubaaee002006-06-26 02:48:02 +02002713
Willy Tarreaua15645d2007-03-18 16:22:39 +01002714 /*
2715 * 7: check if result will be cacheable with a cookie.
2716 * We'll block the response if security checks have caught
2717 * nasty things such as a cacheable cookie.
2718 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002719 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2720 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002721 (t->be->beprm->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002722
Willy Tarreaua15645d2007-03-18 16:22:39 +01002723 /* we're in presence of a cacheable response containing
2724 * a set-cookie header. We'll block it as requested by
2725 * the 'checkcache' option, and send an alert.
2726 */
2727 if (t->srv) {
2728 t->srv->cur_sess--;
2729 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002730 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002731 t->be->beprm->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002732
Willy Tarreaua15645d2007-03-18 16:22:39 +01002733 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2734 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2735 send_log(t->be, LOG_ALERT,
2736 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2737 t->be->beprm->id, t->srv?t->srv->id:"<dispatch>");
2738 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002739 }
2740
Willy Tarreaua15645d2007-03-18 16:22:39 +01002741 /*
2742 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002743 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002744 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002745 if (!(t->flags & SN_CONN_CLOSED) &&
2746 ((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE)) {
2747 if ((unlikely(msg->sl.st.v_l != 8) ||
2748 unlikely(req->data[msg->som + 7] != '0')) &&
2749 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002750 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002751 goto return_bad_resp;
2752 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002753 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002754
Willy Tarreaubaaee002006-06-26 02:48:02 +02002755
Willy Tarreaua15645d2007-03-18 16:22:39 +01002756 /*************************************************************
2757 * OK, that's finished for the headers. We have done what we *
2758 * could. Let's switch to the DATA state. *
2759 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002760
Willy Tarreaua15645d2007-03-18 16:22:39 +01002761 t->srv_state = SV_STDATA;
2762 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
2763 t->logs.t_data = tv_diff(&t->logs.tv_accept, &now);
2764
2765 /* client connection already closed or option 'forceclose' required :
2766 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002767 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002768 if ((req->l == 0) &&
2769 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->beprm->options & PR_O_FORCE_CLO)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002770 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002771 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002772
2773 /* We must ensure that the read part is still alive when switching
2774 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002775 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002776 if (t->be->beprm->srvtimeout)
2777 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002778
Willy Tarreaua15645d2007-03-18 16:22:39 +01002779 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002780 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002781 }
2782
Willy Tarreaua15645d2007-03-18 16:22:39 +01002783#ifdef CONFIG_HAP_TCPSPLICE
2784 if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) {
2785 /* TCP splicing supported by both FE and BE */
2786 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002787 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002788#endif
2789 /* if the user wants to log as soon as possible, without counting
2790 bytes from the server, then this is the right moment. */
2791 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2792 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01002793 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002794 sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002795 }
2796
Willy Tarreaua15645d2007-03-18 16:22:39 +01002797 /* Note: we must not try to cheat by jumping directly to DATA,
2798 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002799 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002800
2801 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002802 }
2803 else if (s == SV_STDATA) {
2804 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002805 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002806 tv_eternity(&rep->rex);
2807 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002808 fd_delete(t->srv_fd);
2809 if (t->srv) {
2810 t->srv->cur_sess--;
2811 t->srv->failed_resp++;
2812 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002813 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002814 t->srv_state = SV_STCLOSE;
2815 if (!(t->flags & SN_ERR_MASK))
2816 t->flags |= SN_ERR_SRVCL;
2817 if (!(t->flags & SN_FINST_MASK))
2818 t->flags |= SN_FINST_D;
2819 /* We used to have a free connection slot. Since we'll never use it,
2820 * we have to inform the server that it may be used by another session.
2821 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002822 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002823 task_wakeup(&rq, t->srv->queue_mgt);
2824
2825 return 1;
2826 }
2827 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002828 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002829 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002830 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002831 shutdown(t->srv_fd, SHUT_RD);
2832 t->srv_state = SV_STSHUTR;
2833 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2834 return 1;
2835 }
2836 /* end of client read and no more data to send */
2837 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002838 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002839 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002840 shutdown(t->srv_fd, SHUT_WR);
2841 /* We must ensure that the read part is still alive when switching
2842 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002843 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002844 if (t->be->beprm->srvtimeout)
2845 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002846
2847 t->srv_state = SV_STSHUTW;
2848 return 1;
2849 }
2850 /* read timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002851 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002852 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002853 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002854 shutdown(t->srv_fd, SHUT_RD);
2855 t->srv_state = SV_STSHUTR;
2856 if (!(t->flags & SN_ERR_MASK))
2857 t->flags |= SN_ERR_SRVTO;
2858 if (!(t->flags & SN_FINST_MASK))
2859 t->flags |= SN_FINST_D;
2860 return 1;
2861 }
2862 /* write timeout */
Willy Tarreaud7971282006-07-29 18:36:34 +02002863 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002864 MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002865 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002866 shutdown(t->srv_fd, SHUT_WR);
2867 /* We must ensure that the read part is still alive when switching
2868 * to shutw */
Willy Tarreau2a429502006-10-15 14:52:29 +02002869 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002870 if (t->be->beprm->srvtimeout)
2871 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002872 t->srv_state = SV_STSHUTW;
2873 if (!(t->flags & SN_ERR_MASK))
2874 t->flags |= SN_ERR_SRVTO;
2875 if (!(t->flags & SN_FINST_MASK))
2876 t->flags |= SN_FINST_D;
2877 return 1;
2878 }
2879
2880 /* recompute request time-outs */
2881 if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002882 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2883 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002884 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002885 }
2886 }
2887 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau2a429502006-10-15 14:52:29 +02002888 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2889 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002890 if (t->be->beprm->srvtimeout) {
2891 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002892 /* FIXME: to prevent the server from expiring read timeouts during writes,
2893 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002894 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895 }
2896 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002897 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002898 }
2899 }
2900
2901 /* recompute response time-outs */
2902 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02002903 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2904 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002905 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002906 }
2907 }
2908 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02002909 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
2910 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01002911 if (t->be->beprm->srvtimeout)
2912 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002913 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002914 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002915 }
2916 }
2917
2918 return 0; /* other cases change nothing */
2919 }
2920 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002921 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002922 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002923 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002924 fd_delete(t->srv_fd);
2925 if (t->srv) {
2926 t->srv->cur_sess--;
2927 t->srv->failed_resp++;
2928 }
Willy Tarreau73de9892006-11-30 11:40:23 +01002929 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002930 //close(t->srv_fd);
2931 t->srv_state = SV_STCLOSE;
2932 if (!(t->flags & SN_ERR_MASK))
2933 t->flags |= SN_ERR_SRVCL;
2934 if (!(t->flags & SN_FINST_MASK))
2935 t->flags |= SN_FINST_D;
2936 /* We used to have a free connection slot. Since we'll never use it,
2937 * we have to inform the server that it may be used by another session.
2938 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002939 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002940 task_wakeup(&rq, t->srv->queue_mgt);
2941
2942 return 1;
2943 }
2944 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002945 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002946 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002947 fd_delete(t->srv_fd);
2948 if (t->srv)
2949 t->srv->cur_sess--;
2950 //close(t->srv_fd);
2951 t->srv_state = SV_STCLOSE;
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 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002960 else if (tv_cmp2_ms(&req->wex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002961 //MY_FD_CLR(t->srv_fd, StaticWriteEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02002962 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002963 fd_delete(t->srv_fd);
2964 if (t->srv)
2965 t->srv->cur_sess--;
2966 //close(t->srv_fd);
2967 t->srv_state = SV_STCLOSE;
2968 if (!(t->flags & SN_ERR_MASK))
2969 t->flags |= SN_ERR_SRVTO;
2970 if (!(t->flags & SN_FINST_MASK))
2971 t->flags |= SN_FINST_D;
2972 /* We used to have a free connection slot. Since we'll never use it,
2973 * we have to inform the server that it may be used by another session.
2974 */
Willy Tarreau830ff452006-12-17 19:31:23 +01002975 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002976 task_wakeup(&rq, t->srv->queue_mgt);
2977
2978 return 1;
2979 }
2980 else if (req->l == 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02002981 if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2982 MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002983 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002984 }
2985 }
2986 else { /* buffer not empty */
Willy Tarreau2a429502006-10-15 14:52:29 +02002987 if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) {
2988 MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */
Willy Tarreau830ff452006-12-17 19:31:23 +01002989 if (t->be->beprm->srvtimeout) {
2990 tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002991 /* FIXME: to prevent the server from expiring read timeouts during writes,
2992 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002993 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002994 }
2995 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002996 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002997 }
2998 }
2999 return 0;
3000 }
3001 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003002 if (rep->flags & BF_READ_ERROR) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003003 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003004 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003005 fd_delete(t->srv_fd);
3006 if (t->srv) {
3007 t->srv->cur_sess--;
3008 t->srv->failed_resp++;
3009 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003010 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003011 //close(t->srv_fd);
3012 t->srv_state = SV_STCLOSE;
3013 if (!(t->flags & SN_ERR_MASK))
3014 t->flags |= SN_ERR_SRVCL;
3015 if (!(t->flags & SN_FINST_MASK))
3016 t->flags |= SN_FINST_D;
3017 /* We used to have a free connection slot. Since we'll never use it,
3018 * we have to inform the server that it may be used by another session.
3019 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003020 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003021 task_wakeup(&rq, t->srv->queue_mgt);
3022
3023 return 1;
3024 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003025 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003026 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003027 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003028 fd_delete(t->srv_fd);
3029 if (t->srv)
3030 t->srv->cur_sess--;
3031 //close(t->srv_fd);
3032 t->srv_state = SV_STCLOSE;
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 }
Willy Tarreaud7971282006-07-29 18:36:34 +02003041 else if (tv_cmp2_ms(&rep->rex, &now) <= 0) {
Willy Tarreau2a429502006-10-15 14:52:29 +02003042 //MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003043 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003044 fd_delete(t->srv_fd);
3045 if (t->srv)
3046 t->srv->cur_sess--;
3047 //close(t->srv_fd);
3048 t->srv_state = SV_STCLOSE;
3049 if (!(t->flags & SN_ERR_MASK))
3050 t->flags |= SN_ERR_SRVTO;
3051 if (!(t->flags & SN_FINST_MASK))
3052 t->flags |= SN_FINST_D;
3053 /* We used to have a free connection slot. Since we'll never use it,
3054 * we have to inform the server that it may be used by another session.
3055 */
Willy Tarreau830ff452006-12-17 19:31:23 +01003056 if (may_dequeue_tasks(t->srv, t->be->beprm))
Willy Tarreaubaaee002006-06-26 02:48:02 +02003057 task_wakeup(&rq, t->srv->queue_mgt);
3058
3059 return 1;
3060 }
3061 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau2a429502006-10-15 14:52:29 +02003062 if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3063 MY_FD_CLR(t->srv_fd, StaticReadEvent);
Willy Tarreaud7971282006-07-29 18:36:34 +02003064 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003065 }
3066 }
3067 else {
Willy Tarreau2a429502006-10-15 14:52:29 +02003068 if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) {
3069 MY_FD_SET(t->srv_fd, StaticReadEvent);
Willy Tarreau830ff452006-12-17 19:31:23 +01003070 if (t->be->beprm->srvtimeout)
3071 tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003073 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003074 }
3075 }
3076 return 0;
3077 }
3078 else { /* SV_STCLOSE : nothing to do */
3079 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3080 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003081 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
3082 t->uniq_id, t->be->beprm->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003083 write(1, trash, len);
3084 }
3085 return 0;
3086 }
3087 return 0;
3088}
3089
3090
3091/*
3092 * Produces data for the session <s> depending on its source. Expects to be
3093 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3094 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3095 * session, which it uses to keep on being called when there is free space in
3096 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3097 * if it changes the session state from CL_STSHUTR, otherwise 0.
3098 */
3099int produce_content(struct session *s)
3100{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003101 if (s->data_source == DATA_SRC_NONE) {
3102 s->flags &= ~SN_SELF_GEN;
3103 return 1;
3104 }
3105 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003106 /* dump server statistics */
3107 return produce_content_stats(s);
3108 }
3109 else {
3110 /* unknown data source */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003111 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003112 client_retnclose(s, error_message(s, HTTP_ERR_500));
3113 if (!(s->flags & SN_ERR_MASK))
3114 s->flags |= SN_ERR_PRXCOND;
3115 if (!(s->flags & SN_FINST_MASK))
3116 s->flags |= SN_FINST_R;
3117 s->flags &= ~SN_SELF_GEN;
3118 return 1;
3119 }
3120}
3121
3122
3123/*
3124 * Produces statistics data for the session <s>. Expects to be called with
3125 * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN
3126 * flag from the session, which it uses to keep on being called when there is
3127 * free space in the buffer, of simply by letting an empty buffer upon return.
3128 * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0.
3129 */
3130int produce_content_stats(struct session *s)
3131{
3132 struct buffer *rep = s->rep;
3133 struct proxy *px;
3134 struct chunk msg;
3135 unsigned int up;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003136
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003137 msg.len = 0;
3138 msg.str = trash;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003139
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003140 switch (s->data_state) {
3141 case DATA_ST_INIT:
3142 /* the function had not been called yet */
3143 s->flags |= SN_SELF_GEN; // more data will follow
Willy Tarreaubaaee002006-06-26 02:48:02 +02003144
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003145 chunk_printf(&msg, sizeof(trash),
3146 "HTTP/1.0 200 OK\r\n"
3147 "Cache-Control: no-cache\r\n"
3148 "Connection: close\r\n"
3149 "Content-Type: text/html\r\n"
3150 "\r\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003151
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003152 s->txn.status = 200;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003153 client_retnclose(s, &msg); // send the start of the response.
3154 msg.len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003155
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003156 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3157 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3158 if (!(s->flags & SN_FINST_MASK))
3159 s->flags |= SN_FINST_R;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003160
Willy Tarreaub326fcc2007-03-03 13:54:32 +01003161 if (s->txn.meth == HTTP_METH_HEAD) {
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003162 /* that's all we return in case of HEAD request */
3163 s->data_state = DATA_ST_FIN;
3164 s->flags &= ~SN_SELF_GEN;
3165 return 1;
3166 }
3167
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003168 s->data_state = DATA_ST_HEAD; /* let's start producing data */
3169 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003170
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003171 case DATA_ST_HEAD:
3172 /* WARNING! This must fit in the first buffer !!! */
3173 chunk_printf(&msg, sizeof(trash),
3174 "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n"
3175 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3176 "<style type=\"text/css\"><!--\n"
3177 "body {"
3178 " font-family: helvetica, arial;"
3179 " font-size: 12px;"
3180 " font-weight: normal;"
3181 " color: black;"
3182 " background: white;"
3183 "}\n"
3184 "th,td {"
3185 " font-size: 0.8em;"
3186 " align: center;"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003187 "}\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003188 "h1 {"
3189 " font-size: xx-large;"
3190 " margin-bottom: 0.5em;"
3191 "}\n"
3192 "h2 {"
3193 " font-family: helvetica, arial;"
3194 " font-size: x-large;"
3195 " font-weight: bold;"
3196 " font-style: italic;"
3197 " color: #6020a0;"
3198 " margin-top: 0em;"
3199 " margin-bottom: 0em;"
3200 "}\n"
3201 "h3 {"
3202 " font-family: helvetica, arial;"
3203 " font-size: 16px;"
3204 " font-weight: bold;"
3205 " color: #b00040;"
3206 " background: #e8e8d0;"
3207 " margin-top: 0em;"
3208 " margin-bottom: 0em;"
3209 "}\n"
3210 "li {"
3211 " margin-top: 0.25em;"
3212 " margin-right: 2em;"
3213 "}\n"
3214 ".hr {margin-top: 0.25em;"
3215 " border-color: black;"
3216 " border-bottom-style: solid;"
3217 "}\n"
3218 ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n"
3219 ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n"
3220 ".total {background: #20D0D0;color: #ffff80;}\n"
3221 ".frontend {background: #e8e8d0;}\n"
3222 ".backend {background: #e8e8d0;}\n"
3223 ".active0 {background: #ff9090;}\n"
3224 ".active1 {background: #ffd020;}\n"
3225 ".active2 {background: #ffffa0;}\n"
3226 ".active3 {background: #c0ffc0;}\n"
3227 ".active4 {background: #e0e0e0;}\n"
3228 ".backup0 {background: #ff9090;}\n"
3229 ".backup1 {background: #ff80ff;}\n"
3230 ".backup2 {background: #c060ff;}\n"
3231 ".backup3 {background: #b0d0ff;}\n"
3232 ".backup4 {background: #e0e0e0;}\n"
3233 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003234 "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 +01003235 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3236 "table.tbl th.empty { border-style: none; empty-cells: hide;}\n"
3237 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3238 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3239 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003240 "-->\n"
3241 "</style></head>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003242
3243 if (buffer_write_chunk(rep, &msg) != 0)
3244 return 0;
3245
3246 s->data_state = DATA_ST_INFO;
3247 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003248
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003249 case DATA_ST_INFO:
3250 up = (now.tv_sec - start_date.tv_sec);
3251
3252 /* WARNING! this has to fit the first packet too.
3253 * We are around 3.5 kB, add adding entries will
3254 * become tricky if we want to support 4kB buffers !
3255 */
3256 chunk_printf(&msg, sizeof(trash),
3257 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3258 PRODUCT_NAME "</a></h1>\n"
3259 "<h2>Statistics Report for pid %d</h2>\n"
3260 "<hr width=\"100%%\" class=\"hr\">\n"
3261 "<h3>&gt; General process information</h3>\n"
3262 "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3263 "<p><b>pid = </b> %d (nbproc = %d)<br>\n"
3264 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3265 "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n"
3266 "<b>maxsock = </b> %d<br>\n"
3267 "<b>maxconn = </b> %d (current conns = %d)<br>\n"
3268 "</td><td align=\"center\" nowrap>\n"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003269 "<table class=\"lgd\"><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003270 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3271 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003272 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003273 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3274 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003275 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003276 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3277 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003278 "</tr><tr>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003279 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3280 "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>"
3281 "</tr></table>\n"
3282 "</td>"
3283 "<td align=\"left\" nowrap width=\"1%%\">"
Willy Tarreauf2b74c22007-03-25 22:44:08 +02003284 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
3285 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3286 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3287 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003288 "</ul>"
3289 "</td>"
3290 "</tr></table>\n"
3291 "",
3292 pid, pid, global.nbproc,
3293 up / 86400, (up % 86400) / 3600,
3294 (up % 3600) / 60, (up % 60),
3295 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3296 global.rlimit_memmax ? " MB" : "",
3297 global.rlimit_nofile,
3298 global.maxsock,
3299 global.maxconn,
3300 actconn
3301 );
Willy Tarreaubaaee002006-06-26 02:48:02 +02003302
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003303 if (buffer_write_chunk(rep, &msg) != 0)
3304 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003305
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003306 memset(&s->data_ctx, 0, sizeof(s->data_ctx));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003307
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003308 s->data_ctx.stats.px = proxy;
3309 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3310 s->data_state = DATA_ST_LIST;
3311 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003312
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003313 case DATA_ST_LIST:
3314 /* dump proxies */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003315 while (s->data_ctx.stats.px) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003316 px = s->data_ctx.stats.px;
3317 /* skip the disabled proxies and non-networked ones */
3318 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3319 if (produce_content_stats_proxy(s, px) == 0)
3320 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003321
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003322 s->data_ctx.stats.px = px->next;
3323 s->data_ctx.stats.px_st = DATA_ST_PX_INIT;
3324 }
3325 /* here, we just have reached the last proxy */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003326
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003327 s->data_state = DATA_ST_END;
3328 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003329
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003330 case DATA_ST_END:
Willy Tarreau0214c3a2007-01-07 13:47:30 +01003331 chunk_printf(&msg, sizeof(trash), "</body></html>\n");
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003332 if (buffer_write_chunk(rep, &msg) != 0)
3333 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003334
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003335 s->data_state = DATA_ST_FIN;
3336 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003337
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003338 case DATA_ST_FIN:
3339 s->flags &= ~SN_SELF_GEN;
3340 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003341
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003342 default:
3343 /* unknown state ! */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003344 s->txn.status = 500;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003345 client_retnclose(s, error_message(s, HTTP_ERR_500));
3346 if (!(s->flags & SN_ERR_MASK))
3347 s->flags |= SN_ERR_PRXCOND;
3348 if (!(s->flags & SN_FINST_MASK))
3349 s->flags |= SN_FINST_R;
3350 s->flags &= ~SN_SELF_GEN;
3351 return 1;
3352 }
3353}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003354
Willy Tarreaubaaee002006-06-26 02:48:02 +02003355
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003356/*
3357 * Dumps statistics for a proxy.
3358 * Returns 0 if it had to stop dumping data because of lack of buffer space,
3359 * ot non-zero if everything completed.
3360 */
3361int produce_content_stats_proxy(struct session *s, struct proxy *px)
3362{
3363 struct buffer *rep = s->rep;
3364 struct server *sv;
3365 struct chunk msg;
3366
3367 msg.len = 0;
3368 msg.str = trash;
3369
3370 switch (s->data_ctx.stats.px_st) {
3371 case DATA_ST_PX_INIT:
3372 /* we are on a new proxy */
3373
3374 if (s->be->fiprm->uri_auth && s->be->fiprm->uri_auth->scope) {
3375 /* we have a limited scope, we have to check the proxy name */
3376 struct stat_scope *scope;
3377 int len;
3378
3379 len = strlen(px->id);
3380 scope = s->be->fiprm->uri_auth->scope;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003381
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003382 while (scope) {
3383 /* match exact proxy name */
3384 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3385 break;
3386
3387 /* match '.' which means 'self' proxy */
3388 if (!strcmp(scope->px_id, ".") && px == s->fe)
3389 break;
3390 scope = scope->next;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003391 }
3392
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003393 /* proxy name not found : don't dump anything */
3394 if (scope == NULL)
3395 return 1;
3396 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003397
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003398 s->data_ctx.stats.px_st = DATA_ST_PX_TH;
3399 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003400
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003401 case DATA_ST_PX_TH:
3402 /* print a new table */
3403 chunk_printf(&msg, sizeof(trash),
3404 "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n"
3405 "<tr align=\"center\" class=\"titre\">"
3406 "<th colspan=2 class=\"pxname\">%s</th>"
3407 "<th colspan=18 class=\"empty\"></th>"
3408 "</tr>\n"
3409 "<tr align=\"center\" class=\"titre\">"
3410 "<th rowspan=2></th>"
3411 "<th colspan=2>Queue</th><th colspan=4>Sessions</th>"
3412 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3413 "<th colspan=3>Errors</th><th colspan=6>Server</th>"
3414 "</tr>\n"
3415 "<tr align=\"center\" class=\"titre\">"
Willy Tarreau35d66b02007-01-02 00:28:21 +01003416 "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>"
3417 "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>"
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003418 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3419 "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>"
3420 "<th>Bck</th><th>Check</th><th>Down</th></tr>\n"
3421 "",
3422 px->id);
3423
3424 if (buffer_write_chunk(rep, &msg) != 0)
3425 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003426
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003427 s->data_ctx.stats.px_st = DATA_ST_PX_FE;
3428 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003429
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003430 case DATA_ST_PX_FE:
3431 /* print the frontend */
3432 if (px->cap & PR_CAP_FE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003433 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003434 /* name, queue */
3435 "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>"
3436 /* sessions : current, max, limit, cumul. */
3437 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3438 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003439 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003440 /* denied: req, resp */
3441 "<td align=right>%d</td><td align=right>%d</td>"
3442 /* errors : request, connect, response */
3443 "<td align=right>%d</td><td align=right></td><td align=right></td>"
3444 /* server status : reflect backend status */
3445 "<td align=center>%s</td>"
3446 /* rest of server: nothing */
3447 "<td align=center colspan=5></td></tr>"
3448 "",
3449 px->feconn, px->feconn_max, px->maxconn, px->cum_feconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003450 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003451 px->denied_req, px->denied_resp,
3452 px->failed_req,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003453 px->state == PR_STRUN ? "OPEN" :
3454 px->state == PR_STIDLE ? "FULL" : "STOP");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003455
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003456 if (buffer_write_chunk(rep, &msg) != 0)
3457 return 0;
3458 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003459
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003460 s->data_ctx.stats.sv = px->srv; /* may be NULL */
3461 s->data_ctx.stats.px_st = DATA_ST_PX_SV;
3462 /* fall through */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003463
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003464 case DATA_ST_PX_SV:
3465 /* stats.sv has been initialized above */
3466 while (s->data_ctx.stats.sv != NULL) {
3467 static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d &uarr;", "UP %d/%d &darr;", "UP", "<i>no check</i>" };
3468 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003469
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003470 sv = s->data_ctx.stats.sv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003471
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003472 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
3473 if (!(sv->state & SRV_CHECKED))
3474 sv_state = 4;
3475 else if (sv->state & SRV_RUNNING)
3476 if (sv->health == sv->rise + sv->fall - 1)
3477 sv_state = 3; /* UP */
3478 else
3479 sv_state = 2; /* going down */
3480 else
3481 if (sv->health)
3482 sv_state = 1; /* going up */
3483 else
3484 sv_state = 0; /* DOWN */
3485
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003486 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003487 /* name */
3488 "<tr align=\"center\" class=\"%s%d\"><td>%s</td>"
3489 /* queue : current, max */
3490 "<td align=right>%d</td><td align=right>%d</td>"
3491 /* sessions : current, max, limit, cumul */
3492 "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>"
3493 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003494 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003495 /* denied: req, resp */
3496 "<td align=right></td><td align=right>%d</td>"
3497 /* errors : request, connect, response */
3498 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3499 "",
Willy Tarreau368e96a2007-01-07 00:16:15 +01003500 (sv->state & SRV_BACKUP) ? "backup" : "active",
Willy Tarreau128e9542007-01-01 22:01:43 +01003501 sv_state, sv->id,
3502 sv->nbpend, sv->nbpend_max,
3503 sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003504 sv->bytes_in, sv->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003505 sv->failed_secu,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003506 sv->failed_conns, sv->failed_resp);
Willy Tarreau128e9542007-01-01 22:01:43 +01003507
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003508 /* status */
3509 chunk_printf(&msg, sizeof(trash), "<td nowrap>");
3510 chunk_printf(&msg, sizeof(trash),
3511 srv_hlt_st[sv_state],
3512 (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health),
3513 (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise));
3514
Willy Tarreau128e9542007-01-01 22:01:43 +01003515 chunk_printf(&msg, sizeof(trash),
3516 /* weight */
3517 "</td><td>%d</td>"
3518 /* act, bck */
3519 "<td>%s</td><td>%s</td>"
3520 "",
Willy Tarreau417fae02007-03-25 21:16:40 +02003521 sv->uweight,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003522 (sv->state & SRV_BACKUP) ? "-" : "Y",
3523 (sv->state & SRV_BACKUP) ? "Y" : "-");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003524
3525 /* check failures : unique, fatal */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003526 if (sv->state & SRV_CHECKED)
3527 chunk_printf(&msg, sizeof(trash),
3528 "<td align=right>%d</td><td align=right>%d</td></tr>\n",
3529 sv->failed_checks, sv->down_trans);
3530 else
3531 chunk_printf(&msg, sizeof(trash),
3532 "<td colspan=2></td></tr>\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003533
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003534 if (buffer_write_chunk(rep, &msg) != 0)
3535 return 0;
3536
3537 s->data_ctx.stats.sv = sv->next;
3538 } /* while sv */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003539
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003540 s->data_ctx.stats.px_st = DATA_ST_PX_BE;
3541 /* fall through */
3542
3543 case DATA_ST_PX_BE:
3544 /* print the backend */
3545 if (px->cap & PR_CAP_BE) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003546 chunk_printf(&msg, sizeof(trash),
Willy Tarreau128e9542007-01-01 22:01:43 +01003547 /* name */
3548 "<tr align=center class=\"backend\"><td>Backend</td>"
3549 /* queue : current, max */
3550 "<td align=right>%d</td><td align=right>%d</td>"
3551 /* sessions : current, max, limit, cumul. */
3552 "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>"
3553 /* bytes : in, out */
Willy Tarreau35d66b02007-01-02 00:28:21 +01003554 "<td align=right>%lld</td><td align=right>%lld</td>"
Willy Tarreau128e9542007-01-01 22:01:43 +01003555 /* denied: req, resp */
3556 "<td align=right>%d</td><td align=right>%d</td>"
3557 /* errors : request, connect, response */
3558 "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n"
3559 /* server status : reflect backend status (up/down) : we display UP
3560 * if the backend has known working servers or if it has no server at
3561 * all (eg: for stats). Tthen we display the total weight, number of
3562 * active and backups. */
3563 "<td align=center>%s</td><td align=center>%d</td>"
3564 "<td align=center>%d</td><td align=center>%d</td>"
3565 /* rest of server: nothing */
3566 "<td align=center colspan=2></td></tr>"
3567 "",
3568 px->nbpend /* or px->totpend ? */, px->nbpend_max,
3569 px->beconn, px->beconn_max, px->fullconn, px->cum_beconn,
Willy Tarreau35d66b02007-01-02 00:28:21 +01003570 px->bytes_in, px->bytes_out,
Willy Tarreau128e9542007-01-01 22:01:43 +01003571 px->denied_req, px->denied_resp,
3572 px->failed_conns, px->failed_resp,
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003573 (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN",
3574 px->srv_map_sz, px->srv_act, px->srv_bck);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003575
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003576 if (buffer_write_chunk(rep, &msg) != 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003577 return 0;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003578 }
3579
3580 s->data_ctx.stats.px_st = DATA_ST_PX_END;
3581 /* fall through */
3582
3583 case DATA_ST_PX_END:
3584 chunk_printf(&msg, sizeof(trash), "</table><p>\n");
3585
3586 if (buffer_write_chunk(rep, &msg) != 0)
3587 return 0;
3588
3589 s->data_ctx.stats.px_st = DATA_ST_PX_FIN;
3590 /* fall through */
3591
3592 case DATA_ST_PX_FIN:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003593 return 1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003594
3595 default:
3596 /* unknown state, we should put an abort() here ! */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003597 return 1;
3598 }
3599}
3600
3601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003602/* Iterate the same filter through all request headers.
3603 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003604 * Since it can manage the switch to another backend, it updates the per-proxy
3605 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003606 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003607int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003608{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003609 char term;
3610 char *cur_ptr, *cur_end, *cur_next;
3611 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003612 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003613 struct hdr_idx_elem *cur_hdr;
3614 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003615
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003616 last_hdr = 0;
3617
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003618 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003619 old_idx = 0;
3620
3621 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003622 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003623 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003624 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003625 (exp->action == ACT_ALLOW ||
3626 exp->action == ACT_DENY ||
3627 exp->action == ACT_TARPIT))
3628 return 0;
3629
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003630 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003631 if (!cur_idx)
3632 break;
3633
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003634 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003635 cur_ptr = cur_next;
3636 cur_end = cur_ptr + cur_hdr->len;
3637 cur_next = cur_end + cur_hdr->cr + 1;
3638
3639 /* Now we have one header between cur_ptr and cur_end,
3640 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003641 */
3642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003643 /* The annoying part is that pattern matching needs
3644 * that we modify the contents to null-terminate all
3645 * strings before testing them.
3646 */
3647
3648 term = *cur_end;
3649 *cur_end = '\0';
3650
3651 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3652 switch (exp->action) {
3653 case ACT_SETBE:
3654 /* It is not possible to jump a second time.
3655 * FIXME: should we return an HTTP/500 here so that
3656 * the admin knows there's a problem ?
3657 */
3658 if (t->be != t->fe)
3659 break;
3660
3661 /* Swithing Proxy */
3662 t->be = (struct proxy *) exp->replace;
3663
3664 /* right now, the backend switch is not too much complicated
3665 * because we have associated req_cap and rsp_cap to the
3666 * frontend, and the beconn will be updated later.
3667 */
3668
3669 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3670 t->req->cto = t->be->beprm->contimeout;
3671 last_hdr = 1;
3672 break;
3673
3674 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003675 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003676 last_hdr = 1;
3677 break;
3678
3679 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003680 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003681 last_hdr = 1;
3682 t->be->beprm->denied_req++;
3683 break;
3684
3685 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003686 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003687 last_hdr = 1;
3688 t->be->beprm->denied_req++;
3689 break;
3690
3691 case ACT_REPLACE:
3692 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3693 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3694 /* FIXME: if the user adds a newline in the replacement, the
3695 * index will not be recalculated for now, and the new line
3696 * will not be counted as a new header.
3697 */
3698
3699 cur_end += delta;
3700 cur_next += delta;
3701 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003702 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003703 break;
3704
3705 case ACT_REMOVE:
3706 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3707 cur_next += delta;
3708
3709 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003710 txn->req.eoh += delta;
3711 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3712 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003713 cur_hdr->len = 0;
3714 cur_end = NULL; /* null-term has been rewritten */
3715 break;
3716
3717 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003718 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003719 if (cur_end)
3720 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003722 /* keep the link from this header to next one in case of later
3723 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003724 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003725 old_idx = cur_idx;
3726 }
3727 return 0;
3728}
3729
3730
3731/* Apply the filter to the request line.
3732 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3733 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003734 * Since it can manage the switch to another backend, it updates the per-proxy
3735 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003736 */
3737int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3738{
3739 char term;
3740 char *cur_ptr, *cur_end;
3741 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003742 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003743 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003744
Willy Tarreau58f10d72006-12-04 02:26:12 +01003745
Willy Tarreau3d300592007-03-18 18:34:41 +01003746 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003747 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003748 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003749 (exp->action == ACT_ALLOW ||
3750 exp->action == ACT_DENY ||
3751 exp->action == ACT_TARPIT))
3752 return 0;
3753 else if (exp->action == ACT_REMOVE)
3754 return 0;
3755
3756 done = 0;
3757
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003758 cur_ptr = req->data + txn->req.som;
3759 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003760
3761 /* Now we have the request line between cur_ptr and cur_end */
3762
3763 /* The annoying part is that pattern matching needs
3764 * that we modify the contents to null-terminate all
3765 * strings before testing them.
3766 */
3767
3768 term = *cur_end;
3769 *cur_end = '\0';
3770
3771 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3772 switch (exp->action) {
3773 case ACT_SETBE:
3774 /* It is not possible to jump a second time.
3775 * FIXME: should we return an HTTP/500 here so that
3776 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003777 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003778 if (t->be != t->fe)
3779 break;
3780
3781 /* Swithing Proxy */
3782 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003784 /* right now, the backend switch is not too much complicated
3785 * because we have associated req_cap and rsp_cap to the
3786 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003787 */
3788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003789 t->rep->rto = t->req->wto = t->be->beprm->srvtimeout;
3790 t->req->cto = t->be->beprm->contimeout;
3791 done = 1;
3792 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003794 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003795 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003796 done = 1;
3797 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003799 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003800 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003801 t->be->beprm->denied_req++;
3802 done = 1;
3803 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003805 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003806 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003807 t->be->beprm->denied_req++;
3808 done = 1;
3809 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003810
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003811 case ACT_REPLACE:
3812 *cur_end = term; /* restore the string terminator */
3813 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3814 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3815 /* FIXME: if the user adds a newline in the replacement, the
3816 * index will not be recalculated for now, and the new line
3817 * will not be counted as a new header.
3818 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003819
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003820 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003821 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003822
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003823 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003824 HTTP_MSG_RQMETH,
3825 cur_ptr, cur_end + 1,
3826 NULL, NULL);
3827 if (unlikely(!cur_end))
3828 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003829
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003830 /* we have a full request and we know that we have either a CR
3831 * or an LF at <ptr>.
3832 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003833 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3834 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003835 /* there is no point trying this regex on headers */
3836 return 1;
3837 }
3838 }
3839 *cur_end = term; /* restore the string terminator */
3840 return done;
3841}
Willy Tarreau97de6242006-12-27 17:18:38 +01003842
Willy Tarreau58f10d72006-12-04 02:26:12 +01003843
Willy Tarreau58f10d72006-12-04 02:26:12 +01003844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003845/*
3846 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3847 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003848 * unparsable request. Since it can manage the switch to another backend, it
3849 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003850 */
3851int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3852{
Willy Tarreau3d300592007-03-18 18:34:41 +01003853 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003854 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003855 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003856 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003857
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003858 /*
3859 * The interleaving of transformations and verdicts
3860 * makes it difficult to decide to continue or stop
3861 * the evaluation.
3862 */
3863
Willy Tarreau3d300592007-03-18 18:34:41 +01003864 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003865 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3866 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3867 exp = exp->next;
3868 continue;
3869 }
3870
3871 /* Apply the filter to the request line. */
3872 ret = apply_filter_to_req_line(t, req, exp);
3873 if (unlikely(ret < 0))
3874 return -1;
3875
3876 if (likely(ret == 0)) {
3877 /* The filter did not match the request, it can be
3878 * iterated through all headers.
3879 */
3880 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003881 }
3882 exp = exp->next;
3883 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003884 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003885}
3886
3887
Willy Tarreaua15645d2007-03-18 16:22:39 +01003888
Willy Tarreau58f10d72006-12-04 02:26:12 +01003889/*
3890 * Manager client-side cookie
3891 */
3892void manage_client_side_cookies(struct session *t, struct buffer *req)
3893{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003894 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003895 char *p1, *p2, *p3, *p4;
3896 char *del_colon, *del_cookie, *colon;
3897 int app_cookies;
3898
3899 appsess *asession_temp = NULL;
3900 appsess local_asession;
3901
3902 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003903 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003904
Willy Tarreau830ff452006-12-17 19:31:23 +01003905 if (t->be->beprm->cookie_name == NULL &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003906 t->be->beprm->appsession_name == NULL &&
3907 t->be->fiprm->capture_name == NULL)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003908 return;
3909
Willy Tarreau2a324282006-12-05 00:05:46 +01003910 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003911 * we start with the start line.
3912 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003913 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003914 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003915
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003916 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003917 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003918 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003919
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003920 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003921 cur_ptr = cur_next;
3922 cur_end = cur_ptr + cur_hdr->len;
3923 cur_next = cur_end + cur_hdr->cr + 1;
3924
3925 /* We have one full header between cur_ptr and cur_end, and the
3926 * next header starts at cur_next. We're only interested in
3927 * "Cookie:" headers.
3928 */
3929
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003930 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3931 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003932 old_idx = cur_idx;
3933 continue;
3934 }
3935
3936 /* Now look for cookies. Conforming to RFC2109, we have to support
3937 * attributes whose name begin with a '$', and associate them with
3938 * the right cookie, if we want to delete this cookie.
3939 * So there are 3 cases for each cookie read :
3940 * 1) it's a special attribute, beginning with a '$' : ignore it.
3941 * 2) it's a server id cookie that we *MAY* want to delete : save
3942 * some pointers on it (last semi-colon, beginning of cookie...)
3943 * 3) it's an application cookie : we *MAY* have to delete a previous
3944 * "special" cookie.
3945 * At the end of loop, if a "special" cookie remains, we may have to
3946 * remove it. If no application cookie persists in the header, we
3947 * *MUST* delete it
3948 */
3949
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003950 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003951
Willy Tarreau58f10d72006-12-04 02:26:12 +01003952 /* del_cookie == NULL => nothing to be deleted */
3953 del_colon = del_cookie = NULL;
3954 app_cookies = 0;
3955
3956 while (p1 < cur_end) {
3957 /* skip spaces and colons, but keep an eye on these ones */
3958 while (p1 < cur_end) {
3959 if (*p1 == ';' || *p1 == ',')
3960 colon = p1;
3961 else if (!isspace((int)*p1))
3962 break;
3963 p1++;
3964 }
3965
3966 if (p1 == cur_end)
3967 break;
3968
3969 /* p1 is at the beginning of the cookie name */
3970 p2 = p1;
3971 while (p2 < cur_end && *p2 != '=')
3972 p2++;
3973
3974 if (p2 == cur_end)
3975 break;
3976
3977 p3 = p2 + 1; /* skips the '=' sign */
3978 if (p3 == cur_end)
3979 break;
3980
3981 p4 = p3;
3982 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',')
3983 p4++;
3984
3985 /* here, we have the cookie name between p1 and p2,
3986 * and its value between p3 and p4.
3987 * we can process it :
3988 *
3989 * Cookie: NAME=VALUE;
3990 * | || || |
3991 * | || || +--> p4
3992 * | || |+-------> p3
3993 * | || +--------> p2
3994 * | |+------------> p1
3995 * | +-------------> colon
3996 * +--------------------> cur_ptr
3997 */
3998
3999 if (*p1 == '$') {
4000 /* skip this one */
4001 }
4002 else {
4003 /* first, let's see if we want to capture it */
Willy Tarreau830ff452006-12-17 19:31:23 +01004004 if (t->fe->fiprm->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004005 txn->cli_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01004006 (p4 - p1 >= t->fe->fiprm->capture_namelen) &&
4007 memcmp(p1, t->fe->fiprm->capture_name, t->fe->fiprm->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004008 int log_len = p4 - p1;
4009
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004010 if ((txn->cli_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004011 Alert("HTTP logging : out of memory.\n");
4012 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004013 if (log_len > t->fe->fiprm->capture_len)
4014 log_len = t->fe->fiprm->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004015 memcpy(txn->cli_cookie, p1, log_len);
4016 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004017 }
4018 }
4019
Willy Tarreau830ff452006-12-17 19:31:23 +01004020 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
4021 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004022 /* Cool... it's the right one */
Willy Tarreau830ff452006-12-17 19:31:23 +01004023 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004024 char *delim;
4025
4026 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4027 * have the server ID betweek p3 and delim, and the original cookie between
4028 * delim+1 and p4. Otherwise, delim==p4 :
4029 *
4030 * Cookie: NAME=SRV~VALUE;
4031 * | || || | |
4032 * | || || | +--> p4
4033 * | || || +--------> delim
4034 * | || |+-----------> p3
4035 * | || +------------> p2
4036 * | |+----------------> p1
4037 * | +-----------------> colon
4038 * +------------------------> cur_ptr
4039 */
4040
Willy Tarreau830ff452006-12-17 19:31:23 +01004041 if (t->be->beprm->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004042 for (delim = p3; delim < p4; delim++)
4043 if (*delim == COOKIE_DELIM)
4044 break;
4045 }
4046 else
4047 delim = p4;
4048
4049
4050 /* Here, we'll look for the first running server which supports the cookie.
4051 * This allows to share a same cookie between several servers, for example
4052 * to dedicate backup servers to specific servers only.
4053 * However, to prevent clients from sticking to cookie-less backup server
4054 * when they have incidentely learned an empty cookie, we simply ignore
4055 * empty cookies and mark them as invalid.
4056 */
4057 if (delim == p3)
4058 srv = NULL;
4059
4060 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004061 if (srv->cookie && (srv->cklen == delim - p3) &&
4062 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004063 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004064 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004065 txn->flags &= ~TX_CK_MASK;
4066 txn->flags |= TX_CK_VALID;
4067 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004068 t->srv = srv;
4069 break;
4070 } else {
4071 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004072 txn->flags &= ~TX_CK_MASK;
4073 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004074 }
4075 }
4076 srv = srv->next;
4077 }
4078
Willy Tarreau3d300592007-03-18 18:34:41 +01004079 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004080 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004081 txn->flags &= ~TX_CK_MASK;
4082 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004083 }
4084
4085 /* depending on the cookie mode, we may have to either :
4086 * - delete the complete cookie if we're in insert+indirect mode, so that
4087 * the server never sees it ;
4088 * - remove the server id from the cookie value, and tag the cookie as an
4089 * application cookie so that it does not get accidentely removed later,
4090 * if we're in cookie prefix mode
4091 */
Willy Tarreau830ff452006-12-17 19:31:23 +01004092 if ((t->be->beprm->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004093 int delta; /* negative */
4094
4095 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4096 p4 += delta;
4097 cur_end += delta;
4098 cur_next += delta;
4099 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004100 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004101
4102 del_cookie = del_colon = NULL;
4103 app_cookies++; /* protect the header from deletion */
4104 }
4105 else if (del_cookie == NULL &&
Willy Tarreau830ff452006-12-17 19:31:23 +01004106 (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 +01004107 del_cookie = p1;
4108 del_colon = colon;
4109 }
4110 } else {
4111 /* now we know that we must keep this cookie since it's
4112 * not ours. But if we wanted to delete our cookie
4113 * earlier, we cannot remove the complete header, but we
4114 * can remove the previous block itself.
4115 */
4116 app_cookies++;
4117
4118 if (del_cookie != NULL) {
4119 int delta; /* negative */
4120
4121 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4122 p4 += delta;
4123 cur_end += delta;
4124 cur_next += delta;
4125 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004126 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004127 del_cookie = del_colon = NULL;
4128 }
4129 }
4130
Willy Tarreau830ff452006-12-17 19:31:23 +01004131 if ((t->be->beprm->appsession_name != NULL) &&
4132 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004133 /* first, let's see if the cookie is our appcookie*/
4134
4135 /* Cool... it's the right one */
4136
4137 asession_temp = &local_asession;
4138
4139 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4140 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4141 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4142 return;
4143 }
4144
Willy Tarreau830ff452006-12-17 19:31:23 +01004145 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4146 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004147 asession_temp->serverid = NULL;
4148
4149 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004150 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *) &asession_temp) != 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004151 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4152 /* free previously allocated memory */
4153 pool_free_to(apools.sessid, local_asession.sessid);
4154 Alert("Not enough memory process_cli():asession:calloc().\n");
4155 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4156 return;
4157 }
4158
4159 asession_temp->sessid = local_asession.sessid;
4160 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004161 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004162 } else {
4163 /* free previously allocated memory */
4164 pool_free_to(apools.sessid, local_asession.sessid);
4165 }
4166
4167 if (asession_temp->serverid == NULL) {
4168 Alert("Found Application Session without matching server.\n");
4169 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004170 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004171 while (srv) {
4172 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004173 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004174 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004175 txn->flags &= ~TX_CK_MASK;
4176 txn->flags |= TX_CK_VALID;
4177 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004178 t->srv = srv;
4179 break;
4180 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004181 txn->flags &= ~TX_CK_MASK;
4182 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004183 }
4184 }
4185 srv = srv->next;
4186 }/* end while(srv) */
4187 }/* end else if server == NULL */
4188
Willy Tarreau830ff452006-12-17 19:31:23 +01004189 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004190 }/* end if ((t->proxy->appsession_name != NULL) ... */
4191 }
4192
4193 /* we'll have to look for another cookie ... */
4194 p1 = p4;
4195 } /* while (p1 < cur_end) */
4196
4197 /* There's no more cookie on this line.
4198 * We may have marked the last one(s) for deletion.
4199 * We must do this now in two ways :
4200 * - if there is no app cookie, we simply delete the header ;
4201 * - if there are app cookies, we must delete the end of the
4202 * string properly, including the colon/semi-colon before
4203 * the cookie name.
4204 */
4205 if (del_cookie != NULL) {
4206 int delta;
4207 if (app_cookies) {
4208 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4209 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004210 cur_hdr->len += delta;
4211 } else {
4212 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004213
4214 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004215 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4216 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004217 cur_hdr->len = 0;
4218 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004219 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004220 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004221 }
4222
4223 /* keep the link from this header to next one */
4224 old_idx = cur_idx;
4225 } /* end of cookie processing on this header */
4226}
4227
4228
Willy Tarreaua15645d2007-03-18 16:22:39 +01004229/* Iterate the same filter through all response headers contained in <rtr>.
4230 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4231 */
4232int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4233{
4234 char term;
4235 char *cur_ptr, *cur_end, *cur_next;
4236 int cur_idx, old_idx, last_hdr;
4237 struct http_txn *txn = &t->txn;
4238 struct hdr_idx_elem *cur_hdr;
4239 int len, delta;
4240
4241 last_hdr = 0;
4242
4243 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4244 old_idx = 0;
4245
4246 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004247 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004248 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004249 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004250 (exp->action == ACT_ALLOW ||
4251 exp->action == ACT_DENY))
4252 return 0;
4253
4254 cur_idx = txn->hdr_idx.v[old_idx].next;
4255 if (!cur_idx)
4256 break;
4257
4258 cur_hdr = &txn->hdr_idx.v[cur_idx];
4259 cur_ptr = cur_next;
4260 cur_end = cur_ptr + cur_hdr->len;
4261 cur_next = cur_end + cur_hdr->cr + 1;
4262
4263 /* Now we have one header between cur_ptr and cur_end,
4264 * and the next header starts at cur_next.
4265 */
4266
4267 /* The annoying part is that pattern matching needs
4268 * that we modify the contents to null-terminate all
4269 * strings before testing them.
4270 */
4271
4272 term = *cur_end;
4273 *cur_end = '\0';
4274
4275 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4276 switch (exp->action) {
4277 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004278 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004279 last_hdr = 1;
4280 break;
4281
4282 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004283 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004284 last_hdr = 1;
4285 break;
4286
4287 case ACT_REPLACE:
4288 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4289 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4290 /* FIXME: if the user adds a newline in the replacement, the
4291 * index will not be recalculated for now, and the new line
4292 * will not be counted as a new header.
4293 */
4294
4295 cur_end += delta;
4296 cur_next += delta;
4297 cur_hdr->len += delta;
4298 txn->rsp.eoh += delta;
4299 break;
4300
4301 case ACT_REMOVE:
4302 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4303 cur_next += delta;
4304
4305 /* FIXME: this should be a separate function */
4306 txn->rsp.eoh += delta;
4307 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4308 txn->hdr_idx.used--;
4309 cur_hdr->len = 0;
4310 cur_end = NULL; /* null-term has been rewritten */
4311 break;
4312
4313 }
4314 }
4315 if (cur_end)
4316 *cur_end = term; /* restore the string terminator */
4317
4318 /* keep the link from this header to next one in case of later
4319 * removal of next header.
4320 */
4321 old_idx = cur_idx;
4322 }
4323 return 0;
4324}
4325
4326
4327/* Apply the filter to the status line in the response buffer <rtr>.
4328 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4329 * or -1 if a replacement resulted in an invalid status line.
4330 */
4331int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4332{
4333 char term;
4334 char *cur_ptr, *cur_end;
4335 int done;
4336 struct http_txn *txn = &t->txn;
4337 int len, delta;
4338
4339
Willy Tarreau3d300592007-03-18 18:34:41 +01004340 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004341 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004342 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004343 (exp->action == ACT_ALLOW ||
4344 exp->action == ACT_DENY))
4345 return 0;
4346 else if (exp->action == ACT_REMOVE)
4347 return 0;
4348
4349 done = 0;
4350
4351 cur_ptr = rtr->data + txn->rsp.som;
4352 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4353
4354 /* Now we have the status line between cur_ptr and cur_end */
4355
4356 /* The annoying part is that pattern matching needs
4357 * that we modify the contents to null-terminate all
4358 * strings before testing them.
4359 */
4360
4361 term = *cur_end;
4362 *cur_end = '\0';
4363
4364 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4365 switch (exp->action) {
4366 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004367 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004368 done = 1;
4369 break;
4370
4371 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004372 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004373 done = 1;
4374 break;
4375
4376 case ACT_REPLACE:
4377 *cur_end = term; /* restore the string terminator */
4378 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4379 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4380 /* FIXME: if the user adds a newline in the replacement, the
4381 * index will not be recalculated for now, and the new line
4382 * will not be counted as a new header.
4383 */
4384
4385 txn->rsp.eoh += delta;
4386 cur_end += delta;
4387
4388 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
4389 HTTP_MSG_RQMETH,
4390 cur_ptr, cur_end + 1,
4391 NULL, NULL);
4392 if (unlikely(!cur_end))
4393 return -1;
4394
4395 /* we have a full respnse and we know that we have either a CR
4396 * or an LF at <ptr>.
4397 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004398 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004399 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4400 /* there is no point trying this regex on headers */
4401 return 1;
4402 }
4403 }
4404 *cur_end = term; /* restore the string terminator */
4405 return done;
4406}
4407
4408
4409
4410/*
4411 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4412 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4413 * unparsable response.
4414 */
4415int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4416{
Willy Tarreau3d300592007-03-18 18:34:41 +01004417 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004418 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004419 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004420 int ret;
4421
4422 /*
4423 * The interleaving of transformations and verdicts
4424 * makes it difficult to decide to continue or stop
4425 * the evaluation.
4426 */
4427
Willy Tarreau3d300592007-03-18 18:34:41 +01004428 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004429 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4430 exp->action == ACT_PASS)) {
4431 exp = exp->next;
4432 continue;
4433 }
4434
4435 /* Apply the filter to the status line. */
4436 ret = apply_filter_to_sts_line(t, rtr, exp);
4437 if (unlikely(ret < 0))
4438 return -1;
4439
4440 if (likely(ret == 0)) {
4441 /* The filter did not match the response, it can be
4442 * iterated through all headers.
4443 */
4444 apply_filter_to_resp_headers(t, rtr, exp);
4445 }
4446 exp = exp->next;
4447 }
4448 return 0;
4449}
4450
4451
4452
4453/*
4454 * Manager server-side cookies
4455 */
4456void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4457{
4458 struct http_txn *txn = &t->txn;
4459 char *p1, *p2, *p3, *p4;
4460
4461 appsess *asession_temp = NULL;
4462 appsess local_asession;
4463
4464 char *cur_ptr, *cur_end, *cur_next;
4465 int cur_idx, old_idx, delta;
4466
4467 if (t->be->beprm->cookie_name == NULL &&
4468 t->be->beprm->appsession_name == NULL &&
4469 t->be->fiprm->capture_name == NULL &&
4470 !(t->be->beprm->options & PR_O_CHK_CACHE))
4471 return;
4472
4473 /* Iterate through the headers.
4474 * we start with the start line.
4475 */
4476 old_idx = 0;
4477 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4478
4479 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4480 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004481 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004482
4483 cur_hdr = &txn->hdr_idx.v[cur_idx];
4484 cur_ptr = cur_next;
4485 cur_end = cur_ptr + cur_hdr->len;
4486 cur_next = cur_end + cur_hdr->cr + 1;
4487
4488 /* We have one full header between cur_ptr and cur_end, and the
4489 * next header starts at cur_next. We're only interested in
4490 * "Cookie:" headers.
4491 */
4492
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004493 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4494 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004495 old_idx = cur_idx;
4496 continue;
4497 }
4498
4499 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004500 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004501
4502
4503 /* maybe we only wanted to see if there was a set-cookie */
4504 if (t->be->beprm->cookie_name == NULL &&
4505 t->be->beprm->appsession_name == NULL &&
4506 t->be->fiprm->capture_name == NULL)
4507 return;
4508
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004509 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004510
4511 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004512 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4513 break;
4514
4515 /* p1 is at the beginning of the cookie name */
4516 p2 = p1;
4517
4518 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4519 p2++;
4520
4521 if (p2 == cur_end || *p2 == ';') /* next cookie */
4522 break;
4523
4524 p3 = p2 + 1; /* skip the '=' sign */
4525 if (p3 == cur_end)
4526 break;
4527
4528 p4 = p3;
4529 while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';')
4530 p4++;
4531
4532 /* here, we have the cookie name between p1 and p2,
4533 * and its value between p3 and p4.
4534 * we can process it.
4535 */
4536
4537 /* first, let's see if we want to capture it */
4538 if (t->be->fiprm->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004539 txn->srv_cookie == NULL &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004540 (p4 - p1 >= t->be->fiprm->capture_namelen) &&
4541 memcmp(p1, t->be->fiprm->capture_name, t->be->fiprm->capture_namelen) == 0) {
4542 int log_len = p4 - p1;
4543
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004544 if ((txn->srv_cookie = pool_alloc(capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004545 Alert("HTTP logging : out of memory.\n");
4546 }
4547
4548 if (log_len > t->be->fiprm->capture_len)
4549 log_len = t->be->fiprm->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004550 memcpy(txn->srv_cookie, p1, log_len);
4551 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004552 }
4553
4554 /* now check if we need to process it for persistence */
4555 if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) &&
4556 (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) {
4557 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004558 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004559
4560 /* If the cookie is in insert mode on a known server, we'll delete
4561 * this occurrence because we'll insert another one later.
4562 * We'll delete it too if the "indirect" option is set and we're in
4563 * a direct access. */
4564 if (((t->srv) && (t->be->beprm->options & PR_O_COOK_INS)) ||
4565 ((t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_IND))) {
4566 /* this header must be deleted */
4567 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4568 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4569 txn->hdr_idx.used--;
4570 cur_hdr->len = 0;
4571 cur_next += delta;
4572 txn->rsp.eoh += delta;
4573
Willy Tarreau3d300592007-03-18 18:34:41 +01004574 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004575 }
4576 else if ((t->srv) && (t->srv->cookie) &&
4577 (t->be->beprm->options & PR_O_COOK_RW)) {
4578 /* replace bytes p3->p4 with the cookie name associated
4579 * with this server since we know it.
4580 */
4581 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4582 cur_hdr->len += delta;
4583 cur_next += delta;
4584 txn->rsp.eoh += delta;
4585
Willy Tarreau3d300592007-03-18 18:34:41 +01004586 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004587 }
4588 else if ((t->srv) && (t->srv->cookie) &&
4589 (t->be->beprm->options & PR_O_COOK_PFX)) {
4590 /* insert the cookie name associated with this server
4591 * before existing cookie, and insert a delimitor between them..
4592 */
4593 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4594 cur_hdr->len += delta;
4595 cur_next += delta;
4596 txn->rsp.eoh += delta;
4597
4598 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004599 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004600 }
4601 }
4602 /* next, let's see if the cookie is our appcookie */
4603 else if ((t->be->beprm->appsession_name != NULL) &&
4604 (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) {
4605
4606 /* Cool... it's the right one */
4607
4608 size_t server_id_len = strlen(t->srv->id) + 1;
4609 asession_temp = &local_asession;
4610
4611 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4612 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4613 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4614 return;
4615 }
4616 memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len);
4617 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
4618 asession_temp->serverid = NULL;
4619
4620 /* only do insert, if lookup fails */
4621 if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) {
4622 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4623 Alert("Not enough Memory process_srv():asession:calloc().\n");
4624 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4625 return;
4626 }
4627 asession_temp->sessid = local_asession.sessid;
4628 asession_temp->serverid = local_asession.serverid;
4629 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
4630 }/* end if (chtbl_lookup()) */
4631 else {
4632 /* free wasted memory */
4633 pool_free_to(apools.sessid, local_asession.sessid);
4634 } /* end else from if (chtbl_lookup()) */
4635
4636 if (asession_temp->serverid == NULL) {
4637 if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) {
4638 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4639 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4640 return;
4641 }
4642 asession_temp->serverid[0] = '\0';
4643 }
4644
4645 if (asession_temp->serverid[0] == '\0')
4646 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4647
4648 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
4649
4650#if defined(DEBUG_HASH)
4651 print_table(&(t->be->beprm->htbl_proxy));
4652#endif
4653 }/* end if ((t->proxy->appsession_name != NULL) ... */
4654 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4655 } /* we're now at the end of the cookie value */
4656
4657 /* keep the link from this header to next one */
4658 old_idx = cur_idx;
4659 } /* end of cookie processing on this header */
4660}
4661
4662
4663
4664/*
4665 * Check if response is cacheable or not. Updates t->flags.
4666 */
4667void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4668{
4669 struct http_txn *txn = &t->txn;
4670 char *p1, *p2;
4671
4672 char *cur_ptr, *cur_end, *cur_next;
4673 int cur_idx;
4674
Willy Tarreau3d300592007-03-18 18:34:41 +01004675 if (!txn->flags & TX_CACHEABLE)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004676 return;
4677
4678 /* Iterate through the headers.
4679 * we start with the start line.
4680 */
4681 cur_idx = 0;
4682 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4683
4684 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4685 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004686 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004687
4688 cur_hdr = &txn->hdr_idx.v[cur_idx];
4689 cur_ptr = cur_next;
4690 cur_end = cur_ptr + cur_hdr->len;
4691 cur_next = cur_end + cur_hdr->cr + 1;
4692
4693 /* We have one full header between cur_ptr and cur_end, and the
4694 * next header starts at cur_next. We're only interested in
4695 * "Cookie:" headers.
4696 */
4697
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004698 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4699 if (val) {
4700 if ((cur_end - (cur_ptr + val) >= 8) &&
4701 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4702 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4703 return;
4704 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004705 }
4706
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004707 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4708 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004709 continue;
4710
4711 /* OK, right now we know we have a cache-control header at cur_ptr */
4712
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004713 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004714
4715 if (p1 >= cur_end) /* no more info */
4716 continue;
4717
4718 /* p1 is at the beginning of the value */
4719 p2 = p1;
4720
4721 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2))
4722 p2++;
4723
4724 /* we have a complete value between p1 and p2 */
4725 if (p2 < cur_end && *p2 == '=') {
4726 /* we have something of the form no-cache="set-cookie" */
4727 if ((cur_end - p1 >= 21) &&
4728 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4729 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004730 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004731 continue;
4732 }
4733
4734 /* OK, so we know that either p2 points to the end of string or to a comma */
4735 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4736 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4737 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4738 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004739 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004740 return;
4741 }
4742
4743 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004744 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004745 continue;
4746 }
4747 }
4748}
4749
4750
Willy Tarreau58f10d72006-12-04 02:26:12 +01004751/*
4752 * Try to retrieve a known appsession in the URI, then the associated server.
4753 * If the server is found, it's assigned to the session.
4754 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004755void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004756{
Willy Tarreau3d300592007-03-18 18:34:41 +01004757 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004758 appsess *asession_temp = NULL;
4759 appsess local_asession;
4760 char *request_line;
4761
Willy Tarreau830ff452006-12-17 19:31:23 +01004762 if (t->be->beprm->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004763 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004764 (request_line = memchr(begin, ';', len)) == NULL ||
4765 ((1 + t->be->beprm->appsession_name_len + 1 + t->be->beprm->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004766 return;
4767
4768 /* skip ';' */
4769 request_line++;
4770
4771 /* look if we have a jsessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004772 if (strncasecmp(request_line, t->be->beprm->appsession_name, t->be->beprm->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004773 return;
4774
4775 /* skip jsessionid= */
Willy Tarreau830ff452006-12-17 19:31:23 +01004776 request_line += t->be->beprm->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004777
4778 /* First try if we already have an appsession */
4779 asession_temp = &local_asession;
4780
4781 if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) {
4782 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4783 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4784 return;
4785 }
4786
4787 /* Copy the sessionid */
Willy Tarreau830ff452006-12-17 19:31:23 +01004788 memcpy(asession_temp->sessid, request_line, t->be->beprm->appsession_len);
4789 asession_temp->sessid[t->be->beprm->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004790 asession_temp->serverid = NULL;
4791
4792 /* only do insert, if lookup fails */
Willy Tarreau830ff452006-12-17 19:31:23 +01004793 if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *)&asession_temp)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004794 if ((asession_temp = pool_alloc(appsess)) == NULL) {
4795 /* free previously allocated memory */
4796 pool_free_to(apools.sessid, local_asession.sessid);
4797 Alert("Not enough memory process_cli():asession:calloc().\n");
4798 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4799 return;
4800 }
4801 asession_temp->sessid = local_asession.sessid;
4802 asession_temp->serverid = local_asession.serverid;
Willy Tarreau830ff452006-12-17 19:31:23 +01004803 chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004804 }
4805 else {
4806 /* free previously allocated memory */
4807 pool_free_to(apools.sessid, local_asession.sessid);
4808 }
4809
Willy Tarreau830ff452006-12-17 19:31:23 +01004810 tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004811 asession_temp->request_count++;
4812
4813#if defined(DEBUG_HASH)
4814 print_table(&(t->proxy->htbl_proxy));
4815#endif
4816 if (asession_temp->serverid == NULL) {
4817 Alert("Found Application Session without matching server.\n");
4818 } else {
Willy Tarreau830ff452006-12-17 19:31:23 +01004819 struct server *srv = t->be->beprm->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004820 while (srv) {
4821 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreau830ff452006-12-17 19:31:23 +01004822 if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004823 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004824 txn->flags &= ~TX_CK_MASK;
4825 txn->flags |= TX_CK_VALID;
4826 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004827 t->srv = srv;
4828 break;
4829 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004830 txn->flags &= ~TX_CK_MASK;
4831 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004832 }
4833 }
4834 srv = srv->next;
4835 }
4836 }
4837}
4838
4839
Willy Tarreaub2513902006-12-17 14:52:38 +01004840/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004841 * In a GET or HEAD request, check if the requested URI matches the stats uri
4842 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004843 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004844 * It is assumed that the request is either a HEAD or GET and that the
4845 * t->be->fiprm->uri_auth field is valid. An HTTP/401 response may be sent, or
4846 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004847 *
4848 * Returns 1 if the session's state changes, otherwise 0.
4849 */
4850int stats_check_uri_auth(struct session *t, struct proxy *backend)
4851{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004852 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004853 struct uri_auth *uri_auth = backend->uri_auth;
4854 struct user_auth *user;
4855 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004856 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004857
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004858 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004859 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004860 return 0;
4861
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004862 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004863
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004864 /* the URI is in h */
4865 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004866 return 0;
4867
4868 /* we are in front of a interceptable URI. Let's check
4869 * if there's an authentication and if it's valid.
4870 */
4871 user = uri_auth->users;
4872 if (!user) {
4873 /* no user auth required, it's OK */
4874 authenticated = 1;
4875 } else {
4876 authenticated = 0;
4877
4878 /* a user list is defined, we have to check.
4879 * skip 21 chars for "Authorization: Basic ".
4880 */
4881
4882 /* FIXME: this should move to an earlier place */
4883 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004884 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4885 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4886 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004887 if (len > 14 &&
4888 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004889 txn->auth_hdr.str = h;
4890 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004891 break;
4892 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004893 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004894 }
4895
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004896 if (txn->auth_hdr.len < 21 ||
4897 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004898 user = NULL;
4899
4900 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004901 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4902 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004903 user->user_pwd, user->user_len)) {
4904 authenticated = 1;
4905 break;
4906 }
4907 user = user->next;
4908 }
4909 }
4910
4911 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004912 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004913
4914 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004915 msg.str = trash;
4916 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004917 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004918 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004919 if (!(t->flags & SN_ERR_MASK))
4920 t->flags |= SN_ERR_PRXCOND;
4921 if (!(t->flags & SN_FINST_MASK))
4922 t->flags |= SN_FINST_R;
4923 return 1;
4924 }
4925
4926 /* The request is valid, the user is authenticate. Let's start sending
4927 * data.
4928 */
4929 t->cli_state = CL_STSHUTR;
4930 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
4931 t->logs.t_request = tv_diff(&t->logs.tv_accept, &now);
4932 t->data_source = DATA_SRC_STATS;
4933 t->data_state = DATA_ST_INIT;
4934 produce_content(t);
4935 return 1;
4936}
4937
4938
Willy Tarreaubaaee002006-06-26 02:48:02 +02004939/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004940 * Print a debug line with a header
4941 */
4942void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4943{
4944 int len, max;
4945 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4946 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4947 max = end - start;
4948 UBOUND(max, sizeof(trash) - len - 1);
4949 len += strlcpy2(trash + len, start, max + 1);
4950 trash[len++] = '\n';
4951 write(1, trash, len);
4952}
4953
4954
4955/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004956 * Local variables:
4957 * c-indent-level: 8
4958 * c-basic-offset: 8
4959 * End:
4960 */