blob: 16028cedafcaf52f812370264488c68b2c84ff46 [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>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
33#include <common/time.h>
34#include <common/uri_auth.h>
35#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau8797c062007-05-07 00:55:35 +020037#include <types/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <types/capture.h>
39#include <types/client.h>
40#include <types/global.h>
41#include <types/httperr.h>
42#include <types/polling.h>
43#include <types/proxy.h>
44#include <types/server.h>
45
Willy Tarreau8797c062007-05-07 00:55:35 +020046#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/backend.h>
48#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
51#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010052#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/proto_http.h>
54#include <proto/queue.h>
Willy Tarreau91861262007-10-17 17:06:05 +020055#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/session.h>
57#include <proto/task.h>
58
Willy Tarreau6d1a9882007-01-07 02:03:04 +010059#ifdef CONFIG_HAP_TCPSPLICE
60#include <libtcpsplice.h>
61#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Willy Tarreau58f10d72006-12-04 02:26:12 +010063#define DEBUG_PARSE_NO_SPEEDUP
64#undef DEBUG_PARSE_NO_SPEEDUP
65
Willy Tarreau976f1ee2006-12-17 10:06:03 +010066/* This is used to perform a quick jump as an alternative to a break/continue
67 * instruction. The first argument is the label for normal operation, and the
68 * second one is the break/continue instruction in the no_speedup mode.
69 */
70
71#ifdef DEBUG_PARSE_NO_SPEEDUP
72#define QUICK_JUMP(x,y) y
73#else
74#define QUICK_JUMP(x,y) goto x
75#endif
76
Willy Tarreau1c47f852006-07-09 08:22:27 +020077/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010078const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020079 "HTTP/1.0 200 OK\r\n"
80 "Cache-Control: no-cache\r\n"
81 "Connection: close\r\n"
82 "Content-Type: text/html\r\n"
83 "\r\n"
84 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
85
Willy Tarreau0f772532006-12-23 20:51:41 +010086const struct chunk http_200_chunk = {
87 .str = (char *)&HTTP_200,
88 .len = sizeof(HTTP_200)-1
89};
90
91const char *HTTP_302 =
92 "HTTP/1.0 302 Found\r\n"
93 "Cache-Control: no-cache\r\n"
94 "Connection: close\r\n"
95 "Location: "; /* not terminated since it will be concatenated with the URL */
96
97/* same as 302 except that the browser MUST retry with the GET method */
98const char *HTTP_303 =
99 "HTTP/1.0 303 See Other\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Connection: close\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
105const char *HTTP_401_fmt =
106 "HTTP/1.0 401 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200109 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
116 [HTTP_ERR_400] = 400,
117 [HTTP_ERR_403] = 403,
118 [HTTP_ERR_408] = 408,
119 [HTTP_ERR_500] = 500,
120 [HTTP_ERR_502] = 502,
121 [HTTP_ERR_503] = 503,
122 [HTTP_ERR_504] = 504,
123};
124
Willy Tarreau80587432006-12-24 17:47:20 +0100125static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100127 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
133
134 [HTTP_ERR_403] =
135 "HTTP/1.0 403 Forbidden\r\n"
136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
141
142 [HTTP_ERR_408] =
143 "HTTP/1.0 408 Request Time-out\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
149
150 [HTTP_ERR_500] =
151 "HTTP/1.0 500 Server Error\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
157
158 [HTTP_ERR_502] =
159 "HTTP/1.0 502 Bad Gateway\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
165
166 [HTTP_ERR_503] =
167 "HTTP/1.0 503 Service Unavailable\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
173
174 [HTTP_ERR_504] =
175 "HTTP/1.0 504 Gateway Time-out\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
181
182};
183
Willy Tarreau80587432006-12-24 17:47:20 +0100184/* We must put the messages here since GCC cannot initialize consts depending
185 * on strlen().
186 */
187struct chunk http_err_chunks[HTTP_ERR_SIZE];
188
Willy Tarreau42250582007-04-01 01:30:43 +0200189#define FD_SETS_ARE_BITFIELDS
190#ifdef FD_SETS_ARE_BITFIELDS
191/*
192 * This map is used with all the FD_* macros to check whether a particular bit
193 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
194 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
195 * byte should be encoded. Be careful to always pass bytes from 0 to 255
196 * exclusively to the macros.
197 */
198fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
199fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
200
201#else
202#error "Check if your OS uses bitfields for fd_sets"
203#endif
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205void init_proto_http()
206{
Willy Tarreau42250582007-04-01 01:30:43 +0200207 int i;
208 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100209 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200210
Willy Tarreau80587432006-12-24 17:47:20 +0100211 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
212 if (!http_err_msgs[msg]) {
213 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
214 abort();
215 }
216
217 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
218 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
219 }
Willy Tarreau42250582007-04-01 01:30:43 +0200220
221 /* initialize the log header encoding map : '{|}"#' should be encoded with
222 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
223 * URL encoding only requires '"', '#' to be encoded as well as non-
224 * printable characters above.
225 */
226 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
227 memset(url_encode_map, 0, sizeof(url_encode_map));
228 for (i = 0; i < 32; i++) {
229 FD_SET(i, hdr_encode_map);
230 FD_SET(i, url_encode_map);
231 }
232 for (i = 127; i < 256; i++) {
233 FD_SET(i, hdr_encode_map);
234 FD_SET(i, url_encode_map);
235 }
236
237 tmp = "\"#{|}";
238 while (*tmp) {
239 FD_SET(*tmp, hdr_encode_map);
240 tmp++;
241 }
242
243 tmp = "\"#";
244 while (*tmp) {
245 FD_SET(*tmp, url_encode_map);
246 tmp++;
247 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200248
249 /* memory allocations */
250 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200251 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100252}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253
Willy Tarreau53b6c742006-12-17 13:37:46 +0100254/*
255 * We have 26 list of methods (1 per first letter), each of which can have
256 * up to 3 entries (2 valid, 1 null).
257 */
258struct http_method_desc {
259 http_meth_t meth;
260 int len;
261 const char text[8];
262};
263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100264const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100265 ['C' - 'A'] = {
266 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
267 },
268 ['D' - 'A'] = {
269 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
270 },
271 ['G' - 'A'] = {
272 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
273 },
274 ['H' - 'A'] = {
275 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
276 },
277 ['P' - 'A'] = {
278 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
279 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
280 },
281 ['T' - 'A'] = {
282 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
283 },
284 /* rest is empty like this :
285 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
286 */
287};
288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289/* It is about twice as fast on recent architectures to lookup a byte in a
290 * table than two perform a boolean AND or OR between two tests. Refer to
291 * RFC2616 for those chars.
292 */
293
294const char http_is_spht[256] = {
295 [' '] = 1, ['\t'] = 1,
296};
297
298const char http_is_crlf[256] = {
299 ['\r'] = 1, ['\n'] = 1,
300};
301
302const char http_is_lws[256] = {
303 [' '] = 1, ['\t'] = 1,
304 ['\r'] = 1, ['\n'] = 1,
305};
306
307const char http_is_sep[256] = {
308 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
309 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
310 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
311 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
312 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
313};
314
315const char http_is_ctl[256] = {
316 [0 ... 31] = 1,
317 [127] = 1,
318};
319
320/*
321 * A token is any ASCII char that is neither a separator nor a CTL char.
322 * Do not overwrite values in assignment since gcc-2.95 will not handle
323 * them correctly. Instead, define every non-CTL char's status.
324 */
325const char http_is_token[256] = {
326 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
327 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
328 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
329 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
330 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
331 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
332 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
333 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
334 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
335 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
336 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
337 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
338 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
339 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
340 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
341 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
342 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
343 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
344 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
345 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
346 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
347 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
348 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
349 ['|'] = 1, ['}'] = 0, ['~'] = 1,
350};
351
352
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100353/*
354 * An http ver_token is any ASCII which can be found in an HTTP version,
355 * which includes 'H', 'T', 'P', '/', '.' and any digit.
356 */
357const char http_is_ver_token[256] = {
358 ['.'] = 1, ['/'] = 1,
359 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
360 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
361 ['H'] = 1, ['P'] = 1, ['T'] = 1,
362};
363
364
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365#ifdef DEBUG_FULL
366static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" };
367static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" };
368#endif
369
Willy Tarreau42250582007-04-01 01:30:43 +0200370static void http_sess_log(struct session *s);
371
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100372/*
373 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
374 * CRLF. Text length is measured first, so it cannot be NULL.
375 * The header is also automatically added to the index <hdr_idx>, and the end
376 * of headers is automatically adjusted. The number of bytes added is returned
377 * on success, otherwise <0 is returned indicating an error.
378 */
379int http_header_add_tail(struct buffer *b, struct http_msg *msg,
380 struct hdr_idx *hdr_idx, const char *text)
381{
382 int bytes, len;
383
384 len = strlen(text);
385 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
386 if (!bytes)
387 return -1;
388 msg->eoh += bytes;
389 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
390}
391
392/*
393 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
394 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
395 * the buffer is only opened and the space reserved, but nothing is copied.
396 * The header is also automatically added to the index <hdr_idx>, and the end
397 * of headers is automatically adjusted. The number of bytes added is returned
398 * on success, otherwise <0 is returned indicating an error.
399 */
400int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
401 struct hdr_idx *hdr_idx, const char *text, int len)
402{
403 int bytes;
404
405 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
406 if (!bytes)
407 return -1;
408 msg->eoh += bytes;
409 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
410}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
412/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100413 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
414 * If so, returns the position of the first non-space character relative to
415 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
416 * to return a pointer to the place after the first space. Returns 0 if the
417 * header name does not match. Checks are case-insensitive.
418 */
419int http_header_match2(const char *hdr, const char *end,
420 const char *name, int len)
421{
422 const char *val;
423
424 if (hdr + len >= end)
425 return 0;
426 if (hdr[len] != ':')
427 return 0;
428 if (strncasecmp(hdr, name, len) != 0)
429 return 0;
430 val = hdr + len + 1;
431 while (val < end && HTTP_IS_SPHT(*val))
432 val++;
433 if ((val >= end) && (len + 2 <= end - hdr))
434 return len + 2; /* we may replace starting from second space */
435 return val - hdr;
436}
437
Willy Tarreau33a7e692007-06-10 19:45:56 +0200438/* Find the end of the header value contained between <s> and <e>.
439 * See RFC2616, par 2.2 for more information. Note that it requires
440 * a valid header to return a valid result.
441 */
442const char *find_hdr_value_end(const char *s, const char *e)
443{
444 int quoted, qdpair;
445
446 quoted = qdpair = 0;
447 for (; s < e; s++) {
448 if (qdpair) qdpair = 0;
449 else if (quoted && *s == '\\') qdpair = 1;
450 else if (quoted && *s == '"') quoted = 0;
451 else if (*s == '"') quoted = 1;
452 else if (*s == ',') return s;
453 }
454 return s;
455}
456
457/* Find the first or next occurrence of header <name> in message buffer <sol>
458 * using headers index <idx>, and return it in the <ctx> structure. This
459 * structure holds everything necessary to use the header and find next
460 * occurrence. If its <idx> member is 0, the header is searched from the
461 * beginning. Otherwise, the next occurrence is returned. The function returns
462 * 1 when it finds a value, and 0 when there is no more.
463 */
464int http_find_header2(const char *name, int len,
465 const char *sol, struct hdr_idx *idx,
466 struct hdr_ctx *ctx)
467{
468 __label__ return_hdr, next_hdr;
469 const char *eol, *sov;
470 int cur_idx;
471
472 if (ctx->idx) {
473 /* We have previously returned a value, let's search
474 * another one on the same line.
475 */
476 cur_idx = ctx->idx;
477 sol = ctx->line;
478 sov = sol + ctx->val + ctx->vlen;
479 eol = sol + idx->v[cur_idx].len;
480
481 if (sov >= eol)
482 /* no more values in this header */
483 goto next_hdr;
484
485 /* values remaining for this header, skip the comma */
486 sov++;
487 while (sov < eol && http_is_lws[(unsigned char)*sov])
488 sov++;
489
490 goto return_hdr;
491 }
492
493 /* first request for this header */
494 sol += hdr_idx_first_pos(idx);
495 cur_idx = hdr_idx_first_idx(idx);
496
497 while (cur_idx) {
498 eol = sol + idx->v[cur_idx].len;
499
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200500 if (len == 0) {
501 /* No argument was passed, we want any header.
502 * To achieve this, we simply build a fake request. */
503 while (sol + len < eol && sol[len] != ':')
504 len++;
505 name = sol;
506 }
507
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 if ((len < eol - sol) &&
509 (sol[len] == ':') &&
510 (strncasecmp(sol, name, len) == 0)) {
511
512 sov = sol + len + 1;
513 while (sov < eol && http_is_lws[(unsigned char)*sov])
514 sov++;
515 return_hdr:
516 ctx->line = sol;
517 ctx->idx = cur_idx;
518 ctx->val = sov - sol;
519
520 eol = find_hdr_value_end(sov, eol);
521 ctx->vlen = eol - sov;
522 return 1;
523 }
524 next_hdr:
525 sol = eol + idx->v[cur_idx].cr + 1;
526 cur_idx = idx->v[cur_idx].next;
527 }
528 return 0;
529}
530
531int http_find_header(const char *name,
532 const char *sol, struct hdr_idx *idx,
533 struct hdr_ctx *ctx)
534{
535 return http_find_header2(name, strlen(name), sol, idx, ctx);
536}
537
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100539 * indicators accordingly. Note that if <status> is 0, or if the message
540 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200541 */
542void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100543 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200544{
545 t->srv_state = SV_STCLOSE;
Willy Tarreau0f772532006-12-23 20:51:41 +0100546 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100547 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100548 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100549 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200550 }
551 if (!(t->flags & SN_ERR_MASK))
552 t->flags |= err;
553 if (!(t->flags & SN_FINST_MASK))
554 t->flags |= finst;
555}
556
Willy Tarreau80587432006-12-24 17:47:20 +0100557/* This function returns the appropriate error location for the given session
558 * and message.
559 */
560
561struct chunk *error_message(struct session *s, int msgnum)
562{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200563 if (s->be->errmsg[msgnum].str)
564 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100565 else if (s->fe->errmsg[msgnum].str)
566 return &s->fe->errmsg[msgnum];
567 else
568 return &http_err_chunks[msgnum];
569}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570
Willy Tarreau53b6c742006-12-17 13:37:46 +0100571/*
572 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
573 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
574 */
575static http_meth_t find_http_meth(const char *str, const int len)
576{
577 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100578 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100579
580 m = ((unsigned)*str - 'A');
581
582 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 for (h = http_methods[m]; h->len > 0; h++) {
584 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100585 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100586 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100587 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100588 };
589 return HTTP_METH_OTHER;
590 }
591 return HTTP_METH_NONE;
592
593}
594
Willy Tarreaubaaee002006-06-26 02:48:02 +0200595/* Processes the client and server jobs of a session task, then
596 * puts it back to the wait queue in a clean state, or
597 * cleans up its resources if it must be deleted. Returns
598 * the time the task accepts to wait, or TIME_ETERNITY for
599 * infinity.
600 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200601void process_session(struct task *t, struct timeval *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200602{
603 struct session *s = t->context;
604 int fsm_resync = 0;
605
606 do {
607 fsm_resync = 0;
608 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
609 fsm_resync |= process_cli(s);
610 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
611 fsm_resync |= process_srv(s);
612 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
613 } while (fsm_resync);
614
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200615 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100616
617 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
618 session_process_counters(s);
619
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200620 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
621 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200622
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200623 tv_min(&t->expire, &s->req->rex, &s->req->wex);
624 tv_bound(&t->expire, &s->req->cex);
625 tv_bound(&t->expire, &s->rep->rex);
626 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreau036fae02008-01-06 13:24:40 +0100627 if (s->cli_state == CL_STHEADERS)
628 tv_bound(&t->expire, &s->txn.exp);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629
630 /* restore t to its place in the task list */
631 task_queue(t);
632
633#ifdef DEBUG_FULL
634 /* DEBUG code : this should never ever happen, otherwise it indicates
635 * that a task still has something to do and will provoke a quick loop.
636 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200637 if (tv_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200638 exit(100);
639#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200640 *next = t->expire;
641 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 }
643
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100644 s->fe->feconn--;
645 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200646 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 actconn--;
648
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200649 if (unlikely((global.mode & MODE_DEBUG) &&
650 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200651 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100652 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200653 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100654 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655 write(1, trash, len);
656 }
657
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200658 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100659 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660
661 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200662 if (s->logs.logwait &&
663 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200664 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
665 if (s->fe->to_log & LW_REQ)
666 http_sess_log(s);
667 else
668 tcp_sess_log(s);
669 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670
671 /* the task MUST not be in the run queue anymore */
672 task_delete(t);
673 session_free(s);
674 task_free(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200675 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200676}
677
678
Willy Tarreau42250582007-04-01 01:30:43 +0200679extern const char sess_term_cond[8];
680extern const char sess_fin_state[8];
681extern const char *monthname[12];
682const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
683const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
684 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
685 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200686struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200687struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100688
Willy Tarreau42250582007-04-01 01:30:43 +0200689/*
690 * send a log for the session when we have enough info about it.
691 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100692 */
Willy Tarreau42250582007-04-01 01:30:43 +0200693static void http_sess_log(struct session *s)
694{
695 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
696 struct proxy *fe = s->fe;
697 struct proxy *be = s->be;
698 struct proxy *prx_log;
699 struct http_txn *txn = &s->txn;
700 int tolog;
701 char *uri, *h;
702 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200703 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200704 static char tmpline[MAX_SYSLOG_LEN];
705 int hdr;
706
707 if (fe->logfac1 < 0 && fe->logfac2 < 0)
708 return;
709 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100710
Willy Tarreau42250582007-04-01 01:30:43 +0200711 if (s->cli_addr.ss_family == AF_INET)
712 inet_ntop(AF_INET,
713 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
714 pn, sizeof(pn));
715 else
716 inet_ntop(AF_INET6,
717 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
718 pn, sizeof(pn));
719
Willy Tarreaufe944602007-10-25 10:34:16 +0200720 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200721
722 /* FIXME: let's limit ourselves to frontend logging for now. */
723 tolog = fe->to_log;
724
725 h = tmpline;
726 if (fe->to_log & LW_REQHDR &&
727 txn->req.cap &&
728 (h < tmpline + sizeof(tmpline) - 10)) {
729 *(h++) = ' ';
730 *(h++) = '{';
731 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
732 if (hdr)
733 *(h++) = '|';
734 if (txn->req.cap[hdr] != NULL)
735 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
736 '#', hdr_encode_map, txn->req.cap[hdr]);
737 }
738 *(h++) = '}';
739 }
740
741 if (fe->to_log & LW_RSPHDR &&
742 txn->rsp.cap &&
743 (h < tmpline + sizeof(tmpline) - 7)) {
744 *(h++) = ' ';
745 *(h++) = '{';
746 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
747 if (hdr)
748 *(h++) = '|';
749 if (txn->rsp.cap[hdr] != NULL)
750 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
751 '#', hdr_encode_map, txn->rsp.cap[hdr]);
752 }
753 *(h++) = '}';
754 }
755
756 if (h < tmpline + sizeof(tmpline) - 4) {
757 *(h++) = ' ';
758 *(h++) = '"';
759 uri = txn->uri ? txn->uri : "<BADREQ>";
760 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
761 '#', url_encode_map, uri);
762 *(h++) = '"';
763 }
764 *h = '\0';
765
766 svid = (tolog & LW_SVID) ?
767 (s->data_source != DATA_SRC_STATS) ?
768 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
769
770 send_log(prx_log, LOG_INFO,
771 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
772 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
773 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
774 pn,
775 (s->cli_addr.ss_family == AF_INET) ?
776 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
777 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200778 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
779 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200780 fe->id, be->id, svid,
781 s->logs.t_request,
782 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
783 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
784 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
785 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
786 txn->status,
787 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
788 txn->cli_cookie ? txn->cli_cookie : "-",
789 txn->srv_cookie ? txn->srv_cookie : "-",
790 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
791 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
792 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
793 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
794 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
795 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
796
797 s->logs.logwait = 0;
798}
799
Willy Tarreau117f59e2007-03-04 18:17:17 +0100800
801/*
802 * Capture headers from message starting at <som> according to header list
803 * <cap_hdr>, and fill the <idx> structure appropriately.
804 */
805void capture_headers(char *som, struct hdr_idx *idx,
806 char **cap, struct cap_hdr *cap_hdr)
807{
808 char *eol, *sol, *col, *sov;
809 int cur_idx;
810 struct cap_hdr *h;
811 int len;
812
813 sol = som + hdr_idx_first_pos(idx);
814 cur_idx = hdr_idx_first_idx(idx);
815
816 while (cur_idx) {
817 eol = sol + idx->v[cur_idx].len;
818
819 col = sol;
820 while (col < eol && *col != ':')
821 col++;
822
823 sov = col + 1;
824 while (sov < eol && http_is_lws[(unsigned char)*sov])
825 sov++;
826
827 for (h = cap_hdr; h; h = h->next) {
828 if ((h->namelen == col - sol) &&
829 (strncasecmp(sol, h->name, h->namelen) == 0)) {
830 if (cap[h->index] == NULL)
831 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200832 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100833
834 if (cap[h->index] == NULL) {
835 Alert("HTTP capture : out of memory.\n");
836 continue;
837 }
838
839 len = eol - sov;
840 if (len > h->len)
841 len = h->len;
842
843 memcpy(cap[h->index], sov, len);
844 cap[h->index][len]=0;
845 }
846 }
847 sol = eol + idx->v[cur_idx].cr + 1;
848 cur_idx = idx->v[cur_idx].next;
849 }
850}
851
852
Willy Tarreau42250582007-04-01 01:30:43 +0200853/* either we find an LF at <ptr> or we jump to <bad>.
854 */
855#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
856
857/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
858 * otherwise to <http_msg_ood> with <state> set to <st>.
859 */
860#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
861 ptr++; \
862 if (likely(ptr < end)) \
863 goto good; \
864 else { \
865 state = (st); \
866 goto http_msg_ood; \
867 } \
868 } while (0)
869
870
Willy Tarreaubaaee002006-06-26 02:48:02 +0200871/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100872 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100873 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
874 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
875 * will give undefined results.
876 * Note that it is upon the caller's responsibility to ensure that ptr < end,
877 * and that msg->sol points to the beginning of the response.
878 * If a complete line is found (which implies that at least one CR or LF is
879 * found before <end>, the updated <ptr> is returned, otherwise NULL is
880 * returned indicating an incomplete line (which does not mean that parts have
881 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
882 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
883 * upon next call.
884 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200885 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100886 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
887 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200888 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100889 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100890const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100891 const char *ptr, const char *end,
892 char **ret_ptr, int *ret_state)
893{
894 __label__
895 http_msg_rpver,
896 http_msg_rpver_sp,
897 http_msg_rpcode,
898 http_msg_rpcode_sp,
899 http_msg_rpreason,
900 http_msg_rpline_eol,
901 http_msg_ood, /* out of data */
902 http_msg_invalid;
903
904 switch (state) {
905 http_msg_rpver:
906 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100907 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100908 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
909
910 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100911 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100912 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
913 }
914 goto http_msg_invalid;
915
916 http_msg_rpver_sp:
917 case HTTP_MSG_RPVER_SP:
918 if (likely(!HTTP_IS_LWS(*ptr))) {
919 msg->sl.st.c = ptr - msg_buf;
920 goto http_msg_rpcode;
921 }
922 if (likely(HTTP_IS_SPHT(*ptr)))
923 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
924 /* so it's a CR/LF, this is invalid */
925 goto http_msg_invalid;
926
927 http_msg_rpcode:
928 case HTTP_MSG_RPCODE:
929 if (likely(!HTTP_IS_LWS(*ptr)))
930 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
931
932 if (likely(HTTP_IS_SPHT(*ptr))) {
933 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
934 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
935 }
936
937 /* so it's a CR/LF, so there is no reason phrase */
938 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
939 http_msg_rsp_reason:
940 /* FIXME: should we support HTTP responses without any reason phrase ? */
941 msg->sl.st.r = ptr - msg_buf;
942 msg->sl.st.r_l = 0;
943 goto http_msg_rpline_eol;
944
945 http_msg_rpcode_sp:
946 case HTTP_MSG_RPCODE_SP:
947 if (likely(!HTTP_IS_LWS(*ptr))) {
948 msg->sl.st.r = ptr - msg_buf;
949 goto http_msg_rpreason;
950 }
951 if (likely(HTTP_IS_SPHT(*ptr)))
952 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
953 /* so it's a CR/LF, so there is no reason phrase */
954 goto http_msg_rsp_reason;
955
956 http_msg_rpreason:
957 case HTTP_MSG_RPREASON:
958 if (likely(!HTTP_IS_CRLF(*ptr)))
959 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
960 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
961 http_msg_rpline_eol:
962 /* We have seen the end of line. Note that we do not
963 * necessarily have the \n yet, but at least we know that we
964 * have EITHER \r OR \n, otherwise the response would not be
965 * complete. We can then record the response length and return
966 * to the caller which will be able to register it.
967 */
968 msg->sl.st.l = ptr - msg->sol;
969 return ptr;
970
971#ifdef DEBUG_FULL
972 default:
973 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
974 exit(1);
975#endif
976 }
977
978 http_msg_ood:
979 /* out of data */
980 if (ret_state)
981 *ret_state = state;
982 if (ret_ptr)
983 *ret_ptr = (char *)ptr;
984 return NULL;
985
986 http_msg_invalid:
987 /* invalid message */
988 if (ret_state)
989 *ret_state = HTTP_MSG_ERROR;
990 return NULL;
991}
992
993
994/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100995 * This function parses a request line between <ptr> and <end>, starting with
996 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
997 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
998 * will give undefined results.
999 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1000 * and that msg->sol points to the beginning of the request.
1001 * If a complete line is found (which implies that at least one CR or LF is
1002 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1003 * returned indicating an incomplete line (which does not mean that parts have
1004 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1005 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1006 * upon next call.
1007 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001008 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001009 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1010 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001011 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001012 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001013const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 const char *ptr, const char *end,
1015 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001016{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001017 __label__
1018 http_msg_rqmeth,
1019 http_msg_rqmeth_sp,
1020 http_msg_rquri,
1021 http_msg_rquri_sp,
1022 http_msg_rqver,
1023 http_msg_rqline_eol,
1024 http_msg_ood, /* out of data */
1025 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001026
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001027 switch (state) {
1028 http_msg_rqmeth:
1029 case HTTP_MSG_RQMETH:
1030 if (likely(HTTP_IS_TOKEN(*ptr)))
1031 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001032
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001033 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001034 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001035 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1036 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001037
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001038 if (likely(HTTP_IS_CRLF(*ptr))) {
1039 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001040 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001041 http_msg_req09_uri:
1042 msg->sl.rq.u = ptr - msg_buf;
1043 http_msg_req09_uri_e:
1044 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1045 http_msg_req09_ver:
1046 msg->sl.rq.v = ptr - msg_buf;
1047 msg->sl.rq.v_l = 0;
1048 goto http_msg_rqline_eol;
1049 }
1050 goto http_msg_invalid;
1051
1052 http_msg_rqmeth_sp:
1053 case HTTP_MSG_RQMETH_SP:
1054 if (likely(!HTTP_IS_LWS(*ptr))) {
1055 msg->sl.rq.u = ptr - msg_buf;
1056 goto http_msg_rquri;
1057 }
1058 if (likely(HTTP_IS_SPHT(*ptr)))
1059 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1060 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1061 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001062
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001063 http_msg_rquri:
1064 case HTTP_MSG_RQURI:
1065 if (likely(!HTTP_IS_LWS(*ptr)))
1066 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 if (likely(HTTP_IS_SPHT(*ptr))) {
1069 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1070 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1071 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1074 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 http_msg_rquri_sp:
1077 case HTTP_MSG_RQURI_SP:
1078 if (likely(!HTTP_IS_LWS(*ptr))) {
1079 msg->sl.rq.v = ptr - msg_buf;
1080 goto http_msg_rqver;
1081 }
1082 if (likely(HTTP_IS_SPHT(*ptr)))
1083 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1084 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1085 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 http_msg_rqver:
1088 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001089 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001091
1092 if (likely(HTTP_IS_CRLF(*ptr))) {
1093 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1094 http_msg_rqline_eol:
1095 /* We have seen the end of line. Note that we do not
1096 * necessarily have the \n yet, but at least we know that we
1097 * have EITHER \r OR \n, otherwise the request would not be
1098 * complete. We can then record the request length and return
1099 * to the caller which will be able to register it.
1100 */
1101 msg->sl.rq.l = ptr - msg->sol;
1102 return ptr;
1103 }
1104
1105 /* neither an HTTP_VER token nor a CRLF */
1106 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001108#ifdef DEBUG_FULL
1109 default:
1110 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1111 exit(1);
1112#endif
1113 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 http_msg_ood:
1116 /* out of data */
1117 if (ret_state)
1118 *ret_state = state;
1119 if (ret_ptr)
1120 *ret_ptr = (char *)ptr;
1121 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001122
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001123 http_msg_invalid:
1124 /* invalid message */
1125 if (ret_state)
1126 *ret_state = HTTP_MSG_ERROR;
1127 return NULL;
1128}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001129
1130
Willy Tarreau8973c702007-01-21 23:58:29 +01001131/*
1132 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001133 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001134 * when data are missing and recalled at the exact same location with no
1135 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001136 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1137 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001139void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1140{
1141 __label__
1142 http_msg_rqbefore,
1143 http_msg_rqbefore_cr,
1144 http_msg_rqmeth,
1145 http_msg_rqline_end,
1146 http_msg_hdr_first,
1147 http_msg_hdr_name,
1148 http_msg_hdr_l1_sp,
1149 http_msg_hdr_l1_lf,
1150 http_msg_hdr_l1_lws,
1151 http_msg_hdr_val,
1152 http_msg_hdr_l2_lf,
1153 http_msg_hdr_l2_lws,
1154 http_msg_complete_header,
1155 http_msg_last_lf,
1156 http_msg_ood, /* out of data */
1157 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001158
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 int state; /* updated only when leaving the FSM */
1160 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001161
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001162 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001163 ptr = buf->lr;
1164 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001166 if (unlikely(ptr >= end))
1167 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001168
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001169 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 /*
1171 * First, states that are specific to the response only.
1172 * We check them first so that request and headers are
1173 * closer to each other (accessed more often).
1174 */
1175 http_msg_rpbefore:
1176 case HTTP_MSG_RPBEFORE:
1177 if (likely(HTTP_IS_TOKEN(*ptr))) {
1178 if (likely(ptr == buf->data)) {
1179 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001180 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 } else {
1182#if PARSE_PRESERVE_EMPTY_LINES
1183 /* only skip empty leading lines, don't remove them */
1184 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001185 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001186#else
1187 /* Remove empty leading lines, as recommended by
1188 * RFC2616. This takes a lot of time because we
1189 * must move all the buffer backwards, but this
1190 * is rarely needed. The method above will be
1191 * cleaner when we'll be able to start sending
1192 * the request from any place in the buffer.
1193 */
1194 buf->lr = ptr;
1195 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001196 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 msg->sol = buf->data;
1198 ptr = buf->data;
1199 end = buf->r;
1200#endif
1201 }
1202 hdr_idx_init(idx);
1203 state = HTTP_MSG_RPVER;
1204 goto http_msg_rpver;
1205 }
1206
1207 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1208 goto http_msg_invalid;
1209
1210 if (unlikely(*ptr == '\n'))
1211 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1212 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1213 /* stop here */
1214
1215 http_msg_rpbefore_cr:
1216 case HTTP_MSG_RPBEFORE_CR:
1217 EXPECT_LF_HERE(ptr, http_msg_invalid);
1218 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1219 /* stop here */
1220
1221 http_msg_rpver:
1222 case HTTP_MSG_RPVER:
1223 case HTTP_MSG_RPVER_SP:
1224 case HTTP_MSG_RPCODE:
1225 case HTTP_MSG_RPCODE_SP:
1226 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001227 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001228 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001229 if (unlikely(!ptr))
1230 return;
1231
1232 /* we have a full response and we know that we have either a CR
1233 * or an LF at <ptr>.
1234 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001235 //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 +01001236 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1237
1238 msg->sol = ptr;
1239 if (likely(*ptr == '\r'))
1240 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1241 goto http_msg_rpline_end;
1242
1243 http_msg_rpline_end:
1244 case HTTP_MSG_RPLINE_END:
1245 /* msg->sol must point to the first of CR or LF. */
1246 EXPECT_LF_HERE(ptr, http_msg_invalid);
1247 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1248 /* stop here */
1249
1250 /*
1251 * Second, states that are specific to the request only
1252 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 http_msg_rqbefore:
1254 case HTTP_MSG_RQBEFORE:
1255 if (likely(HTTP_IS_TOKEN(*ptr))) {
1256 if (likely(ptr == buf->data)) {
1257 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001258 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259 } else {
1260#if PARSE_PRESERVE_EMPTY_LINES
1261 /* only skip empty leading lines, don't remove them */
1262 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001263 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001264#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 /* Remove empty leading lines, as recommended by
1266 * RFC2616. This takes a lot of time because we
1267 * must move all the buffer backwards, but this
1268 * is rarely needed. The method above will be
1269 * cleaner when we'll be able to start sending
1270 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001271 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001272 buf->lr = ptr;
1273 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001274 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 msg->sol = buf->data;
1276 ptr = buf->data;
1277 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001278#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001280 /* we will need this when keep-alive will be supported
1281 hdr_idx_init(idx);
1282 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001283 state = HTTP_MSG_RQMETH;
1284 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1288 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 if (unlikely(*ptr == '\n'))
1291 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1292 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 http_msg_rqbefore_cr:
1296 case HTTP_MSG_RQBEFORE_CR:
1297 EXPECT_LF_HERE(ptr, http_msg_invalid);
1298 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001300
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 http_msg_rqmeth:
1302 case HTTP_MSG_RQMETH:
1303 case HTTP_MSG_RQMETH_SP:
1304 case HTTP_MSG_RQURI:
1305 case HTTP_MSG_RQURI_SP:
1306 case HTTP_MSG_RQVER:
1307 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001308 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 if (unlikely(!ptr))
1310 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001311
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001312 /* we have a full request and we know that we have either a CR
1313 * or an LF at <ptr>.
1314 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001315 //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 +01001316 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 msg->sol = ptr;
1319 if (likely(*ptr == '\r'))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001322
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 http_msg_rqline_end:
1324 case HTTP_MSG_RQLINE_END:
1325 /* check for HTTP/0.9 request : no version information available.
1326 * msg->sol must point to the first of CR or LF.
1327 */
1328 if (unlikely(msg->sl.rq.v_l == 0))
1329 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 EXPECT_LF_HERE(ptr, http_msg_invalid);
1332 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001334
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 /*
1336 * Common states below
1337 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 http_msg_hdr_first:
1339 case HTTP_MSG_HDR_FIRST:
1340 msg->sol = ptr;
1341 if (likely(!HTTP_IS_CRLF(*ptr))) {
1342 goto http_msg_hdr_name;
1343 }
1344
1345 if (likely(*ptr == '\r'))
1346 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1347 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001348
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 http_msg_hdr_name:
1350 case HTTP_MSG_HDR_NAME:
1351 /* assumes msg->sol points to the first char */
1352 if (likely(HTTP_IS_TOKEN(*ptr)))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001355 if (likely(*ptr == ':')) {
1356 msg->col = ptr - buf->data;
1357 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1358 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001359
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362 http_msg_hdr_l1_sp:
1363 case HTTP_MSG_HDR_L1_SP:
1364 /* assumes msg->sol points to the first char and msg->col to the colon */
1365 if (likely(HTTP_IS_SPHT(*ptr)))
1366 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 /* header value can be basically anything except CR/LF */
1369 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 if (likely(!HTTP_IS_CRLF(*ptr))) {
1372 goto http_msg_hdr_val;
1373 }
1374
1375 if (likely(*ptr == '\r'))
1376 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1377 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001378
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379 http_msg_hdr_l1_lf:
1380 case HTTP_MSG_HDR_L1_LF:
1381 EXPECT_LF_HERE(ptr, http_msg_invalid);
1382 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 http_msg_hdr_l1_lws:
1385 case HTTP_MSG_HDR_L1_LWS:
1386 if (likely(HTTP_IS_SPHT(*ptr))) {
1387 /* replace HT,CR,LF with spaces */
1388 for (; buf->data+msg->sov < ptr; msg->sov++)
1389 buf->data[msg->sov] = ' ';
1390 goto http_msg_hdr_l1_sp;
1391 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001392 /* we had a header consisting only in spaces ! */
1393 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 goto http_msg_complete_header;
1395
1396 http_msg_hdr_val:
1397 case HTTP_MSG_HDR_VAL:
1398 /* assumes msg->sol points to the first char, msg->col to the
1399 * colon, and msg->sov points to the first character of the
1400 * value.
1401 */
1402 if (likely(!HTTP_IS_CRLF(*ptr)))
1403 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001404
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 msg->eol = ptr;
1406 /* Note: we could also copy eol into ->eoh so that we have the
1407 * real header end in case it ends with lots of LWS, but is this
1408 * really needed ?
1409 */
1410 if (likely(*ptr == '\r'))
1411 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1412 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 http_msg_hdr_l2_lf:
1415 case HTTP_MSG_HDR_L2_LF:
1416 EXPECT_LF_HERE(ptr, http_msg_invalid);
1417 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 http_msg_hdr_l2_lws:
1420 case HTTP_MSG_HDR_L2_LWS:
1421 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1422 /* LWS: replace HT,CR,LF with spaces */
1423 for (; msg->eol < ptr; msg->eol++)
1424 *msg->eol = ' ';
1425 goto http_msg_hdr_val;
1426 }
1427 http_msg_complete_header:
1428 /*
1429 * It was a new header, so the last one is finished.
1430 * Assumes msg->sol points to the first char, msg->col to the
1431 * colon, msg->sov points to the first character of the value
1432 * and msg->eol to the first CR or LF so we know how the line
1433 * ends. We insert last header into the index.
1434 */
1435 /*
1436 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1437 write(2, msg->sol, msg->eol-msg->sol);
1438 fprintf(stderr,"\n");
1439 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1442 idx, idx->tail) < 0))
1443 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001444
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 msg->sol = ptr;
1446 if (likely(!HTTP_IS_CRLF(*ptr))) {
1447 goto http_msg_hdr_name;
1448 }
1449
1450 if (likely(*ptr == '\r'))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1452 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 http_msg_last_lf:
1455 case HTTP_MSG_LAST_LF:
1456 /* Assumes msg->sol points to the first of either CR or LF */
1457 EXPECT_LF_HERE(ptr, http_msg_invalid);
1458 ptr++;
1459 buf->lr = ptr;
1460 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001461 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 return;
1463#ifdef DEBUG_FULL
1464 default:
1465 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1466 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001467#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 }
1469 http_msg_ood:
1470 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001471 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 buf->lr = ptr;
1473 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 http_msg_invalid:
1476 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001477 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 return;
1479}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481/*
1482 * manages the client FSM and its socket. BTW, it also tries to handle the
1483 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1484 * 0 else.
1485 */
1486int process_cli(struct session *t)
1487{
1488 int s = t->srv_state;
1489 int c = t->cli_state;
1490 struct buffer *req = t->req;
1491 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1494 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001495 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 req->rex.tv_sec, req->rex.tv_usec,
1497 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001498
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 if (c == CL_STHEADERS) {
1500 /*
1501 * Now parse the partial (or complete) lines.
1502 * We will check the request syntax, and also join multi-line
1503 * headers. An index of all the lines will be elaborated while
1504 * parsing.
1505 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001506 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001509 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 * req->data + req->eoh = end of processed headers / start of current one
1511 * req->data + req->eol = end of current header or line (LF or CRLF)
1512 * req->lr = first non-visited byte
1513 * req->r = end of data
1514 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001517 struct http_txn *txn = &t->txn;
1518 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001522 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 /* 1: we might have to print this header in debug mode */
1525 if (unlikely((global.mode & MODE_DEBUG) &&
1526 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001527 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001529
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001530 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 eol = sol + msg->sl.rq.l;
1532 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001533
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001534 sol += hdr_idx_first_pos(&txn->hdr_idx);
1535 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001538 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001540 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1541 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 }
1543 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001544
Willy Tarreau58f10d72006-12-04 02:26:12 +01001545
1546 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548 * If not so, we check the FD and buffer states before leaving.
1549 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1551 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001552 *
1553 */
1554
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001555 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 /*
1557 * First, let's catch bad requests.
1558 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001559 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001561
1562 /* 1: Since we are in header mode, if there's no space
1563 * left for headers, we won't be able to free more
1564 * later, so the session will never terminate. We
1565 * must terminate it now.
1566 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (unlikely(req->l >= req->rlim - req->data)) {
1568 /* FIXME: check if URI is set and return Status
1569 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570 */
Willy Tarreau06619262006-12-17 08:37:22 +01001571 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001572 }
1573
1574 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1576 /* read error, or last read : give up. */
Willy Tarreaufa645582007-06-03 15:59:52 +02001577 buffer_shutr(req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001578 fd_delete(t->cli_fd);
1579 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001580 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001581 if (!(t->flags & SN_ERR_MASK))
1582 t->flags |= SN_ERR_CLICL;
1583 if (!(t->flags & SN_FINST_MASK))
1584 t->flags |= SN_FINST_R;
1585 return 1;
1586 }
1587
1588 /* 3: has the read timeout expired ? */
Willy Tarreau036fae02008-01-06 13:24:40 +01001589 else if (unlikely(tv_isle(&req->rex, &now) ||
1590 tv_isle(&txn->exp, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001591 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001592 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001593 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001594 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595 if (!(t->flags & SN_ERR_MASK))
1596 t->flags |= SN_ERR_CLITO;
1597 if (!(t->flags & SN_FINST_MASK))
1598 t->flags |= SN_FINST_R;
1599 return 1;
1600 }
1601
1602 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001603 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001604 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001605 * full. We cannot loop here since stream_sock_read will disable it only if
1606 * req->l == rlim-data
1607 */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01001608 if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001609 tv_eternity(&req->rex);
1610 }
1611 return t->cli_state != CL_STHEADERS;
1612 }
1613
1614
1615 /****************************************************************
1616 * More interesting part now : we know that we have a complete *
1617 * request which at least looks like HTTP. We have an indicator *
1618 * of each header's length, so we can parse them quickly. *
1619 ****************************************************************/
1620
Willy Tarreau9cdde232007-05-02 20:58:19 +02001621 /* ensure we keep this pointer to the beginning of the message */
1622 msg->sol = req->data + msg->som;
1623
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001624 /*
1625 * 1: identify the method
1626 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001627 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001628
1629 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001631 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001633 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 if (unlikely((t->fe->monitor_uri_len != 0) &&
1635 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1636 !memcmp(&req->data[msg->sl.rq.u],
1637 t->fe->monitor_uri,
1638 t->fe->monitor_uri_len))) {
1639 /*
1640 * We have found the monitor URI
1641 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001642 struct acl_cond *cond;
1643 cur_proxy = t->fe;
1644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001646
1647 /* Check if we want to fail this monitor request or not */
1648 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1649 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
1650 if (cond->pol == ACL_COND_UNLESS)
1651 ret = !ret;
1652
1653 if (ret) {
1654 /* we fail this request, let's return 503 service unavail */
1655 txn->status = 503;
1656 client_retnclose(t, error_message(t, HTTP_ERR_503));
1657 goto return_prx_cond;
1658 }
1659 }
1660
1661 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001662 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 client_retnclose(t, &http_200_chunk);
1664 goto return_prx_cond;
1665 }
1666
1667 /*
1668 * 3: Maybe we have to copy the original REQURI for the logs ?
1669 * Note: we cannot log anymore if the request has been
1670 * classified as invalid.
1671 */
1672 if (unlikely(t->logs.logwait & LW_REQ)) {
1673 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001674 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 int urilen = msg->sl.rq.l;
1676
1677 if (urilen >= REQURI_LEN)
1678 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001679 memcpy(txn->uri, &req->data[msg->som], urilen);
1680 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681
1682 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001683 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 } else {
1685 Alert("HTTP logging : out of memory.\n");
1686 }
1687 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001688
Willy Tarreau06619262006-12-17 08:37:22 +01001689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1691 if (unlikely(msg->sl.rq.v_l == 0)) {
1692 int delta;
1693 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001694 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001695 cur_end = msg->sol + msg->sl.rq.l;
1696 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 if (msg->sl.rq.u_l == 0) {
1699 /* if no URI was set, add "/" */
1700 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1701 cur_end += delta;
1702 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001703 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 /* add HTTP version */
1705 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1706 msg->eoh += delta;
1707 cur_end += delta;
1708 cur_end = (char *)http_parse_reqline(msg, req->data,
1709 HTTP_MSG_RQMETH,
1710 msg->sol, cur_end + 1,
1711 NULL, NULL);
1712 if (unlikely(!cur_end))
1713 goto return_bad_req;
1714
1715 /* we have a full HTTP/1.0 request now and we know that
1716 * we have either a CR or an LF at <ptr>.
1717 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001718 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001719 }
1720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721
1722 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001723 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001724 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001725 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001726
1727 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001729 * As opposed to version 1.2, now they will be evaluated in the
1730 * filters order and not in the header order. This means that
1731 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001732 *
1733 * We can now check whether we want to switch to another
1734 * backend, in which case we will re-check the backend's
1735 * filters and various options. In order to support 3-level
1736 * switching, here's how we should proceed :
1737 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001738 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001739 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001740 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001741 * There cannot be any switch from there, so ->be cannot be
1742 * changed anymore.
1743 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001744 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001745 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001746 * The response path will be able to apply either ->be, or
1747 * ->be then ->fe filters in order to match the reverse of
1748 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001749 */
1750
Willy Tarreau06619262006-12-17 08:37:22 +01001751 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001752 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001753 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001754 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001755
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001756 /* first check whether we have some ACLs set to block this request */
1757 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001758 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001759 if (cond->pol == ACL_COND_UNLESS)
1760 ret = !ret;
1761
1762 if (ret) {
1763 txn->status = 403;
1764 /* let's log the request time */
1765 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1766 client_retnclose(t, error_message(t, HTTP_ERR_403));
1767 goto return_prx_cond;
1768 }
1769 }
1770
Willy Tarreau06619262006-12-17 08:37:22 +01001771 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001772 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1774 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001775 }
1776
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001777 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1778 /* to ensure correct connection accounting on
1779 * the backend, we count the connection for the
1780 * one managing the queue.
1781 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001782 t->be->beconn++;
1783 if (t->be->beconn > t->be->beconn_max)
1784 t->be->beconn_max = t->be->beconn;
1785 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001786 t->flags |= SN_BE_ASSIGNED;
1787 }
1788
Willy Tarreau06619262006-12-17 08:37:22 +01001789 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001790 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001791 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001792 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001793 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001794 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001795 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001796 goto return_prx_cond;
1797 }
1798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01001800 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 !(t->flags & SN_CONN_CLOSED)) {
1802 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001803 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001805
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001806 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 old_idx = 0;
1808
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001809 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1810 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 cur_ptr = cur_next;
1812 cur_end = cur_ptr + cur_hdr->len;
1813 cur_next = cur_end + cur_hdr->cr + 1;
1814
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001815 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1816 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 /* 3 possibilities :
1818 * - we have already set Connection: close,
1819 * so we remove this line.
1820 * - we have not yet set Connection: close,
1821 * but this line indicates close. We leave
1822 * it untouched and set the flag.
1823 * - we have not yet set Connection: close,
1824 * and this line indicates non-close. We
1825 * replace it.
1826 */
1827 if (t->flags & SN_CONN_CLOSED) {
1828 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001829 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001831 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1832 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 cur_hdr->len = 0;
1834 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001835 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1836 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1837 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 cur_next += delta;
1839 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001840 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 }
1842 t->flags |= SN_CONN_CLOSED;
1843 }
1844 }
1845 old_idx = cur_idx;
1846 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001847 }
1848 /* add request headers from the rule sets in the same order */
1849 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1850 if (unlikely(http_header_add_tail(req,
1851 &txn->req,
1852 &txn->hdr_idx,
1853 rule_set->req_add[cur_idx])) < 0)
1854 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001855 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001856
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001858 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001859 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001860 /* we have to check the URI and auth for this request */
1861 if (stats_check_uri_auth(t, rule_set))
1862 return 1;
1863 }
1864
Willy Tarreau55ea7572007-06-17 19:56:27 +02001865 /* now check whether we have some switching rules for this request */
1866 if (!(t->flags & SN_BE_ASSIGNED)) {
1867 struct switching_rule *rule;
1868
1869 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
1870 int ret;
1871
1872 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
1873 if (cond->pol == ACL_COND_UNLESS)
1874 ret = !ret;
1875
1876 if (ret) {
1877 t->be = rule->be.backend;
1878 t->be->beconn++;
1879 if (t->be->beconn > t->be->beconn_max)
1880 t->be->beconn_max = t->be->beconn;
1881 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001882
1883 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01001884 t->rep->rto = t->req->wto = t->be->timeout.server;
1885 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001886 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02001887 t->flags |= SN_BE_ASSIGNED;
1888 break;
1889 }
1890 }
1891 }
1892
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001893 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1894 /* No backend was set, but there was a default
1895 * backend set in the frontend, so we use it and
1896 * loop again.
1897 */
1898 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001899 t->be->beconn++;
1900 if (t->be->beconn > t->be->beconn_max)
1901 t->be->beconn_max = t->be->beconn;
1902 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001903
1904 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01001905 t->rep->rto = t->req->wto = t->be->timeout.server;
1906 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001907 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001908 t->flags |= SN_BE_ASSIGNED;
1909 }
1910 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001911
Willy Tarreau58f10d72006-12-04 02:26:12 +01001912
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001913 if (!(t->flags & SN_BE_ASSIGNED)) {
1914 /* To ensure correct connection accounting on
1915 * the backend, we count the connection for the
1916 * one managing the queue.
1917 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001918 t->be->beconn++;
1919 if (t->be->beconn > t->be->beconn_max)
1920 t->be->beconn_max = t->be->beconn;
1921 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001922 t->flags |= SN_BE_ASSIGNED;
1923 }
1924
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001925 /*
1926 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001927 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001928 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001929 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001930 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001931
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001932 /*
1933 * If HTTP PROXY is set we simply get remote server address
1934 * parsing incoming request.
1935 */
1936 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
1937 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
1938 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001939
Willy Tarreau2a324282006-12-05 00:05:46 +01001940 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001941 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001942 * so let's do the same now.
1943 */
1944
1945 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001946 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001947 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001948 }
1949
1950
1951 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001952 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001953 * Note that doing so might move headers in the request, but
1954 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001955 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001956 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01001957 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
1958 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001959 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001960
Willy Tarreau58f10d72006-12-04 02:26:12 +01001961
Willy Tarreau2a324282006-12-05 00:05:46 +01001962 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001963 * 9: add X-Forwarded-For if either the frontend or the backend
1964 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001965 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001966 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001967 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001968 /* Add an X-Forwarded-For header unless the source IP is
1969 * in the 'except' network range.
1970 */
1971 if ((!t->fe->except_mask.s_addr ||
1972 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1973 != t->fe->except_net.s_addr) &&
1974 (!t->be->except_mask.s_addr ||
1975 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1976 != t->be->except_net.s_addr)) {
1977 int len;
1978 unsigned char *pn;
1979 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001980
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001981 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1982 pn[0], pn[1], pn[2], pn[3]);
1983
1984 if (unlikely(http_header_add_tail2(req, &txn->req,
1985 &txn->hdr_idx, trash, len)) < 0)
1986 goto return_bad_req;
1987 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001988 }
1989 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001990 /* FIXME: for the sake of completeness, we should also support
1991 * 'except' here, although it is mostly useless in this case.
1992 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001993 int len;
1994 char pn[INET6_ADDRSTRLEN];
1995 inet_ntop(AF_INET6,
1996 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1997 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001998 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1999 if (unlikely(http_header_add_tail2(req, &txn->req,
2000 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002001 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002002 }
2003 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002004
Willy Tarreau2a324282006-12-05 00:05:46 +01002005 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002006 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002007 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002008 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002009 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002010 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002011 if ((unlikely(msg->sl.rq.v_l != 8) ||
2012 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2013 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002014 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002015 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002016 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002017 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002018
Willy Tarreau2a324282006-12-05 00:05:46 +01002019 /*************************************************************
2020 * OK, that's finished for the headers. We have done what we *
2021 * could. Let's switch to the DATA state. *
2022 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002023
Willy Tarreau2a324282006-12-05 00:05:46 +01002024 t->cli_state = CL_STDATA;
2025 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002026
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002027 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002028
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002029 if (!tv_isset(&t->fe->timeout.client) ||
2030 (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server))) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002031 /* If the client has no timeout, or if the server is not ready yet,
2032 * and we know for sure that it can expire, then it's cleaner to
2033 * disable the timeout on the client side so that too low values
2034 * cannot make the sessions abort too early.
2035 *
2036 * FIXME-20050705: the server needs a way to re-enable this time-out
2037 * when it switches its state, otherwise a client can stay connected
2038 * indefinitely. This now seems to be OK.
2039 */
2040 tv_eternity(&req->rex);
2041 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002042
Willy Tarreau1fa31262007-12-03 00:36:16 +01002043 /* When a connection is tarpitted, we use the tarpit timeout,
2044 * which may be the same as the connect timeout if unspecified.
2045 * If unset, then set it to zero because we really want it to
2046 * eventually expire.
Willy Tarreau2a324282006-12-05 00:05:46 +01002047 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002048 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002049 t->req->l = 0;
2050 /* flush the request so that we can drop the connection early
2051 * if the client closes first.
2052 */
Willy Tarreau1fa31262007-12-03 00:36:16 +01002053 if (!tv_add_ifset(&req->cex, &now, &t->be->timeout.tarpit))
Willy Tarreaud825eef2007-05-12 22:35:00 +02002054 req->cex = now;
Willy Tarreau2a324282006-12-05 00:05:46 +01002055 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002056
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002057 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01002058 goto process_data;
2059
2060 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002061 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002062 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01002063 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002064 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002065 return_prx_cond:
2066 if (!(t->flags & SN_ERR_MASK))
2067 t->flags |= SN_ERR_PRXCOND;
2068 if (!(t->flags & SN_FINST_MASK))
2069 t->flags |= SN_FINST_R;
2070 return 1;
2071
Willy Tarreaubaaee002006-06-26 02:48:02 +02002072 }
2073 else if (c == CL_STDATA) {
2074 process_data:
2075 /* FIXME: this error handling is partly buggy because we always report
2076 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
2077 * or HEADER phase. BTW, it's not logical to expire the client while
2078 * we're waiting for the server to connect.
2079 */
2080 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002081 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002082 buffer_shutr(req);
2083 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002084 fd_delete(t->cli_fd);
2085 t->cli_state = CL_STCLOSE;
2086 if (!(t->flags & SN_ERR_MASK))
2087 t->flags |= SN_ERR_CLICL;
2088 if (!(t->flags & SN_FINST_MASK)) {
2089 if (t->pend_pos)
2090 t->flags |= SN_FINST_Q;
2091 else if (s == SV_STCONN)
2092 t->flags |= SN_FINST_C;
2093 else
2094 t->flags |= SN_FINST_D;
2095 }
2096 return 1;
2097 }
2098 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002099 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002100 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002101 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002102 t->cli_state = CL_STSHUTR;
2103 return 1;
2104 }
2105 /* last server read and buffer empty */
2106 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002107 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002108 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002109 shutdown(t->cli_fd, SHUT_WR);
2110 /* We must ensure that the read part is still alive when switching
2111 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002112 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002113 tv_add_ifset(&req->rex, &now, &t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002114 t->cli_state = CL_STSHUTW;
2115 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2116 return 1;
2117 }
2118 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002119 else if (tv_isle(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002120 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002121 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002122 t->cli_state = CL_STSHUTR;
2123 if (!(t->flags & SN_ERR_MASK))
2124 t->flags |= SN_ERR_CLITO;
2125 if (!(t->flags & SN_FINST_MASK)) {
2126 if (t->pend_pos)
2127 t->flags |= SN_FINST_Q;
2128 else if (s == SV_STCONN)
2129 t->flags |= SN_FINST_C;
2130 else
2131 t->flags |= SN_FINST_D;
2132 }
2133 return 1;
2134 }
2135 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002136 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002137 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002138 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002139 shutdown(t->cli_fd, SHUT_WR);
2140 /* We must ensure that the read part is still alive when switching
2141 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002142 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002143 tv_add_ifset(&req->rex, &now, &t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002144
2145 t->cli_state = CL_STSHUTW;
2146 if (!(t->flags & SN_ERR_MASK))
2147 t->flags |= SN_ERR_CLITO;
2148 if (!(t->flags & SN_FINST_MASK)) {
2149 if (t->pend_pos)
2150 t->flags |= SN_FINST_Q;
2151 else if (s == SV_STCONN)
2152 t->flags |= SN_FINST_C;
2153 else
2154 t->flags |= SN_FINST_D;
2155 }
2156 return 1;
2157 }
2158
2159 if (req->l >= req->rlim - req->data) {
2160 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002161 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002162 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002163 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002164 }
2165 } else {
2166 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002167 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002168 if (!tv_isset(&t->fe->timeout.client) ||
2169 (t->srv_state < SV_STDATA && tv_isset(&t->be->timeout.server)))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002170 /* If the client has no timeout, or if the server not ready yet, and we
2171 * know for sure that it can expire, then it's cleaner to disable the
2172 * timeout on the client side so that too low values cannot make the
2173 * sessions abort too early.
2174 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002175 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002176 else
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002177 tv_add(&req->rex, &now, &t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002178 }
2179 }
2180
2181 if ((rep->l == 0) ||
2182 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002183 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2184 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002185 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002186 }
2187 } else {
2188 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002189 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2190 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002191 if (tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002192 /* FIXME: to prevent the client from expiring read timeouts during writes,
2193 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002194 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002195 }
2196 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002197 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002198 }
2199 }
2200 return 0; /* other cases change nothing */
2201 }
2202 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002203 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002204 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002205 fd_delete(t->cli_fd);
2206 t->cli_state = CL_STCLOSE;
2207 if (!(t->flags & SN_ERR_MASK))
2208 t->flags |= SN_ERR_CLICL;
2209 if (!(t->flags & SN_FINST_MASK)) {
2210 if (t->pend_pos)
2211 t->flags |= SN_FINST_Q;
2212 else if (s == SV_STCONN)
2213 t->flags |= SN_FINST_C;
2214 else
2215 t->flags |= SN_FINST_D;
2216 }
2217 return 1;
2218 }
2219 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2220 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002221 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002222 fd_delete(t->cli_fd);
2223 t->cli_state = CL_STCLOSE;
2224 return 1;
2225 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002226 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002227 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002228 fd_delete(t->cli_fd);
2229 t->cli_state = CL_STCLOSE;
2230 if (!(t->flags & SN_ERR_MASK))
2231 t->flags |= SN_ERR_CLITO;
2232 if (!(t->flags & SN_FINST_MASK)) {
2233 if (t->pend_pos)
2234 t->flags |= SN_FINST_Q;
2235 else if (s == SV_STCONN)
2236 t->flags |= SN_FINST_C;
2237 else
2238 t->flags |= SN_FINST_D;
2239 }
2240 return 1;
2241 }
2242
2243 if (t->flags & SN_SELF_GEN) {
2244 produce_content(t);
2245 if (rep->l == 0) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002246 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002247 fd_delete(t->cli_fd);
2248 t->cli_state = CL_STCLOSE;
2249 return 1;
2250 }
2251 }
2252
2253 if ((rep->l == 0)
2254 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002255 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2256 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002257 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002258 }
2259 } else {
2260 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002261 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2262 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002263 if (!tv_add_ifset(&rep->wex, &now, &t->fe->timeout.client))
Willy Tarreaud7971282006-07-29 18:36:34 +02002264 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002265 }
2266 }
2267 return 0;
2268 }
2269 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002270 if (req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002271 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002272 fd_delete(t->cli_fd);
2273 t->cli_state = CL_STCLOSE;
2274 if (!(t->flags & SN_ERR_MASK))
2275 t->flags |= SN_ERR_CLICL;
2276 if (!(t->flags & SN_FINST_MASK)) {
2277 if (t->pend_pos)
2278 t->flags |= SN_FINST_Q;
2279 else if (s == SV_STCONN)
2280 t->flags |= SN_FINST_C;
2281 else
2282 t->flags |= SN_FINST_D;
2283 }
2284 return 1;
2285 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002286 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002287 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002288 fd_delete(t->cli_fd);
2289 t->cli_state = CL_STCLOSE;
2290 return 1;
2291 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002292 else if (tv_isle(&req->rex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002293 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002294 fd_delete(t->cli_fd);
2295 t->cli_state = CL_STCLOSE;
2296 if (!(t->flags & SN_ERR_MASK))
2297 t->flags |= SN_ERR_CLITO;
2298 if (!(t->flags & SN_FINST_MASK)) {
2299 if (t->pend_pos)
2300 t->flags |= SN_FINST_Q;
2301 else if (s == SV_STCONN)
2302 t->flags |= SN_FINST_C;
2303 else
2304 t->flags |= SN_FINST_D;
2305 }
2306 return 1;
2307 }
2308 else if (req->l >= req->rlim - req->data) {
2309 /* no room to read more data */
2310
2311 /* FIXME-20050705: is it possible for a client to maintain a session
2312 * after the timeout by sending more data after it receives a close ?
2313 */
2314
Willy Tarreau66319382007-04-08 17:17:37 +02002315 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002316 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002317 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002318 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2319 }
2320 } else {
2321 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002322 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002323 if (!tv_add_ifset(&req->rex, &now, &t->fe->timeout.client))
Willy Tarreaud7971282006-07-29 18:36:34 +02002324 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002325 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2326 }
2327 }
2328 return 0;
2329 }
2330 else { /* CL_STCLOSE: nothing to do */
2331 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2332 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002333 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002334 write(1, trash, len);
2335 }
2336 return 0;
2337 }
2338 return 0;
2339}
2340
2341
2342/*
2343 * manages the server FSM and its socket. It returns 1 if a state has changed
2344 * (and a resync may be needed), 0 else.
2345 */
2346int process_srv(struct session *t)
2347{
2348 int s = t->srv_state;
2349 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002350 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002351 struct buffer *req = t->req;
2352 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002353 int conn_err;
2354
2355#ifdef DEBUG_FULL
2356 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2357#endif
Willy Tarreauee991362007-05-14 14:37:50 +02002358
2359#if 0
2360 fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n",
2361 __FUNCTION__, __LINE__,
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002362 t->fe->timeout.client.tv_sec, t->fe->timeout.client.tv_usec,
2363 t->fe->timeout.connect.tv_sec, t->fe->timeout.connect.tv_usec,
2364 t->fe->timeout.server.tv_sec, t->fe->timeout.server.tv_usec);
Willy Tarreauee991362007-05-14 14:37:50 +02002365 fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n",
2366 __FUNCTION__, __LINE__,
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002367 t->be->timeout.client.tv_sec, t->be->timeout.client.tv_usec,
2368 t->be->timeout.connect.tv_sec, t->be->timeout.connect.tv_usec,
2369 t->be->timeout.server.tv_sec, t->be->timeout.server.tv_usec);
Willy Tarreauee991362007-05-14 14:37:50 +02002370
2371 fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n",
2372 __FUNCTION__, __LINE__,
2373 req->cto.tv_sec, req->cto.tv_usec,
2374 req->rto.tv_sec, req->rto.tv_usec,
2375 req->wto.tv_sec, req->wto.tv_usec);
2376
2377 fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n",
2378 __FUNCTION__, __LINE__,
2379 rep->cto.tv_sec, rep->cto.tv_usec,
2380 rep->rto.tv_sec, rep->rto.tv_usec,
2381 rep->wto.tv_sec, rep->wto.tv_usec);
2382#endif
2383
Willy Tarreaubaaee002006-06-26 02:48:02 +02002384 //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s,
Willy Tarreauf161a342007-04-08 16:59:42 +02002385 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2386 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002387 //);
2388 if (s == SV_STIDLE) {
2389 if (c == CL_STHEADERS)
2390 return 0; /* stay in idle, waiting for data to reach the client side */
2391 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2392 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002393 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002394 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002395 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002396 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002397 /* note that this must not return any error because it would be able to
2398 * overwrite the client_retnclose() output.
2399 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002400 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002401 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002402 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002403 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 +02002404
2405 return 1;
2406 }
2407 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002408 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002409 /* This connection is being tarpitted. The CLIENT side has
2410 * already set the connect expiration date to the right
2411 * timeout. We just have to check that it has not expired.
2412 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002413 if (!tv_isle(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002414 return 0;
2415
2416 /* We will set the queue timer to the time spent, just for
2417 * logging purposes. We fake a 500 server error, so that the
2418 * attacker will not suspect his connection has been tarpitted.
2419 * It will not cause trouble to the logs because we can exclude
2420 * the tarpitted connections by filtering on the 'PT' status flags.
2421 */
2422 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002423 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002424 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002425 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002426 return 1;
2427 }
2428
Willy Tarreaubaaee002006-06-26 02:48:02 +02002429 /* Right now, we will need to create a connection to the server.
2430 * We might already have tried, and got a connection pending, in
2431 * which case we will not do anything till it's pending. It's up
2432 * to any other session to release it and wake us up again.
2433 */
2434 if (t->pend_pos) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002435 if (!tv_isle(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002436 return 0;
2437 else {
2438 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002439 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002440 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002441 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002442 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002443 if (t->srv)
2444 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002445 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002446 return 1;
2447 }
2448 }
2449
2450 do {
2451 /* first, get a connection */
2452 if (srv_redispatch_connect(t))
2453 return t->srv_state != SV_STIDLE;
2454
2455 /* try to (re-)connect to the server, and fail if we expire the
2456 * number of retries.
2457 */
2458 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002459 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002460 return t->srv_state != SV_STIDLE;
2461 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002462 } while (1);
2463 }
2464 }
2465 else if (s == SV_STCONN) { /* connection in progress */
2466 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2467 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002468 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2469 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002470 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002471 fd_delete(t->srv_fd);
2472 if (t->srv)
2473 t->srv->cur_sess--;
2474
2475 /* note that this must not return any error because it would be able to
2476 * overwrite the client_retnclose() output.
2477 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002478 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002479 return 1;
2480 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002481 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002482 //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 +02002483 return 0; /* nothing changed */
2484 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002485 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002486 /* timeout, asynchronous connect error or first write error */
2487 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2488
2489 fd_delete(t->srv_fd);
2490 if (t->srv)
2491 t->srv->cur_sess--;
2492
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002493 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002494 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2495 else
2496 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2497
2498 /* ensure that we have enough retries left */
2499 if (srv_count_retry_down(t, conn_err))
2500 return 1;
2501
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002502 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002503 /* We're on our last chance, and the REDISP option was specified.
2504 * We will ignore cookie and force to balance or use the dispatcher.
2505 */
2506 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002507 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002508 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002509
2510 if (t->srv)
2511 t->srv->failed_conns++;
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002512 t->be->redispatches++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002513
2514 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2515 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002516 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002517
2518 /* first, get a connection */
2519 if (srv_redispatch_connect(t))
2520 return t->srv_state != SV_STIDLE;
2521 }
2522
Willy Tarreaubaaee002006-06-26 02:48:02 +02002523 do {
2524 /* Now we will try to either reconnect to the same server or
2525 * connect to another server. If the connection gets queued
2526 * because all servers are saturated, then we will go back to
2527 * the SV_STIDLE state.
2528 */
2529 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002530 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002531 return t->srv_state != SV_STCONN;
2532 }
2533
2534 /* we need to redispatch the connection to another server */
2535 if (srv_redispatch_connect(t))
2536 return t->srv_state != SV_STCONN;
2537 } while (1);
2538 }
2539 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002540 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002541
2542 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2543 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002544 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002545 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002546 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002547 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002548 if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002549 /* FIXME: to prevent the server from expiring read timeouts during writes,
2550 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002551 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002552 }
2553 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002554 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002555 }
2556
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002557 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002558 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002559 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02002560 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002561
2562 t->srv_state = SV_STDATA;
2563 if (t->srv)
2564 t->srv->cum_sess++;
2565 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2566
2567 /* if the user wants to log as soon as possible, without counting
2568 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002569 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002570 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002571 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002572 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002573#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002574 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002575 /* TCP splicing supported by both FE and BE */
2576 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2577 }
2578#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002579 }
2580 else {
2581 t->srv_state = SV_STHEADERS;
2582 if (t->srv)
2583 t->srv->cum_sess++;
2584 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002585 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2586 /* reset hdr_idx which was already initialized by the request.
2587 * right now, the http parser does it.
2588 * hdr_idx_init(&t->txn.hdr_idx);
2589 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002590 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002591 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002592 return 1;
2593 }
2594 }
2595 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002596 /*
2597 * Now parse the partial (or complete) lines.
2598 * We will check the response syntax, and also join multi-line
2599 * headers. An index of all the lines will be elaborated while
2600 * parsing.
2601 *
2602 * For the parsing, we use a 28 states FSM.
2603 *
2604 * Here is the information we currently have :
2605 * rep->data + req->som = beginning of response
2606 * rep->data + req->eoh = end of processed headers / start of current one
2607 * rep->data + req->eol = end of current header or line (LF or CRLF)
2608 * rep->lr = first non-visited byte
2609 * rep->r = end of data
2610 */
2611
2612 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002613 struct http_msg *msg = &txn->rsp;
2614 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002615
Willy Tarreaua15645d2007-03-18 16:22:39 +01002616 if (likely(rep->lr < rep->r))
2617 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002618
Willy Tarreaua15645d2007-03-18 16:22:39 +01002619 /* 1: we might have to print this header in debug mode */
2620 if (unlikely((global.mode & MODE_DEBUG) &&
2621 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2622 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2623 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002624
Willy Tarreaua15645d2007-03-18 16:22:39 +01002625 sol = rep->data + msg->som;
2626 eol = sol + msg->sl.rq.l;
2627 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002628
Willy Tarreaua15645d2007-03-18 16:22:39 +01002629 sol += hdr_idx_first_pos(&txn->hdr_idx);
2630 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002631
Willy Tarreaua15645d2007-03-18 16:22:39 +01002632 while (cur_idx) {
2633 eol = sol + txn->hdr_idx.v[cur_idx].len;
2634 debug_hdr("srvhdr", t, sol, eol);
2635 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2636 cur_idx = txn->hdr_idx.v[cur_idx].next;
2637 }
2638 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002639
Willy Tarreaubaaee002006-06-26 02:48:02 +02002640
Willy Tarreau66319382007-04-08 17:17:37 +02002641 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002642 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002643 * full. We cannot loop here since stream_sock_read will disable it only if
2644 * rep->l == rlim-data
2645 */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002646 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002647 tv_eternity(&rep->rex);
2648 }
2649
2650
2651 /*
2652 * Now we quickly check if we have found a full valid response.
2653 * If not so, we check the FD and buffer states before leaving.
2654 * A full response is indicated by the fact that we have seen
2655 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2656 * responses are checked first.
2657 *
2658 * Depending on whether the client is still there or not, we
2659 * may send an error response back or not. Note that normally
2660 * we should only check for HTTP status there, and check I/O
2661 * errors somewhere else.
2662 */
2663
2664 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2665
2666 /* Invalid response, or read error or write error */
2667 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2668 (req->flags & BF_WRITE_ERROR) ||
2669 (rep->flags & BF_READ_ERROR))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002670 buffer_shutr(rep);
2671 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002672 fd_delete(t->srv_fd);
2673 if (t->srv) {
2674 t->srv->cur_sess--;
2675 t->srv->failed_resp++;
2676 }
2677 t->be->failed_resp++;
2678 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002679 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002680 client_return(t, error_message(t, HTTP_ERR_502));
2681 if (!(t->flags & SN_ERR_MASK))
2682 t->flags |= SN_ERR_SRVCL;
2683 if (!(t->flags & SN_FINST_MASK))
2684 t->flags |= SN_FINST_H;
2685 /* We used to have a free connection slot. Since we'll never use it,
2686 * we have to inform the server that it may be used by another session.
2687 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002688 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002689 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002690
Willy Tarreaua15645d2007-03-18 16:22:39 +01002691 return 1;
2692 }
2693
2694 /* end of client write or end of server read.
2695 * since we are in header mode, if there's no space left for headers, we
2696 * won't be able to free more later, so the session will never terminate.
2697 */
2698 else if (unlikely(rep->flags & BF_READ_NULL ||
2699 c == CL_STSHUTW || c == CL_STCLOSE ||
2700 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002701 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002702 buffer_shutr(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002703 t->srv_state = SV_STSHUTR;
2704 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2705 return 1;
2706 }
2707
2708 /* read timeout : return a 504 to the client.
2709 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002710 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002711 tv_isle(&rep->rex, &now))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002712 buffer_shutr(rep);
2713 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002714 fd_delete(t->srv_fd);
2715 if (t->srv) {
2716 t->srv->cur_sess--;
2717 t->srv->failed_resp++;
2718 }
2719 t->be->failed_resp++;
2720 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002721 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002722 client_return(t, error_message(t, HTTP_ERR_504));
2723 if (!(t->flags & SN_ERR_MASK))
2724 t->flags |= SN_ERR_SRVTO;
2725 if (!(t->flags & SN_FINST_MASK))
2726 t->flags |= SN_FINST_H;
2727 /* We used to have a free connection slot. Since we'll never use it,
2728 * we have to inform the server that it may be used by another session.
2729 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002730 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002731 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002732 return 1;
2733 }
2734
2735 /* last client read and buffer empty */
2736 /* FIXME!!! here, we don't want to switch to SHUTW if the
2737 * client shuts read too early, because we may still have
2738 * some work to do on the headers.
2739 * The side-effect is that if the client completely closes its
2740 * connection during SV_STHEADER, the connection to the server
2741 * is kept until a response comes back or the timeout is reached.
2742 */
2743 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2744 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002745 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002746 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002747
2748 /* We must ensure that the read part is still
2749 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002750 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002751 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002752
2753 shutdown(t->srv_fd, SHUT_WR);
2754 t->srv_state = SV_STSHUTW;
2755 return 1;
2756 }
2757
2758 /* write timeout */
2759 /* FIXME!!! here, we don't want to switch to SHUTW if the
2760 * client shuts read too early, because we may still have
2761 * some work to do on the headers.
2762 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002763 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002764 tv_isle(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002765 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002766 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002767 shutdown(t->srv_fd, SHUT_WR);
2768 /* We must ensure that the read part is still alive
2769 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002770 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002771 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002772
2773 t->srv_state = SV_STSHUTW;
2774 if (!(t->flags & SN_ERR_MASK))
2775 t->flags |= SN_ERR_SRVTO;
2776 if (!(t->flags & SN_FINST_MASK))
2777 t->flags |= SN_FINST_H;
2778 return 1;
2779 }
2780
2781 /*
2782 * And now the non-error cases.
2783 */
2784
2785 /* Data remaining in the request buffer.
2786 * This happens during the first pass here, and during
2787 * long posts.
2788 */
2789 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002790 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2791 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002792 if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002793 /* FIXME: to prevent the server from expiring read timeouts during writes,
2794 * we refresh it. */
2795 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002796 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002797 else
2798 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002799 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002800 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002801
Willy Tarreaua15645d2007-03-18 16:22:39 +01002802 /* nothing left in the request buffer */
2803 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002804 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2805 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002806 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002807 }
2808 }
2809
2810 return t->srv_state != SV_STHEADERS;
2811 }
2812
2813
2814 /*****************************************************************
2815 * More interesting part now : we know that we have a complete *
2816 * response which at least looks like HTTP. We have an indicator *
2817 * of each header's length, so we can parse them quickly. *
2818 ****************************************************************/
2819
Willy Tarreau9cdde232007-05-02 20:58:19 +02002820 /* ensure we keep this pointer to the beginning of the message */
2821 msg->sol = rep->data + msg->som;
2822
Willy Tarreaua15645d2007-03-18 16:22:39 +01002823 /*
2824 * 1: get the status code and check for cacheability.
2825 */
2826
2827 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002828 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002829
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002830 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002831 case 200:
2832 case 203:
2833 case 206:
2834 case 300:
2835 case 301:
2836 case 410:
2837 /* RFC2616 @13.4:
2838 * "A response received with a status code of
2839 * 200, 203, 206, 300, 301 or 410 MAY be stored
2840 * by a cache (...) unless a cache-control
2841 * directive prohibits caching."
2842 *
2843 * RFC2616 @9.5: POST method :
2844 * "Responses to this method are not cacheable,
2845 * unless the response includes appropriate
2846 * Cache-Control or Expires header fields."
2847 */
2848 if (likely(txn->meth != HTTP_METH_POST) &&
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002849 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
Willy Tarreau3d300592007-03-18 18:34:41 +01002850 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002851 break;
2852 default:
2853 break;
2854 }
2855
2856 /*
2857 * 2: we may need to capture headers
2858 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002859 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002860 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002861 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002862
2863 /*
2864 * 3: we will have to evaluate the filters.
2865 * As opposed to version 1.2, now they will be evaluated in the
2866 * filters order and not in the header order. This means that
2867 * each filter has to be validated among all headers.
2868 *
2869 * Filters are tried with ->be first, then with ->fe if it is
2870 * different from ->be.
2871 */
2872
2873 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2874
2875 cur_proxy = t->be;
2876 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002877 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002878
2879 /* try headers filters */
2880 if (rule_set->rsp_exp != NULL) {
2881 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2882 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002883 if (t->srv) {
2884 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002885 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002886 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002887 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002888 return_srv_prx_502:
Willy Tarreaufa645582007-06-03 15:59:52 +02002889 buffer_shutr(rep);
2890 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002891 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002892 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002893 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002894 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895 if (!(t->flags & SN_ERR_MASK))
2896 t->flags |= SN_ERR_PRXCOND;
2897 if (!(t->flags & SN_FINST_MASK))
2898 t->flags |= SN_FINST_H;
2899 /* We used to have a free connection slot. Since we'll never use it,
2900 * we have to inform the server that it may be used by another session.
2901 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002902 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002903 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002904 return 1;
2905 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002906 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002907
Willy Tarreaua15645d2007-03-18 16:22:39 +01002908 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002909 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002910 if (t->srv) {
2911 t->srv->cur_sess--;
2912 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002913 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002914 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002915 goto return_srv_prx_502;
2916 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002917
Willy Tarreaua15645d2007-03-18 16:22:39 +01002918 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002919 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002920 !(t->flags & SN_CONN_CLOSED)) {
2921 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002922 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002923 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002924
Willy Tarreaua15645d2007-03-18 16:22:39 +01002925 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2926 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002927
Willy Tarreaua15645d2007-03-18 16:22:39 +01002928 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2929 cur_hdr = &txn->hdr_idx.v[cur_idx];
2930 cur_ptr = cur_next;
2931 cur_end = cur_ptr + cur_hdr->len;
2932 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002933
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002934 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2935 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002936 /* 3 possibilities :
2937 * - we have already set Connection: close,
2938 * so we remove this line.
2939 * - we have not yet set Connection: close,
2940 * but this line indicates close. We leave
2941 * it untouched and set the flag.
2942 * - we have not yet set Connection: close,
2943 * and this line indicates non-close. We
2944 * replace it.
2945 */
2946 if (t->flags & SN_CONN_CLOSED) {
2947 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2948 txn->rsp.eoh += delta;
2949 cur_next += delta;
2950 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2951 txn->hdr_idx.used--;
2952 cur_hdr->len = 0;
2953 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002954 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2955 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2956 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002957 cur_next += delta;
2958 cur_hdr->len += delta;
2959 txn->rsp.eoh += delta;
2960 }
2961 t->flags |= SN_CONN_CLOSED;
2962 }
2963 }
2964 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002965 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002966 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002967
Willy Tarreaua15645d2007-03-18 16:22:39 +01002968 /* add response headers from the rule sets in the same order */
2969 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002970 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2971 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002972 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002973 }
2974
Willy Tarreaua15645d2007-03-18 16:22:39 +01002975 /* check whether we're already working on the frontend */
2976 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002977 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002978 cur_proxy = t->fe;
2979 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002980
Willy Tarreaua15645d2007-03-18 16:22:39 +01002981 /*
2982 * 4: check for server cookie.
2983 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002984 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2985 || (t->be->options & PR_O_CHK_CACHE))
2986 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002987
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002988
Willy Tarreaua15645d2007-03-18 16:22:39 +01002989 /*
Willy Tarreau396d2c62007-11-04 19:30:00 +01002990 * 5: check for cache-control or pragma headers if required.
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002991 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002992 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2993 check_response_for_cacheability(t, rep);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002994
2995 /*
2996 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01002997 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002998 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2999 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003000 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003001
Willy Tarreaua15645d2007-03-18 16:22:39 +01003002 /* the server is known, it's not the one the client requested, we have to
3003 * insert a set-cookie here, except if we want to insert only on POST
3004 * requests and this one isn't. Note that servers which don't have cookies
3005 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003006 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003007 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003008 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003009 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003010
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003011 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3012 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003013 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01003014 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003015
Willy Tarreaua15645d2007-03-18 16:22:39 +01003016 /* Here, we will tell an eventual cache on the client side that we don't
3017 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3018 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3019 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3020 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003021 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
3022
3023 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3024
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003025 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3026 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003027 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003028 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003029 }
3030
3031
3032 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003033 * 7: check if result will be cacheable with a cookie.
3034 * We'll block the response if security checks have caught
3035 * nasty things such as a cacheable cookie.
3036 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003037 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3038 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003039 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003040
Willy Tarreaua15645d2007-03-18 16:22:39 +01003041 /* we're in presence of a cacheable response containing
3042 * a set-cookie header. We'll block it as requested by
3043 * the 'checkcache' option, and send an alert.
3044 */
3045 if (t->srv) {
3046 t->srv->cur_sess--;
3047 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003048 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003049 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003050
Willy Tarreaua15645d2007-03-18 16:22:39 +01003051 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003052 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003053 send_log(t->be, LOG_ALERT,
3054 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003055 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003056 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003057 }
3058
Willy Tarreaua15645d2007-03-18 16:22:39 +01003059 /*
3060 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003061 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003062 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003063 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003064 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003065 if ((unlikely(msg->sl.st.v_l != 8) ||
3066 unlikely(req->data[msg->som + 7] != '0')) &&
3067 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003068 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003069 goto return_bad_resp;
3070 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003071 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072
Willy Tarreaubaaee002006-06-26 02:48:02 +02003073
Willy Tarreaua15645d2007-03-18 16:22:39 +01003074 /*************************************************************
3075 * OK, that's finished for the headers. We have done what we *
3076 * could. Let's switch to the DATA state. *
3077 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003078
Willy Tarreaua15645d2007-03-18 16:22:39 +01003079 t->srv_state = SV_STDATA;
3080 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003081 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003082
3083 /* client connection already closed or option 'forceclose' required :
3084 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003085 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003086 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003087 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003088 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003089 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003090
3091 /* We must ensure that the read part is still alive when switching
3092 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003093 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003094 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003095
Willy Tarreaua15645d2007-03-18 16:22:39 +01003096 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003097 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003098 }
3099
Willy Tarreaua15645d2007-03-18 16:22:39 +01003100#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003101 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003102 /* TCP splicing supported by both FE and BE */
3103 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003104 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003105#endif
3106 /* if the user wants to log as soon as possible, without counting
3107 bytes from the server, then this is the right moment. */
3108 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3109 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01003110 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003111 if (t->fe->to_log & LW_REQ)
3112 http_sess_log(t);
3113 else
3114 tcp_sess_log(t);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01003115 t->logs.bytes_in = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003116 }
3117
Willy Tarreaua15645d2007-03-18 16:22:39 +01003118 /* Note: we must not try to cheat by jumping directly to DATA,
3119 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003120 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003121
3122 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003123 }
3124 else if (s == SV_STDATA) {
3125 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003126 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02003127 buffer_shutr(rep);
3128 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003129 fd_delete(t->srv_fd);
3130 if (t->srv) {
3131 t->srv->cur_sess--;
3132 t->srv->failed_resp++;
3133 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003134 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003135 t->srv_state = SV_STCLOSE;
3136 if (!(t->flags & SN_ERR_MASK))
3137 t->flags |= SN_ERR_SRVCL;
3138 if (!(t->flags & SN_FINST_MASK))
3139 t->flags |= SN_FINST_D;
3140 /* We used to have a free connection slot. Since we'll never use it,
3141 * we have to inform the server that it may be used by another session.
3142 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003143 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003144 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003145
3146 return 1;
3147 }
3148 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003149 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003150 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003151 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003152 t->srv_state = SV_STSHUTR;
3153 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3154 return 1;
3155 }
3156 /* end of client read and no more data to send */
3157 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003158 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003159 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003160 shutdown(t->srv_fd, SHUT_WR);
3161 /* We must ensure that the read part is still alive when switching
3162 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003163 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003164 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003165
3166 t->srv_state = SV_STSHUTW;
3167 return 1;
3168 }
3169 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003170 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003171 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003172 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003173 t->srv_state = SV_STSHUTR;
3174 if (!(t->flags & SN_ERR_MASK))
3175 t->flags |= SN_ERR_SRVTO;
3176 if (!(t->flags & SN_FINST_MASK))
3177 t->flags |= SN_FINST_D;
3178 return 1;
3179 }
3180 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003181 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003182 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003183 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003184 shutdown(t->srv_fd, SHUT_WR);
3185 /* We must ensure that the read part is still alive when switching
3186 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003187 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003188 tv_add_ifset(&rep->rex, &now, &t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003189 t->srv_state = SV_STSHUTW;
3190 if (!(t->flags & SN_ERR_MASK))
3191 t->flags |= SN_ERR_SRVTO;
3192 if (!(t->flags & SN_FINST_MASK))
3193 t->flags |= SN_FINST_D;
3194 return 1;
3195 }
3196
3197 /* recompute request time-outs */
3198 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003199 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3200 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003201 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003202 }
3203 }
3204 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003205 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3206 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003207 if (tv_add_ifset(&req->wex, &now, &t->be->timeout.server)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003208 /* FIXME: to prevent the server from expiring read timeouts during writes,
3209 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003210 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003211 }
3212 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003213 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003214 }
3215 }
3216
3217 /* recompute response time-outs */
3218 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003219 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003220 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003221 }
3222 }
3223 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003224 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003225 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02003226 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003227 }
3228 }
3229
3230 return 0; /* other cases change nothing */
3231 }
3232 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003233 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003234 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003235 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003236 fd_delete(t->srv_fd);
3237 if (t->srv) {
3238 t->srv->cur_sess--;
3239 t->srv->failed_resp++;
3240 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003241 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003242 //close(t->srv_fd);
3243 t->srv_state = SV_STCLOSE;
3244 if (!(t->flags & SN_ERR_MASK))
3245 t->flags |= SN_ERR_SRVCL;
3246 if (!(t->flags & SN_FINST_MASK))
3247 t->flags |= SN_FINST_D;
3248 /* We used to have a free connection slot. Since we'll never use it,
3249 * we have to inform the server that it may be used by another session.
3250 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003251 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003252 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003253
3254 return 1;
3255 }
3256 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003257 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003258 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003259 fd_delete(t->srv_fd);
3260 if (t->srv)
3261 t->srv->cur_sess--;
3262 //close(t->srv_fd);
3263 t->srv_state = SV_STCLOSE;
3264 /* We used to have a free connection slot. Since we'll never use it,
3265 * we have to inform the server that it may be used by another session.
3266 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003267 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003268 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003269
3270 return 1;
3271 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003272 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003273 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003274 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003275 fd_delete(t->srv_fd);
3276 if (t->srv)
3277 t->srv->cur_sess--;
3278 //close(t->srv_fd);
3279 t->srv_state = SV_STCLOSE;
3280 if (!(t->flags & SN_ERR_MASK))
3281 t->flags |= SN_ERR_SRVTO;
3282 if (!(t->flags & SN_FINST_MASK))
3283 t->flags |= SN_FINST_D;
3284 /* We used to have a free connection slot. Since we'll never use it,
3285 * we have to inform the server that it may be used by another session.
3286 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003287 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003288 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003289
3290 return 1;
3291 }
3292 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003293 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3294 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003295 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003296 }
3297 }
3298 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003299 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3300 /* restart writing */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003301 if (!tv_add_ifset(&req->wex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02003302 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003303 }
3304 }
3305 return 0;
3306 }
3307 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003308 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003309 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003310 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003311 fd_delete(t->srv_fd);
3312 if (t->srv) {
3313 t->srv->cur_sess--;
3314 t->srv->failed_resp++;
3315 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003316 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003317 //close(t->srv_fd);
3318 t->srv_state = SV_STCLOSE;
3319 if (!(t->flags & SN_ERR_MASK))
3320 t->flags |= SN_ERR_SRVCL;
3321 if (!(t->flags & SN_FINST_MASK))
3322 t->flags |= SN_FINST_D;
3323 /* We used to have a free connection slot. Since we'll never use it,
3324 * we have to inform the server that it may be used by another session.
3325 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003326 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003327 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003328
3329 return 1;
3330 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003331 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003332 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003333 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003334 fd_delete(t->srv_fd);
3335 if (t->srv)
3336 t->srv->cur_sess--;
3337 //close(t->srv_fd);
3338 t->srv_state = SV_STCLOSE;
3339 /* We used to have a free connection slot. Since we'll never use it,
3340 * we have to inform the server that it may be used by another session.
3341 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003342 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003343 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003344
3345 return 1;
3346 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003347 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003348 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003349 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003350 fd_delete(t->srv_fd);
3351 if (t->srv)
3352 t->srv->cur_sess--;
3353 //close(t->srv_fd);
3354 t->srv_state = SV_STCLOSE;
3355 if (!(t->flags & SN_ERR_MASK))
3356 t->flags |= SN_ERR_SRVTO;
3357 if (!(t->flags & SN_FINST_MASK))
3358 t->flags |= SN_FINST_D;
3359 /* We used to have a free connection slot. Since we'll never use it,
3360 * we have to inform the server that it may be used by another session.
3361 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003362 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003363 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003364
3365 return 1;
3366 }
3367 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003368 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003369 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003370 }
3371 }
3372 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003373 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003374 if (!tv_add_ifset(&rep->rex, &now, &t->be->timeout.server))
Willy Tarreaud7971282006-07-29 18:36:34 +02003375 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003376 }
3377 }
3378 return 0;
3379 }
3380 else { /* SV_STCLOSE : nothing to do */
3381 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3382 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003383 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003384 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003385 write(1, trash, len);
3386 }
3387 return 0;
3388 }
3389 return 0;
3390}
3391
3392
3393/*
3394 * Produces data for the session <s> depending on its source. Expects to be
3395 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3396 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3397 * session, which it uses to keep on being called when there is free space in
3398 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3399 * if it changes the session state from CL_STSHUTR, otherwise 0.
3400 */
3401int produce_content(struct session *s)
3402{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003403 if (s->data_source == DATA_SRC_NONE) {
3404 s->flags &= ~SN_SELF_GEN;
3405 return 1;
3406 }
3407 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003408 /* dump server statistics */
Willy Tarreau55bb8452007-10-17 18:44:57 +02003409 int ret = stats_dump_http(s, s->be->uri_auth,
3410 (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML);
Willy Tarreau91861262007-10-17 17:06:05 +02003411 if (ret >= 0)
3412 return ret;
3413 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003414 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003415
Willy Tarreau91861262007-10-17 17:06:05 +02003416 /* unknown data source or internal error */
3417 s->txn.status = 500;
3418 client_retnclose(s, error_message(s, HTTP_ERR_500));
3419 if (!(s->flags & SN_ERR_MASK))
3420 s->flags |= SN_ERR_PRXCOND;
3421 if (!(s->flags & SN_FINST_MASK))
3422 s->flags |= SN_FINST_R;
3423 s->flags &= ~SN_SELF_GEN;
3424 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003425}
3426
3427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003428/* Iterate the same filter through all request headers.
3429 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003430 * Since it can manage the switch to another backend, it updates the per-proxy
3431 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003432 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003433int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003434{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003435 char term;
3436 char *cur_ptr, *cur_end, *cur_next;
3437 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003438 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003439 struct hdr_idx_elem *cur_hdr;
3440 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003442 last_hdr = 0;
3443
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003444 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003445 old_idx = 0;
3446
3447 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003448 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003449 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003450 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003451 (exp->action == ACT_ALLOW ||
3452 exp->action == ACT_DENY ||
3453 exp->action == ACT_TARPIT))
3454 return 0;
3455
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003456 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003457 if (!cur_idx)
3458 break;
3459
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003460 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003461 cur_ptr = cur_next;
3462 cur_end = cur_ptr + cur_hdr->len;
3463 cur_next = cur_end + cur_hdr->cr + 1;
3464
3465 /* Now we have one header between cur_ptr and cur_end,
3466 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003467 */
3468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003469 /* The annoying part is that pattern matching needs
3470 * that we modify the contents to null-terminate all
3471 * strings before testing them.
3472 */
3473
3474 term = *cur_end;
3475 *cur_end = '\0';
3476
3477 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3478 switch (exp->action) {
3479 case ACT_SETBE:
3480 /* It is not possible to jump a second time.
3481 * FIXME: should we return an HTTP/500 here so that
3482 * the admin knows there's a problem ?
3483 */
3484 if (t->be != t->fe)
3485 break;
3486
3487 /* Swithing Proxy */
3488 t->be = (struct proxy *) exp->replace;
3489
3490 /* right now, the backend switch is not too much complicated
3491 * because we have associated req_cap and rsp_cap to the
3492 * frontend, and the beconn will be updated later.
3493 */
3494
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003495 t->rep->rto = t->req->wto = t->be->timeout.server;
3496 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003497 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003498 last_hdr = 1;
3499 break;
3500
3501 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003502 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003503 last_hdr = 1;
3504 break;
3505
3506 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003507 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003508 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003509 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003510 break;
3511
3512 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003513 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003514 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003515 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003516 break;
3517
3518 case ACT_REPLACE:
3519 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3520 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3521 /* FIXME: if the user adds a newline in the replacement, the
3522 * index will not be recalculated for now, and the new line
3523 * will not be counted as a new header.
3524 */
3525
3526 cur_end += delta;
3527 cur_next += delta;
3528 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003529 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003530 break;
3531
3532 case ACT_REMOVE:
3533 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3534 cur_next += delta;
3535
3536 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003537 txn->req.eoh += delta;
3538 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3539 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003540 cur_hdr->len = 0;
3541 cur_end = NULL; /* null-term has been rewritten */
3542 break;
3543
3544 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003545 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003546 if (cur_end)
3547 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003548
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003549 /* keep the link from this header to next one in case of later
3550 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003551 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003552 old_idx = cur_idx;
3553 }
3554 return 0;
3555}
3556
3557
3558/* Apply the filter to the request line.
3559 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3560 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003561 * Since it can manage the switch to another backend, it updates the per-proxy
3562 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003563 */
3564int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3565{
3566 char term;
3567 char *cur_ptr, *cur_end;
3568 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003569 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003570 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003571
Willy Tarreau58f10d72006-12-04 02:26:12 +01003572
Willy Tarreau3d300592007-03-18 18:34:41 +01003573 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003574 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003575 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003576 (exp->action == ACT_ALLOW ||
3577 exp->action == ACT_DENY ||
3578 exp->action == ACT_TARPIT))
3579 return 0;
3580 else if (exp->action == ACT_REMOVE)
3581 return 0;
3582
3583 done = 0;
3584
Willy Tarreau9cdde232007-05-02 20:58:19 +02003585 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003586 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003587
3588 /* Now we have the request line between cur_ptr and cur_end */
3589
3590 /* The annoying part is that pattern matching needs
3591 * that we modify the contents to null-terminate all
3592 * strings before testing them.
3593 */
3594
3595 term = *cur_end;
3596 *cur_end = '\0';
3597
3598 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3599 switch (exp->action) {
3600 case ACT_SETBE:
3601 /* It is not possible to jump a second time.
3602 * FIXME: should we return an HTTP/500 here so that
3603 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003604 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003605 if (t->be != t->fe)
3606 break;
3607
3608 /* Swithing Proxy */
3609 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003611 /* right now, the backend switch is not too much complicated
3612 * because we have associated req_cap and rsp_cap to the
3613 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003614 */
3615
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003616 t->rep->rto = t->req->wto = t->be->timeout.server;
3617 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003618 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003619 done = 1;
3620 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003621
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003622 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003623 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003624 done = 1;
3625 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003626
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003627 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003628 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003629 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003630 done = 1;
3631 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003633 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003634 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003635 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003636 done = 1;
3637 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003639 case ACT_REPLACE:
3640 *cur_end = term; /* restore the string terminator */
3641 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3642 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3643 /* FIXME: if the user adds a newline in the replacement, the
3644 * index will not be recalculated for now, and the new line
3645 * will not be counted as a new header.
3646 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003647
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003648 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003649 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003650
Willy Tarreau9cdde232007-05-02 20:58:19 +02003651 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003652 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003653 HTTP_MSG_RQMETH,
3654 cur_ptr, cur_end + 1,
3655 NULL, NULL);
3656 if (unlikely(!cur_end))
3657 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003659 /* we have a full request and we know that we have either a CR
3660 * or an LF at <ptr>.
3661 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003662 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3663 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003664 /* there is no point trying this regex on headers */
3665 return 1;
3666 }
3667 }
3668 *cur_end = term; /* restore the string terminator */
3669 return done;
3670}
Willy Tarreau97de6242006-12-27 17:18:38 +01003671
Willy Tarreau58f10d72006-12-04 02:26:12 +01003672
Willy Tarreau58f10d72006-12-04 02:26:12 +01003673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003674/*
3675 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3676 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003677 * unparsable request. Since it can manage the switch to another backend, it
3678 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003679 */
3680int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3681{
Willy Tarreau3d300592007-03-18 18:34:41 +01003682 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003683 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003684 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003685 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003687 /*
3688 * The interleaving of transformations and verdicts
3689 * makes it difficult to decide to continue or stop
3690 * the evaluation.
3691 */
3692
Willy Tarreau3d300592007-03-18 18:34:41 +01003693 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003694 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3695 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3696 exp = exp->next;
3697 continue;
3698 }
3699
3700 /* Apply the filter to the request line. */
3701 ret = apply_filter_to_req_line(t, req, exp);
3702 if (unlikely(ret < 0))
3703 return -1;
3704
3705 if (likely(ret == 0)) {
3706 /* The filter did not match the request, it can be
3707 * iterated through all headers.
3708 */
3709 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003710 }
3711 exp = exp->next;
3712 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003713 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003714}
3715
3716
Willy Tarreaua15645d2007-03-18 16:22:39 +01003717
Willy Tarreau58f10d72006-12-04 02:26:12 +01003718/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003719 * Manage client-side cookie. It can impact performance by about 2% so it is
3720 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003721 */
3722void manage_client_side_cookies(struct session *t, struct buffer *req)
3723{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003724 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003725 char *p1, *p2, *p3, *p4;
3726 char *del_colon, *del_cookie, *colon;
3727 int app_cookies;
3728
3729 appsess *asession_temp = NULL;
3730 appsess local_asession;
3731
3732 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003733 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003734
Willy Tarreau2a324282006-12-05 00:05:46 +01003735 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003736 * we start with the start line.
3737 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003738 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003739 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003740
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003741 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003742 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003743 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003744
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003745 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003746 cur_ptr = cur_next;
3747 cur_end = cur_ptr + cur_hdr->len;
3748 cur_next = cur_end + cur_hdr->cr + 1;
3749
3750 /* We have one full header between cur_ptr and cur_end, and the
3751 * next header starts at cur_next. We're only interested in
3752 * "Cookie:" headers.
3753 */
3754
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003755 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3756 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003757 old_idx = cur_idx;
3758 continue;
3759 }
3760
3761 /* Now look for cookies. Conforming to RFC2109, we have to support
3762 * attributes whose name begin with a '$', and associate them with
3763 * the right cookie, if we want to delete this cookie.
3764 * So there are 3 cases for each cookie read :
3765 * 1) it's a special attribute, beginning with a '$' : ignore it.
3766 * 2) it's a server id cookie that we *MAY* want to delete : save
3767 * some pointers on it (last semi-colon, beginning of cookie...)
3768 * 3) it's an application cookie : we *MAY* have to delete a previous
3769 * "special" cookie.
3770 * At the end of loop, if a "special" cookie remains, we may have to
3771 * remove it. If no application cookie persists in the header, we
3772 * *MUST* delete it
3773 */
3774
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003775 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003776
Willy Tarreau58f10d72006-12-04 02:26:12 +01003777 /* del_cookie == NULL => nothing to be deleted */
3778 del_colon = del_cookie = NULL;
3779 app_cookies = 0;
3780
3781 while (p1 < cur_end) {
3782 /* skip spaces and colons, but keep an eye on these ones */
3783 while (p1 < cur_end) {
3784 if (*p1 == ';' || *p1 == ',')
3785 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003786 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003787 break;
3788 p1++;
3789 }
3790
3791 if (p1 == cur_end)
3792 break;
3793
3794 /* p1 is at the beginning of the cookie name */
3795 p2 = p1;
3796 while (p2 < cur_end && *p2 != '=')
3797 p2++;
3798
3799 if (p2 == cur_end)
3800 break;
3801
3802 p3 = p2 + 1; /* skips the '=' sign */
3803 if (p3 == cur_end)
3804 break;
3805
3806 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003807 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003808 p4++;
3809
3810 /* here, we have the cookie name between p1 and p2,
3811 * and its value between p3 and p4.
3812 * we can process it :
3813 *
3814 * Cookie: NAME=VALUE;
3815 * | || || |
3816 * | || || +--> p4
3817 * | || |+-------> p3
3818 * | || +--------> p2
3819 * | |+------------> p1
3820 * | +-------------> colon
3821 * +--------------------> cur_ptr
3822 */
3823
3824 if (*p1 == '$') {
3825 /* skip this one */
3826 }
3827 else {
3828 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003829 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003830 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003831 (p4 - p1 >= t->fe->capture_namelen) &&
3832 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003833 int log_len = p4 - p1;
3834
Willy Tarreau086b3b42007-05-13 21:45:51 +02003835 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003836 Alert("HTTP logging : out of memory.\n");
3837 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003838 if (log_len > t->fe->capture_len)
3839 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003840 memcpy(txn->cli_cookie, p1, log_len);
3841 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003842 }
3843 }
3844
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003845 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3846 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003847 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003848 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003849 char *delim;
3850
3851 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3852 * have the server ID betweek p3 and delim, and the original cookie between
3853 * delim+1 and p4. Otherwise, delim==p4 :
3854 *
3855 * Cookie: NAME=SRV~VALUE;
3856 * | || || | |
3857 * | || || | +--> p4
3858 * | || || +--------> delim
3859 * | || |+-----------> p3
3860 * | || +------------> p2
3861 * | |+----------------> p1
3862 * | +-----------------> colon
3863 * +------------------------> cur_ptr
3864 */
3865
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003866 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003867 for (delim = p3; delim < p4; delim++)
3868 if (*delim == COOKIE_DELIM)
3869 break;
3870 }
3871 else
3872 delim = p4;
3873
3874
3875 /* Here, we'll look for the first running server which supports the cookie.
3876 * This allows to share a same cookie between several servers, for example
3877 * to dedicate backup servers to specific servers only.
3878 * However, to prevent clients from sticking to cookie-less backup server
3879 * when they have incidentely learned an empty cookie, we simply ignore
3880 * empty cookies and mark them as invalid.
3881 */
3882 if (delim == p3)
3883 srv = NULL;
3884
3885 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003886 if (srv->cookie && (srv->cklen == delim - p3) &&
3887 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003888 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003889 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003890 txn->flags &= ~TX_CK_MASK;
3891 txn->flags |= TX_CK_VALID;
3892 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003893 t->srv = srv;
3894 break;
3895 } else {
3896 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003897 txn->flags &= ~TX_CK_MASK;
3898 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003899 }
3900 }
3901 srv = srv->next;
3902 }
3903
Willy Tarreau3d300592007-03-18 18:34:41 +01003904 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003905 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003906 txn->flags &= ~TX_CK_MASK;
3907 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003908 }
3909
3910 /* depending on the cookie mode, we may have to either :
3911 * - delete the complete cookie if we're in insert+indirect mode, so that
3912 * the server never sees it ;
3913 * - remove the server id from the cookie value, and tag the cookie as an
3914 * application cookie so that it does not get accidentely removed later,
3915 * if we're in cookie prefix mode
3916 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003917 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003918 int delta; /* negative */
3919
3920 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3921 p4 += delta;
3922 cur_end += delta;
3923 cur_next += delta;
3924 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003925 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003926
3927 del_cookie = del_colon = NULL;
3928 app_cookies++; /* protect the header from deletion */
3929 }
3930 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003931 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003932 del_cookie = p1;
3933 del_colon = colon;
3934 }
3935 } else {
3936 /* now we know that we must keep this cookie since it's
3937 * not ours. But if we wanted to delete our cookie
3938 * earlier, we cannot remove the complete header, but we
3939 * can remove the previous block itself.
3940 */
3941 app_cookies++;
3942
3943 if (del_cookie != NULL) {
3944 int delta; /* negative */
3945
3946 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3947 p4 += delta;
3948 cur_end += delta;
3949 cur_next += delta;
3950 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003951 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003952 del_cookie = del_colon = NULL;
3953 }
3954 }
3955
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003956 if ((t->be->appsession_name != NULL) &&
3957 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003958 /* first, let's see if the cookie is our appcookie*/
3959
3960 /* Cool... it's the right one */
3961
3962 asession_temp = &local_asession;
3963
Willy Tarreau63963c62007-05-13 21:29:55 +02003964 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003965 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3966 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3967 return;
3968 }
3969
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003970 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3971 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003972 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003973
Willy Tarreau58f10d72006-12-04 02:26:12 +01003974 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003975 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3976 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003977 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003978 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003979 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003980 Alert("Not enough memory process_cli():asession:calloc().\n");
3981 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3982 return;
3983 }
3984
3985 asession_temp->sessid = local_asession.sessid;
3986 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02003987 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003988 } else {
3989 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003990 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003991 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003992 if (asession_temp->serverid == NULL) {
3993 Alert("Found Application Session without matching server.\n");
3994 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003995 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003996 while (srv) {
3997 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003998 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003999 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004000 txn->flags &= ~TX_CK_MASK;
4001 txn->flags |= TX_CK_VALID;
4002 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004003 t->srv = srv;
4004 break;
4005 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004006 txn->flags &= ~TX_CK_MASK;
4007 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004008 }
4009 }
4010 srv = srv->next;
4011 }/* end while(srv) */
4012 }/* end else if server == NULL */
4013
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004014 tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004015 }/* end if ((t->proxy->appsession_name != NULL) ... */
4016 }
4017
4018 /* we'll have to look for another cookie ... */
4019 p1 = p4;
4020 } /* while (p1 < cur_end) */
4021
4022 /* There's no more cookie on this line.
4023 * We may have marked the last one(s) for deletion.
4024 * We must do this now in two ways :
4025 * - if there is no app cookie, we simply delete the header ;
4026 * - if there are app cookies, we must delete the end of the
4027 * string properly, including the colon/semi-colon before
4028 * the cookie name.
4029 */
4030 if (del_cookie != NULL) {
4031 int delta;
4032 if (app_cookies) {
4033 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4034 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004035 cur_hdr->len += delta;
4036 } else {
4037 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004038
4039 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004040 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4041 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004042 cur_hdr->len = 0;
4043 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004044 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004045 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004046 }
4047
4048 /* keep the link from this header to next one */
4049 old_idx = cur_idx;
4050 } /* end of cookie processing on this header */
4051}
4052
4053
Willy Tarreaua15645d2007-03-18 16:22:39 +01004054/* Iterate the same filter through all response headers contained in <rtr>.
4055 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4056 */
4057int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4058{
4059 char term;
4060 char *cur_ptr, *cur_end, *cur_next;
4061 int cur_idx, old_idx, last_hdr;
4062 struct http_txn *txn = &t->txn;
4063 struct hdr_idx_elem *cur_hdr;
4064 int len, delta;
4065
4066 last_hdr = 0;
4067
4068 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4069 old_idx = 0;
4070
4071 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004072 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004073 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004074 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004075 (exp->action == ACT_ALLOW ||
4076 exp->action == ACT_DENY))
4077 return 0;
4078
4079 cur_idx = txn->hdr_idx.v[old_idx].next;
4080 if (!cur_idx)
4081 break;
4082
4083 cur_hdr = &txn->hdr_idx.v[cur_idx];
4084 cur_ptr = cur_next;
4085 cur_end = cur_ptr + cur_hdr->len;
4086 cur_next = cur_end + cur_hdr->cr + 1;
4087
4088 /* Now we have one header between cur_ptr and cur_end,
4089 * and the next header starts at cur_next.
4090 */
4091
4092 /* The annoying part is that pattern matching needs
4093 * that we modify the contents to null-terminate all
4094 * strings before testing them.
4095 */
4096
4097 term = *cur_end;
4098 *cur_end = '\0';
4099
4100 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4101 switch (exp->action) {
4102 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004103 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004104 last_hdr = 1;
4105 break;
4106
4107 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004108 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004109 last_hdr = 1;
4110 break;
4111
4112 case ACT_REPLACE:
4113 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4114 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4115 /* FIXME: if the user adds a newline in the replacement, the
4116 * index will not be recalculated for now, and the new line
4117 * will not be counted as a new header.
4118 */
4119
4120 cur_end += delta;
4121 cur_next += delta;
4122 cur_hdr->len += delta;
4123 txn->rsp.eoh += delta;
4124 break;
4125
4126 case ACT_REMOVE:
4127 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4128 cur_next += delta;
4129
4130 /* FIXME: this should be a separate function */
4131 txn->rsp.eoh += delta;
4132 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4133 txn->hdr_idx.used--;
4134 cur_hdr->len = 0;
4135 cur_end = NULL; /* null-term has been rewritten */
4136 break;
4137
4138 }
4139 }
4140 if (cur_end)
4141 *cur_end = term; /* restore the string terminator */
4142
4143 /* keep the link from this header to next one in case of later
4144 * removal of next header.
4145 */
4146 old_idx = cur_idx;
4147 }
4148 return 0;
4149}
4150
4151
4152/* Apply the filter to the status line in the response buffer <rtr>.
4153 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4154 * or -1 if a replacement resulted in an invalid status line.
4155 */
4156int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4157{
4158 char term;
4159 char *cur_ptr, *cur_end;
4160 int done;
4161 struct http_txn *txn = &t->txn;
4162 int len, delta;
4163
4164
Willy Tarreau3d300592007-03-18 18:34:41 +01004165 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004166 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004167 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004168 (exp->action == ACT_ALLOW ||
4169 exp->action == ACT_DENY))
4170 return 0;
4171 else if (exp->action == ACT_REMOVE)
4172 return 0;
4173
4174 done = 0;
4175
Willy Tarreau9cdde232007-05-02 20:58:19 +02004176 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004177 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4178
4179 /* Now we have the status line between cur_ptr and cur_end */
4180
4181 /* The annoying part is that pattern matching needs
4182 * that we modify the contents to null-terminate all
4183 * strings before testing them.
4184 */
4185
4186 term = *cur_end;
4187 *cur_end = '\0';
4188
4189 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4190 switch (exp->action) {
4191 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004192 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004193 done = 1;
4194 break;
4195
4196 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004197 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004198 done = 1;
4199 break;
4200
4201 case ACT_REPLACE:
4202 *cur_end = term; /* restore the string terminator */
4203 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4204 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4205 /* FIXME: if the user adds a newline in the replacement, the
4206 * index will not be recalculated for now, and the new line
4207 * will not be counted as a new header.
4208 */
4209
4210 txn->rsp.eoh += delta;
4211 cur_end += delta;
4212
Willy Tarreau9cdde232007-05-02 20:58:19 +02004213 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004214 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004215 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004216 cur_ptr, cur_end + 1,
4217 NULL, NULL);
4218 if (unlikely(!cur_end))
4219 return -1;
4220
4221 /* we have a full respnse and we know that we have either a CR
4222 * or an LF at <ptr>.
4223 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004224 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004225 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4226 /* there is no point trying this regex on headers */
4227 return 1;
4228 }
4229 }
4230 *cur_end = term; /* restore the string terminator */
4231 return done;
4232}
4233
4234
4235
4236/*
4237 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4238 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4239 * unparsable response.
4240 */
4241int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4242{
Willy Tarreau3d300592007-03-18 18:34:41 +01004243 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004244 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004245 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004246 int ret;
4247
4248 /*
4249 * The interleaving of transformations and verdicts
4250 * makes it difficult to decide to continue or stop
4251 * the evaluation.
4252 */
4253
Willy Tarreau3d300592007-03-18 18:34:41 +01004254 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004255 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4256 exp->action == ACT_PASS)) {
4257 exp = exp->next;
4258 continue;
4259 }
4260
4261 /* Apply the filter to the status line. */
4262 ret = apply_filter_to_sts_line(t, rtr, exp);
4263 if (unlikely(ret < 0))
4264 return -1;
4265
4266 if (likely(ret == 0)) {
4267 /* The filter did not match the response, it can be
4268 * iterated through all headers.
4269 */
4270 apply_filter_to_resp_headers(t, rtr, exp);
4271 }
4272 exp = exp->next;
4273 }
4274 return 0;
4275}
4276
4277
4278
4279/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004280 * Manage server-side cookies. It can impact performance by about 2% so it is
4281 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004282 */
4283void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4284{
4285 struct http_txn *txn = &t->txn;
4286 char *p1, *p2, *p3, *p4;
4287
4288 appsess *asession_temp = NULL;
4289 appsess local_asession;
4290
4291 char *cur_ptr, *cur_end, *cur_next;
4292 int cur_idx, old_idx, delta;
4293
Willy Tarreaua15645d2007-03-18 16:22:39 +01004294 /* Iterate through the headers.
4295 * we start with the start line.
4296 */
4297 old_idx = 0;
4298 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4299
4300 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4301 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004302 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004303
4304 cur_hdr = &txn->hdr_idx.v[cur_idx];
4305 cur_ptr = cur_next;
4306 cur_end = cur_ptr + cur_hdr->len;
4307 cur_next = cur_end + cur_hdr->cr + 1;
4308
4309 /* We have one full header between cur_ptr and cur_end, and the
4310 * next header starts at cur_next. We're only interested in
4311 * "Cookie:" headers.
4312 */
4313
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004314 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4315 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004316 old_idx = cur_idx;
4317 continue;
4318 }
4319
4320 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004321 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004322
4323
4324 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004325 if (t->be->cookie_name == NULL &&
4326 t->be->appsession_name == NULL &&
4327 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004328 return;
4329
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004330 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004331
4332 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004333 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4334 break;
4335
4336 /* p1 is at the beginning of the cookie name */
4337 p2 = p1;
4338
4339 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4340 p2++;
4341
4342 if (p2 == cur_end || *p2 == ';') /* next cookie */
4343 break;
4344
4345 p3 = p2 + 1; /* skip the '=' sign */
4346 if (p3 == cur_end)
4347 break;
4348
4349 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004350 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004351 p4++;
4352
4353 /* here, we have the cookie name between p1 and p2,
4354 * and its value between p3 and p4.
4355 * we can process it.
4356 */
4357
4358 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004359 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004360 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004361 (p4 - p1 >= t->be->capture_namelen) &&
4362 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004363 int log_len = p4 - p1;
4364
Willy Tarreau086b3b42007-05-13 21:45:51 +02004365 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004366 Alert("HTTP logging : out of memory.\n");
4367 }
4368
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004369 if (log_len > t->be->capture_len)
4370 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004371 memcpy(txn->srv_cookie, p1, log_len);
4372 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004373 }
4374
4375 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004376 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4377 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004378 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004379 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004380
4381 /* If the cookie is in insert mode on a known server, we'll delete
4382 * this occurrence because we'll insert another one later.
4383 * We'll delete it too if the "indirect" option is set and we're in
4384 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004385 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4386 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004387 /* this header must be deleted */
4388 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4389 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4390 txn->hdr_idx.used--;
4391 cur_hdr->len = 0;
4392 cur_next += delta;
4393 txn->rsp.eoh += delta;
4394
Willy Tarreau3d300592007-03-18 18:34:41 +01004395 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004396 }
4397 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004398 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004399 /* replace bytes p3->p4 with the cookie name associated
4400 * with this server since we know it.
4401 */
4402 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4403 cur_hdr->len += delta;
4404 cur_next += delta;
4405 txn->rsp.eoh += delta;
4406
Willy Tarreau3d300592007-03-18 18:34:41 +01004407 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004408 }
4409 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004410 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004411 /* insert the cookie name associated with this server
4412 * before existing cookie, and insert a delimitor between them..
4413 */
4414 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4415 cur_hdr->len += delta;
4416 cur_next += delta;
4417 txn->rsp.eoh += delta;
4418
4419 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004420 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004421 }
4422 }
4423 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004424 else if ((t->be->appsession_name != NULL) &&
4425 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004426
4427 /* Cool... it's the right one */
4428
4429 size_t server_id_len = strlen(t->srv->id) + 1;
4430 asession_temp = &local_asession;
4431
Willy Tarreau63963c62007-05-13 21:29:55 +02004432 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004433 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4434 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4435 return;
4436 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004437 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4438 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004439 asession_temp->serverid = NULL;
4440
4441 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004442 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004443 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004444 Alert("Not enough Memory process_srv():asession:calloc().\n");
4445 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4446 return;
4447 }
4448 asession_temp->sessid = local_asession.sessid;
4449 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004450 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4451 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004452 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004453 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004454 }
4455
Willy Tarreaua15645d2007-03-18 16:22:39 +01004456 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004457 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004458 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4459 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4460 return;
4461 }
4462 asession_temp->serverid[0] = '\0';
4463 }
4464
4465 if (asession_temp->serverid[0] == '\0')
4466 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4467
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004468 tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004469
4470#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004471 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004472#endif
4473 }/* end if ((t->proxy->appsession_name != NULL) ... */
4474 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4475 } /* we're now at the end of the cookie value */
4476
4477 /* keep the link from this header to next one */
4478 old_idx = cur_idx;
4479 } /* end of cookie processing on this header */
4480}
4481
4482
4483
4484/*
4485 * Check if response is cacheable or not. Updates t->flags.
4486 */
4487void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4488{
4489 struct http_txn *txn = &t->txn;
4490 char *p1, *p2;
4491
4492 char *cur_ptr, *cur_end, *cur_next;
4493 int cur_idx;
4494
Willy Tarreau5df51872007-11-25 16:20:08 +01004495 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004496 return;
4497
4498 /* Iterate through the headers.
4499 * we start with the start line.
4500 */
4501 cur_idx = 0;
4502 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4503
4504 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4505 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004506 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004507
4508 cur_hdr = &txn->hdr_idx.v[cur_idx];
4509 cur_ptr = cur_next;
4510 cur_end = cur_ptr + cur_hdr->len;
4511 cur_next = cur_end + cur_hdr->cr + 1;
4512
4513 /* We have one full header between cur_ptr and cur_end, and the
4514 * next header starts at cur_next. We're only interested in
4515 * "Cookie:" headers.
4516 */
4517
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004518 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4519 if (val) {
4520 if ((cur_end - (cur_ptr + val) >= 8) &&
4521 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4522 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4523 return;
4524 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004525 }
4526
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004527 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4528 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004529 continue;
4530
4531 /* OK, right now we know we have a cache-control header at cur_ptr */
4532
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004533 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004534
4535 if (p1 >= cur_end) /* no more info */
4536 continue;
4537
4538 /* p1 is at the beginning of the value */
4539 p2 = p1;
4540
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004541 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004542 p2++;
4543
4544 /* we have a complete value between p1 and p2 */
4545 if (p2 < cur_end && *p2 == '=') {
4546 /* we have something of the form no-cache="set-cookie" */
4547 if ((cur_end - p1 >= 21) &&
4548 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4549 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004550 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004551 continue;
4552 }
4553
4554 /* OK, so we know that either p2 points to the end of string or to a comma */
4555 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4556 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4557 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4558 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004559 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004560 return;
4561 }
4562
4563 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004564 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004565 continue;
4566 }
4567 }
4568}
4569
4570
Willy Tarreau58f10d72006-12-04 02:26:12 +01004571/*
4572 * Try to retrieve a known appsession in the URI, then the associated server.
4573 * If the server is found, it's assigned to the session.
4574 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004575void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004576{
Willy Tarreau3d300592007-03-18 18:34:41 +01004577 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004578 appsess *asession_temp = NULL;
4579 appsess local_asession;
4580 char *request_line;
4581
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004582 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004583 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004584 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004585 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004586 return;
4587
4588 /* skip ';' */
4589 request_line++;
4590
4591 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004592 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004593 return;
4594
4595 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004596 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004597
4598 /* First try if we already have an appsession */
4599 asession_temp = &local_asession;
4600
Willy Tarreau63963c62007-05-13 21:29:55 +02004601 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004602 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4603 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4604 return;
4605 }
4606
4607 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004608 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4609 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004610 asession_temp->serverid = NULL;
4611
4612 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004613 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004614 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004615 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004616 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004617 Alert("Not enough memory process_cli():asession:calloc().\n");
4618 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4619 return;
4620 }
4621 asession_temp->sessid = local_asession.sessid;
4622 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004623 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004624 }
4625 else {
4626 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004627 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004628 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004629
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004630 tv_add(&asession_temp->expire, &now, &t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004631 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004632
Willy Tarreau58f10d72006-12-04 02:26:12 +01004633#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004634 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004635#endif
4636 if (asession_temp->serverid == NULL) {
4637 Alert("Found Application Session without matching server.\n");
4638 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004639 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004640 while (srv) {
4641 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004642 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004643 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004644 txn->flags &= ~TX_CK_MASK;
4645 txn->flags |= TX_CK_VALID;
4646 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004647 t->srv = srv;
4648 break;
4649 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004650 txn->flags &= ~TX_CK_MASK;
4651 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004652 }
4653 }
4654 srv = srv->next;
4655 }
4656 }
4657}
4658
4659
Willy Tarreaub2513902006-12-17 14:52:38 +01004660/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004661 * In a GET or HEAD request, check if the requested URI matches the stats uri
4662 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004663 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004664 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004665 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004666 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004667 *
4668 * Returns 1 if the session's state changes, otherwise 0.
4669 */
4670int stats_check_uri_auth(struct session *t, struct proxy *backend)
4671{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004672 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004673 struct uri_auth *uri_auth = backend->uri_auth;
4674 struct user_auth *user;
4675 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004676 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004677
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004678 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004679 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004680 return 0;
4681
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004682 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004683
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004684 /* the URI is in h */
4685 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004686 return 0;
4687
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004688 h += uri_auth->uri_len;
4689 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4690 if (memcmp(h, ";up", 3) == 0) {
4691 t->flags |= SN_STAT_HIDEDWN;
4692 break;
4693 }
4694 h++;
4695 }
4696
4697 if (uri_auth->refresh) {
4698 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4699 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4700 if (memcmp(h, ";norefresh", 10) == 0) {
4701 t->flags |= SN_STAT_NORFRSH;
4702 break;
4703 }
4704 h++;
4705 }
4706 }
4707
Willy Tarreau55bb8452007-10-17 18:44:57 +02004708 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4709 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4710 if (memcmp(h, ";csv", 4) == 0) {
4711 t->flags |= SN_STAT_FMTCSV;
4712 break;
4713 }
4714 h++;
4715 }
4716
Willy Tarreaub2513902006-12-17 14:52:38 +01004717 /* we are in front of a interceptable URI. Let's check
4718 * if there's an authentication and if it's valid.
4719 */
4720 user = uri_auth->users;
4721 if (!user) {
4722 /* no user auth required, it's OK */
4723 authenticated = 1;
4724 } else {
4725 authenticated = 0;
4726
4727 /* a user list is defined, we have to check.
4728 * skip 21 chars for "Authorization: Basic ".
4729 */
4730
4731 /* FIXME: this should move to an earlier place */
4732 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004733 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4734 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4735 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004736 if (len > 14 &&
4737 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004738 txn->auth_hdr.str = h;
4739 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004740 break;
4741 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004742 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004743 }
4744
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004745 if (txn->auth_hdr.len < 21 ||
4746 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004747 user = NULL;
4748
4749 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004750 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4751 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004752 user->user_pwd, user->user_len)) {
4753 authenticated = 1;
4754 break;
4755 }
4756 user = user->next;
4757 }
4758 }
4759
4760 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004761 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004762
4763 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004764 msg.str = trash;
4765 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004766 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004767 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004768 if (!(t->flags & SN_ERR_MASK))
4769 t->flags |= SN_ERR_PRXCOND;
4770 if (!(t->flags & SN_FINST_MASK))
4771 t->flags |= SN_FINST_R;
4772 return 1;
4773 }
4774
4775 /* The request is valid, the user is authenticate. Let's start sending
4776 * data.
4777 */
4778 t->cli_state = CL_STSHUTR;
4779 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004780 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01004781 t->data_source = DATA_SRC_STATS;
4782 t->data_state = DATA_ST_INIT;
4783 produce_content(t);
4784 return 1;
4785}
4786
4787
Willy Tarreaubaaee002006-06-26 02:48:02 +02004788/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004789 * Print a debug line with a header
4790 */
4791void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4792{
4793 int len, max;
4794 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4795 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4796 max = end - start;
4797 UBOUND(max, sizeof(trash) - len - 1);
4798 len += strlcpy2(trash + len, start, max + 1);
4799 trash[len++] = '\n';
4800 write(1, trash, len);
4801}
4802
4803
Willy Tarreau8797c062007-05-07 00:55:35 +02004804/************************************************************************/
4805/* The code below is dedicated to ACL parsing and matching */
4806/************************************************************************/
4807
4808
4809
4810
4811/* 1. Check on METHOD
4812 * We use the pre-parsed method if it is known, and store its number as an
4813 * integer. If it is unknown, we use the pointer and the length.
4814 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004815static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004816{
4817 int len, meth;
4818
Willy Tarreauae8b7962007-06-09 23:10:04 +02004819 len = strlen(*text);
4820 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004821
4822 pattern->val.i = meth;
4823 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004824 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004825 if (!pattern->ptr.str)
4826 return 0;
4827 pattern->len = len;
4828 }
4829 return 1;
4830}
4831
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004832static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004833acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4834 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004835{
4836 int meth;
4837 struct http_txn *txn = l7;
4838
Willy Tarreauc11416f2007-06-17 16:58:38 +02004839 if (txn->req.msg_state != HTTP_MSG_BODY)
4840 return 0;
4841
Willy Tarreau8797c062007-05-07 00:55:35 +02004842 meth = txn->meth;
4843 test->i = meth;
4844 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004845 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4846 /* ensure the indexes are not affected */
4847 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004848 test->len = txn->req.sl.rq.m_l;
4849 test->ptr = txn->req.sol;
4850 }
4851 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4852 return 1;
4853}
4854
4855static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4856{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004857 int icase;
4858
Willy Tarreau8797c062007-05-07 00:55:35 +02004859 if (test->i != pattern->val.i)
4860 return 0;
4861
4862 if (test->i != HTTP_METH_OTHER)
4863 return 1;
4864
4865 /* Other method, we must compare the strings */
4866 if (pattern->len != test->len)
4867 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004868
4869 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4870 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4871 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau8797c062007-05-07 00:55:35 +02004872 return 0;
4873 return 1;
4874}
4875
4876/* 2. Check on Request/Status Version
4877 * We simply compare strings here.
4878 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004879static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004880{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004881 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004882 if (!pattern->ptr.str)
4883 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004884 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004885 return 1;
4886}
4887
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004888static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004889acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4890 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004891{
4892 struct http_txn *txn = l7;
4893 char *ptr;
4894 int len;
4895
Willy Tarreauc11416f2007-06-17 16:58:38 +02004896 if (txn->req.msg_state != HTTP_MSG_BODY)
4897 return 0;
4898
Willy Tarreau8797c062007-05-07 00:55:35 +02004899 len = txn->req.sl.rq.v_l;
4900 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4901
4902 while ((len-- > 0) && (*ptr++ != '/'));
4903 if (len <= 0)
4904 return 0;
4905
4906 test->ptr = ptr;
4907 test->len = len;
4908
4909 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4910 return 1;
4911}
4912
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004913static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004914acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4915 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004916{
4917 struct http_txn *txn = l7;
4918 char *ptr;
4919 int len;
4920
Willy Tarreauc11416f2007-06-17 16:58:38 +02004921 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4922 return 0;
4923
Willy Tarreau8797c062007-05-07 00:55:35 +02004924 len = txn->rsp.sl.st.v_l;
4925 ptr = txn->rsp.sol;
4926
4927 while ((len-- > 0) && (*ptr++ != '/'));
4928 if (len <= 0)
4929 return 0;
4930
4931 test->ptr = ptr;
4932 test->len = len;
4933
4934 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4935 return 1;
4936}
4937
4938/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004939static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004940acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4941 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004942{
4943 struct http_txn *txn = l7;
4944 char *ptr;
4945 int len;
4946
Willy Tarreauc11416f2007-06-17 16:58:38 +02004947 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4948 return 0;
4949
Willy Tarreau8797c062007-05-07 00:55:35 +02004950 len = txn->rsp.sl.st.c_l;
4951 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4952
4953 test->i = __strl2ui(ptr, len);
4954 test->flags = ACL_TEST_F_VOL_1ST;
4955 return 1;
4956}
4957
4958/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004959static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004960acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4961 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004962{
4963 struct http_txn *txn = l7;
4964
Willy Tarreauc11416f2007-06-17 16:58:38 +02004965 if (txn->req.msg_state != HTTP_MSG_BODY)
4966 return 0;
4967 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4968 /* ensure the indexes are not affected */
4969 return 0;
4970
Willy Tarreau8797c062007-05-07 00:55:35 +02004971 test->len = txn->req.sl.rq.u_l;
4972 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4973
Willy Tarreauf3d25982007-05-08 22:45:09 +02004974 /* we do not need to set READ_ONLY because the data is in a buffer */
4975 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004976 return 1;
4977}
4978
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004979static int
4980acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4981 struct acl_expr *expr, struct acl_test *test)
4982{
4983 struct http_txn *txn = l7;
4984
4985 if (txn->req.msg_state != HTTP_MSG_BODY)
4986 return 0;
4987 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4988 /* ensure the indexes are not affected */
4989 return 0;
4990
4991 /* Parse HTTP request */
4992 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4993 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4994 test->i = AF_INET;
4995
4996 /*
4997 * If we are parsing url in frontend space, we prepare backend stage
4998 * to not parse again the same url ! optimization lazyness...
4999 */
5000 if (px->options & PR_O_HTTP_PROXY)
5001 l4->flags |= SN_ADDR_SET;
5002
5003 test->flags = ACL_TEST_F_READ_ONLY;
5004 return 1;
5005}
5006
5007static int
5008acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5009 struct acl_expr *expr, struct acl_test *test)
5010{
5011 struct http_txn *txn = l7;
5012
5013 if (txn->req.msg_state != HTTP_MSG_BODY)
5014 return 0;
5015 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5016 /* ensure the indexes are not affected */
5017 return 0;
5018
5019 /* Same optimization as url_ip */
5020 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5021 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5022
5023 if (px->options & PR_O_HTTP_PROXY)
5024 l4->flags |= SN_ADDR_SET;
5025
5026 test->flags = ACL_TEST_F_READ_ONLY;
5027 return 1;
5028}
5029
Willy Tarreauc11416f2007-06-17 16:58:38 +02005030/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5031 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5032 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005033static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005034acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005035 struct acl_expr *expr, struct acl_test *test)
5036{
5037 struct http_txn *txn = l7;
5038 struct hdr_idx *idx = &txn->hdr_idx;
5039 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005040
5041 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5042 /* search for header from the beginning */
5043 ctx->idx = 0;
5044
Willy Tarreau33a7e692007-06-10 19:45:56 +02005045 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5046 test->flags |= ACL_TEST_F_FETCH_MORE;
5047 test->flags |= ACL_TEST_F_VOL_HDR;
5048 test->len = ctx->vlen;
5049 test->ptr = (char *)ctx->line + ctx->val;
5050 return 1;
5051 }
5052
5053 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5054 test->flags |= ACL_TEST_F_VOL_HDR;
5055 return 0;
5056}
5057
Willy Tarreau33a7e692007-06-10 19:45:56 +02005058static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005059acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5060 struct acl_expr *expr, struct acl_test *test)
5061{
5062 struct http_txn *txn = l7;
5063
5064 if (txn->req.msg_state != HTTP_MSG_BODY)
5065 return 0;
5066 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5067 /* ensure the indexes are not affected */
5068 return 0;
5069
5070 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5071}
5072
5073static int
5074acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5075 struct acl_expr *expr, struct acl_test *test)
5076{
5077 struct http_txn *txn = l7;
5078
5079 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5080 return 0;
5081
5082 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5083}
5084
5085/* 6. Check on HTTP header count. The number of occurrences is returned.
5086 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5087 */
5088static int
5089acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005090 struct acl_expr *expr, struct acl_test *test)
5091{
5092 struct http_txn *txn = l7;
5093 struct hdr_idx *idx = &txn->hdr_idx;
5094 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005095 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005096
Willy Tarreau33a7e692007-06-10 19:45:56 +02005097 ctx.idx = 0;
5098 cnt = 0;
5099 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5100 cnt++;
5101
5102 test->i = cnt;
5103 test->flags = ACL_TEST_F_VOL_HDR;
5104 return 1;
5105}
5106
Willy Tarreauc11416f2007-06-17 16:58:38 +02005107static int
5108acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5109 struct acl_expr *expr, struct acl_test *test)
5110{
5111 struct http_txn *txn = l7;
5112
5113 if (txn->req.msg_state != HTTP_MSG_BODY)
5114 return 0;
5115 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5116 /* ensure the indexes are not affected */
5117 return 0;
5118
5119 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5120}
5121
5122static int
5123acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5124 struct acl_expr *expr, struct acl_test *test)
5125{
5126 struct http_txn *txn = l7;
5127
5128 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5129 return 0;
5130
5131 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5132}
5133
Willy Tarreau33a7e692007-06-10 19:45:56 +02005134/* 7. Check on HTTP header's integer value. The integer value is returned.
5135 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005136 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005137 */
5138static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005139acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005140 struct acl_expr *expr, struct acl_test *test)
5141{
5142 struct http_txn *txn = l7;
5143 struct hdr_idx *idx = &txn->hdr_idx;
5144 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005145
5146 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5147 /* search for header from the beginning */
5148 ctx->idx = 0;
5149
Willy Tarreau33a7e692007-06-10 19:45:56 +02005150 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5151 test->flags |= ACL_TEST_F_FETCH_MORE;
5152 test->flags |= ACL_TEST_F_VOL_HDR;
5153 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5154 return 1;
5155 }
5156
5157 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5158 test->flags |= ACL_TEST_F_VOL_HDR;
5159 return 0;
5160}
5161
Willy Tarreauc11416f2007-06-17 16:58:38 +02005162static int
5163acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5164 struct acl_expr *expr, struct acl_test *test)
5165{
5166 struct http_txn *txn = l7;
5167
5168 if (txn->req.msg_state != HTTP_MSG_BODY)
5169 return 0;
5170 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5171 /* ensure the indexes are not affected */
5172 return 0;
5173
5174 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5175}
5176
5177static int
5178acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5179 struct acl_expr *expr, struct acl_test *test)
5180{
5181 struct http_txn *txn = l7;
5182
5183 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5184 return 0;
5185
5186 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5187}
5188
Willy Tarreau737b0c12007-06-10 21:28:46 +02005189/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5190 * the first '/' after the possible hostname, and ends before the possible '?'.
5191 */
5192static int
5193acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5194 struct acl_expr *expr, struct acl_test *test)
5195{
5196 struct http_txn *txn = l7;
5197 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005198
Willy Tarreauc11416f2007-06-17 16:58:38 +02005199 if (txn->req.msg_state != HTTP_MSG_BODY)
5200 return 0;
5201 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5202 /* ensure the indexes are not affected */
5203 return 0;
5204
Willy Tarreau737b0c12007-06-10 21:28:46 +02005205 ptr = txn->req.sol + txn->req.sl.rq.u;
5206 end = ptr + txn->req.sl.rq.u_l;
5207
5208 if (ptr >= end)
5209 return 0;
5210
5211 /* RFC2616, par. 5.1.2 :
5212 * Request-URI = "*" | absuri | abspath | authority
5213 */
5214
5215 if (*ptr == '*')
5216 return 0;
5217
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005218 if (isalpha((unsigned char)*ptr)) {
Willy Tarreau737b0c12007-06-10 21:28:46 +02005219 /* this is a scheme as described by RFC3986, par. 3.1 */
5220 ptr++;
5221 while (ptr < end &&
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005222 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
Willy Tarreau737b0c12007-06-10 21:28:46 +02005223 ptr++;
5224 /* skip '://' */
5225 if (ptr == end || *ptr++ != ':')
5226 return 0;
5227 if (ptr == end || *ptr++ != '/')
5228 return 0;
5229 if (ptr == end || *ptr++ != '/')
5230 return 0;
5231 }
5232 /* skip [user[:passwd]@]host[:[port]] */
5233
5234 while (ptr < end && *ptr != '/')
5235 ptr++;
5236
5237 if (ptr == end)
5238 return 0;
5239
5240 /* OK, we got the '/' ! */
5241 test->ptr = ptr;
5242
5243 while (ptr < end && *ptr != '?')
5244 ptr++;
5245
5246 test->len = ptr - test->ptr;
5247
5248 /* we do not need to set READ_ONLY because the data is in a buffer */
5249 test->flags = ACL_TEST_F_VOL_1ST;
5250 return 1;
5251}
5252
5253
Willy Tarreau8797c062007-05-07 00:55:35 +02005254
5255/************************************************************************/
5256/* All supported keywords must be declared here. */
5257/************************************************************************/
5258
5259/* Note: must not be declared <const> as its list will be overwritten */
5260static struct acl_kw_list acl_kws = {{ },{
5261 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5262 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5263 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
Willy Tarreauae8b7962007-06-09 23:10:04 +02005264 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005265
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005266 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5267 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5268 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5269 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5270 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5271 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
5272 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
5273 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip },
5274 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005275
Willy Tarreauc11416f2007-06-17 16:58:38 +02005276 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str },
5277 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg },
5278 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg },
5279 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end },
5280 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub },
5281 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir },
5282 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom },
5283 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int },
5284 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int },
5285
5286 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str },
5287 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg },
5288 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg },
5289 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end },
5290 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub },
5291 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir },
5292 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom },
5293 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int },
5294 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005295
5296 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5297 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5298 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5299 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5300 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5301 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5302 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5303
Willy Tarreauf3d25982007-05-08 22:45:09 +02005304 { NULL, NULL, NULL, NULL },
5305
5306#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005307 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5308 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5309 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5310 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5311 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5312 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5313 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5314
Willy Tarreau8797c062007-05-07 00:55:35 +02005315 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5316 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5317 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5318 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5319 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5320 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5321 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5322 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5323
5324 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5325 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5326 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5327 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5328 { NULL, NULL, NULL, NULL },
5329#endif
5330}};
5331
5332
5333__attribute__((constructor))
5334static void __http_protocol_init(void)
5335{
5336 acl_register_keywords(&acl_kws);
5337}
5338
5339
Willy Tarreau58f10d72006-12-04 02:26:12 +01005340/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005341 * Local variables:
5342 * c-indent-level: 8
5343 * c-basic-offset: 8
5344 * End:
5345 */