blob: 372743146a4dd50f650b4cd317d361e591ff57f7 [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 t->expire = s->req->rex;
624 tv_min(&t->expire, &s->req->rex, &s->req->wex);
625 tv_bound(&t->expire, &s->req->cex);
626 tv_bound(&t->expire, &s->rep->rex);
627 tv_bound(&t->expire, &s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200628
629 /* restore t to its place in the task list */
630 task_queue(t);
631
632#ifdef DEBUG_FULL
633 /* DEBUG code : this should never ever happen, otherwise it indicates
634 * that a task still has something to do and will provoke a quick loop.
635 */
Willy Tarreaud825eef2007-05-12 22:35:00 +0200636 if (tv_remain2(&now, &t->expire) <= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637 exit(100);
638#endif
Willy Tarreaud825eef2007-05-12 22:35:00 +0200639 *next = t->expire;
640 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200641 }
642
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100643 s->fe->feconn--;
644 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200645 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 actconn--;
647
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200648 if (unlikely((global.mode & MODE_DEBUG) &&
649 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100651 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200652 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100653 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654 write(1, trash, len);
655 }
656
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200657 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100658 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659
660 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200661 if (s->logs.logwait &&
662 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200663 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
664 if (s->fe->to_log & LW_REQ)
665 http_sess_log(s);
666 else
667 tcp_sess_log(s);
668 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669
670 /* the task MUST not be in the run queue anymore */
671 task_delete(t);
672 session_free(s);
673 task_free(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200674 tv_eternity(next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675}
676
677
Willy Tarreau42250582007-04-01 01:30:43 +0200678extern const char sess_term_cond[8];
679extern const char sess_fin_state[8];
680extern const char *monthname[12];
681const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
682const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
683 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
684 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200685struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200686struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100687
Willy Tarreau42250582007-04-01 01:30:43 +0200688/*
689 * send a log for the session when we have enough info about it.
690 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100691 */
Willy Tarreau42250582007-04-01 01:30:43 +0200692static void http_sess_log(struct session *s)
693{
694 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
695 struct proxy *fe = s->fe;
696 struct proxy *be = s->be;
697 struct proxy *prx_log;
698 struct http_txn *txn = &s->txn;
699 int tolog;
700 char *uri, *h;
701 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200702 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200703 static char tmpline[MAX_SYSLOG_LEN];
704 int hdr;
705
706 if (fe->logfac1 < 0 && fe->logfac2 < 0)
707 return;
708 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100709
Willy Tarreau42250582007-04-01 01:30:43 +0200710 if (s->cli_addr.ss_family == AF_INET)
711 inet_ntop(AF_INET,
712 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
713 pn, sizeof(pn));
714 else
715 inet_ntop(AF_INET6,
716 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
717 pn, sizeof(pn));
718
Willy Tarreaufe944602007-10-25 10:34:16 +0200719 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200720
721 /* FIXME: let's limit ourselves to frontend logging for now. */
722 tolog = fe->to_log;
723
724 h = tmpline;
725 if (fe->to_log & LW_REQHDR &&
726 txn->req.cap &&
727 (h < tmpline + sizeof(tmpline) - 10)) {
728 *(h++) = ' ';
729 *(h++) = '{';
730 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
731 if (hdr)
732 *(h++) = '|';
733 if (txn->req.cap[hdr] != NULL)
734 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
735 '#', hdr_encode_map, txn->req.cap[hdr]);
736 }
737 *(h++) = '}';
738 }
739
740 if (fe->to_log & LW_RSPHDR &&
741 txn->rsp.cap &&
742 (h < tmpline + sizeof(tmpline) - 7)) {
743 *(h++) = ' ';
744 *(h++) = '{';
745 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
746 if (hdr)
747 *(h++) = '|';
748 if (txn->rsp.cap[hdr] != NULL)
749 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
750 '#', hdr_encode_map, txn->rsp.cap[hdr]);
751 }
752 *(h++) = '}';
753 }
754
755 if (h < tmpline + sizeof(tmpline) - 4) {
756 *(h++) = ' ';
757 *(h++) = '"';
758 uri = txn->uri ? txn->uri : "<BADREQ>";
759 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
760 '#', url_encode_map, uri);
761 *(h++) = '"';
762 }
763 *h = '\0';
764
765 svid = (tolog & LW_SVID) ?
766 (s->data_source != DATA_SRC_STATS) ?
767 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
768
769 send_log(prx_log, LOG_INFO,
770 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
771 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
772 " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n",
773 pn,
774 (s->cli_addr.ss_family == AF_INET) ?
775 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
776 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200777 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
778 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200779 fe->id, be->id, svid,
780 s->logs.t_request,
781 (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1,
782 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
783 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
784 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
785 txn->status,
786 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
787 txn->cli_cookie ? txn->cli_cookie : "-",
788 txn->srv_cookie ? txn->srv_cookie : "-",
789 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
790 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
791 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
792 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
793 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
794 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
795
796 s->logs.logwait = 0;
797}
798
Willy Tarreau117f59e2007-03-04 18:17:17 +0100799
800/*
801 * Capture headers from message starting at <som> according to header list
802 * <cap_hdr>, and fill the <idx> structure appropriately.
803 */
804void capture_headers(char *som, struct hdr_idx *idx,
805 char **cap, struct cap_hdr *cap_hdr)
806{
807 char *eol, *sol, *col, *sov;
808 int cur_idx;
809 struct cap_hdr *h;
810 int len;
811
812 sol = som + hdr_idx_first_pos(idx);
813 cur_idx = hdr_idx_first_idx(idx);
814
815 while (cur_idx) {
816 eol = sol + idx->v[cur_idx].len;
817
818 col = sol;
819 while (col < eol && *col != ':')
820 col++;
821
822 sov = col + 1;
823 while (sov < eol && http_is_lws[(unsigned char)*sov])
824 sov++;
825
826 for (h = cap_hdr; h; h = h->next) {
827 if ((h->namelen == col - sol) &&
828 (strncasecmp(sol, h->name, h->namelen) == 0)) {
829 if (cap[h->index] == NULL)
830 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200831 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100832
833 if (cap[h->index] == NULL) {
834 Alert("HTTP capture : out of memory.\n");
835 continue;
836 }
837
838 len = eol - sov;
839 if (len > h->len)
840 len = h->len;
841
842 memcpy(cap[h->index], sov, len);
843 cap[h->index][len]=0;
844 }
845 }
846 sol = eol + idx->v[cur_idx].cr + 1;
847 cur_idx = idx->v[cur_idx].next;
848 }
849}
850
851
Willy Tarreau42250582007-04-01 01:30:43 +0200852/* either we find an LF at <ptr> or we jump to <bad>.
853 */
854#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
855
856/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
857 * otherwise to <http_msg_ood> with <state> set to <st>.
858 */
859#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
860 ptr++; \
861 if (likely(ptr < end)) \
862 goto good; \
863 else { \
864 state = (st); \
865 goto http_msg_ood; \
866 } \
867 } while (0)
868
869
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100871 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100872 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
873 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
874 * will give undefined results.
875 * Note that it is upon the caller's responsibility to ensure that ptr < end,
876 * and that msg->sol points to the beginning of the response.
877 * If a complete line is found (which implies that at least one CR or LF is
878 * found before <end>, the updated <ptr> is returned, otherwise NULL is
879 * returned indicating an incomplete line (which does not mean that parts have
880 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
881 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
882 * upon next call.
883 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200884 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100885 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
886 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200887 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100888 */
Willy Tarreaua15645d2007-03-18 16:22:39 +0100889const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +0100890 const char *ptr, const char *end,
891 char **ret_ptr, int *ret_state)
892{
893 __label__
894 http_msg_rpver,
895 http_msg_rpver_sp,
896 http_msg_rpcode,
897 http_msg_rpcode_sp,
898 http_msg_rpreason,
899 http_msg_rpline_eol,
900 http_msg_ood, /* out of data */
901 http_msg_invalid;
902
903 switch (state) {
904 http_msg_rpver:
905 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100906 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100907 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
908
909 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100910 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100911 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
912 }
913 goto http_msg_invalid;
914
915 http_msg_rpver_sp:
916 case HTTP_MSG_RPVER_SP:
917 if (likely(!HTTP_IS_LWS(*ptr))) {
918 msg->sl.st.c = ptr - msg_buf;
919 goto http_msg_rpcode;
920 }
921 if (likely(HTTP_IS_SPHT(*ptr)))
922 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
923 /* so it's a CR/LF, this is invalid */
924 goto http_msg_invalid;
925
926 http_msg_rpcode:
927 case HTTP_MSG_RPCODE:
928 if (likely(!HTTP_IS_LWS(*ptr)))
929 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
930
931 if (likely(HTTP_IS_SPHT(*ptr))) {
932 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
933 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
934 }
935
936 /* so it's a CR/LF, so there is no reason phrase */
937 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
938 http_msg_rsp_reason:
939 /* FIXME: should we support HTTP responses without any reason phrase ? */
940 msg->sl.st.r = ptr - msg_buf;
941 msg->sl.st.r_l = 0;
942 goto http_msg_rpline_eol;
943
944 http_msg_rpcode_sp:
945 case HTTP_MSG_RPCODE_SP:
946 if (likely(!HTTP_IS_LWS(*ptr))) {
947 msg->sl.st.r = ptr - msg_buf;
948 goto http_msg_rpreason;
949 }
950 if (likely(HTTP_IS_SPHT(*ptr)))
951 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
952 /* so it's a CR/LF, so there is no reason phrase */
953 goto http_msg_rsp_reason;
954
955 http_msg_rpreason:
956 case HTTP_MSG_RPREASON:
957 if (likely(!HTTP_IS_CRLF(*ptr)))
958 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
959 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
960 http_msg_rpline_eol:
961 /* We have seen the end of line. Note that we do not
962 * necessarily have the \n yet, but at least we know that we
963 * have EITHER \r OR \n, otherwise the response would not be
964 * complete. We can then record the response length and return
965 * to the caller which will be able to register it.
966 */
967 msg->sl.st.l = ptr - msg->sol;
968 return ptr;
969
970#ifdef DEBUG_FULL
971 default:
972 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
973 exit(1);
974#endif
975 }
976
977 http_msg_ood:
978 /* out of data */
979 if (ret_state)
980 *ret_state = state;
981 if (ret_ptr)
982 *ret_ptr = (char *)ptr;
983 return NULL;
984
985 http_msg_invalid:
986 /* invalid message */
987 if (ret_state)
988 *ret_state = HTTP_MSG_ERROR;
989 return NULL;
990}
991
992
993/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100994 * This function parses a request line between <ptr> and <end>, starting with
995 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
996 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
997 * will give undefined results.
998 * Note that it is upon the caller's responsibility to ensure that ptr < end,
999 * and that msg->sol points to the beginning of the request.
1000 * If a complete line is found (which implies that at least one CR or LF is
1001 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1002 * returned indicating an incomplete line (which does not mean that parts have
1003 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1004 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1005 * upon next call.
1006 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001007 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001008 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1009 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001010 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001011 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001012const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state,
Willy Tarreau8973c702007-01-21 23:58:29 +01001013 const char *ptr, const char *end,
1014 char **ret_ptr, int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001015{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001016 __label__
1017 http_msg_rqmeth,
1018 http_msg_rqmeth_sp,
1019 http_msg_rquri,
1020 http_msg_rquri_sp,
1021 http_msg_rqver,
1022 http_msg_rqline_eol,
1023 http_msg_ood, /* out of data */
1024 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001025
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001026 switch (state) {
1027 http_msg_rqmeth:
1028 case HTTP_MSG_RQMETH:
1029 if (likely(HTTP_IS_TOKEN(*ptr)))
1030 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001031
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001032 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001033 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001034 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1035 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001036
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001037 if (likely(HTTP_IS_CRLF(*ptr))) {
1038 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001039 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040 http_msg_req09_uri:
1041 msg->sl.rq.u = ptr - msg_buf;
1042 http_msg_req09_uri_e:
1043 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1044 http_msg_req09_ver:
1045 msg->sl.rq.v = ptr - msg_buf;
1046 msg->sl.rq.v_l = 0;
1047 goto http_msg_rqline_eol;
1048 }
1049 goto http_msg_invalid;
1050
1051 http_msg_rqmeth_sp:
1052 case HTTP_MSG_RQMETH_SP:
1053 if (likely(!HTTP_IS_LWS(*ptr))) {
1054 msg->sl.rq.u = ptr - msg_buf;
1055 goto http_msg_rquri;
1056 }
1057 if (likely(HTTP_IS_SPHT(*ptr)))
1058 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1059 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1060 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001062 http_msg_rquri:
1063 case HTTP_MSG_RQURI:
1064 if (likely(!HTTP_IS_LWS(*ptr)))
1065 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 if (likely(HTTP_IS_SPHT(*ptr))) {
1068 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1069 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1073 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001074
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 http_msg_rquri_sp:
1076 case HTTP_MSG_RQURI_SP:
1077 if (likely(!HTTP_IS_LWS(*ptr))) {
1078 msg->sl.rq.v = ptr - msg_buf;
1079 goto http_msg_rqver;
1080 }
1081 if (likely(HTTP_IS_SPHT(*ptr)))
1082 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1083 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1084 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 http_msg_rqver:
1087 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001088 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001089 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001090
1091 if (likely(HTTP_IS_CRLF(*ptr))) {
1092 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1093 http_msg_rqline_eol:
1094 /* We have seen the end of line. Note that we do not
1095 * necessarily have the \n yet, but at least we know that we
1096 * have EITHER \r OR \n, otherwise the request would not be
1097 * complete. We can then record the request length and return
1098 * to the caller which will be able to register it.
1099 */
1100 msg->sl.rq.l = ptr - msg->sol;
1101 return ptr;
1102 }
1103
1104 /* neither an HTTP_VER token nor a CRLF */
1105 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107#ifdef DEBUG_FULL
1108 default:
1109 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1110 exit(1);
1111#endif
1112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 http_msg_ood:
1115 /* out of data */
1116 if (ret_state)
1117 *ret_state = state;
1118 if (ret_ptr)
1119 *ret_ptr = (char *)ptr;
1120 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001122 http_msg_invalid:
1123 /* invalid message */
1124 if (ret_state)
1125 *ret_state = HTTP_MSG_ERROR;
1126 return NULL;
1127}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001128
1129
Willy Tarreau8973c702007-01-21 23:58:29 +01001130/*
1131 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001132 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001133 * when data are missing and recalled at the exact same location with no
1134 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001135 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1136 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001138void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1139{
1140 __label__
1141 http_msg_rqbefore,
1142 http_msg_rqbefore_cr,
1143 http_msg_rqmeth,
1144 http_msg_rqline_end,
1145 http_msg_hdr_first,
1146 http_msg_hdr_name,
1147 http_msg_hdr_l1_sp,
1148 http_msg_hdr_l1_lf,
1149 http_msg_hdr_l1_lws,
1150 http_msg_hdr_val,
1151 http_msg_hdr_l2_lf,
1152 http_msg_hdr_l2_lws,
1153 http_msg_complete_header,
1154 http_msg_last_lf,
1155 http_msg_ood, /* out of data */
1156 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001157
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 int state; /* updated only when leaving the FSM */
1159 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001160
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001161 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162 ptr = buf->lr;
1163 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001164
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001165 if (unlikely(ptr >= end))
1166 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001169 /*
1170 * First, states that are specific to the response only.
1171 * We check them first so that request and headers are
1172 * closer to each other (accessed more often).
1173 */
1174 http_msg_rpbefore:
1175 case HTTP_MSG_RPBEFORE:
1176 if (likely(HTTP_IS_TOKEN(*ptr))) {
1177 if (likely(ptr == buf->data)) {
1178 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001179 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001180 } else {
1181#if PARSE_PRESERVE_EMPTY_LINES
1182 /* only skip empty leading lines, don't remove them */
1183 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001184 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001185#else
1186 /* Remove empty leading lines, as recommended by
1187 * RFC2616. This takes a lot of time because we
1188 * must move all the buffer backwards, but this
1189 * is rarely needed. The method above will be
1190 * cleaner when we'll be able to start sending
1191 * the request from any place in the buffer.
1192 */
1193 buf->lr = ptr;
1194 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001195 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 msg->sol = buf->data;
1197 ptr = buf->data;
1198 end = buf->r;
1199#endif
1200 }
1201 hdr_idx_init(idx);
1202 state = HTTP_MSG_RPVER;
1203 goto http_msg_rpver;
1204 }
1205
1206 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1207 goto http_msg_invalid;
1208
1209 if (unlikely(*ptr == '\n'))
1210 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1211 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1212 /* stop here */
1213
1214 http_msg_rpbefore_cr:
1215 case HTTP_MSG_RPBEFORE_CR:
1216 EXPECT_LF_HERE(ptr, http_msg_invalid);
1217 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1218 /* stop here */
1219
1220 http_msg_rpver:
1221 case HTTP_MSG_RPVER:
1222 case HTTP_MSG_RPVER_SP:
1223 case HTTP_MSG_RPCODE:
1224 case HTTP_MSG_RPCODE_SP:
1225 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001226 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001227 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001228 if (unlikely(!ptr))
1229 return;
1230
1231 /* we have a full response and we know that we have either a CR
1232 * or an LF at <ptr>.
1233 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001234 //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 +01001235 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1236
1237 msg->sol = ptr;
1238 if (likely(*ptr == '\r'))
1239 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1240 goto http_msg_rpline_end;
1241
1242 http_msg_rpline_end:
1243 case HTTP_MSG_RPLINE_END:
1244 /* msg->sol must point to the first of CR or LF. */
1245 EXPECT_LF_HERE(ptr, http_msg_invalid);
1246 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1247 /* stop here */
1248
1249 /*
1250 * Second, states that are specific to the request only
1251 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 http_msg_rqbefore:
1253 case HTTP_MSG_RQBEFORE:
1254 if (likely(HTTP_IS_TOKEN(*ptr))) {
1255 if (likely(ptr == buf->data)) {
1256 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001257 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258 } else {
1259#if PARSE_PRESERVE_EMPTY_LINES
1260 /* only skip empty leading lines, don't remove them */
1261 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001262 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001263#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 /* Remove empty leading lines, as recommended by
1265 * RFC2616. This takes a lot of time because we
1266 * must move all the buffer backwards, but this
1267 * is rarely needed. The method above will be
1268 * cleaner when we'll be able to start sending
1269 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001270 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 buf->lr = ptr;
1272 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001273 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 msg->sol = buf->data;
1275 ptr = buf->data;
1276 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001277#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001279 /* we will need this when keep-alive will be supported
1280 hdr_idx_init(idx);
1281 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001282 state = HTTP_MSG_RQMETH;
1283 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1287 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 if (unlikely(*ptr == '\n'))
1290 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1291 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001293
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001294 http_msg_rqbefore_cr:
1295 case HTTP_MSG_RQBEFORE_CR:
1296 EXPECT_LF_HERE(ptr, http_msg_invalid);
1297 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001299
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300 http_msg_rqmeth:
1301 case HTTP_MSG_RQMETH:
1302 case HTTP_MSG_RQMETH_SP:
1303 case HTTP_MSG_RQURI:
1304 case HTTP_MSG_RQURI_SP:
1305 case HTTP_MSG_RQVER:
1306 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001307 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 if (unlikely(!ptr))
1309 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 /* we have a full request and we know that we have either a CR
1312 * or an LF at <ptr>.
1313 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001314 //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 +01001315 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 msg->sol = ptr;
1318 if (likely(*ptr == '\r'))
1319 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001321
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 http_msg_rqline_end:
1323 case HTTP_MSG_RQLINE_END:
1324 /* check for HTTP/0.9 request : no version information available.
1325 * msg->sol must point to the first of CR or LF.
1326 */
1327 if (unlikely(msg->sl.rq.v_l == 0))
1328 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 EXPECT_LF_HERE(ptr, http_msg_invalid);
1331 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001333
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 /*
1335 * Common states below
1336 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 http_msg_hdr_first:
1338 case HTTP_MSG_HDR_FIRST:
1339 msg->sol = ptr;
1340 if (likely(!HTTP_IS_CRLF(*ptr))) {
1341 goto http_msg_hdr_name;
1342 }
1343
1344 if (likely(*ptr == '\r'))
1345 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1346 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 http_msg_hdr_name:
1349 case HTTP_MSG_HDR_NAME:
1350 /* assumes msg->sol points to the first char */
1351 if (likely(HTTP_IS_TOKEN(*ptr)))
1352 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001353
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 if (likely(*ptr == ':')) {
1355 msg->col = ptr - buf->data;
1356 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1357 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001358
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001359 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001360
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361 http_msg_hdr_l1_sp:
1362 case HTTP_MSG_HDR_L1_SP:
1363 /* assumes msg->sol points to the first char and msg->col to the colon */
1364 if (likely(HTTP_IS_SPHT(*ptr)))
1365 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 /* header value can be basically anything except CR/LF */
1368 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 if (likely(!HTTP_IS_CRLF(*ptr))) {
1371 goto http_msg_hdr_val;
1372 }
1373
1374 if (likely(*ptr == '\r'))
1375 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1376 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 http_msg_hdr_l1_lf:
1379 case HTTP_MSG_HDR_L1_LF:
1380 EXPECT_LF_HERE(ptr, http_msg_invalid);
1381 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001382
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 http_msg_hdr_l1_lws:
1384 case HTTP_MSG_HDR_L1_LWS:
1385 if (likely(HTTP_IS_SPHT(*ptr))) {
1386 /* replace HT,CR,LF with spaces */
1387 for (; buf->data+msg->sov < ptr; msg->sov++)
1388 buf->data[msg->sov] = ' ';
1389 goto http_msg_hdr_l1_sp;
1390 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001391 /* we had a header consisting only in spaces ! */
1392 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 goto http_msg_complete_header;
1394
1395 http_msg_hdr_val:
1396 case HTTP_MSG_HDR_VAL:
1397 /* assumes msg->sol points to the first char, msg->col to the
1398 * colon, and msg->sov points to the first character of the
1399 * value.
1400 */
1401 if (likely(!HTTP_IS_CRLF(*ptr)))
1402 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 msg->eol = ptr;
1405 /* Note: we could also copy eol into ->eoh so that we have the
1406 * real header end in case it ends with lots of LWS, but is this
1407 * really needed ?
1408 */
1409 if (likely(*ptr == '\r'))
1410 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1411 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 http_msg_hdr_l2_lf:
1414 case HTTP_MSG_HDR_L2_LF:
1415 EXPECT_LF_HERE(ptr, http_msg_invalid);
1416 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_hdr_l2_lws:
1419 case HTTP_MSG_HDR_L2_LWS:
1420 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1421 /* LWS: replace HT,CR,LF with spaces */
1422 for (; msg->eol < ptr; msg->eol++)
1423 *msg->eol = ' ';
1424 goto http_msg_hdr_val;
1425 }
1426 http_msg_complete_header:
1427 /*
1428 * It was a new header, so the last one is finished.
1429 * Assumes msg->sol points to the first char, msg->col to the
1430 * colon, msg->sov points to the first character of the value
1431 * and msg->eol to the first CR or LF so we know how the line
1432 * ends. We insert last header into the index.
1433 */
1434 /*
1435 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1436 write(2, msg->sol, msg->eol-msg->sol);
1437 fprintf(stderr,"\n");
1438 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1441 idx, idx->tail) < 0))
1442 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 msg->sol = ptr;
1445 if (likely(!HTTP_IS_CRLF(*ptr))) {
1446 goto http_msg_hdr_name;
1447 }
1448
1449 if (likely(*ptr == '\r'))
1450 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1451 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 http_msg_last_lf:
1454 case HTTP_MSG_LAST_LF:
1455 /* Assumes msg->sol points to the first of either CR or LF */
1456 EXPECT_LF_HERE(ptr, http_msg_invalid);
1457 ptr++;
1458 buf->lr = ptr;
1459 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001460 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 return;
1462#ifdef DEBUG_FULL
1463 default:
1464 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1465 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001466#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 }
1468 http_msg_ood:
1469 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001470 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 buf->lr = ptr;
1472 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 http_msg_invalid:
1475 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001476 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 return;
1478}
1479
1480/*
1481 * manages the client FSM and its socket. BTW, it also tries to handle the
1482 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1483 * 0 else.
1484 */
1485int process_cli(struct session *t)
1486{
1487 int s = t->srv_state;
1488 int c = t->cli_state;
1489 struct buffer *req = t->req;
1490 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n",
1493 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001494 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 req->rex.tv_sec, req->rex.tv_usec,
1496 rep->wex.tv_sec, rep->wex.tv_usec);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 if (c == CL_STHEADERS) {
1499 /*
1500 * Now parse the partial (or complete) lines.
1501 * We will check the request syntax, and also join multi-line
1502 * headers. An index of all the lines will be elaborated while
1503 * parsing.
1504 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001508 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 * req->data + req->eoh = end of processed headers / start of current one
1510 * req->data + req->eol = end of current header or line (LF or CRLF)
1511 * req->lr = first non-visited byte
1512 * req->r = end of data
1513 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001516 struct http_txn *txn = &t->txn;
1517 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001521 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 /* 1: we might have to print this header in debug mode */
1524 if (unlikely((global.mode & MODE_DEBUG) &&
1525 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001526 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001528
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001529 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 eol = sol + msg->sl.rq.l;
1531 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001532
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001533 sol += hdr_idx_first_pos(&txn->hdr_idx);
1534 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001537 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001539 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1540 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 }
1542 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001543
Willy Tarreau58f10d72006-12-04 02:26:12 +01001544
1545 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001547 * If not so, we check the FD and buffer states before leaving.
1548 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1550 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001551 *
1552 */
1553
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001554 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 /*
1556 * First, let's catch bad requests.
1557 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001558 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001560
1561 /* 1: Since we are in header mode, if there's no space
1562 * left for headers, we won't be able to free more
1563 * later, so the session will never terminate. We
1564 * must terminate it now.
1565 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 if (unlikely(req->l >= req->rlim - req->data)) {
1567 /* FIXME: check if URI is set and return Status
1568 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001569 */
Willy Tarreau06619262006-12-17 08:37:22 +01001570 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001571 }
1572
1573 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1575 /* read error, or last read : give up. */
Willy Tarreaufa645582007-06-03 15:59:52 +02001576 buffer_shutr(req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577 fd_delete(t->cli_fd);
1578 t->cli_state = CL_STCLOSE;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001579 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001580 if (!(t->flags & SN_ERR_MASK))
1581 t->flags |= SN_ERR_CLICL;
1582 if (!(t->flags & SN_FINST_MASK))
1583 t->flags |= SN_FINST_R;
1584 return 1;
1585 }
1586
1587 /* 3: has the read timeout expired ? */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001588 else if (unlikely(tv_isle(&req->rex, &now))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001589 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001590 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001591 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001592 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001593 if (!(t->flags & SN_ERR_MASK))
1594 t->flags |= SN_ERR_CLITO;
1595 if (!(t->flags & SN_FINST_MASK))
1596 t->flags |= SN_FINST_R;
1597 return 1;
1598 }
1599
1600 /* 4: do we need to re-enable the read socket ? */
Willy Tarreau66319382007-04-08 17:17:37 +02001601 else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02001602 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001603 * full. We cannot loop here since stream_sock_read will disable it only if
1604 * req->l == rlim-data
1605 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02001606 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreau58f10d72006-12-04 02:26:12 +01001607 tv_eternity(&req->rex);
1608 }
1609 return t->cli_state != CL_STHEADERS;
1610 }
1611
1612
1613 /****************************************************************
1614 * More interesting part now : we know that we have a complete *
1615 * request which at least looks like HTTP. We have an indicator *
1616 * of each header's length, so we can parse them quickly. *
1617 ****************************************************************/
1618
Willy Tarreau9cdde232007-05-02 20:58:19 +02001619 /* ensure we keep this pointer to the beginning of the message */
1620 msg->sol = req->data + msg->som;
1621
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 /*
1623 * 1: identify the method
1624 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001625 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
1627 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001629 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 if (unlikely((t->fe->monitor_uri_len != 0) &&
1633 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1634 !memcmp(&req->data[msg->sl.rq.u],
1635 t->fe->monitor_uri,
1636 t->fe->monitor_uri_len))) {
1637 /*
1638 * We have found the monitor URI
1639 */
1640 t->flags |= SN_MONITOR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001641 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 client_retnclose(t, &http_200_chunk);
1643 goto return_prx_cond;
1644 }
1645
1646 /*
1647 * 3: Maybe we have to copy the original REQURI for the logs ?
1648 * Note: we cannot log anymore if the request has been
1649 * classified as invalid.
1650 */
1651 if (unlikely(t->logs.logwait & LW_REQ)) {
1652 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001653 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 int urilen = msg->sl.rq.l;
1655
1656 if (urilen >= REQURI_LEN)
1657 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001658 memcpy(txn->uri, &req->data[msg->som], urilen);
1659 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660
1661 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001662 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 } else {
1664 Alert("HTTP logging : out of memory.\n");
1665 }
1666 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001667
Willy Tarreau06619262006-12-17 08:37:22 +01001668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1670 if (unlikely(msg->sl.rq.v_l == 0)) {
1671 int delta;
1672 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001673 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 cur_end = msg->sol + msg->sl.rq.l;
1675 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001676
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 if (msg->sl.rq.u_l == 0) {
1678 /* if no URI was set, add "/" */
1679 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1680 cur_end += delta;
1681 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001682 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 /* add HTTP version */
1684 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1685 msg->eoh += delta;
1686 cur_end += delta;
1687 cur_end = (char *)http_parse_reqline(msg, req->data,
1688 HTTP_MSG_RQMETH,
1689 msg->sol, cur_end + 1,
1690 NULL, NULL);
1691 if (unlikely(!cur_end))
1692 goto return_bad_req;
1693
1694 /* we have a full HTTP/1.0 request now and we know that
1695 * we have either a CR or an LF at <ptr>.
1696 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001697 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001698 }
1699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700
1701 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001702 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001703 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001704 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001705
1706 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001708 * As opposed to version 1.2, now they will be evaluated in the
1709 * filters order and not in the header order. This means that
1710 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001711 *
1712 * We can now check whether we want to switch to another
1713 * backend, in which case we will re-check the backend's
1714 * filters and various options. In order to support 3-level
1715 * switching, here's how we should proceed :
1716 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001717 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001718 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001719 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001720 * There cannot be any switch from there, so ->be cannot be
1721 * changed anymore.
1722 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001723 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001724 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001725 * The response path will be able to apply either ->be, or
1726 * ->be then ->fe filters in order to match the reverse of
1727 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001728 */
1729
Willy Tarreau06619262006-12-17 08:37:22 +01001730 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001731 struct acl_cond *cond;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001732 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001733 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001734
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001735 /* first check whether we have some ACLs set to block this request */
1736 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02001737 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001738 if (cond->pol == ACL_COND_UNLESS)
1739 ret = !ret;
1740
1741 if (ret) {
1742 txn->status = 403;
1743 /* let's log the request time */
1744 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
1745 client_retnclose(t, error_message(t, HTTP_ERR_403));
1746 goto return_prx_cond;
1747 }
1748 }
1749
Willy Tarreau06619262006-12-17 08:37:22 +01001750 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01001751 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
1753 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001754 }
1755
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001756 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
1757 /* to ensure correct connection accounting on
1758 * the backend, we count the connection for the
1759 * one managing the queue.
1760 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001761 t->be->beconn++;
1762 if (t->be->beconn > t->be->beconn_max)
1763 t->be->beconn_max = t->be->beconn;
1764 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001765 t->flags |= SN_BE_ASSIGNED;
1766 }
1767
Willy Tarreau06619262006-12-17 08:37:22 +01001768 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01001769 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01001770 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001771 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001772 /* let's log the request time */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02001773 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreau80587432006-12-24 17:47:20 +01001774 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001775 goto return_prx_cond;
1776 }
1777
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001779 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001780 !(t->flags & SN_CONN_CLOSED)) {
1781 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001782 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001783 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01001784
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001785 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 old_idx = 0;
1787
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001788 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1789 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 cur_ptr = cur_next;
1791 cur_end = cur_ptr + cur_hdr->len;
1792 cur_next = cur_end + cur_hdr->cr + 1;
1793
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001794 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1795 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001796 /* 3 possibilities :
1797 * - we have already set Connection: close,
1798 * so we remove this line.
1799 * - we have not yet set Connection: close,
1800 * but this line indicates close. We leave
1801 * it untouched and set the flag.
1802 * - we have not yet set Connection: close,
1803 * and this line indicates non-close. We
1804 * replace it.
1805 */
1806 if (t->flags & SN_CONN_CLOSED) {
1807 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001808 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001810 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1811 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 cur_hdr->len = 0;
1813 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001814 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1815 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1816 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 cur_next += delta;
1818 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001819 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 }
1821 t->flags |= SN_CONN_CLOSED;
1822 }
1823 }
1824 old_idx = cur_idx;
1825 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02001826 }
1827 /* add request headers from the rule sets in the same order */
1828 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
1829 if (unlikely(http_header_add_tail(req,
1830 &txn->req,
1831 &txn->hdr_idx,
1832 rule_set->req_add[cur_idx])) < 0)
1833 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001834 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01001837 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001838 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01001839 /* we have to check the URI and auth for this request */
1840 if (stats_check_uri_auth(t, rule_set))
1841 return 1;
1842 }
1843
Willy Tarreau55ea7572007-06-17 19:56:27 +02001844 /* now check whether we have some switching rules for this request */
1845 if (!(t->flags & SN_BE_ASSIGNED)) {
1846 struct switching_rule *rule;
1847
1848 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
1849 int ret;
1850
1851 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
1852 if (cond->pol == ACL_COND_UNLESS)
1853 ret = !ret;
1854
1855 if (ret) {
1856 t->be = rule->be.backend;
1857 t->be->beconn++;
1858 if (t->be->beconn > t->be->beconn_max)
1859 t->be->beconn_max = t->be->beconn;
1860 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02001861
1862 /* assign new parameters to the session from the new backend */
1863 t->rep->rto = t->req->wto = t->be->srvtimeout;
1864 t->req->cto = t->be->contimeout;
1865 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02001866 t->flags |= SN_BE_ASSIGNED;
1867 break;
1868 }
1869 }
1870 }
1871
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001872 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
1873 /* No backend was set, but there was a default
1874 * backend set in the frontend, so we use it and
1875 * loop again.
1876 */
1877 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001878 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 */
1884 t->rep->rto = t->req->wto = t->be->srvtimeout;
1885 t->req->cto = t->be->contimeout;
1886 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01001887 t->flags |= SN_BE_ASSIGNED;
1888 }
1889 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01001890
Willy Tarreau58f10d72006-12-04 02:26:12 +01001891
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001892 if (!(t->flags & SN_BE_ASSIGNED)) {
1893 /* To ensure correct connection accounting on
1894 * the backend, we count the connection for the
1895 * one managing the queue.
1896 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001897 t->be->beconn++;
1898 if (t->be->beconn > t->be->beconn_max)
1899 t->be->beconn_max = t->be->beconn;
1900 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001901 t->flags |= SN_BE_ASSIGNED;
1902 }
1903
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001904 /*
1905 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01001906 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001907 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01001908 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01001909 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001910
Willy Tarreau58f10d72006-12-04 02:26:12 +01001911
Willy Tarreau58f10d72006-12-04 02:26:12 +01001912
Willy Tarreau58f10d72006-12-04 02:26:12 +01001913
Willy Tarreau2a324282006-12-05 00:05:46 +01001914 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001915 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01001916 * so let's do the same now.
1917 */
1918
1919 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001920 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001921 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01001922 }
1923
1924
1925 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001926 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01001927 * Note that doing so might move headers in the request, but
1928 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01001929 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01001930 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01001931 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
1932 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01001933 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001934
Willy Tarreau58f10d72006-12-04 02:26:12 +01001935
Willy Tarreau2a324282006-12-05 00:05:46 +01001936 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01001937 * 9: add X-Forwarded-For if either the frontend or the backend
1938 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01001939 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001940 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01001941 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001942 /* Add an X-Forwarded-For header unless the source IP is
1943 * in the 'except' network range.
1944 */
1945 if ((!t->fe->except_mask.s_addr ||
1946 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
1947 != t->fe->except_net.s_addr) &&
1948 (!t->be->except_mask.s_addr ||
1949 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
1950 != t->be->except_net.s_addr)) {
1951 int len;
1952 unsigned char *pn;
1953 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01001954
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001955 len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d",
1956 pn[0], pn[1], pn[2], pn[3]);
1957
1958 if (unlikely(http_header_add_tail2(req, &txn->req,
1959 &txn->hdr_idx, trash, len)) < 0)
1960 goto return_bad_req;
1961 }
Willy Tarreau2a324282006-12-05 00:05:46 +01001962 }
1963 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02001964 /* FIXME: for the sake of completeness, we should also support
1965 * 'except' here, although it is mostly useless in this case.
1966 */
Willy Tarreau2a324282006-12-05 00:05:46 +01001967 int len;
1968 char pn[INET6_ADDRSTRLEN];
1969 inet_ntop(AF_INET6,
1970 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
1971 pn, sizeof(pn));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001972 len = sprintf(trash, "X-Forwarded-For: %s", pn);
1973 if (unlikely(http_header_add_tail2(req, &txn->req,
1974 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001975 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01001976 }
1977 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001978
Willy Tarreau2a324282006-12-05 00:05:46 +01001979 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001980 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02001981 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01001982 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02001983 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001984 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02001985 if ((unlikely(msg->sl.rq.v_l != 8) ||
1986 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
1987 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01001988 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01001989 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01001990 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01001991 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001992
Willy Tarreau2a324282006-12-05 00:05:46 +01001993 /*************************************************************
1994 * OK, that's finished for the headers. We have done what we *
1995 * could. Let's switch to the DATA state. *
1996 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02001997
Willy Tarreau2a324282006-12-05 00:05:46 +01001998 t->cli_state = CL_STDATA;
1999 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002000
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002001 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002002
Willy Tarreaud825eef2007-05-12 22:35:00 +02002003 if (!tv_isset(&t->fe->clitimeout) ||
2004 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002005 /* If the client has no timeout, or if the server is not ready yet,
2006 * and we know for sure that it can expire, then it's cleaner to
2007 * disable the timeout on the client side so that too low values
2008 * cannot make the sessions abort too early.
2009 *
2010 * FIXME-20050705: the server needs a way to re-enable this time-out
2011 * when it switches its state, otherwise a client can stay connected
2012 * indefinitely. This now seems to be OK.
2013 */
2014 tv_eternity(&req->rex);
2015 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002016
Willy Tarreau2a324282006-12-05 00:05:46 +01002017 /* When a connection is tarpitted, we use the queue timeout for the
2018 * tarpit delay, which currently happens to be the server's connect
2019 * timeout. If unset, then set it to zero because we really want it
2020 * to expire at one moment.
2021 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002022 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002023 t->req->l = 0;
2024 /* flush the request so that we can drop the connection early
2025 * if the client closes first.
2026 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002027 if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout))
Willy Tarreaud825eef2007-05-12 22:35:00 +02002028 req->cex = now;
Willy Tarreau2a324282006-12-05 00:05:46 +01002029 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002030
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002031 /* OK let's go on with the BODY now */
Willy Tarreau06619262006-12-17 08:37:22 +01002032 goto process_data;
2033
2034 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002035 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002036 txn->status = 400;
Willy Tarreau80587432006-12-24 17:47:20 +01002037 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002038 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002039 return_prx_cond:
2040 if (!(t->flags & SN_ERR_MASK))
2041 t->flags |= SN_ERR_PRXCOND;
2042 if (!(t->flags & SN_FINST_MASK))
2043 t->flags |= SN_FINST_R;
2044 return 1;
2045
Willy Tarreaubaaee002006-06-26 02:48:02 +02002046 }
2047 else if (c == CL_STDATA) {
2048 process_data:
2049 /* FIXME: this error handling is partly buggy because we always report
2050 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
2051 * or HEADER phase. BTW, it's not logical to expire the client while
2052 * we're waiting for the server to connect.
2053 */
2054 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002055 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002056 buffer_shutr(req);
2057 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002058 fd_delete(t->cli_fd);
2059 t->cli_state = CL_STCLOSE;
2060 if (!(t->flags & SN_ERR_MASK))
2061 t->flags |= SN_ERR_CLICL;
2062 if (!(t->flags & SN_FINST_MASK)) {
2063 if (t->pend_pos)
2064 t->flags |= SN_FINST_Q;
2065 else if (s == SV_STCONN)
2066 t->flags |= SN_FINST_C;
2067 else
2068 t->flags |= SN_FINST_D;
2069 }
2070 return 1;
2071 }
2072 /* last read, or end of server write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002073 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002074 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002075 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002076 t->cli_state = CL_STSHUTR;
2077 return 1;
2078 }
2079 /* last server read and buffer empty */
2080 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002081 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002082 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002083 shutdown(t->cli_fd, SHUT_WR);
2084 /* We must ensure that the read part is still alive when switching
2085 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002086 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002087 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002088 t->cli_state = CL_STSHUTW;
2089 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2090 return 1;
2091 }
2092 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002093 else if (tv_isle(&req->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002094 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002095 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002096 t->cli_state = CL_STSHUTR;
2097 if (!(t->flags & SN_ERR_MASK))
2098 t->flags |= SN_ERR_CLITO;
2099 if (!(t->flags & SN_FINST_MASK)) {
2100 if (t->pend_pos)
2101 t->flags |= SN_FINST_Q;
2102 else if (s == SV_STCONN)
2103 t->flags |= SN_FINST_C;
2104 else
2105 t->flags |= SN_FINST_D;
2106 }
2107 return 1;
2108 }
2109 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002110 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002111 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002112 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002113 shutdown(t->cli_fd, SHUT_WR);
2114 /* We must ensure that the read part is still alive when switching
2115 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002116 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002117 tv_add_ifset(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002118
2119 t->cli_state = CL_STSHUTW;
2120 if (!(t->flags & SN_ERR_MASK))
2121 t->flags |= SN_ERR_CLITO;
2122 if (!(t->flags & SN_FINST_MASK)) {
2123 if (t->pend_pos)
2124 t->flags |= SN_FINST_Q;
2125 else if (s == SV_STCONN)
2126 t->flags |= SN_FINST_C;
2127 else
2128 t->flags |= SN_FINST_D;
2129 }
2130 return 1;
2131 }
2132
2133 if (req->l >= req->rlim - req->data) {
2134 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002135 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002136 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002137 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002138 }
2139 } else {
2140 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002141 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaud825eef2007-05-12 22:35:00 +02002142 if (!tv_isset(&t->fe->clitimeout) ||
2143 (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout)))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002144 /* If the client has no timeout, or if the server not ready yet, and we
2145 * know for sure that it can expire, then it's cleaner to disable the
2146 * timeout on the client side so that too low values cannot make the
2147 * sessions abort too early.
2148 */
Willy Tarreaud7971282006-07-29 18:36:34 +02002149 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002150 else
Willy Tarreaud825eef2007-05-12 22:35:00 +02002151 tv_add(&req->rex, &now, &t->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002152 }
2153 }
2154
2155 if ((rep->l == 0) ||
2156 ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002157 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2158 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002159 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002160 }
2161 } else {
2162 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002163 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2164 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002165 if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002166 /* FIXME: to prevent the client from expiring read timeouts during writes,
2167 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002168 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002169 }
2170 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002171 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002172 }
2173 }
2174 return 0; /* other cases change nothing */
2175 }
2176 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002177 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002178 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002179 fd_delete(t->cli_fd);
2180 t->cli_state = CL_STCLOSE;
2181 if (!(t->flags & SN_ERR_MASK))
2182 t->flags |= SN_ERR_CLICL;
2183 if (!(t->flags & SN_FINST_MASK)) {
2184 if (t->pend_pos)
2185 t->flags |= SN_FINST_Q;
2186 else if (s == SV_STCONN)
2187 t->flags |= SN_FINST_C;
2188 else
2189 t->flags |= SN_FINST_D;
2190 }
2191 return 1;
2192 }
2193 else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)
2194 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002195 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002196 fd_delete(t->cli_fd);
2197 t->cli_state = CL_STCLOSE;
2198 return 1;
2199 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002200 else if (tv_isle(&rep->wex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002201 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002202 fd_delete(t->cli_fd);
2203 t->cli_state = CL_STCLOSE;
2204 if (!(t->flags & SN_ERR_MASK))
2205 t->flags |= SN_ERR_CLITO;
2206 if (!(t->flags & SN_FINST_MASK)) {
2207 if (t->pend_pos)
2208 t->flags |= SN_FINST_Q;
2209 else if (s == SV_STCONN)
2210 t->flags |= SN_FINST_C;
2211 else
2212 t->flags |= SN_FINST_D;
2213 }
2214 return 1;
2215 }
2216
2217 if (t->flags & SN_SELF_GEN) {
2218 produce_content(t);
2219 if (rep->l == 0) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002220 buffer_shutw(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002221 fd_delete(t->cli_fd);
2222 t->cli_state = CL_STCLOSE;
2223 return 1;
2224 }
2225 }
2226
2227 if ((rep->l == 0)
2228 || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002229 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2230 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002231 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002232 }
2233 } else {
2234 /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02002235 if (EV_FD_COND_S(t->cli_fd, DIR_WR)) {
2236 /* restart writing */
Willy Tarreau33014d02007-06-03 15:25:37 +02002237 if (!tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002238 tv_eternity(&rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002239 }
2240 }
2241 return 0;
2242 }
2243 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002244 if (req->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002245 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002246 fd_delete(t->cli_fd);
2247 t->cli_state = CL_STCLOSE;
2248 if (!(t->flags & SN_ERR_MASK))
2249 t->flags |= SN_ERR_CLICL;
2250 if (!(t->flags & SN_FINST_MASK)) {
2251 if (t->pend_pos)
2252 t->flags |= SN_FINST_Q;
2253 else if (s == SV_STCONN)
2254 t->flags |= SN_FINST_C;
2255 else
2256 t->flags |= SN_FINST_D;
2257 }
2258 return 1;
2259 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002260 else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002261 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002262 fd_delete(t->cli_fd);
2263 t->cli_state = CL_STCLOSE;
2264 return 1;
2265 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002266 else if (tv_isle(&req->rex, &now)) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002267 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268 fd_delete(t->cli_fd);
2269 t->cli_state = CL_STCLOSE;
2270 if (!(t->flags & SN_ERR_MASK))
2271 t->flags |= SN_ERR_CLITO;
2272 if (!(t->flags & SN_FINST_MASK)) {
2273 if (t->pend_pos)
2274 t->flags |= SN_FINST_Q;
2275 else if (s == SV_STCONN)
2276 t->flags |= SN_FINST_C;
2277 else
2278 t->flags |= SN_FINST_D;
2279 }
2280 return 1;
2281 }
2282 else if (req->l >= req->rlim - req->data) {
2283 /* no room to read more data */
2284
2285 /* FIXME-20050705: is it possible for a client to maintain a session
2286 * after the timeout by sending more data after it receives a close ?
2287 */
2288
Willy Tarreau66319382007-04-08 17:17:37 +02002289 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002290 /* stop reading until we get some space */
Willy Tarreaud7971282006-07-29 18:36:34 +02002291 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002292 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2293 }
2294 } else {
2295 /* there's still some space in the buffer */
Willy Tarreau66319382007-04-08 17:17:37 +02002296 if (EV_FD_COND_S(t->cli_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002297 if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002298 tv_eternity(&req->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002299 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2300 }
2301 }
2302 return 0;
2303 }
2304 else { /* CL_STCLOSE: nothing to do */
2305 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2306 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002307 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 +02002308 write(1, trash, len);
2309 }
2310 return 0;
2311 }
2312 return 0;
2313}
2314
2315
2316/*
2317 * manages the server FSM and its socket. It returns 1 if a state has changed
2318 * (and a resync may be needed), 0 else.
2319 */
2320int process_srv(struct session *t)
2321{
2322 int s = t->srv_state;
2323 int c = t->cli_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002324 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002325 struct buffer *req = t->req;
2326 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002327 int conn_err;
2328
2329#ifdef DEBUG_FULL
2330 fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]);
2331#endif
Willy Tarreauee991362007-05-14 14:37:50 +02002332
2333#if 0
2334 fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n",
2335 __FUNCTION__, __LINE__,
2336 t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec,
2337 t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec,
2338 t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec);
2339 fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n",
2340 __FUNCTION__, __LINE__,
2341 t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec,
2342 t->be->contimeout.tv_sec, t->be->contimeout.tv_usec,
2343 t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec);
2344
2345 fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n",
2346 __FUNCTION__, __LINE__,
2347 req->cto.tv_sec, req->cto.tv_usec,
2348 req->rto.tv_sec, req->rto.tv_usec,
2349 req->wto.tv_sec, req->wto.tv_usec);
2350
2351 fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n",
2352 __FUNCTION__, __LINE__,
2353 rep->cto.tv_sec, rep->cto.tv_usec,
2354 rep->rto.tv_sec, rep->rto.tv_usec,
2355 rep->wto.tv_sec, rep->wto.tv_usec);
2356#endif
2357
Willy Tarreaubaaee002006-06-26 02:48:02 +02002358 //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 +02002359 //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
2360 //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002361 //);
2362 if (s == SV_STIDLE) {
2363 if (c == CL_STHEADERS)
2364 return 0; /* stay in idle, waiting for data to reach the client side */
2365 else if (c == CL_STCLOSE || c == CL_STSHUTW ||
2366 (c == CL_STSHUTR &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002367 (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002368 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002369 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002370 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002371 /* note that this must not return any error because it would be able to
2372 * overwrite the client_retnclose() output.
2373 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002374 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002375 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002376 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002377 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 +02002378
2379 return 1;
2380 }
2381 else {
Willy Tarreau3d300592007-03-18 18:34:41 +01002382 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002383 /* This connection is being tarpitted. The CLIENT side has
2384 * already set the connect expiration date to the right
2385 * timeout. We just have to check that it has not expired.
2386 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002387 if (!tv_isle(&req->cex, &now))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002388 return 0;
2389
2390 /* We will set the queue timer to the time spent, just for
2391 * logging purposes. We fake a 500 server error, so that the
2392 * attacker will not suspect his connection has been tarpitted.
2393 * It will not cause trouble to the logs because we can exclude
2394 * the tarpitted connections by filtering on the 'PT' status flags.
2395 */
2396 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002397 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002398 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002399 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002400 return 1;
2401 }
2402
Willy Tarreaubaaee002006-06-26 02:48:02 +02002403 /* Right now, we will need to create a connection to the server.
2404 * We might already have tried, and got a connection pending, in
2405 * which case we will not do anything till it's pending. It's up
2406 * to any other session to release it and wake us up again.
2407 */
2408 if (t->pend_pos) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002409 if (!tv_isle(&req->cex, &now))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002410 return 0;
2411 else {
2412 /* we've been waiting too long here */
Willy Tarreaud7971282006-07-29 18:36:34 +02002413 tv_eternity(&req->cex);
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002414 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002415 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002416 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002417 if (t->srv)
2418 t->srv->failed_conns++;
Willy Tarreau73de9892006-11-30 11:40:23 +01002419 t->fe->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002420 return 1;
2421 }
2422 }
2423
2424 do {
2425 /* first, get a connection */
2426 if (srv_redispatch_connect(t))
2427 return t->srv_state != SV_STIDLE;
2428
2429 /* try to (re-)connect to the server, and fail if we expire the
2430 * number of retries.
2431 */
2432 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002433 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002434 return t->srv_state != SV_STIDLE;
2435 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002436 } while (1);
2437 }
2438 }
2439 else if (s == SV_STCONN) { /* connection in progress */
2440 if (c == CL_STCLOSE || c == CL_STSHUTW ||
2441 (c == CL_STSHUTR &&
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002442 ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
2443 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreaud7971282006-07-29 18:36:34 +02002444 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002445 fd_delete(t->srv_fd);
2446 if (t->srv)
2447 t->srv->cur_sess--;
2448
2449 /* note that this must not return any error because it would be able to
2450 * overwrite the client_retnclose() output.
2451 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002452 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002453 return 1;
2454 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002455 if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02002456 //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 +02002457 return 0; /* nothing changed */
2458 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002459 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002460 /* timeout, asynchronous connect error or first write error */
2461 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2462
2463 fd_delete(t->srv_fd);
2464 if (t->srv)
2465 t->srv->cur_sess--;
2466
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002467 if (!(req->flags & BF_WRITE_STATUS))
Willy Tarreaubaaee002006-06-26 02:48:02 +02002468 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2469 else
2470 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2471
2472 /* ensure that we have enough retries left */
2473 if (srv_count_retry_down(t, conn_err))
2474 return 1;
2475
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002476 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002477 /* We're on our last chance, and the REDISP option was specified.
2478 * We will ignore cookie and force to balance or use the dispatcher.
2479 */
2480 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002481 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002482 task_wakeup(t->srv->queue_mgt);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002483
2484 if (t->srv)
2485 t->srv->failed_conns++;
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002486 t->be->redispatches++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002487
2488 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
2489 t->srv = NULL; /* it's left to the dispatcher to choose a server */
Willy Tarreaua5e65752007-03-18 20:53:22 +01002490 http_flush_cookie_flags(txn);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002491
2492 /* first, get a connection */
2493 if (srv_redispatch_connect(t))
2494 return t->srv_state != SV_STIDLE;
2495 }
2496
Willy Tarreaubaaee002006-06-26 02:48:02 +02002497 do {
2498 /* Now we will try to either reconnect to the same server or
2499 * connect to another server. If the connection gets queued
2500 * because all servers are saturated, then we will go back to
2501 * the SV_STIDLE state.
2502 */
2503 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002504 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002505 return t->srv_state != SV_STCONN;
2506 }
2507
2508 /* we need to redispatch the connection to another server */
2509 if (srv_redispatch_connect(t))
2510 return t->srv_state != SV_STCONN;
2511 } while (1);
2512 }
2513 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002514 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002515
2516 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
2517 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002518 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaud7971282006-07-29 18:36:34 +02002519 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002520 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02002521 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002522 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002523 /* FIXME: to prevent the server from expiring read timeouts during writes,
2524 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002525 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002526 }
2527 else
Willy Tarreaud7971282006-07-29 18:36:34 +02002528 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002529 }
2530
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002531 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02002532 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002533 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02002534 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002535
2536 t->srv_state = SV_STDATA;
2537 if (t->srv)
2538 t->srv->cum_sess++;
2539 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
2540
2541 /* if the user wants to log as soon as possible, without counting
2542 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01002543 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002544 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02002545 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002546 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002547#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002548 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01002549 /* TCP splicing supported by both FE and BE */
2550 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2551 }
2552#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02002553 }
2554 else {
2555 t->srv_state = SV_STHEADERS;
2556 if (t->srv)
2557 t->srv->cum_sess++;
2558 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002559 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
2560 /* reset hdr_idx which was already initialized by the request.
2561 * right now, the http parser does it.
2562 * hdr_idx_init(&t->txn.hdr_idx);
2563 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002564 }
Willy Tarreaud7971282006-07-29 18:36:34 +02002565 tv_eternity(&req->cex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002566 return 1;
2567 }
2568 }
2569 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002570 /*
2571 * Now parse the partial (or complete) lines.
2572 * We will check the response syntax, and also join multi-line
2573 * headers. An index of all the lines will be elaborated while
2574 * parsing.
2575 *
2576 * For the parsing, we use a 28 states FSM.
2577 *
2578 * Here is the information we currently have :
2579 * rep->data + req->som = beginning of response
2580 * rep->data + req->eoh = end of processed headers / start of current one
2581 * rep->data + req->eol = end of current header or line (LF or CRLF)
2582 * rep->lr = first non-visited byte
2583 * rep->r = end of data
2584 */
2585
2586 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002587 struct http_msg *msg = &txn->rsp;
2588 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002589
Willy Tarreaua15645d2007-03-18 16:22:39 +01002590 if (likely(rep->lr < rep->r))
2591 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002592
Willy Tarreaua15645d2007-03-18 16:22:39 +01002593 /* 1: we might have to print this header in debug mode */
2594 if (unlikely((global.mode & MODE_DEBUG) &&
2595 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2596 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2597 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002598
Willy Tarreaua15645d2007-03-18 16:22:39 +01002599 sol = rep->data + msg->som;
2600 eol = sol + msg->sl.rq.l;
2601 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002602
Willy Tarreaua15645d2007-03-18 16:22:39 +01002603 sol += hdr_idx_first_pos(&txn->hdr_idx);
2604 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002605
Willy Tarreaua15645d2007-03-18 16:22:39 +01002606 while (cur_idx) {
2607 eol = sol + txn->hdr_idx.v[cur_idx].len;
2608 debug_hdr("srvhdr", t, sol, eol);
2609 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2610 cur_idx = txn->hdr_idx.v[cur_idx].next;
2611 }
2612 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002613
Willy Tarreaubaaee002006-06-26 02:48:02 +02002614
Willy Tarreau66319382007-04-08 17:17:37 +02002615 if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002616 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01002617 * full. We cannot loop here since stream_sock_read will disable it only if
2618 * rep->l == rlim-data
2619 */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002620 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002621 tv_eternity(&rep->rex);
2622 }
2623
2624
2625 /*
2626 * Now we quickly check if we have found a full valid response.
2627 * If not so, we check the FD and buffer states before leaving.
2628 * A full response is indicated by the fact that we have seen
2629 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2630 * responses are checked first.
2631 *
2632 * Depending on whether the client is still there or not, we
2633 * may send an error response back or not. Note that normally
2634 * we should only check for HTTP status there, and check I/O
2635 * errors somewhere else.
2636 */
2637
2638 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2639
2640 /* Invalid response, or read error or write error */
2641 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
2642 (req->flags & BF_WRITE_ERROR) ||
2643 (rep->flags & BF_READ_ERROR))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002644 buffer_shutr(rep);
2645 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002646 fd_delete(t->srv_fd);
2647 if (t->srv) {
2648 t->srv->cur_sess--;
2649 t->srv->failed_resp++;
2650 }
2651 t->be->failed_resp++;
2652 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002653 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002654 client_return(t, error_message(t, HTTP_ERR_502));
2655 if (!(t->flags & SN_ERR_MASK))
2656 t->flags |= SN_ERR_SRVCL;
2657 if (!(t->flags & SN_FINST_MASK))
2658 t->flags |= SN_FINST_H;
2659 /* We used to have a free connection slot. Since we'll never use it,
2660 * we have to inform the server that it may be used by another session.
2661 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002662 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002663 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002664
Willy Tarreaua15645d2007-03-18 16:22:39 +01002665 return 1;
2666 }
2667
2668 /* end of client write or end of server read.
2669 * since we are in header mode, if there's no space left for headers, we
2670 * won't be able to free more later, so the session will never terminate.
2671 */
2672 else if (unlikely(rep->flags & BF_READ_NULL ||
2673 c == CL_STSHUTW || c == CL_STCLOSE ||
2674 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002675 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002676 buffer_shutr(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002677 t->srv_state = SV_STSHUTR;
2678 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2679 return 1;
2680 }
2681
2682 /* read timeout : return a 504 to the client.
2683 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002684 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002685 tv_isle(&rep->rex, &now))) {
Willy Tarreaufa645582007-06-03 15:59:52 +02002686 buffer_shutr(rep);
2687 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002688 fd_delete(t->srv_fd);
2689 if (t->srv) {
2690 t->srv->cur_sess--;
2691 t->srv->failed_resp++;
2692 }
2693 t->be->failed_resp++;
2694 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002695 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002696 client_return(t, error_message(t, HTTP_ERR_504));
2697 if (!(t->flags & SN_ERR_MASK))
2698 t->flags |= SN_ERR_SRVTO;
2699 if (!(t->flags & SN_FINST_MASK))
2700 t->flags |= SN_FINST_H;
2701 /* We used to have a free connection slot. Since we'll never use it,
2702 * we have to inform the server that it may be used by another session.
2703 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002704 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002705 task_wakeup(t->srv->queue_mgt);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002706 return 1;
2707 }
2708
2709 /* last client read and buffer empty */
2710 /* FIXME!!! here, we don't want to switch to SHUTW if the
2711 * client shuts read too early, because we may still have
2712 * some work to do on the headers.
2713 * The side-effect is that if the client completely closes its
2714 * connection during SV_STHEADER, the connection to the server
2715 * is kept until a response comes back or the timeout is reached.
2716 */
2717 else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) &&
2718 (req->l == 0))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002719 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002720 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002721
2722 /* We must ensure that the read part is still
2723 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002724 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002725 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002726
2727 shutdown(t->srv_fd, SHUT_WR);
2728 t->srv_state = SV_STSHUTW;
2729 return 1;
2730 }
2731
2732 /* write timeout */
2733 /* FIXME!!! here, we don't want to switch to SHUTW if the
2734 * client shuts read too early, because we may still have
2735 * some work to do on the headers.
2736 */
Willy Tarreauf161a342007-04-08 16:59:42 +02002737 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002738 tv_isle(&req->wex, &now))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002739 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02002740 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002741 shutdown(t->srv_fd, SHUT_WR);
2742 /* We must ensure that the read part is still alive
2743 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002744 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002745 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002746
2747 t->srv_state = SV_STSHUTW;
2748 if (!(t->flags & SN_ERR_MASK))
2749 t->flags |= SN_ERR_SRVTO;
2750 if (!(t->flags & SN_FINST_MASK))
2751 t->flags |= SN_FINST_H;
2752 return 1;
2753 }
2754
2755 /*
2756 * And now the non-error cases.
2757 */
2758
2759 /* Data remaining in the request buffer.
2760 * This happens during the first pass here, and during
2761 * long posts.
2762 */
2763 else if (likely(req->l)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002764 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
2765 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02002766 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002767 /* FIXME: to prevent the server from expiring read timeouts during writes,
2768 * we refresh it. */
2769 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002770 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002771 else
2772 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002773 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002774 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002775
Willy Tarreaua15645d2007-03-18 16:22:39 +01002776 /* nothing left in the request buffer */
2777 else {
Willy Tarreau66319382007-04-08 17:17:37 +02002778 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
2779 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02002780 tv_eternity(&req->wex);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002781 }
2782 }
2783
2784 return t->srv_state != SV_STHEADERS;
2785 }
2786
2787
2788 /*****************************************************************
2789 * More interesting part now : we know that we have a complete *
2790 * response which at least looks like HTTP. We have an indicator *
2791 * of each header's length, so we can parse them quickly. *
2792 ****************************************************************/
2793
Willy Tarreau9cdde232007-05-02 20:58:19 +02002794 /* ensure we keep this pointer to the beginning of the message */
2795 msg->sol = rep->data + msg->som;
2796
Willy Tarreaua15645d2007-03-18 16:22:39 +01002797 /*
2798 * 1: get the status code and check for cacheability.
2799 */
2800
2801 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002802 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002803
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002804 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002805 case 200:
2806 case 203:
2807 case 206:
2808 case 300:
2809 case 301:
2810 case 410:
2811 /* RFC2616 @13.4:
2812 * "A response received with a status code of
2813 * 200, 203, 206, 300, 301 or 410 MAY be stored
2814 * by a cache (...) unless a cache-control
2815 * directive prohibits caching."
2816 *
2817 * RFC2616 @9.5: POST method :
2818 * "Responses to this method are not cacheable,
2819 * unless the response includes appropriate
2820 * Cache-Control or Expires header fields."
2821 */
2822 if (likely(txn->meth != HTTP_METH_POST) &&
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002823 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
Willy Tarreau3d300592007-03-18 18:34:41 +01002824 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002825 break;
2826 default:
2827 break;
2828 }
2829
2830 /*
2831 * 2: we may need to capture headers
2832 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002833 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01002834 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002835 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002836
2837 /*
2838 * 3: we will have to evaluate the filters.
2839 * As opposed to version 1.2, now they will be evaluated in the
2840 * filters order and not in the header order. This means that
2841 * each filter has to be validated among all headers.
2842 *
2843 * Filters are tried with ->be first, then with ->fe if it is
2844 * different from ->be.
2845 */
2846
2847 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2848
2849 cur_proxy = t->be;
2850 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002851 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002852
2853 /* try headers filters */
2854 if (rule_set->rsp_exp != NULL) {
2855 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2856 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002857 if (t->srv) {
2858 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002859 t->srv->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002860 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002861 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002862 return_srv_prx_502:
Willy Tarreaufa645582007-06-03 15:59:52 +02002863 buffer_shutr(rep);
2864 buffer_shutw(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002865 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002866 t->srv_state = SV_STCLOSE;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002867 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01002868 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002869 if (!(t->flags & SN_ERR_MASK))
2870 t->flags |= SN_ERR_PRXCOND;
2871 if (!(t->flags & SN_FINST_MASK))
2872 t->flags |= SN_FINST_H;
2873 /* We used to have a free connection slot. Since we'll never use it,
2874 * we have to inform the server that it may be used by another session.
2875 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002876 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02002877 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002878 return 1;
2879 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002880 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002881
Willy Tarreaua15645d2007-03-18 16:22:39 +01002882 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002883 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002884 if (t->srv) {
2885 t->srv->cur_sess--;
2886 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002887 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002888 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002889 goto return_srv_prx_502;
2890 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002891
Willy Tarreaua15645d2007-03-18 16:22:39 +01002892 /* We might have to check for "Connection:" */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002893 if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01002894 !(t->flags & SN_CONN_CLOSED)) {
2895 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002896 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002897 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002898
Willy Tarreaua15645d2007-03-18 16:22:39 +01002899 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2900 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002901
Willy Tarreaua15645d2007-03-18 16:22:39 +01002902 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2903 cur_hdr = &txn->hdr_idx.v[cur_idx];
2904 cur_ptr = cur_next;
2905 cur_end = cur_ptr + cur_hdr->len;
2906 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002907
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002908 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2909 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002910 /* 3 possibilities :
2911 * - we have already set Connection: close,
2912 * so we remove this line.
2913 * - we have not yet set Connection: close,
2914 * but this line indicates close. We leave
2915 * it untouched and set the flag.
2916 * - we have not yet set Connection: close,
2917 * and this line indicates non-close. We
2918 * replace it.
2919 */
2920 if (t->flags & SN_CONN_CLOSED) {
2921 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2922 txn->rsp.eoh += delta;
2923 cur_next += delta;
2924 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2925 txn->hdr_idx.used--;
2926 cur_hdr->len = 0;
2927 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002928 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2929 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2930 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002931 cur_next += delta;
2932 cur_hdr->len += delta;
2933 txn->rsp.eoh += delta;
2934 }
2935 t->flags |= SN_CONN_CLOSED;
2936 }
2937 }
2938 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002939 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002940 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002941
Willy Tarreaua15645d2007-03-18 16:22:39 +01002942 /* add response headers from the rule sets in the same order */
2943 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002944 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2945 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002946 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002947 }
2948
Willy Tarreaua15645d2007-03-18 16:22:39 +01002949 /* check whether we're already working on the frontend */
2950 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002951 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002952 cur_proxy = t->fe;
2953 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002954
Willy Tarreaua15645d2007-03-18 16:22:39 +01002955 /*
2956 * 4: check for server cookie.
2957 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002958 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2959 || (t->be->options & PR_O_CHK_CACHE))
2960 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002961
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002962
Willy Tarreaua15645d2007-03-18 16:22:39 +01002963 /*
Willy Tarreau396d2c62007-11-04 19:30:00 +01002964 * 5: check for cache-control or pragma headers if required.
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002965 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002966 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2967 check_response_for_cacheability(t, rep);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002968
2969 /*
2970 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01002971 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002972 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2973 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01002974 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002975
Willy Tarreaua15645d2007-03-18 16:22:39 +01002976 /* the server is known, it's not the one the client requested, we have to
2977 * insert a set-cookie here, except if we want to insert only on POST
2978 * requests and this one isn't. Note that servers which don't have cookies
2979 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002980 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002981 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002982 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01002983 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002984
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002985 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2986 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01002987 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01002988 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002989
Willy Tarreaua15645d2007-03-18 16:22:39 +01002990 /* Here, we will tell an eventual cache on the client side that we don't
2991 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2992 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2993 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2994 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02002995 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
2996
2997 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2998
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002999 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3000 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003001 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003002 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003003 }
3004
3005
3006 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003007 * 7: check if result will be cacheable with a cookie.
3008 * We'll block the response if security checks have caught
3009 * nasty things such as a cacheable cookie.
3010 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003011 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3012 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003013 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003014
Willy Tarreaua15645d2007-03-18 16:22:39 +01003015 /* we're in presence of a cacheable response containing
3016 * a set-cookie header. We'll block it as requested by
3017 * the 'checkcache' option, and send an alert.
3018 */
3019 if (t->srv) {
3020 t->srv->cur_sess--;
3021 t->srv->failed_secu++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003022 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003023 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003024
Willy Tarreaua15645d2007-03-18 16:22:39 +01003025 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003026 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003027 send_log(t->be, LOG_ALERT,
3028 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003029 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003030 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003031 }
3032
Willy Tarreaua15645d2007-03-18 16:22:39 +01003033 /*
3034 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003035 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003036 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003037 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003038 ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003039 if ((unlikely(msg->sl.st.v_l != 8) ||
3040 unlikely(req->data[msg->som + 7] != '0')) &&
3041 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003042 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003043 goto return_bad_resp;
3044 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003045 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003046
Willy Tarreaubaaee002006-06-26 02:48:02 +02003047
Willy Tarreaua15645d2007-03-18 16:22:39 +01003048 /*************************************************************
3049 * OK, that's finished for the headers. We have done what we *
3050 * could. Let's switch to the DATA state. *
3051 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003052
Willy Tarreaua15645d2007-03-18 16:22:39 +01003053 t->srv_state = SV_STDATA;
3054 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003055 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003056
3057 /* client connection already closed or option 'forceclose' required :
3058 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003059 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003060 if ((req->l == 0) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003061 (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003062 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003063 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003064
3065 /* We must ensure that the read part is still alive when switching
3066 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003067 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003068 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003069
Willy Tarreaua15645d2007-03-18 16:22:39 +01003070 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003071 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072 }
3073
Willy Tarreaua15645d2007-03-18 16:22:39 +01003074#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003075 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003076 /* TCP splicing supported by both FE and BE */
3077 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003078 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003079#endif
3080 /* if the user wants to log as soon as possible, without counting
3081 bytes from the server, then this is the right moment. */
3082 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3083 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreaub4987172007-03-18 16:28:03 +01003084 t->logs.bytes_in = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003085 if (t->fe->to_log & LW_REQ)
3086 http_sess_log(t);
3087 else
3088 tcp_sess_log(t);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +01003089 t->logs.bytes_in = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003090 }
3091
Willy Tarreaua15645d2007-03-18 16:22:39 +01003092 /* Note: we must not try to cheat by jumping directly to DATA,
3093 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003094 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003095
3096 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003097 }
3098 else if (s == SV_STDATA) {
3099 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003100 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreaufa645582007-06-03 15:59:52 +02003101 buffer_shutr(rep);
3102 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003103 fd_delete(t->srv_fd);
3104 if (t->srv) {
3105 t->srv->cur_sess--;
3106 t->srv->failed_resp++;
3107 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003108 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003109 t->srv_state = SV_STCLOSE;
3110 if (!(t->flags & SN_ERR_MASK))
3111 t->flags |= SN_ERR_SRVCL;
3112 if (!(t->flags & SN_FINST_MASK))
3113 t->flags |= SN_FINST_D;
3114 /* We used to have a free connection slot. Since we'll never use it,
3115 * we have to inform the server that it may be used by another session.
3116 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003117 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003118 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003119
3120 return 1;
3121 }
3122 /* last read, or end of client write */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003123 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003124 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003125 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003126 t->srv_state = SV_STSHUTR;
3127 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3128 return 1;
3129 }
3130 /* end of client read and no more data to send */
3131 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003132 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003133 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003134 shutdown(t->srv_fd, SHUT_WR);
3135 /* We must ensure that the read part is still alive when switching
3136 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003137 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003138 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003139
3140 t->srv_state = SV_STSHUTW;
3141 return 1;
3142 }
3143 /* read timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003144 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003145 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003146 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003147 t->srv_state = SV_STSHUTR;
3148 if (!(t->flags & SN_ERR_MASK))
3149 t->flags |= SN_ERR_SRVTO;
3150 if (!(t->flags & SN_FINST_MASK))
3151 t->flags |= SN_FINST_D;
3152 return 1;
3153 }
3154 /* write timeout */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003155 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003156 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003157 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003158 shutdown(t->srv_fd, SHUT_WR);
3159 /* We must ensure that the read part is still alive when switching
3160 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003161 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003162 tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003163 t->srv_state = SV_STSHUTW;
3164 if (!(t->flags & SN_ERR_MASK))
3165 t->flags |= SN_ERR_SRVTO;
3166 if (!(t->flags & SN_FINST_MASK))
3167 t->flags |= SN_FINST_D;
3168 return 1;
3169 }
3170
3171 /* recompute request time-outs */
3172 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003173 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3174 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003175 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003176 }
3177 }
3178 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreau66319382007-04-08 17:17:37 +02003179 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3180 /* restart writing */
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003181 if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003182 /* FIXME: to prevent the server from expiring read timeouts during writes,
3183 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003184 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003185 }
3186 else
Willy Tarreaud7971282006-07-29 18:36:34 +02003187 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003188 }
3189 }
3190
3191 /* recompute response time-outs */
3192 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003193 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003194 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003195 }
3196 }
3197 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003198 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003199 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003200 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003201 }
3202 }
3203
3204 return 0; /* other cases change nothing */
3205 }
3206 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003207 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003208 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003209 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003210 fd_delete(t->srv_fd);
3211 if (t->srv) {
3212 t->srv->cur_sess--;
3213 t->srv->failed_resp++;
3214 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003215 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003216 //close(t->srv_fd);
3217 t->srv_state = SV_STCLOSE;
3218 if (!(t->flags & SN_ERR_MASK))
3219 t->flags |= SN_ERR_SRVCL;
3220 if (!(t->flags & SN_FINST_MASK))
3221 t->flags |= SN_FINST_D;
3222 /* We used to have a free connection slot. Since we'll never use it,
3223 * we have to inform the server that it may be used by another session.
3224 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003225 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003226 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003227
3228 return 1;
3229 }
3230 else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003231 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003232 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003233 fd_delete(t->srv_fd);
3234 if (t->srv)
3235 t->srv->cur_sess--;
3236 //close(t->srv_fd);
3237 t->srv_state = SV_STCLOSE;
3238 /* We used to have a free connection slot. Since we'll never use it,
3239 * we have to inform the server that it may be used by another session.
3240 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003241 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003242 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003243
3244 return 1;
3245 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003246 else if (tv_isle(&req->wex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003247 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreaufa645582007-06-03 15:59:52 +02003248 buffer_shutw(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003249 fd_delete(t->srv_fd);
3250 if (t->srv)
3251 t->srv->cur_sess--;
3252 //close(t->srv_fd);
3253 t->srv_state = SV_STCLOSE;
3254 if (!(t->flags & SN_ERR_MASK))
3255 t->flags |= SN_ERR_SRVTO;
3256 if (!(t->flags & SN_FINST_MASK))
3257 t->flags |= SN_FINST_D;
3258 /* We used to have a free connection slot. Since we'll never use it,
3259 * we have to inform the server that it may be used by another session.
3260 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003261 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003262 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003263
3264 return 1;
3265 }
3266 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003267 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3268 /* stop writing */
Willy Tarreaud7971282006-07-29 18:36:34 +02003269 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003270 }
3271 }
3272 else { /* buffer not empty */
Willy Tarreau66319382007-04-08 17:17:37 +02003273 if (EV_FD_COND_S(t->srv_fd, DIR_WR)) {
3274 /* restart writing */
Willy Tarreau33014d02007-06-03 15:25:37 +02003275 if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003276 tv_eternity(&req->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003277 }
3278 }
3279 return 0;
3280 }
3281 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003282 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003283 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003284 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003285 fd_delete(t->srv_fd);
3286 if (t->srv) {
3287 t->srv->cur_sess--;
3288 t->srv->failed_resp++;
3289 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003290 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003291 //close(t->srv_fd);
3292 t->srv_state = SV_STCLOSE;
3293 if (!(t->flags & SN_ERR_MASK))
3294 t->flags |= SN_ERR_SRVCL;
3295 if (!(t->flags & SN_FINST_MASK))
3296 t->flags |= SN_FINST_D;
3297 /* We used to have a free connection slot. Since we'll never use it,
3298 * we have to inform the server that it may be used by another session.
3299 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003300 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003301 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003302
3303 return 1;
3304 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003305 else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003306 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003307 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003308 fd_delete(t->srv_fd);
3309 if (t->srv)
3310 t->srv->cur_sess--;
3311 //close(t->srv_fd);
3312 t->srv_state = SV_STCLOSE;
3313 /* We used to have a free connection slot. Since we'll never use it,
3314 * we have to inform the server that it may be used by another session.
3315 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003316 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003317 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003318
3319 return 1;
3320 }
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003321 else if (tv_isle(&rep->rex, &now)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003322 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003323 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003324 fd_delete(t->srv_fd);
3325 if (t->srv)
3326 t->srv->cur_sess--;
3327 //close(t->srv_fd);
3328 t->srv_state = SV_STCLOSE;
3329 if (!(t->flags & SN_ERR_MASK))
3330 t->flags |= SN_ERR_SRVTO;
3331 if (!(t->flags & SN_FINST_MASK))
3332 t->flags |= SN_FINST_D;
3333 /* We used to have a free connection slot. Since we'll never use it,
3334 * we have to inform the server that it may be used by another session.
3335 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003336 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau96bcfd72007-04-29 10:41:56 +02003337 task_wakeup(t->srv->queue_mgt);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003338
3339 return 1;
3340 }
3341 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003342 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreaud7971282006-07-29 18:36:34 +02003343 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003344 }
3345 }
3346 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003347 if (EV_FD_COND_S(t->srv_fd, DIR_RD)) {
Willy Tarreaua8b55e32007-05-13 16:08:19 +02003348 if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout))
Willy Tarreaud7971282006-07-29 18:36:34 +02003349 tv_eternity(&rep->rex);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003350 }
3351 }
3352 return 0;
3353 }
3354 else { /* SV_STCLOSE : nothing to do */
3355 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3356 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003357 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003358 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003359 write(1, trash, len);
3360 }
3361 return 0;
3362 }
3363 return 0;
3364}
3365
3366
3367/*
3368 * Produces data for the session <s> depending on its source. Expects to be
3369 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3370 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3371 * session, which it uses to keep on being called when there is free space in
3372 * the buffer, of simply by letting an empty buffer upon return. It returns 1
3373 * if it changes the session state from CL_STSHUTR, otherwise 0.
3374 */
3375int produce_content(struct session *s)
3376{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003377 if (s->data_source == DATA_SRC_NONE) {
3378 s->flags &= ~SN_SELF_GEN;
3379 return 1;
3380 }
3381 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003382 /* dump server statistics */
Willy Tarreau55bb8452007-10-17 18:44:57 +02003383 int ret = stats_dump_http(s, s->be->uri_auth,
3384 (s->flags & SN_STAT_FMTCSV) ? 0 : STAT_FMT_HTML);
Willy Tarreau91861262007-10-17 17:06:05 +02003385 if (ret >= 0)
3386 return ret;
3387 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003388 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003389
Willy Tarreau91861262007-10-17 17:06:05 +02003390 /* unknown data source or internal error */
3391 s->txn.status = 500;
3392 client_retnclose(s, error_message(s, HTTP_ERR_500));
3393 if (!(s->flags & SN_ERR_MASK))
3394 s->flags |= SN_ERR_PRXCOND;
3395 if (!(s->flags & SN_FINST_MASK))
3396 s->flags |= SN_FINST_R;
3397 s->flags &= ~SN_SELF_GEN;
3398 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003399}
3400
3401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003402/* Iterate the same filter through all request headers.
3403 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003404 * Since it can manage the switch to another backend, it updates the per-proxy
3405 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003406 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003407int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003408{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003409 char term;
3410 char *cur_ptr, *cur_end, *cur_next;
3411 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003412 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003413 struct hdr_idx_elem *cur_hdr;
3414 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003416 last_hdr = 0;
3417
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003418 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003419 old_idx = 0;
3420
3421 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003422 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003423 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003424 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003425 (exp->action == ACT_ALLOW ||
3426 exp->action == ACT_DENY ||
3427 exp->action == ACT_TARPIT))
3428 return 0;
3429
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003430 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003431 if (!cur_idx)
3432 break;
3433
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003434 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003435 cur_ptr = cur_next;
3436 cur_end = cur_ptr + cur_hdr->len;
3437 cur_next = cur_end + cur_hdr->cr + 1;
3438
3439 /* Now we have one header between cur_ptr and cur_end,
3440 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003441 */
3442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003443 /* The annoying part is that pattern matching needs
3444 * that we modify the contents to null-terminate all
3445 * strings before testing them.
3446 */
3447
3448 term = *cur_end;
3449 *cur_end = '\0';
3450
3451 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3452 switch (exp->action) {
3453 case ACT_SETBE:
3454 /* It is not possible to jump a second time.
3455 * FIXME: should we return an HTTP/500 here so that
3456 * the admin knows there's a problem ?
3457 */
3458 if (t->be != t->fe)
3459 break;
3460
3461 /* Swithing Proxy */
3462 t->be = (struct proxy *) exp->replace;
3463
3464 /* right now, the backend switch is not too much complicated
3465 * because we have associated req_cap and rsp_cap to the
3466 * frontend, and the beconn will be updated later.
3467 */
3468
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003469 t->rep->rto = t->req->wto = t->be->srvtimeout;
3470 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003471 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003472 last_hdr = 1;
3473 break;
3474
3475 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003476 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003477 last_hdr = 1;
3478 break;
3479
3480 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003481 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003482 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003483 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003484 break;
3485
3486 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003487 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003488 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003489 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003490 break;
3491
3492 case ACT_REPLACE:
3493 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3494 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3495 /* FIXME: if the user adds a newline in the replacement, the
3496 * index will not be recalculated for now, and the new line
3497 * will not be counted as a new header.
3498 */
3499
3500 cur_end += delta;
3501 cur_next += delta;
3502 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003503 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003504 break;
3505
3506 case ACT_REMOVE:
3507 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3508 cur_next += delta;
3509
3510 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003511 txn->req.eoh += delta;
3512 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3513 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003514 cur_hdr->len = 0;
3515 cur_end = NULL; /* null-term has been rewritten */
3516 break;
3517
3518 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003519 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003520 if (cur_end)
3521 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003522
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003523 /* keep the link from this header to next one in case of later
3524 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003525 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003526 old_idx = cur_idx;
3527 }
3528 return 0;
3529}
3530
3531
3532/* Apply the filter to the request line.
3533 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3534 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003535 * Since it can manage the switch to another backend, it updates the per-proxy
3536 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003537 */
3538int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3539{
3540 char term;
3541 char *cur_ptr, *cur_end;
3542 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003543 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003544 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003545
Willy Tarreau58f10d72006-12-04 02:26:12 +01003546
Willy Tarreau3d300592007-03-18 18:34:41 +01003547 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003548 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003549 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003550 (exp->action == ACT_ALLOW ||
3551 exp->action == ACT_DENY ||
3552 exp->action == ACT_TARPIT))
3553 return 0;
3554 else if (exp->action == ACT_REMOVE)
3555 return 0;
3556
3557 done = 0;
3558
Willy Tarreau9cdde232007-05-02 20:58:19 +02003559 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003560 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003561
3562 /* Now we have the request line between cur_ptr and cur_end */
3563
3564 /* The annoying part is that pattern matching needs
3565 * that we modify the contents to null-terminate all
3566 * strings before testing them.
3567 */
3568
3569 term = *cur_end;
3570 *cur_end = '\0';
3571
3572 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3573 switch (exp->action) {
3574 case ACT_SETBE:
3575 /* It is not possible to jump a second time.
3576 * FIXME: should we return an HTTP/500 here so that
3577 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003578 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003579 if (t->be != t->fe)
3580 break;
3581
3582 /* Swithing Proxy */
3583 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003584
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003585 /* right now, the backend switch is not too much complicated
3586 * because we have associated req_cap and rsp_cap to the
3587 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003588 */
3589
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003590 t->rep->rto = t->req->wto = t->be->srvtimeout;
3591 t->req->cto = t->be->contimeout;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003592 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003593 done = 1;
3594 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003596 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003597 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003598 done = 1;
3599 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003601 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003602 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003603 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003604 done = 1;
3605 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003607 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003608 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003609 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003610 done = 1;
3611 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003612
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003613 case ACT_REPLACE:
3614 *cur_end = term; /* restore the string terminator */
3615 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3616 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3617 /* FIXME: if the user adds a newline in the replacement, the
3618 * index will not be recalculated for now, and the new line
3619 * will not be counted as a new header.
3620 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003621
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003622 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003623 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003624
Willy Tarreau9cdde232007-05-02 20:58:19 +02003625 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003626 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003627 HTTP_MSG_RQMETH,
3628 cur_ptr, cur_end + 1,
3629 NULL, NULL);
3630 if (unlikely(!cur_end))
3631 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003633 /* we have a full request and we know that we have either a CR
3634 * or an LF at <ptr>.
3635 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003636 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3637 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003638 /* there is no point trying this regex on headers */
3639 return 1;
3640 }
3641 }
3642 *cur_end = term; /* restore the string terminator */
3643 return done;
3644}
Willy Tarreau97de6242006-12-27 17:18:38 +01003645
Willy Tarreau58f10d72006-12-04 02:26:12 +01003646
Willy Tarreau58f10d72006-12-04 02:26:12 +01003647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003648/*
3649 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3650 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003651 * unparsable request. Since it can manage the switch to another backend, it
3652 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003653 */
3654int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3655{
Willy Tarreau3d300592007-03-18 18:34:41 +01003656 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003657 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003658 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003659 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003661 /*
3662 * The interleaving of transformations and verdicts
3663 * makes it difficult to decide to continue or stop
3664 * the evaluation.
3665 */
3666
Willy Tarreau3d300592007-03-18 18:34:41 +01003667 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003668 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3669 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3670 exp = exp->next;
3671 continue;
3672 }
3673
3674 /* Apply the filter to the request line. */
3675 ret = apply_filter_to_req_line(t, req, exp);
3676 if (unlikely(ret < 0))
3677 return -1;
3678
3679 if (likely(ret == 0)) {
3680 /* The filter did not match the request, it can be
3681 * iterated through all headers.
3682 */
3683 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003684 }
3685 exp = exp->next;
3686 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003687 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003688}
3689
3690
Willy Tarreaua15645d2007-03-18 16:22:39 +01003691
Willy Tarreau58f10d72006-12-04 02:26:12 +01003692/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003693 * Manage client-side cookie. It can impact performance by about 2% so it is
3694 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003695 */
3696void manage_client_side_cookies(struct session *t, struct buffer *req)
3697{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003698 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003699 char *p1, *p2, *p3, *p4;
3700 char *del_colon, *del_cookie, *colon;
3701 int app_cookies;
3702
3703 appsess *asession_temp = NULL;
3704 appsess local_asession;
3705
3706 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003707 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003708
Willy Tarreau2a324282006-12-05 00:05:46 +01003709 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003710 * we start with the start line.
3711 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003712 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003713 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003714
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003715 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003716 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003717 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003718
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003719 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003720 cur_ptr = cur_next;
3721 cur_end = cur_ptr + cur_hdr->len;
3722 cur_next = cur_end + cur_hdr->cr + 1;
3723
3724 /* We have one full header between cur_ptr and cur_end, and the
3725 * next header starts at cur_next. We're only interested in
3726 * "Cookie:" headers.
3727 */
3728
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003729 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3730 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003731 old_idx = cur_idx;
3732 continue;
3733 }
3734
3735 /* Now look for cookies. Conforming to RFC2109, we have to support
3736 * attributes whose name begin with a '$', and associate them with
3737 * the right cookie, if we want to delete this cookie.
3738 * So there are 3 cases for each cookie read :
3739 * 1) it's a special attribute, beginning with a '$' : ignore it.
3740 * 2) it's a server id cookie that we *MAY* want to delete : save
3741 * some pointers on it (last semi-colon, beginning of cookie...)
3742 * 3) it's an application cookie : we *MAY* have to delete a previous
3743 * "special" cookie.
3744 * At the end of loop, if a "special" cookie remains, we may have to
3745 * remove it. If no application cookie persists in the header, we
3746 * *MUST* delete it
3747 */
3748
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003749 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003750
Willy Tarreau58f10d72006-12-04 02:26:12 +01003751 /* del_cookie == NULL => nothing to be deleted */
3752 del_colon = del_cookie = NULL;
3753 app_cookies = 0;
3754
3755 while (p1 < cur_end) {
3756 /* skip spaces and colons, but keep an eye on these ones */
3757 while (p1 < cur_end) {
3758 if (*p1 == ';' || *p1 == ',')
3759 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003760 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003761 break;
3762 p1++;
3763 }
3764
3765 if (p1 == cur_end)
3766 break;
3767
3768 /* p1 is at the beginning of the cookie name */
3769 p2 = p1;
3770 while (p2 < cur_end && *p2 != '=')
3771 p2++;
3772
3773 if (p2 == cur_end)
3774 break;
3775
3776 p3 = p2 + 1; /* skips the '=' sign */
3777 if (p3 == cur_end)
3778 break;
3779
3780 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003781 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003782 p4++;
3783
3784 /* here, we have the cookie name between p1 and p2,
3785 * and its value between p3 and p4.
3786 * we can process it :
3787 *
3788 * Cookie: NAME=VALUE;
3789 * | || || |
3790 * | || || +--> p4
3791 * | || |+-------> p3
3792 * | || +--------> p2
3793 * | |+------------> p1
3794 * | +-------------> colon
3795 * +--------------------> cur_ptr
3796 */
3797
3798 if (*p1 == '$') {
3799 /* skip this one */
3800 }
3801 else {
3802 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003803 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003804 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003805 (p4 - p1 >= t->fe->capture_namelen) &&
3806 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003807 int log_len = p4 - p1;
3808
Willy Tarreau086b3b42007-05-13 21:45:51 +02003809 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003810 Alert("HTTP logging : out of memory.\n");
3811 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003812 if (log_len > t->fe->capture_len)
3813 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003814 memcpy(txn->cli_cookie, p1, log_len);
3815 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003816 }
3817 }
3818
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003819 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3820 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003821 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003822 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003823 char *delim;
3824
3825 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3826 * have the server ID betweek p3 and delim, and the original cookie between
3827 * delim+1 and p4. Otherwise, delim==p4 :
3828 *
3829 * Cookie: NAME=SRV~VALUE;
3830 * | || || | |
3831 * | || || | +--> p4
3832 * | || || +--------> delim
3833 * | || |+-----------> p3
3834 * | || +------------> p2
3835 * | |+----------------> p1
3836 * | +-----------------> colon
3837 * +------------------------> cur_ptr
3838 */
3839
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003840 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003841 for (delim = p3; delim < p4; delim++)
3842 if (*delim == COOKIE_DELIM)
3843 break;
3844 }
3845 else
3846 delim = p4;
3847
3848
3849 /* Here, we'll look for the first running server which supports the cookie.
3850 * This allows to share a same cookie between several servers, for example
3851 * to dedicate backup servers to specific servers only.
3852 * However, to prevent clients from sticking to cookie-less backup server
3853 * when they have incidentely learned an empty cookie, we simply ignore
3854 * empty cookies and mark them as invalid.
3855 */
3856 if (delim == p3)
3857 srv = NULL;
3858
3859 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003860 if (srv->cookie && (srv->cklen == delim - p3) &&
3861 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003862 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003863 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003864 txn->flags &= ~TX_CK_MASK;
3865 txn->flags |= TX_CK_VALID;
3866 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003867 t->srv = srv;
3868 break;
3869 } else {
3870 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003871 txn->flags &= ~TX_CK_MASK;
3872 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003873 }
3874 }
3875 srv = srv->next;
3876 }
3877
Willy Tarreau3d300592007-03-18 18:34:41 +01003878 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003879 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003880 txn->flags &= ~TX_CK_MASK;
3881 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003882 }
3883
3884 /* depending on the cookie mode, we may have to either :
3885 * - delete the complete cookie if we're in insert+indirect mode, so that
3886 * the server never sees it ;
3887 * - remove the server id from the cookie value, and tag the cookie as an
3888 * application cookie so that it does not get accidentely removed later,
3889 * if we're in cookie prefix mode
3890 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003891 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003892 int delta; /* negative */
3893
3894 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3895 p4 += delta;
3896 cur_end += delta;
3897 cur_next += delta;
3898 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003899 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003900
3901 del_cookie = del_colon = NULL;
3902 app_cookies++; /* protect the header from deletion */
3903 }
3904 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003905 (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 +01003906 del_cookie = p1;
3907 del_colon = colon;
3908 }
3909 } else {
3910 /* now we know that we must keep this cookie since it's
3911 * not ours. But if we wanted to delete our cookie
3912 * earlier, we cannot remove the complete header, but we
3913 * can remove the previous block itself.
3914 */
3915 app_cookies++;
3916
3917 if (del_cookie != NULL) {
3918 int delta; /* negative */
3919
3920 delta = buffer_replace2(req, del_cookie, p1, 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 del_cookie = del_colon = NULL;
3927 }
3928 }
3929
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003930 if ((t->be->appsession_name != NULL) &&
3931 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003932 /* first, let's see if the cookie is our appcookie*/
3933
3934 /* Cool... it's the right one */
3935
3936 asession_temp = &local_asession;
3937
Willy Tarreau63963c62007-05-13 21:29:55 +02003938 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003939 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3940 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3941 return;
3942 }
3943
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003944 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3945 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003946 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003947
Willy Tarreau58f10d72006-12-04 02:26:12 +01003948 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003949 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3950 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003951 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003952 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003953 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003954 Alert("Not enough memory process_cli():asession:calloc().\n");
3955 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3956 return;
3957 }
3958
3959 asession_temp->sessid = local_asession.sessid;
3960 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02003961 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003962 } else {
3963 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003964 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003965 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003966 if (asession_temp->serverid == NULL) {
3967 Alert("Found Application Session without matching server.\n");
3968 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003969 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003970 while (srv) {
3971 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003972 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003973 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003974 txn->flags &= ~TX_CK_MASK;
3975 txn->flags |= TX_CK_VALID;
3976 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003977 t->srv = srv;
3978 break;
3979 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003980 txn->flags &= ~TX_CK_MASK;
3981 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003982 }
3983 }
3984 srv = srv->next;
3985 }/* end while(srv) */
3986 }/* end else if server == NULL */
3987
Willy Tarreaud825eef2007-05-12 22:35:00 +02003988 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003989 }/* end if ((t->proxy->appsession_name != NULL) ... */
3990 }
3991
3992 /* we'll have to look for another cookie ... */
3993 p1 = p4;
3994 } /* while (p1 < cur_end) */
3995
3996 /* There's no more cookie on this line.
3997 * We may have marked the last one(s) for deletion.
3998 * We must do this now in two ways :
3999 * - if there is no app cookie, we simply delete the header ;
4000 * - if there are app cookies, we must delete the end of the
4001 * string properly, including the colon/semi-colon before
4002 * the cookie name.
4003 */
4004 if (del_cookie != NULL) {
4005 int delta;
4006 if (app_cookies) {
4007 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4008 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004009 cur_hdr->len += delta;
4010 } else {
4011 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004012
4013 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004014 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4015 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004016 cur_hdr->len = 0;
4017 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004018 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004019 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004020 }
4021
4022 /* keep the link from this header to next one */
4023 old_idx = cur_idx;
4024 } /* end of cookie processing on this header */
4025}
4026
4027
Willy Tarreaua15645d2007-03-18 16:22:39 +01004028/* Iterate the same filter through all response headers contained in <rtr>.
4029 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4030 */
4031int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4032{
4033 char term;
4034 char *cur_ptr, *cur_end, *cur_next;
4035 int cur_idx, old_idx, last_hdr;
4036 struct http_txn *txn = &t->txn;
4037 struct hdr_idx_elem *cur_hdr;
4038 int len, delta;
4039
4040 last_hdr = 0;
4041
4042 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4043 old_idx = 0;
4044
4045 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004046 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004047 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004048 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004049 (exp->action == ACT_ALLOW ||
4050 exp->action == ACT_DENY))
4051 return 0;
4052
4053 cur_idx = txn->hdr_idx.v[old_idx].next;
4054 if (!cur_idx)
4055 break;
4056
4057 cur_hdr = &txn->hdr_idx.v[cur_idx];
4058 cur_ptr = cur_next;
4059 cur_end = cur_ptr + cur_hdr->len;
4060 cur_next = cur_end + cur_hdr->cr + 1;
4061
4062 /* Now we have one header between cur_ptr and cur_end,
4063 * and the next header starts at cur_next.
4064 */
4065
4066 /* The annoying part is that pattern matching needs
4067 * that we modify the contents to null-terminate all
4068 * strings before testing them.
4069 */
4070
4071 term = *cur_end;
4072 *cur_end = '\0';
4073
4074 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4075 switch (exp->action) {
4076 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004077 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004078 last_hdr = 1;
4079 break;
4080
4081 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004082 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004083 last_hdr = 1;
4084 break;
4085
4086 case ACT_REPLACE:
4087 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4088 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4089 /* FIXME: if the user adds a newline in the replacement, the
4090 * index will not be recalculated for now, and the new line
4091 * will not be counted as a new header.
4092 */
4093
4094 cur_end += delta;
4095 cur_next += delta;
4096 cur_hdr->len += delta;
4097 txn->rsp.eoh += delta;
4098 break;
4099
4100 case ACT_REMOVE:
4101 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4102 cur_next += delta;
4103
4104 /* FIXME: this should be a separate function */
4105 txn->rsp.eoh += delta;
4106 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4107 txn->hdr_idx.used--;
4108 cur_hdr->len = 0;
4109 cur_end = NULL; /* null-term has been rewritten */
4110 break;
4111
4112 }
4113 }
4114 if (cur_end)
4115 *cur_end = term; /* restore the string terminator */
4116
4117 /* keep the link from this header to next one in case of later
4118 * removal of next header.
4119 */
4120 old_idx = cur_idx;
4121 }
4122 return 0;
4123}
4124
4125
4126/* Apply the filter to the status line in the response buffer <rtr>.
4127 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4128 * or -1 if a replacement resulted in an invalid status line.
4129 */
4130int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4131{
4132 char term;
4133 char *cur_ptr, *cur_end;
4134 int done;
4135 struct http_txn *txn = &t->txn;
4136 int len, delta;
4137
4138
Willy Tarreau3d300592007-03-18 18:34:41 +01004139 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004140 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004141 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004142 (exp->action == ACT_ALLOW ||
4143 exp->action == ACT_DENY))
4144 return 0;
4145 else if (exp->action == ACT_REMOVE)
4146 return 0;
4147
4148 done = 0;
4149
Willy Tarreau9cdde232007-05-02 20:58:19 +02004150 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004151 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4152
4153 /* Now we have the status line between cur_ptr and cur_end */
4154
4155 /* The annoying part is that pattern matching needs
4156 * that we modify the contents to null-terminate all
4157 * strings before testing them.
4158 */
4159
4160 term = *cur_end;
4161 *cur_end = '\0';
4162
4163 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4164 switch (exp->action) {
4165 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004166 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004167 done = 1;
4168 break;
4169
4170 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004171 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004172 done = 1;
4173 break;
4174
4175 case ACT_REPLACE:
4176 *cur_end = term; /* restore the string terminator */
4177 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4178 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4179 /* FIXME: if the user adds a newline in the replacement, the
4180 * index will not be recalculated for now, and the new line
4181 * will not be counted as a new header.
4182 */
4183
4184 txn->rsp.eoh += delta;
4185 cur_end += delta;
4186
Willy Tarreau9cdde232007-05-02 20:58:19 +02004187 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004188 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004189 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004190 cur_ptr, cur_end + 1,
4191 NULL, NULL);
4192 if (unlikely(!cur_end))
4193 return -1;
4194
4195 /* we have a full respnse and we know that we have either a CR
4196 * or an LF at <ptr>.
4197 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004198 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004199 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4200 /* there is no point trying this regex on headers */
4201 return 1;
4202 }
4203 }
4204 *cur_end = term; /* restore the string terminator */
4205 return done;
4206}
4207
4208
4209
4210/*
4211 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4212 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4213 * unparsable response.
4214 */
4215int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4216{
Willy Tarreau3d300592007-03-18 18:34:41 +01004217 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004218 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004219 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004220 int ret;
4221
4222 /*
4223 * The interleaving of transformations and verdicts
4224 * makes it difficult to decide to continue or stop
4225 * the evaluation.
4226 */
4227
Willy Tarreau3d300592007-03-18 18:34:41 +01004228 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004229 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4230 exp->action == ACT_PASS)) {
4231 exp = exp->next;
4232 continue;
4233 }
4234
4235 /* Apply the filter to the status line. */
4236 ret = apply_filter_to_sts_line(t, rtr, exp);
4237 if (unlikely(ret < 0))
4238 return -1;
4239
4240 if (likely(ret == 0)) {
4241 /* The filter did not match the response, it can be
4242 * iterated through all headers.
4243 */
4244 apply_filter_to_resp_headers(t, rtr, exp);
4245 }
4246 exp = exp->next;
4247 }
4248 return 0;
4249}
4250
4251
4252
4253/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004254 * Manage server-side cookies. It can impact performance by about 2% so it is
4255 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004256 */
4257void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4258{
4259 struct http_txn *txn = &t->txn;
4260 char *p1, *p2, *p3, *p4;
4261
4262 appsess *asession_temp = NULL;
4263 appsess local_asession;
4264
4265 char *cur_ptr, *cur_end, *cur_next;
4266 int cur_idx, old_idx, delta;
4267
Willy Tarreaua15645d2007-03-18 16:22:39 +01004268 /* Iterate through the headers.
4269 * we start with the start line.
4270 */
4271 old_idx = 0;
4272 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4273
4274 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4275 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004276 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004277
4278 cur_hdr = &txn->hdr_idx.v[cur_idx];
4279 cur_ptr = cur_next;
4280 cur_end = cur_ptr + cur_hdr->len;
4281 cur_next = cur_end + cur_hdr->cr + 1;
4282
4283 /* We have one full header between cur_ptr and cur_end, and the
4284 * next header starts at cur_next. We're only interested in
4285 * "Cookie:" headers.
4286 */
4287
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004288 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4289 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004290 old_idx = cur_idx;
4291 continue;
4292 }
4293
4294 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004295 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004296
4297
4298 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004299 if (t->be->cookie_name == NULL &&
4300 t->be->appsession_name == NULL &&
4301 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004302 return;
4303
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004304 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004305
4306 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004307 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4308 break;
4309
4310 /* p1 is at the beginning of the cookie name */
4311 p2 = p1;
4312
4313 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4314 p2++;
4315
4316 if (p2 == cur_end || *p2 == ';') /* next cookie */
4317 break;
4318
4319 p3 = p2 + 1; /* skip the '=' sign */
4320 if (p3 == cur_end)
4321 break;
4322
4323 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004324 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004325 p4++;
4326
4327 /* here, we have the cookie name between p1 and p2,
4328 * and its value between p3 and p4.
4329 * we can process it.
4330 */
4331
4332 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004333 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004334 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004335 (p4 - p1 >= t->be->capture_namelen) &&
4336 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004337 int log_len = p4 - p1;
4338
Willy Tarreau086b3b42007-05-13 21:45:51 +02004339 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004340 Alert("HTTP logging : out of memory.\n");
4341 }
4342
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004343 if (log_len > t->be->capture_len)
4344 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004345 memcpy(txn->srv_cookie, p1, log_len);
4346 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004347 }
4348
4349 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004350 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4351 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004352 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004353 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004354
4355 /* If the cookie is in insert mode on a known server, we'll delete
4356 * this occurrence because we'll insert another one later.
4357 * We'll delete it too if the "indirect" option is set and we're in
4358 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004359 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4360 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004361 /* this header must be deleted */
4362 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4363 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4364 txn->hdr_idx.used--;
4365 cur_hdr->len = 0;
4366 cur_next += delta;
4367 txn->rsp.eoh += delta;
4368
Willy Tarreau3d300592007-03-18 18:34:41 +01004369 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004370 }
4371 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004372 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004373 /* replace bytes p3->p4 with the cookie name associated
4374 * with this server since we know it.
4375 */
4376 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4377 cur_hdr->len += delta;
4378 cur_next += delta;
4379 txn->rsp.eoh += delta;
4380
Willy Tarreau3d300592007-03-18 18:34:41 +01004381 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004382 }
4383 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004384 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004385 /* insert the cookie name associated with this server
4386 * before existing cookie, and insert a delimitor between them..
4387 */
4388 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4389 cur_hdr->len += delta;
4390 cur_next += delta;
4391 txn->rsp.eoh += delta;
4392
4393 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004394 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004395 }
4396 }
4397 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004398 else if ((t->be->appsession_name != NULL) &&
4399 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004400
4401 /* Cool... it's the right one */
4402
4403 size_t server_id_len = strlen(t->srv->id) + 1;
4404 asession_temp = &local_asession;
4405
Willy Tarreau63963c62007-05-13 21:29:55 +02004406 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004407 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4408 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4409 return;
4410 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004411 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4412 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004413 asession_temp->serverid = NULL;
4414
4415 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004416 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004417 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004418 Alert("Not enough Memory process_srv():asession:calloc().\n");
4419 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4420 return;
4421 }
4422 asession_temp->sessid = local_asession.sessid;
4423 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004424 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4425 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004426 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004427 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004428 }
4429
Willy Tarreaua15645d2007-03-18 16:22:39 +01004430 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004431 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004432 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4433 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4434 return;
4435 }
4436 asession_temp->serverid[0] = '\0';
4437 }
4438
4439 if (asession_temp->serverid[0] == '\0')
4440 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4441
Willy Tarreaud825eef2007-05-12 22:35:00 +02004442 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004443
4444#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004445 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004446#endif
4447 }/* end if ((t->proxy->appsession_name != NULL) ... */
4448 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4449 } /* we're now at the end of the cookie value */
4450
4451 /* keep the link from this header to next one */
4452 old_idx = cur_idx;
4453 } /* end of cookie processing on this header */
4454}
4455
4456
4457
4458/*
4459 * Check if response is cacheable or not. Updates t->flags.
4460 */
4461void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4462{
4463 struct http_txn *txn = &t->txn;
4464 char *p1, *p2;
4465
4466 char *cur_ptr, *cur_end, *cur_next;
4467 int cur_idx;
4468
Willy Tarreau5df51872007-11-25 16:20:08 +01004469 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004470 return;
4471
4472 /* Iterate through the headers.
4473 * we start with the start line.
4474 */
4475 cur_idx = 0;
4476 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4477
4478 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4479 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004480 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004481
4482 cur_hdr = &txn->hdr_idx.v[cur_idx];
4483 cur_ptr = cur_next;
4484 cur_end = cur_ptr + cur_hdr->len;
4485 cur_next = cur_end + cur_hdr->cr + 1;
4486
4487 /* We have one full header between cur_ptr and cur_end, and the
4488 * next header starts at cur_next. We're only interested in
4489 * "Cookie:" headers.
4490 */
4491
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004492 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4493 if (val) {
4494 if ((cur_end - (cur_ptr + val) >= 8) &&
4495 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4496 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4497 return;
4498 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004499 }
4500
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004501 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4502 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004503 continue;
4504
4505 /* OK, right now we know we have a cache-control header at cur_ptr */
4506
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004507 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004508
4509 if (p1 >= cur_end) /* no more info */
4510 continue;
4511
4512 /* p1 is at the beginning of the value */
4513 p2 = p1;
4514
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004515 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004516 p2++;
4517
4518 /* we have a complete value between p1 and p2 */
4519 if (p2 < cur_end && *p2 == '=') {
4520 /* we have something of the form no-cache="set-cookie" */
4521 if ((cur_end - p1 >= 21) &&
4522 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4523 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004524 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004525 continue;
4526 }
4527
4528 /* OK, so we know that either p2 points to the end of string or to a comma */
4529 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4530 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4531 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4532 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004533 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004534 return;
4535 }
4536
4537 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004538 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004539 continue;
4540 }
4541 }
4542}
4543
4544
Willy Tarreau58f10d72006-12-04 02:26:12 +01004545/*
4546 * Try to retrieve a known appsession in the URI, then the associated server.
4547 * If the server is found, it's assigned to the session.
4548 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004549void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004550{
Willy Tarreau3d300592007-03-18 18:34:41 +01004551 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004552 appsess *asession_temp = NULL;
4553 appsess local_asession;
4554 char *request_line;
4555
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004556 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004557 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004558 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004559 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004560 return;
4561
4562 /* skip ';' */
4563 request_line++;
4564
4565 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004566 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004567 return;
4568
4569 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004570 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004571
4572 /* First try if we already have an appsession */
4573 asession_temp = &local_asession;
4574
Willy Tarreau63963c62007-05-13 21:29:55 +02004575 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004576 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4577 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4578 return;
4579 }
4580
4581 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004582 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4583 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004584 asession_temp->serverid = NULL;
4585
4586 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004587 if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004588 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004589 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004590 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004591 Alert("Not enough memory process_cli():asession:calloc().\n");
4592 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4593 return;
4594 }
4595 asession_temp->sessid = local_asession.sessid;
4596 asession_temp->serverid = local_asession.serverid;
Willy Tarreau51041c72007-09-09 21:56:53 +02004597 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004598 }
4599 else {
4600 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004601 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004602 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004603
Willy Tarreaud825eef2007-05-12 22:35:00 +02004604 tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004605 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004606
Willy Tarreau58f10d72006-12-04 02:26:12 +01004607#if defined(DEBUG_HASH)
Willy Tarreau51041c72007-09-09 21:56:53 +02004608 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004609#endif
4610 if (asession_temp->serverid == NULL) {
4611 Alert("Found Application Session without matching server.\n");
4612 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004613 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004614 while (srv) {
4615 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004616 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004617 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004618 txn->flags &= ~TX_CK_MASK;
4619 txn->flags |= TX_CK_VALID;
4620 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004621 t->srv = srv;
4622 break;
4623 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004624 txn->flags &= ~TX_CK_MASK;
4625 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004626 }
4627 }
4628 srv = srv->next;
4629 }
4630 }
4631}
4632
4633
Willy Tarreaub2513902006-12-17 14:52:38 +01004634/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004635 * In a GET or HEAD request, check if the requested URI matches the stats uri
4636 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004637 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004638 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004639 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004640 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004641 *
4642 * Returns 1 if the session's state changes, otherwise 0.
4643 */
4644int stats_check_uri_auth(struct session *t, struct proxy *backend)
4645{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004646 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004647 struct uri_auth *uri_auth = backend->uri_auth;
4648 struct user_auth *user;
4649 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004650 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004652 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004653 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004654 return 0;
4655
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004656 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004657
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004658 /* the URI is in h */
4659 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004660 return 0;
4661
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004662 h += uri_auth->uri_len;
4663 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4664 if (memcmp(h, ";up", 3) == 0) {
4665 t->flags |= SN_STAT_HIDEDWN;
4666 break;
4667 }
4668 h++;
4669 }
4670
4671 if (uri_auth->refresh) {
4672 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4673 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4674 if (memcmp(h, ";norefresh", 10) == 0) {
4675 t->flags |= SN_STAT_NORFRSH;
4676 break;
4677 }
4678 h++;
4679 }
4680 }
4681
Willy Tarreau55bb8452007-10-17 18:44:57 +02004682 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4683 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4684 if (memcmp(h, ";csv", 4) == 0) {
4685 t->flags |= SN_STAT_FMTCSV;
4686 break;
4687 }
4688 h++;
4689 }
4690
Willy Tarreaub2513902006-12-17 14:52:38 +01004691 /* we are in front of a interceptable URI. Let's check
4692 * if there's an authentication and if it's valid.
4693 */
4694 user = uri_auth->users;
4695 if (!user) {
4696 /* no user auth required, it's OK */
4697 authenticated = 1;
4698 } else {
4699 authenticated = 0;
4700
4701 /* a user list is defined, we have to check.
4702 * skip 21 chars for "Authorization: Basic ".
4703 */
4704
4705 /* FIXME: this should move to an earlier place */
4706 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004707 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4708 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4709 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004710 if (len > 14 &&
4711 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004712 txn->auth_hdr.str = h;
4713 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004714 break;
4715 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004716 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004717 }
4718
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004719 if (txn->auth_hdr.len < 21 ||
4720 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004721 user = NULL;
4722
4723 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004724 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4725 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004726 user->user_pwd, user->user_len)) {
4727 authenticated = 1;
4728 break;
4729 }
4730 user = user->next;
4731 }
4732 }
4733
4734 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004735 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004736
4737 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004738 msg.str = trash;
4739 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004740 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01004741 client_retnclose(t, &msg);
Willy Tarreaub2513902006-12-17 14:52:38 +01004742 if (!(t->flags & SN_ERR_MASK))
4743 t->flags |= SN_ERR_PRXCOND;
4744 if (!(t->flags & SN_FINST_MASK))
4745 t->flags |= SN_FINST_R;
4746 return 1;
4747 }
4748
4749 /* The request is valid, the user is authenticate. Let's start sending
4750 * data.
4751 */
4752 t->cli_state = CL_STSHUTR;
4753 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004754 t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub2513902006-12-17 14:52:38 +01004755 t->data_source = DATA_SRC_STATS;
4756 t->data_state = DATA_ST_INIT;
4757 produce_content(t);
4758 return 1;
4759}
4760
4761
Willy Tarreaubaaee002006-06-26 02:48:02 +02004762/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004763 * Print a debug line with a header
4764 */
4765void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4766{
4767 int len, max;
4768 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
4769 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
4770 max = end - start;
4771 UBOUND(max, sizeof(trash) - len - 1);
4772 len += strlcpy2(trash + len, start, max + 1);
4773 trash[len++] = '\n';
4774 write(1, trash, len);
4775}
4776
4777
Willy Tarreau8797c062007-05-07 00:55:35 +02004778/************************************************************************/
4779/* The code below is dedicated to ACL parsing and matching */
4780/************************************************************************/
4781
4782
4783
4784
4785/* 1. Check on METHOD
4786 * We use the pre-parsed method if it is known, and store its number as an
4787 * integer. If it is unknown, we use the pointer and the length.
4788 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004789static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004790{
4791 int len, meth;
4792
Willy Tarreauae8b7962007-06-09 23:10:04 +02004793 len = strlen(*text);
4794 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004795
4796 pattern->val.i = meth;
4797 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004798 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004799 if (!pattern->ptr.str)
4800 return 0;
4801 pattern->len = len;
4802 }
4803 return 1;
4804}
4805
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004806static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004807acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4808 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004809{
4810 int meth;
4811 struct http_txn *txn = l7;
4812
Willy Tarreauc11416f2007-06-17 16:58:38 +02004813 if (txn->req.msg_state != HTTP_MSG_BODY)
4814 return 0;
4815
Willy Tarreau8797c062007-05-07 00:55:35 +02004816 meth = txn->meth;
4817 test->i = meth;
4818 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004819 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4820 /* ensure the indexes are not affected */
4821 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004822 test->len = txn->req.sl.rq.m_l;
4823 test->ptr = txn->req.sol;
4824 }
4825 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4826 return 1;
4827}
4828
4829static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4830{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004831 int icase;
4832
Willy Tarreau8797c062007-05-07 00:55:35 +02004833 if (test->i != pattern->val.i)
4834 return 0;
4835
4836 if (test->i != HTTP_METH_OTHER)
4837 return 1;
4838
4839 /* Other method, we must compare the strings */
4840 if (pattern->len != test->len)
4841 return 0;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004842
4843 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4844 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4845 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau8797c062007-05-07 00:55:35 +02004846 return 0;
4847 return 1;
4848}
4849
4850/* 2. Check on Request/Status Version
4851 * We simply compare strings here.
4852 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004853static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004854{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004855 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004856 if (!pattern->ptr.str)
4857 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004858 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004859 return 1;
4860}
4861
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004862static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004863acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4864 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004865{
4866 struct http_txn *txn = l7;
4867 char *ptr;
4868 int len;
4869
Willy Tarreauc11416f2007-06-17 16:58:38 +02004870 if (txn->req.msg_state != HTTP_MSG_BODY)
4871 return 0;
4872
Willy Tarreau8797c062007-05-07 00:55:35 +02004873 len = txn->req.sl.rq.v_l;
4874 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4875
4876 while ((len-- > 0) && (*ptr++ != '/'));
4877 if (len <= 0)
4878 return 0;
4879
4880 test->ptr = ptr;
4881 test->len = len;
4882
4883 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4884 return 1;
4885}
4886
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004887static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004888acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4889 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004890{
4891 struct http_txn *txn = l7;
4892 char *ptr;
4893 int len;
4894
Willy Tarreauc11416f2007-06-17 16:58:38 +02004895 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4896 return 0;
4897
Willy Tarreau8797c062007-05-07 00:55:35 +02004898 len = txn->rsp.sl.st.v_l;
4899 ptr = txn->rsp.sol;
4900
4901 while ((len-- > 0) && (*ptr++ != '/'));
4902 if (len <= 0)
4903 return 0;
4904
4905 test->ptr = ptr;
4906 test->len = len;
4907
4908 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4909 return 1;
4910}
4911
4912/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004913static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004914acl_fetch_stcode(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.c_l;
4925 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4926
4927 test->i = __strl2ui(ptr, len);
4928 test->flags = ACL_TEST_F_VOL_1ST;
4929 return 1;
4930}
4931
4932/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004933static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004934acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4935 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004936{
4937 struct http_txn *txn = l7;
4938
Willy Tarreauc11416f2007-06-17 16:58:38 +02004939 if (txn->req.msg_state != HTTP_MSG_BODY)
4940 return 0;
4941 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4942 /* ensure the indexes are not affected */
4943 return 0;
4944
Willy Tarreau8797c062007-05-07 00:55:35 +02004945 test->len = txn->req.sl.rq.u_l;
4946 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4947
Willy Tarreauf3d25982007-05-08 22:45:09 +02004948 /* we do not need to set READ_ONLY because the data is in a buffer */
4949 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004950 return 1;
4951}
4952
Willy Tarreauc11416f2007-06-17 16:58:38 +02004953/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4954 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4955 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004956static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004957acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004958 struct acl_expr *expr, struct acl_test *test)
4959{
4960 struct http_txn *txn = l7;
4961 struct hdr_idx *idx = &txn->hdr_idx;
4962 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004963
4964 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4965 /* search for header from the beginning */
4966 ctx->idx = 0;
4967
Willy Tarreau33a7e692007-06-10 19:45:56 +02004968 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4969 test->flags |= ACL_TEST_F_FETCH_MORE;
4970 test->flags |= ACL_TEST_F_VOL_HDR;
4971 test->len = ctx->vlen;
4972 test->ptr = (char *)ctx->line + ctx->val;
4973 return 1;
4974 }
4975
4976 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4977 test->flags |= ACL_TEST_F_VOL_HDR;
4978 return 0;
4979}
4980
Willy Tarreau33a7e692007-06-10 19:45:56 +02004981static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004982acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4983 struct acl_expr *expr, struct acl_test *test)
4984{
4985 struct http_txn *txn = l7;
4986
4987 if (txn->req.msg_state != HTTP_MSG_BODY)
4988 return 0;
4989 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4990 /* ensure the indexes are not affected */
4991 return 0;
4992
4993 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4994}
4995
4996static int
4997acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4998 struct acl_expr *expr, struct acl_test *test)
4999{
5000 struct http_txn *txn = l7;
5001
5002 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5003 return 0;
5004
5005 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5006}
5007
5008/* 6. Check on HTTP header count. The number of occurrences is returned.
5009 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5010 */
5011static int
5012acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005013 struct acl_expr *expr, struct acl_test *test)
5014{
5015 struct http_txn *txn = l7;
5016 struct hdr_idx *idx = &txn->hdr_idx;
5017 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005018 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005019
Willy Tarreau33a7e692007-06-10 19:45:56 +02005020 ctx.idx = 0;
5021 cnt = 0;
5022 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5023 cnt++;
5024
5025 test->i = cnt;
5026 test->flags = ACL_TEST_F_VOL_HDR;
5027 return 1;
5028}
5029
Willy Tarreauc11416f2007-06-17 16:58:38 +02005030static int
5031acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5032 struct acl_expr *expr, struct acl_test *test)
5033{
5034 struct http_txn *txn = l7;
5035
5036 if (txn->req.msg_state != HTTP_MSG_BODY)
5037 return 0;
5038 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5039 /* ensure the indexes are not affected */
5040 return 0;
5041
5042 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5043}
5044
5045static int
5046acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5047 struct acl_expr *expr, struct acl_test *test)
5048{
5049 struct http_txn *txn = l7;
5050
5051 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5052 return 0;
5053
5054 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5055}
5056
Willy Tarreau33a7e692007-06-10 19:45:56 +02005057/* 7. Check on HTTP header's integer value. The integer value is returned.
5058 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005059 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005060 */
5061static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005062acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005063 struct acl_expr *expr, struct acl_test *test)
5064{
5065 struct http_txn *txn = l7;
5066 struct hdr_idx *idx = &txn->hdr_idx;
5067 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005068
5069 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5070 /* search for header from the beginning */
5071 ctx->idx = 0;
5072
Willy Tarreau33a7e692007-06-10 19:45:56 +02005073 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5074 test->flags |= ACL_TEST_F_FETCH_MORE;
5075 test->flags |= ACL_TEST_F_VOL_HDR;
5076 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5077 return 1;
5078 }
5079
5080 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5081 test->flags |= ACL_TEST_F_VOL_HDR;
5082 return 0;
5083}
5084
Willy Tarreauc11416f2007-06-17 16:58:38 +02005085static int
5086acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5087 struct acl_expr *expr, struct acl_test *test)
5088{
5089 struct http_txn *txn = l7;
5090
5091 if (txn->req.msg_state != HTTP_MSG_BODY)
5092 return 0;
5093 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5094 /* ensure the indexes are not affected */
5095 return 0;
5096
5097 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5098}
5099
5100static int
5101acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5102 struct acl_expr *expr, struct acl_test *test)
5103{
5104 struct http_txn *txn = l7;
5105
5106 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5107 return 0;
5108
5109 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5110}
5111
Willy Tarreau737b0c12007-06-10 21:28:46 +02005112/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5113 * the first '/' after the possible hostname, and ends before the possible '?'.
5114 */
5115static int
5116acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5117 struct acl_expr *expr, struct acl_test *test)
5118{
5119 struct http_txn *txn = l7;
5120 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005121
Willy Tarreauc11416f2007-06-17 16:58:38 +02005122 if (txn->req.msg_state != HTTP_MSG_BODY)
5123 return 0;
5124 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5125 /* ensure the indexes are not affected */
5126 return 0;
5127
Willy Tarreau737b0c12007-06-10 21:28:46 +02005128 ptr = txn->req.sol + txn->req.sl.rq.u;
5129 end = ptr + txn->req.sl.rq.u_l;
5130
5131 if (ptr >= end)
5132 return 0;
5133
5134 /* RFC2616, par. 5.1.2 :
5135 * Request-URI = "*" | absuri | abspath | authority
5136 */
5137
5138 if (*ptr == '*')
5139 return 0;
5140
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005141 if (isalpha((unsigned char)*ptr)) {
Willy Tarreau737b0c12007-06-10 21:28:46 +02005142 /* this is a scheme as described by RFC3986, par. 3.1 */
5143 ptr++;
5144 while (ptr < end &&
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005145 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
Willy Tarreau737b0c12007-06-10 21:28:46 +02005146 ptr++;
5147 /* skip '://' */
5148 if (ptr == end || *ptr++ != ':')
5149 return 0;
5150 if (ptr == end || *ptr++ != '/')
5151 return 0;
5152 if (ptr == end || *ptr++ != '/')
5153 return 0;
5154 }
5155 /* skip [user[:passwd]@]host[:[port]] */
5156
5157 while (ptr < end && *ptr != '/')
5158 ptr++;
5159
5160 if (ptr == end)
5161 return 0;
5162
5163 /* OK, we got the '/' ! */
5164 test->ptr = ptr;
5165
5166 while (ptr < end && *ptr != '?')
5167 ptr++;
5168
5169 test->len = ptr - test->ptr;
5170
5171 /* we do not need to set READ_ONLY because the data is in a buffer */
5172 test->flags = ACL_TEST_F_VOL_1ST;
5173 return 1;
5174}
5175
5176
Willy Tarreau8797c062007-05-07 00:55:35 +02005177
5178/************************************************************************/
5179/* All supported keywords must be declared here. */
5180/************************************************************************/
5181
5182/* Note: must not be declared <const> as its list will be overwritten */
5183static struct acl_kw_list acl_kws = {{ },{
5184 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth },
5185 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str },
5186 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str },
Willy Tarreauae8b7962007-06-09 23:10:04 +02005187 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +02005188
Willy Tarreauae8b7962007-06-09 23:10:04 +02005189 { "url", acl_parse_str, acl_fetch_url, acl_match_str },
5190 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg },
5191 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end },
5192 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub },
5193 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir },
5194 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom },
5195 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg },
Willy Tarreau8797c062007-05-07 00:55:35 +02005196
Willy Tarreauc11416f2007-06-17 16:58:38 +02005197 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str },
5198 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg },
5199 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg },
5200 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end },
5201 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub },
5202 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir },
5203 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom },
5204 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int },
5205 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int },
5206
5207 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str },
5208 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg },
5209 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg },
5210 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end },
5211 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub },
5212 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir },
5213 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom },
5214 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int },
5215 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005216
5217 { "path", acl_parse_str, acl_fetch_path, acl_match_str },
5218 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg },
5219 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg },
5220 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end },
5221 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub },
5222 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir },
5223 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom },
5224
Willy Tarreauf3d25982007-05-08 22:45:09 +02005225 { NULL, NULL, NULL, NULL },
5226
5227#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005228 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5229 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5230 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5231 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5232 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5233 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5234 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5235
Willy Tarreau8797c062007-05-07 00:55:35 +02005236 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5237 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5238 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5239 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5240 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5241 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5242 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5243 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5244
5245 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5246 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5247 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5248 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5249 { NULL, NULL, NULL, NULL },
5250#endif
5251}};
5252
5253
5254__attribute__((constructor))
5255static void __http_protocol_init(void)
5256{
5257 acl_register_keywords(&acl_kws);
5258}
5259
5260
Willy Tarreau58f10d72006-12-04 02:26:12 +01005261/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005262 * Local variables:
5263 * c-indent-level: 8
5264 * c-basic-offset: 8
5265 * End:
5266 */